Show Menu
Cheatography

Python Basics Cheat Sheet by

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, ...):
    statements
    return expr

Boolean Operators

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

List Operations

list.append(a)
Append a to list
list.insert(i, a)
Insert a before ith item in list
list.sort()
Sort list
list.pop(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(expr)       Converts expr to integer
str(expr)       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[start : end : step ]
slice s from start (included) to end (excluded) with increment step
s + t
concat­enate s with t

Statements

If Statement
if expr :
    statements
elif expr :
    statements
else:
    statements

While Loop
while expr :
    statements

For Loop
for var in collection :
    statements

Counting For Loop
for i in range(start, end, step):
    statements
 

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.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter