1 Variables
Variables¶
Python variables hold values using =. There are a number of different variable types.
We will cover 6 different variables types:
- String
- Integer
- Float
- Boolean
- List
- Dictionary (advanced)
1) String¶
First we create a string variable and then print its value.
Important. All variable values can be printed with print().
aStringVariable = 'This is a string version 2'
print(aStringVariable)
This is a string version 2
2-3) Integer and Float¶
- Integer types are counting number like 0, 1, 2 and include negative numbers like -5, -3, -2
- Float types are decimal numbers like 10.2 and 12.6 and also include negative numbers like -26.12
One famous float is Pi = 3.1415926535
Making either an integer or float variable is similar to making a string variable.
anInteger = 10
aFloat = 12.6
4) Boolean¶
Boolean variables can be either True or False
Important. The words True and False are built into Python.
oneBoolean = True
anotherBoolean = False
With just the concept of String, Integer and Float.
We can now start thinking about some algebra like add, substract, and divide
Here is an axample of add (+).
newInteger = 10 + 2
print(newInteger)
anInteger = 10
newInteger = anInteger + anInteger
print(newInteger)
12 20
And subtract
a = 10
b = 20
print(a - b)
aMinusb = a -b
print(aMinusb)
-10 -10
And divide
a = 10
b = 20
print(a/b)
0.5
With this algebraic notation, we can basically do everyhting you would do in a calculator!
Errors in Python¶
What if we try to add a String and an Integer? Does that make sense?
aString = 'a'
anInteger = 6
aString + anInteger
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/cudmore/Dropbox/teaching-2023/iot-fall-2023/docs/docs/intro-to-python/1.variables.ipynb Cell 15 line 1 ----> <a href='vscode-notebook-cell:/Users/cudmore/Dropbox/teaching-2023/iot-fall-2023/docs/docs/intro-to-python/1.variables.ipynb#X33sZmlsZQ%3D%3D?line=0'>1</a> aString + anInteger TypeError: can only concatenate str (not "int") to str
What if we reverse the order? Do we get the same error?
anInteger + aString
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/cudmore/Dropbox/teaching-2023/iot-fall-2023/docs/docs/intro-to-python/1.variables.ipynb Cell 17 line 1 ----> <a href='vscode-notebook-cell:/Users/cudmore/Dropbox/teaching-2023/iot-fall-2023/docs/docs/intro-to-python/1.variables.ipynb#X32sZmlsZQ%3D%3D?line=0'>1</a> anInteger + aString TypeError: unsupported operand type(s) for +: 'int' and 'str'
All Python errors have a name, like TypeError and then a description like can only concatenate str (not "int") to str.
It is important (and usefull) to read the error and think about the line of code that triggered it.
In this case, String + Integer is not allowed. Likewise Integer + String is not allowed.
Next, can we add an Integer to a Float? I think we can?
anInt = 10
aFloat = 12.6
newVariable = anInt + aFloat
print(anInt)
print(aFloat)
print(newVariable)
10 12.6 22.6
So, when we add an Integer to a Float. We end up with a Float.
5) List¶
Lists can contain a mixture of different variable types (including lists!). Any item in a list can be indexed with []. List indices start at 0.
Important. In this example, we are also adding a comment with #. A comment is something for you to take a note for yourself and is often used to explain to some other person using your code what is going on.
# define some lists
aList = [1, 2, 3]
anotherList = [10.1, 11, 12.3]
pi = 3.1415926535
aThirdList = [aList, 'b', pi]
# print the results
print(aList)
print(aList[0])
print(anotherList[2])
print(aThirdList[2])
[1, 2, 3] 1 12.3 3.1415926535
6) Dictionary (beyond the scope of this tutorial but important!)¶
Dictionaries are a container to hold any type of variable including dictionaries!
The nice thing about dictionaries is you access the values inside using a key. Because of this, dictionaries are said to represent key value pairs.
The syntax for create a dictionary is a bit more complicated. Here, we will focus on accessing data in a dictionary by its key name.
The value for each key can be any Python type including Int, Float, String, Dict, etc.!
aFloatVariable = 12.6
anotherList = [10.1, 11, 12.3]
aDictionary = {
'firstKey': 'firstKeyValue',
'secondKey': 10,
'thirdKey': aFloatVariable,
'fourthKey': anotherList
}
print(aDictionary['secondKey'])
print(aDictionary['fourthKey'])
10 [10.1, 11, 12.3]
Review¶
- We went over the basic Python variable types like integer, float, boolean, and string.
- We introduced a Python list with []. Each element of a list can be any variable type
- We introduced a Python dictionary that uses a
keyto access its value. Like a list, the value can in turn be any Python type.
There are a number of other types we did not cover. For example there is:
- set() which is an unordered collection of things, like a list but no order using []
- tuple() is a fixed collection of things, once it is created it can not be modified.