Data Types:
In Python, data types are fundamental categories used to classify different types of data that variables can hold. Python is a dynamically-typed language, which means that you don't need to explicitly declare the data type of a variable. Instead, Python infers the data type based on the value assigned to the variable. Here are some of the common data types in Python:
Numeric Types:
int: Integer data type for whole numbers. Example:
x = 10
.float: Floating-point data type for numbers with decimal points. Example:
y = 3.14
.complex: Complex number data type with a real and an imaginary part. Example:
z = 2 + 3j
.
Text Type:
- str: String data type for sequences of characters, enclosed in single or double quotes. Example:
name = "John"
.
- str: String data type for sequences of characters, enclosed in single or double quotes. Example:
Boolean Type:
- bool: Boolean data type representing True or False values. Example:
is_active = True
.
- bool: Boolean data type representing True or False values. Example:
Sequence Types:
list: A mutable ordered collection that can hold elements of different data types. Example:
my_list = [1, 2, "hello"]
.tuple: An immutable ordered collection. Once created, its elements cannot be changed. Example:
my_tuple = (10, 20, 30)
.
Set Types:
set: An unordered collection of unique elements. Example:
my_set = {1, 2, 3}
.frozenset: An immutable set. Once created, its elements cannot be changed. Example:
frozen_set = frozenset({4, 5, 6})
.
Mapping Type:
- dict: A collection of key-value pairs, where keys are unique. Example:
my_dict = {"name": "Alice", "age": 25}
.
- dict: A collection of key-value pairs, where keys are unique. Example:
NoneType:
- None: A special data type representing the absence of a value, similar to null in other languages. Example:
my_variable = None
.
- None: A special data type representing the absence of a value, similar to null in other languages. Example:
Data Structures:
Data structures are fundamental constructs used to organize and store data in a way that facilitates efficient data manipulation and retrieval. They provide a way to represent and manage data effectively, making it easier to perform various operations and algorithms.
๐ Array:
๐ Collection of elements stored in contiguous memory locations.
๐ Elements accessed by index for quick retrieval.
โ๏ธ Constant time access, but may require resizing for changes.
๐ Linked List:
๐ Linear collection of nodes, each containing data and a reference to the next node.
โก๏ธ Efficient insertions and deletions.
๐ฐ๏ธ Linear time access to elements.
๐ Stack:
๐ฅ Last-in, first-out (LIFO) data structure.
โก๏ธ "Push" elements onto the top, "pop" elements from the top.
๐ Used for function calls, undo mechanisms.
๐ถ Queue:
๐ถ First-in, first-out (FIFO) data structure.
โก๏ธ "Enqueue" elements at the rear, "dequeue" elements from the front.
๐๏ธ Used for task scheduling, BFS algorithms.
Tasks:
Give the Difference between List, Tuple and set.
List:
A List is a mutable (modifiable) ordered collection of elements in Python.
It is defined using square brackets [] and elements are separated by commas.
Elements in a List can be changed, added, or removed after creation.
Lists allow duplicate elements, and their order is preserved.
Tuple:
A Tuple is an immutable (unchangeable) ordered collection of elements in Python.
It is defined using parentheses () and elements are separated by commas.
Once a Tuple is created, its elements cannot be modified, added, or removed.
Tuples allow duplicate elements, and their order is preserved.
Set:
A Set is a mutable (modifiable) unordered collection of unique elements in Python.
It is defined using curly braces {} or the
set()
function and elements are separated by commas.Sets automatically remove duplicate elements, and their order is not guaranteed.