Python Equations Flashcards

1
Q

Print integers from 12 to 15

A

for i in range(12,16):
print(i)

12

13

14

15

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

Print by 2s between 0 and 8

A

for i in range(0,10,2):
print(i)

0

2

4

6

8

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

Print 5 random numbers between 1 and 10

A

import random
for i in range(5):
print(random.randint(1,10))

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

Use for loop to print numbers 1 through 10

A

for i in range(1,11):
print(i)

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

Use while loop to print numbers 1 to 10

A

i=0
while i <10:
i=i+1
print(i)

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

Write a function that will say “Hello Jay”

A
def hello(name):
 print('Hello '+name)

hello(‘Jay’)

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

Create a function to divide 42 by

2

12

0

1

with exception handling

A
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))

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

What are the differences in exception handling:

try:

except:

code

vs

try:

code

except:

A

try/except/code will run through all of the code

try/code/except will stopon the first error

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

Print all items in a list name supplies

A

for i in range(len(supplies)):
print(supplies[i])

The len() of the list really helps

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

Example of reference in a list in code

Spam = Cheese

What happens when you change something in Cheese

A

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]

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

Example of raw string?

A

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.

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

Example of a multiline string?

A

print(‘'’Dear Alice,
Eve’s cat has been arreseted for cat napping, cat burglary and extortion.

Sincerly,
Bob’’’)

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

Example of a walk thru a file tree?

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