287x Filetype PDF File size 0.13 MB Source: groklearning-cdn.com
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: ⾠ two equals signs, not one
print('x is 3') Convert string to integer Convert integer to string
Decide between two blocks Are two values not equal? int('365') str(365)
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 x > 3 print(i) ⾠ 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') x <= 3 total = 0
elif mark >= 50: 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
‣elif can be used without else Repeat a block over a string
The answer is a Boolean: for c in 'Hello': range(10, 0, -1)
‣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
print('howdy') msg = 'I grok Python!'
Count a character in a string for i in range(len(msg)):
Less than another string? print(i, msg[i])
if msg < 'n': msg.count('l')
print('a-m') Replace a character or string Putting it together: Celsius to Fahrenheit converter
else:
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 celsius = int(input('Temp. in Celsius: '))
at a time (lexicographic order) 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(fahrenheit, '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)
return [f(self.for
else:
return funcs(sel Learn more in Intro. to Programming @ groklearning.com
n__(self):
no reviews yet
Please Login to review.