3.2 Programming Flashcards
1) What are the 5 mathematical operators in python and their symbols?
2) What is modulus in python?
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
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?
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()
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?
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.
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?
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
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?
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”
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?
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.
1) What is selection?
2) What is an if statement?
3) What is an elif statement?
4) What is an else statement?
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.
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?
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.
1) What is a string?
2) How do you return the number of letters in a string?
3) What is string concatenation?
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.
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?
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
1) What is a subroutine?
2) What are the two types of subroutines?
3) Explain the two types of subroutines
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.
1) What is a scope within a program?
2) What is a local variable?
3) What is a global variable?
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.
1) Why do we use local variables?
2) What does the ‘global’ keyword do, and should you be using it?
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.
1) What is exception handling?
2) What is used in python to write exception handlers?
3) How does it work?
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.
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?
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.