Show Menu
Cheatography

Python Course - Day 1 Cheat Sheet (DRAFT) by

Basic cheat sheet for Python Introduction

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Arithmetic Operators

x + y
Addition, adds x to y
x - y
Substr­action, substract y from x
x / y
Division, divide x by y
x * y
Multip­lic­ation, multiply x by y
x // y
Floor division, division operation that returns the largest integer that is less than or equal to the result of the division
x % y
Modulo, find the remainder after dividing x by y (26 % 5 = 1))
x**y
Expone­nti­ation, calculate a x to the power of y

Comparison Operators

x == y
is x equal to y?
x != y
is x not equal to y?
x > y
is x greater than y?
x < y
is x lower than y?
x >= y
is x greater than or equal to y?
x <= y
is x lower than or equal to y?

Logical Operators

not
opposite Boolean value
and
both Boolean value must be True
or
at least one Boolean value must be True

Membership Operators

substring in my_string
checks if substring appears in my_string

Snippets - Strings

# Slicing - variable[start:stop:step]
my_text = "abcdefgh"
print(my_text[3:6]) # Output: "def"
print(my_text[3:6:2]) # Output: "df"
print(my_text[::]) # Output: "abcdefgh"
print(my_text[::-1]) # Output: "hgfedcba"

# Concatenation
my_text = "Hello" + " " + "World"
really_happy = "I am happy" + "!" * 5

# Formatting
name = "Alice"
age = 25

print(f"My name is {name} and I am {age} years old.") # Output: "My name is Alice and I am 25 years old."
 

String operations

len(s)
Returns length of s
s.coun­t(s­ubs­tring)
Returns how many times substring appears in s
s.find­(su­bst­ring)
Returns the index of the first occurence of substring in s, or -1 if not found
s.lower()
Returns s with all characters to lowercase
s.upper()
Returns s with all characters to uppercase
s.strip()
Returns s with leading and trailing whitespace removed
s.capi­tal­ize()
Returns s with the first letter capita­lized
s.repl­ace­(old, new)
Returns a string in which occurences of old in s are replaced by new
s.spli­t(d­eli­miter)
Returns a list by splitting s when a delimiter
separa­tor.jo­in(­text1, text2, ..., textn)
Returns elements of an iterable into a single string with a separator string
s.star­tsw­ith­(pr­efix)
returns True if s starts with the specified prefix
s.ends­wit­h(s­uffix)
returns True if s ends with the specified suffix
s.isal­pha()
returns True if all characters in s are alphabetic
s.isal­pha­num()
returns True if all characters in s are alphan­umeric
s.isdi­git()
returns True if all characters in s are digits

Data Types

int
scalar
Integer, represent a number from -2'147­'48­3'648 to 2'147'­483'647
float
scalar
Float, represent a floating point number
bool
scalar
Boolean, represent either True or False
None
scalar
NoneType, represent an empty object
str
non-scalar
String, represent a chain of characters
tuple
non-scalar
Tuple, represent an immutable and ordered collection of data

Snippets - Loops

# for - range(start, stop, step)
for i in range(10):
	print(i) # Output: "0", "1", ..., "9"

# for - in
my_text = "parse"
for c in my_text:
	print(c) # Output: "p", "a", "r", "s", "e"

# while
continue = True
i = 0
while(continue):
	if i >= 3:
		continue = False
	else:
		print(i) # Output: "0", "1", "2"