Sunday, 10 May 2026

πŸš€ Day 43/150 – Power of a Number in Python

 

πŸš€ Day 43/150 – Power of a Number in Python

Finding the power of a number means raising a number to an exponent.

Example:
2³ = 2 × 2 × 2 = 8
5² = 25

Let’s explore different ways to calculate power in Python πŸ‘‡


πŸ”Ή Method 1 – Using ** Operator

base = 2 exp = 3 result = base ** exp print("Power:", result)






✅ Easiest and most common method.

πŸ”Ή Method 2 – Using pow() Function

base = 2 exp = 3 result = pow(base, exp) print("Power:", result)






✅ Built-in function for power calculation.

πŸ”Ή Method 3 – Using Loop

base = 2 exp = 3 result = 1 for i in range(exp): result *= base print("Power:", result)





✅ Good for understanding logic.


πŸ”Ή Method 4 – Taking User Input

base = int(input("Enter base: ")) exp = int(input("Enter exponent: ")) print("Power:", base ** exp)



✅ Dynamic version.


πŸ”Ή Method 5 – Using Recursion

def power(base, exp): if exp == 0: return 1 return base * power(base, exp - 1) print(power(2, 3))




✅ Great for learning recursion.


πŸ”Ή Output

Power: 8

πŸ”₯ Key Takeaways

✔️ ** is the simplest way
✔️ pow() is built-in alternative
✔️ Loops help understand logic
✔️ Recursion builds concepts

Saturday, 9 May 2026

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

 


Explanation:

πŸ”Ή Step 1: Create Tuple
x = ([],)
x is a tuple
Tuple is immutable ❗
Inside tuple:
[]

is a mutable list

πŸ‘‰ Current value:

([],)

πŸ”Ή Step 2: Execute x[0] += [1]
x[0] += [1]

This line is tricky 😈

Python internally performs TWO actions.

⚡ Step 2.1: Modify the List
[] += [1]

This updates list in-place.

πŸ‘‰ List becomes:

[1]

So internally:

x → ([1],)
⚡ Step 2.2: Try Reassignment

After modifying list, Python also tries:

x[0] = [1]

⚠️ But tuple is immutable ❌

Tuple does NOT allow item assignment.

πŸ”Ή Step 3: Error Occurs

Python raises:

TypeError

πŸ‘‰ Program stops here

πŸ”Ή Step 4: print(x) Never Executes
print(x)

This line is never reached because error already happened.

⚡ Important Twist 😈

Even though error occurs:
πŸ‘‰ list WAS modified before error

Internally tuple becomes:

([1],)

But print never runs.

πŸ”₯ Final Output
TypeError

Friday, 8 May 2026

πŸš€ Day 42/150 – ASCII Value of a Character in Python

 

πŸš€ Day 42/150 – ASCII Value of a Character in Python

The ASCII value is the numeric code assigned to characters.

Example:
A = 65
a = 97
0 = 48

Let’s explore different ways to find ASCII value in Python πŸ‘‡

πŸ”Ή Method 1 – Using ord()

ch = 'A' print("ASCII Value:", ord(ch))




✅ ord() returns the ASCII/Unicode value.

πŸ”Ή Method 2 – Taking User Input

ch = input("Enter a character: ") print("ASCII Value:", ord(ch))



✅ Dynamic version.


πŸ”Ή Method 3 – Using Function

def get_ascii(ch): return ord(ch) print(get_ascii('a'))



✅ Reusable approach.


πŸ”Ή Method 4 – Using Loop for String

text = "ABC" for ch in text: print(ch, "=", ord(ch))



✅ Useful for multiple characters.


πŸ”Ή Method 5 – Using Dictionary Comprehension

chars = ['A', 'B', 'C'] ascii_values = {ch: ord(ch) for ch in chars} print(ascii_values)




✅ Great for storing multiple values.

πŸ”Ή Output

ASCII Value: 65

πŸ”₯ Key Takeaways

✔️ Use ord() to get ASCII value
✔️ Works for letters, digits, symbols
✔️ Useful in encoding problems
✔️ Can handle single or multiple characters

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

 


Explanation:

πŸ”Ή Step 1: Create Generator

x = (i for i in range(4))

This creates a generator object

Values inside generator:

0, 1, 2, 3

⚠️ Important:

Generator values are produced one by one
Once used → they disappear 😈

πŸ”Ή Step 2: Execute sum(x)
sum(x)

Python starts consuming generator:

0 + 1 + 2 + 3 = 6

πŸ‘‰ Result:

6

πŸ”Ή Step 3: Generator Gets Exhausted

After sum(x):

x → empty generator

⚠️ All values already consumed

Generator now has:

nothing left

πŸ”Ή Step 4: Execute list(x)
list(x)

But generator already empty ❗

So:

[]

πŸ”Ή Step 5: Print Final Output
print(6, [])

πŸ‘‰ Final Output:

6 []


Python Coding Challenge - (ID -070526)



Explanation:

πŸ”Ή Step 1: Understand Operator Priority

and is evaluated before or

So Python reads:

([] and 5) or {} or 7


πŸ”Ή Step 2: Evaluate [] and 5

[] and 5

πŸ‘‰ [] is an empty list

Empty list = Falsy ❌

For and:

If first value is falsy → return it immediately

πŸ‘‰ Result:

[]


πŸ”Ή Step 3: Now Expression Becomes

[] or {} or 7


πŸ”Ή Step 4: Evaluate [] or {}

πŸ‘‰ First value:

[]

Empty list = Falsy ❌

Move to next value

πŸ‘‰ Second value:

{}

Empty dictionary = Falsy ❌

Move to next value


πŸ”Ή Step 5: Evaluate 7

7

7 is Truthy ✅

For or:

Python returns the first truthy value

πŸ‘‰ Result:

7 

Wednesday, 6 May 2026

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

 


 Code Explanation:

πŸ”Ή 1. Outer Function Definition
def outer(x):
✅ Explanation:
A function outer is defined.
It takes one argument: x.
This function will return another function.

πŸ”Ή 2. Inner Function Definition
def inner(y):
✅ Explanation:
Inside outer, another function inner is defined.
It takes parameter y.

πŸ”Ή 3. Using Outer Variable Inside Inner
return x + y
✅ Explanation:
inner uses:
y → its own parameter
x → from outer function

πŸ‘‰ This is called a closure:

Inner function remembers the value of x even after outer finishes

πŸ”Ή 4. Returning Inner Function
return inner
✅ Explanation:
outer does NOT return a value directly
It returns the function inner itself

πŸ”Ή 5. Calling Outer Function
f = outer(5)
πŸ” What happens:
outer(5) is executed
x = 5
Returns inner function

πŸ‘‰ Now:

f → inner (with x = 5 stored)

πŸ”Ή 6. Calling Returned Function
print(f(3))
πŸ” What happens:
Calls:
inner(3)

Inside inner:

x = 5 (remembered from closure)
y = 3
Calculation:
5 + 3 = 8
🎯 Final Output
8

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

 


Explanation:

πŸ”Ή Step 1: Create Generator

x = (i for i in range(4))
This creates a generator object
Values generated:
0, 1, 2, 3

⚠️ Important:

Generator values are used only once
After consuming values → generator becomes empty 😈

πŸ”Ή Step 2: Execute sum(x)
sum(x)

Python starts consuming generator values:

0 + 1 + 2 + 3 = 6

πŸ‘‰ Result:

6

⚠️ Now generator x is exhausted:

x → empty

πŸ”Ή Step 3: Execute max(x, default=0)
max(x, default=0)

But generator already consumed all values ❗

So internally:

max([], default=0)

πŸ‘‰ Since generator is empty:

default=0 is returned

πŸ‘‰ Result:

0

πŸ”Ή Step 4: Print Final Output
print(6, 0)

πŸ‘‰ Output:

6 0

Book: Medical Research with Python Tools

πŸš€ Day 41/150 – Find LCM of Two Numbers in Python

 

πŸš€ Day 41/150 – Find LCM of Two Numbers in Python

The LCM (Least Common Multiple) of two numbers is the smallest number that is divisible by both numbers.

Example:
LCM of 4 and 6 = 12

Let’s explore different ways to find LCM in Python πŸ‘‡

πŸ”Ή Method 1 – Using Loop

a = 4 b = 6 greater = max(a, b) while True: if greater % a == 0 and greater % b == 0: print("LCM:", greater) break greater += 1










✅ Starts from the greater number and checks multiples.

πŸ”Ή Method 2 – Taking User Input

a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) greater = max(a, b) while True: if greater % a == 0 and greater % b == 0: print("LCM:", greater) break greater += 1






✅ Dynamic version using user input.

πŸ”Ή Method 3 – Using GCD Formula

Formula:

LCM = (a × b) // GCD(a, b)

import math a = 4 b = 6 lcm = (a * b) // math.gcd(a, b) print("LCM:", lcm)




✅ Fastest and most efficient method.

πŸ”Ή Method 4 – Using Function

import math def find_lcm(a, b): return (a * b) // math.gcd(a, b) print(find_lcm(4, 6))





✅ Reusable and clean code.

πŸ”Ή Output

LCM: 12

πŸ”₯ Key Takeaways

✔️ LCM means smallest common multiple
✔️ Loop method is beginner-friendly
✔️ GCD formula is best for efficiency
✔️ Functions make code reusable

Tuesday, 5 May 2026

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

 


Code Explanation:

πŸ”Ή 1. Class Definition
class Test:
✅ Explanation:
A class Test is created.
It will store a list in each object.

πŸ”Ή 2. Constructor (__init__)
def __init__(self, x=[]):
    self.x = x
✅ Explanation:
Constructor runs when object is created.
Parameter x=[] is a default argument.
⚠️ Important:
This list is created only once
It is shared across all objects

πŸ”Ή 3. Assigning to Instance
self.x = x
✅ Explanation:
Assigns the same list (x) to the object
Since x is shared → all objects refer to same list

πŸ”Ή 4. Creating First Object
a = Test()
✅ What happens:
No argument passed → uses default list []
So:
a.x → []

πŸ”Ή 5. Creating Second Object
b = Test()
✅ What happens:
Again uses SAME default list
So:
b.x → []
⚠️ Key Insight:
a.x is b.x → True

πŸ‘‰ Both refer to same list


πŸ”Ή 6. Modifying List via a
a.x.append(1)
✅ Explanation:
Adds 1 to the shared list
So now:
a.x → [1]
b.x → [1]

πŸ”Ή 7. Printing b.x
print(b.x)
✅ Output:
[1]

Final Output:
[1]

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (257) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (10) BI (10) Books (262) Bootcamp (11) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (31) data (6) Data Analysis (32) Data Analytics (22) data management (15) Data Science (356) Data Strucures (17) Deep Learning (161) 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 (296) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (33) pytho (1) Python (1343) Python Coding Challenge (1135) Python Mathematics (1) Python Mistakes (51) Python Quiz (504) 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)