Python Variables PT. 2

Variables Part 2

Welcome to part two of the variables instruction. In this lesson, we will cover floats using lists and dictionaries. We will include links to Python documentation that can further your understanding of what functions are available to lists and dictionaries. Don’t be discouraged if the concepts still seem a bit foreign to you. Becoming an elite developer takes practice and a lot of frustration.

Floats are decimal numbers. In the previous lesson, I discussed integers as numbers that could be whole or decimal numbers. If you set a variable to a non-decimal number, then Python will default all operations to a rounded form of that variable; this could become problematic if you need precise calculations. Luckily there are string formatters, sometimes called precision modifiers in Python, that can set the number of decimals needed. We have provided an example of this below.

iVar1 = 7.4433 #This is an int

The above example would display as 7.44. You can add or subtract decimal places but changing 2f to your desired length. Python will automatically round these based on how many decimals you set. You will also see a second example using the built-in round function. The round function will attempt round based on the number provided in the second argument. These are two sides to the same coin.

Lists have built-in functions to manage data. When you create a list, you will probably want to add items to it. All objects in Python have members. These members could be attributes or functions. Lists have a member function called append(). Below we have provided an example of defining a list and adding to it. It is important to note that when you add something to a list, it adds at the end, meaning the highest-numbered position.

lVar3 = ["item 1",7,"Item 3"] #This is a list with strings and an int
lVar3.append("Item 4")

Executing the above code will add “item 4” to position 3 of the list. Rember that the positions start at 0, so the highest position is always the length of the list minus 1. Also, take note that data within a list is enclosed in brackets [ ]. If you use parenthesis or curly braces, then it will change the type and available member functions.

Tuples are immutable lists. A tuple is a list that cannot be changed. Once a list has been converted to a tuple or a tuple defined, it cannot be added to or modified in any way. You might use a tuple to store survey data or other information where integrity must be maintained at all times. Below is an example of a tuple with the same data as our previous list.

tVar3 = ("item 1", 7, "Item 3")

Dictionaries are key-value pairs of data that require data to be accessed by providing a key. In the previous lesson, I showed how you could access a variable in a list by providing the position, like lVar3[0], which accesses the first element in the list. You can do that with a dictionary, but you would end up with both the key and value. If you want to get just the value, you will need to know a key. A common use of this is to store the names and other attributes of a person. Dictionaries are defined with curly braces. Below is an example of a dictionary with a name and age. It will also print the name of the person stored.

dVar1 = 7 {"name": "John Doe", "age": 21}

Additional Resources

Python Dictionaries

Python Lists

Python Tuples

Python Rounding

Leave a Comment