python recap Flashcards
Choosing which lines of code to execute based on a condition
Example:
num = int(input(“Enter a number”))
if num == 5:
print(“Correct”)
else:
print(“Nope”)
For loops
for counter in range(5):
print(“This message prints 5 times”)
While loops
answer = “”
while answer != “London”:
answer = input(“What is the capital of England?”)
—
num = 1
while num <= 5:
print(num)
num +=1
Used to store multiple data items under a single identifier (variable name)
Examples:
emptyList = []
myList = [“Bananas”, “Apples”, “Pears”]
myList.append(“Grapes”)
numList = [3,6,9,12]
print(myList)
print(myList[2])
print(len(myList))
myList.remove(“Apples”)
Example Procedure:
Subroutines
def sayHello():
print(“Hello how are you?”)
def sayHello(name):
print(“Hello”, name, “how are you?”)
sayHello()
sayHello(“Billy”)
Example Function:
Subroutines
def add(num1,num2):
answer = num1 + num2
return num2
answer = add(4,9)
print(answer)