Skip to main content

Python Tutorial

PYTHON DATA STRUCTURE

Data Structure is a way to organizing and storing data in computer memory so that we can access and modify it efficiently. In this post, we will learn about some cool feature of python data structure.

Python Data Structure

Python has following commonly used data structure.
  • List
  • Tuple
  • Set
  • Dictionary
In our previous post, we have already learned a lot about the list. In this post, we will learn about Tuple, Set, and Dictionary data structure.

Tuple

A tuple consists of a number of values separated by commas.

Create Tuple

#Question: Create a tuple of numbers 1, 2, 3, 4, 5
#Answer:
my_tuple = 1, 2, 3, 4, 5
or, my_tupe = (1, 2, 3, 4, 5)
Both of above syntax is correct.
There is the special syntax to construct of tuples containing 0 or 1 items.
#Question: Create tuple contain 0 item
#Answer: my_tuple = ()
#Question: Create tuple contain 1 item
#Answer: my_tuple = '1',
Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma.

Access Tuple

#Question: Access 3 from my_tuple = (1, 2, 3, 4, 5)
#Answer: my_tuple[2] = 3
Above syntax is perfectly fine but generally, we access tuple item by unpacking.
my_tuple = (1, 2, 3, 4, 5) #packing
a, b, c, d, e = my_tuple #unpacking

Update Tuple

my_tuple = (1, 2, 3, 4, 5) 
my_tuple[1] = 5
Above statement is not correct. we generally can not update a tuple because it is not mutable.

Nested Tuple

Tuple is immutable but it may contain a mutable data structure and we can modify/update it.
my_tupple = ([1, 2, 3, 4, 5], [1, 3, 10])
my_tuple[0][1] = 5
print (my_tuple)
#([1, 5, 3, 4, 5], [1, 3, 10])

Traverse Tuple

my_tuple = (1, 2, 3, 4, 5)
for x in my_tuple:
     print (x)

Set

A set is an unordered data structure with no duplicate. Set also support basic mathematical operations like union, intersection, difference, and symmetric difference. Curly braces or set() function used to create set to create empty set we have to use set() not {}.
my_set = {1, 2, 3, 4, 5} #create set
my_set1 = set(1, 2, 3, 4, 5) #create set
my_set - my_set1 #return numbers in my_set but not in my_set1
my_set | my_set1 #return union
my_set & my_set1 #return intersection
my_set ^ my_set1 #return union - intersection

Traverse Set

my_set = {1, 2, 3, 4, 5}
for x in my_set:
     print (x)
Set does not support indexing so we can not write my_set[0]  to access a set item.

Dictionary

Dictionary is an unordered set of key : values pair, with the requirement that the keys are unique.

Create Dictionary

my_dictionary = {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'} #create dictionary
my_dictionary = {} #create empty dictionary

Access Dictionary

We can access dictionary values using the key.
my_dictionary[1] #return 'A' 

Update Dictionary

my_dictionary = {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'} #create dictionary
my_dictionary[1] = 'E' # my_dictionary = {1: 'E', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}

Traverse Dictionary

my_dictionary = {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
for k, v in my_dictionary.items():
     print (k, v)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Comments

Popular posts from this blog

Clique in Graph

Clique in Graph In this tutorial, we will learn about the clique in the graph theory. Clique In graph theory, a clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Equations

In general we frame real life problems into some mathematical formula called equations in which we use constant and some variables(unknown) Example: Ram is 12 years old.Ram 's age is 3/5 of Shyam's age what is Shyam's age. Let Shyam's age is x then 3/5 of sham's age is 3*x/5 x*3/5=12 x=60/3=20 so Shyam's age is 20. Linear equations When there is a linear combination of unknown variables is there then eqn is linear Example: 3x+ 2y+ 4z=9 Non Linear Equations: Equations in which non linear combination of the variable is there then eqn is non linear. When any 2 unknown variable multiplication present in the equation then it is the nonlinear equation. Example: 2x+5yz=98 is a nonlinear equation because yz multiplication is there: Quadratic equation in one variable: x^2 + 4*x + 2=0 is non linear because unknown variable x is being multiplied in term x^2 Quadratic equation in many variable: x^2 + y^2 +z^2 + w=0 cubic equati...

Practice Problem on Square Measurement

Practice Problem on Square Measurement What is the area of square having side length 5 unit? 25 unit 2     10 unit 25 unit 10 unit 2 What is the  perimeter of a square   having side length 5 unit? 20 unit 20 unit 2 10 unit 10 unit 2 What is the  diagonal  length of a square   having side length 5 unit? 5√2 unit     5√2 unit 2 10 unit 10 unit 2 What is the side length of square having area equal to  25 unit 2  ? 5 unit 5  unit 2 10 unit 10  unit 2 There is a petrol pump in a area having length and width equal to 500 meter. What will be cost to put bricks in the whole area if bricks having length and width equal to 5 cm and cost of 1 bricks is Rs 5? Rs 500000000 Rs 50000000 Rs 5000000 Rs 500000 Related Post:  Square Mensuration Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. ...

Vertex Cover

Vertex Cover In this tutorial, We will learn about the vertex cover in graph theory. Vertex Cover  A vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the graph, either u or v is in vertex cover i.e minimum number of vertices that covers all edges of the given graph. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.