Learn Python Coding
38.7K subscribers
1.06K photos
37 videos
24 files
853 links
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
πŸ“š Advanced Algorithms and Data Structures (2021)

1⃣ Join Channel Download:
https://t.me/+MhmkscCzIYQ2MmM8

2⃣ Download Book: https://t.me/c/1854405158/47

πŸ’¬ Tags: #DataStructures #Algorithms

USEFUL CHANNELS FOR YOU
❀5❀‍πŸ”₯2πŸ‘2
πŸ“š Advanced Data Structures and Algorithms (2023)

1⃣ Join Channel Download:
https://t.me/+MhmkscCzIYQ2MmM8

2⃣ Download Book: https://t.me/c/1854405158/88

πŸ’¬ Tags: #DataStructures #Algorithms

USEFUL CHANNELS FOR YOU
πŸ‘5❀‍πŸ”₯1
πŸ“š Data Structures with Python (2023)

1⃣ Join Channel Download:
https://t.me/+MhmkscCzIYQ2MmM8

2⃣ Download Book: https://t.me/c/1854405158/89

πŸ’¬ Tags: #DataStructures

USEFUL CHANNELS FOR YOU
πŸ‘4❀‍πŸ”₯1
πŸ“š Data Structures the Fun Way (2022)

1⃣ Join Channel Download:
https://t.me/+MhmkscCzIYQ2MmM8

2⃣ Download Book: https://t.me/c/1854405158/160

πŸ’¬ Tags: #DataStructures

USEFUL CHANNELS FOR YOU
πŸ‘6❀1πŸ‘1
πŸ“š Linked Data (2023)

1⃣ Join Channel Download:
https://t.me/+MhmkscCzIYQ2MmM8

2⃣ Download Book: https://t.me/c/1854405158/171

πŸ’¬ Tags: #DataStructures

USEFUL CHANNELS FOR YOU
πŸ‘6❀2
Open Guide to Data Structures and Algorithms

A must-read for anyone starting their journey in computer science and programming. This open-access book offers a clear, beginner-friendly introduction to the core concepts of data structures and algorithms, with simple explanations and practical examples. Whether you're a student or a self-learner, this guide is a solid foundation to build your DSA knowledge. Highly recommended for those who want to learn efficiently and effectively.

Read it here:
https://pressbooks.palni.org/anopenguidetodatastructuresandalgorithms

#DSA #Algorithms #DataStructures #ProgrammingBasics #CSforBeginners #OpenSourceLearning #CodingJourney #TechEducation #ComputerScience #PythonBeginners

⚑️ BEST DATA SCIENCE CHANNELS ON TELEGRAM 🌟
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘4πŸ”₯2
Topic: Python List vs Tuple β€” Differences and Use Cases

---

Key Differences

β€’ Lists are mutable β€” you can change, add, or remove elements.

β€’ Tuples are immutable β€” once created, they cannot be changed.

---

Creating Lists and Tuples

my_list = [1, 2, 3]
my_tuple = (1, 2, 3)


---

When to Use Each

β€’ Use lists when you need a collection that can change over time.

β€’ Use tuples when the collection should remain constant, providing safer and faster data handling.

---

Common Tuple Uses

β€’ Returning multiple values from a function.

def get_coordinates():
return (10, 20)

x, y = get_coordinates()


β€’ Using as keys in dictionaries (since tuples are hashable, lists are not).

---

Converting Between Lists and Tuples

list_to_tuple = tuple(my_list)
tuple_to_list = list(my_tuple)


---

Performance Considerations

β€’ Tuples are slightly faster than lists due to immutability.

---

Summary

β€’ Lists: mutable, dynamic collections.

β€’ Tuples: immutable, fixed collections.

β€’ Choose based on whether data should change or stay constant.

---

#Python #Lists #Tuples #DataStructures #ProgrammingTips

https://t.me/DataScience4
❀2
Topic: Linked Lists in Python – Part 1: Introduction and Singly Linked List Basics

---

What is a Linked List?

β€’ A linked list is a linear data structure where each element (called a node) contains:

β€’ The data value.
β€’ A pointer (or reference) to the next node.

β€’ Unlike arrays, linked lists don’t require contiguous memory and can grow dynamically.

---

Why Use Linked Lists?

β€’ Efficient insertions/deletions at the beginning or middle.
β€’ No need for pre-defining size, unlike arrays.
β€’ Used in memory-efficient applications like OS kernels, compilers, and real-time systems.

---

Types of Linked Lists

β€’ Singly Linked List – Each node points to the next.
β€’ Doubly Linked List – Nodes have next and previous pointers.
β€’ Circular Linked List – The last node points back to the head.

---

Basic Structure of a Node

class Node:
def __init__(self, data):
self.data = data
self.next = None


---

Building a Singly Linked List

class LinkedList:
def __init__(self):
self.head = None

def append(self, data):
new_node = Node(data)

if not self.head:
self.head = new_node
return

current = self.head
while current.next:
current = current.next
current.next = new_node


---

Traversing the List

def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")


Usage:

ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
ll.display() # Output: 10 -> 20 -> 30 -> None


---

Inserting at the Beginning

def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node


---

Summary

β€’ A singly linked list stores data as a sequence of nodes linked by references.

β€’ Supports dynamic memory usage, fast insertions, and flexible resizing.

β€’ The key is managing node connections safely and efficiently.

---

Exercise

β€’ Implement a method length() that returns the number of nodes in the list.

---

#DataStructures #LinkedList #DSA #Python #CodingBasics

https://t.me/DataScience4
❀3
Topic: Data Structures – Trees – Part 1 of 4: Introduction and Binary Trees

---

### 1. What is a Tree in Data Structures?

A tree is a non-linear hierarchical data structure consisting of nodes connected by edges. It's widely used in real-world applications like:

β€’ File systems
β€’ Databases (e.g., B-Trees)
β€’ Compilers (parse trees)
β€’ Artificial intelligence (decision trees)

---

### 2. Terminologies in Trees

β€’ Node: Basic unit of a tree
β€’ Root: Topmost node (only one)
β€’ Parent/Child: A node that connects to another below/above
β€’ Leaf: A node with no children
β€’ Edge: Connection between parent and child
β€’ Subtree: Any child node and its descendants
β€’ Depth: Distance from the root to a node
β€’ Height: Longest path from a node to a leaf
β€’ Degree: Number of children a node has

---

### 3. Binary Tree Basics

A Binary Tree is a tree in which each node has at most two children, referred to as:

β€’ Left child
β€’ Right child

#### Real-life example:

Imagine a family tree where each person can have two children.

---

### 4. Types of Binary Trees

β€’ Full Binary Tree – every node has 0 or 2 children
β€’ Perfect Binary Tree – all internal nodes have 2 children, and all leaves are at the same level
β€’ Complete Binary Tree – all levels are filled except possibly the last, which is filled from left to right
β€’ Skewed Tree – all nodes only have one child (left or right)
β€’ Balanced Binary Tree – the height difference of left and right subtrees is minimal

---

### 5. Binary Tree Representation in Python

Using Class & Object:

class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None

# Example
root = Node(1)
root.left = Node(2)
root.right = Node(3)


---

### 6. Tree Traversals

Tree traversal means visiting all the nodes of a tree in a specific order.

β€’ Inorder (LNR)
β€’ Preorder (NLR)
β€’ Postorder (LRN)
β€’ Level Order (BFS using queue)

#### Example – Inorder Traversal (Left β†’ Root β†’ Right):

def inorder(root):
if root:
inorder(root.left)
print(root.data, end=" ")
inorder(root.right)


---

### 7. Build a Simple Binary Tree and Traverse It

# Tree Structure:
# 1
# / \
# 2 3
# / \
# 4 5

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

print("Inorder Traversal:")
inorder(root) # Output: 4 2 5 1 3


---

### 8. Applications of Binary Trees

β€’ Expression evaluation
β€’ Search algorithms (Binary Search Trees)
β€’ Priority queues (Heaps)
β€’ Huffman encoding trees (data compression)
β€’ Syntax trees in compilers

---

### 9. Key Characteristics

β€’ Recursive nature makes tree problems suitable for recursion
β€’ Not sequential – can't be represented with only arrays or lists
β€’ Memory-efficient using pointers in linked structure

---

### 10. Summary

β€’ Trees are hierarchical and non-linear
β€’ Binary trees limit nodes to max 2 children
β€’ Various types of binary trees serve different use cases
β€’ Tree traversal is fundamental for solving tree problems

---

### Exercise

Create a class-based binary tree and implement:
β€’ Inorder, Preorder, and Postorder traversals
β€’ Function to count total nodes and leaf nodes

---

#DSA #BinaryTree #DataStructures #Python #TreeTraversal

https://t.me/DataScience4
❀8
Topic: Data Structures – Trees – Part 2 of 4: Binary Search Trees (BST)

---

### 1. What is a Binary Search Tree (BST)?

A Binary Search Tree (BST) is a binary tree where all nodes follow the below properties:

β€’ The left child of a node contains only nodes with values less than the node's value.
β€’ The right child of a node contains only nodes with values greater than the node's value.
β€’ Both left and right subtrees must also be BSTs.

This property allows for efficient searching, insertion, and deletion.

---

### 2. Why Use BSTs?

β€’ Search operations are faster than in linear structures (like lists).
β€’ Ordered traversal becomes very efficient.
β€’ Time complexity (on average):
 ‒ Search: O(log n)
 ‒ Insert: O(log n)
 ‒ Delete: O(log n)
*Worst case is O(n) for skewed trees.*

---

### 3. Implementing a BST in Python

class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None


#### Insert Function:

def insert(root, key):
if root is None:
return Node(key)
if key < root.key:
root.left = insert(root.left, key)
else:
root.right = insert(root.right, key)
return root


#### Example Tree:

root = None
for val in [50, 30, 70, 20, 40, 60, 80]:
root = insert(root, val)


This creates:

        50
/ \
30 70
/ \ / \
20 40 60 80


---

### 4. Searching in BST

def search(root, key):
if root is None or root.key == key:
return root
if key < root.key:
return search(root.left, key)
else:
return search(root.right, key)


---

### 5. Finding Minimum and Maximum

def find_min(root):
while root.left:
root = root.left
return root.key

def find_max(root):
while root.right:
root = root.right
return root.key


---

### 6. Deleting a Node in BST

There are 3 cases:

1. Node has no children
2. Node has one child
3. Node has two children

def delete(root, key):
if root is None:
return root

if key < root.key:
root.left = delete(root.left, key)
elif key > root.key:
root.right = delete(root.right, key)
else:
# Node with only one child or no child
if root.left is None:
return root.right
elif root.right is None:
return root.left
# Node with two children
temp = find_min_node(root.right)
root.key = temp.key
root.right = delete(root.right, temp.key)

return root

def find_min_node(node):
while node.left:
node = node.left
return node


---

### 7. Inorder Traversal of BST

Inorder traversal of a BST gives sorted order:

def inorder(root):
if root:
inorder(root.left)
print(root.key, end=" ")
inorder(root.right)


---

### 8. Time and Space Complexity Summary

| Operation | Best Case | Average Case | Worst Case |
| --------- | --------- | ------------ | ---------- |
| Search | O(log n) | O(log n) | O(n) |
| Insert | O(log n) | O(log n) | O(n) |
| Delete | O(log n) | O(log n) | O(n) |

*Worst case occurs when tree is skewed (e.g., inserting sorted data)*

---

### 9. Applications of BST

β€’ Search engines
β€’ Sorted maps and sets
β€’ Auto-complete features
β€’ Database indexing
β€’ Tree-based dictionaries

---

### 10. Summary

β€’ BST enforces ordering which helps efficient operations
β€’ Insertion, search, and deletion rely on recursive logic
β€’ Traversals help in data processing
β€’ Performance degrades in unbalanced trees β€” next part will cover balanced trees

---

### Exercise

β€’ Write code to insert, delete, and search in a BST.
β€’ Traverse the BST in inorder, preorder, and postorder.
β€’ Add a function to count number of nodes and leaf nodes.
β€’ Try inserting sorted data and observe tree structure (hint: use print or drawing).

#DSA #DataStructures #Tree #Python

https://t.me/DataScience4
πŸ‘3❀2