Monday, 4 May 2026

Python Coding challenge - Day 1137| What is the output of the following Python Code?

 


Code Explanation:

๐Ÿ”น 1. Initializing the List
funcs = []
An empty list funcs is created
This list will store functions

๐Ÿ”น 2. Starting the Loop
for i in range(3):

Loop runs 3 times with values:

i = 0, 1, 2

๐Ÿ”น 3. Defining the Function Inside Loop
def f():
    return i
A function f is defined in each iteration
⚠️ Important: The function does not store the current value of i immediately
Instead, it refers to i (late binding)

๐Ÿ‘‰ This means:

All functions will look up i when they are called, not when they are created

๐Ÿ”น 4. Appending Function to List
funcs.append(f)
The function f is added to the list
After loop ends, funcs contains 3 functions

๐Ÿ”น 5. After Loop Ends
Final value of i is:
i = 2
All functions refer to this same i

๐Ÿ”น 6. Calling the Functions
print([f() for f in funcs])
Each function is called one by one
Each function returns the current value of i, which is 2

๐Ÿ”น ✅ Final Output
[2, 2, 2]

Python Coding challenge - Day 1136| What is the output of the following Python Code?

 


Code Explanation:

๐Ÿ”น 1. Metaclass Definition
class Meta(type):
    def __new__(cls, name, bases, dct):
        dct['x'] = 100
        return super().__new__(cls, name, bases, dct)
Meta is a metaclass (inherits from type)
__new__ runs when a class is being created, not an object
It receives the class attributes in dct

It modifies the class dictionary by setting:

x = 100

๐Ÿ”น 2. Class Creation (A)
class A(metaclass=Meta):
    x = 10
Python sends this class definition to the metaclass

Internally:

Meta.__new__(Meta, 'A', (), {'x': 10})

The metaclass changes:

{'x': 10} → {'x': 100}

๐Ÿ”น 3. Final Class Structure

After metaclass processing, class A becomes:

class A:
    x = 100
The original x = 10 is overwritten

๐Ÿ”น 4. Object Creation
obj = A()
Creates an instance of class A
obj itself has no x attribute

๐Ÿ”น 5. Attribute Lookup
print(obj.x)
Python checks:
obj → not found
class A → finds x = 100

๐Ÿ”น ✅ Final Output
100

Sunday, 3 May 2026

๐Ÿš€ Day 38/150 – Prime Number Check in Python

 

๐Ÿš€ Day 38/150 – Prime Number Check in Python

A Prime Number is a number greater than 1 that has only two factors: 1 and itself.

Examples:
2, 3, 5, 7, 11 → Prime Numbers
4, 6, 8, 9 → Not Prime Numbers

Let’s explore different ways to check prime number in Python ๐Ÿ‘‡

๐Ÿ”น Method 1 – Using for Loop

n = 7 is_prime = True if n <= 1: is_prime = False else: for i in range(2, n): if n % i == 0: is_prime = False break print("Prime Number" if is_prime else "Not Prime Number")

Simple beginner-friendly method.


๐Ÿ”น Method 2 – Taking User Input

n = int(input("Enter a number: ")) is_prime = True if n <= 1: is_prime = False else: for i in range(2, n): if n % i == 0: is_prime = False break print("Prime Number" if is_prime else "Not Prime Number")

Useful when you want to test different numbers.

๐Ÿ”น Method 3 – Optimized Using √n

n = 29 is_prime = True if n <= 1: is_prime = False else: for i in range(2, int(n ** 0.5) + 1): if n % i == 0: is_prime = False break print("Prime Number" if is_prime else "Not Prime Number")

 More efficient because factors repeat after the square root.


๐Ÿ”น Method 4 – Using while Loop

n = 13 i = 2 is_prime = True if n <= 1: is_prime = False else: while i < n: if n % i == 0: is_prime = False break i += 1 print("Prime Number" if is_prime else "Not Prime Number")

Same logic, just using a different loop.


๐Ÿ’ก Key Takeaways
    1)Prime numbers have exactly two factors
    2)Numbers less than or equal to 1 are not prime
    3)Checking up to √n is faster than checking all numbers
    4)The optimized method is better for larger values









๐Ÿš€ Day 39/150 – Print Prime Numbers in a Range in Python

 


๐Ÿš€ Day 39/150 – Print Prime Numbers in a Range in Python

Prime numbers are numbers greater than 1 that have only two factors: 1 and itself.

Examples:

2, 3, 5, 7, 11, 13...

Let’s explore different ways to print prime numbers in a given range using Python ๐Ÿ‘‡

๐Ÿ”น Method 1 – Using for Loop

start = 1 end = 20 for num in range(start, end + 1): if num > 1: for i in range(2, num): if num % i == 0: break else: print(num, end=" ")





✅ Simple beginner-friendly method.

๐Ÿ”น Method 2 – Taking User Input

start = int(input("Enter start: ")) end = int(input("Enter end: ")) for num in range(start, end + 1): if num > 1: for i in range(2, num): if num % i == 0: break else: print(num, end=" ")





✅ Useful for dynamic programs.

๐Ÿ”น Method 3 – Optimized Using √n

start = 1 end = 50 for num in range(start, end + 1): if num > 1: is_prime = True for i in range(2, int(num ** 0.5) + 1): if num % i == 0: is_prime = False break if is_prime: print(num, end=" ")





✅ Faster for larger ranges.



๐Ÿ”น Method 4 – Using Function

def is_prime(n): if n <= 1: return False for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False return True for num in range(1, 21): if is_prime(num): print(num, end=" ")






✅ Clean and reusable.


๐ŸŽฏ Output

2 3 5 7 11 13 17 19


๐Ÿ”‘ Key Takeaways

  • Prime numbers are greater than 1.
  • Use nested loops to test each number.
  • Check till √n for optimization.
  • Functions make code reusable.

Python Coding Challenge - Question with Answer (ID -030526)

 


Explanation:

๐Ÿ”น Line 1: Creating the List
x = [1, 2, 3]
A list named x is created.
It contains three elements:
Index 0 → 1
Index 1 → 2
Index 2 → 3

๐Ÿ”น Line 2: The Print Statement
print(x.pop(1) + x[1])

Let’s break this into parts:

๐Ÿ”ธ Step 1: x.pop(1)
The pop(1) function:
Removes the element at index 1
Returns the removed value

๐Ÿ‘‰ Before pop: x = [1, 2, 3]
๐Ÿ‘‰ After pop: x = [1, 3]
๐Ÿ‘‰ Returned value: 2

๐Ÿ”ธ Step 2: x[1]
Now the updated list is [1, 3]
x[1] refers to the element at index 1

๐Ÿ‘‰ x[1] = 3

๐Ÿ”ธ Step 3: Addition
2 + 3 = 5

๐Ÿ”น Final Output
5

Saturday, 2 May 2026

๐Ÿš€ Day 37/150 – Multiplication Table in Python

 

๐Ÿš€ Day 37/150 – Multiplication Table in Python

A multiplication table shows the result of multiplying a number with a series of numbers.

Example for 5:

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

Let’s explore different ways to print multiplication table in Python ๐Ÿ‘‡


๐Ÿ”น Method 1 – Using for Loop

n = 5 for i in range(1, 11): print(n, "x", i, "=", n * i)



✅ Most common and easiest method.

๐Ÿ”น Method 2 – Taking User Input

n = int(input("Enter a number: ")) for i in range(1, 11): print(n, "x", i, "=", n * i)



✅ Useful for dynamic tables.

๐Ÿ”น Method 3 – Using while Loop

n = 5 i = 1 while i <= 10: print(n, "x", i, "=", n * i) i += 1




✅ Good for loop practice.


๐Ÿ”น Method 4 – Using List Comprehension

n = 5 table = [n * i for i in range(1, 11)] print(table)



✅ Creates values as a list.


๐Ÿ”น Method 5 – Using Function

def table(n): for i in range(1, 11): print(f"{n} x {i} = {n * i}") table(5)



✅ Reusable and clean method.


๐ŸŽฏ Output

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50

๐Ÿ”‘ Key Takeaways

  • Use for loop for fixed repetitions.
  • Use while loop for manual control.
  • f-strings make output cleaner.
  • Functions help reuse code.

๐Ÿ”ฅ Follow for Day 38/150 – Prime Number Check in Python


100 Python Projects — From Beginner to Expert

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (255) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (29) Azure (10) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (32) Data Analytics (22) data management (15) Data Science (354) Data Strucures (17) Deep Learning (159) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (73) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (42) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (293) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (33) pytho (1) Python (1338) Python Coding Challenge (1134) Python Mathematics (1) Python Mistakes (51) Python Quiz (494) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)