Show Menu
Cheatography

Python - Baisc Usage & Syntax Cheat Sheet (DRAFT) by

Basic Python usage & syntax

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

Comment

comment - single line
# this is a single line comment
comment - multiline comments
"­"­" this is multi lines comments "­"­"

Variables Declar­ation

integer
studen­ts_­count = 1000
decimal
rating = 4.99
boolean
is_pub­lished = False
string - single line
course­_name = "­Pyt­hon­"
string - multiple lines
course­_name = "­"­" Multiple Lines "­"­"
string concat­enation
full_name = first_name + " " + last_name
formatted string
full_name = f"{f­irs­t_name} {last_­nam­e}"
assign multiple variables
x, y, z = "­Joh­n", "­Ale­x", "­May­"
one value to multiple variables
x = y = z = "­Val­ue"
uppack
animals = ["mo­nke­y", "­cat­", "­dog­"] x, y, z = animals
boolean - capitalise the first letter
single line - can use either single or double quote
multiple lines - use triple double quote to wrap the entire strings
formatted string - use 'f' and curly braces with the double quotes wrap with the separated strings

Output Variables

text output
print(­"­Hello World")
variable output
print(x + y + x)

Global Variable

def myfunc(): 
   global x 
   x = " John"
myfunc()
print("Hello " + x)

Data Types

text Type: str
numeric Types: int, float, complex
sequence Types: list, tuple, range
mapping Type: dict
set Types: set, frozenset
boolean Type: bool
binary Types: bytes, bytearray, memoryview
none Type: NoneType

Collection Types

List - like an array
fruits = ['apple', 'orange', 'banana']
Dictionary - key value pair
fruit = {'name­'='­apple, 'colour: 'red'}
Tuples - order is unchan­gable
fruit = ('apple', 'orange', 'banana')
Set - set items are unchan­gable
fruit = {'apple', 'orange', ;banana'}

If - else

 

while loop

 

for loop

 

function

 
 

Built-in function

check type of variable
x = 2.8
print(­typ­e(x))
<class 'float­'>
casting
int(), float(), str()
x = int(3)
x will be 3
input
username = input(­"­Enter your name")
user type "­Joh­n"
username = "­Joh­n"

Strings

slicing strings
a = "­Hello World"
print(­a[2:7])
'llo W'
from index to the end
 
print(­a[2:])
'llo World'
from start to index
 
print(­a[:8])
'Hello Wo'
from negative to negative
-2 is not included 'd'
print(­a[-­7:-2])
'o Wor'

String Methods

upper case
print(­"­Hello World".u­pp­er())
"­HELLO WORLD"
lower case
print(­"­Hello World".l­ower()
"­hello world"
trim white space (leading & trailing)
print(­" No Space ".strip
"No Space"
replace string
print(­"­Hello John".r­epl­ace­("Jo­hn",­"­Ale­x"))
"­Hello Alex"
split string
print(­"­Hello, World".s­pl­it(­"­,")
return ["He­llo­", "­Wor­ld"]

String Concat­enation

use '+' operator
"­Hello " + "­Wor­ld"
"­Hello World"
use join()
"­".jo­in(­["Hello "­," World]­"
"­Hello World"
use format() - convert number to text
age = 18"  text = "I am {} years old"  print(­tex­t.f­orm­at(­age))
"I am 18 years old"
"+' operator cannot combine string and number
{} is a placeh­older

Formatted String

# formatted string
first = "Mosh"
last = "Hamedani"
full = f"{first} {last}"
print(full)

Number

There are three types of number: integer, float & complex number
x = 1   # integer
x = 1.1  # float
x = 1 + 2j  # a + bi    # complex number

print(10 + 3)  # addition
print(10 - 3)  # subtraction
print(10 * 3)  # multiplication
print(10 / 3)  # division outcome with float
print(10 // 3)  # division outcome with integer
print(10 % 3)  # modulus, the remainder of a division
print(10 ** 3)  # exponent, the power