|
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())
|
|
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))
|
|
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()
|
|
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"
|
|
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()
|
|
Annotate / Comment on a line of code
#short comments
|
|
Annotate / comment on a block of code
'''for longer comments'''
|
|
Creating a variable
name = "Josh"
|
|
Length of string
len(name)
|
|
Print a variable
print(name)
|
|
Coverting [String↔Integer]
str(age) 'or' int(age)
|
|
Addition
2+2
|
|
Subtraction
4-1
|
|
Multiplication
3*5
|
|
Division
15/10
|
|
To the power of...
1.5**2
|
|
Multiplying A String
"Ethan" *14
|
|
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)
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.