Python Data Types Part - 2
Python List
Earlier when discussing strings, we introduced the concept of a sequence in Python. Lists can be thought of as the most general version of a sequence in Python. Unlike strings, they are mutable, meaning the elements inside a list can be changed!
Lists are constructed with brackets [ ]
and commas separating every element in the list.
Let's go ahead and see how we can construct lists!
my_list = [1,2,3]
We just created a list of integers, but lists can actually hold different object types. For example:
my_list = ['A string',23,100.232,'o']
Python Dictionaries
If you're familiar with other languages you can think of these python dictionaries as hash tables.
So, what are mappings? Mappings are a collection of objects
that are stored by a key, unlike a sequence that stored objects by
their relative position. This is an important distinction since mappings won't
retain order since they have objects defined by a key.
A Python dictionary consists of a key and then an
associated value. That value can be almost any Python object.
Let's see how we can construct dictionaries to get a better understanding of how they work!
Make a dictionary with{ }
and: to signify a
key and a valuemy_dict = {'key1':'value1','key2':'value2'}
Call values by their key
my_dict['key2']
OUTPUT
'value2'
Python Tuples
Python tuples are very similar to lists, however, unlike lists, they are immutable meaning they cannot
be changed. You would use tuples to present things that shouldn't be changed,
such as days of the week, or dates on a calendar.
The construction of tuples uses ( )
with elements separated
by commas. For example:
Create a tuple
t = (1,2,3)
When to use Tuples?
You may be wondering, "Why to bother using tuples when
they have fewer available methods?" To be honest, tuples are not used as
often as lists in programming but are used when immutability is
necessary. If in your program you are passing around an object and need to make
sure it does not get changed, then a tuple becomes your solution. It
provides a convenient source of data integrity.
Python Sets
Sets are an unordered collection of unique elements. We can construct them by using the set()
function. Let's go ahead and make a set to
see how it works
x = set()
We add to sets with the add()
method
x.add(1)
print(x)
OUTPUT
{1}
Note the curly brackets. This does not indicate a dictionary! Although you can draw analogies as a set is a dictionary with only keys.
Python Booleans
Python comes with Booleans (with predefined True and False displays that are basically just the integers 1 and 0). It also has a placeholder object called None. Let's walk through a few quick examples of Booleans (we will dive deeper into them later in this course).
Set object to be a boolean
a = True
print(a)
OUTPUT
True
We can also use comparison operators to create Booleans.
print(1 > 2)
OUTPUT
False
If you want a detailed tutorial on the python list, python sets, python booleans, python tuples click on the link
Post a Comment
Post a Comment
if you have any doubts, please let me know