Code With Python
38.9K subscribers
823 photos
24 videos
21 files
739 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Media is too big
VIEW IN TELEGRAM
Channel owners rise up!

"I want to monetize my telegram channel". It is definitely possible!


Check our website, register your channel today!


Our community homeπŸ‘‡
https://t.iss.one/waybien









Sponsored By WaybienAds
✨ 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
Python tip:
Use f-strings for easy and readable string formatting.

name = "Alice"
age = 30
message = f"Hello, my name is {name} and I am {age} years old."
print(message)


Python tip:
Utilize list comprehensions for concise and efficient list creation.

numbers = [1, 2, 3, 4, 5]
squares = [x * x for x in numbers if x % 2 == 0]
print(squares)


Python tip:
Use enumerate() to iterate over a sequence while also getting the index of each item.

fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")


Python tip:
Use zip() to iterate over multiple iterables in parallel.

names = ["Alice", "Bob"]
ages = [25, 30]
for name, age in zip(names, ages):
print(f"{name} is {age} years old.")


Python tip:
Always use the with statement when working with files to ensure they are properly closed, even if errors occur.

with open("example.txt", "w") as f:
f.write("Hello, world!\n")
f.write("This is a test.")
# File is automatically closed here


Python tip:
Use *args to allow a function to accept a variable number of positional arguments.

def sum_all(*args):
total = 0
for num in args:
total += num
return total

print(sum_all(1, 2, 3))
print(sum_all(10, 20, 30, 40))


Python tip:
Use **kwargs to allow a function to accept a variable number of keyword arguments (as a dictionary).

def display_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

display_info(name="Bob", age=40, city="New York")


Python tip:
Employ defaultdict from the collections module to simplify handling missing keys in dictionaries by providing a default factory.

from collections import defaultdict

data = [("fruit", "apple"), ("vegetable", "carrot"), ("fruit", "banana")]
categorized = defaultdict(list)
for category, item in data:
categorized[category].append(item)
print(categorized)


Python tip:
Use if __name__ == "__main__": to define code that only runs when the script is executed directly, not when imported as a module.

def greet(name):
return f"Hello, {name}!"

if __name__ == "__main__":
print("Running directly as a script.")
print(greet("World"))
else:
print("This module was imported.")


Python tip:
Apply type hints to your code for improved readability, maintainability, and to enable static analysis tools.

def add(a: int, b: int) -> int:
return a + b

result: int = add(5, 3)
print(result)


#PythonTips #PythonProgramming #PythonForBeginners #PythonTricks #CodeQuality #Pythonic #BestPractices #LearnPython

━━━━━━━━━━━━━━━
By: @DataScience4 ✨
❀4
✨ 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
✨ Topic: Python Basics ✨

πŸ“– Begin your Python journey with these beginner-friendly tutorials. Learn fundamental Python concepts to kickstart your career. This foundation will equip you with the necessary skills to further advance your budding Python programming skills.

🏷️ #357_resources
✨ 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
πŸŽ‰ SPOTO Double 11 Mega Sale – Free IT Kits + Your Chance to Win!

πŸ”₯ IT Certs Have Never Been This Affordable β€” Don't Wait, Claim Your Spot!
πŸ’Ό Whether you're targeting #CCNA, #CCNP, #CCIE, #PMP, or other top #IT certifications,SPOTO offers the YEAR'S LOWEST PRICES on real exam dumps + 1-on-1 exam support!

πŸ‘‡ Grab Your Free Resources Now:
πŸ”— IT Certs E-book:https://bit.ly/49zHfxI
πŸ”—Test Your IT Skills for Free: https://bit.ly/49fI7Yu
πŸ”— AI & Machine Learning Kit: https://bit.ly/4p8BITr
πŸ”— Cloud Study Guide: https://bit.ly/43mtpen

🎁 Join SPOTO 11.11 Lucky Draw:
πŸ“± iPhone 17
πŸ›’ Amazon Gift Card $100
πŸ“˜ CCNA/PMP Course Training + Study Material + eBook
Enter the Draw πŸ‘‰: https://bit.ly/47HkoxV

πŸ‘₯ Join Study Group for Free Tips & Materials:
https://chat.whatsapp.com/LPxNVIb3qvF7NXOveLCvup

πŸŽ“ Get 1-on-1 Exam Help Now:
wa.link/88qwta

⏰ Limited Time Offer – Don't Miss Out! Act Now!

πŸ”” Subscribe now: Today, one gold trade changed everything. Find out how inside. | InsideAds
❔ Interview question

How do you create a new directory using the os module in Python, and what is the recommended way to handle cases where the directory might already exist?

Answer: The primary function to create a new directory (and any necessary parent directories) is os.makedirs(). To gracefully manage situations where the target directory might already exist without causing a FileExistsError, the recommended approach is to set the exist_ok parameter to True. This ensures that if the directory already exists, no exception is raised, allowing your program to continue execution smoothly. An example usage would be os.makedirs('path/to/my/new_directory', exist_ok=True).

tags: #interview #os #PythonBasics #FileSystem

━━━━━━━━━━━━━━━
By: @DataScience4 ✨
🐍 Python: Unobvious and Probable

Python, for all its readability and clear syntax, holds a treasury of less-trodden paths and nuanced behaviors that can catch even seasoned developers off guard. Understanding these intricacies deepens one's mastery and illuminates the language's design philosophy.

The Enigma of the ~ Operator: Bitwise NOT

Often overlooked outside of bit manipulation contexts, the unary ~ operator performs a bitwise NOT operation. For integers, its behavior can seem counter-intuitive at first glance.

Mathematically, ~x is equivalent to -(x+1).

x = 5
result = ~x
print(f"~{x} is {result}") # Output: ~5 is -6

y = -10
result = ~y
print(f"~{y} is {result}") # Output: ~-10 is 9


This behavior stems from how negative numbers are represented in two's complement form within computers. While its primary role is in low-level bitwise operations, it finds practical use in libraries like NumPy for inverting boolean arrays or selections, where ~ acts as a logical NOT.

import numpy as np

arr = np.array([True, False, True])
inverted_arr = ~arr
print(f"Original: {arr}, Inverted: {inverted_arr}") # Output: Original: [ True False True], Inverted: [False True False]

Its unobvious integer arithmetic hides a powerful, foundational operation.

all() and any() with Empty Sequences

The built-in functions all() and any() are crucial for evaluating the truthiness of elements within an iterable. Their behavior when faced with an empty sequence, however, is a classic source of mild confusion.

β€’ all(iterable) returns True if all elements of the iterable are truthy (or if the iterable is empty).
β€’ any(iterable) returns True if any element of the iterable is truthy (and False if the iterable is empty).

empty_list = []
print(f"all({empty_list}) is {all(empty_list)}") # Output: all([]) is True
print(f"any({empty_list}) is {any(empty_list)}") # Output: any([]) is False

truthy_list = [1, True, 'hello']
print(f"all({truthy_list}) is {all(truthy_list)}") # Output: all([1, True, 'hello']) is True
print(f"any({truthy_list}) is {any(truthy_list)}") # Output: any([1, True, 'hello']) is True

mixed_list = [0, 1, '', True]
print(f"all({mixed_list}) is {all(mixed_list)}") # Output: all([0, 1, '', True]) is False
print(f"any({mixed_list}) is {any(mixed_list)}") # Output: any([0, 1, '', True]) is True


The result all([]) being True is an example of a "vacuously true" statement: there are no falsy elements in an empty list, so the condition "all elements are truthy" holds. This design prevents unexpected errors in loops or conditional checks where an empty sequence might otherwise break logic. any([]) being False is straightforward: there are no elements to be truthy.

The is vs. == for Small Integers and Strings

Python has two primary ways to check for equality: == (value equality) and is (identity equality, checking if two variables refer to the exact same object in memory). While is is generally reserved for None or specific memory optimizations, CPython exhibits an unobvious caching behavior for certain immutable objects.
❀1
a = 100
b = 100
print(f"a == b: {a == b}") # Output: a == b: True
print(f"a is b: {a is b}") # Output: a is b: True (for integers -5 to 256)

c = 300
d = 300
print(f"c == d: {c == d}") # Output: c == d: True
print(f"c is d: {c is d}") # Output: c is d: False (for integers outside -5 to 256)

s1 = "hello"
s2 = "hello"
print(f"s1 is s2: {s1 is s2}") # Output: s1 is s2: True (string interning for short, simple strings)

s3 = "hello world!"
s4 = "hello world!"
print(f"s3 is s4: {s3 is s4}") # Output: s3 is s4: False (interring not guaranteed for complex strings)

CPython pre-allocates and caches integer objects in the range of -5 to 256. Similarly, short, simple string literals are often "interned" for performance. This means that multiple references to these specific values will point to the same object in memory, making is return True. This is an implementation detail and should not be relied upon for general equality checks, where == is the correct semantic choice.

Mutable Default Arguments

A common pitfall for new and experienced developers alike arises from mutable objects used as default arguments in function definitions. Default arguments are evaluated once when the function is defined, not on each call.

def add_item_to_list(item, data=[]):
data.append(item)
return data

list1 = add_item_to_list(1)
print(f"List 1: {list1}") # Output: List 1: [1]

list2 = add_item_to_list(2)
print(f"List 2: {list2}") # Output: List 2: [1, 2] - Unobvious! `data` is the same list object as before.

list3 = add_item_to_list(3, []) # Passed a new list
print(f"List 3: {list3}") # Output: List 3: [3]
print(f"List 2 after List 3: {list2}") # Output: List 2 after List 3: [1, 2] - Unchanged.

The "unobvious" part is that data in the list2 call is the same list object that was modified by list1. The standard workaround is to use None as a sentinel value:

def add_item_to_list_safe(item, data=None):
if data is None:
data = []
data.append(item)
return data

list4 = add_item_to_list_safe(1)
print(f"List 4 (safe): {list4}") # Output: List 4 (safe): [1]

list5 = add_item_to_list_safe(2)
print(f"List 5 (safe): {list5}") # Output: List 5 (safe): [2] - Now as expected.


Chained Comparisons

Python allows for elegant chained comparisons, which can sometimes surprise those accustomed to other languages that require explicit logical operators (and, &&).

x = 7

# Traditional (and verbose)
if 0 < x and x < 10:
print("x is between 0 and 10 (exclusive) - traditional")

# Python's elegant chained comparison
if 0 < x < 10:
print("x is between 0 and 10 (exclusive) - chained")

# More complex chaining
a, b, c = 1, 2, 3
if a < b == c:
print("a is less than b, and b is equal to c") # False, since b != c

This unobvious syntactic sugar evaluates from left to right, short-circuiting if any comparison is false. It is equivalent to (0 < x) and (x < 10) but offers a cleaner, more mathematical notation.

The Walrus Operator (:=)

Introduced in Python 3.8, the assignment expression operator :=, informally known as the "walrus operator," allows you to assign a value to a variable as part of an expression. This can lead to more concise code in situations where you would otherwise repeat an expression or assign it on a separate line.
❀1
# Without walrus
data = [1, 2, 3]
n = len(data)
if n > 0:
print(f"List has {n} items")

# With walrus
if (n := len(data)) > 0:
print(f"List has {n} items")

# In a loop condition
records = [("Alice", 30), ("Bob", 25), ("Charlie", 35)]
processed_records = []
while (record := records.pop()) if records else None: # Unobvious but powerful loop condition
processed_records.append(record)
print(f"Processing: {record}")
print(f"Processed all: {processed_records}")

The := operator enables patterns that are less common in earlier Python versions, making code more dense and, at times, more efficient by avoiding redundant computations, but it requires a slightly different way of thinking about expressions.

Conclusion

Python's journey from a simple scripting language to a powerhouse for diverse applications has imbued it with a rich set of features. Exploring these unobvious behaviors, from the mathematical elegance of ~ and the logical quirks of all() with empty sequences to the subtle optimizations of object caching and the syntactic conciseness of chained comparisons and the walrus operator, strengthens a developer's grasp of the language's core. These nuances are not merely trivia; they are cornerstones for writing robust, efficient, and truly Pythonic code.

---
tags: python, programming, unobvious, nuances, features, operators, all, any, bitwise, walrus, is, equals, mutable defaults

━━━━━━━━━━━━━━━
By: @DataScience4 ✨
❀4
✨ The Python Standard REPL: Try Out Code and Ideas Quickly ✨

πŸ“– The Python REPL gives you instant feedback as you code. Learn to use this powerful tool to type, run, debug, edit, and explore Python interactively.

🏷️ #intermediate #tools
❀3
πŸ† Connecting Python to MySQL: `mysql-connector`

πŸ“’ Master connecting Python to MySQL with `mysql-connector-python`, and troubleshoot 'module not found' errors for seamless data integration.

⚑ Tap to unlock the complete answer and gain instant insight.

━━━━━━━━━━━━━━━
By: @DataScience4 ✨
❀2πŸ‘1
πŸ† Generate Website Screenshots with Python Flask

πŸ“’ Generate website screenshots effortlessly! Learn to build your own tool using Python and Flask. Essential for web development and QA.

⚑ Tap to unlock the complete answer and gain instant insight.

━━━━━━━━━━━━━━━
By: @DataScience4 ✨
✨ How to Convert Bytes to Strings in Python ✨

πŸ“– Turn Python bytes to strings, pick the right encoding, and validate results with clear error handling strategies.

🏷️ #basics
❀1
Tip for clean tests in Python:

In most cases, your tests should cover:

- all happy path scenarios
- edge/corner/boundary cases
- negative tests
- security checks and invalid inputs

import uuid
from dataclasses import dataclass
from typing import Optional


@dataclass
class User:
    username: str


class InMemoryUserRepository:
    def __init__(self):
        self._users = []

    def add(self, user: User) -> None:
        self._users.append(user)

    def search(self, query: Optional[str] = None) -> list[User]:
        if query is None:
            return self._users
        else:
            return [
                user
                for user in self._users
                if query in user.username
            ]


# happy path
def test_search_users_without_query_lists_all_users():
    user1 = User(username="[email protected]")
    user2 = User(username="[email protected]")
    repository = InMemoryUserRepository()
    repository.add(user1)
    repository.add(user2)

    assert repository.search() == [user1, user2]


# happy path
def test_search_users_with_email_part_lists_all_matching_users():
    user1 = User(username="[email protected]")
    user2 = User(username="[email protected]")
    user3 = User(username="[email protected]")
    repository = InMemoryUserRepository()
    repository.add(user1)
    repository.add(user2)
    repository.add(user3)

    assert repository.search("doe") == [user1, user3]


# edge test case
def test_search_users_with_empty_query_lists_all_users():
    user1 = User(username="[email protected]")
    user2 = User(username="[email protected]")
    repository = InMemoryUserRepository()
    repository.add(user1)
    repository.add(user2)

    assert repository.search("") == [user1, user2]


# negative test case
def test_search_users_with_random_query_lists_zero_users():
    user1 = User(username="[email protected]")
    repository = InMemoryUserRepository()
    repository.add(user1)

    assert repository.search(str(uuid.uuid4())) == []


# security test
def test_search_users_with_sql_injection_has_no_effect():
    user1 = User(username="[email protected]")
    repository = InMemoryUserRepository()
    repository.add(user1)

    repository.search("DELETE FROM USERS;")
    assert repository.search() == [user1]


πŸ‘‰ @DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
❀5πŸ‘3
✨ Quiz: How to Serve a Website With FastAPI Using HTML and Jinja2 ✨

πŸ“– Review how to build dynamic websites with FastAPI and Jinja2, and serve HTML, CSS, and JS with HTMLResponse and StaticFiles.

🏷️ #intermediate #api #front-end #web-dev
✨ How to Use Google's Gemini CLI for AI Code Assistance ✨

πŸ“– Learn how to use Gemini CLI to bring Google's AI-powered coding assistance directly into your terminal to help you analyze and fix code.

🏷️ #intermediate #ai #tools