Show Menu

Python Basics Cheat Sheet by

python

Python IDLE Shell

1. Press the Windows Key
2. Type "­Python IDLE"

How to create a new Python file

1. In the Python Shell, click File Tab
2. Click New File

Print Statements

print("Hello")
Hello
print(4+5)
9

Function Definition

def name (arg1, arg2, ...):
    state­ments
    return expr

Boolean Operators

x is y
x not y
x and y
x or y

List Operations

list.ap­pen­d(a)
Append a to list
list.in­ser­t(i, a)
Insert a before ith item in list
list.sort()
Sort list
list.po­p(i)
Remove i th item in list
 

Data Types

Integer
-256, 15
String
"­Hel­lo", "­app­le"
Boolean
True, False
List
[ value, ... ]
Tuple
( value, ... )

Conversion Functions

int(e­xpr)       Converts expr to integer
str(e­xpr)       Converts expr to string

Arithmetic Operators

x + y
add
x - y
subtract
x * y
multiply
x / y
divide
x % y
modulus
x ** y
xy

Compar­ators

x < y
Less
x <= y
Less or equal
x > y
Greater
x >= y
Greater or equal
x == y
Equal
x != y
Not equal
 

String / List / Tuple Operations

len(s)
length of s
s[i]
ith item in s (starts from 0)
s[s­tart : end : step ]
slice s from start (included) to end (excluded) with increment step
s + t
concat­enate s with t

Statements

If Statem­ent
if expr :
    state­ments
elif expr :
    state­ments
else:
    state­ments

While Loop
while expr :
    state­ments

For Loop
for var in colle­ction :
    state­ments

Counting For Loop
for i in range(­st­art, end, step):
    state­ments

Download the Python Basics Cheat Sheet

1 Page
//media.cheatography.com/storage/thumb/sachua_python-basics.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.