List:
List is an interesting data type in python, this data type
is a list of values that are stored by one name in our computer storage, and
fortunately lists are iterable like string that means we can iterate on that to
select one of the elements in our list. so now let’s see an example of list:
My_list = [1,2,3,’a’,’b’,”Zaki”]
We always put the values of a list inside brackets [] and
each element separate by coma (,) from other elements as I mentioned before we
can select any element of our list by its index like:
My_list = [2,2,2.7]
My_list [0]
We can put the index of our element inside brackets to see
or change the value of that index like:
My_list = [1,2,3.7]
My_list [0] = 0
Now our first value (1) in our list changed to 0 because we
assigned that to value one on above example.
The next interesting thing about lists is this that we can
sum two or more than two list with each other like:
My_first_list = [1,2]
My_second_list = [3,4]
My_first_list+ My_second_list
In here the result will be [1,2,3,4] note that we can’t
perform these (/,*,-,%) operations on lists.
It was the main concept of lists for now we will have more
topics about it soon.
Dictionaries:
Dictionaries are a list of values that each of them has a
key to identify them, in this case every element of our dictionaries have two
part, a key and then a colon to associate a value to our key then the value of
course all of the keys and values are among {} and each element separate by
coma (,) from other elements like lists, we will see this in below example:
My_dic = {‘key1’:2,100:”Zaki”,’key3’:4.5}
Dictionaries uses their keys us their elements index so if
you try to select a value by its index you will face to an error. For selecting
an element in a dictionary you have to know its key and put that instead of its
index among brackets like:
My_dic = {‘key1’:2,100:”Zaki”,’key3’:4.5}
My_dic [100]
In here 100 is the key for the value “Zaki” if we want to
use a string type key we just type it inside brackets with citations or double
citations but for numbers we just use them lonely like:
My_dic = {‘key1’:2,100:”Zaki”,’key3’:4.5}
My_dic [‘key3’]
It will return value 4.5 for us from our dictionary, of
course that when we select an element we can assign that to a new value like:
My_dic = {‘key1’:2,100:”Zaki”,’key3’:4.5}
My_dic [‘key3’] = 1
now the value of our key3 is 1 in our dictionary. We can add
a new key and value very simple later in our code by the following way:
fruit_price = {‘apple’:1.5,’orange’:3,’banana’:2}
fruit_price [‘cherry’]=2.5
now if we check our dictionary we can see our new key by the
name of ‘cherry’ among our elements with its value.
Tuples:
Tuples are just like lists they store a list of values for
us but the main deference is this that tuples can’t be reassigned that means
that when we assign values to tuple we can’t change them later. we can define
tuples like below example:
My_tuple = (1,2,3)
As you see we write tuples values inside parenthesis that
these parenthesis shows that it is a tuple not a list. We can use indexing to
see the value of indexes like:
My_tuple = (‘one’,2,3)
My_tuple [0]
This will return string ‘one’ for us but if we try to assign
it to a new value we will face to error.
Sets:
Sets are a list of values that every value is unique in it, that
means which we do not have any duplicate element in sets. We can define a set
by following way:
My_set=set()
Now if we check the My_list type with “ type() ” in our code
we will see that it returns a set data type. Now you may ask what can I do with
an empty set? You are right an empty set is use less for us in here so if we
want to add some values in it we have to use add method to add values in our
empty set by following way:
My_set=set()
My_set.add(VALUE)
I typed VALUE inside parenthesis for pointing out where the
value has to be, for example we can add number or strings instead of VALUE and
it will contain it in our list but if we try to add the same value again it
will not give us error but it will also not contain it I our set. Now how to
remove an existing value from our list? Too easy just instead of .add use
.remove and write the value instead of VALUE like below:
My_set=set()
My_set.remove(VALUE)
Now you will not see that value in your set.
Note: we have methods for each data type that we learned up
to now and we can see the list of them by putting a dot at the of a variable
name, if I try to make topics on them so it will be too large topic and in the
other side they are very easy I believe you can find the out by yourself so try
each of them and see what happens.
It was python variables i hope you enjoy from reading this
topic feel free about asking your questions in below.
No comments