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

Saturday, 17 January 2026

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

 


Step 1: Creating the tuple

t = (1, 2, [3, 4])

Here, t is a tuple containing:

  • 1 → integer (immutable)

  • 2 → integer (immutable)

  • [3, 4] → a list (mutable)

So the structure is:

t = (immutable, immutable, mutable)

Step 2: Understanding t[2] += [5]

t[2] += [5]

This line is equivalent to:

t[2] = t[2] + [5]

Now two things happen internally:

๐Ÿ‘‰ First: The list inside the tuple is modified

Because lists are mutable, t[2] (which is [3, 4]) gets updated in place to:

[3, 4, 5]

๐Ÿ‘‰ Second: Python tries to assign back to the tuple

After modifying the list, Python tries to do:

t[2] = [3, 4, 5]

But this fails because tuples are immutable — you cannot assign to an index in a tuple.

❌ Result: Error occurs

You will get this error:

TypeError: 'tuple' object does not support item assignment

❗ Important Observation (Tricky Part)

Even though an error occurs, the internal list actually gets modified before the error.

So if you check the tuple in memory after the error, it would conceptually be:

(1, 2, [3, 4, 5])

But print(t) never runs because the program crashes before that line.


✅ If you want this to work without error:

Use this instead:

t[2].append(5)
print(t)

Output:

(1, 2, [3, 4, 5])

Mastering Pandas with Python


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

 


Code Explanation:

1. Global Variable Definition
x = 10

A global variable x is created.

Value of x is 10.

This variable is accessible outside functions.

2. Function Definition (outer)
def outer():

A function named outer is defined.

No code runs at this point.

Function execution starts only when called.

3. Local Variable Inside Function
    x = 5

A local variable x is created inside outer.

This shadows the global x.

This x exists only within outer().

4. Lambda Function and Closure
    return map(lambda y: y + x, range(3))

range(3) produces values: 0, 1, 2

The lambda function:

Uses variable x

Captures x from outer’s local scope

This behavior is called a closure

map() is lazy, so no calculation happens yet.

A map object is returned.

5. Global Variable Reassignment
x = 20

The global x is updated from 10 to 20.

This does not affect the lambda inside outer.

Closures remember their own scope, not global changes.

6. Function Call and Map Evaluation
result = list(outer())

outer() is called.

Inside outer():

x = 5 is used

list() forces map() execution:

y Calculation Result
0 0 + 5 5
1 1 + 5 6
2 2 + 5 7

Final list becomes:
[5, 6, 7]

7. Output
print(result)

Output:
[5, 6, 7]

๐Ÿ“Š How Food Habits & Lifestyle Impact Student GPA — Dataset + Python Code

Friday, 16 January 2026

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


Code Explanation:

Tuple Initialization
t = (5, -5, 10)

A tuple named t is created.

It contains both positive and negative integers.

Tuples are immutable, meaning their values cannot be changed.

Creating a Tuple of Absolute Values
u = tuple(abs(i) for i in t)

Each element of tuple t is passed to the abs() function.

Negative numbers are converted to positive.

A new tuple u is formed:

u = (5, 5, 10)

Reversing the Tuple
v = u[::-1]

Tuple slicing with step -1 is used.

This reverses the order of elements in u.

Resulting tuple:

v = (10, 5, 5)

Summing Selected Elements Using Index
x = sum(v[i] for i in range(1, len(v)))

range(1, len(v)) generates indices 1 and 2.

Elements selected:

v[1] = 5

v[2] = 5

These values are added together.

Result:

x = 10

Displaying the Output
print(x)

Prints the final calculated value.

Final Output
10

๐Ÿ“Š How Food Habits & Lifestyle Impact Student GPA — Dataset + Python Code

 

Thursday, 15 January 2026

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

 




Explanation:

1. Create a dictionary of scores
scores = {"A": 85, "B": 72, "C": 90}

This stores names as keys and their scores as values.

2. Set the minimum score option
opt = {"min": 80}

This option decides the minimum score required to pass the filter.

3. Filter dictionary keys
res = list(filter(lambda k: scores[k] >= opt["min"], scores))

filter loops over the dictionary keys ("A", "B", "C").

scores[k] gets each key’s value.

Keeps only keys whose score is ≥ 80.

list() converts the result into a list.

4. Print the filtered result
print(res)

Displays the keys that passed the condition.

Output
['A', 'C']

Mathematics with Python Solving Problems and Visualizing Concepts

Tuesday, 13 January 2026

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

 


Step-by-step execution

  1. lst = [10, 20, 30]
    A list with three elements is created.

  2. for i in lst:
    The loop iterates over each element in the list one by one.


▶️ Iteration 1

    i = 10
  • Check: if i == 20 → False

  • So print(i) runs → prints 10


▶️ Iteration 2

    i = 20
  • Check: if i == 20 → True

  • break executes → the loop stops immediately

  • print(i) is not executed for 20


▶️ Iteration 3

  • Never runs, because the loop already stopped at break.


Final Output

10

Key Concepts

KeywordMeaning
forLoops through each element in a list
ifChecks a condition
breakStops the loop immediately
print(i)Prints the current value

Summary

  • The loop starts printing values from the list.

  • When it encounters 20, break stops the loop.

  • So only 10 is printed.

๐Ÿ‘‰ Output: 10

100 Python Projects — From Beginner to Expert


Monday, 12 January 2026

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

 


Let’s explain it step-by-step:

s = {1, 2, 3} s.add([4, 5])
print(s)

 What happens here?

Line 1

s = {1, 2, 3}

A set is created with three integers.


Line 2

s.add([4, 5])

Here you are trying to add a list [4, 5] into a set.

๐Ÿ‘‰ But sets can only store hashable (immutable) objects.

  • int, float, str, tuple → hashable ✅

  • list, dict, set → not hashable

A list is mutable, so Python does not allow it inside a set.

So this line raises an error:

TypeError: unhashable type: 'list'

Line 3

print(s)

This line is never executed, because the program stops at the error in line 2.


Final Output

There is no output — instead you get:

TypeError: unhashable type: 'list'

✅ How to fix it?

If you want to store 4 and 5 as a group inside the set, use a tuple:

s = {1, 2, 3} s.add((4, 5))
print(s)

Output (order may vary):

{1, 2, 3, (4, 5)}

Or if you want to add them as individual elements:

s = {1, 2, 3} s.update([4, 5])
print(s)

Output:

{1, 2, 3, 4, 5}

๐Ÿ”‘ Key Concept

Sets only allow immutable (hashable) elements.
That’s why lists can’t go inside sets — but tuples can 

900 Days Python Coding Challenges with Explanation 

Sunday, 11 January 2026

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

 


 Step 1: Create the tuple

t = (1, 2, 3)

tuple is immutable → its values cannot be changed.


๐Ÿ”น Step 2: Loop through the tuple

for i in t:

Each element of t is assigned one by one to the variable i:

Loop iterationi value
1st1
2nd2
3rd3

๐Ÿ”น Step 3: Modify i

i = i * 2

This only changes the local variable i, not the tuple.

So:

  • When i = 1 → i becomes 2

  • When i = 2 → i becomes 4

  • When i = 3 → i becomes 6

But t never changes because:

  • You are not assigning back to t

  • And tuples cannot be modified in place


๐Ÿ”น Step 4: Print the tuple

print(t)

Since t was never changed, output is:

(1, 2, 3)

Final Answer

Output:

(1, 2, 3)

Key Concept

PointExplanation
Tuples are immutableTheir values cannot be changed
i is just a copyChanging i does not affect t
No assignment to tSo t stays the same

๐Ÿ”น If you actually want to double the values:

t = tuple(i * 2 for i in t)
print(t)

Output:

(2, 4, 6)

AUTOMATING EXCEL WITH PYTHON

Friday, 9 January 2026

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

 


Step 1: Understand range(3)

range(3) → 0, 1, 2

So the loop runs with i = 0, then 1, then 2.


๐Ÿ”น Step 2: Loop execution

iCondition i > 1Action
0FalseGo to else → print(0)
1FalseGo to else → print(1)
2Truecontinue → skip print

๐Ÿ”น What continue does

continue skips the rest of the loop body and jumps to the next iteration.

So when i == 2, continue is executed → print(i) is skipped.


Final Output

0
1

๐Ÿ”น Key Points

  • else runs only when if condition is False.

  • continue skips the remaining code in the loop for that iteration.

  • So 2 is never printed.


In one line:

This code prints all values of i less than or equal to 1 and skips values greater than 1.

Applied NumPy From Fundamentals to High-Performance Computing 

Wednesday, 7 January 2026

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

 


Step-by-Step Explanation

1️⃣ Create the original list

arr = [1, 2, 3]

arr points to a list in memory:

arr → [1, 2, 3]

2️⃣ Make a copy of the list

arr3 = arr.copy()
  • .copy() creates a new list object with the same values.

  • arr3 is a separate list, not a reference to arr.

arr → [1, 2, 3]
arr3 → [1, 2, 3] (different memory location)

3️⃣ Modify the copied list

arr3.append(4)

This only changes arr3:

arr → [1, 2, 3]
arr3 → [1, 2, 3, 4]

4️⃣ Print original list

print(arr)

Output

[1, 2, 3]

Key Concept

OperationEffect
.copy()Creates a new list
=Only creates a reference
append()Modifies list in place

 Compare with reference assignment:

arr = [1, 2, 3] arr3 = arr # reference, not copy arr3.append(4)
print(arr)

Output:

[1, 2, 3, 4]

Because both variables point to the same list.


Final Answer

๐Ÿ‘‰ arr remains unchanged because arr3 is a separate copy, not a reference.

Printed Output:

[1, 2, 3]

Heart Disease Prediction Analysis using Python


Tuesday, 6 January 2026

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

 


Step 1

t = (1, 2, 3)

A tuple t is created with values:
(1, 2, 3)


๐Ÿ”น Step 2

t[0] = 10

Here you are trying to change the first element of the tuple.

Problem:
Tuples are immutable, meaning once created, their elements cannot be changed.

So Python raises an error:

TypeError: 'tuple' object does not support item assignment

๐Ÿ”น Step 3

print(t)

This line is never executed because the program stops when the error occurs in the previous line.


Final Result

The code produces an error, not output:

TypeError: 'tuple' object does not support item assignment

Key Concept

Data TypeMutable?
List ([])Yes
Tuple (())❌ No

✔️ Correct Way (if you want to modify it)

Convert tuple to list, modify, then convert back:

t = (1, 2, 3) lst = list(t) lst[0] = 10 t = tuple(lst)
print(t)

Output:

(10, 2, 3)

Applied NumPy From Fundamentals to High-Performance Computing

Monday, 5 January 2026

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

 


Step-by-step Explanation

Line 1

lst = [1, 2, 3]

You create a list:

lst → [1, 2, 3]

Line 2

lst = lst.append(4)

This is the key tricky part.

  • lst.append(4) modifies the list in-place

  • But append() returns None

So this line becomes:

lst = None

Because:

lst.append(4) → None

The list is updated internally, but you overwrite lst with None.


Line 3

print(lst)

Since lst is now None, it prints:

None

✅ Final Output

None

 Why this happens

FunctionModifies listReturns value
append()YesNone

So you should not assign it back.


✅ Correct way to do it

lst = [1, 2, 3] lst.append(4)
print(lst)

Output:

[1, 2, 3, 4]

 Summary

  • append() changes the list but returns None

  • Writing lst = lst.append(4) replaces your list with None

  • Always use lst.append(...) without assignment


Book: APPLICATION OF PYTHON FOR CYBERSECURITY

Sunday, 4 January 2026

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

 


Step-by-step explanation

1. Initial list

arr = [1, 2, 3]

arr is a list with three integers.


2. Loop execution

for i in arr:
i = i * 2
  • The loop goes through each element of arr.

  • i is a temporary variable that receives the value of each element — not the reference to the list element.

Iteration by iteration:

Iterationi beforei = i * 2arr
112[1, 2, 3]
224[1, 2, 3]
336[1, 2, 3]

๐Ÿ‘‰ i changes, but arr does not change.


3. Final print

print(arr)

Since the list was never modified, the output is:

[1, 2, 3]

Why doesn’t the list change?

Because:

  • i is a copy of the value, not the element inside the list.

  • Reassigning i does not update arr.

This is equivalent to:

x = 1
x = x * 2 # changes x only, not the original source

Correct way to modify the list

If you want to update the list, use the index:

arr = [1, 2, 3] for idx in range(len(arr)): arr[idx] = arr[idx] * 2
print(arr)

Output:

[2, 4, 6]

Key takeaway

Looping as for i in arr gives you values — not positions.
To change the list, loop over indices or use list comprehension.

Example:

arr = [x * 2 for x in arr]

Book:  Probability and Statistics using Python

Summary

CodeModifies list?
for i in arr: i *= 2❌ No
for i in range(len(arr))✅ Yes
arr = [x*2 for x in arr]✅ Yes

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


 Code Explanation:

1. Tuple Initialization
t = (1, (2, 3), 4)

A tuple t is created.

It contains:

Integer 1

A nested tuple (2, 3)

Integer 4

2. Variable Initialization
s = 0

A variable s is initialized to 0.

It will store the running total (sum).

3. Loop Through Tuple Elements
for i in t:

A for loop iterates over each element in the tuple t.

On each iteration, i takes one element from t.

Iterations:

1st: i = 1

2nd: i = (2, 3)

3rd: i = 4

4. Type Checking
if type(i) == tuple:

Checks whether the current element i is a tuple.

This is important because (2, 3) is a tuple inside the main tuple.

5. If Element is a Tuple
s += sum(i)

sum(i) adds all values inside the tuple (2, 3) → 2 + 3 = 5.

The result is added to s.

6. If Element is Not a Tuple
else:
    s += i

If i is not a tuple (i.e., an integer), it is directly added to s.

7. Print the Result
print(s)

Prints the final value of s.


Final Output
10

Python Interview Preparation for Students & Professionals

Saturday, 3 January 2026

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

 


Code Explanation:

List Initialization
lst = [1, 2, 3]

A list named lst is created.

It contains three elements: 1, 2, and 3.

Empty List Creation
out = []

An empty list named out is created.

This list will store the result.

For Loop Setup
for i in range(len(lst)):

len(lst) → 3

range(3) → 0, 1, 2

So the loop is ready to run with i = 0, then 1, then 2.

First Iteration of Loop
out.append(lst[i])

First value of i is 0

lst[0] is 1

So 1 is appended to out

Now out = [1]

Break Statement
break

break stops the loop immediately.

No further iterations happen (i = 1 and i = 2 are skipped).

Print Statement
print(out)

Prints the contents of out.

Final Output
[1]

Python Interview Preparation for Students & Professionals

Friday, 2 January 2026

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

 


Code Explanation:

1. Importing Required Function
from functools import reduce

Explanation:

Imports the reduce() function from Python’s functools module.

reduce() is used to apply a function cumulatively to items in an iterable.

2. Creating a List of Lists
lists = [[1], [2, 3], [4]]

Explanation:

lists is a nested list (a list containing other lists).

Here:

First element → [1]

Second element → [2, 3]

Third element → [4]

3. Using reduce() to Flatten the List
result = reduce(lambda a, b: a + b, lists)

3.1 reduce()

reduce(function, iterable) repeatedly applies function to elements of iterable.

3.2 lambda a, b: a + b

This is an anonymous function.

It takes two lists (a and b) and returns their concatenation (a + b).

3.3 Step-by-step working
Step a b a + b
1 [1] [2, 3] [1, 2, 3]
2 [1, 2, 3] [4] [1, 2, 3, 4]

So the final value of result becomes:

[1, 2, 3, 4]

4. Printing the Output
print("Flattened:", result)

Explanation:

Prints the text "Flattened:" followed by the flattened list stored in result.

5. Final Output
Flattened: [1, 2, 3, 4]

900 Days Python Coding Challenges with Explanation


Thursday, 1 January 2026

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


 Explanation:

Create a list
nums = [1, 2, 3]


A list named nums is created with three elements: 1, 2, and 3.

Apply map()
result = map(lambda x: x * 2, nums)


map() creates an iterator that will apply lambda x: x * 2 to each element of nums.

Important: At this point, no calculation happens yet — map() is lazy.

Clear the original list
nums.clear()


This removes all elements from nums.

Now nums becomes an empty list: [].

Convert map to list and print
print(list(result))


Now iteration happens.

But nums is already empty.

So map() has no elements to process.

Final Output
[]

Python Development & Support Services

Wednesday, 31 December 2025

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


 Explanation:

1. Importing NumPy
import numpy as np

Imports the NumPy library.

np is an alias used to access NumPy functions easily.

2. Creating a NumPy Array
x = np.array([1, 3, 5])

Creates a NumPy array named x.

x contains three elements: 1, 3, and 5.

3. Initializing a Variable
y = 1

Initializes variable y with value 1.

We use 1 because this variable will store a product (multiplication), and 1 is the neutral element for multiplication.

4. Looping Through the Array
for i in range(len(x)):

len(x) gives the length of the array → 3.

The loop runs with i = 0, 1, 2.

i is used as the index to access elements of x.

5. Multiplying Each Element
    y *= x[i]

Multiplies the current value of y by the element at index i.

Step-by-step:

i = 0 → y = 1 × x[0] = 1 × 1 = 1

i = 1 → y = 1 × x[1] = 1 × 3 = 3

i = 2 → y = 3 × x[2] = 3 × 5 = 15

6. Printing the Result

print(y)

Prints the final value of y.

Final Output
15

Mastering Task Scheduling & Workflow Automation with Python


Monday, 29 December 2025

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

 


What happens step by step

  1. The function f() is called.

  2. Python enters the try block.

  3. return 10 is executed — Python prepares to return 10, but does not exit the function yet.

  4. Before the function actually returns, Python must execute the finally block.

  5. The finally block contains return 20.

  6. This new return overrides the previous return 10.

  7. So the function returns 20.

  8. print(f()) prints 20.


Final Output

20

Key Rule

If a finally block contains a return, it always overrides any return from try or except.


Important takeaway

Using return inside finally is usually discouraged because:

  • It hides exceptions

  • It overrides earlier returns

  • It can make debugging very confusing


100 Python Projects — From Beginner to Expert

In short

BlockWhat it does
tryPrepares return 10
finallyExecutes and overrides with return 20
Result20

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (181) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (8) BI (10) Books (261) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (299) Cybersecurity (29) Data Analysis (25) Data Analytics (16) data management (15) Data Science (242) Data Strucures (15) Deep Learning (99) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (18) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (51) Git (9) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (220) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (12) PHP (20) Projects (32) Python (1238) Python Coding Challenge (970) Python Mistakes (32) Python Quiz (397) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)