Table of contents
- ✅Introduction🐍📊
- ✅Give the Difference between List, Tuple, and Set. Do Handson and put screenshots as per your understanding.📚🌟
- ✅Create the below Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary. 🚀📣
- ✅Create a List of cloud service providers eg. Write a program to add Digital Ocean to the list of cloud_providers and sort the list in alphabetical order.🖊️
- ✅Conclusion
✅Introduction🐍📊
Welcome to the fascinating world of Python programming, where data comes to life through a diverse array of data types and structures. Just like building blocks, these tools empower you to manage and manipulate information efficiently.🛠️📚
Python's data realm will equip you with the knowledge to wield lists, dictionaries, tuples, and more, like a true programming maestro. 🎩
✅Give the Difference between List, Tuple, and Set. Do Handson and put screenshots as per your understanding.📚🌟
List | Tuple | Set |
Lists are mutable, meaning you can change, add, or remove elements after creation. | Tuples are immutable, meaning that once created, you cannot change, add, or remove elements. | Sets are mutable, so you can add or remove elements after creation |
Defined using square brackets [] . | Defined using parentheses () . | Defined using curly braces {} . |
Lists maintain the order of elements as they are added. | Tuples also maintain the order of elements. | Sets do not maintain the order of elements, and their order may vary when printed. |
Lists can contain duplicate elements. | tuples can also contain duplicate elements. | Sets do not allow duplicate elements; each element is unique. |
Example: my_list = [1, 2, 3, 4] | Example: my_tuple = (1, 2.5, 3, 4) | my_set = {1, 2, 3, 4} |
In a list, we can perform operations like creating a list, accessing elements, slicing, adding elements, Extending a List, etc.
list_of_data=[1,2.5,True,"ad"]
add a new item in list :
list_of_data.append("cycle") print(list_of_data)
clear all the items in list:
list_of_data.clear()
print(list_of_data)
number of times the value appears in the list:
print(list_of_data.count(2.5))
list_of_number=[1,25,6,7,9] print(list_of_number)
join two list:
list_of_number.extend(list_of_data)
print(list_of_number)
print(dir(list_of_number))
List Length:
print(len(list_of_number))
slicing operation in list:
print(list_of_number[0:5])
print all the items one by one:
for i in range(0,len(list_of_number)):
print(list_of_number[i])
in a tuple, we can perform operations like creating a tuple, accessing elements, slicing, unpacking, searching, etc.
tuple = (1,2,"ad",True,1)
print(tuple)
#Access Tuple Items
print(tuple[1])
# print last item
print(tuple[-1])
#slicing operation in tuple
print(tuple[1:3])
if "ad" in tuple:
print("Yes, 'ad' is in the tuple")
#Unpacking a Tuple
(a, b, c, d, e) =tuple
print(a)
print(b)
print(c)
print(d)
print(e)
#index() Method Search for the first occurrence of the value 2 and return its position:
x = tuple.index(2)
print(x)
in a set, we can perform operations like Creating a Set, Adding Elements, Removing Elements, Union, Clearing a Set, etc.
set = {12.5, 123, "ad"}
print(set)
#Get the Length of a Set
print(len(set))
#to know the datatype
print(type(set))
# Access Items in set
# we can not access items in a set by referring to an index or a key.
for x in set:
print(x)
#Add Items in set
set.add(2)
print(set)
#To add items from another set into the current set
tropical = {"pineapple", "mango", "papaya"}
set.update(tropical)
print(set)
#to remove a set item
set.remove("mango")
print(set)
✅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"
}
print(fav_tools[2])
output is:
✅Create a List of cloud service providers eg. 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"]
cloud_providers.append("Digital Ocean")
cloud_providers.sort()
print(cloud_providers)
output is:
✅Conclusion
solid understanding of Python data types and data structures forms the cornerstone of effective programming. 🐍💡
From the fundamental building blocks like integers, floats, strings, and booleans, to the more complex collections like lists, tuples, and sets, Python offers a versatile range of data types that empower developers to represent and manipulate information in various ways. Each data type comes with its unique characteristics, strengths, and appropriate use cases.