Table of contents
- Data Types in Python:
- Data Structures in Python:
- ⚡Tasks
- 🔹Task 1 - Give the Difference between List, Tuple, and Set. Do Handson and put screenshots as per your understanding.
- List:
- Tuple:
- Set:
- 🔹Task 2 - Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.
- 🔹Task 3 - Create a List of cloud service providers eg.
- ⚡Conclusion:
Data Types in Python:
In Python, data types define the nature of the variables and the operations that can be performed on them. Some of the common data types include:
🔹Numeric Types: Integers (int), Floating-Point Numbers (float), and Complex Numbers (complex).
🔹 Text Type: Strings (str).
🔹 Boolean Type: Boolean (bool) representing True or False.
🔹 Sequence Types: Lists, Tuples, and Strings.
🔹 Mapping Type: Dictionaries (dict).
🔹 Set Types: Sets (set) and Frozen sets (frozenset).
🔹 None Type: Represents the absence of a value (None).Data structure in Python
A data structure in Python is a way to store and organize data efficiently. The data structure in Python is categorized as Primitive and Non-primitive, based on its fundamental characteristics and usage.
Data Structures in Python:
Python offers various data structures to organize and manage data efficiently. Here are some popular ones:
📜List :
Lists are ordered and mutable collections of elements enclosed in square brackets []. They can hold various data types and allow duplicate elements.
For example:
fruits = ['apple', 'banana', 'orange']
🍐Tuples:
Tuples are ordered and immutable collections of elements enclosed in parentheses (). They are similar to lists but cannot be modified after creation.
For example:
dimensions = (10, 20, 30)
🔠Sets :
Sets are unordered collections of unique elements enclosed in curly braces {}. They eliminate duplicates automatically.
For example:
colors = {'red', 'green', 'blue'}
🗂️Dictionaries :
Dictionaries are another essential data structure in Python. They are collections of key-value pairs enclosed in curly braces {}. Each key is unique, and it maps to a corresponding value. Dictionaries are also known as associative arrays or hash maps.
For example:
# Example of a dictionary representing a person's details
person = {
'name': 'Nikhil Raut',
'age': 26,
'occupation': 'DevOps Engineer',
'location': 'India'
}
⚡Tasks
🔹Task 1 - Give the Difference between List, Tuple, and Set. Do Handson and put screenshots as per your understanding.
List:
List_of_names = [ ] -> Empty list
Important operations-
- Append - To add values to the list.
Syntax:
List1.append(value)
List_of_names.append("Aish")
List_of_names.append("Priya")
2. Clear - To empty the list.
Syntax:
List1.clear()
3. Count - To count the occurrences of any value.
List_of_num = [1, 1, 2, 3, 4, 2, 2, 1]
Print(List_of_num.count(1))
4. Extend - To combine 2 lists.
List_of_names.extend(list_of_nums)
5. Len - We can get the length of the list meaning the total number of values in a list.
Print(len(list_of_names))
6. Dir - To know what are all the operations can be done for that list.
Print(dir(List_of_names))
7. Index in the list - Position of the value in the list, starts with 0.
Print(List_of_names[0])
8. Slicing - We can iterate the list and get only a part of the list in the output.
Print(List_of_names[0:3])
- It will slice the list with values starting from the 0th to the 2nd position.
Print(List_of_names[1:4])
- It will slice the list with values starting from the 1st to the 3rd position.
9. Concatenate - Combine two lists, so now list one is appended with list 2 and the number of values in the list1 is list1+list2.
List1.extend(list2)
Print(list1)
Tuple:
- It's a sequence of values.
T1 = 10
T2=("2") -> not a tuple
T2=("2",) -> It is a Tuple as it is a sequence now.
- We can not change the value of a tuple - Immutable
T2 = (1,2,3)
T2[0].append(2) -> cannot be done
T2=([1,2,3], 4, 6)
T2[0].append(4) -> will change the value since the 0th element is a list that is mutable.
- Packed and unpacked tuples-
T2 = (10,20,30) -> Pecked tuple
Print(T2) -> will show packed value -> (10,20,30)
a,b,c = (10,20,30) -> unpacked tuple
Print(a,b,c) -> shows unpacked value -> 10 20 30
Set:
- Unordered collection.
Input - banana, mango grapes.
Output - grapes, banana, mango.
- Only holds unique values.
Here banana is added twice in the set but the set only allows a single occurrence.
Important operations-
- Add
2. Remove
3. Union
Combines two sets.
4. Intersection
Common values in both sets.
set1.intersection(set2)
5. Difference
set1.difference(set2)
Common values in set1 and set2 will be removed from set1.
A-B = C
6. Subset/superset
Check if set2 is a subset of set1 or set1 is a superset of set2.
Subset - > All values of set2 is in set1 then set2 is subset of set1
Superset -> set1 is a superset of set2 in the above ex.
🔹Task 2 - 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"
}
To print the values of all the keys in the above dictionary, I used for loop and iterate the keys to get all the values.
for i in fav_tools:
print(fav_tools[i])
Or use the get method -
🔹Task 3 - 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.
⚡Conclusion:
In this Article, we learned about data structures in Python and did exercises on different operations in List, Tuple, set, and dictionary. We also saw the difference between List, Tuple, and Set.
Thank you for Reading!📘Happy coding! 😊🐍
👍If you enjoyed this blog post, spread the love with likes, shares, and comments💖