Data Types: Data types are fundamental categories that classify different types of data in Python. They define what kind of value a variable can hold and what operations can be performed on that data. In Python, everything is treated as an object, and data types are actually classes that define the characteristics and behaviors of the objects.
Numeric Data Types:
Integer: Represents whole numbers, like 1, -3, 100, etc.
Float: Represents decimal numbers, like 3.14, -2.5, 0.75, etc.
Complex: Represents complex numbers with both real and imaginary parts, like 2+3j, -1-2j, etc.
Sequential Data Types:
String: Represents a sequence of characters, like "hello", 'Python', etc.
List: Represents an ordered collection of items. Lists can contain elements of different data types, and you can modify, add, or remove elements from them.
Tuple: Similar to lists, but tuples are immutable, meaning once created, their elements cannot be changed.
Boolean Data Type:
- Represents truth values and has two possible values: True or False. Booleans are commonly used for making decisions in control flow statements.
Set Data Type:
- Represents an unordered collection of unique elements. Sets are useful for performing mathematical set operations like union, intersection, etc.
Dictionary Data Type:
- Represents an unordered collection of key-value pairs. Dictionaries are extremely useful for fast data retrieval using unique keys. Each key in the dictionary maps to a corresponding value, and you can easily access, modify, or add elements using the keys.
Data Structures: Data structures are ways of organizing and storing data in a computer's memory to allow efficient access, modification, and manipulation of the data. They form the backbone of any program as they determine how data is managed and processed.
- Lists: Python Lists are dynamic arrays that can hold elements of different data types. You can use square brackets [] to create a list, and the elements are indexed starting from 0. Lists are mutable, which means you can change their contents by adding or removing elements.
Example:
my_list = [1, 2, 'hello', 3.14]
- Tuples: Python Tuples are similar to lists but with one key difference – they are immutable, meaning you cannot modify their elements once created. Tuples are usually used for situations where you want to ensure the data remains constant and cannot be accidentally changed.
Example:
my_tuple = (10, 'world', 3.14)
- Dictionaries: Python Dictionaries are collections of key-value pairs, where each key is unique and maps to a specific value. Dictionaries provide fast access to values using keys, making them efficient for tasks like data lookup and mapping.
Example:
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
These are just some of the basic data types and data structures in Python. Understanding them is crucial for effectively storing, manipulating, and processing data in your Python programs.
Task-1
Let's explore the differences between lists, tuples, and sets, and I'll provide some hands-on examples along with screenshots to illustrate their usage.
- List:
Lists are ordered collections that can hold elements of different data types.
Lists are mutable, meaning you can change their content by adding, removing, or modifying elements.
Lists are created using square brackets
[]
.
Hands-on Example with Screenshot:
# creating a list
my_list = [1,2,"hello"]
# adding elements to the list
my_list.append("Keshav")
print("list after adding an element: ", my_list)
# modifying an element in the list
my_list[0] = 20
print("List after modifyhing an element : ",my_list)
# removing an element from the list
my_list.remove("hello")
print("list after remmoving an element: ", my_list)
Screenshot:
- Tuple:
Tuples are ordered collections similar to lists, but they are immutable, meaning you cannot change their elements after creation.
Tuples are used when you want to ensure data integrity and prevent accidental modifications.
Tuples are created using parentheses
()
.
Hands-on Example with Screenshot:
# creating a tuple
my_tuple = (0,'keshav',123)
# Accessing elements in tuple
print("First element of the tuple: ", my_tuple[0])
print("Last element of the tuple: ", my_tuple[-1])
# Trying to modify an element in the tuple (will result in an error)
try:
my_tuple[0] = 100
except TypeError as e:
print("Error",e)
Screenshot:
-
Set:
Sets are unordered collections of unique elements, meaning they do not allow duplicate values.
Sets are useful for performing mathematical set operations like union, intersection, etc.
Sets are created using curly braces
{}
or the built-inset()
function.
Hands-on Example with Screenshot:
# creating a set
my_set = {1,2,2,3,'hello','hello'}
# adding elements to the set
my_set.add('keshav')
print('set after adding an element: ', my_set)
# removing an element from the set
my_set.remove(2)
print("set after removing an element: ", my_set)
Screenshot:
In the set example, notice that duplicate elements 'hello' and '2' were automatically removed, as sets only keep unique values.
These examples and screenshots should help you understand the key differences between lists, tuples, and sets in Python. Lists are suitable for ordered collections with possible modifications, tuples for constant and immutable data, and sets for storing unique elements with unordered characteristics.
Task-2
Create a Dictionary and use Dictionary methods to print your favorite tool just by using the keys of the Dictionary.
# creating a dictionary
fav_tools = {
1:"Linux",
2:"Git",
3:"Docker",
4:"Kubernetes",
5:"Terraform",
6:"Ansible",
7:"Chef"
}
# Printing the fav_tool using the key only
print("My fav tool is",fav_tools[1],"and",fav_tools[2] )
This will print the values of key 1 and 2 which is Linux and Git
Task-3
List Hands-On
# creating a list
cloud_providers = ["AWS","GCP","Azure"]
# adding ditial ocean cloud
cloud_providers.append('Digital Ocean')
print("After adding new item", cloud_providers)
# sorting alphabetically
cloud_providers.sort()
print("Updated and sorted list of cloud service providers:")
print(cloud_providers)
Let's explain the program step by step:
First, we have a list called
cloud_providers
, which contains three cloud service provider names: "AWS", "GCP", and "Azure".We want to add "Digital Ocean" to the list. To do this, we use the
append()
method of lists, which allows us to add an element to the end of the list.
cloud_providers.append('Digital Ocean')
After this line executes, the cloud_providers
list will have the new item "Digital Ocean" added to it.
- Next, we want to sort the list in alphabetical order. To do this, we use the
sort()
method of lists, which rearranges the elements in ascending order.
cloud_providers.sort()
After this line executes, the cloud_providers
list will be sorted alphabetically.
- Finally, we print the updated and sorted list of cloud service providers using the
print()
function.
print("Updated and sorted list of cloud service providers:")
print(cloud_providers)
When you run the entire program, it will produce the following output:
After adding new item ['AWS', 'GCP', 'Azure', 'Digital Ocean']
Updated and sorted list of cloud service providers:
['AWS', 'Azure', 'Digital Ocean', 'GCP']
As you can see, "Digital Ocean" is added to the original list, and the list is sorted alphabetically, resulting in the updated and sorted list of cloud service providers.