indexing
making and printing the list:
listFood = [“chocolate”, “cheese”, “fruit”]
print(listFood[1])
slicing (start, stop, step)
start stop step refers to the positions
[start:stop:step]
start = the index number the index begins
stop = the index number the index ends
step = the jump range
append()
to add an item to the end of a list:
listNames.append(“sally”)
insert()
to insert an item into a specific place on the list:
listNames.insert(2, “elisa”)
remove()
to remove an item from a list:
listNames.remove(“penelope”)
pop()
takes out an item at a certain index in a list:
listNames.pop(3)
del list[]
deletes permanently the item at a particular index:
del listNames[4]
sort()
sorts a list into ascending order:
listNames.sort()
len()
prints out the number of terms in a list:
print(len(listNames))
index()
returns the number where the item appears in the list:
print(listNames.index(“freya”))
using “in”
“in” is used to say if something is in the list then something else should happen:
if “elsa” in listNames:
validation - range
user is asked to input something between a certain range:
numRange = int(input(“Please enter a number between 11 and 18”))
validation - lookup
user is asked to input something from a store of data:
country = input(“Please enter a country from the list above”)
“==”
to compare terms:
if item == “a”:
nested for loops
a loop inside a loop:
x = [1, 2]
y = [4, 5]
for i in x:
for j in y:
print(i, j)
when do you use a nested loop?
if you have multiple lists that you want to do something with, or if you have a list within a list (a nested list)
def functionName():
the way of naming a function:
def addTwoNumbers():
print(2+2)
Formal parameters
items in brackets which are variables:
def addTwoGivenNumbers(num1 + num2):
print(num1+num2)
Actual parameters
items in brackets which are actual things:
addTwoNumbers(6+7):
print(6+7)
return functions
instead of printing, it is returning something:
def returnAdditionOfTwoNumbers(num1+num2):
return num1+num2