Operators, Control Statements, and Loops
Assignment cont.
- Python is sort-of pointer-y
- example:
x = [] y = x y.append("frog") print(x) #["frog"] print(y) #["frog"]
Operators
- Arithmetic
+
,-
,/
,*
/
<- floating-point division- 5/2 -> 2.5
- 5.5/3.3 -> 1.66667
//
<- floored quotient**
- power+=
,-=
,/=
,//=
,*=
,**=
-
There is no
++
nor--
- relational
- ==,!=,>=,<=,<,>
- logical
not
-> like ! (there is no ! in Python)-
or
-> like and
-> like &&
- membership operator
in
- returns
True
orFalse
x in y
returnsTrue
if x is a member in y, otherwise returnsFalse
- Example:
x = [ 1,2,3,4 ] 4 in x #True 50 in x #False y = {'a':10,'b':50} 'a' in y #True 50 in y #False, 50 is not a key 50 in y.values() #True
- is
- Returns
True
orFalse
- Tests object identity
- Don’t use it unless you have a good reason to.
- not the same as
==
- okay to use to compare with
None
- Pyflakes recommends using
is
withTrue
andFalse
- don’t
- Returns
Control Statements
if
/elif
/else
- Example:
if x == 5: stuff() #called the body elif x == 6: stuff() else: stuff()
- No parentheses around expression
- don’t forget colons
- no curly braces -> watch your indentation
- Python is whitespace delimited
- Example:
Truthiness/Falsiness
- Falsey values
- False
- None
- numeric zero
- ””
- empty built-in data structures
- Truthy values
- True
- not Falsey
bool()
bool(truthy) #True
bool(falsey) #False
- be careful when comparing against True and False using
==
1 == True #True
[] == False #False
bool([]) == False #True
- aside from numeric types, objects of different types do not compare equal
- Prime test material
Loops
while
- pre-check loop
- no post-check loop
- leave parentheses off of expression
- don’t forget the colon
- pre-check loop
for
- different than c++
- Iterable -> can iterate with
for
- Iterator object can yield/return its members one at a time
- File objects are iterable
for <var> in <iterable>:
for x in range():
- leave parentheses off of expression
- don’t forget the colon
- break
- breaks out of the innermost loop
- continue
- continues to next iteration of loop
- pass <- placeholder
- else
- executes if loop completes uninterrupted