You are on page 1of 1

Python 3 cheatsheet (the basics) GROK

LEARNING
Interact with the user (input and output) Text (strings) Variables
Print a message Single quoted Creating a variable
print('Hello, world!') 'perfect' celsius = 25
Print multiple values (of different types) Double quoted Using a variable
ndays = 365 "credit" celsius*9/5 + 32
print('There are', ndays, 'in a year')
Multi-line
Asking the user for a string
'''Hello,
name = input('What is your name? ') World!''' Whole numbers (integers)
Asking the user for a whole number (an integer) Add (concatenate) strings Addition and subtraction
num = int(input('Enter a number: ')) 'Hello' + 'World' 365 + 1 - 2
Multiply string by integer Multiplication and division
Decide between options 'Echo...'*4 25*9/5 + 32
Decide to run a block (or not) Are two values equal? Length of a string Powers (2 to the power of 8)
x = 3 x == 3 len('Hello') 2**8
if x == 3:
print('x is 3') two equals signs, not one Convert string to integer Convert integer to string
Are two values not equal? int('365') str(365)
Decide between two blocks
mark = 80 x != 3
if mark >= 50: Less than another? Repeat a block (a fixed number of times)
print('pass')
else: x < 3 Repeat a block 10 times Count from 0 to 9
print('fail')
Greater than another? for i in range(10): range(10)
Decide between many blocks print(i)
x > 3 range starts from 0 and goes
mark = 80 Sum the numbers 0 to 9 up to, but not including, 10
if mark >= 65: Less than or equal to?
print('credit') total = 0
elif mark >= 50: x <= 3 for i in range(10): Count from 1 to 10
print('pass') Greater than or equal to? total = total + i range(1, 11)
else: print(total)
print('fail') x >= 3 Count from 10 down to 1
Repeat a block over a string
elif can be used without else The answer is a Boolean: range(10, 0, -1)
for c in 'Hello':
elif can be used many times True or False print(c) Count 2 at a time to 10
Keep printing on one line range(0, 11, 2)
String manipulation for c in 'Hello': Count down 2 at a time
print(c, end=' ')
Compare two strings Convert to uppercase print('!') range(10, 0, -2)
msg = 'hello' msg.upper() Repeat a block over list (or string) indices
if msg == 'hello':
also lower and title msg = 'I grok Python!'
print('howdy')
Count a character in a string for i in range(len(msg)):
Less than another string? print(i, msg[i])
msg.count('l')
if msg < 'n':
print('a-m') Replace a character or string
else: Putting it together: Celsius to Fahrenheit converter
print('n-z') msg.replace('l','X')
Ask the user for a temperature in degrees Celsius
strings are compared character Delete a character or string
at a time (lexicographic order)
celsius = int(input('Temp. in Celsius: '))
msg.replace('l','')
Is a character in a string? Calculate the conversion
'e' in msg Is the string all lowercase? fahrenheit = celsius*9/5 + 32
Is a string in another string? msg.islower() Output the result
'ell' in msg also isupper and istitle print(farenheit, 'Fahrenheit')
s = [datetime.
hs.insert(0, lamb

f __init__(self, format
self.format = format

def __getitem__(self, i):


funcs = self._months[i]
if isinstance(i, slice)

Learn more in Intro. to Programming @ groklearning.com


return [f(self.for
else:
return funcs(sel

n__(self):