Python Cheatsheet

zhuting
2 min readJun 6, 2023
Photo by benjamin lehman on Unsplash

This article serves as my personal notes while reading the book: Python for Everybody: Exploring Data in Python 3, capturing key points and insights for future reference. As a developer transitioning from another language, I have omitted several details due to the similarities between the languages. Instead, I have listed only the unfamiliar parts that require my attention.

1.6 Interpreter and compiler

2.3 Python reserves 35 keywords:

and, continue, finally, is, raise, as, def, for, lambda, return, assert, 
del, from, None, True, async, elif, global, nonlocal, try, await, else,
if, not, while, break, except, import, or, with, class, False, in, pass,
yield

2.8 Modulus operations

>>> 7//3
2

2.10 Asking the user for input

>>> user_input = input()
what time is it?
>>> print(user_input)
what time is it?

3 Conditional execution

3.7 Catching exception using try and except

try:
fahr = float(inp)
cel = (fahr - 32) * 5 / 9
print(cel)
except:
print("Please enter a number')

4 Functions

Built-in functions

max() min() type() len() int() float() str()
math.sin() math.sqrt() math.pi

max

5 Iteration

// while statement
while n > 0:
print(n)
n = n -1
print('Blastoff!')

// Definite loops using for
friends = ['A', 'B', 'c']
for friend in friends:
print('Happy learning', friend)

6 Strings

A string is a sequence of characters.

fruit = 'banana'
len(fruit)

// the operator [a:b] returns the part of the string from index a to b,
// including the first but excluding the last

s = 'Monty Python'
print(s[0:5]) // Monty
print(s[:3])
print(s[3:])

Strings are immutable.

--

--