Coding Problems - Modules 1-6 Flashcards

1
Q

Write an expression that evaluates to True if x is even

A

x%2==0

the remainder of x/2 is equal to 0

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

Write a ‘while loop’ that counts down from 5 to 1, then prints blastoff!

A
n=5
while n>0:
\_\_\_\_print(n)
\_\_\_\_n-=1
print("blastoff!")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List the 3 different ways to divide

A

integer (floor) division //
regular division /
modulus % (returns remainder ONLY)

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

How do you determine the data type of a value?

A

type(value)

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

What mathematical operations can you perform on strings?

A

+ and *

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

What happens if you put a semi-colon at end of statement? What about a period?

A

Semi-colon - nothing

Period - syntax error

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

When can you use return statements?

A

ONLY inside functions

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

How to convert to string, float, and integer?

A
str(32)
'32'
float(32)
32.0
int(32.3)
32
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Print(x,y) and print(x+y)

A

x_y vs. xy
comma will default to space separator
to change this, print(x,y,sep=”\n”)

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

Write a function called do_twice that takes a function object as an argument and calls itself twice

A

def do_twice(f):
____f()
____f()

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

By default, print advances to the next line. How do you override that behavior?

A

print(“yes”,end=” “)
print(“no”)
yes no

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

How to break up a long line of code without any errors

A

print(“let’s practice”,\
“breaking this up”)
let’s practice breaking this up

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