Save
Computer science
Coding
Save
Share
Learn
Content
Leaderboard
Share
Learn
Created by
Nazor
Visit profile
Cards (10)
Generating a random number
Import
random
print(“Roll the dice”)
number =
random.randint
(1,6)
#First number is the
lowest
number that can be generated
#Last number is the
highest
number it can generate
print(“You got”,number)
#Prints out
randomly
generated
number
one dimensional array
#------One dimensional array------#
#Defines a list with the variable name you give it
#Words must be put in
quotation
marks
animals = ["dog","cat","lion","mice"]
#Numbers
do
not
need to be put in quotation marks
numbers = [1,2,3,4]
Positions in lists
#---------Positions in lists--------#
#Position 1 in the list is known as 0
print(numbers[0])
#Position 2 = the animal
third
in the array
print(animals[2])
#Prints animals in position
1
and
2
but
not
3
print(animals[1:3])
#Prints numbers from position
1
and
onwards
print(numbers[1:])
#Prints animals up to position
3
but
not
including
position 3
print(animals[:3])
Editing elements in a list
#------Editing elements in a list-------#
#current list of animals
print(animals)
#modifies the first value in the list
animals[0
] = "monkey"
print()
print(animals)
#Letting the user choose which position they would like to replace
choice1 = int(input("Which position in the list would you like to edit?" ))
#User chooses their animal in replacement
choice2 = input("Which animal would you like instead? ")
animals[choice1] = choice2
#Prints out the new list
print(animals)
Adding and removing from a list
#List of students
students = ["Nazor","Zara","Obi"]
#append adds a student to the list
students.append(
"Patience")
print(students)
#remove takes away a student from the list
students.remove
(students[2])
print(students)
#List of scores
scores = [34,56,23,72]
#prints specific student and score. Converts the
score
to a
string
as you can't add a
string
to
integer
print(students[0]+" has "+str(scores[2])+" points.")
Merging lists
students = ["Nazor","Zara","Obi"]
visitors = ["Tobi","Elise","Rachel"]
#extends the student list by adding the visitors
students.extend
(visitors)
#prints out the length of the students list (number of students in the list)
print(
len
(students))
#Prints out the last student from the list. Minus numbers count from the end of the list
print(students[
-1
])
#Prints out the
last
3
students in the list
print(students[-3:])
Searching lists
#----------Searching lists----------#
#list of people
people = ["jack","kim","tony","mike"]
#searches a list
#While true loop used so that the program does not
end
after we have searched for one student
while True:
name = input("Student search: ")
if name in people:
print("They are in my list.")
else:
print("They are not in my list")
print()
print()
Sorting a list
#-----------Sorting a list-----------#
#colour list
colourlist = ["red","green","blue","yellow","purple","black","orange"]
print(colourlist)
#sorts colours into alphabetical order
#need the word sorted infront of the list you want to sort
print(
sorted(colourlist
))
Random from a list
#always need this
import random
numbers = [1,2,3,4,5,6,7,8,9,10]
#Randomly choses a number from the list
choice =
random.choice
(numbers)
print(choice)
2 dimensional arrays
scores = [
["Jane", 23,69,72],
["Bob",12,62,45],
["Toni",78,96,56],
]
#Prints out the first element of the list
print(scores[0])
#Number in the first bracket is the row you are looking for
#Number in the second bracket is the column you are looking for
#Will print out
first
column,
first
row which is the name
print(scores[0][0])
#If we want Jane's second test result:
print(scores[0][
2
])