3.2 Programming Flashcards

1
Q

1) What are the 5 mathematical operators in python and their symbols?
2) What is modulus in python?

A

1) + Addition
- Subtraction
* Multiplication
/ Division (With decimal)
// Division (Without decimal/ integer result)
2) Modulus returns the remainder of a division
It is written as MOD, eg. 5MOD2. It can also be written as %, eg. 5%2

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

1) What are the four different data types?
2) What is type casting?
3) How do you cast an integer, a string and a float?

A

1) String, ie. text
Int, ie. integer/ whole number
Float, ie. numbers with decimals
Boolean, ie. returns true or false
2) Type casting is changing one data type to another.
3) To cast an integer (change the variable to an integer) you use int().
To cast a string, you use str()
To cast a float, you use float()

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

1) What does rounding in python return, and what is the function used?
2) What is random in python, and what is the random function in python?
3) What is a variable?

A

1) Rounding in python returns a value to the nearest integer. The function used is round()
round(number, digits), eg. round(5.76543, 2), This round 5.76543 to 2 decimal places. number: Required. The number to be rounded digits: Optional. The number of decimals to use when rounding the number. Default is 0.
2) Random is a pre written function in python to generate random numbers. First, it has to be imported at the top of the code as ‘import random’.
Then use the random.randint(x,y) function to generate a random integer. (Numbers x and y are both included)
3) A variable is a named piece of memory that holds a value.

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

1) What does iteration mean?
2) What are for loops, and how would you code a for loop to repeat a piece of code 5 times in python?
3) How would you put steps in a for loop?
4) What are while loops?

A

1) Iteration means that a sequence of instructions or code is being repeated until a specific end result is achieved.
2) For loops are loops that are repeated a specific number of times (count controlled iteration). To repeat a piece of code 5 times, you would write,
‘for i in range(5):’
3) x = range(3, 8, 2)
for n in x:
print(n)
This prints: 3, 5, 7
The third parameter in range shows how large the steps are.
4) While loops are loops that are repeated until a specific condition is met (condition controlled iteration). They are written like this:
while boolean condition:
Do this

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

1) What is a list?
2) How is a list created, and how do you create a list that cannot be changed?
3) How do you print an item from a list?
4) How do you change an item in a list to another value?

A

1) A list stores more than one piece of data with the same variable name.
2) A list is created using square brackets.
eg. names = [“Mary”, “Sean”, “Atif”]
To create a list with values that cannot be changed, you use round brackets.
eg. names = (“Mary”, “Sean”, “Atif”)
3) You print an item from a list like this:
print(players[2])
This prints item 3 (the index starts from 0)
4) To change an item you do this:
players[0] = “Bill”
Changes the item in index 0 to “Bill”

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

In python:
1) How do you add an item to a list?
2) How do you remove an item from a list with a specific value?
3) How do you insert an item into a list?
4) How do you remove an item from a list with a specific index?

A

1) listName.append(“Dave”)
2) listName.remove(“Sean”)
3) listName.insert(2, “Julia”)
The first argument is for the index, and the second argument is the value.
4) listName.pop(1)
You insert the index of the item that you want to remove.
If no parameters are entered (eg. listName.pop() ), then it removes the last value from the list.

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

1) What is selection?
2) What is an if statement?
3) What is an elif statement?
4) What is an else statement?

A

1) Selection means that the section of code will run only if the condition is met.
2) An if statement is a statement that will run if the condition is true.
3) An ‘elif’ statement can be used if the if statement is not true, but gives another condition that is needed to run.
4) An ‘else’ statement will only run if all the other if and elif statements are false.

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

1) What is a function in python?
2) How is a subroutine declared in python?
3) What does it mean to ‘return’ data, and how is data returned in python?
4) What are arguments, and how do you put arguments into a function?

A

1) A function is a named section of code that performs a specific task. A function is created by defining it. A function can be used by calling it.
2) In python, a subroutine is declared using the ‘def’ keyword.
eg. def myFunction():
3) A function can return (send) some data back to the main program. The ‘return’ keyword is used to return data from the function to the main program.
eg. def number():
num1 = 10
return num1

num2 = number()
4) Arguments are data that can be put into a function, and are put into the function using brackets. Arguments are like variables used by the function.

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

1) What is a string?
2) How do you return the number of letters in a string?
3) What is string concatenation?

A

1) Any data put within quotation marks is a string.
2) You use the len() function. The len function can be used as a parameter so the code more efficient, eg. print(len(“Hello”))
3) String concatenation ‘+’: adds two strings together.

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

1) What is slicing strings, and how do you slice strings in python?
2) What are the functions to turn python strings into upper and lower case, and capitalise each word?
3) What are the functions to check if python strings are upper case, lower case, or each word is capitalised?
4) What are the functions to convert a character into a unicode number, and convert a unicode number into a character?

A

1) Slicing strings returns a range of characters from a string.
For example, print(b[2:5]) gets the characters from position 2 to 5 (including 2 but not including 5)
2) .upper() returns all the characters in a string as upper case.
.lower() returns all the characters in a string as lower case.
.title() capitalises the first letter of each word.
3) .isUpper() checks if all the characters in a string are upper case.
.isLower() checks if all the characters in a string are lower case.
.isTitle() checks if the first letter of each word is capitalised.
4) ord() returns the unicode number of a specific character
chr() returns the character for a specific unicode

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

1) What is a subroutine?
2) What are the two types of subroutines?
3) Explain the two types of subroutines

A

1) A subroutine is a named, self-contained section of code that performs a specific task. It can return one or more values, but it doesn’t have to.
2) There are two different types of subroutines: procedures and functions.
A procedure is a type of subroutine where nothing is returned: it just carries out the instructions and goes back to the next instruction after the call statement.
A function is a type of subroutine that always return a value.

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

1) What is a scope within a program?
2) What is a local variable?
3) What is a global variable?

A

1) The scope of a variable is where the variable can be accessed within a program, and can either be local or global. If it is a local scope, then it can only be accessed inside the function. If it is a global scope, then the function can be accessed anywhere in the main program.
2) A local variable is a variable that is assigned inside a function. They have a local scope, so it can only be accessed by that function and it only exists while the function is executing.
3) A global variable is a variable that is assigned outside of a function (it is assigned in the main program). They have a global scope, so they can be accessed anywhere in the program.

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

1) Why do we use local variables?
2) What does the ‘global’ keyword do, and should you be using it?

A

1) The main reason is to save memory. Local variables keep functions or subroutines self contained, so they can be used in any program. It also avoids confusion. Programs will be easier to read and debug as there is no need to reference variables outside the procedure.
2) The use of the global keyword within a function tells the function that the variable is referring to the global one outside of the function, instead of creating a separate local variable. However, using the ‘global’ keyword is generally bad practice, and should only be used when necessary. Using global variables can cause problems in larger programs because it will be more difficult to keep track of the variables.

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

1) What is exception handling?
2) What is used in python to write exception handlers?
3) How does it work?

A

1) Exception handling is writing code to prevent a program from crashing.
2) You would use try/except in python to write exception handlers.
3) This is how the try/except/else/finally works:
The try block lets you test a block of code for error.
The except block lets you handle the error.
The else block lets you execute code when there is no error.
The finally block lets you execute code regardless of the result of the try and except blocks.

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

1) How do you open a file in python?
2) What are modes, and what are the 3 mode options, and what happens if the program doesn’t exist?
3) What is the read and readline function in python?
4) What is the readlines function in python?

A

1) myFile = open(“nameOfFile.txt”, “w”)
2) Modes tell the program how to open the file
The three mode options are:
“r” - Read: default. Opens the file for reading, error if file does not exist.
“a” - Append: opens the for appending, creates a new file if it doesn’t exist.
“w” - Write: opens the for for writing (and will replace one), creates a new file if it doesn’t exist.
3) .read reads all the text as one long string
eg. print(myFile.read())
.readline() reads the lines one after the other
eg. print(myFile.readline())
print(myFile.readline())
4) .readlines() creates an array for each line of the text file.

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

1) How do you read a file in python?
2) How do you write a file in python?

A

1) How to read data from a file:
myFile = open(“textfile.txt”, “r”)
myData = myFile.read()
print(myData)
myFile.close()

The .read() function reads the text file as a big block of code
You must always end the text file line with the .close() function.

2) This is how to write a text file:
myFile = open(“testfile.txt”, “w”)
myData = “My first data in a file!”
myFile.write(myData)
myFile.close()

Now, the testfile.txt will have the text “My first data in a file!”, and all other text will have been cleared from it.

17
Q

1) What is a dictionary in python and how are they written?
3) How do you retrieve values from a dictionary?
4) How do you add a new key to a dictionary?
5) How do you step through values in a dictionary like in a list?

A

1) A dictionary is used to store data in key:value pairs. Dictionaries are written with curly brackets, and use a colon to separate the key and value pairs
eg. myDict = {“Name” : “Dave”, “Age” : 54}
2) myDict[“Name”]
Similar to lists, square brackets are used, and the key name.
3) myDict[ “Hair-col”] = “Brown”
This is how you add a new key and its values
4) for i in myDict:
print(myDict[i])
“i” takes the value of each key in the myDict.

18
Q

Explain what test data is and describe the following types of test data:
normal (typical)
boundary (extreme)
erroneous data

A

Test data is data that is used to test whether or not a program is functioning correctly. Ideally, test data should cover a range of possible and impossible inputs, each designed to prove a program works or to highlight any flaws.
normal data - typical, sensible data that the program should accept and be able to process
boundary data - valid data that falls at the boundary of any possible ranges, sometimes known as extreme data
erroneous data - data that the program cannot process and should not accept.
Boundary data would be for example:
If the allowed range is 1 to 10, then boundary data is 0, 1, 10, 11, ie. either side of the allowed boundary.

19
Q

1) State the method used with a list variable which will - without an argument provided - order the numbers ascending
2) When working with text files, what do we use at the end of a string to signify to move onto a new line in the file
3) What tests could a developer use to improve the testing of their code?

A

1) The method is: ‘.sort()’
2) The \n is the newline character and will force a string onto a new line in the file.
3) - It could be tested with different lengths of input
- It could be tested with the empty string
- It could be tested with invalid data

20
Q

1) What is assignment?
2) What is a relational operator?

A

1) Assignment is giving a variable a value.
2) Relational operators allow for assignment and for comparisons to be made. They are used in condition testing such as IF statements and loops.

21
Q

1) State some benefits of developing solutions using the structured approach
2) What is the difference between REPEAT…UNTIL and WHILE…ENDWHILE in pseudocode?

A

1) - Subroutines can be developed in isolation.
- It is easier to find errors, and testing is more effective than without a structure.
- Subroutines can be updated without affecting the overall program.
2) - The REPEAT…UNTIL structure tests the condition at the end, but the WHILE…ENDWHILE structure will not.
- The REPEAT…UNTIL structure will always excecute at least once, while the WHILE…ENDWHILE loop may never excecute.