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

Wednesday, 3 December 2025

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

 


Line-by-Line Explanation

✅ 1. Dictionary Created

d = {"x": 5, "y": 15}
  • A dictionary with:

    • Key "x" → Value 5

    • Key "y" → Value 15


✅ 2. Initialize Sum Variable

s = 0
  • s will store the final total.


✅ 3. Loop Through Values

for v in d.values():
  • .values() returns only the values:

    5, 15

✅ 4. Conditional Addition (Ternary If-Else)

s += v if v > 10 else 2

This means:

  • If v > 10 → add v

  • Else → add 2


Loop Execution

IterationvCondition v > 10Added to sNew s
1st5❌ False+22
2nd15✅ True+1517

✅ 5. Final Output

print(s)

Output:

17

Key Concepts Used

✅ Dictionary
✅ Loop
✅ .values()
✅ Ternary if-else
✅ Accumulator variable

AUTOMATING EXCEL WITH PYTHON

Tuesday, 2 December 2025

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

 


Actual Output

[10 20 30]

Why didn’t the array change?

Even though we write:

i = i + 5

๐Ÿ‘‰ This DOES NOT modify the NumPy array.

 What really happens:

StepExplanation
for i in x:i receives a copy of each value, not the original element
i = i + 5Only changes the local variable i
xThe original NumPy array stays unchanged

So this loop works like:

i = 10 → i = 15 (x unchanged) i = 20 → i = 25 (x unchanged)
i = 30 → i = 35 (x unchanged)

But x never updates, so output is still:

[10 20 30]

Correct Way to Modify the NumPy Array

✔ Method 1: Using Index

for idx in range(len(x)): x[idx] = x[idx] + 5
print(x)

✅ Output:

[15 25 35]

✔ Method 2: Best & Fastest Way (Vectorized)

x = x + 5
print(x)

✅ Output:

[15 25 35]

Key Concept (IMPORTANT for Interviews)

Loop variable in NumPy gives a copy, not a reference.

 To change NumPy arrays, use indexing or vectorized operations.

Network Engineering with Python: Create Robust, Scalable & Real-World Applications

 

Monday, 1 December 2025

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

 


Step-by-Step Execution

Initial list

arr = [1, 2, 3]

1st Iteration

i = 1
arr.remove(1)

List becomes:

[2, 3]

Now Python moves to the next index (index 1).


2nd Iteration

Now the list is:

[2, 3]

Index 1 has:

i = 3

So:

arr.remove(3)

List becomes:

[2]

Loop Ends

There is no next element to continue.


Final Output

[2]

Why This Happens (Important Concept)

  • You are modifying the list while looping over it

  • When an element is removed:

    • The list shifts to the left

    • The loop skips elements

  • This causes unexpected results


Correct Way to Remove All Elements

✔️ Option 1: Loop on a copy

for i in arr[:]:
arr.remove(i)

✔️ Option 2: Use clear()

arr.clear()

Key Interview Point

Never modify a list while iterating over it directly.

Mastering Pandas with Python

Sunday, 30 November 2025

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

 


Step-by-Step Execution

✅ range(3) generates:

0, 1, 2

✅ Initial value:

x = 0
๐Ÿ” Loop Iterations:

➤ 1st Iteration:

i = 0
x = x + i = 0 + 0 = 0

➤ 2nd Iteration:

i = 1
x = x + i = 0 + 1 = 1

➤ 3rd Iteration:

i = 2
x = x + i = 1 + 2 = 3

After the Loop Ends

  • i remains stored as the last value → 2

  • x = 3

✅ Final Output:

2 3

Important Concept (Interview Tricky Point)

✅ In Python, loop variables (i) stay alive after the loop ends, unlike some other languages.

Mastering Pandas with Python

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

 


What is happening?

๐Ÿ”น 1. map() creates an iterator

res = map(lambda x: x + 1, nums)

This does NOT create a list immediately. It creates a lazy iterator:

res → (2, 3, 4)

…but nothing is calculated yet.


๐Ÿ”น 2. The for loop consumes the iterator

for i in res:
pass
  • This loop runs through all values: 2, 3, 4

  • But since we used pass, nothing is printed

  • Important: After this loop, the iterator is now EXHAUSTED


๐Ÿ”น 3. Printing after exhaustion

print(list(res))
  • res is already empty

  • So converting it to a list gives:

[]

Final Output

[]

Key Tricky Rule

 A map() object can be used ONLY ONCE.
Once it is looped through, it becomes empty forever.


Correct Way (If you want reuse)

res = list(map(lambda x: x + 1, nums)) for i in res: pass
print(res) # [2, 3, 4]

Python for Civil Engineering: Concepts, Computation & Real-world Applications

Friday, 28 November 2025

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

 


Step-by-Step Explanation

1️⃣ Lists Creation

a = [1, 2, 3]
b = [10, 20, 30]
  • a contains: 1, 2, 3

  • b contains: 10, 20, 30


2️⃣ zip(a, b)

zip(a, b)

This pairs elements from both lists:

(1, 10) (2, 20)
(3, 30)

3️⃣ Loop Execution

for i, j in zip(a, b):
  • i gets values from list a

  • j gets values from list b

Iterationij
1st110
2nd220
3rd330

4️⃣ Inside Loop

i = i + 100
  • This changes only the loop variable i,

  • ✅ It does NOT change the original list a

Example:

  • 1 → becomes 101

  • 2 → becomes 102

  • 3 → becomes 103

But this updated i is never printed or used further.


5️⃣ Print Statement

print(j)
  • Only j is printed

  • j comes from list b

So it prints:

10 20
30

Final Output

10 20
30

๐Ÿ”ฅ Key Learning Points

✅ Changing i does not modify list a
✅ zip() pairs values but does not link memory
✅ Only j is printed, so i + 100 has no visible effect


Mathematics with Python Solving Problems and Visualizing Concepts

Thursday, 27 November 2025

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

 


Step-by-Step Explanation

✅ 1. Create an Empty List

funcs = []

This list will store functions (lambdas).


✅ 2. Loop Runs 3 Times

for i in range(3):

The values of i will be:

0 → 1 → 2

✅ 3. Lambda Is Appended Each Time

funcs.append(lambda: i)

⚠️ Important:
You are NOT storing the VALUE of i,
You are storing a function that will return i later.

So after the loop:

  • funcs[0] → returns i

  • funcs[1] → returns i

  • funcs[2] → returns i

All three lambdas refer to the SAME variable i.


✅ 4. Final Value of i

After the loop finishes:

i = 2

✅ 5. Calling All Functions

print([f() for f in funcs])

This executes:

f() → returns i → which is 2

So all functions return:

[2, 2, 2]

✅ ✅ Final Output

[2, 2, 2]

Why This Happens? (Late Binding)

Python lambdas:

  • Do NOT remember the value

  • They remember the variable reference

  • Value is looked up only when the function is called

This behavior is called Late Binding.


✅ ✅ ✅ Correct Way (Fix This Problem)

✅ Solution 1: Use Default Argument

funcs = [] for i in range(3): funcs.append(lambda i=i: i)
print([f() for f in funcs])

✅ Output:

[0, 1, 2]

Why this works:

  • i=i captures the value immediately

  • Each lambda stores its own copy

Python for Civil Engineering: Concepts, Computation & Real-world Applications


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

 


✅ Step-by-Step Explanation

๐Ÿ”น 1. This is your list:

nums = [0, 1, 2, 3, 4]

It contains both falsy and truthy values.


๐Ÿ”น 2. This is the filter with lambda:

result = filter(lambda x: x, nums)
  • lambda x: x means:
    ๐Ÿ‘‰ Return the value itself.

  • filter() keeps only values that are truthy.

  • In Python, these are falsy values:

    • 0, None, False, ""

So 0 is removed, and all non-zero numbers remain.


๐Ÿ”น 3. Convert result to list:

print(list(result))

Since filter() returns an iterator, we convert it to a list to display it.


✅ FINAL OUTPUT

[1, 2, 3, 4]

 Key Concept

๐Ÿ‘‰ This line:

filter(lambda x: x, nums)

means:

“Keep only the values that are True in a boolean sense.”

APPLICATION OF PYTHON IN FINANCE 

Tuesday, 25 November 2025

Python Coding Challenge - Question with Answer (01261125)

 


Explanation:

๐Ÿ”น Line 1: Creating a Nested List
data = [[1,2], [3,4], [5,6]]

Explanation

data is a list of lists (nested list).

It contains three separate sublists.

Each sublist holds two numbers.

We will later flatten these sublists into one single list.

๐Ÿ”น Line 2: Creating an Empty Output List
flat = []

Explanation

flat is an empty list.

This list will store all numbers from the nested structure after flattening.

It starts empty and will be filled step by step.

๐Ÿ”น Line 3: Flattening Using map() With Lambda
list(map(lambda x: flat.extend(x), data))

Explanation

This is the most important line. Here’s how it works:

A. map()

map() loops over each sublist inside data.

B. lambda x

x represents each sublist from data, one at a time:

First: [1, 2]

Second: [3, 4]

Third: [5, 6]

C. flat.extend(x)

.extend() adds each element of x into flat.

It does not add the sublist — it adds the individual numbers.

D. How flat changes
Step Value of x flat after extend
1 [1, 2] [1, 2]
2 [3, 4] [1, 2, 3, 4]
3 [5, 6] [1, 2, 3, 4, 5, 6]
E. Why list(...) ?

map() doesn’t run immediately.

Wrapping it with list() forces all iterations to execute.

๐Ÿ”น Line 4: Printing the Final Output
print(flat)

Explanation

Prints the fully flattened list.

All nested values are now in one single list.

๐ŸŽ‰ Final Output
[1, 2, 3, 4, 5, 6]

APPLICATION OF PYTHON FOR GAME DEVELOPMENT

Monday, 24 November 2025

Python Coding Challenge - Question with Answer (01251125)

 


Explanation:

Initialize i
i = 0

The variable i is set to 0.

It will be used as the counter for the while loop.

Create an empty list
funcs = []

funcs is an empty list.

We will store lambda functions inside this list.

Start the while loop
while i < 5:

The loop runs as long as i is less than 5.

So the loop will execute for: i = 0, 1, 2, 3, 4.

Append a lambda that captures the current value of i
funcs.append(lambda i=i: i)

Why is i=i important?

i=i is a default argument.

Default arguments in Python are evaluated at the moment the function is created.

So each lambda stores the current value of i during that specific loop iteration.

What values get stored?

When i = 0 → lambda stores 0

When i = 1 → lambda stores 1

When i = 2 → lambda stores 2

When i = 3 → lambda stores 3

When i = 4 → lambda stores 4

So five different lambdas are created, each holding a different number.

Increment i
i += 1

After each iteration, i increases by 1.

This moves the loop to the next number.

Call all lambda functions and print their outputs
print([f() for f in funcs])

What happens here?

A list comprehension calls each stored lambda f() in funcs.

Each lambda returns the value it captured earlier.

Final Output:
[0, 1, 2, 3, 4]

Python for Civil Engineering: Concepts, Computation & Real-world Applications

Sunday, 23 November 2025

Python Coding Challenge - Question with Answer (01241125)


 Explanation:

1. Function Definition
def f(n):

You define a function named f.

It expects one argument n.

2. First Condition – Check if n is even
    if n % 2 == 0:

n % 2 gives the remainder when n is divided by 2.

If the remainder is 0, then n is even.

For n = 6:

6 % 2 = 0, so this condition is TRUE.

3. First Return
        return n // 2

Because the first condition is true, the function immediately returns integer division of n by 2.

6 // 2 = 3

4. Second Condition (Skipped)
    if n % 3 == 0:
        return n // 3

These lines are never reached for n = 6.

Even though 6 is divisible by 3, the function already returned at the previous line.

5. Final Return (Also Skipped)
    return n

This would run only if neither condition was true.

But in this case, the function has already returned earlier.

6. Function Call
print(f(6))

Calls f(6)

As shown above, f(6) returns 3

So the program prints:

3

Final Output
3

Medical Research with Python Tools

Saturday, 22 November 2025

Python Coding Challenge - Question with Answer (01231125)


 Explanation:


Initialization
i = 3

The variable i is set to 3.

This will be the starting value for the loop.

While Loop Condition
while i > 1:

The loop will keep running as long as i is greater than 1.

First iteration → i = 3 → condition TRUE

Second iteration → i = 2 → condition TRUE

Third time → i = 1 → condition FALSE → loop stops

Inner For Loop
for j in range(2):

range(2) gives values 0 and 1.

This means the inner loop runs 2 times for each while loop cycle.

Print Statement
print(i - j, end=" ")

This prints the result of i - j each time.

When i = 3:

j = 0 → prints 3

j = 1 → prints 2

When i = 2:

j = 0 → prints 2

j = 1 → prints 1

Decrease i
i -= 1

After each full inner loop, i is decreased by 1.

i = 3 → becomes 2

i = 2 → becomes 1

Now i = 1 → while loop stops.

Final Output
3 2 2 1

Python for Ethical Hacking Tools, Libraries, and Real-World Applications


Friday, 21 November 2025

Python Coding Challenge - Question with Answer (01221125)

 

Explanation:

1. Initialize the List
nums = [9, 8, 7, 6]

A list named nums is created.

Value: [9, 8, 7, 6].

2. Initialize the Sum Variable
s = 0

A variable s is created to store the running total.

Initial value: 0.

3. Start the Loop
for i in range(1, len(nums), 2):

range(1, len(nums), 2) generates numbers starting at 1, increasing by 2.

For nums of length 4, this produces:
i = 1, 3

So the loop will run two times.

4. Loop Iteration Details
Iteration 1 (i = 1)
s += nums[i-1] - nums[i]

nums[i-1] → nums[0] → 9

nums[i] → nums[1] → 8

Calculation: 9 - 8 = 1

Update s:
s = 0 + 1 = 1

Iteration 2 (i = 3)
s += nums[i-1] - nums[i]

nums[i-1] → nums[2] → 7

nums[i] → nums[3] → 6

Calculation: 7 - 6 = 1

Update s:
s = 1 + 1 = 2

5. Final Output
print(s)

The final value of s is 2.

Output: 2

Final Answer: 2

Probability and Statistics using Python

Thursday, 20 November 2025

Python Coding Challenge - Question with Answer (01211125)


 Explanation:

Initialize total
total = 0

A variable total is created.

It starts with the value 0.

This will accumulate the sum during the loops.

Start of the outer loop
for i in range(1, 4):

i takes values 1, 2, 3
(because range(1,4) includes 1,2,3)

We will analyze each iteration separately.

Outer Loop Iteration Details
When i = 1
j = i   →  j = 1
Enter the inner loop:
Condition: j > 0 → 1 > 0 → true

Step 1:
total += (i + j) = (1 + 1) = 2
total = 2
j -= 2 → j = -1

Next check:

j > 0 → -1 > 0 → false, inner loop stops.

When i = 2
j = i   →  j = 2

Step 1:
total += (2 + 2) = 4
total = 2 + 4 = 6
j -= 2 → j = 0

Next check:

j > 0 → 0 > 0 → false, inner loop ends.

When i = 3
j = i  → j = 3

Step 1:
total += (3 + 3) = 6
total = 6 + 6 = 12
j -= 2 → j = 1

Step 2:

Condition: j > 0 → 1 > 0 → true

total += (3 + 1) = 4
total = 12 + 4 = 16
j -= 2 → j = -1

Next check:

j > 0 → false → exit.

Final Output
print(total)

The final value collected from all iterations is:

Output: 16

100 Python Projects — From Beginner to Expert

Wednesday, 19 November 2025

Python Coding Challenge - Question with Answer (01201125)

 


Explanation:

x = 0

Initializes the variable x with 0.

x will act as the counter for the loop.

while x < 5:

Starts a while loop that will run as long as x is less than 5.

Loop iterations will occur for x = 0, 1, 2, 3, 4.

if x % 2 == 0: print("E", end='')

Checks if x is even (x % 2 == 0).

If true, prints "E".

end='' ensures that the next output prints on the same line without a newline.

elif x % 2 != 0: print("O", end='')

If the if condition is false, checks if x is odd (x % 2 != 0).

If true, prints "O" on the same line.

x += 1

Increments x by 1 after each iteration.

This moves the loop forward and prevents an infinite loop.

Step-by-Step Execution Table
x Condition     True Output x after increment
0 x % 2 == 0 → True     E     1
1 x % 2 != 0 → Tru e     O     2
2 x % 2 == 0 → True         E     3
3 x % 2 != 0 → True         O     4
4 x % 2 == 0 → True         E     5

Final Output:
EOEOE

Network Engineering with Python: Create Robust, Scalable & Real-World Applications


Tuesday, 18 November 2025

Python Coding Challenge - Question with Answer (01191125)

 


Explanation:

lst = [1, 2, 3]

Creates a list lst containing three integers: 1, 2, 3.

This list will be used in the loops.

result = 0

Initializes a variable result to 0.

This variable will accumulate the count of times i == j is True.

for i in lst:

Starts the outer loop.

i will take each value in the list sequentially: 1, then 2, then 3.

for j in lst:

Starts the inner loop inside the outer loop.

For each value of i, j will iterate over the list [1, 2, 3].

result += (i == j)

(i == j) is a comparison: returns True if i equals j, else False.

In Python, True is treated as 1 and False as 0 when used in arithmetic.

Adds 1 to result if i == j, otherwise adds 0.

Step-by-step calculation:

i j i == j Added to result Cumulative result
1 1 True                 1                         1
1 2 False         0                         1
1 3 False         0                         1
2 1 False         0                         1
2 2 True                 1                         2
2 3 False         0                         2
3 1 False 0 2
3 2 False 0 2
3 3 True 1 3

Correction: Actually, let's carefully check:
Wait, outer loop iterates i=1,2,3 and inner loop j=1,2,3.

Count True values: (1==1),(2==2),(3==3) once for each combination?

Actually, let's compute carefully:

i=1: j=1 → True → +1 → result=1

i=1: j=2 → False → +0 → result=1

i=1: j=3 → False → +0 → result=1

i=2: j=1 → False → +0 → result=1

i=2: j=2 → True → +1 → result=2

i=2: j=3 → False → +0 → result=2

i=3: j=1 → False → +0 → result=2

i=3: j=2 → False → +0 → result=2

i=3: j=3 → True → +1 → result=3

Total result = 3

Ah! So in my previous message I mistakenly said 9. The correct answer is 3. Thanks for double-checking!

print(result)

Prints the final value of result.

Output:

3

Probability and Statistics using Python


Monday, 17 November 2025

Python Coding Challenge - Question with Answer (01181125)

 


Explanation:

1. Dictionary Creation
d = {'apple': 2, 'banana': 3, 'cherry': 4}

Creates a dictionary d with keys as fruit names and values as numbers.

Example content:

'apple': 2
'banana': 3
'cherry': 4

2. Initialize Counter
count = 0

Initializes a variable count to store the running total.

Starts at 0.

3. Loop Over Dictionary Items
for k, v in d.items():

Loops over each key-value pair in the dictionary.

k = key (fruit name), v = value (number).

.items() gives pairs: ('apple', 2), ('banana', 3), ('cherry', 4).

4. Check for Letter 'a' in Key
if 'a' in k:

Checks if the key contains the letter 'a'.

Only keys with 'a' are processed.

'apple' → True, 'banana' → True, 'cherry' → False.

5. Add Value to Counter
count += v

Adds the value v to count if the key has 'a'.

Step by step:

'apple': count = 0 + 2 → 2

'banana': count = 2 + 3 → 5

'cherry': skipped

6. Print Final Count
print(count)

Prints the final total of values where keys contain 'a'.

Output:

5

AUTOMATING EXCEL WITH PYTHON

Sunday, 16 November 2025

Python Coding Challenge - Question with Answer (01171125)

 


Explanation:

Assign a value to num
num = 5

We set num = 5.
We will use this value for checking divisibility.

Create the list a
a = [5, 10, 11, 13]

This list contains four numbers:
5, 10, 11, 13

Initialize sum variable
s = 0

We create s to store the total of selected numbers.
Starting value = 0

Start loop — go through each value in list
for v in a:

The loop takes each value v from the list:

First: v = 5

Then: v = 10

Then: v = 11

Then: v = 13

Check divisibility using IF + continue
    if v % num == 0:
        continue

We check:
If v is divisible by num (5), skip it.
Check each:

v v % 5 Divisible? Action
5 0             Yes         Skip
10 0             Yes         Skip
11 1              No         Add
13 3              No         Add

continue means: skip the rest of the loop and go to the next number.

Add the non-skipped values
    s += v

Only numbers NOT divisible by 5 get added:

s = 11 + 13 = 24

Print the final answer
print(s)

So the output is:

24

100 Python Projects — From Beginner to Expert


Python Coding Challenge - Question with Answer (01161125)

 Explanation:

List Initialization
a = [1, 4, 7, 10]

You create a list a that contains four numbers:
1, 4, 7, 10


Variable Initialization
s = 0

You create a variable s to store the sum.
Initially, sum = 0.

Loop Through Each Value
for v in a:

This loop will pick each value from list a, one by one.
So v becomes:
1 → 4 → 7 → 10

Check Condition
if v % 3 == 1:

You check if the number leaves remainder 1 when divided by 3.

Let’s check each:

v v % 3 Remainder Condition v%3==1?
1 1             Yes             ✔ Add
4 1             Yes             ✔ Add
7 1             Yes             ✔ Add
10 1             Yes             ✔ Add

All numbers satisfy the condition.

Add to Sum
s += v

Add all numbers that passed the condition.

s = 1 + 4 + 7 + 10 = 22

Print the Final Sum
print(s)

This prints:

22

Final Output: 22

Python Interview Preparation for Students & Professionals

Friday, 14 November 2025

Python Coding Challenge - Question with Answer (01151125)


Explanation:

Line 1: Initialization
count = 0

A variable count is created and initialized to 0.

This variable will act as the loop counter.

Line 2: Start of While Loop
while count < 5:

The loop will continue as long as count is less than 5.

The condition is checked before each iteration.

Line 3: Increment Counter
count += 1

In each iteration, count is increased by 1.

Equivalent to count = count + 1.

So count will take values: 1, 2, 3, 4, 5 during the iterations.

Line 4: Continue Condition
if count == 4:
    continue

If count equals 4, the continue statement executes.

continue means: skip the rest of this iteration and move to the next iteration.

Here, print(count) will be skipped when count == 4.

Line 5: Print Statement
print(count, end=" ")

If count is not 4, it will be printed.

end=" " ensures that the output is printed on the same line separated by a space.

Execution Flow
Iteration count value if count==4? Action Output so far
1             1                     No         print 1         1
2             2                     No         print 2         1 2
3             3                     No         print 3 1 2 3
4             4                     Yes continue (skip) 1 2 3
5             5                     No print 5 1 2 3 5

Final Output
1 2 3 5

HANDS-ON STATISTICS FOR DATA ANALYSIS IN PYTHON

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (150) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (216) Data Strucures (13) Deep Learning (67) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (185) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1215) Python Coding Challenge (882) Python Quiz (341) Python Tips (5) Questions (2) 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 (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)