A visually appealing arrangement of various data structure representations, including arrays, linked lists, stacks, and queues, each depicted with colorful icons, set against a soft gradient background. The arrangement forms a cohesive flow, illustrating their relationships and functionalities in Python programming, centered perspective, high brightness.

Data Structures in Python

CONTENTS

1. Built-in Data Structures
2. Input/ Output Function
3. Control Flow

01

Built-in Data Structures

A close-up view of a colorful list displayed on a digital device screen, showing various items in a neat order. The background is a simple, blurred workspace with soft lighting, highlighting the list's structure and details. The list items include reminders, tasks, and notes, with elements suggesting mutability and organization. The composition should convey a clean, modern aesthetic.

Lists

Lists are ordered, mutable (changeable), and allow duplicate elements.

They are defined using square brackets []. Items are separated by commas (,).

We can access elements by passing the position and can also change values.

Lists

List Methods

MethodPurposeExample
append(x)Adds an item to the end of the listlist.append(10)
insert(i, x)Inserts an item at index ilist.insert(2, 50)
remove(x)Removes the first occurrence of value xlist.remove(3)
pop(i)Removes and returns item at index i (default last)list.pop()
clear()Removes all items from the listlist.clear()
sort()Sorts the list in ascending orderlist.sort()
reverse()Reverses the list orderlist.reverse()
count(x)Returns how many times x appearslist.count(5)
index(x)Returns the index of the first occurrence of xlist.index("apple")
extend(iterable)Adds multiple items (joins lists)list.extend([4,5])
len()Returns the number of elementslen(l)

Tuples

Tuples are ordered, immutable (unchangeable), and allow duplicate elements.

They are defined using parentheses ().

Tuples

Tuple Methods

MethodPurposeExample
count(x)Returns how many times value x appears in the tuplet.count(2)
index(x)Returns the index of the first occurrence of xt.index("apple")
len()Returns the number of elementslen(l)
A close-up of a computer screen displaying a Python script that includes a tuple definition, showing the code 'my_tuple = (1, 2, 3, 4, 5)' prominently. The background should be a modern workspace with a well-organized desk, a coffee mug, and some notepads, creating a tech-savvy atmosphere with good lighting.

Example of Tuple

Creating a tuple

my_tuple = (1, 2, 3, 4, 5) print(my_tuple) /# Output: (1, 2, 3, 4, 5)

Accessing elements

print(my_tuple[0]) /# Output: 1 (first element) print(my_tuple[-1]) /# Output: 5 (last element)

Tuples are immutable; you cannot modify elements.

/# my_tuple[1] = 10 /# This will raise a TypeError

Tuples can be used to store heterogeneous data

person = ("Alice", 30, "Engineer") print(person) /# Output: ('Alice', 30, 'Engineer')

Dictionaries

• Dictionaries are ordered, mutable, and use key-value pairs to store data.

• Keys are unique, and values can be of any data type.

• They are defined using curly braces {}.

Dictionary Methods

MethodPurposeExample
get(key)Returns the value of the given key (does not show error if key doesn’t exist)d.get("name")
keys()Returns all keys in the dictionaryd.keys()
values()Returns all values in the dictionaryd.values()
items()Returns all key-value pairs as tuplesd.items()
update(d2)Updates dictionary with elements from another dictionaryd.update({"age": 21})
pop(key)Removes the item with the given keyd.pop("name")
popitem()Removes the last inserted key-value paird.popitem()
clear()Removes all items from the dictionaryd.clear()

Example of Dictionary

Creating a dictionary

my_dict = {"name": "Alice", "age": 30, "city": "New York"} print(my_dict) /# Output: {'name': 'Alice', 'age': 30, 'city': 'New York'}

Accessing values

print(my_dict["name"]) /# Output: Alice

Modifying values

my_dict["age"] = 31 print(my_dict) /# Output: {'name': 'Alice', 'age': 31, 'city': 'New York'}

Adding new key-value pairs

my_dict["occupation"] = "Engineer" print(my_dict) /# Output: {'name': 'Alice', 'age': 31, 'city': 'New York', 'occupation': 'Engineer'}

Removing key-value pairs

del my_dict["city"] print(my_dict) /# Output: {'name': 'Alice', 'age': 31, 'occupation': 'Engineer'}

a vibrant image of a diverse collection of unique colored stones, arranged in a circular pattern to signify a set, with some stones being larger and more prominent, while others are smaller, against a soft neutral background that emphasizes the variety and uniqueness of each stone, close-up view, high saturation.

Sets

A set is a collection of unique elements (no duplicates).

It is unordered, meaning the items do not have a fixed position.

Sets are mutable, so we can add or remove elements.

Sets

Set Methods

MethodPurposeExample
add(x)Adds an elements.add(10)
remove(x)Removes x (error if not found)s.remove(3)
discard(x)Removes x (no error if not found)s.discard(3)
clear()Removes all itemss.clear()
union(s2)Combines setss1.union(s2)
intersection(s2)Common elementss1.intersection(s2)
A detailed screenshot of Python code demonstrating the creation and usage of a frozenset, with clear syntax highlighting, capturing the essence of immutability in sets. The code piece consists of various frozenset operations like union and intersection, set against a modern coding environment with a dark theme for contrast.

Frozenset

Immutable version of a set (cannot be changed after creation).

No add(), remove(), etc.

Supports only reading operations like union() and intersection.

A frozenset represented as a transparent container showing the numbers 1, 2, and 3 floating inside. The container is set against a minimalist background that highlights the concept of immutability, with soft lighting illuminating the frozenset. The focus is on the unique structure of the frozenset, capturing a sense of order and clarity.

Frozenset

Example:

fs = frozenset({1, 2, 3})

Difference Between List, Tuple, Set, and Dictionary

FeatureListTupleSetDictionary
DefinitionOrdered collection of itemsOrdered collection of itemsUnordered collection of unique itemsCollection of key–value pairs
Syntax[](){}{key: value}
MutabilityMutable (can change values)Immutable (cannot change values)Mutable, but elements must be uniqueMutable (keys are unique)
AllowsYesYesNoKeys: No, Values: Yes
IndexingSupportedSupported (fixed data that shouldn’t change)Not supportedAccessed using key
Best UsedGeneral purpose data storageSupported (fixed data that shouldn’t change)Removing duplicates, math set operationsData mapped as key → value pairs

02

Input/ Output Function

A close-up of a computer screen displaying Python code with the input() and print() functions prominently featured, with a programmer's hands typing on the keyboard, surrounded by notes and a coffee mug, in a modern, clean workspace, warm lighting, to evoke a sense of productivity and coding environment.

Input/ Output Function

The input() function is used to take input from the user. It reads a line from input, converts it into a string, and returns it.

name = input("Enter your name: ") print(f"Hello, {name}!")

The print() function is used to display output to the console. It can take multiple arguments, which are printed with a space between them by default.

name = "Alice" age = 30 print("Name:", name, "Age:", age)

03

Control Flow

Control Flow

Control flow statements in Python allow you to manage the execution of code based on certain conditions or repeatedly execute code blocks.

Conditional Statements

if Statement

Executes a block of code if a specified condition is true. Example:

age = 18 if age >= 18: print("You are an adult.")

if-else Statement

Executes one block of code if the condition is true, and another block if it is false. Example:

age = 16 if age >= 18: print("You are an adult.") else: print("You are not an adult.")

if-elif-else Statement

Allows you to check multiple conditions. If the first condition is false, it checks the next one, and so on. Example:

age = 20 if age < 13: print("You are a child.") elif age < 20: print("You are a teenager.") else: print("You are an adult.")

Thank You