Python Equations Flashcards
Print integers from 12 to 15
for i in range(12,16):
print(i)
12
13
14
15
Print by 2s between 0 and 8
for i in range(0,10,2):
print(i)
0
2
4
6
8
Print 5 random numbers between 1 and 10
import random
for i in range(5):
print(random.randint(1,10))
Use for loop to print numbers 1 through 10
for i in range(1,11):
print(i)
Use while loop to print numbers 1 to 10
i=0
while i <10:
i=i+1
print(i)
Write a function that will say “Hello Jay”
def hello(name): print('Hello '+name)
hello(‘Jay’)
Create a function to divide 42 by
2
12
0
1
with exception handling
def spam(divideBy): try: return 42 / divideBy except ZeroDivisionError: print('Error: Invalid Argument.')
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
What are the differences in exception handling:
try:
except:
code
vs
try:
code
except:
try/except/code will run through all of the code
try/code/except will stopon the first error
Print all items in a list name supplies
for i in range(len(supplies)):
print(supplies[i])
The len() of the list really helps
Example of reference in a list in code
Spam = Cheese
What happens when you change something in Cheese
spam=[0,1,2,3,4,5]
cheese=spam
cheese[1]=’Hello’
spam
[0, ‘Hello’, 2, 3, 4, 5]
cheese
[0, ‘Hello’, 2, 3, 4, 5]
Example of raw string?
print(r’That is Carol's cat.’)
That is Carol's cat.
The r makes it a raw string and ignores the \ as an escape charcter and instead prints it.
Example of a multiline string?
print(‘'’Dear Alice,
Eve’s cat has been arreseted for cat napping, cat burglary and extortion.
Sincerly,
Bob’’’)
Example of a walk thru a file tree?