Python 3 cheatsheet (the basics), Lecture notes of Printing

msg = 'I grok Python!' for i in range(len(msg)): print(i, msg[i]). Repeat a block over list (or string) indices for i in range(10): print(i).

Typology: Lecture notes

2021/2022

Uploaded on 07/05/2022

paul.kc
paul.kc šŸ‡¦šŸ‡ŗ

4.7

(68)

1K documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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

Partial preview of the text

Download Python 3 cheatsheet (the basics) and more Lecture notes Printing in PDF only on Docsity!

GROK

LEARNING

celsius = int(input('Temp. in Celsius: ')) Ask the user for a temperature in degrees Celsius fahrenheit = celsius*9/5 + 32 Calculate the conversion print(fahrenheit, 'Fahrenheit') Output the result

Putting it together: Celsius to Fahrenheit converter

'e' in msg Is a character in a string? 'ell' in msg Is a string in another string? msg.upper() Convert to uppercase msg.count('l') Count a character in a string msg.replace('l','X') Replace a character or string msg.replace('l','') Delete a character or string if msg < 'n': print('a-m') else: print('n-z') Less than another string? strings are compared character at a time ( lexicographic order ) msg = 'hello' if msg == 'hello': print('howdy') Compare two strings

String manipulation

also lower and title msg.islower() Is the string all lowercase? also isupper and istitle range(10) Count from 0 to 9 range(1, 11) Count from 1 to 10 range(10, 0, -1) Count from 10 down to 1 range(0, 11, 2) Count 2 at a time to 10 range(10, 0, -2) Count down 2 at a time for c in 'Hello': print(c) Repeat a block over a string for c in 'Hello': print(c, end=' ') print('!') Keep printing on one line msg = 'I grok Python!' for i in range(len(msg)): print(i, msg[i]) Repeat a block over list (or string) indices for i in range(10): print(i) Repeat a block 10 times total = 0 for i in range(10): total = total + i print(total) Sum the numbers 0 to 9

Repeat a block (a fixed number of times)

range starts from 0 and goes up to, but not including, 10 x == 3 Are two values equal? True False The answer is a Boolean : or x != 3 Are two values not equal? x < 3 Less than another? x > 3 Greater than another? x <= 3 Less than or equal to? x >= 3 Greater than or equal to? mark = 80 if mark >= 65: print('credit') elif mark >= 50: print('pass') else: print('fail') Decide between many blocks ‣elif can be used without else ‣elif can be used many times mark = 80 if mark >= 50: print('pass') else: print('fail') Decide between two blocks x = 3 if x == 3: print('x is 3') Decide to run a block (or not)

Decide between options

two equals signs, not one

Addition and subtraction 259/5 + 32 Multiplication and division 2* Powers ( 2 to the power of 8 ) str(365) Convert integer to string

Whole numbers ( integers )

celsius = 25 Creating a variable celsius*9/5 + 32 Using a variable

Variables

'perfect' Single quoted "credit" Double quoted 'Hello' + 'World' Add ( concatenate ) strings 'Echo...'* Multiply string by integer len('Hello') Length of a string int('365') Convert string to integer '''Hello, World!''' Multi-line

Text ( strings )

print('Hello, world!') Print a message name = input('What is your name? ') Asking the user for a string num = int(input('Enter a number: ')) Asking the user for a whole number (an integer) ndays = 365 print('There are', ndays, 'in a year') Print multiple values (of different types)

Interact with the user ( input and output )

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 n_(self):

Learn more in Intro. to Programming @ groklearning.com

Python 3 cheatsheet (the basics)