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

Thursday, 13 March 2025

Python Coding Challange - Question With Answer(01130325)

 


Step-by-Step Execution:

  1. Initialization:

    number = 0
  2. First Iteration (Loop Start - number = 0):

    • number += 4 → number = 4
    • if number == 7: → False, so continue does not execute.
    • print(4)
  3. Second Iteration (Loop Start - number = 4):

    • number += 4 → number = 8
    • if number == 7: → False, so continue does not execute.
    • print(8)
  4. Third Iteration (Loop Start - number = 8):

    • Condition while number < 8: fails because number is no longer less than 8.
    • The loop stops.

Final Output:

4
8

Understanding the continue Statement:

  • The continue statement skips the rest of the current iteration and moves to the next loop cycle.
  • However, number == 7 never occurs in this program because the values of number are 4 and 8.
  • So, the continue statement never runs and has no effect in this case.

Key Observations:

  1. The loop increments number by 4 in each iteration.
  2. The condition number == 7 never happens.
  3. The loop stops when number reaches 8.

Wednesday, 12 March 2025

Python Coding Challange - Question With Answer(01120325)

 


Let's analyze the code step by step:

python
val = 9 # Step 1: Assigns the value 9 to the variable 'val'
val = 10 # Step 2: Reassigns the value 10 to the variable 'val'
print(val) # Step 3: Prints the current value of 'val'

Explanation:

  1. First assignment (val = 9): The variable val is created and assigned the value 9.

  2. Second assignment (val = 10): The value 9 is overwritten by 10. In Python, variables store only one value at a time, so the previous value (9) is lost.

  3. Printing the value (print(val)): Since val now holds 10, the output will be:

    10

Key Takeaways:

  • Python executes statements sequentially.
  • The latest assignment determines the final value of a variable.
  • The previous value (9) is replaced and no longer accessible.

Thus, the output of the given code is 10.

Monday, 10 March 2025

Python Coding Challange - Question With Answer(01110325)

 


Step-by-Step Execution:

  1. Initial Values:

  • a = 6 
  • b = 3
  1. First Operation: a *= b

    • This means:

      a = a * b # 6 * 3 = 18
    • Now, a = 18 and b = 3.
  2. Second Operation: b *= a

    • This means:

      b = b * a # 3 * 18 = 54
    • Now, b = 54.
  3. Final Output:

    • print(b) prints 54.

Answer: 54

Python Coding Challange - Question With Answer(01100325)

 


Step-by-step Execution:

  1. Outer Loop (for i in range(0, 1)):

    • range(0, 1) generates the sequence [0], so the loop runs once with i = 0.
    • It prints 0.
  2. Inner Loop (for j in range(0, 0)):

    • range(0, 0) generates an empty sequence [] (because 0 to 0-1 is an empty range).
    • Since the range is empty, the inner loop does not execute at all.
    • No value of j is printed.

Output:

0

The inner loop never runs, so only i = 0 is printed.

Saturday, 8 March 2025

Python Coding Challange - Question With Answer(01080325)

 


Step-by-Step Execution:

  1. The string assigned to equation is:


    '130 + 145 = 275'
  2. The for loop iterates over each character (symbol) in the string.

  3. The if condition:

    if symbol not in '11':
    • The expression '11' is a string, not a set of separate characters.
    • When Python checks symbol not in '11', it interprets it as:
      • "1" in "11" → True
      • "1" in "11" → True (again)
      • Any other character not in "11" → True (so it gets printed)
    • Effectively, the condition only excludes the character '1'.
  4. The loop prints all characters except '1'.


Expected Output:

Removing '1' from the equation, we get:


3
0 +
4 5
=
2 7
5

Key Observation:

  • The not in '11' check works the same as not in '1' because Python does not treat "11" as separate characters ('1' and '1').
  • '11' as a string is not a set or a list, so it doesn't mean "both instances of 1."
  • This is why only '1' is removed.

Thursday, 6 March 2025

Python Coding Challange - Question With Answer(01070325)

 


Step-by-Step Execution:

  1. The range(1, 10, 3) generates numbers starting from 1, incrementing by 3 each time, up to (but not including) 10.

    • The sequence generated: 1, 4, 7
  2. First iteration: i = 1

    • print(1) → Output: 1
    • if(i == 4): Condition False, so break is not executed.
  3. Second iteration: i = 4

    • print(4) → Output: 4
    • if(i == 4): Condition True, so break is executed, stopping the loop.
  4. The loop terminates before reaching i = 7.

Final Output:

1
4

Explanation:

  • The loop starts at 1 and increments by 3 each iteration.
  • When i becomes 4, the break statement executes, stopping the loop.
  • The number 7 is never printed because the loop exits before reaching it.


Python Coding Challange - Question With Answer(01060325)

 


Step-by-Step Execution

  1. Initialize num

    • num = 2 (This sets num to 2 before the loop starts.)
  2. Start the while loop

    • The loop runs as long as num < 7.
  3. Loop Iteration Details

    • First Iteration:
      • num += 2 → num = 2 + 2 = 4
      • print(num) → Output: 4
    • Second Iteration:
      • num += 2 → num = 4 + 2 = 6
      • print(num) → Output: 6
    • Third Iteration:
      • num += 2 → num = 6 + 2 = 8
      • print(num) → Output: 8
  4. Exit Condition

    • Now, num = 8, which is not less than 7, so the loop stops.

Final Output

4
6
8

Tuesday, 4 March 2025

Python Coding Challange - Question With Answer(01050325)

 


Explanation:

  1. The variable num is initialized with the value 1.
  2. The while loop checks the condition num < 5. Since 1 < 5 is True, the loop executes.
  3. Inside the loop, print(num) prints the current value of num.
  4. However, there is no statement to increment num, meaning num always remains 1.
  5. Since 1 < 5 is always True, the loop never stops and results in an infinite loop.

Output (Infinite Loop):

plaintext
1
1 1 1
...

(The loop will continue printing 1 forever.)

How to Fix It?

To ensure the loop terminates correctly, we should increment num inside the loop:


num = 1
while num < 5: print(num)
num += 1 # Increment num

Correct Output:


1
2 3
4

Now, the loop stops when num becomes 5, as 5 < 5 is False.

Monday, 3 March 2025

Python Coding Challange - Question With Answer(01040325)

 


Step-by-step evaluation:

  1. if num > 7 or num < 21:
    • num > 7 → False (since 7 is not greater than 7)
    • num < 21 → True (since 7 is less than 21)
    • False or True → True → Prints "1"
  2. if num > 10 or num < 15:
    • num > 10 → False (since 7 is not greater than 10)
    • num < 15 → True (since 7 is less than 15)
    • False or True → True → Prints "2"

Final Output:

1
2

Python Coding Challange - Question With Answer(01030325)

 


Step 1: print(0)

This prints 0 to the console.

Step 2: for i in range(1,1):

  • The range(start, stop) function generates numbers starting from start (1) and stopping before stop (1).
  • The range(1,1) means it starts at 1 but must stop before 1.
  • Since the starting value is already at the stopping value, no numbers are generated.

Step 3: print(i) inside the loop

  • Since the loop has no numbers to iterate over, the loop body is never executed.
  • The print(i) statement inside the loop is never reached.

Final Output:

0

Only 0 is printed, and the loop does nothing.

Thursday, 27 February 2025

Python Coding Challange - Question With Answer(01270225)

 


Step-by-Step Execution

  1. The outer loop (i) runs from 0 to 1 (i.e., range(0, 2) generates [0, 1]).
  2. Inside the outer loop, the inner loop (j) also runs from 0 to 1 (i.e., range(0, 2) generates [0, 1]).
  3. For each value of i, the inner loop runs completely before moving to the next i.

Execution Flow

Iterationi Valuej ValuePrinted Output
1st000 → 0
2nd011
3rd101 → 0
4th111

Final Output

0
0 1 1 0
1

👉 Key Takeaways:

  • The outer loop (i) controls how many times the inner loop runs.
  • The inner loop (j) executes completely for each value of i.
  • This results in a nested iteration structure where j repeats for every i.

Tuesday, 25 February 2025

Python Coding Challange - Question With Answer(01250225)

 


  1. range(1, 10, 3):

    • This generates a sequence of numbers starting from 1, up to (but not including) 10, with a step size of 3.
    • So, it will generate the numbers: 1, 4, 7.
  2. for i in range(1, 10, 3)::
    • This sets up a loop that will iterate through the values 1, 4, and 7 one by one.
  3. print(i):

    • This prints the current value of i during each iteration.
  4. if (i == 4)::

    • This checks if the current value of i is equal to 4.
  5. break:

    • If i equals 4, the break statement will terminate the loop immediately.
    • This means that when i reaches 4, the loop stops, and no further numbers are printed or processed.

What happens during execution:

  • In the first iteration, i = 1, so it prints 1.
  • In the second iteration, i = 4, so it prints 4 and then hits the if condition (i == 4), which triggers the break statement.
  • As a result, the loop stops immediately after i = 4 and does not proceed to the next number (7).

Output:

1
4

The loop breaks before it prints 7.

Monday, 24 February 2025

Python Coding Challange - Question With Answer(01240225)

 


Step-by-Step Execution:

  1. import numpy as np
    • This imports the NumPy library, which provides efficient array operations.
  2. numbers = np.array([1, 2, 3])
    • This creates a NumPy array:

      numbers = [1, 2, 3] (as a NumPy array)
  3. new_numbers = numbers + 1
    • NumPy allows element-wise operations, so adding 1 increases each element by 1:

      [1+1, 2+1, 3+1][2, 3, 4]
  4. print(new_numbers.tolist())
    • new_numbers.tolist() converts the NumPy array back into a Python list:

      [2, 3, 4]
    • Finally, it prints the result.

Final Output:


[2, 3, 4]

Why use NumPy?

  • Faster operations than standard Python lists.
  • Supports vectorized computations without loops.
  • Memory efficient for large datasets.

Saturday, 22 February 2025

Python Coding Challange - Question With Answer(01220225)

 


Code Analysis and Explanation


queue = {'name', 'age', 'DOB'}
print(queue)

1. Understanding the Code

  • queue is assigned a set containing three string elements: 'name', 'age', and 'DOB'.
  • The print(queue) statement displays the contents of the set.

2. Key Properties of Sets in Python

a) Sets are Unordered

  • Unlike lists ([]) or tuples (()), sets {} do not maintain a fixed order for their elements.
  • When printing, Python determines the order dynamically, so the output may vary each time you run the code.

b) Sets Contain Unique Elements

  • If duplicate values were added to the set, Python would automatically remove them because sets store only unique values.

c) Sets are Mutable

  • You can add or remove elements from a set using .add() and .remove().

3. Possible Outputs

Since sets do not maintain order, the printed output could be any of the following:


{'name', 'age', 'DOB'}
{'age', 'DOB', 'name'} {'DOB', 'name', 'age'}
{'DOB', 'age', 'name'}
  • The elements will always be present, but their order is not guaranteed.

4. Why is the Order Different Each Time?

  • Sets are implemented as hash tables in Python.
  • Hashing ensures fast lookups but does not maintain order.

5. What If You Want a Fixed Order?

If you want to maintain order, consider:

  1. Using a List ([])

    queue = ['name', 'age', 'DOB']
    print(queue) # Always prints: ['name', 'age', 'DOB']
  2. Sorting the Set Before Printing


    print(sorted(queue)) # Prints: ['DOB', 'age', 'name']

6. Example Set Operations


queue.add('gender') # Add an element
queue.remove('age') # Remove an element
print(queue) # Output may vary

7. Summary

Sets are unordered → Elements may print in a different order.
Sets contain unique elements → Duplicates are automatically removed.
Use lists if order matters → Lists maintain insertion order.

Friday, 21 February 2025

Python Coding Challange - Question With Answer(01210225)

 


Explanation:

  1. String Indexing

    • The string "Python" consists of characters indexed as follows:

      P y t h o n 0 1 2 3 4 5
    • Each character has a corresponding index, starting from 0.
  2. String Slicing (x[2:5])

    • The slice notation [start:stop] extracts characters from index start up to (but not including) stop.
    • Here, x[2:5] means:
      • Start from index 2 ('t')
      • Go up to index 5 (which is 'n' but not included)
      • The extracted portion is 'tho'.

Output:


tho

Thursday, 20 February 2025

Python Coding Challange - Question With Answer(01200225)

 

Step-by-Step Execution:

  1. Function Definition:

    python
    def extend_list(lst):
    lst.extend([10])
    • The function extend_list(lst) takes a list lst as an argument.
    • It uses .extend([10]), which appends 10 to the existing list.
  2. List Initialization:

    python
    b = [5, 10, 15, 20]
    • A list b is created with four elements: [5, 10, 15, 20].
    • Initially, len(b) = 4.
  3. Function Call:

    python
    extend_list(b)
    • The list b is passed to extend_list(lst).
    • Inside the function, lst.extend([10]) adds 10 to the end of b.
    • Now, b becomes [5, 10, 15, 20, 10].
  4. Printing the Length:

    python
    print(len(b))
    • The updated list has 5 elements: [5, 10, 15, 20, 10].
    • len(b) returns 5.

Final Output:

5

Key Points:

  1. .extend([10]) appends 10 to the list.
  2.  Since lists are mutable, b is modified directly.
  3. The function does not return a new list; it updates the existing list.
  4. len(b) increases from 4 to 5 after the function call.

Wednesday, 19 February 2025

Python Coding Challange - Question With Answer(01190225)

 


Step-by-Step Execution

  1. Define outer() function

    • Inside outer(), a variable x is initialized with the value 2.
    • inner() is defined inside outer() and modifies x using nonlocal.
  2. Define inner() function

    • inner() accesses the x variable from outer() using nonlocal.
    • The x *= 2 operation doubles the value of x.
    • inner() returns the updated value of x.
  3. Return the inner() function

    • When outer() is called, it returns inner() instead of executing it immediately.

Execution of closure = outer()

  • outer() is executed, and inner() is returned.
  • The x variable remains inside the closure and is remembered.

First Call: print(closure())

  • inner() is called.
  • x = 2 (from outer()).
  • x *= 2 → Now x = 4.
  • Returns 4, so it prints 4.

Second Call: print(closure())

  • inner() is called again.
  • x = 4 (previously modified value).
  • x *= 2 → Now x = 8.
  • Returns 8, so it prints 8.

Final Output

4
8

Key Concepts Used

  1. Closure: The function inner() remembers the variable x from outer(), even after outer() has finished execution.
  2. nonlocal keyword: This allows inner() to modify x in outer(), instead of creating a new local variable.
  3. State Retention: The value of x persists between function calls.

Tuesday, 18 February 2025

Python Coding Challange - Question With Answer(01180225)

 


Step-by-step Execution:

  1. Define a Set:


    set1 = {10, 20, 30}
    • set1 is a set containing {10, 20, 30}.
  2. Remove an Element:

    set2 = set1.remove(20)
    • The .remove(20) method removes the element 20 from set1.
    • However, remove() does not return anything; it modifies set1 in place.
    • This means set2 gets assigned the return value of set1.remove(20), which is None.
  3. Print the Result:


    print(set2)
    • Since set2 = None, the output will be:

      None

Final Output:


None

Key Learning:

  • .remove() modifies the original set but returns None.
  • If you assign set2 = set1.remove(x), set2 will always be None.

Monday, 17 February 2025

Python Coding Challange - Question With Answer(01170225)

 


Step-by-Step Execution:

First Iteration (a = 0)

  • b = 0 → d[0] = 0
  • b = 1 → d[0] = 1 (overwrites previous value)
  • b = 2 → d[0] = 2 (overwrites previous value)
  • b = 3 → d[0] = 3 (overwrites previous value)
  • b = 4 → d[0] = 4 (overwrites previous value)

Second Iteration (a = 1)

  • b = 0 → d[1] = 0
  • b = 1 → d[1] = 1 (overwrites previous value)
  • b = 2 → d[1] = 2 (overwrites previous value)
  • b = 3 → d[1] = 3 (overwrites previous value)
  • b = 4 → d[1] = 4 (overwrites previous value)

Final Dictionary Output

After the loops complete, we have:


{0: 4, 1: 4}

Why?

  • Each value of b replaces the previous one for the current a.
  • The final assigned value for both 0 and 1 is 4, since it's the last value of b in the inner loop.

Summary

  • The dictionary keys are 0 and 1 (values of a).
  • Each key gets assigned the final value of b, which is 4.
  • The dictionary ends up as {0: 4, 1: 4}.

Sunday, 16 February 2025

Python Coding Challange - Question With Answer(01160225)

 


Explanation of the Code


s = 'robot' # Assign the string 'robot' to variable s
a, b, c, d, e = s # Unpacking the string into individual characters # a = 'r', b = 'o', c = 'b', d = 'o', e = 't' b = d = '*' # Assign '*' to both b and d s = (a, b, c, d, e) # Create a tuple with the modified values
print(s) # Output the tuple

Step-by-Step Execution

VariableValue Before ModificationValue After Modification
a'r''r'
b'o''*'
c'b''b'
d'o''*'
e't''t'

Final Tuple Created

('r', '*', 'b', '*', 't')

Final Output

('r', '*', 'b', '*', 't')

Key Concepts Used:

  1. String Unpacking – The string 'robot' is unpacked into five variables (a, b, c, d, e).
  2. Variable Assignment – b and d are assigned '*'.
  3. Tuple Construction – A tuple (a, b, c, d, e) is created and printed.

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (248) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (145) 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 (10) 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 (81) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1018) Python Coding Challenge (454) Python Quiz (100) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) 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)