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