Table of contents
What are Data Types?
In Python, data types are classifications or categories that determine the type of data that a variable can hold. The data type of a variable determines the operations that can be performed on it and the values that it can store. Here are some commonly used data types in Python:
Numeric Types:
int: Represents integers (whole numbers), e.g., 5, -10, 0.
float: Represents floating-point numbers (real numbers with decimal points), e.g., 3.14, -2.5, 0.0.
complex: Represents complex numbers in the form
a + bj
, wherea
andb
are floats andj
represents the square root of -1, e.g., 2+3j, -1+2j.
Text Type:
- str: Represents strings, which are sequences of characters enclosed in single quotes (' ') or double quotes (" "), e.g., "Hello", 'Python'.
Sequence Types:
list: Represents an ordered collection of items enclosed in square brackets [], where the items can be of different data types, e.g., [1, 2, 3], ['apple', 'banana', 'cherry'].
tuple: Represents an ordered collection of items enclosed in parentheses (), similar to a list, but tuples are immutable (their values cannot be changed), e.g., (1, 2, 3), ('a', 'b', 'c').
Mapping Type:
- dict: Represents key-value pairs enclosed in curly braces {}. Each key is unique and associated with a value, e.g., {'name': 'John', 'age': 25}.
Set Types:
set: Represents an unordered collection of unique elements enclosed in curly braces {}, e.g., {1, 2, 3}, {'apple', 'banana', 'cherry'}.
frozenset: Similar to a set, but immutable (values cannot be changed), e.g., frozenset({1, 2, 3}).
Boolean Type:
- bool: Represents the Boolean values True and False, which are used for logical operations.
None Type:
- None: Represents the absence of a value or a null value.
These are the basic data types in Python, but there are also other specialized data types and data structures available through modules or libraries, such as dates, times, arrays, etc.
What is Data Structures?
Data structures in Python refer to the way data is organized and stored in memory. Python provides several built-in data structures that allow efficient storage, retrieval, and manipulation of data. These data structures help in solving various programming problems by providing specific ways to store and access data.
Here are some commonly used data structures in Python:
- Lists: Lists are ordered collections of items enclosed in square brackets ([]). They can store elements of different data types and allow indexing and slicing operations.
Example:
pythonCopy codemy_list = [1, 2, 3, "four", 5.6]
print(my_list[0]) # Output: 1
print(my_list[3]) # Output: four
- Tuples: Tuples are similar to lists but are immutable, meaning their elements cannot be modified once defined. They are enclosed in parentheses (()).
Example:
pythonCopy codemy_tuple = (1, 2, 3, "four", 5.6)
print(my_tuple[2]) # Output: 3
- Sets: Sets are unordered collections of unique elements enclosed in curly braces ({}). They are useful when you want to perform mathematical set operations like union, intersection, etc.
Example:
pythonCopy codemy_set = {1, 2, 3, 3, 4}
print(my_set) # Output: {1, 2, 3, 4}
- Dictionaries: Dictionaries are key-value pairs enclosed in curly braces ({}). They allow efficient retrieval of values based on keys.
Example:
pythonCopy codemy_dict = {"name": "John", "age": 25, "city": "New York"}
print(my_dict["name"]) # Output: John
- Arrays: Arrays in Python can be created using the
array
module. They are similar to lists but can only store elements of the same data type.
Example:
pythonCopy codeimport array
my_array = array.array('i', [1, 2, 3, 4, 5])
print(my_array[2]) # Output: 3
These are just a few examples of data structures in Python. Depending on the requirements of your program, you can choose the appropriate data structure to efficiently handle and manipulate your data. Python also provides additional data structures like stacks, queues, heaps, and more, which can be implemented using built-in data structures or external libraries.
Tasks:
- Give the Difference between List, Tuple and set. Do Handson and put screenshots as per your understanding.
In Python, lists, tuples, and sets are three different types of collections used to store a group of related values.
Lists:
Lists are defined using square brackets [].
Lists are mutable, which means you can change the values of elements in a list.
Lists can contain duplicate values.
Lists are ordered, meaning that the elements of a list are stored in a particular order.
Lists allow indexing and slicing operations.
Tuple
Tuples are defined using parentheses ().
Tuples are immutable, which means you cannot change the values of elements in a tuple.
Tuples can contain duplicate values.
Tuples are ordered, meaning that the elements of a tuple are stored in a particular order.
Tuples allow indexing and slicing operations.
Set
Sets are defined using curly braces {} or the set() function.
Sets are mutable, which means you can change the values of elements in a set.
Sets cannot contain duplicate values.
Sets are unordered, meaning that the elements of a set are not stored in a particular order.
Sets do not allow indexing or slicing operations.
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"
}
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. [Hint: Use keys to built-in functions for Lists]