Learn Python Coding
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
إظهار المزيد📈 نظرة تحليلية على قناة تيليجرام Learn Python Coding
تُعد قناة Learn Python Coding (@pythonre) في القطاع اللغوي الإنكليزية لاعباً نشطاً. يضم المجتمع حالياً 39 190 مشتركاً، محتلاً المرتبة 3 497 في فئة التكنولوجيات والتطبيقات والمرتبة 10 504 في منطقة الهند.
📊 مؤشرات الجمهور والحراك
منذ تأسيسه في невідомо، حقق المشروع نمواً سريعاً وجمع 39 190 مشتركاً.
بحسب آخر البيانات بتاريخ 11 يونيو, 2026، تحافظ القناة على نشاط مستقر. خلال آخر 30 يوماً تغيّر عدد الأعضاء بمقدار 445، وفي آخر 24 ساعة بمقدار 15، مع بقاء الوصول العام مرتفعاً.
- حالة التحقق: غير موثّقة
- معدل التفاعل (ER): يبلغ متوسط تفاعل الجمهور 2.22%. وخلال أول 24 ساعة من النشر يحصد المحتوى عادةً 0.91% من ردود الفعل نسبةً إلى إجمالي المشتركين.
- وصول المنشورات: يحصل كل منشور على متوسط 870 مشاهدة. وخلال اليوم الأول يجمع عادةً 358 مشاهدة.
- التفاعلات والاستجابة: يتفاعل الجمهور بانتظام؛ متوسط التفاعلات لكل منشور يبلغ 4.
- الاهتمامات الموضوعية: يركز المحتوى على مواضيع رئيسية مثل math, harvard, oxford, supervision, waybienad.
📝 الوصف وسياسة المحتوى
يصف المؤلف القناة بأنها مساحة للتعبير عن الآراء الذاتية:
“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”
بفضل وتيرة التحديث المرتفعة (أحدث البيانات بتاريخ 12 يونيو, 2026) تحافظ القناة على حداثتها ومستوى وصول مرتفع. وتُظهر التحليلات تفاعلاً نشطاً من الجمهور، ما يجعلها نقطة تأثير مهمة ضمن فئة التكنولوجيات والتطبيقات.
def function_name(parameters):
# block of code
return result
---
### 4. Creating a Simple Function
def greet():
print("Hello, welcome to Python functions!")
greet() # Calling the function
---
### 5. Function with Parameters
def greet_user(name):
print(f"Hello, {name}!")
greet_user("Hussein")
---
### 6. Function with Return Value
def add(a, b):
return a + b
result = add(10, 5)
print(result) # Output: 15
---
### 7. Positional vs Keyword Arguments
def student_info(name, age):
print(f"Name: {name}, Age: {age}")
student_info("Ali", 22) # Positional
student_info(age=22, name="Ali") # Keyword
---
### 8. Default Parameter Values
def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Output: Hello, Guest!
greet("Hussein") # Output: Hello, Hussein!
---
### 9. Variable Number of Arguments
#### \*args – Multiple positional arguments:
def sum_all(*numbers):
total = 0
for num in numbers:
total += num
return total
print(sum_all(1, 2, 3, 4)) # Output: 10
#### \*\*kwargs – Multiple keyword arguments:
def print_details(**info):
for key, value in info.items():
print(f"{key}: {value}")
print_details(name="Ali", age=24, country="Egypt")
---
### **10. Scope of Variables**
#### Local vs Global Variables
x = "global"
def func():
x = "local"
print(x)
func() # Output: local
print(x) # Output: global
Use global keyword if you want to modify a global variable inside a function.
---
### 11. Docstrings (Function Documentation)
def square(n):
"""Returns the square of a number."""
return n * n
print(square.__doc__) # Output: Returns the square of a number.
---
### 12. Best Practices
• Use descriptive names for functions
• Keep functions short and focused
• Avoid side effects unless needed
• Add docstrings for documentation
---
### Exercise
• Create a function that takes a list and returns the average
• Create a function that takes any number of scores and returns the highest
• Create a function with default arguments for greeting a user by name and language
---
#Python #Functions #CodingBasics #ModularProgramming #CodeReuse #PythonBeginners
https://t.me/DataScience4requests library and a free API like ip-api.com.
---
### Step-by-Step Code
import tkinter as tk
from tkinter import messagebox
import requests
# Function to fetch IP information
def track_ip():
ip = entry.get().strip()
if not ip:
messagebox.showwarning("Input Error", "Please enter an IP or domain.")
return
try:
url = f"http://ip-api.com/json/{ip}"
response = requests.get(url)
data = response.json()
if data["status"] == "fail":
messagebox.showerror("Error", data["message"])
return
# Show info
result_text.set(
f"IP: {data['query']}\n"
f"Country: {data['country']}\n"
f"Region: {data['regionName']}\n"
f"City: {data['city']}\n"
f"ZIP: {data['zip']}\n"
f"ISP: {data['isp']}\n"
f"Timezone: {data['timezone']}\n"
f"Latitude: {data['lat']}\n"
f"Longitude: {data['lon']}"
)
except Exception as e:
messagebox.showerror("Error", str(e))
# GUI Setup
app = tk.Tk()
app.title("IP Tracker")
app.geometry("400x400")
app.resizable(False, False)
# Widgets
tk.Label(app, text="Enter IP Address or Domain:", font=("Arial", 12)).pack(pady=10)
entry = tk.Entry(app, width=40, font=("Arial", 12))
entry.pack()
tk.Button(app, text="Track IP", command=track_ip, font=("Arial", 12)).pack(pady=10)
result_text = tk.StringVar()
result_label = tk.Label(app, textvariable=result_text, justify="left", font=("Courier", 10))
result_label.pack(pady=10)
app.mainloop()
---
### Requirements
Install the requests library if not already installed:
pip install requests
---
### Exercise
• Enhance the app to export the result to a .txt or .csv file
• Add a map preview using a web view or link to Google Maps
• Add dark mode toggle for the GUI
---
\#Python #Tkinter #IPTracker #Networking #GUI #DesktopApp
https://t.me/DataScience4class TrieNode:
def __init__(self):
self.children = {}
self.is_end_of_word = False
---
#### Basic Operations
Insert Word:
def insert(root, word):
node = root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_end_of_word = True
Search Word:
def search(root, word):
node = root
for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.is_end_of_word
---
### 4. Example Usage of Trie
root = TrieNode()
insert(root, "hello")
insert(root, "helium")
print(search(root, "hello")) # True
print(search(root, "hel")) # False (prefix only)
---
### 5. Advantages and Disadvantages
| Data Structure | Advantages | Disadvantages |
| -------------- | -------------------------------------- | ------------------------------- |
| Red-Black Tree | Balanced, efficient search and update | Complex implementation |
| Trie | Fast prefix search and auto-completion | Memory-heavy with many children |
---
### 6. Summary
* Red-Black Trees and AVL Trees both keep BSTs balanced but with different balancing rules
* Tries are specialized for string data, enabling efficient prefix operations
* Both structures are essential for advanced algorithms and systems
---
### 7. Exercise
• Implement basic Red-Black Tree insertion (research needed)
• Implement delete operation in Trie
• Use Trie to implement a simple autocomplete function
• Compare search time for BST vs Trie on string data sets
---
#DSA #RedBlackTree #Trie #AdvancedTrees #DataStructures #Python
https://t.me/DataScience4log(n) for efficient performance.
---
### 2. AVL Tree – Introduction
An AVL Tree is a self-balancing Binary Search Tree named after inventors Adelson-Velsky and Landis.
Properties:
• For every node, the balance factor (height of left subtree – height of right subtree) must be -1, 0, or +1
• Automatically re-balances after insertions and deletions
---
### 3. Balance Factor Calculation
balance = height(left subtree) - height(right subtree)
Cases:
• Balance Factor > 1 → Left-heavy
• Balance Factor < -1 → Right-heavy
---
### 4. Rotations in AVL Trees
To restore balance, AVL trees use rotations:
Single Rotations:
• Right Rotation (LL Case)
• Left Rotation (RR Case)
Double Rotations:
• Left-Right Rotation (LR Case)
• Right-Left Rotation (RL Case)
---
### 5. AVL Tree Node Structure
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.height = 1
---
### 6. Helper Functions
def get_height(node):
if not node:
return 0
return node.height
def get_balance(node):
if not node:
return 0
return get_height(node.left) - get_height(node.right)
def right_rotate(z):
y = z.left
T3 = y.right
y.right = z
z.left = T3
z.height = 1 + max(get_height(z.left), get_height(z.right))
y.height = 1 + max(get_height(y.left), get_height(y.right))
return y
def left_rotate(z):
y = z.right
T2 = y.left
y.left = z
z.right = T2
z.height = 1 + max(get_height(z.left), get_height(z.right))
y.height = 1 + max(get_height(y.left), get_height(y.right))
return y
---
### 7. Insertion in AVL Tree
def insert(node, key):
if not node:
return Node(key)
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
else:
return node # no duplicate keys
# Update height
node.height = 1 + max(get_height(node.left), get_height(node.right))
# Get balance factor
balance = get_balance(node)
# Balance the tree
# Case 1 - Left Left
if balance > 1 and key < node.left.key:
return right_rotate(node)
# Case 2 - Right Right
if balance < -1 and key > node.right.key:
return left_rotate(node)
# Case 3 - Left Right
if balance > 1 and key > node.left.key:
node.left = left_rotate(node.left)
return right_rotate(node)
# Case 4 - Right Left
if balance < -1 and key < node.right.key:
node.right = right_rotate(node.right)
return left_rotate(node)
return node
---
### 8. Example AVL Tree Construction
root = None
for val in [10, 20, 30, 40, 50, 25]:
root = insert(root, val)
After insertion, the AVL Tree balances itself using appropriate rotations.
---
### 9. Traversals
Use any standard traversal to see the output:
def inorder(root):
if root:
inorder(root.left)
print(root.key, end=" ")
inorder(root.right)
---
### 10. Summary
• AVL Trees ensure that BSTs stay balanced
• Balance is maintained using rotations after insertion
• Time complexity for all operations is O(log n)
• AVL Trees are ideal when frequent insertions and deletions are expected
---
### Exercise
• Build an AVL Tree from a set of values
• Add functions for insert, get_height, get_balance, and inorder
• Test insertions that trigger all four types of rotations
• Bonus: Implement AVL deletion (complex but possible)
---
#DSA #BalancedTree #BinarySearchTree #Python
https://t.me/DataScienceclass 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/DataScience4class 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/DataScience4cv2.imread) or PIL (Image.open).
2. How do you organize images by class labels if they are stored in subfolders?
Iterate over each subfolder, treat folder names as labels, and map images accordingly.
3. What is the difference between OpenCV and PIL for image reading?
OpenCV reads images in BGR format and uses NumPy arrays; PIL uses RGB and has more image manipulation utilities.
4. How do you resize images before feeding them to a model?
Use cv2.resize() or PIL’s resize() method.
5. What is a good practice to handle different image sizes in datasets?
Resize all images to a fixed size or use data loaders that apply transformations.
6. How to convert images to NumPy arrays?
In OpenCV, images are already NumPy arrays; with PIL, use np.array(image).
7. How do you normalize images?
Scale pixel values, typically to \[0,1] by dividing by 255 or standardize with mean and std.
8. How can you load large datasets efficiently?
Use generators or data loaders to load images batch-wise instead of loading all at once.
9. What is `torchvision.datasets.ImageFolder`?
A PyTorch utility to load images from a directory with subfolders as class labels.
10. How do you apply transformations and augmentations during image loading?
Use torchvision.transforms or TensorFlow preprocessing layers.
11. How can you split datasets into training and validation sets?
Use libraries like sklearn.model_selection.train_test_split or parameters in dataset loaders.
12. How do you handle corrupted or unreadable images during loading?
Use try-except blocks to catch exceptions and skip those files.
13. How do you batch images for training deep learning models?
Use DataLoader in PyTorch or TensorFlow datasets with batching enabled.
14. What are common image augmentations used during training?
Flips, rotations, scaling, cropping, color jittering, and normalization.
15. How do you convert labels (class names) to numeric indices?
Create a mapping dictionary from class names to indices.
16. How can you visualize images and labels after loading?
Use matplotlib’s imshow() and print labels alongside.
17. How to read images in grayscale?
With OpenCV: cv2.imread(path, cv2.IMREAD_GRAYSCALE).
18. How to save processed images after loading?
Use cv2.imwrite() or PIL.Image.save().
19. How do you organize dataset information (images and labels) in Python?
Use lists, dictionaries, or pandas DataFrames.
20. How to handle imbalanced datasets?
Use class weighting, oversampling, or undersampling techniques during data loading.
---
Summary
Mastering image loading and organization is fundamental for effective data preprocessing in computer vision projects.
---
#Python #ImageProcessing #DatasetHandling #OpenCV #DeepLearning
https://t.me/DataScience4torch.utils.data.Dataset.
import os
from PIL import Image
from torch.utils.data import Dataset
class CustomImageDataset(Dataset):
def __init__(self, root_dir, transform=None):
self.root_dir = root_dir
self.transform = transform
self.image_paths = []
self.labels = []
self.class_to_idx = {}
classes = sorted(os.listdir(root_dir))
self.class_to_idx = {cls_name: idx for idx, cls_name in enumerate(classes)}
for cls_name in classes:
cls_dir = os.path.join(root_dir, cls_name)
for img_name in os.listdir(cls_dir):
img_path = os.path.join(cls_dir, img_name)
self.image_paths.append(img_path)
self.labels.append(self.class_to_idx[cls_name])
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
img_path = self.image_paths[idx]
image = Image.open(img_path).convert("RGB")
label = self.labels[idx]
if self.transform:
image = self.transform(image)
return image, label
---
2. Using Data Augmentation with `transforms`
Data augmentation helps improve model generalization by artificially increasing dataset diversity.
from torchvision import transforms
transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
Pass this transform to the custom dataset:
dataset = CustomImageDataset(root_dir='dataset/', transform=transform)
---
3. Loading Dataset with DataLoader
from torch.utils.data import DataLoader
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
---
4. Summary
• Custom dataset classes offer flexibility in how data is loaded and labeled.
• Data augmentation techniques such as flipping and rotation can be applied using torchvision transforms.
• Use DataLoader for batching and shuffling during training.
---
Exercise
• Extend the custom dataset to handle grayscale images and apply a random brightness adjustment transform.
---
#Python #DatasetHandling #PyTorch #DataAugmentation #ImageProcessing
https://t.me/DataScience4from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# Define transformations (resize, normalize, convert to tensor)
transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
dataset = datasets.ImageFolder(root='dataset/', transform=transform)
# Create DataLoader for batching and shuffling
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
# Access class names
class_names = dataset.classes
print(class_names)
---
2. Iterating Through DataLoader
for images, labels in dataloader:
print(images.shape) # (batch_size, 3, 128, 128)
print(labels)
# Use images and labels for training or validation
break
---
3. Using TensorFlow `image_dataset_from_directory`
TensorFlow Keras also provides utilities for loading datasets organized in folders.
import tensorflow as tf
dataset = tf.keras.preprocessing.image_dataset_from_directory(
'dataset/',
image_size=(128, 128),
batch_size=32,
label_mode='int' # can be 'categorical', 'binary', or None
)
class_names = dataset.class_names
print(class_names)
for images, labels in dataset.take(1):
print(images.shape)
print(labels)
---
4. Dataset Splitting
You can split datasets into training and validation sets easily:
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
'dataset/',
validation_split=0.2,
subset="training",
seed=123,
image_size=(128, 128),
batch_size=32
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
'dataset/',
validation_split=0.2,
subset="validation",
seed=123,
image_size=(128, 128),
batch_size=32
)
---
5. Summary
• PyTorch’s ImageFolder + DataLoader offers a quick way to load and batch datasets.
• TensorFlow’s image\_dataset\_from\_directory provides similar high-level dataset loading.
• Both allow easy transformations, batching, and shuffling.
---
Exercise
• Write code to normalize images in TensorFlow dataset using map() with Rescaling.
---
#Python #DatasetHandling #PyTorch #TensorFlow #ImageProcessing
https://t.me/DataScience4dataset/
class1/
img1.jpg
img2.jpg
class2/
img3.jpg
img4.jpg
You can use Python libraries like os, OpenCV, or PIL to read and organize images by their classes.
---
2. Code Example Using OpenCV
import os
import cv2
dataset_path = "dataset"
data = []
labels = []
for class_name in os.listdir(dataset_path):
class_dir = os.path.join(dataset_path, class_name)
if os.path.isdir(class_dir):
for img_name in os.listdir(class_dir):
img_path = os.path.join(class_dir, img_name)
img = cv2.imread(img_path)
if img is not None:
data.append(img)
labels.append(class_name)
print(f"Total images: {len(data)}")
print(f"Total labels: {len(labels)}")
---
3. Optional: Resize Images for Uniformity
target_size = (128, 128)
resized_img = cv2.resize(img, target_size)
Use this inside the loop before appending img to data.
---
4. Using PIL (Pillow) Instead of OpenCV
from PIL import Image
img = Image.open(img_path)
img = img.resize((128, 128))
img_array = np.array(img)
---
5. Organizing Images in a Dictionary
dataset_dict = {}
for class_name in os.listdir(dataset_path):
class_dir = os.path.join(dataset_path, class_name)
if os.path.isdir(class_dir):
dataset_dict[class_name] = []
for img_name in os.listdir(class_dir):
img_path = os.path.join(class_dir, img_name)
img = cv2.imread(img_path)
if img is not None:
dataset_dict[class_name].append(img)
---
6. Summary
• Use os.listdir() to iterate dataset directories.
• Read images with cv2.imread() or PIL.Image.open().
• Resize images to a uniform shape for model input.
• Store images and labels in lists or dictionaries for easy access.
---
Exercise
• Extend the code to save the loaded images and labels as numpy arrays for faster loading in the future.
---
#Python #ImageProcessing #DatasetHandling #OpenCV #PIL
https://t.me/DataScience4cv2.imread() to read and cv2.imshow() to display images.
3. What color format does OpenCV use by default?
OpenCV uses BGR format instead of RGB.
4. How to convert an image from BGR to Grayscale?
Use cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).
5. What is the difference between `cv2.imshow()` and matplotlib’s `imshow()`?
cv2.imshow() uses BGR and OpenCV windows; matplotlib uses RGB and inline plotting.
6. How do you write/save an image to disk?
Use cv2.imwrite('filename.jpg', image).
7. What are image thresholds?
Techniques to segment images into binary images based on pixel intensity.
8. What is Gaussian Blur, and why is it used?
A smoothing filter to reduce image noise and detail.
9. Explain the Canny Edge Detection process.
It detects edges using gradients, non-maximum suppression, and hysteresis thresholding.
10. How do you capture video from a webcam using OpenCV?
Use cv2.VideoCapture(0) and read frames in a loop.
11. What are contours in OpenCV?
Curves joining continuous points with the same intensity, used for shape detection.
12. How do you find and draw contours?
Use cv2.findContours() and cv2.drawContours().
13. What are morphological operations?
Operations like erosion and dilation that process shapes in binary images.
14. What is the difference between erosion and dilation?
Erosion removes pixels on object edges; dilation adds pixels.
15. How to perform image masking in OpenCV?
Use cv2.bitwise_and() with an image and a mask.
16. What is the use of `cv2.waitKey()`?
Waits for a key event for a specified time; needed to display images properly.
17. How do you resize an image?
Use cv2.resize(image, (width, height)).
18. Explain how to save a video using OpenCV.
Use cv2.VideoWriter() with codec, fps, and frame size.
19. How to convert an image from OpenCV BGR format to RGB for matplotlib?
Use cv2.cvtColor(image, cv2.COLOR_BGR2RGB).
20. What is the difference between `cv2.threshold()` and `cv2.adaptiveThreshold()`?
cv2.threshold() applies global threshold; adaptiveThreshold() applies varying thresholds locally.
---
Summary
These questions cover basic to intermediate OpenCV concepts essential for interviews and practical use.
---
#Python #OpenCV #InterviewQuestions #ComputerVision #ImageProcessing
https://t.me/DataScience4import cv2
cap = cv2.VideoCapture('video.mp4')
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
cv2.imshow('Video Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
---
2. Capturing Video from Webcam
cap = cv2.VideoCapture(0) # 0 is usually the built-in webcam
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow('Webcam Feed', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
---
3. Saving Video to File
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
out.write(frame) # write frame to output file
cv2.imshow('Recording', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
---
4. Real-Time Video Processing
• Example: Convert webcam feed to grayscale live.
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Webcam', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
---
Summary
• You can capture video from files or webcams easily with OpenCV.
• Real-time processing lets you modify frames on the fly (filters, detections).
• Saving video requires specifying codec, frame rate, and resolution.
---
Exercise
• Write a program that captures webcam video, applies Canny edge detection to each frame in real-time, and saves the processed video to disk. Quit when pressing 'q'.
---
#Python #OpenCV #VideoProcessing #Webcam #RealTime
https://t.me/DataScience4](https://t.me/DataScience4import cv2
image = cv2.imread('shapes.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Threshold to get binary image
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
cv2.imshow("Contours", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
---
2. Morphological Operations
• Used to remove noise or fill gaps.
* Erosion: Removes pixels on object boundaries.
* Dilation: Adds pixels to object boundaries.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
eroded = cv2.erode(thresh, kernel, iterations=1)
dilated = cv2.dilate(thresh, kernel, iterations=1)
• Other useful operations:
* Opening: erosion followed by dilation (removes noise).
* Closing: dilation followed by erosion (closes small holes).
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
---
3. Image Masking
• Use a mask to isolate part of an image.
mask = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
masked_img = cv2.bitwise_and(image, mask)
cv2.imshow("Masked Image", masked_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
---
Summary
• Contours help detect shapes and boundaries.
• Morphological operations clean up binary images by removing noise or filling holes.
• Masks isolate regions of interest for further processing.
---
Exercise
• Load an image with multiple shapes, use contour detection to count the number of distinct shapes, then draw bounding rectangles around each.
---
#Python #OpenCV #Contours #Morphology #ImageMasking
https://t.me/DataScience4import cv2
image = cv2.imread('image.jpg', 0) # load as grayscale
_, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
cv2.imshow("Binary Image", binary)
cv2.waitKey(0)
cv2.destroyAllWindows()
• Common thresholding types:
* THRESH_BINARY
* THRESH_BINARY_INV
* THRESH_TRUNC
* THRESH_TOZERO
* THRESH_OTSU (automatic threshold)
_, otsu = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
---
2. Image Blurring (Smoothing)
• Helps reduce image noise and detail.
# Gaussian Blur
blurred = cv2.GaussianBlur(image, (5, 5), 0)
# Median Blur
median = cv2.medianBlur(image, 5)
# Bilateral Filter (preserves edges)
bilateral = cv2.bilateralFilter(image, 9, 75, 75)
---
3. Edge Detection with Canny
• Canny edge detection is one of the most popular techniques for detecting edges in an image.
edges = cv2.Canny(image, 100, 200)
cv2.imshow("Edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
* The two thresholds (100 and 200) are used for hysteresis (strong vs. weak edges).
---
4. Combining Blur + Edge Detection
blurred = cv2.GaussianBlur(image, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
• Blurring before edge detection reduces noise and improves accuracy.
---
Summary
• Thresholding simplifies images to black & white based on intensity.
• Blurring smooths out images and reduces noise.
• Canny Edge Detection is a reliable method for identifying edges.
• These tools are fundamental for object detection, image segmentation, and OCR tasks.
---
Exercise
• Write a program that:
1. Loads an image in grayscale.
2. Applies Gaussian blur.
3. Uses Canny to detect edges.
4. Saves the final edge-detected image to disk.
---
#Python #OpenCV #ImageProcessing #EdgeDetection #ComputerVision
https://t.me/DataScience4pip install opencv-python
---
1. Reading and Displaying Images
import cv2
# Read the image
image = cv2.imread('image.jpg')
# Display the image in a window
cv2.imshow('My Image', image)
cv2.waitKey(0) # Wait for any key to close the window
cv2.destroyAllWindows()
---
2. Image Shape and Type
print(image.shape) # (height, width, channels)
print(image.dtype) # uint8 (8-bit integers for each channel)
---
3. Converting Color Spaces
# Convert BGR to Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Convert BGR to RGB (for matplotlib or image correction)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
---
4. Saving an Image
cv2.imwrite('gray_image.png', gray)
---
5. Drawing Shapes
# Draw a red rectangle
cv2.rectangle(image, (50, 50), (200, 200), (0, 0, 255), 2)
# Draw a filled circle
cv2.circle(image, (150, 150), 40, (255, 0, 0), -1)
# Draw text
cv2.putText(image, 'OpenCV!', (40, 300), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Drawn Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
---
6. Resize and Flip
# Resize image
resized = cv2.resize(image, (300, 300))
# Flip image horizontally
flipped = cv2.flip(image, 1)
---
Summary
• OpenCV allows you to read, display, modify, and save images easily.
• You can perform basic tasks like drawing, resizing, flipping, and color transformations.
• These operations are the building blocks for image analysis, preprocessing, and machine vision applications.
---
Exercise
• Write a program that:
1. Loads an image.
2. Converts it to grayscale.
3. Draws a blue circle in the center.
4. Saves the new image to disk.
---
#Python #OpenCV #ImageProcessing #ComputerVision #Beginners
https://t.me/DataScience4def merge_sorted_lists(l1, l2):
dummy = Node(0)
tail = dummy
while l1 and l2:
if l1.data < l2.data:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next
tail.next = l1 or l2
return dummy.next
---
2. Sorting a Linked List (Merge Sort – O(n log n))
def merge_sort(head):
if not head or not head.next:
return head
# Split list
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
mid = slow.next
slow.next = None
left = merge_sort(head)
right = merge_sort(mid)
return merge_sorted_lists(left, right)
• Attach this to your LinkedList class to sort self.head.
---
3. Removing Duplicates from a Sorted List
def remove_duplicates(self):
current = self.head
while current and current.next:
if current.data == current.next.data:
current.next = current.next.next
else:
current = current.next
---
4. Intersection Point of Two Linked Lists
def get_intersection_node(headA, headB):
if not headA or not headB:
return None
a, b = headA, headB
while a != b:
a = a.next if a else headB
b = b.next if b else headA
return a # Can be None or the intersecting node
---
5. Flatten a Multilevel Linked List
Imagine a list where each node has next and child pointers. Recursively flatten:
def flatten(node):
if not node:
return None
next_node = node.next
if node.child:
child_tail = flatten(node.child)
node.next = node.child
node.child = None
child_tail.next = flatten(next_node)
return child_tail if child_tail.next else child_tail
else:
node.next = flatten(next_node)
return node
---
Summary
• You now know how to merge, sort, and deduplicate linked lists.
• Techniques like merge sort, two-pointer traversal, and recursive flattening are essential for mastering linked lists.
• These problems are frequently asked in interviews at top tech companies.
---
Exercise
• Given two unsorted linked lists, write a function that returns a new linked list containing only the elements present in both lists (intersection), without using extra space or sets.
---
#DSA #LinkedList #MergeSort #AdvancedDSA #CodingInterview
https://t.me/DataScience4def reverse(self):
prev = None
current = self.head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
self.head = prev
• Time Complexity: O(n)
• Space Complexity: O(1)
---
2. Find the Middle of the Linked List
• Use the slow and fast pointer approach.
def find_middle(self):
slow = fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow.data if slow else None
---
3. Detect a Loop in the Linked List
• Use Floyd’s Cycle Detection Algorithm (a.k.a. Tortoise and Hare).
def has_loop(self):
slow = fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
---
4. Find the N-th Node from the End
def nth_from_end(self, n):
first = self.head
second = self.head
for _ in range(n):
if not first:
return None
first = first.next
while first:
first = first.next
second = second.next
return second.data
---
5. Full Example: Testing All Methods
ll = LinkedList()
for value in [10, 20, 30, 40, 50]:
ll.append(value)
ll.display()
ll.reverse()
ll.display()
print("Middle:", ll.find_middle())
print("Has Loop?", ll.has_loop())
print("3rd from End:", ll.nth_from_end(3))
---
Summary
• Use pointer manipulation for efficient algorithms in linked lists.
• Common techniques:
• Fast/slow pointers
• Reversal by in-place re-linking
• Two-pointer gap approach for nth from end
---
Exercise
• Write a method is_palindrome() that checks if the linked list reads the same forwards and backwards (without converting it to a list or using extra space).
---
#DSA #LinkedList #ReverseList #LoopDetection #TwoPointerTechnique
https://t.me/DataScience4
متاح الآن! بحث تيليغرام 2025 — أهم رؤى العام 
