2.2.3 additional programming techniques Flashcards
Name two ways to manipulate strings [2]
Concatenation - combining strings [1]
Slicing - taking part of a string [1]
What would be the output of this code?
name = “Dave”
print (name[0:2])
“Da”
What is a record? [2]
A set of data [1] spanning multiple fields [1]
How do you declare an array in Python?
listName = []
What is a 2D array? [1]
An array that contains other arrays. [1]
How would you access the first item of the second array in a 2D Python array called data?
data [1][0]
How would you access the third item of an array called data in Python?
data[2]
How would you access the last item of an array called data in Python?
data[-1]
Write the code to open a file in Python
file = open(“filename”)
Write the code to read one line of a file in Python
file.readline()
Write the code to read a whole file in Python
file.read()
Write the code to close a file in Python
file.close()
Write the code to declare the function ‘findPrice’ with the parameters price and VAT.
def findPrice (price, VAT):
Write the Python to create an array called letters containing the letters a, b, c, d
letters = [‘a’,’b’,’c’,’d’]
Write the Python code to generate a random number between 1 and 100
import random # import the random library
number = random.randint(1,100) #Generate a number between 1 - 100