2.2.3 Additional Programming Techniques Flashcards

1
Q

Name two ways to manipulate strings [2]

A

Concatenation - combining strings [1]
Slicing - taking part of a string [1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What would be the output of this code?

name = “Dave”
print (name[0:2])

A

“Da”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a record? [2]

A

A set of data [1] spanning multiple fields [1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you declare an array in Python?

A

listName = []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a 2D array? [1]

A

An array that contains other arrays. [1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would you access the first item of the second array in a 2D Python array called data?

A

data [1][0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you access the third item of an array called data in Python?

A

data[2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you access the last item of an array called data in Python?

A

data[-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write the code to open a file in Python

A

file = open(“filename”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write the code to read one line of a file in Python

A

file.readline()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write the code to read a whole file in Python

A

file.read()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write the code to close a file in Python

A

file.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write the code to declare the function ‘findPrice’ with the parameters price and VAT.

A

def findPrice (price, VAT):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write the Python to create an array called letters containing the letters a, b, c, d

A

letters = [‘a’,’b’,’c’,’d’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write the Python code to generate a random number between 1 and 100

A

import random # import the random library
number = random.randint(1,100) #Generate a number between 1 - 100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

State the two types of subroutines [2]

A

Functions - return a value
Procedures - don’t return a value

17
Q

What is the difference between a local and global variables? [2]

A

Local variables are defined within a subroutine. They can only be accessed in that subroutine [1]

Global variables are defined outside of a subroutine. They can be accessed anywhere in the code. [1]