python code Flashcards
print a message
print(‘hello, world’)
print multiple values (of different types)
ndays=365
print(‘there are’,ndays,’in a year’)
asking the user for a string
name=input(‘what is your name?’)
asking the user for a whole number (integer)
num=int(input(‘enter a number’))
creating a variable
celsius=25
using a variable
celcius*9/5+32
powers
2 to the power of 8
2**8
decide to run a block (or not)
x=3
if x==3:
print(‘x is 3’)
decide between two blocks
mark=80 if mark>=50: print('pass') else: print('fail')
decide between many blocks
mark=80 if mark>= 65: print('credit') elif mark>=50: print('pass') else: print('fail')
equal
==
not equal
!=
less than/greater than
< / >
less than or equal to/greater than or equal to
<= / >=
repeat a block 10 times
for i in range(10):
print(i)
sum the numbers 0 to 9
total=0
for i in range(10):
total=total+1
print(total)
count from 0 to 9
range(10)
count from 1 to 10
range(1,11)
count from 10 down to 1
range(10,0,-1)
count 2 at a time to 10
range(0,11,2)