r = 10
p = 3.14 * 2 * r
10
62.8
r
p
# Now we have basic types and variables, we can do stuff with them
x = 1
y = 2
x + y - 2 # This is an expression, the intepretter
# has to "evaluate it" to figure out the value by
# combining variables (x, y), values (2) and operators (+, -)
# When we're calling a function (here the "type" function),
# we're also evaluating an expression
type(3.14)
# Statements are instructions to the intepreter
x = 1 # This is a statement, your telling Python, make x refer to the value
# 1
if x > 0: # Conditionals (like if (and others we'll see soon,
# are also "statements"))
print("boo")
Symbol | Meaning | Precedence |
---|---|---|
+ | Addition | Low |
- | Subtraction | Low |
* | Multiplication | Medium |
/ | Division | Medium |
// | Floor Division | Medium |
% | Remainder (mod) | Medium |
** | Exponentiation | High |
5 + 10 + 12 # We can use "+" to add together strings of numbers
# Some arithmetic operators are also "overloaded" to work with strings
"This" + "is" + "contrived" # The '+' concatenates strings
# Note, this doesn't work because Python doesn't know how to add a string and
# a number together
"this" + 5
# How to fix this?
x = int("25")
x = int(34.287)
x = int(True)
x = float("34.287")
x = float(12)
x = float(False)
x = str(34.287)
x = str(12)
x = str(True)
x = bool("text")
x = bool(0)
x = bool(34.287)
# Remember everything in Python has a type, discoverable with "type()"
# What's the type of x?
x = 12
y = 5
type(x)
type(y)
# So what's the type of our expression result?
type(x // y)
type(x / y) # And this one ?
type(x + 5.0)
>
>=
<
!=
==
and
or
<=