1/21
Looks like no tags are added yet.
Common For Loop Formats
for i in range(stop, start, step)
for i in range(stop, start)
for i in range(stop)
String Iteration Using For Loops
Format?
Function
Format: for character in “string”:
Function: loops through every character in a string
When should you use while loops? How do you avoid creating an infinite loop?
When you want to a loop to run as long as a condition is True.
Ex: run this loop while counter < 1
Make sure the condition eventually becomes False to avoid an infinite loop.
True or False: You can usually rewrite for loops to be while loops.
True
If/Elif/Else
Indention matters! Make sure the code controlled by an if, elif, or else structure is appropriately indented under that structure!
If/Elif/Else
Indention matters! Make sure the code controlled by an if, elif, or else structure is appropriately indented under that structure!
True or False: Once a condition is met, it will not check the other elif(s) or else statements attached to it.
True. When an if statement is executed, any elif(s) or else structures attached to it will not run.
Ex:
a = 5
if a == 5:
print(“a = 5”)
elif b == 4:
print(“b = 4”)
else:
print(“if and elif didn’t run”)
Output:
a = 5
The initial condition you are checking. If met, it will run the code indented under it.
If
another condition that you are checking given that “If” was False
Elif
The code that runs given that none of the other conditions are true.
Else
Do these data conversions always work or only sometimes work?
Anything —> String
Anything —> Bool
Bool —> Anything
String —> Int
String —> Float
Float —> Int
Int —> Float
Anything —> String (str())
Always
Anything —> Bool (bool())
Always
““, 0, & 0.0 —> False
Anything else is True
Bool —> Anything
Always
str(True) —> “True”, str(False) —> “False”
float(True) —> 1.0, float(False) —> 0.0
int(True) —> 1, int(False) —> 0
String —> Int
Sometimes, string must be only numbers w/ no decimal points
int(“1.0”) —> ValueError, int(“Dog”) —> ValueError
String —> Float
Sometimes, string must be only #s and have at most 1 decimal point
float(“Dog”) —> ValueError
Float —> Int
Always, rounds down to nearest int
int(-2.8) —> -2, int(1.4) —> 4, int(1.8) —> 1
Int —> Float
Always, adds .0 to end of the int
float(2) —> 2.0
Logic Operations: Both conditions must be true
And
(T & T —> T); (F & F —> F); (T & F —> F)
Logic Operations: Either condition must be true
Or
(T or T —> T); (F or T —> T); (F or F —> F)
“Is equal to”
==
“Not equal to”
!=
Which circumstances elicit a NameError?
using a variable that is undefined within its scope
Which circumstances elicit a TypeError?
using a variable in an inappropriate operation
aString = “hello” / print(aString + 2)
print(“Hello” / 2)
Which circumstances elicit a ZeroDivisionError?
Trying to divide by 0
print(len(variable/name) / 0)
print(0 / 0)
Which circumstances elicit a ValueError?
giving a method an inappropriate value
print(int(“Five"))
Describe Error Handling Format
try:
#Code to run goes here
except ValueError:
#Code to run if there is a Value Error
except ZeroDivisionError:
#Code to run if there is a ZeroDivisionError
except:
#Code to run if there are ANY other errors
else:
#Code to run if there are NO errors
finally:
#Code to run no matter what
Rewrite this for loop as a while loop:
for i in range(0, 11):
print(i)
i = 0
while i < 11:
print(i)
i += 1
Write the output of each line of code?
adding_function(1, 2)
print(adding_function(1, 2))
added_variable = adding_function(1, 2)
adding_function(1, 2) —> returns 3, but does nothing w/ it here
print(adding_function(1, 2)) —> prints 3 to the console
added_variable = adding_function(1, 2) —> gives added_variable the value 3
Important: don’t forget the difference between returning and
printing within a method!
What is Returning?
the value that calling the function will give