Python | Algorithms | Data Structures | Cyber ​​Security | Networks
38.6K subscribers
779 photos
23 videos
21 files
714 links
This channel is for Programmers, Coders, Software Engineers.

1) Python
2) django
3) python frameworks
4) Data Structures
5) Algorithms
6) DSA

Admin: @Hussein_Sheikho

Ad & Earn money form your channel:
https://telega.io/?r=nikapsOH
Download Telegram
from nltk.corpus import stopwords
# nltk.download('stopwords') # Run once
stop_words = set(stopwords.words('english'))
filtered = [w for w in words if not w.lower() in stop_words]


VII. Word Normalization (Stemming & Lemmatization)

• Stemming (reduce words to their root form).
from nltk.stem import PorterStemmer
ps = PorterStemmer()
stemmed = ps.stem("running") # 'run'

• Lemmatization (reduce words to their dictionary form).
from nltk.stem import WordNetLemmatizer
# nltk.download('wordnet') # Run once
lemmatizer = WordNetLemmatizer()
lemma = lemmatizer.lemmatize("better", pos="a") # 'good'


VIII. Advanced NLP Analysis
(Requires pip install spacy and python -m spacy download en_core_web_sm)

• Part-of-Speech (POS) Tagging.
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple is looking at buying a U.K. startup.")
for token in doc: print(token.text, token.pos_)

• Named Entity Recognition (NER).
for ent in doc.ents:
print(ent.text, ent.label_) # Apple ORG, U.K. GPE

• Get word frequency distribution.
from nltk.probability import FreqDist
fdist = FreqDist(word_tokenize("this is a test this is only a test"))


IX. Text Formatting & Encoding

• Format strings with f-strings.
name = "Alice"
age = 30
message = f"Name: {name}, Age: {age}"

• Pad a string with leading zeros.
number = "42".zfill(5) # '00042'

• Encode a string to bytes.
byte_string = "hello".encode('utf-8')

• Decode bytes to a string.
original_string = byte_string.decode('utf-8')


X. Text Vectorization
(Requires pip install scikit-learn)

• Create a Bag-of-Words (BoW) model.
from sklearn.feature_extraction.text import CountVectorizer
corpus = ["This is the first document.", "This is the second document."]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)

• Get feature names (the vocabulary).
print(vectorizer.get_feature_names_out())

• Create a TF-IDF model.
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(corpus)


XI. More String Utilities

• Center a string within a width.
centered = "Hello".center(20, '-') # '-------Hello--------'

• Check if a string is in title case.
"This Is A Title".istitle() # True

• Find the highest index of a substring.
"test test".rfind("test") # Returns 5

• Split from the right.
"path/to/file.txt".rsplit('/', 1) # ['path/to', 'file.txt']

• Create a character translation table.
table = str.maketrans('aeiou', '12345')
vowels_to_num = "hello".translate(table) # 'h2ll4'

• Remove a specific prefix.
"TestCase".removeprefix("Test") # 'Case'

• Remove a specific suffix.
"filename.txt".removesuffix(".txt") # 'filename'

• Check for unicode decimal characters.
"½".isdecimal() # False
"123".isdecimal() # True

• Check for unicode numeric characters.
"½".isnumeric() # True
"²".isnumeric() # True


#Python #TextProcessing #NLP #RegEx #NLTK

━━━━━━━━━━━━━━━
By: @DataScience4
LlamaIndex | AI Coding Tools

📖 A high-level framework for building RAG and agentic applications.

🏷️ #Python
embedding | AI Coding Glossary

📖 A learned vector representation that maps discrete items, such as words, sentences, documents, images, or users, into a continuous space.

🏷️ #Python
vector database | AI Coding Glossary

📖 A data system optimized for storing and retrieving embedding vectors.

🏷️ #Python
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {"filename": file.filename, "content_type": file.content_type}

• Set a response cookie.
from fastapi import Response
@app.post("/cookie/")
def create_cookie(response: Response):
response.set_cookie(key="fakesession", value="fake-cookie-session-value")
return {"message": "Cookie set"}

• Read a request cookie.
from fastapi import Cookie
@app.get("/read-cookie/")
def read_cookie(fakesession: Optional[str] = Cookie(None)):
return {"session": fakesession}


X. Error Handling & Advanced Responses

• Handle HTTPException.
from fastapi import HTTPException
@app.get("/items/{item_id}")
def read_item(item_id: str):
if item_id not in items_db:
raise HTTPException(status_code=404, detail="Item not found")

• Return a custom JSONResponse.
from fastapi.responses import JSONResponse
@app.get("/custom/")
def custom_response():
return JSONResponse(status_code=202, content={"message": "Accepted"})

• Return an HTMLResponse.
from fastapi.responses import HTMLResponse
@app.get("/page", response_class=HTMLResponse)
def read_page():
return "<h1>Hello World</h1>"

• Define a WebSocket endpoint.
from fastapi import WebSocket
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Hello from WebSocket")
await websocket.close()

• Handle a path operation that may not exist.
@app.get("/{full_path:path}")
def read_catch_all(full_path: str):
return {"path": full_path}

• Mount a static files directory.
from fastapi.staticfiles import StaticFiles
app.mount("/static", StaticFiles(directory="static"), name="static")

• Add a custom exception handler.
from fastapi import Request
from fastapi.responses import JSONResponse

class MyCustomException(Exception): pass

@app.exception_handler(MyCustomException)
async def custom_exception_handler(request: Request, exc: MyCustomException):
return JSONResponse(status_code=418, content={"message": "I'm a teapot"})

• Read a request header.
from fastapi import Header
@app.get("/headers/")
async def read_headers(user_agent: Optional[str] = Header(None)):
return {"User-Agent": user_agent}

• Get a raw Request object.
from fastapi import Request
@app.get("/request-info")
def get_request_info(request: Request):
return {"client_host": request.client.host}


#Python #FastAPI #API #WebDevelopment #Pydantic

━━━━━━━━━━━━━━━
By: @DataScience4
• Fetch related ForeignKey objects in the same query.
entries = Entry.objects.select_related('author').all()

• Fetch related ManyToManyField objects in a separate efficient query.
entries = Entry.objects.prefetch_related('tags').all()

• Load only specific model fields.
entries = Entry.objects.only('headline')

• Defer loading of specific model fields.
entries = Entry.objects.defer('body_text')

• Execute raw, unmanaged SQL.
authors = Author.objects.raw('SELECT * FROM myapp_author')

• Get results as a list of tuples.
Entry.objects.values_list('headline', 'pub_date')


XV. Transactions

• Import the transaction module.
from django.db import transaction

• Run a block of code within a database transaction.
with transaction.atomic():
# All database operations here are either committed together or rolled back.
author.save()
entry.save()


XVI. Managers & Model Methods

• Create a custom Manager for common queries.
class PublishedEntryManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(status='published')

• Add a custom method to a QuerySet via its Manager.
Entry.objects.get_queryset().by_author("John Doe")

• Add a custom method to a model for object-specific logic.
class Entry(models.Model):
#...
def is_recent(self):
return self.pub_date > timezone.now() - timedelta(days=1)


#Python #Django #ORM #Database #Backend

━━━━━━━━━━━━━━━
By: @DataScience4
1
inference | AI Coding Glossary

📖 The phase where a trained model processes new inputs to produce outputs.

🏷️ #Python
zero-shot learning | AI Coding Glossary

📖 A machine learning setting where a model handles classes or tasks that were not encountered during training.

🏷️ #Python
# Check if `n > 0` and `(n & (n - 1)) == 0`.

• Pow(x, n): Implement pow(x, n).
# Use exponentiation by squaring for an O(log n) solution.

• Majority Element:
# Boyer-Moore Voting Algorithm for an O(n) time, O(1) space solution.

• Excel Sheet Column Number:
# Base-26 conversion from string to integer.

• Valid Number:
# Use a state machine or a series of careful conditional checks.

• Integer to English Words:
# Handle numbers in chunks of three (hundreds, tens, ones) with helper functions.

• Sqrt(x): Compute and return the square root of x.
# Use binary search or Newton's method.

• Gray Code:
# Formula: `i ^ (i >> 1)`.

• Shuffle an Array:
# Implement the Fisher-Yates shuffle algorithm.


IX. Python Concepts

• Explain the GIL (Global Interpreter Lock):
# Conceptual: A mutex that allows only one thread to execute Python bytecode at a time in CPython.

• Difference between __str__ and __repr__:
# __str__ is for end-users (readable), __repr__ is for developers (unambiguous).

• Implement a Context Manager (with statement):
class MyContext:
def __enter__(self): # setup
return self
def __exit__(self, exc_type, exc_val, exc_tb): # teardown
pass

• Implement itertools.groupby logic:
# Iterate through the sorted iterable, collecting items into a sublist until the key changes.


#Python #CodingInterview #DataStructures #Algorithms #SystemDesign

━━━━━━━━━━━━━━━
By: @DataScience4
3
def process_data(data):
if data is None:
return "Error: No data provided."
if not isinstance(data, list) or not data:
return "Error: Invalid data format."

# ... logic is now at the top level ...
print("Processing data...")
return "Done"


#Python #CleanCode #Programming #BestPractices #CodingTips

━━━━━━━━━━━━━━━
By: @DataScience4
9. Use isinstance() for Type Checking
(It's safer and more robust than type() because it correctly handles inheritance.)

Cluttered Way (brittle, fails on subclasses):
class MyList(list): pass
my_list_instance = MyList()
if type(my_list_instance) == list:
print("It's a list!") # This will not print

Clean Way (correctly handles subclasses):
class MyList(list): pass
my_list_instance = MyList()
if isinstance(my_list_instance, list):
print("It's an instance of list or its subclass!") # This prints


10. Use the else Block in try/except
(Clearly separates the code that runs on success from the try block being monitored.)

Cluttered Way:
try:
data = my_ risky_operation()
# It's not clear if this next part can also raise an error
process_data(data)
except ValueError:
handle_error()

Clean Way:
try:
data = my_risky_operation()
except ValueError:
handle_error()
else:
# This code only runs if the 'try' block succeeds with NO exception
process_data(data)


#Python #CleanCode #Programming #BestPractices #CodeReadability

━━━━━━━━━━━━━━━
By: @DataScience4
10👍3
few-shot learning | AI Coding Glossary

📖 A setting where a model adapts to a new task using only a small number of labeled examples.

🏷️ #Python
Learning Common Algorithms with Python

• This lesson covers fundamental algorithms implemented in Python. Understanding these concepts is crucial for building efficient software. We will explore searching, sorting, and recursion.

Linear Search: This is the simplest search algorithm. It sequentially checks each element of the list until a match is found or the whole list has been searched. Its time complexity is O(n).

def linear_search(data, target):
for i in range(len(data)):
if data[i] == target:
return i # Return the index of the found element
return -1 # Return -1 if the element is not found

# Example
my_list = [4, 2, 7, 1, 9, 5]
print(f"Linear Search: Element 7 found at index {linear_search(my_list, 7)}")


Binary Search: A much more efficient search algorithm, but it requires the list to be sorted first. It works by repeatedly dividing the search interval in half. Its time complexity is O(log n).

def binary_search(sorted_data, target):
low = 0
high = len(sorted_data) - 1

while low <= high:
mid = (low + high) // 2
if sorted_data[mid] < target:
low = mid + 1
elif sorted_data[mid] > target:
high = mid - 1
else:
return mid # Element found
return -1 # Element not found

# Example
my_sorted_list = [1, 2, 4, 5, 7, 9]
print(f"Binary Search: Element 7 found at index {binary_search(my_sorted_list, 7)}")


Bubble Sort: A simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The process is repeated until the list is sorted. Its time complexity is O(n^2).

def bubble_sort(data):
n = len(data)
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
if data[j] > data[j+1]:
# Swap the elements
data[j], data[j+1] = data[j+1], data[j]
return data

# Example
my_list_to_sort = [4, 2, 7, 1, 9, 5]
print(f"Bubble Sort: Sorted list is {bubble_sort(my_list_to_sort)}")


Recursion (Factorial): Recursion is a method where a function calls itself to solve a problem. A classic example is calculating the factorial of a number (n!). It must have a base case to stop the recursion.

def factorial(n):
# Base case: if n is 1 or 0, factorial is 1
if n == 0 or n == 1:
return 1
# Recursive step: n * factorial of (n-1)
else:
return n * factorial(n - 1)

# Example
num = 5
print(f"Recursion: Factorial of {num} is {factorial(num)}")


#Python #Algorithms #DataStructures #Coding #Programming #LearnToCode

━━━━━━━━━━━━━━━
By: @DataScience4
1
reasoning model | AI Coding Glossary

📖 A generative model tuned to solve multi-step problems.

🏷️ #Python
chain of thought (CoT) | AI Coding Glossary

📖 A prompting technique that asks models to show intermediate steps, often improving multi-step reasoning but not guaranteeing accurate explanations.

🏷️ #Python
Interview Question

What is the potential pitfall of using a mutable object (like a list or dictionary) as a default argument in a Python function?

Answer: A common pitfall is that the default argument is evaluated only once, when the function is defined, not each time it is called. If that default object is mutable, any modifications made to it in one call will persist and be visible in subsequent calls.

This can lead to unexpected and buggy behavior.

Incorrect Example (The Pitfall):

def add_to_list(item, my_list=[]):
my_list.append(item)
return my_list

# First call seems to work fine
print(add_to_list(1)) # Output: [1]

# Second call has unexpected behavior
print(add_to_list(2)) # Output: [1, 2] -- The list from the first call was reused!

# Third call continues the trend
print(add_to_list(3)) # Output: [1, 2, 3]


The Correct, Idiomatic Solution:

The standard practice is to use None as the default and create a new mutable object inside the function if one isn't provided.

def add_to_list_safe(item, my_list=None):
if my_list is None:
my_list = [] # Create a new list for each call
my_list.append(item)
return my_list

# Each call now works independently
print(add_to_list_safe(1)) # Output: [1]
print(add_to_list_safe(2)) # Output: [2]
print(add_to_list_safe(3)) # Output: [3]


tags: #Python #Interview #CodingInterview #PythonTips #Developer #SoftwareEngineering #TechInterview

━━━━━━━━━━━━━━━
By: @DataScience4
2
How to Properly Indent Python Code

📖 Learn how to properly indent Python code in IDEs, Python-aware editors, and plain text editors—plus explore PEP 8 formatters like Black and Ruff.

🏷️ #basics #best-practices #python
1
😰 80 pages with problems, solutions, and code from a Python developer interview, ranging from simple to complex

⬇️ Save the PDF, it will come in handy!

#python #job
Please open Telegram to view this post
VIEW IN TELEGRAM
1
Editorial Guidelines

📖 See how Real Python's editorial guidelines shape comprehensive, up-to-date resources, with Python experts, educators, and editors refining all learning content.

🏷️ #Python
Send Feedback

📖 We welcome ideas, suggestions, feedback, and the occasional rant. Did you find a topic confusing? Or did you find an error in the text or code? Send us your feedback via this page.

🏷️ #Python