Coding Problems - Modules 1-6 Flashcards
Write an expression that evaluates to True if x is even
x%2==0
the remainder of x/2 is equal to 0
Write a ‘while loop’ that counts down from 5 to 1, then prints blastoff!
n=5 while n>0: \_\_\_\_print(n) \_\_\_\_n-=1 print("blastoff!")
List the 3 different ways to divide
integer (floor) division //
regular division /
modulus % (returns remainder ONLY)
How do you determine the data type of a value?
type(value)
What mathematical operations can you perform on strings?
+ and *
What happens if you put a semi-colon at end of statement? What about a period?
Semi-colon - nothing
Period - syntax error
When can you use return statements?
ONLY inside functions
How to convert to string, float, and integer?
str(32) '32' float(32) 32.0 int(32.3) 32
Print(x,y) and print(x+y)
x_y vs. xy
comma will default to space separator
to change this, print(x,y,sep=”\n”)
Write a function called do_twice that takes a function object as an argument and calls itself twice
def do_twice(f):
____f()
____f()
By default, print advances to the next line. How do you override that behavior?
print(“yes”,end=” “)
print(“no”)
yes no
How to break up a long line of code without any errors
print(“let’s practice”,\
“breaking this up”)
let’s practice breaking this up