Comparison Operators & Statements Flashcards
Control Flow Tools:
break (def + ex.)
continue (def + ex.)
pass (def + ex.)
utilized within loops for additional functionality
( 1 )
break: breaks out of the closest enclosing loop
n_string = “sammy”
for letter in n_string:
if letter == ‘a’ :
break
print ( letter )
returns only s then stops running
( 2 )
continue: goes to the top of the closest enclosing loop
n_string = “sammy”
for letter in n_string:
if letter == ‘a’ :
continue
print ( letter )
returns all letters except a in sammy
( 3 )
pass: does nothing, used to create empty functions for later use
Assignment Operators
(=, +=, -=, *=, /=, %=, **=, //=)
- equal: c = a + b, c has value of a + b
and operators perform and operation on a variable and then assign the new value to the original value
used often in for loops
- add and: c += a, equil. c = c + a
- minus and: c -= a, equil. c = c - a
- multi. and: c *= a, equil. c = c * a
- divide and: c /= a, equil. c = c / a
- modulus and
- exponent and
- floor dividision and
Boolean Comparison Operators
(==, !=/<>, >, >=, <=)
- equals: True if both values are equal
- not equal: True is both values are not equal
- greater than: True if value if greater
- less than: True if value if value less
- greater than or equal to: (see sbove) + =
- less than or equal too: (see sbove) + =
Logical Operators: Chaining Comparison Operators (3)
and, or, not
Identity & Membership Operators
(4)
Identity Operators
is, is not
Membership Operators
in, not in
Example:
check in see if value in list
2 in [3 , 4 , 5] # returns False
2 not in [3 , 4 , 5] # returns True
for loop generic syntax
how to utilize underscores in for loops
( 1 )
my_iterable = [item_1, item_2, item_3]
for i (could be anything) in my_iterable:
do some code
( 2 )
when you don’t plan to use the variable name in the iteration
example below: print ‘some term’ for num of items in my_iterable
for _ in my_iterable:
print( ‘some term’ )
( 1 ) for loop list comprehension
( 2 ) if else statements in list
( 3 ) nested loop, nested loop list comprehension
( 1 )
traditional for loop
for element in var_name:
do something
within list
var_name = [# do something for element in var],
[# do something for element in var if # condition]
( 2 )
var_name = [var if # condition else # condition for i in var]
( 3 )
standard for loop
for x in [2, 4, 6] :
for y in [1, 10, 100] :
list.append( x * y )
nested example
var = [x * y for x in [ 2, 4, 6] for y in [1, 10, 100] ]
if, elif, else statement generic example
if some_condition:
execute code
elif another_condition:
do something different
else:
neither the other conditions, execute code
example of iterating throught packed data structures and accessing items within it
( tuples )
( dictionaries )
( 1 )
my_list = [( 1 , 2 ) , ( 3 , 4 ) , ( 5 , 6 )]
for a , b in my_list:
print ( a )
this function only prints the first item from each tuple
( 2 )
my_dict = { ‘k1’ : 1 , ‘k2’ : 2 , ‘k3’ : 3 }
for key , value in my_dict.items( ):
print ( value )
prints only the value, print ( key ) returns keys, print ( key , value ) return both,
for item in my_dict.items( ):
print ( item )
returns tuple of key value pairs