Day 14: 90DaysOfChallenge

Day 14: 90DaysOfChallenge

Data Structure in Python

Data structures are a way of organizing and storing data so that they can be accessed and worked with efficiently. There are two types of Data Structures in Python - Primitive Data Structures and Non-primitive Data Structures.

Primitive Data Structures

Primitive Data Structures are the basic building block of data manipulation and contain simple and pure data values. Python has four primitive variable types:

  • INTEGER - It represents numeric data i.e. whole numbers from negative infinity to infinity, like 4, 5, or -1. Ex-

    num=20

    print(num)

    Output

    20

  • FLOAT - It represents rational numbers i.e. numbers containing decimal figures, like 2.32, 4.90 or -109.20. Ex-

    num=20.12

    print(num)

    Output

    20.12

  • STRING - It is a collection of alphabets, words, or other characters. It is created by enclosing a sequence of characters within a pair of single or double quotes. For example: "Python", "programming", etc. Ex-

    name="Python"

    print(name)

    Output

    Python

  • BOOLEAN - It is a built-in datatype and contains only two values - True and False and is used for conditional and comparison operations. Ex-

    x=2

    y=4

    print(x==y)

    Output

    False

Non-primitive Data Structures

Non-primitive data structures are one of the most complex form of the data structure family. It not only stores the value of a single datatype but also stores a collection of values with different datatypes. There are 5 five non-primitive data structures available:

  • LIST - Lists are used to store collections of heterogeneous data and is an in-built datatype in Python. It is mutable means the values can change even after its initialization. The values are written inside square braces. Ex-

    x=[] #empty list

    y=[1,3,"python",5,10.21]

    print(y) #This prints the list

    print(type(y)) #This returns the kind of datatype

    Output

    [1, 3, 'python', 5, 10.21]

    <class 'list'>

  • DICTIONARY - It is made of key-value pairs. Key is used to identify the item and the value holds the value of the item. Key has to be unique value as it is used as an identifier and value can store repeated values. It is written inside curly braces. Ex-

    dict = {"student1" : 20, "student2" : 30}

    print(dict)

    print(type(dict))

    Output

    {'student1': 20, 'student2': 30}

    <class 'dict'>

  • SET - It is an unordered data structure that stores unique values. It is written inside curly braces. It is mutable means the values can change even after its initialization. Ex-

    set= { 21, 32, 34, 43}

    print(set)

    print(type(set))

    Output

    {32, 34, 43, 21}

    <class 'set'>

  • TUPLES - Tuple is also used to store collections of heterogeneous data but it is immutable means once initialized it's values cannot be further changed or modified. It is very fast as compared to list. It is written inside round brackets. Ex-

    tuple = ("Shilpi", 32, "30", True)

    print(tuple)

    print(type(tuple))

    Output

    ('Shilpi', 32, '30', True)

    <class 'tuple'>

ARRAY - Arrays are used to store collections of homogeneous data. Array in Python can be created by importing array module. array(data_type, value_list) is used to create an array with data type and value list specified in its arguments. Ex-

# importing "array" for array creations

import array as arr

# creating an array with integer type

a = arr.array('i', [1, 2, 3])

print(a)

print(type(a))

Output

array('i', [1, 2, 3])

<class 'array.array'>

Tasks:

  1. Difference between List, Tuple and Set

ListTupleSet
List is an ordered collection.Tuple is an ordered collection.Set is an unordered collections.
It can contain repeated values.It can contain repeated values.It contains unique values.
It is mutable means the values can be modified even after initialization.It is immutable means it's values cannot be modified after initialization.It is mutable means the values can be modified even after initialization.
It is written inside square brackets.It is written inside round brackets.It is written inside curly brackets.
Ex- list = [1, "python", 2, 32,23, "java"]Ex- tuple= (1,"python", 2, 32,23, "java")Ex- set= {1,"python", 2, 32,23, "java"}

List:

list = [1, "python", 2, 32,23, "java"]
print(list)
print(type(list))

Output:

[1, 'python', 2, 32, 23, 'java']
<class 'list'>

Tuple:

tuple= (1,"python", 2, 32,23, "java")
print(tuple)
print(type(tuple))

Output:

(1, 'python', 2, 32, 23, 'java')
<class 'tuple'>

Set:

set= {1,"python", 2, 32,23, "java"}
print(set)
print(type(set))

Output:

{32, 1, 2, 23, 'java', 'python'}
<class 'set'>
  1. Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.

fav_tools = {

1:"Linux",

2:"Git",

3:"Docker",

4:"Kubernetes",

5:"Terraform",

6:"Ansible",

7:"Chef" }

fav_tools = { 
  1:"Linux", 
  2:"Git", 
  3:"Docker", 
  4:"Kubernetes", 
  5:"Terraform", 
  6:"Ansible", 
  7:"Chef"
}
print("Favourite tool is: "  + fav_tools[5])

Output:

Favourite tool is: Terraform
  1. Create a List of cloud service providers eg.

    cloud_providers = ["AWS","GCP","Azure"]

    Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.

cloud_providers = ["AWS","GCP","Azure", "Digital Ocean"]
cloud_providers.sort()
print(cloud_providers)

Output:

['AWS', 'Azure', 'Digital Ocean', 'GCP']

Thanks for reading!

~Shilpi