Showing posts with label Python Quiz. Show all posts
Showing posts with label Python Quiz. Show all posts

Wednesday, 20 May 2026

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

 


Explanation:

1. Defining a Class

class A:
A new class named A is created.
Classes are blueprints for creating objects.

2. Creating a Property
x = property(lambda s: 5)
What is property()?
property() is a built-in Python function.
It is used to create getter, setter, and deleter methods.

Here, only a getter is provided.

Understanding the Lambda Function
lambda s: 5

This is equivalent to:

def get_x(s):
    return 5
s represents the object instance (self).
Whenever x is accessed, Python calls this function.
It always returns 5.

So:

x = property(lambda s: 5)

means:

“Create a read-only property named x that always returns 5.”

3. Creating an Object and Accessing Property
print(A().x)

Step-by-step:
A() creates an object of class A.
.x accesses the property.
The lambda function runs and returns 5.
print() displays the result.

Output
5

BOOK: Mastering Pandas with Python

Tuesday, 19 May 2026

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

 


Code Explanation:

๐Ÿ”น Step 1: Create Generator
x = (i for i in [0,0,5,0])

A generator x is created.

Generator values:

0, 0, 5, 0

⚠️ Important:
Generator gives values ONE BY ONE when consumed.

๐Ÿ”น Step 2: Execute any(x)
any(x)
๐Ÿงฉ What any() Does

any() checks values one by one.

Stops immediately when it finds first truthy value ✅

๐Ÿ”น Step 2.1: First Value
0

๐Ÿ‘‰ 0 is falsy ❌
Continue checking.

๐Ÿ”น Step 2.2: Second Value
0

๐Ÿ‘‰ Again falsy ❌
Continue.

๐Ÿ”น Step 2.3: Third Value
5

๐Ÿ‘‰ 5 is truthy ✅

So:

any(x) → True

AND ⚠️
Generator stops HERE.

Consumed values:

0, 0, 5

Remaining value:

0

๐Ÿ”น Step 3: Execute next(x)
next(x)

Generator already consumed:

0,0,5

Only remaining value:

0

So:

next(x) → 0

๐Ÿ”น Step 4: Print Result
print(0)

๐Ÿ‘‰ Final Output:

0

Sunday, 17 May 2026

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

 


Explanation:

๐Ÿ”น Step 1: Create Bytes Object
b"abc"

This is a bytes object.

Internally:

a → 97
b → 98
c → 99

(Bytes store ASCII integer values)

๐Ÿ”น Step 2: Create memoryview
x = memoryview(b"abc")
๐Ÿงฉ What memoryview Does

memoryview allows direct access to binary data
without copying it.

So:

x

becomes a memory view of:

b"abc"

๐Ÿ”น Step 3: Access Index 1
x[1]

Index positions:

0 → a
1 → b
2 → c

At index 1:

b

BUT ⚠️

For memoryview of bytes:
Python returns INTEGER byte value,
NOT character.

ASCII of:

b → 98

So:

x[1] → 98

๐Ÿ”น Step 4: Print Result
print(98)

๐Ÿ‘‰ Final Output:

98

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

 


Explanation:

Step 1: range(2)
What it does
range(2)

creates numbers:

0, 1

So loop will run 2 times.

Step 2: Start of for Loop
Line
for i in range(2):
Meaning
Variable i stores values one by one
First i = 0
Then i = 1

Step 3: First Iteration
Current value
i = 0
Executed line
pass
Meaning

pass means:

Do nothing

So nothing happens.

Step 4: Second Iteration
Current value
i = 1
Executed line
pass

Again, nothing happens.

Step 5: Loop Ends

The loop finishes normally because:

all values are completed
no break statement is used

So Python goes to the else block.

Step 6: else Block Executes
Line
else:
    print(i)
Current value of i
1

because last loop iteration stored 1 in i.

Step 7: Output
Printed value
1

Final Output
1

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

 


Explanation:

๐Ÿ”น Step 1: Understand Operator Priority

In Python:

and → evaluated before → or

So Python first evaluates:

0 and [1]
{} and 7

Expression becomes:

[] or 0 or {} or "X"

๐Ÿ”น Step 2: Evaluate 0 and [1]
0 and [1]

๐Ÿ‘‰ 0 is falsy ❌

For and:

If first value is falsy → return it immediately

So:

0 and [1] → 0

๐Ÿ”น Step 3: Evaluate {} and 7
{} and 7

๐Ÿ‘‰ {} is empty dictionary

Empty dict = falsy ❌

For and:

Return first falsy value

So:

{} and 7 → {}

๐Ÿ”น Step 4: Expression Now Becomes
[] or 0 or {} or "X"

Now Python evaluates or from left to right.

๐Ÿ”น Step 5: Evaluate []
[]

๐Ÿ‘‰ Empty list = falsy ❌
Move to next value

๐Ÿ”น Step 6: Evaluate 0
0

๐Ÿ‘‰ 0 = falsy ❌
Move ahead

๐Ÿ”น Step 7: Evaluate {}
{}

๐Ÿ‘‰ Empty dictionary = falsy ❌
Move ahead

๐Ÿ”น Step 8: Evaluate "X"
"X"

๐Ÿ‘‰ Non-empty string = truthy ✅

For or:

Return FIRST truthy value

So result becomes:

"X"

๐Ÿ”น Step 9: Final Print
print("X")

๐Ÿ‘‰ Final Output:

X

Saturday, 16 May 2026

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

 


Explanation:

๐Ÿ”น Step 1: Create First List
a = []

Python creates a NEW empty list object.

Visual:

a ───> []

๐Ÿ”น Step 2: Create Second List
b = []

Python creates ANOTHER new empty list.

Visual:

a ───> []

b ───> []

⚠️ Important:
Even though lists look same,
they are DIFFERENT objects in memory ๐Ÿ˜ˆ

๐Ÿ”น Step 3: Execute a is b
a is b
๐Ÿงฉ What is Checks

is checks:

Are both variables pointing to SAME object?

NOT:

Do values look same?

๐Ÿ”น Step 4: Compare Memory Identity

Here:

a → first list object
b → second list object

Different objects ❌

So:

a is b → False

๐Ÿ”น Step 5: Print Result
print(False)

๐Ÿ‘‰ Final Output:

False

Wednesday, 13 May 2026

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

 


Explanation:

๐Ÿ”น Step 1: Create List
x = [1,2]

A list x is created containing:

[1,2]

๐Ÿ”น Step 2: Execute x.clear()
x.clear()
๐Ÿงฉ What clear() Does
Removes ALL elements from list
Modifies original list directly

So:

[1,2] → []

๐Ÿ”น Step 3: Important Twist ๐Ÿ˜ˆ

Most people think:

clear() returns []

BUT ❌

clear() returns:

None

Because:

It modifies list in-place
Does NOT create new list

๐Ÿ”น Step 4: Execute print()
print(x.clear())

Since:

x.clear() → None

Python prints:

None

๐Ÿ”ฅ Final Output
None

Book: 100 Python Projects — From Beginner to Expert

Tuesday, 12 May 2026

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

 


Explanation:

๐Ÿ”น Step 1: Create Tuple
x = ([1,2],)
x is a tuple
Tuple contains one list:
[1,2]

๐Ÿ‘‰ Current value:

([1, 2],)

⚠️ Important:

Tuple itself is immutable ❌
But list inside tuple is mutable ✅

๐Ÿ”น Step 2: Execute x[0] += [3]
x[0] += [3]

This line is VERY tricky ๐Ÿ˜ˆ

Python internally performs TWO operations.

⚡ Step 2.1: Modify the Inner List
[1,2] += [3]

This updates list IN-PLACE.

๐Ÿ‘‰ List becomes:

[1,2,3]

So internally tuple now looks like:

([1,2,3],)
⚡ Step 2.2: Python Tries Reassignment

After modifying list, Python internally tries:

x[0] = [1,2,3]

BUT ❗

Tuple does NOT allow item assignment
Tuple is immutable

So Python raises:

TypeError

๐Ÿ”น Step 3: Error Occurs Before Print
print(x)

This line never executes because error already happened.

⚡ Important Twist ๐Ÿ˜ˆ

Even though error occurs:
๐Ÿ‘‰ List WAS modified successfully before error

Internally:

x = ([1,2,3],)

BUT print never runs.

๐Ÿ”ฅ Final Result
TypeError

Monday, 11 May 2026

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

 


Explanation:

๐Ÿ”น Step 1: Create List
x = [1]
A list x is created
It contains one element

๐Ÿ‘‰ Current value:

[1]

๐Ÿ”น Step 2: Execute x.pop()
x.pop()
๐Ÿงฉ What pop() Does
Removes last element from list
Returns removed element
๐Ÿ‘‰ Before pop()
[1]
๐Ÿ‘‰ Removed element:
1
๐Ÿ‘‰ List after removal:
[]

๐Ÿ”น Step 3: Execute print()
print(x.pop(), x)

Now:

x.pop() returned:
1
Current x is:
[]

So print becomes:

print(1, [])

๐Ÿ”น Step 4: Final Output
1 []

Final Output:

1 []

Sunday, 10 May 2026

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

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 - 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]

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

 


Code Explanation:

๐Ÿ”น 1. Function Definition
def func(x, lst=[]):
✅ Explanation:
A function func is defined with:
x → number of iterations
lst=[] → default list
⚠️ Important:
This list is created only once (when function is defined, not called)
It is shared across all function calls

๐Ÿ”น 2. Loop Execution
for i in range(x):
✅ Explanation:
Loop runs from 0 to x-1
Adds numbers step by step

๐Ÿ”น 3. Appending Values
lst.append(i)
✅ Explanation:
Adds i into the same list lst
Since lst is shared → values accumulate over calls

๐Ÿ”น 4. Returning List
return lst
✅ Explanation:
Returns the updated list

๐Ÿ”น 5. First Function Call
print(func(3))
๐Ÿ” Execution:
x = 3
Loop runs: 0,1,2
List becomes:
[0, 1, 2]

✔️ Output:
[0, 1, 2]
๐Ÿ”น 6. Second Function Call
print(func(2))
๐Ÿšจ Important:
Python does NOT create a new list
It uses the SAME previous list → [0,1,2]
๐Ÿ” Execution:
x = 2
Loop runs: 0,1
These values are appended to existing list:
[0,1,2] + [0,1] → [0,1,2,0,1]
✔️ Output:
[0, 1, 2, 0, 1]
๐ŸŽฏ Final Output
[0, 1, 2]
[0, 1, 2, 0, 1]

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

 


Explanation:

๐Ÿ”น Step 1: Initialize the List
x = [1,2,3]
A list x is created
๐Ÿ‘‰ Current value:
[1, 2, 3]

๐Ÿ”น Step 2: Insert Element
x.insert(1,9)
insert(index, value) places the value at the given index
Elements at and after that index shift right

๐Ÿ‘‰ Insert 9 at index 1

Before:

[1, 2, 3]

After:

[1, 9, 2, 3]

๐Ÿ”น Step 3: Apply Slicing
x[1:3]
Slice from index 1 to 3 (3 is excluded)

๐Ÿ‘‰ From [1, 9, 2, 3]:

Index 1 → 9
Index 2 → 2

๐Ÿ‘‰ Result:

[9, 2]

๐Ÿ”น Step 4: Print Output
print(x[1:3])


๐Ÿ‘‰ Final Output:

[9, 2]

Book: 900 Days Python Coding Challenges with Explanation



Monday, 4 May 2026

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





Explanation:

๐Ÿง  1. List Creation
x = [1,2,3]
Here, a list named x is created with elements:
Index 0 → 1
Index 1 → 2
Index 2 → 3

๐Ÿ‘‰ So the list looks like:
x = [1, 2, 3]

๐Ÿ” 2. Understanding x[0]
x[0] means accessing the element at index 0
Value at index 0 is 1

๐Ÿ‘‰ So:

x[0] = 1

๐Ÿ”„ 3. Evaluating x[x[0]]
Replace x[0] with its value:
x[x[0]] = x[1]
Now access index 1 of the list:
x[1] = 2

๐Ÿ–จ️ 4. Final Output
print(x[x[0]]) becomes:
print(2)

✅ Final Result
2

Book: 1000 Days Python Coding Challenges with Explanation

Sunday, 3 May 2026

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

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (264) 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 (33) Data Analytics (22) data management (15) Data Science (360) Data Strucures (17) Deep Learning (167) 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 (302) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (34) pytho (1) Python (1350) Python Coding Challenge (1142) Python Mathematics (1) Python Mistakes (51) Python Quiz (513) 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)