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

Friday, 18 April 2025

Python Coding Challange - Question with Answer (01180425)

 


Let's break down this Python for loop:

for i in range(0, -2, -2):
print(i)

range(start, stop, step) parameters:

  • start = 0 → where the sequence starts

  • stop = -2 → where the sequence stops (but this value is not included)

  • step = -2 → step (going down by 2)

What happens here?

This range(0, -2, -2) generates a sequence starting at 0 and moves backward in steps of -2, stopping before -2.

So it produces:

[0]

Because:

  • 0 is included ✔️

  • Next value would be -2, but the range excludes the stop value, so it's not included ❌

Output:

0

Summary:

This loop runs only once, printing 0.

APPLICATION OF PYTHON FOR CYBERSECURITY

https://pythonclcoding.gumroad.com/l/dfunwe

Thursday, 17 April 2025

Python Coding Challange - Question with Answer (01170425)

 


Let me explain this code:


num = 1

while num < 6:

    print(num)

This code has a few issues that would cause it to run indefinitely (infinite loop). Let me break down why and how to fix it:


First, num = 1 initializes a variable num with the value 1

The while loop continues executing as long as num < 6 is True

Inside the loop, print(num) prints the current value of num

However, the code is missing an increment statement to update num, so num will always stay 1 and the condition num < 6 will always be True

Here's the corrected version:


python    num += 1  # Increment num by 1 in each iteration

num += 1  # Increment num by 1 in each iteration


This corrected version will:

1. Start with num = 1

2. Print: 1

3. Increment num to 2

4. Print: 2

5. Increment num to 3

6. Print: 3

7. Increment num to 4

8. Print: 4

9. Increment num to 5

10. Print: 5

11. Increment num to 6

12. Stop (because num < 6 is no longer True)


The output will be:

1

2

3

4

5



The key lesson here is that when using while loops, you need to make sure:

1. You have a proper condition that will eventually become False

2. You update the variables involved in the condition inside the loop

3. The loop has a clear way to terminate

400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu



Wednesday, 16 April 2025

Python Coding Challange - Question with Answer (01160425)

 


Let's break down this Python code step by step:


import array as arr
  • This imports Python's built-in array module and gives it the alias arr.

  • The array module provides a space-efficient array (fixed-type) data structure, similar to lists but more efficient for large numeric data.



numbers = arr.array('i', [7, 8, -5])
  • This creates an array named numbers.

  • 'i' is the type code for signed integers (like int), meaning all elements in this array must be integers.

  • [7, 8, -5] is the list of integer values used to initialize the array.



print(numbers[1])
  • This prints the value at index 1 of the array.

  • In Python, indexing starts from 0. So:

    • numbers[0] → 7

    • numbers[1] → 8

    • numbers[2] → -5

Output: 8


Summary:

  • You're creating an integer array [7, 8, -5] and printing the second element (index 1), which is 8.


Application of Electrical and Electronics Engineering Using Python

https://pythonclcoding.gumroad.com/l/iawhhjb

Tuesday, 15 April 2025

Python Coding Challange - Question with Answer (01150425)

 


Explanation:

  • num = 6 sets num to 6.

  • decrement(num) calls the function, but num is passed by value (since integers are immutable in Python).

  • Inside the function, num -= 2 only affects the local variable. It doesn't change the original num.

  • So, print(num) still prints 6.

✅ Final Output:

6

400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu

Sunday, 13 April 2025

Python Coding Challange - Question with Answer (01140425)

 


Explanation step-by-step:

  1. Function Definition:


    def printArr(arr, index):

    This defines a function called printArr that takes:

    • arr: a list of elements

    • index: the current position in the list to print

  2. Base Condition (to stop recursion):


    if(index < len(arr)):

    This checks if the index is still within the bounds of the list. If not, the function stops (ends recursion).

  3. Print the current element:


    print(arr[index])

    This prints the element at the current index.

  4. Recursive Call:


    printArr(arr, index+1)

    This calls the function again, with index+1, to print the next element.


Sample Execution:

Given:


arr = [1, 3, 5, 7]
printArr(arr, 0)

Here's what happens:

CallOutput
printArr(arr, 0)1
printArr(arr, 1)3
printArr(arr, 2)5
printArr(arr, 3)7
printArr(arr, 4)(stops, since 4 >= len(arr))

✅ Output:

1
3 5
7

PYTHON INTERVIEW QUESTIONS AND ANSWERS

https://pythonclcoding.gumroad.com/l/ylxbs

Python Coding Challange - Question with Answer (01130425)



Step 1: color = 'white'

  • This assigns the string 'white' to the variable color.

Step 2: color[4]

  • Python strings are indexed starting from 0.

  • So, the characters in 'white' are indexed like this:


    w h i t e
    0 1 2 3 4
  • color[4] returns 'e'.

Step 3: 


color[5]

  • There is no character at index 5, because 'white' only has indices from 0 to 4.

  • Trying to access color[5] will raise an IndexError.

Final Result:

You’ll get:


IndexError: string index out of range

So the correct explanation is:

  • color[4] = 'e'

  • color[5] = Error (out of range)


400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu

Friday, 11 April 2025

Python Coding Challange - Question With Answer(01110425)

 


Understanding the code:

1. range(0, 6, 4)

  • This means: Start from 0, go up to (but not including) 6, and increment by 4.

  • So, it generates the numbers: 0 and 4.

2. Loop values:

The loop runs with:

    i = 0 
    i = 4

3. Inside the loop:

  • When i == 2: the continue statement would skip the rest of the loop and go to the next iteration.

  • But in this case, i is never 2, so continue is never executed.

4. print(i):

  • Since i is never 2, both 0 and 4 will be printed.

✅ Final Output:

0
4


PYTHON INTERVIEW QUESTIONS AND ANSWERS

https://pythonclcoding.gumroad.com/l/ylxbs

Thursday, 10 April 2025

Python Coding Challange - Question With Answer(01100425)

 


Line-by-line explanation:


prod = getProd(4,5,2)
  • This line is trying to call a function named getProd with arguments 4, 5, and 2.

  • However, at this point in the code, getProd is not yet defined, so Python will throw an error.

  • Error: NameError: name 'getProd' is not defined


print(prod)
  • This line will not run because the previous line causes an error.

  • If the function was properly defined before being called, this line would print the result of 4 * 5 * 2 = 40.


def getProd(a, b, c):
  • This defines a function named getProd that takes 3 arguments: a, b, and c.


return a * b * c
  • This returns the product of the three numbers passed to the function.


Summary:

  • The code tries to use the function before it's defined.

  • Python reads code from top to bottom, so it doesn't know what getProd is when it's first called.

  • To fix it, you should define the function before calling it.

Application of Python Libraries for Civil Engineering

https://pythonclcoding.gumroad.com/l/feegvl

Tuesday, 8 April 2025

Python Coding Challange - Question With Answer(01090425)

 


This is a recursive function. It calculates the sum of all numbers from 1 to num.


 How does recursion work here?

Let's see how sum(5) gets evaluated:

  1. sum(5) → 5 + sum(4)

  2. sum(4) → 4 + sum(3)

  3. sum(3) → 3 + sum(2)

  4. sum(2) → 2 + sum(1)

  5. sum(1) → returns 1 (base case)

Now plug values back in:

  • sum(2) = 2 + 1 = 3

  • sum(3) = 3 + 3 = 6

  • sum(4) = 4 + 6 = 10

  • sum(5) = 5 + 10 = 15


✅ Output:

15

In Simple Words:

This function adds:

5 + 4 + 3 + 2 + 1 = 15


Application of Python in Chemical Engineering

https://pythonclcoding.gumroad.com/l/dlrub

Python Coding Challange - Question With Answer(01080425)

 


Explanation

try block:

  • Python runs the code inside the try block first.

  • print("Hello") executes normally, so it prints:

    Hello
  • Then, raise Exception is used to manually raise an exception. This immediately stops the normal flow and jumps to the except block.

except block:

  • Since an exception occurred, the code inside the except block runs.

  • It prints:


    Python

✅ Final Output:


Hello
Python

This shows how Python handles exceptions:

  1. It tries the code.

  2. If something goes wrong (an exception is raised), it jumps to except and handles it.


100 Python Programs for Beginner with explanation 

https://pythonclcoding.gumroad.com/l/qijrws

Monday, 7 April 2025

Python Coding Challange - Question With Answer(01070425)

 


What happens:

  1. num = 6
    A variable num is created and assigned the value 6.

  2. decrement(num)
    This calls the decrement function and passes the value of num (which is 6) into the function.

  3. Inside the decrement function:

    python
    def decrement(num):
    num -= 2
    • Here, a new local variable num is created inside the function scope (it’s a copy of the one passed in).

    • num -= 2 changes this local copy to 4.

    • But the original num outside the function is not changed because integers are immutable and passed by value (in effect).

  4. print(num)
    This prints the original num, which is still 6.


Output:

6

Summary:

Even though the function decreases num by 2, it only does so inside the function. The original variable remains unchanged because integers are immutable and passed by value in Python functions.


400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu

Saturday, 5 April 2025

Python Coding Challange - Question With Answer(01050425)

 


 Step-by-step Explanation:

  1. playerScores = dict()
    • Creates an empty dictionary named playerScores.

  2. Adding player scores:

    playerScores["MS Dhoni"] = 125
    playerScores["Akshar"] = 133
    • Adds 2 entries to the dictionary:


      {
      "MS Dhoni": 125, "Akshar": 133
    • }
  3. Deleting one entry:

    del playerScores["MS Dhoni"]
    • Removes the key "MS Dhoni" from the dictionary.

    • Now it becomes:


      {
      "Akshar": 133
    • }
  4. Printing values:

    for key, value in playerScores.items():
    print(value)
    • Loops through each key-value pair in the dictionary.

    • Since only "Akshar" is left, it prints:

    Output:

    133

Let me know if you'd like a version that prints both the name and score like:


print(f"{key}: {value}")

Check Now 400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu

Thursday, 3 April 2025

Python Coding Challange - Question With Answer(01040425)

 


What’s happening here?

  • fruits is a list of 5 string items.

    ['Python', 'Py', 'Anaconda', 'CPython', 'Dragon']
    index: 0 1 2 3 4
  • fruits[1:3] is list slicing. It means:

    • Start at index 1 ('Py')

    • Go up to but not including index 3 ('CPython')

So it includes:

  • 'Py' (index 1)

  • 'Anaconda' (index 2)

It stops before index 3.


✅ Output:


['Py', 'Anaconda']

Check Now 400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu

Wednesday, 2 April 2025

Python Coding Challange - Question With Answer(01030425)

 


tep 1: First if Condition

if not (code >= 100 and code <= 200):
print("1")
  • code = 501, so we check:
      code >= 100 → True 
      code <= 200 → False 
      (code >= 100 and code <= 200) → (True and False) → False
  • not (False) → True
  • Since the condition is True, it executes:
    Output: 1 ✅


Step 2: Second if Condition

if not (code >= 400 or code == 501):
print("2")
  • code = 501, so we check:

      code >= 400 → True 
      code == 501 → True 
      (code >= 400 or code == 501) → (True or True) → True
  • not (True) → False
  • Since the condition is False, it does not execute print("2").


Final Output

1

"2" is not printed because its condition evaluates to False.

Check Now 300 Days Python Coding Challenges with Explanation 

https://pythonclcoding.gumroad.com/l/uvmfu

Python Coding Challange - Question With Answer(01020425)

 


Step-by-Step Breakdown:

  1. Variable Assignment:

    word = 'clcoding'

    The string 'clcoding' is assigned to the variable word.

  2. Negative Indexing:

    • In Python, negative indexing allows you to access elements from the end of the string.

    • Here’s the index mapping for 'clcoding':


      c l c o d i n g-
    • 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1
  3. Slicing word[-4:-2]:

    • word[-4:-2] means extracting characters from index -4 to -2 (excluding -2).

    • Looking at the negative indexes:

      • word[-4] → 'd'

      • word[-3] → 'i'

      • word[-2] → 'n' (not included in the slice)

    • The output includes characters at -4 and -3, which are "di".

Final Output:


di

This means the code prints:

di

Check Now 400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu

Monday, 31 March 2025

Python Coding Challange - Question With Answer(01010425)

 


Step-by-step Execution:

  1. try block execution:

    • The try block contains print("Python").

    • Since print("Python") does not cause any error, the statement executes successfully.

    • Output: Python

  2. except block execution:

    • The except block runs only if an exception occurs in the try block.

    • Since no exception occurs, this block is skipped.

  3. Final statement:

    • The next line after the try-except block is print("Anaconda"), which executes normally.

    • Output: Anaconda

Final Output:


Python
Anaconda

Key Takeaways:

  • The except block is only executed if an error occurs in the try block.

  • Since print("Python") executes without an error, the except block is skipped.

  • The program continues executing normally after the try-except block.


Check Now Python Libraries for Civil Engineering

https://pythonclcoding.gumroad.com/l/feegvl

Sunday, 30 March 2025

Python Coding Challange - Question With Answer(01300325)

 


Understanding any() Function:

  • The any() function checks if at least one element in the iterable satisfies the given condition.

  • The condition used is num % 5 == 0, which checks if the number is divisible by 5.

Step-by-Step Execution:

First List: numbers1 = [10, 20, 33]

  • 10 % 5 == 0 ✅ (True)

  • 20 % 5 == 0 ✅ (True)

  • 33 % 5 == 0 ❌ (False)

Since at least one number (10 and 20) is divisible by 5, any() returns True.

Second List: numbers2 = [7, 15, 27]

  • 7 % 5 == 0 ❌ (False)

  • 15 % 5 == 0 ✅ (True)

  • 27 % 5 == 0 ❌ (False)

Since 15 is divisible by 5, any() returns True.

Third List: numbers3 = [9, 14, 21]

  • 9 % 5 == 0 ❌ (False)

  • 14 % 5 == 0 ❌ (False)

  • 21 % 5 == 0 ❌ (False)

Since no number in the list is divisible by 5, any() returns False.

Final Output:


True
True
False

CHECK NOW 400 Days Python Coding Challenges with Explanation

https://pythonclcoding.gumroad.com/l/sputu

Friday, 28 March 2025

Python Coding Challange - Question With Answer(01280325)

 


Explaining String Slicing: word[4:-3:2] in Python

Given the string:


word = 'pythonCoding'

Let's break down the slicing operation [4:-3:2]:

Understanding the Slicing Syntax


word[start:end:step]
  • start = 4 → Begins at index 4 (5th character).

  • end = -3 → Stops at index -3 (3rd character from the end, not included).

  • step = 2 → Selects every second character.


Indexing the String


word = 'pythonCoding'
p y t h o n C o d i n g index 0 1 2 3 4 5 6 7 8 9 10 11
-index -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

Now, applying word[4:-3:2]:

  1. Start at index 4 → 'o'

  2. Step by 2:

    • Index 6 → 'C'

    • Index 8 → 'd'

  3. Stop before index -3 ('i')

Thus, the selected characters are 'oCd'.


Verifying with Code


word = 'pythonCoding'
print(word[4:-3:2]) # Output: 'oCd'

Visualizing the Selection


word = 'pythonCoding'
p y t h o n C o d i n g index 0 1 2 3 4 5 6 7 8 9 10 11 ↓ ↓ ↓
o C d

Each selected character:

  • 'o' → (index 4)

  • 'C' → (index 6, after skipping n)

  • 'd' → (index 8, after skipping o)

The slicing stops before reaching 'i' (index -3).


Conclusion

This slicing technique efficiently extracts a pattern of characters using:  Sart and end indexes
Step size for skipping characters
Combination of positive & negative indexing


Check Now 100 Python Programs for Beginner with explanation

https://pythonclcoding.gumroad.com/l/qijrws

Popular Posts

Categories

100 Python Programs for Beginner (104) AI (41) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (200) C (77) C# (12) C++ (83) Course (67) Coursera (252) Cybersecurity (25) Data Analysis (3) Data Analytics (3) data management (11) Data Science (149) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (86) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1063) Python Coding Challenge (461) Python Quiz (134) Python Tips (5) Questions (2) R (70) React (6) Scripting (3) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)