Cheatography
https://cheatography.com
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(input("How old are you?"))
|
Add a new line name = input("What is your name?\n")
|
Concatenate strings print("Hello "+ name )
|
Concatenate different data types print("You are",age ,"years old")
|
Output the first letter print(name[0])
|
Uppercase/Lowercase format print(name.upper()) | print(name.lower())
|
Uppercase then lowercase format print(name[0].upper()+name[1:].lower())
|
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.insert(1, "OCR")
|
Add to the end of a list mylist.append("GCSE")
|
Remove all Sciences mylist.remove("Science)
|
Delete all items mylist = []
|
Delete third item del mylist[2]
|
Reverse list order mylist.reverse()
|
Sort list order mylist.sort()
|
Join items using a space print(" ".join(mylist))
|
Dictionaries
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,items()
|
|
|
Inequalities
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("Owen")
_________________
Repeat length of string
for x in "Nathan":
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(input("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("Filename.txt","w")
|
Add data to the text file myfile.write("Hello World")
|
Reads entire file into one string myfile.read()
|
Reads first 4 characters into one string myfile.read(4)
|
Reads one line of a file myfile.readline()
|
Reads entire file into a list of strings, one per line myfile.readlines()
|
Steps through lines in a file for eachline in myfile:
|
Close the file myfile.close()
|
("w" write, "r" read, "a" append)
.csv will create a spreadsheet
|
|
Demonstrating knowledge
Annotate / Comment on a line of code #short comments
|
Annotate / comment on a block of code '''for longer comments'''
|
Variables
Creating a variable name = "Josh"
|
Length of string len(name)
|
Print a variable print(name)
|
Coverting [String↔Integer] str(age) 'or' int(age)
|
All variables are strings [str] by default
Numbers
Addition 2+2
|
Subtraction 4-1
|
Multiplication 3*5
|
Division 15/10
|
To the power of... 1.5**2
|
Multiplying A String "Ethan" *14
|
Numbers follow the BIDMAS / BODMAS rule
Creating And Accessing Lists
Creating a list mylist = ["Computer", "Science", 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 "Computer" in mylist
|
Concatenate mylist + ["a"]
|
Remove third item and use mylist.pop(2)
|
Remove last item and use mylist.pop()
|
Find position in the list mylist.index(Science)
|
Count appearances mylist.count(17)
|
Add values sum(mylist)
|
Length of List len(mylist)
|
Compare lists cmp(mylist, list)
|
Biggest number in list max(mylist)
|
Smallest number in list min(mylist)
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets