Unit+2+ +Data+Structures+in+Python
Unit+2+ +Data+Structures+in+Python
saadsheriff1895@gmail.com
J732KOTCSB
Program: MCA
Specialization: Data Science
Semester: 2
Course Name: Python for Data Science
Course Code: 21VMT5S204
Unit Name: Data Structures in Python
Learning Objectives:
1. Variables:
- What are variables?
- Types of variables
2. Operators:
- What are operators?
saadsheriff1895@gmail.com
- Different types of operators- arithmetic, logical, assignment, comparison,
J732KOTCSB
identity, and bitwise.
3. Lists:
- Defining lists
- Adding, deleting, duplicating lists, and other functions of lists.
- Slicing in lists.
4. Tuples:
- Defining tuples.
- Various functions in tuples
- Discussing the difference between tuples and lists.
5. Sets:
- Defining sets.
- Functions of sets.
6. Dictionaries:
- Defining dictionaries
- Functions of dictionaries
7. Converting from one data structure to another.
Conventionally, every programming language follows one amongst the following type cases:
1. snake_case: it is a naming type where every word is separated by an underscore.
Ex- shoe_color, city_location
2. camelCase: naming type where the first word is in lowercase and the initial of every
new word is in uppercase. Ex- shoeColor, cityLocation
3. PascalCase: it is a naming type in which the initial of every word is in uppercase and
all other characters are in lowercase. Ex- ShoeColor, CityLocation.
Usually, in python, for variable names snake case is used. However, it must be noted that it
is a convention, meaning people over the world use snake_case, but it is not a compulsion.
Although, one can name a variable in whichever way they see fit, there are some rules
that are followed while naming variables:
1. Variable names are case sensitive. Therefore, apple and Apple are treated as two
different variables.
saadsheriff1895@gmail.com
2. The name of a variable must start with alphabets ( a-z in lowercase or uppercase) or
J732KOTCSB
underscore ( _ ). For ex- alpha, Alpha, _alpha
3. Special characters like +, - , * , /, etc. are not allowed while naming variables.
4. Variable names cannot begin with a number. Ex- 1Alpha will not be a valid variable
name.
5. Python keywords cannot be used as variable names. Keywords include break,
continue, end, etc.
Keywords are specially reserved words in python. These keywords have a specific function.
For instance, the ‘end’ keyword is used at the end of a loop to break the cycle of iterations,
likewise, ‘break’ is used in situations when the desired output is obtained and the loop is to
be stopped.
Operators:
Operators are symbols that carry out arithmetic and logical calculation. So all variables and
numbers are operands and the symbols are operators.
Types of operators:
1. Arithmetic operators: these are used to perform basic mathematical operations.
saadsheriff1895@gmail.com
J732KOTCSB
saadsheriff1895@gmail.com
J732KOTCSB
3. Comparison operators: as the name suggests these operators are used for
comparison of two variables or values. Returns Boolean values.
Operator Function Example
== Equal to c == d
!= Not equal to c != d
> Greater than c>d
< Less than c<d
>= Greater than equal to c >= d
<= Less than equal to c <= d
5. Identity Operators: they are used to compare two objects, if they are infact the same
object or not.
saadsheriff1895@gmail.com
J732KOTCSBDeclaring a variable:
To declare a variable, type a variable name and then assign a value to it. When you print the
variable name, it displays the value stored inside the variable.
Casting of Variables: to specify the type of data being used is called casting of a variable.
2. Boolean:
Boolean type of data stores only two values- True and False. It is also
interchangeably used with 0 and 1.
3. Strings: store string/text values. They are enclosed within double quotes or single
quotes.
Examples of primitive data type:
All elements inside the list are indexed. Indexing in python starts from 0. Therefore, “Alpha”
in the above example has the index 0 in variable “letters”, while the position of “gamma” is
2.
To view a particular element from the list use the index number along with variable name
while displaying it.
saadsheriff1895@gmail.com
J732KOTCSBSince lists are mutable, we can add, delete, duplicate and change list elements.
Lists are indexed so they can have duplicate values. A few other functions of lists include:
len() – displays the length of the list.
type() – displays the variable datatype.
sort() – displays the list in ascending order.
reverse() – reverses the order of the list
copy() – creates a duplicate of a specific element or series of elements.
count() – counts the number of elements inside the list, etc.
Slicing in lists:
If you want to get, say, all the elements beginning from the 3rd until the end, then you can
slice the list.
Using slicing you can specify the index range, the
saadsheriff1895@gmail.com point where you want to begin until the
J732KOTCSBend.
For instance, if you want to display 1 to 10 in a list of 1 to 100 elements, then:
5. Tuples:
Tuples are another type of data structures that are used to store multiple items in one
variable. Unlike lists, tuples are immutable, i.e. they cannot be changed. Tuples are also
ordered. They cannot be shuffled neither can the positions of items inside a tuple be
changed. The way we used [] to create lists, here, we use (). For instance:
saadsheriff1895@gmail.com
J732KOTCSB
All of these functions look very similar to Lists’. So how are these two different you might
ask?
The main difference is that tuples take up less memory than lists do. Hence, tuples are
faster to execute. They are immutable unlike lists. Lists can easily be reordered, while tuples
cannot. And the easiest difference to identify between them is the type of bracket usage.
Lists use [] while tuples use ().
One must use tuples when they are sure of the order of items, and when they are certain
that items would not be changed. Lists, however, due to their ease of editability can be used
when the elements need to be manipulated.
6. Sets:
Sets are the third type of data structures in Python. The key feature of sets is that they are
unordered and unindexed. Like usual math problems, sets are represented inside curly
braces {}. When we say that lists are unordered, we mean that everytime a variable of
saadsheriff1895@gmail.com
J732KOTCSB
Apart from the mentioned functions, sets have numerous other functions like .isdisjoint(),
.issubset(), .issuperset(), .update(), etc.
Sets are helpful when you want you require to do mathematical operations like combine or
separate items from two different sets. They help remove duplicity from lists and tuples.
sets are also faster than lists.
7. Dictionary:
Dictionary is a data structure. It is ordered. It cannot be duplicated. It can be
changed, hence, they are mutable. They are written within curly braces. Dictionaries
store data in the key : value format. Every key has a value. Just like indexing, keys
are used to identify values here. They can be used for bivariate data.
A list is collection of values that can be identifies by their indexes. For the same
reason, lists are ordered. However, in case of dictionary, you have ‘key’s that do the
work of indexes. These keys help in identifying values. Thus, the dictionary is not
always ordered. Another important differentiation will be the use of brackets. While
the lists use square brackets [], dictionaries use curly braces {}. Inside a list, every
element is separated by a comma. In dictionaries, a key is written, followed by its
value which is then separated by a comma.
Declaring a dictionary:
To declare a dictionary, you enter the variable name and enter the elements in key:value
form.
saadsheriff1895@gmail.com
J732KOTCSB
To print a specific value, enter the key name after the variable. For example:
saadsheriff1895@gmail.com
J732KOTCSB
saadsheriff1895@gmail.com
J732KOTCSB
Similary, for most part, you can convert one data structure to another easily. It must also be
noted that sometimes loops are also used to convert an object from one data structure to
another.
In a gist:
Category Lists Tuples Sets Dictionaries
Mutability Mutable Immutable Mutable Mutable
Ordered Ordered Ordered Unordered Unordered
Index-access Yes Yes No No
Braces [] () {} {}
Duplicates Can contain Can contain Cannot contain Cannot contain
duplicates duplicates duplicates duplicate keys
saadsheriff1895@gmail.com
J732KOTCSB