Table of contents
- New day, New Topic.... Let's learn along π
- Data Types
- Data Structures
- Tasks
- 1 . Give the Difference between List, Tuple and set. Do Hands on and put screenshots as per your understanding.
- 2 . Create below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionaryfav_tools = { 1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef" }
- 3 . Create a List of cloud service providers eg.cloud_providers = ["AWS","GCP","Azure"]Write a program to addDigital Oceanto the list of cloud_providers and sort the list in alphabetical order.[Hint: Use keys to built in functions for Lists]
New day, New Topic.... Let's learn along π
Data Types
- In Python, data types are classifications that classify various types of data.These data types specify how data is stored in memory and the operations that can be done on it.
Here are some common data types in Python:
1 .Integers'int'
:
- Examples:
'4'
,'20'
,'2000'
2 .Floating-points-number'float'
:
- Examples:
'20.0'
,'-0.4'
,'14.3'
3 . Strings'str'
:
- Examples:
"DevOpsParthu"
,"Hello"
,"Python"
4 . Boolean'bool'
:
- Examples:
'True'
,'False'
5 . Lists'list'
:
- Examples:
['1 , 2 , 3']
,['DevOpsParthu'
,'Hello'
,'Python'
]
6 . Tuples'tuple'
:
- Examples:
(1,2,3)
,('a','b','c')
7 . Sets (set
):
- Examples:
{1, 2, 3}
,{'apple', 'orange', 'banana'}
8 . Dictionaries (dict
):
- Examples:
{"name": "DevOpsParthu", "age": 25}
,{"key1": 10, "key2": 20}
Programming efficiently needs a knowledge of the different uses of each form of data. Python is dynamically typed, which means that the interpreter decides the data type of a variable based on its assigned value, eliminating the need for you to declare it explicitly.
- To check what is the data type of the variable used, we can simply write:
your_variable=100type(your_variable)
your_variable=100
print(type(your_variable))
Data Structures
- A data structure is a way for efficiently managing, organizing, and storing data. It defines the connection between the operations that can be performed on data items and the data elements itself. Data structures provide a useful way of arranging and organizing data so that it can be accessed, modified, and processed.Data structures can be categorized into two main types:
1 . Primitive Data Structures:
Integers
int
Strings
str
Floats
float
Boolen
bool
2 . Composite Data Structures:
Lists
list
Tuples
tuple
Sets
set
Dictionaries
dict
Here are some common data structures in Python:
1 . Lists:
- orderly collection of items.
# List
list_of_names=["DevOpsParthu" , "Hello" , "Python"]
print("My_list_of_names: ",list_of_names)
2 . Tuples:
- Similar to lists but immutable (elements can't be changed once set).
# Tuples
tuple_of_numbers=[1,2,100,200]
print("my_tuple_of_numbers: ",tuple_of_numbers)
3 . Sets:
- Unordered collection of unique items.
# Sets
My_set={1,1,1,2,2,2,3,3,3,4,4,4,}
print("Myset: ",My_set)
4 . Dictionaries:
- Collection of key-value pair
# Dictionaries
my_dict={
"name":"DevOpsParthu",
"city":"Hyderabad",
"age":100
}
print("My_dictionarie: ",my_dict)
Tasks
1 . Give the Difference between List, Tuple and set. Do Hands on and put screenshots as per your understanding.
- The difference between List, Tuple, Set is:
1 . Lists:
Mutable: You can modify the list after creation (add,remove or change element).
Example output:
List: [1, 2, 3, 'DevOpsParthu', 'Hello', 'Python']
2 . Tuples:
Immutable: Once a tuple is created, its elements cannot be changed.
Example output: The attempt to modify the tuple will result in an error, but you will see
Tuple: (4, 5, 'Hello', 'Python')
printed.
3 . Unordered and Unique: Sets are unordered collections of unique elements.
- Example output:
Set: {1, 2, 3, 4, 5}
This script demonstrates the characteristics of each data structure, and running it will show you the output in the terminal.
Let's go through the differences between lists, tuples, and sets in Python and provide examples. Follow these steps to execute the examples in VSCode:
Open VSCode and create a new Python file, for example,
Data_Structure_eg.py
.Copy and paste the following code into the file:
1 . Lists:
# List
list_of_names=["DevOpsParthu" , "Hello" , "Python"]
list_of_names.append("Python For DevOps")
print("My_list_of_names: ",list_of_names)
2 . Tuples:
# Tuple
my_tuple = ("mon","tue","wed","thu","fri","sat","sun")
# Attempting to modify a tuple will result in an error:
my_tuple[1] = "tuesday" # This line will raise an error
print("Tuple:", my_tuple)
3 . Sets:
# List
my_set={1,1,2,2,3,3,5,5,8,8,44,44,55,55,100,,100}
my_set.add(2000)
print("my_set: ",my_set)
2 . Create below Dictionary and use Dictionary methods to print your favourite tool just by using the keys of the Dictionaryfav_tools = { 1:"Linux", 2:"Git", 3:"Docker", 4:"Kubernetes", 5:"Terraform", 6:"Ansible", 7:"Chef" }
# Dictionries
fav_tools={
1:"Linux",
2:"Git",
3:"Docker",
4:"Kubernetes",
5:"Terraform",
6:"Ansible",
7:"Chef"
}
# print your favorite tool using key
favorite_tool_key= 4
if favorite_tool_key in fav_tools:
favorite_tool=fav_tools[favorite_tool_key]
print("My_favorite_tool_is: ",favorite_tool)
3 . Create a List of cloud service providers eg.cloud_providers = ["AWS","GCP","Azure"]
Write a program to addDigital Ocean
to the list of cloud_providers and sort the list in alphabetical order.[Hint: Use keys to built in functions for Lists]
# List of cloud service providers
cloud_providers = ["AWS", "GCP", "Azure"]
# Add Digital Ocean to the list
cloud_providers.append("Digital Ocean")
# Sort the list in alphabetical order
cloud_providers.sort()
# Print the updated and sorted list
print("Updated and sorted cloud providers list:", cloud_providers)
Happy Learning
Thanks For Reading! :)
-SriParthuππ₯