Learn Python Coding
39.2K subscribers
648 photos
32 videos
24 files
412 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
๐Ÿ“‚ Reminder about Python map()!

map() โ€” a built-in function that applies the specified function to each element of an iterable object (list, tuple, set, etc.).

The picture shows the basic syntax, an example of use with lambda, and a typical case โ€” data transformation without a manual for loop.

Save it to quickly remember the syntax!

๐Ÿ๐Ÿ’ป๐Ÿ—บ๏ธ #Python #Coding #Programming #LearnToCode #DevTips #Tech
โค7๐Ÿ‘1
Why is enumerate() used in Python? ๐Ÿค”๐Ÿ

It allows you to simultaneously obtain the value of an element and its index when iterating through a list. ๐Ÿ“Šโœจ

This is more convenient and more readable than manually working with a counter. โœ…๐Ÿš€

for i, item in enumerate(items):
print(i, item)


#Python #Coding #Programming #Dev #Tech #Code

โœจ Join Best TG Channels
https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel
https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
โค4๐Ÿ‘1๐Ÿ‘1
Deep copying of objects with the copy module ๐Ÿ๐Ÿ“ฆ

import copy

# Original list with nested structure
original = [[1, 2, 3], [4, 5, 6]]

# 1. Shallow copy
shallow = copy.copy(original)
shallow[0][0] = 'X'
# Oh no! Both lists have changed, because the nested list wasn't copied, but passed by reference
print(f"Original after shallow: {original}") # [['X', 2, 3], [4, 5, 6]]

# Restore the data
original = [[1, 2, 3], [4, 5, 6]]

# 2. Deep copy
deep = copy.deepcopy(original)
deep[0][0] = 'X'
# Everything is fine! Only deep has changed, the original remains untouched
print(f"Original after deep: {original}") # [[1, 2, 3], [4, 5, 6]]

The link trap in Python ๐Ÿ”—๐Ÿ•ณ๏ธ

When you assign a list to another variable (A = B) or make a regular slice (A = B[:]), Python doesn't physically copy the data. It simply creates a new reference to the same objects in memory. If the list contains other mutable objects (lists, dictionaries, custom classes), standard copying methods will only create a shallow copy. The copy module allows you to control this process.

โ€” Breaking the links: The deepcopy function recursively traverses the entire data structure and creates honest, independent duplicates for each nested element. This ensures that changes in the copy will not harm the original data. ๐Ÿ”“๐Ÿ”’
โ€” Safe state: The use of deep copying is critical when implementing design patterns (for example, Snapshot/Memento), creating game state backups, or when you pass complex configurations to functions that may modify them accidentally. ๐Ÿ›ก๏ธ๐Ÿ’พ
โ€” A sensible balance: It's worth remembering that deepcopy works slower and consumes more memory than shallow copying, as it spends resources on creating new objects and checking for cyclic references. Use it specifically when there are nested mutable containers within the structure. โš–๏ธ๐Ÿง 

#Python #Programming #DeepCopy #Coding #Tech #Dev

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
โค6
Regular for-loops are versatile but not always optimal: they add extra interpreter overhead, which is especially noticeable on large data ๐Ÿ

In such cases, it's better to use standard Python tools, for example itertools โš™๏ธ

For example, to get all unique pairs from a list, nested loops are not needed โ€” just combinations():

from itertools import combinations

def get_unique_pairs(items):
return list(combinations(items, 2))

print(get_unique_pairs(['A', 'B', 'C', 'D']))

# Output:
# [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]

Conclusion: instead of manual loops, it's better to use ready-made tools from the standard library โ€” it's cleaner and more efficient ๐Ÿš€

#Python #Coding #Programming #Developer #Tech #Optimization

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
โค5๐Ÿ‘1
โšก๏ธ How Redis counts billions of unique values while barely using memory

There's an algorithm called HyperLogLog. It allows you to roughly estimate how many unique elements have passed through the system, using about 12 KB of memory.

The idea is simple: Redis doesn't store the elements themselves.

It does the following:

- Takes an element
- Calculates a hash from it
- Uses part of the hash as a cell number
- Checks the other part to see how many consecutive zeros it contains
- If the new number is larger than the old one, it updates the cell

Why does this work?

Because a long series of zeros in the hash is rare.

For example:

- 1 consecutive zero - quite common
- 5 consecutive zeros - less common
- 10 consecutive zeros - about a 1 in 1024 chance
- 20 consecutive zeros - a very rare event

If Redis sees a very rare pattern, it means that many different elements have likely passed through it.

Redis uses 16,384 small counters. Each stores the maximum "rarity" it has seen for its group of elements.

Then Redis combines these values mathematically to get an estimate of unique elements.

Not an exact number, but a very close approximation.

The main trick of HyperLogLog:

it can handle millions or even billions of values, but memory hardly increases at all.

That's why Redis can count unique users, IPs, requests, or events without huge tables and lists.

#Redis #HyperLogLog #DataScience #Tech #BigData #MemoryEfficiency

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO
โค3
A 14-day tutorial where you build a Python code-agent CLI in the style of Claude Code from scratch and simultaneously understand how the Agent Harness actually works. ๐Ÿ› ๏ธ๐Ÿค–

In the end, you don't just call a ready-made agent via the API, but you understand the components that make up a Claude Code-like tool. ๐Ÿง โš™๏ธ

https://github.com/bozhouDev/14days-build-claude-code-cli/blob/main/README.en.md

#Python #AI #ClaudeCode #CLI #CodingTutorial #Tech

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO
โค2๐Ÿ”ฅ1
Python can substitute an empty context manager without conditions inside!

It often happens that a resource needs to be opened via with, and sometimes the object is already ready and there's no need to open anything.

This usually leads to code duplication or conditions around with:

if need_open:
f = open(...)
else:
f = existing_file

`nullcontext(obj) behaves like an empty context manager and allows you to maintain a single execution flow.

This is especially useful for APIs, tests, optional resources, dependency injection, and functions that can accept both a path and a ready-made object.

with ctx as resource:
process(resource)

But note that nullcontext() does not close the passed object โ€” it simply passes it on further.

๐Ÿ”ฅ nullcontext() helps to unify scenarios with optional context managers and significantly simplifies the architecture of IO code.

#Python #ContextManager #CodingTips #DevLife #Programming #Tech

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO
โค1
Do you know that Python can shift sequences without slicing and creating new lists?

When you need to cyclically shift data, many use slicing:

data = data[-1:] + data[:-1]

But deque.rotate() does this at the level of the data structure and usually works more efficiently for cyclical operations.

q.rotate(1)

A negative value rotates the queue in the other direction.

q.rotate(-2)

This is useful for ring buffers, task schedulers, cyclical queues, and round-robin algorithms.

workers.rotate(-1)

๐Ÿ”ฅ deque.rotate() allows you to implement cyclical data structures without manual index logic and without creating new lists.

#Python #DataStructures #CodingTips #Programming #Deque #Tech

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO
โค2
How to create your own context manager in Python for opening and closing a connection to the SQLite database

The enter() method is used when opening a connection, and the exit() method is used when closing it:

import sqlite3

class DatabaseConnection:
def __init__(self, db_name):
self.db_name = db_name
self.connection = None

def __enter__(self):
self.connection = sqlite3.connect(self.db_name)
return self.connection

def __exit__(self, exc_type, exc_val, exc_tb):
if self.connection:
self.connection.close()

# Usage
with DatabaseConnection("example.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO

#Python #SQLite #ContextManager #Programming #Coding #Tech
โค3
Catch a useful trick for working with division in Python ๐Ÿ

divmod() takes two numbers and in a single operation returns a tuple with the quotient and remainder from the division ๐Ÿ“Š

#Python #Coding #Programming #Tech #Tips #Dev

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO
โค3
10 GitHub Repositories for Web Development in Python ๐Ÿ

Explore the best Python web development repositories for building APIs, full-stack web apps, dashboards, machine learning demos, internal tools, and interactive Python-based user interfaces. ๐Ÿ”ฅ

https://www.kdnuggets.com/10-github-repositories-for-web-development-in-python

#Python #WebDevelopment #GitHub #Coding #API #Tech

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO
Guessing numbers

This project for beginners in Python is a fun game that generates a random number (within a certain range) that the user must guess after receiving hints.

For each incorrect guess, the user receives additional hints, but at the cost of reducing their overall score.

๐Ÿ’ก๐Ÿ๐ŸŽฎ #Python #Coding #Beginners #Game #Learning #Tech

โœจ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk

โญ๏ธ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A

๐Ÿš€ Level up your AI & Data Science skills with HelloEncyclo โ€” a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
โœ… 13 courses live + 40+ coming soon
๐ŸŽฏ One access, lifetime updates
๐Ÿ”‘ Use code: PRESALE-BOOK-WAVE-2GFG
๐Ÿ‘‰ https://helloencyclo.com/?ref=HUSSEINSHEIKHO
โค2