Comparison Operators & Statements Flashcards

1
Q

Control Flow Tools:

break (def + ex.)

continue (def + ex.)

pass (def + ex.)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Assignment Operators

(=, +=, -=, *=, /=, %=, **=, //=)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Boolean Comparison Operators

(==, !=/<>, >, >=, <=)

A
  • 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) + =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Logical Operators: Chaining Comparison Operators (3)

A

and, or, not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Identity & Membership Operators

(4)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

for loop generic syntax

how to utilize underscores in for loops

A

( 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’ )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

( 1 ) for loop list comprehension

( 2 ) if else statements in list

( 3 ) nested loop, nested loop list comprehension

A

( 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] ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

if, elif, else statement generic example

A

if some_condition:

execute code

elif another_condition:

do something different

else:

neither the other conditions, execute code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

example of iterating throught packed data structures and accessing items within it

( tuples )

( dictionaries )

A

( 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly