Show Menu

Coding Cheat Sheet by

-python

Input & Output

Output a message to the user
print(­"­Hello There")
Ask the user for a text input (string)
name = input(­"What is your name?")
Ask the user for a number input (Integer)
age = int(in­put­("How old are you?"))
Add a new line
name = input(­"What is your name?­\n")
Concat­enate strings
print(­"­Hello "+ name )
Concat­enate different data types
print(­"You are",age ,"years old")
Output the first letter
print(­nam­e[0])
Upperc­ase­/Lo­wercase format
print(­nam­e.u­pper()) | print(­nam­e.l­ower())
Uppercase then lowercase format
print(­nam­e[0­].u­ppe­r()­+na­me[­1:].lo­wer())

IF Statements

IF
x = 3
if x == 3:
print(­"x = 3")
______­___­___­______
IF / ELSE
x = 7
if x > 8:
print(­"­Yes­")
else:
print(­"­No")
______­___­___­______
ELIF
x = 4
if x > 8:
print(­"­A")
elif x > 5:
print(­"­B")
elif x > 2:
print(­"­C")
else:
print(­"­F")
Remember to indent your code [Press the 'Tab' button n the keyboard]

Editing Lists

Replace third item
mylist[2] = 43
Insert in position
mylist.in­sert(1, "­OCR­")
Add to the end of a list
mylist.ap­pen­d("G­CSE­")
Remove all Sciences
mylist.re­mov­e("S­cience)
Delete all items
mylist = []
Delete third item
del mylist[2]
Reverse list order
mylist.re­verse()
Sort list order
mylist.sort()
Join items using a space
print(­" ".jo­in(­myl­ist))

Dictio­naries

Creating a dictionary
mydict = {"a":1, "­b":2, "­c":3}
Returns the value of b
mydict­["b"]
Check in dictionary
"­c" in mydict
Display the keys
mydict.keys()
Display keys and items
mydict­,it­ems()
 

Inequa­lities

Equal to
x == 3
Not equal to
x != 4
Less than / equal to
x <= 2
More than / equal to
x >= 1
Between two numbers
<= 3 x <= 12
AND
x == 1 and z == 4
OR
x == "­A" or x == "­a"

For Loop

Repeat 5 times
for x in range (5):
print(­"­Owe­n")
______­___­___­_____
Repeat length of string
for x in "­Nat­han­":
print(x)
______­___­___­_____
Count from 1 to 10
for x in range (1,11):
print(x)
______­___­___­_____
Count from 1 to 10 in 2's
for x in range (1,11,2):
print(x)
Repeats code a predefined number of times

While Loops

Repeat until false
while True:
print(­"­Hello There!­")
______­___­___­___­_____
Using 'break' to end the loop
while True:
x = input(­"Say Yes")
if x == "­Yes­":
break
______­___­___­___­_____
Until x is more than 100
x = 0
while x < 100:
x = int(in­put­("x ?"))
A while loop will repeat infinitely until the program or user input tells it to stop
Repeats code until a condition is met [within the program]

Creating And Using Files

Create document
myfile = open("F­ile­nam­e.t­xt",­"­w")
Add data to the text file
myfile.wr­ite­("Hello World")
Reads entire file into one string
myfile.read()
Reads first 4 characters into one string
myfile.re­ad(4)
Reads one line of a file
myfile.re­adl­ine()
Reads entire file into a list of strings, one per line
myfile.re­adl­ines()
Steps through lines in a file
for eachline in myfile:
Close the file
myfile.cl­ose()
("w" write, "­r" read, "­a" append)
.csv will create a spread­sheet
 

Demons­trating knowledge

Annotate / Comment on a line of code
#short comments
Annotate / comment on a block of code
'''for longer commen­ts'''
Always annotate you code

Variables

Creating a variable
name = "­Jos­h"
Length of string
len(name)
Print a variable
print(­name)
Coverting [Strin­g↔I­nteger]
str(age) 'or' int(age)
All variables are strings [str] by default

Numbers

Addition
2+2
Subtra­ction
4-1
Multip­lic­ation
3*5
Division
15/10
To the power of...
1.5**2
Multip­lying A String
"­Eth­an" *14
Numbers follow the BIDMAS / BODMAS rule

Creating And Accessing Lists

Creating a list
mylist = ["Co­mpu­ter­", "­Sci­enc­e", 17]
Output full list
print(­mylist)
Access first item
mylist[0]
Access first to second
mylist­[1:2}
Access third item to the end
mylist[2:]
Access up to the third item
mylist[:2]
Check for in list
"­Com­put­er" in mylist
Concat­enate
mylist + ["a"]
Remove third item and use
mylist.pop(2)
Remove last item and use
mylist.pop()
Find position in the list
mylist.in­dex­(Sc­ience)
Count appear­ances
mylist.co­unt(17)
Add values
sum(my­list)
Length of List
len(my­list)
Compare lists
cmp(my­list, list)
Biggest number in list
max(my­list)
Smallest number in list
min(my­list)

Download the Coding Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/j2003j_coding.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.