Showing posts with label Python Quiz. Show all posts
Showing posts with label Python Quiz. Show all posts
Wednesday, 20 May 2026
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
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
-
In today’s digital world, data has become one of the most valuable resources on Earth. Every online interaction, financial transaction, me...
-
Code Explanation: ๐น 1. First List Creation a = [1,2,3] ✅ Explanation: A list a is created. Elements are: [1, 2, 3] ๐น 2. Second List Crea...
-
In the world of modern web and AI applications, APIs are everywhere. Whether you’re serving machine learning models, building scalable mic...
-
IBM Generative AI Engineering Professional Certificate: A Comprehensive Guide Introduction The world of Artificial Intelligence (AI) has s...
-
In the modern digital economy, data has become one of the world’s most valuable resources. Every interaction, transaction, sensor reading,...
-
Artificial Intelligence is no longer a futuristic concept reserved for research laboratories and science fiction. It powers recommendation...
-
Code Explanation: ๐น 1. Function Definition def func(): ✅ Explanation: A function func is defined. It contains try, except, and finally bl...
-
Deep learning has evolved from a niche research topic into one of the most influential technological revolutions in human history. It powe...
-
Code Explanation: ๐น 1. Class Definition class Test: ✅ Explanation: A class named Test is created. Inside this class: A decorator function...
-
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 i...
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)

