Quick check: what’s the type of bin(10)? int? float? Something else? It’s str. bin(), oct(), and hex() always return strings. That’s why bin(10) + 5 fails and why you use int(bin(10), 2) when you need a number again. I wrote a complete guide to Python base conversion functions so you can: ✓ Use bin(), oct(), and hex() correctly ✓ Know they return strings (and convert back when needed) ✓ Avoid float/complex (only int and bool work) ✓ Fix the usual mistakes and read the common errors ✓ See practical use (e.g. colors, permissions) and try exercises ~23 min read, with examples and solutions. Link: https://lnkd.in/dnUxjXBB #Python #LearnPython #Programming #Tech
Vimal Thapliyal’s Post
More Relevant Posts
-
One difference that trips people up in Python: / always returns a float. // always returns an integer. So 10 / 2 is 5.0, not 5. If you want a whole number, use //. Same with %: it gives the remainder, not the quotient. So 15 % 4 is 3 (the remainder), and 15 // 4 is 3 (how many times 4 fits). Together: quotient × divisor + remainder = dividend. I put together a complete guide to all seven arithmetic operators: +, -, , , /, //, %. With examples, float vs floor division, modulus, power, and common mistakes. Read it here: https://lnkd.in/gdP_qRbX #Python #Coding #Developer
To view or add a comment, sign in
-
I recently conducted a benchmark comparing Python 3.13 and 3.14 on the same CPU-heavy task, initially out of curiosity. The results were surprising; the performance difference was significant and has changed my perspective on parallelism in Python. While optimizing a CPU-bound data pipeline, my usual approach was to use ProcessPoolExecutor. Although it effectively handles tasks, the OS-level process spawn cost can accumulate quickly. Python 3.14 introduced a new option: InterpreterPoolExecutor. This allows for multiple isolated Python interpreters within the same process, eliminating GIL conflicts. I benchmarked the performance of Python 3.13 versus 3.14 as follows: ───────────────────────────────────────── 📊 1. HEAVY CPU TASKS (8 tasks, 4 workers) 🔴 Threads: 2.519s (GIL serializes everything) 🟠 Processes: 1.222s (parallel, but costly to spawn) 🟢 Subinterpreters: 1.130s (parallel and lighter) ───────────────────────────────────────── ⚡ 2. STARTUP COST (50 tiny tasks, where it really shows) 🟠 Processes: 0.271s 🟢 Subinterpreters: 0.128s (about 2x faster to start) 📈 3. SCALING (1 → 8 workers) 🔴 Threads: flatlined at ~1.9s (no real scaling benefit) 🟢 Subinterpreters: 2.16s → 0.91s (close to linear scaling) ───────────────────────────────────────── The key takeaway is that we can achieve process-level parallelism with thread-like startup speed, without GIL contention or extra process memory overhead, all within the standard library. Are you still using ProcessPoolExecutor for CPU-bound work? I am genuinely interested in whether subinterpreters could be a practical improvement in your stack. #Python #Python314 #SoftwareEngineering #Performance #Concurrency #BackendDevelopment #DataEngineering
To view or add a comment, sign in
-
-
💡 Understanding Default Parameters in Python While working with functions in Python, default parameters can make our code more flexible and easier to use. Let’s look at this simple example: def func(a, b=2, c=3): return a + b * c print(func(2, c=4)) 1️⃣ Step 1: Function Definition The function func has three parameters: • a • b with a default value of 2 • c with a default value of 3 ➡️ This means that if we call the function without providing values for b or c, Python will automatically use their default values. 2️⃣ Step 2: Calling the Function func(2, c=4) Here is what happens: • The value 2 is assigned to a. • We did not pass a value for b, so Python uses the default value 2. • We explicitly passed c = 4, which overrides the default value 3. So the values inside the function become: a = 2 b = 2 c = 4 3️⃣ Step 3: Evaluating the Expression The function returns: a + b * c Substituting the values: 2 + 2 * 4 According to Python’s order of operations, multiplication happens before addition: 2 + 8 = 10 ➡️ Final Output: 10 🔹 Important Concept Default parameters allow functions to work with optional arguments. They make functions more flexible, cleaner, and easier to reuse. #Python #Programming #AI #DataAnalytics #Coding #LearnPython
To view or add a comment, sign in
-
One rule that saves a lot of bugs in Python: input() always gives you a string. So age = input("Age? ") then age + 5 will error until you do age = int(age). Type conversion is everywhere: string → int for math, int → str for printing, and knowing what each of int(), float(), complex(), bool(), and str() accepts (and what they don’t). I put together a complete guide: explicit vs implicit, all five functions, a “what can convert to what” table, rules, mistakes, and exercises. Read it here: https://lnkd.in/dqSMhkpi #Python #Coding #Developer
To view or add a comment, sign in
-
How do "try" and "except" work in Python for handling errors? In Python, errors can occur during program execution (called exceptions). If they are not handled properly, the program may stop unexpectedly. This is where try and except statements come in. 🔹 "try" Used to wrap the code that might raise an error. 🔹 "except" Used to handle the error if it occurs, preventing the program from crashing. Example: try: x = int(input("Enter a number: ")) print(10 / x) except ValueError: print("Invalid input") except ZeroDivisionError: print("Cannot divide by zero") Python also provides additional clauses to make error handling more powerful: ▪ "else" → runs only if no exception occurs ▪ "finally" → always runs (useful for closing files or cleaning resources) ▪ "raise" → allows developers to trigger custom exceptions Understanding exception handling is essential for writing reliable and robust Python applications. #Python #AI #DataScience #Analytics #Programming #MachineLearning #Instant
To view or add a comment, sign in
-
🚀 Day 2/30 📝 Python Operators - Basics As part of my #30DaysOfPython Today I learned about Python Operators. Operators are symbols that perform operations on variables and values. 🔹 1️⃣ Arithmetic Operators Used for mathematical calculations: + Addition - Subtraction * Multiplication / Division % Modulus (remainder) // Floor division ** Exponent (power) Example: a = 10 b = 3 print(a + b) # 13 print(a / b) # 3.333 print(a // b) # 3 print(a % b) # 1 print(a ** b) # 1000 --- 🔹 2️⃣ Assignment Operators Used to assign and update values: = += -= = /= %= //= *= Example: a = 5 a += 2 # a = 7 --- 🔹 3️⃣ Relational (Comparison) Operators Used to compare two values and return True/False: == != > < >= <= --- 🔹 4️⃣ Logical Operators Used with conditions: and or not --- 🔹 5️⃣ Bitwise & Shift Operators Work at binary level: & | ^ ~ << >> Example: 5 << 2 # 20 5 >> 1 # 2 --- 💡 What I Learned Today: Different types of operators perform different tasks. Some operators work on numbers, some on conditions, and some at binary level. Understanding operators helps in writing logical programs. Excited to continue learning rocket🚀 #Day2✅ #Python #PythonBasics #Operators #LearningJourney #Coding #30DaysOfPython
To view or add a comment, sign in
-
-
Python 3.15 is getting a built-in frozendict. At first glance, it may look like a small addition. It’s not. Why does this matter? • Truly immutable mappings can be safely shared between interpreters • They simplify work in free-threaded environments • They allow hashability when keys and values are hashable • They reduce accidental mutation in large systems types.MappingProxyType was only surface-immutable. Now we get a real, hashable, immutable dict. From an architecture perspective, this is more important than it seems. Most large Python systems don’t break because of Python. They break because of uncontrolled mutability. In architecture reviews, hidden state and implicit contracts are among the most common sources of systemic complexity. Immutability is not just a language feature. It’s an architectural constraint. And constraints create clarity. Curious how people see this shift. Do you use immutability deliberately in Python-heavy systems?
To view or add a comment, sign in
-
Immutability is not just a language feature. It is an architectural constraint. In large Python systems, uncontrolled mutability is a recurring source of systemic complexity. Python 3.15 introducing a built-in frozendict may look incremental. From an architecture perspective, it is not. Constraints create clarity. Clarity reduces long-term complexity.
Python 3.15 is getting a built-in frozendict. At first glance, it may look like a small addition. It’s not. Why does this matter? • Truly immutable mappings can be safely shared between interpreters • They simplify work in free-threaded environments • They allow hashability when keys and values are hashable • They reduce accidental mutation in large systems types.MappingProxyType was only surface-immutable. Now we get a real, hashable, immutable dict. From an architecture perspective, this is more important than it seems. Most large Python systems don’t break because of Python. They break because of uncontrolled mutability. In architecture reviews, hidden state and implicit contracts are among the most common sources of systemic complexity. Immutability is not just a language feature. It’s an architectural constraint. And constraints create clarity. Curious how people see this shift. Do you use immutability deliberately in Python-heavy systems?
To view or add a comment, sign in
-
You can tell a lot about a Python developer by how they use lists. Beginners see lists as a place to store values. Experienced developers see them as a tool to control flow, shape data, and simplify logic. append() is not just adding data. It’s building sequences step by step. pop() is not just removing elements. It’s controlling state. insert(), extend(), remove() small methods, but they quietly influence how clean or messy your code becomes. The interesting thing about Python is this: Many powerful programming habits start with very simple tools. Lists are usually the first data structure we learn. But they’re also one of the ones we keep using for years. Simple syntax. Serious power. Sometimes the most “basic” features in Python are the ones you never outgrow. #Python #DataScience #Ai #Lists
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭𝟴: 𝗳𝗼𝗿–𝗲𝗹𝘀𝗲 𝗶𝗻 𝗣𝘆𝘁𝗵𝗼𝗻 In Python, a loop can have an else block. The else runs only if the loop finishes normally, meaning it was not stopped using break. Basic structure: for i in range(5): print(i) else: print("Loop completed") output: 0 1 2 3 4 Loop completed Since there is no break, the else block executes. Now look at this: for i in range(5): if i == 3: break else: print("Loop completed") output: 0 1 2 Here, else will not execute because the loop stopped early using break. Real-world Example: Searching for an element numbers = [2, 4, 6, 8] for num in numbers: if num == 5: print("Found") break else: print("Not Found") output:Not Found If 5 is not in the list, the loop completes fully, and else runs. Why is this useful? • Searching operations • Validation checks • Avoiding extra flag variables • Without for-else, many beginners use a flag variable to track results. • With for-else, Python handles it cleanly. Important: • else runs only if no break occurs • Works with both for and while loops It’s a small concept, but it makes your code more elegant and readable. #Python #LearnPython #CodingJourney #Programming
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development