Introduction to Programming - 2 Flashcards
Variables can be of different types
Type of a variable tells you…. which is important as.. - (2)
what the computer thinks it is
This is important because it does not make sense, for example, to multiply two strings. What would “apples”*“pears” mean
What are the 4 main data types for variables?
- Numbers
- String
- Boolean
- None
Two variable data types inside Number type is - (2)
Integer
Floating point
What is ‘int’ or integer variable data type and example? - (2)
a whole number – can be positive or negative: … -2, -1, 0, 1, 2 …)
e.g., number of people in a experimental condition
What is ‘float’ or floating point variable data type and example? - (2)
a decimal number: e.g. 3.14159)
e.g., reaction times (RTs)
What is a string variable data type and example? - (3)
A string is a sequence of characters, enclosed within quotation marks, that can include letters, numbers, symbols, and spaces.
It can be of zero or more characters in lengthzero or more characters
(e.g. “Hello World”)
What is a Boolean variable data type?
A boolean variable is a data type that can hold one of two values: True or False
What is none variable data type?
None type – this is a special name for nothing in Python which is useful in various contexts.
Example of using ‘none’
x = None
What is ‘type’ function? - (3)
examine a variable or a thing we type into the console.
“type” refers to the classification of an object. Every object in Python has a type, which determines what kind of operations can be performed on that object and how it behaves.
determine the type of an object
What is the output?
<class ‘int’>
Type of type(3) is <class ‘int’>
Type of type(3.0) is <class ‘float’>
Type of type(“3.0”) is <class ‘str’>
Type of type(None) is <class ‘NoneType’>
Type of type(True) is <class ‘bool’>
Type of type(False) is <class ‘bool’>
In first line, we print the return value of…
To make things clearer… - (2)
return value of the type function
To make things clearer, on the other lines we add some extra information to help with context.
Write a code that stores values into a,b and c and asks its type
a = 3
print(‘Type of a is’, type(a))
b = 3.0
print(‘Type of b is’, type(b))
c = “Hello World”
print(‘Type of c is’, type(c))
What is output?
a = 3
print(‘Type of a is’, type(a))
b = 3.0
print(‘Type of b is’, type(b))
c = “Hello World”
print(‘Type of c is’, type(c))
Type of a is <class ‘int’>
Type of b is <class ‘float’>
Type of c is <class ‘str’>
if we have a variable in a script which we do not know the type of (for example, something that was returned from a function which we did not write), we can ask the script to tell us using
the type function and use {x} in Collab
Note that python makes numbers int by default unless
they have a decimal point in them
Most of the time, you will want float numbers so it is good practice to add a .0 to the end
Alternatively to adding .0, we can:
pass it through the float function
We can also turn floating point number into integer using int function which will
Note… - (3)
this will lose any floating point part of the number
Note that this does not “round” the number properly, it simply truncates it and chops off end of number (i.e., decimals).
Usually int operator forcing a floating point number to be a integer is something you dont want as it’s a common way for new Python programmers to generate bugs
Write code that will turn/cast/convert integer 3 into float and also another line of code that turns 3.25 into integer - (2)
print(type(float(3)))
print(type(int(3.25)))
What is the output of this code? - (4)
print(type(float(3)))
print(type(int(3.25)))
print(int(3.25))
print(int(3.999))
<class ‘float’>
<class ‘int’>
3
3
What are arithmetic operators in Python?
used to perform mathematical operations on numeric operands.
What are the arithmetic operators in Python? - (7)
- Addition (‘+’)
- Subtraction (‘ - ‘ )
- Multiplication (‘*’)
- Divison (‘/’)
- Integer Divison (‘//’)
- Power (**)
7.Modulus/Reminder (‘%’)
What if you mix up data types of numbers?
they tend to become floating numbers in calculations
if you do calculations of the same type they tend to become
the same time data type
Give me what the output will be of what these calculations will be:
print(3.0 + 3)
print(3.999 + 3)
print(3 - 2)
6.0
6.999000000000006
1
Give me what the output will be of what these calculations will be:
print(3 * 2)
print(3.0 * 2)
6
6.0
What is the power operator? (**) - (3)
used for exponentiation, meaning raises the left operand to the power of the right operand
e.g., 5 **3 is 5 raised to the power of 3
used to compute both “normal” power values (like squaring)
What is the output of this code?
print(3.0 ** 3) # 3 to the power of 3
print(9.0 ** 0.5) # 9 to the power of 0.5 (The square root of 9)
27.0
3.0
raising to 1/something is the same as taking a root? So raising to 12 or 0.5 is the same
as taking the square root….
In Python 3, there are two ways to do division - (2)
The first one (/) allows integers to turn into floating point numbers ‘where necessary’ (in other words, if the answer is not an integer).
The alternative integer division operator (//) does not and produces integer result:
What is the output of this calculation code?
print(3 / 2)
print(3 // 2)
1.5
1
When using the integer division operator on floating point numbers, the division will be performed as though
the numbers were integer, but the result will be floating point:
When using the integer division operator on floating point numbers, the division will be performed as though the numbers were integer, but the result will be floating point:
For example:
print(3.0//2) gives you:
1.0
When using the integer division operator on floating point numbers, the division will be performed as though the numbers were integer, but the result will be floating point:
For example:
print(3.0//2.0) gives you:
1.0
What is modulus (‘%’) operator in Python? - (2)
It calculates the remainder of the division of the left operand by the right operand.
What is left over when you do the division
What is the output of this calculation?
print(8 % 3)
2
What error message will this code display?
print(3 + “dlfkjdofd”) # Go on, try it! - (2)
return the error message of ‘unsupported operand type(s) for +: ‘int’ and ‘str’
This tells you that you can only concatenate strings to strings - adding an integer on doesn’t make any sense.
print(3 + “dlfkjdofd”) gives error as
Adding a string and an integer does not make any sense and we get an - (3)
This just means an error
an exceptional event occurred that the computer cannot cope with. The traceback (information in the exception) will normally give you a good hint as to what is wrong.
In this case, it tells you that you can only concatenate strings to strings - adding an integer on doesn’t make any sense.
Write a code working out your age in years in days - (4):
age_in_years = 22
days_per_year = 365
print(“My age in years is:” ,age_in_years)
print(“My age in days in:”, age_in_years*days_per_year)
What is output?
My age in years is: 22
My age in days in: 8030
What is precedence? - (2)
order in which operators are evaluated in an expression.
Operators with higher precedence are evaluated before operators with lower precedence.
Example of precedence: - (4)
For example, consider the expression 3 + 4 * 5.
In Python, the * operator has higher precedence than the + operator.
Therefore, the multiplication operation 4 * 5 is evaluated first, and then the addition operation 3 + (result of 4 * 5) is performed.
As a result, the expression evaluates to 23
The rules for precedence of mathematical operations in Python follow roughly the same rules as those in mathematics
(remember BODMAS)
Another example of precedence in Python
for instance: 1 + 2 * 3 will be evaluated as 1 + (2 * 3) giving a result of 7, because multiplication is higher precedence than addition.
In cases In Python where precedence is not correct, or you simply want to make your code clearer, you can use
brackets in the same way that you would in algebra.
In cases where precedence is not correct, or you simply want to make your code clearer, you can use brackets in the same way that you would in algebra. So, for instance you can write 1+ 2 * 3 the above as:
res = 1 + (2 * 3)
print(res)
What would the output be?
res = 1 + (2 * 3)
print(res)
7
In precedence in Python, brackets can also be nested if necessary for example:
res = 1 + (2 * (3 + 1))
print(res)
What would the output be?
res = 1 + (2 * (3 + 1))
print(res)
9
Having variables (or ‘boxes’) where you can store single numbers or strings is useful, but we quite often want to deal with more than a single piece of data. For example, - (2)
we might have a list of reaction times from an experiment we did on lots of different people, or a list containing the names (or, better, the anonymised ID numbers) of those people.
Alternatively, we might have information such as “this person has this phone number” for a lot of people.
To store collections of data like this, we have access to a set of variable types designed to make it easy. The trick is learning which one to use at which time. You will often choose between a
a list and a dictionary (which we will cover in the next session)
What are variable types that store collections of data? - (3)
- Lists
- Tuples
- Dictionaries
What is a list? - ‘(4)
Lists are ordered collections of items.
They are mutable, meaning you can modify the elements after creation.
Lists can contain elements of different data types, and elements can be accessed by their index.
Lists are created using square brackets []