Show Menu
Cheatography

Python Basics1 Cheat Sheet (DRAFT) by

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

Hello World in Python

print(­"­Hello World!­")

Python Comments

Example 1
 #This is a comment 
Example 2
 #This is a comment 
#written in
#more than just one line
Example 3
 "­"­"   This is a comment 
written in
more than just one line "­"­"

Python Variables

  x = "How old are you ?" #x is of type str  
print(x)
>>> How old are you ?

x = 25 #x now is of type int
print(x)
>>> 25

Python 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
Get the data type of a variable "­var­"
type(var)
 

Python Data Types Examples

Example
Data Type
x = "­­Co­lor­"
list
x = 1
int
x = 1.2
float
x = 2j
complex
x = ["Bl­­ue­"­,­"­­Red­­"­,­"­Ye­­llo­­w"]
list
x = ("Bl­ue",­"­Red­"­,"Ye­llo­w")
tuple
x = range(8)
range
x={"­Age­"­:25­,"He­igh­t":1.72}
dict
x = {"Pi­nk",­"­Red­"}
set
x = frozen­set­({"P­ink­"­,"Re­d"})
frozenset
x = True
bool
x = b"Co­lor­"
bytes
 x = bytear­ray(8) 
bytearray
 x = memory­vie­w(b­yte­s(8)) 
memoryview
Get the data type of x :
memoryview
 x = "­Col­or" 
print(­typ­e(x))
>>> str

Python Casting

Casting is used to specify a type on to a variable and this is done using constr­uctor functions.
Examples
x = int(5)
          #x = 5
x = int(2.8)
     ­ #x = 2
x = float(5)
     ­ #x = 5.0
x = float(2.8)
  #x = 2.8