Python Flashcards
What is an interpreter?
Converts one statement at a time
What is a compiler?
Converts all the statements at once
What is an identifier?
A variable
What are identifier rules?
Must start with a letter or an underscore
The rest can be letters, numbers or underscores
No other characters are permitted
How to create your turtle in Python?
import turtle
leo = turtle.Turtle()
leo.shape(“turtle”)
leo.color(“blue”)
How to move your turtle?
leo.forward(100)
How to move in multiple directions?
leo.left(120) #This rotates the turtle 120 degrees to the left
leo.forward(100)
How to fill an enclosed shape?
leo.fillcolor(“red”)
leo.begin_fill()
#write your turtle code here
leo.end_fill()
Example of an if elif statement?
x = ”cat”
if x == ”dog”:
print(“x is a dog”)
elif x == ”pig”:
print(“x is a pig”)
elif x == ”cat”:
print(“x is a cat”)
else:
print(“x isn’t an animal”)
How to use match statements?
sport = ”basketball”
match sport:
case ”basketball”:
print(“sport is basketball”)
case ”volleyball”:
print(“sport is volleyball”)
case ”rugby”:
print(“sport is rugby”)
case _:
print(“sport is not known”)
How to enter name?
name = str(input(“Please enter your name:”))
How to enter integer?
name = int(input(“Please enter your age:”))
Example of a while statement?
x = 1
while x <=10:
print(x)
x += 1
What is a list?
Instead of
x = 1
y = 2
z = 3
Use
x = [1, 2, 3]
What is range?
Returns a series of outputs. First number is included but not last, eg range(11) would give [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Alternatively, range(1,10) lists numbers 1 to 9. Used in “for” loops
Example of a “for” loop?
for x in range (1,6):
print(x)
How to put text in “for” loops?
for x in [‘cat’, ‘dog’, ‘horse’]:
print(x)
How to put a loop in a loop?
for num1 in range (1, 11):
for num2 in range (1, 11):
print(num1, “ + “, num2, “ = “,
num1 + num2)
Using ‘for’ loops in turtle?
for x in range (4):
leo.forward(200)
leo.left(90)
Using “while” loops in turtle?
x = 0
while (x < 3):
leo.forward(150)
leo.left(120)
How to change Turtle width, change turtle colour and background colour?
turtle.color(“yellow”)
turtle.bgcolor(“black”)
turtle.width(“12”)
Circle in turtle?
leo.circle(150)
Functions?
def welcome_player():
name = input(“Enter a username: “) …
After all your code is done, call upon the function, like
welcome_player()
How to return a certain letter of a certain word?
def print2(value):
print(value[1])
print2(“Superman”)
This will return “u”
Slicing?
x = “Superman”
print(x[2:5])
Would return the string per
Length of string?
len(“Fichy”)
would return “5”
How to slice from the start?
X = “MrFich”
print(x[2:])
This would return “Fich”
How to slice from the back?
x = “MrFich”
print(x[-2:])
This would return “ch”
How to remove the start and end?
return str[1:len(str)-1]
What is a list and how do you access a value of the list?
myList = [“Alpaca”, “Badger”, “Cat”, “Dog”]
Use myList[0] to get Alpaca
Use myList[-1] to get Dog
How to use len() in a list?
len(myList)