Example Code Flashcards
How do you do whole number division?
answer = num1 // num2
How do you do a line break?
\n
How to find the length of a variable
len(word)
Removes extra characters (in this case spaces) from the start and end of a string
text = “ This is some text. ”
print(text.strip(“ ”))
How to display certain letters in a word?
print (“Hello world”[7:10])
Changes the string into upper case.
word.upper()
Changes the string into lower case.
word.lower()
Changes a phrase so that every word has a capital letter at the beginning with the rest of the letters in the word in lower case
word.title()
To the power of
**
When using some math code what do we need to have at the start of the program?
import math
The square root of a number
math.sqrt(num)
but you must have the line import math
Gives you pi (π) to 15 decimal places
math.pi
you must have the line import math
Finds the remainder
x % y
Count up from 1 to 9
for i in range(1,10):
print(i)
counts up from 1 to 9 in 2s
for i in range(1,10,2):
print(i)
Counts down from 10 to 1 in 3s
for i in range(10,1,-3):
print(i)
Prints every letter of a variable on each line
for i in word:
print(i)
Not equal to
!=
What should you write at the top when starting to write code that uses random
import random
Selects a random floating-point number between 0 and 1 and stores it in a variable called “num”
num = random.random()
Selects a random whole number between 0 and 9 (inclusive)
num = random.randint(0,9)
Picks a random value from the options “red”, “black” or “green” and stores it as the variable “colour”
colour = random.choice([“red”,“black”,“green”])
Picks a random number between the numbers 0 and 100 (inclusive) in steps of five
num = random.randrange(0,100,5)
Creates a variable name called “fruit_tuple” which stores four pieces of fruit within it
fruit_tuple = (“apple”,“banana”,“strawberry”,“orange”)
Displays the index (i.e. the numeric key) of the item “strawberry”
print(fruit_tuple.index(“strawberry”))
Displays item 2 from “fruit_tuple”, in this case “strawberry”.
print(fruit_tuple[2])
Creates a list of the names and stores them in the variable “names_list”
names_list = [“John”,“Tim”,“Sam”]
Deletes item 1 from “names_list”
del names_list[1]
Asks the user to enter a name and will add that to the end of “names_list”.
names_list.append(input(“Add a name: “))
Sorts name_list into alphabetical order and saves the list in the new order.
names_list.sort()
Displays names_list in alphabetical order but does not change the order of the original list, which is still saved in the original order.
print(sorted(names_list))
Creates a dictionary called “colours”, where each item is assigned an index of your choosing.
colours = {1:“red”,2:“blue”,3:“green”}
Makes a change to the data stored in position 2 of the colours dictionary. In this case it will change “blue” to “yellow”.
colours[2] = “yellow”
Displays the length of the list (i.e. how many items are in the list).
print(len(x))
This will display data in positions 1, 2 and 3.
print(x[1:4])
print the items in a list on separate lines.
for i in x:
print(i)
Inserts the number 420 into position 2 and pushes everything else along to make space
x.insert(2,420)
Deletes an item from the list.
x.remove(892)
Adds the number 993 to the end of the list.
x.append(993)
If their input is fully CAPS
if msg.isupper()
If their input is fully lower
if msg.islower()