Computational Thinking (II) - Programming Flashcards
How many programming data types are there?
5
What do data types do?
Store data as different types
What happens to memory with data types?
Each data type is allocated a different amount of memory
Why is it important to use the correct data type?
The code is more memory efficient, robust (hard to break) and predictable when the correct data types are used
How can programming languages be typed?
Weakly typed or strongly typed
What are the characteristics of weakly types programming languages?
Weakly try to convert data types to avoid errors
*Can lead to unpredictable results
What are the characteristics of strongly types programming languages?
Strongly don’t convert data types so will produce more errors
*Give more predictable results
What are the 5 main data types and what are their characteristics?
What are the memory requirements for the 5 data types?
What is casting?
Manual conversion between data types
Can be done using int(), real() / float(), bool() and str() commands
In casting, what would int(“1”) achieve?
Converts the string “1” to the integer 1
In casting, what would real(1) achieve?
Converts the integer 1 to the real 1.0
In casting, what would bool(1) achieve?
Converts the integer 1 to the Boolean value True
In casting, what would str(True) achieve?
Converts the Boolean value True to the string “True”
How is casting used with ASCII and what techniques allow it to be used?
ASCII numbers and characters can be found easily using casting
ASC() and CHR() functions are used
E.g. CHR(77) converts ASCII number 77 to ASCII character M
E.g. ASC(b) converts ASCII character b to ASCII number 98
What would be the best data types to use in the following:
What are operators?
Special characters that perform certain functions
What are the arithmetic operators?
Addition Subtraction Multiplication Division Exponentiation Quotient (DIV) Remainder / modulus (MOD)
What does the exponentiation operator ^ do?
Raises a number to a power
What does the quotient (DIV) operator do?
Returns the whole number
What does the remainder / modulus (MOD) operator do?
Returns the remainder
What do operators work on?
Integers / real data values (or a combination of the two)
What mathematical rule do computers follow?
Using BODMAS, calculate the following:
2 + 8 * 2
(2 + 8) * 2
2 + 8 * 2 = 18
(2 + 8) * 2 = 20
List the computing operators, their typical operator, an example and the result
What is the assignment operator?
=
What does the assignment operator do?
Give some examples?
It is used to assign values to constants or variables, for example:
Total = 100 Goals = 3 Cost = Total * 5 n = n + 5
What positioning should occur when using the assignment operator?
The name of the constant should be on the left of the = and whatever is being assigned should be on the right, for example
Dog’s name = Rio
What does a comparison operator do?
Compares the expression on the left hand side to the expression on the right
A Boolean value (True / False) is produced
What are the common comparison operators?
== <> or != < ><= >=
List the common comparison operators, what they mean and examples if they are True or False
What is the difference between age = 25 and age == 25?
age = 25 versus is age 25
What are the two ways data values can be stored?
Constants or variables
What is the name of a constant or variable linked to?
The name of a constant or variable is linked to a memory location that stores the data value
*The size of the memory location depends on the data type
How are constants assigned?
Using the const command
What will happen if a constant value is manually changed
It will result in an error
When is a constant assigned a data value?
At the time of design
What is the benefit of a variable over a constant?
Variables can change their value
How do languages know the data type?
Some languages (e.g. OCR Exam Reference Language) assume the data type when it is assigned (e.g. pressure = 30) for the integer
Other languages need it to be declared (e.g. int pressure = 30) for the integer
What naming conventions exist for constants and variables?
Lower case for the first letter followed by a mixture of letters, numbers and underscores
Variable names must not contain spaces or start with a number
Write a program to calculate the number of points an athlete has (5x points for 1st, 2x points for 2nd and 0 points for anything else)
firsts = 0
seconds = 0
points = 0
firsts = input(“Enter the number of 1st places”)
seconds = input(“Enter the number of 2nd places”)
points = (5 * firsts) + (2 * seconds)
print(points)
What are the benefits of assigning constants?
The constants remain constant(!) so don’t need to be changed when the program runs
If they do change, at design level, they only need to be updated in one location
What are strings made up of?
Characters – alphanumeric (letters, numbers, spaces, symbols etc…)
Ad1@£moT _R87
How are strings written?
Give an example
In “double” or sometimes ‘single’ quotation marks
string1 = “Print me, I’m a string”
print(string1)
Print me, I’m a string
How can strings be joined together, and what is this known as?
Give an example
Strings can be combined by concatenation, often with the + operator
string1 = “My favourite food is”
string2 = “cheese”
print(string1 + “ “ + string2)
My favourite food is cheese
What is the position of the first character in a string usually numbered as?
0
What special function exist in manipulation (known as methods)?
upper lower length left right subString
How are methods used?
Give an example
Methods act on a particular object (object name followed by a dot “.” and the method’s name), for example:
string1. upper()
string2. lower()
string3. length
List some typical functions (methods), what they do and how they would effect the string x which contains “Hello”
Show some Python specific methods
Write a method to get the first three letters of the variable name and have them manipulated into uppercase
name.left(3).upper
Python:
name[:3].upper()
Write a method to get the last two letters of the variable name and have them manipulated into lowercase
name.left(2).lower
Python:
name[-2:].lower()
The following code does not read correctly – what needs to be added to it?
name = David surname = Boatswain
print(name + surname)
A space (“ “) needs to be added as it currently shows DavidBoatswain
name = David surname = Boatswain
print(name + “ “ + surname)
David Boatswain
Write a method to get the first and last letters of the variable club
newclub = club.left(1) + club.right(1)
Python:
newclub = club[:1] + club[-1:]
print(newclub)
What is program flow and what are the two types of selection statement?
The order that steps are carried out – IF and SWITCH selection statements allow for program flow
What structure do IF statements usually have?
IF-THEN-ELSE
What do IF statements allow you to check for?
If a condition is True or False – carry out different actions depending on the outcome
Draw an IF statement flow diagram to check if the password entered is correct
Write a piece of pseudocode for granting access depending on the password entered
password = input(“Enter your password”)
if password == storedpassword then: print(“Access granted”) else: print(“Access denied”) end if
What is a NESTED IF statement?
More complex IF statements – IF within another IF
NESTED IF statements check more than one condition, once the previous condition has been established (True or False)
Write a piece of pseudocode for granting access depending on the password and the Year Group being 11
password = input(“Enter your password”) if user == Year11 then: if password == storedpassword then: print(“Access granted”) else: print(“Access denied”) end if else: print(“User not in Year 11 – access denied”) end if
What are ELSE IF statements (Python: elif)?
ELSE IF statements can check multiple conditions
They only check more conditions is the previous one was False
Write a piece of pseudocode for granting access depending on the password and the Year Group being 11 (with functions for Year 10 and Year 9 also included via ELSE IF statements)
password = input(“Enter your password”) if user == Year11 then: if password == storedpassword then: print(“Access granted”) else: print(“Access denied”) end if elseif user == Year10 then: print(“User not in correct year to access”) elseif user == Year9 then: print(“User not in correct year to access”) else: print(“Access denied”) end if
What is a SWITCH statement (also known as a CASE statement)?
SWITCH (CASE) statements check the value of a variable (rather than checking if the statement is True or False)