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

Friday, 21 August 2026

Python Coding Challenge - Question with Answer (ID 210826)


Explanation:

1. Import Fraction
from fractions import Fraction

The fractions module provides the Fraction class for working with exact rational numbers.

For example:

Fraction(3, 4)

represents exactly:

3/4

Unlike floating-point numbers, Fraction keeps the result exact.

2. Create and Multiply Fractions
x = Fraction(3, 4) * Fraction(8, 9)

Here, two fractions are created:

Fraction(3, 4) → 3/4
Fraction(8, 9) → 8/9

Python multiplies them:

3/4 × 8/9

3. Simplify the Result

Multiply the numerators:

3 × 8 = 24

Multiply the denominators:

4 × 9 = 36

So:

24/36

Python automatically simplifies this fraction:

24/36 = 2/3

Therefore:

x

contains:

2/3

4. print(x)
print(x)

The print() function displays the value stored in x.

✅ Final Output
2/3

Book: Numerical Python for Astronomy and Astrophysics



Thursday, 20 August 2026

Python Coding Challenge - Question with Answer (ID 200826)

 


Explanation:

1. Complete Code
print(dict(zip("ABC", range(3)))["B"])

2. range(3)

First, Python evaluates:

range(3)

This generates:

0, 1, 2

So we have:

"ABC"  →  A  B  C
range  →  0  1  2

3. zip("ABC", range(3))

zip() pairs the elements from both sequences:

zip("ABC", range(3))

creates pairs conceptually like:

('A', 0)
('B', 1)
('C', 2)

4. dict()

Now dict() converts those pairs into a dictionary:

dict(zip("ABC", range(3)))

The resulting dictionary is:

{'A': 0, 'B': 1, 'C': 2}

5. ["B"] — Dictionary Lookup

Now Python accesses the value associated with key "B":

{'A': 0, 'B': 1, 'C': 2}["B"]

The value of "B" is:

1

6. print()

Finally:

print(1)

displays the result.

✅ Final Output
1

Book: AUTOMATING EXCEL WITH PYTHON

Wednesday, 19 August 2026

Python Coding Challenge - Question with Answer (ID 190826)

 


Explanation:

1. Code
print(abs(3+4j))

2. 3 + 4j — Complex Number

In Python, j represents the imaginary unit.

A complex number has the form:

a + bj

Here:

Real part = 3
Imaginary part = 4

So:

3 + 4j

is a complex number.

3. abs() — Finding Magnitude

For a complex number:

abs(a + bj)

calculates its magnitude using:

√(a² + b²)

For 3 + 4j:

√(3² + 4²)
= √(9 + 16)
= √25
= 5

4. print() — Displaying the Result

After calculating:

abs(3+4j)

Python gets:

5.0

Then print() displays it.

5. Final Output
5.0

Book: Numerical Python for Astronomy and Astrophysics

Tuesday, 18 August 2026

Python Coding Challenge - Question with Answer (ID 180826)



Explanation:

Code
print(True << 3 | False)

Heading: Step 1 — True as an Integer

In Python, Boolean values behave like integers in arithmetic and bitwise operations:

True = 1
False = 0

So the expression becomes:

1 << 3 | 0

Heading: Step 2 — Left Shift <<
1 << 3

The << operator shifts the binary bits 3 positions to the left.

Binary representation:

1  →  0001

After shifting 3 positions:

0001 << 3
1000

Binary 1000 is decimal 8.

Therefore:

1 << 3

gives:

8

Heading: Step 3 — Bitwise OR |

Now we have:

8 | 0

Binary:

8 → 1000
0 → 0000

Bitwise OR gives 1 whenever at least one corresponding bit is 1:

1000
0000
----
1000

1000 in binary is 8.

Heading: Step 4 — Final Output

Therefore:

print(True << 3 | False)

produces:

8

Final Answer

Output: 8

Book: 100 Python Challenges to Think Like a Developer

Monday, 17 August 2026

Python Coding Challenge - Question with Answer (ID 170826)

 


Explanation:

1. Code
print(True + True * 2)
\
2. True as an Integer

In Python, bool is a subclass of int.

So Python treats:

True

as:

1

Therefore:

True = 1

3. True * 2

First, Python evaluates the multiplication:

True * 2

Since True is 1:

1 × 2 = 2

So:

True * 2

becomes:

2

4. True + 2

Now the expression becomes:

1 + 2

Therefore:

3

5. Operator Precedence

Python performs multiplication before addition.

So:

True + True * 2

is evaluated as:

True + (True * 2)

not:

(True + True) * 2

6. print()

Finally:

print(3)

displays the result.

✅ Final Output
3

Book: 100 Python Automation Projects for Smart Developers

Sunday, 16 August 2026

Python Coding Challenge - Question with Answer (ID 160826)

 

Explanation:

-0 — Negative Zero

The expression -0 means negative zero.

But in Python, when using integers:

-0

is simply:

0

So Python treats both as the same integer value.


 == — Equality Operator

The == operator checks whether two values are equal.

Python evaluates:

-0 == 0

Since -0 is equal to 0:

0 == 0

the result is:

True


 print() — Display the Result

The print() function displays the result of the comparison:

print(True)


 Final Output

True


Saturday, 15 August 2026

Python Coding Challenge - Question with Answer (ID 150826)

 


Explanation:

1. int("11010", 2)
"11010" is a binary number.
The 2 tells Python to interpret it as base 2.
Binary 11010 = decimal 26.
int("11010", 2)  # 26

2. int("10101", 2)
"10101" is also a binary number.
Python converts it from base 2 to decimal.
Binary 10101 = decimal 21.
int("10101", 2)  # 21

3. ^ — Bitwise XOR

Now Python performs XOR:

  11010
^ 10101
-------
  01111

XOR rules:

Bit 1 Bit 2 Result
0           0             0
0          1             1
1          0             1
1          1             0

So:

11010
10101
-----
01111

01111 in binary = 15 in decimal.

4. print(...)

Finally, print() displays the result:

15

1. int("11010", 2)

  • "11010" is a binary number.
  • The 2 tells Python to interpret it as base 2.
  • Binary 11010 = decimal 26.
int("11010", 2) # 26

2. int("10101", 2)

  • "10101" is also a binary number.
  • Python converts it from base 2 to decimal.
  • Binary 10101 = decimal 21.
int("10101", 2) # 21

3. ^ — Bitwise XOR

Now Python performs XOR:

11010
^ 10101
-------
01111

XOR rules:

Bit 1Bit 2Result
000
011
101
110

So:

11010
10101
-----
01111

01111 in binary = 15 in decimal.

4. print(...)

Finally, print() displays the result:

15

✅ Final Output

15
15

Friday, 14 August 2026

Python Coding Challenge - Question with Answer (ID 140826)

 


Explanation:

1. 7 ^ 3 — Bitwise XOR

The ^ operator performs Bitwise XOR.

Convert the numbers into binary:

7 = 111
3 = 011

Apply XOR:

  111
^ 011
-----
  100

100 in binary is 4.

So:

7 ^ 3

becomes:

4

2. 4 & 5 — Bitwise AND

Now Python evaluates:

4 & 5

Binary representation:

4 = 100
5 = 101

AND keeps 1 only when both bits are 1:

  100
& 101
-----
  100

100 in binary is 4.

3. print()

The final statement becomes:

print(4)

✅ Output
4

Book: 100 Python Challenges to Think Like a Developer

Thursday, 13 August 2026

Python Coding Challenge - Question with Answer (ID 130826)

 


Explanation:

1. ord("A")

ord() converts a character into its Unicode code point.

ord("A")

Output:

65

So, "A" → 65.

2. ord("a")

Similarly:

ord("a")

Output:

97

So, "a" → 97.

3. ^ — Bitwise XOR

Now Python evaluates:

65 ^ 97

Convert both numbers to binary:

65 = 01000001
97 = 01100001

XOR rules:

0 ^ 0 → 0
0 ^ 1 → 1
1 ^ 0 → 1
1 ^ 1 → 0

Therefore:

  01000001
^ 01100001
-----------
  00100000

00100000 in decimal is 32.

4. print()

Finally:

print(32)

✅ Final Output
32

Wednesday, 12 August 2026

Python Coding Challenge - Question with Answer (ID 120826)

 


Explanation:

1. print()

print() ka kaam hai final result ko screen par display karna.

2. lambda x: x*2

Ye ek anonymous function hai — yani function ka koi naam nahi hai.

Normally hum likhte:

def double(x):
    return x*2

Lekin lambda mein:

lambda x: x*2
x → input
x*2 → input par operation

3. (3+2)

Pehle Python brackets ke andar calculation karega:

3+2

Result:

5

4. (lambda x:x*2)(5)

Ab 5 lambda function ko diya gaya:

lambda x: x*2

So:

x = 5

5. x*2

Ab function calculate karega:

5*2

Result:

10
6. Final print()

print() ko 10 milta hai, therefore:

10

Book: Data Analysis Using ML Models (RandomForestClassifier, DecisionTreeClassifier, LogisticRegression)

Tuesday, 11 August 2026

Python Coding Challenge - Question with Answer (ID 110826)

 


Explanation:

๐Ÿ”น Step 1 — "1101"
"1101"

This is a string containing four characters:

1  1  0  1

๐Ÿ”น Step 2 — map(int, "1101")
map(int, "1101")

map() applies int() to each character:

"1" → 1
"1" → 1
"0" → 0
"1" → 1

So the values are:

1, 1, 0, 1

๐Ÿ”น Step 3 — sum()
sum(map(int, "1101"))

Now Python adds them:

1 + 1 + 0 + 1 = 3

So:

sum(...) → 3

๐Ÿ”น Step 4 — % 3

Now the expression becomes:

3 % 3

% is the modulo operator. It gives the remainder after division.

3 ÷ 3 → remainder 0

Therefore:

3 % 3 → 0

๐Ÿ”น Step 5 — print()

Finally:

print(0)

✅ Output
0

Book: 100 Python Projects — From Beginner to Expert


Monday, 10 August 2026

Python Coding Challenge - Question with Answer (ID 100826)

 


Code Explanation:

๐Ÿ”น Line 1: zip("abc", "123")

zip() dono strings ke corresponding characters ko pair karta hai:

a → 1
b → 2
c → 3

So, result logically becomes:

[("a", "1"), ("b", "2"), ("c", "3")]

๐Ÿ”น Line 2: dict(...)

dict() in pairs ko dictionary mein convert karta hai:

{
    "a": "1",
    "b": "2",
    "c": "3"
}

๐Ÿ”น Line 3: ["b"]

["b"] dictionary se key "b" ki value access karta hai:

dict(... )["b"]

Result:

"2"

๐Ÿ”น Line 4: print(...)

Finally, print() value ko screen par display karta hai.

✅ Output
2

Book: 100 Days of Math with Python

Sunday, 9 August 2026

Python Coding Challenge - Question with Answer (ID 090826)

 


Explanation:

๐Ÿ”น Line 1 — print()

print(...)

print() displays the final result on the screen.

๐Ÿ”น Step 1 — "10101"
"10101"

This is a string, containing five characters:

1  0  1  0  1

It is not being treated as a binary number here.

๐Ÿ”น Step 2 — map(int, "10101")
map(int, "10101")

map() applies int() to every character:

"1" → 1
"0" → 0
"1" → 1
"0" → 0
"1" → 1

So the values produced are:

1, 0, 1, 0, 1

๐Ÿ”น Step 3 — sum()
sum(map(int, "10101"))

sum() adds those values:

1 + 0 + 1 + 0 + 1

Result:

3

๐Ÿ”น Step 4 — print()

Now the complete expression becomes:

print(3)

So Python displays:

✅ Output
3


Saturday, 8 August 2026

Python Coding Challenge - Question with Answer (ID 080826)

 


Code Explanation:


๐Ÿ”น Line 1: Create the List

[1, 2, 3]

This list contains three integers:

1    2    3

Python needs to convert these integers into strings before joining them.


๐Ÿ”น Step 2: Apply map()

map(str, [1, 2, 3])

map() applies the str function to each element of the list.

So:

1 → "1"

2 → "2"

3 → "3"

Conceptually, the result is:

"1", "2", "3"

๐Ÿ”น Step 3: Understand str

str(1) → "1"

str(2) → "2"

str(3) → "3"


The important point is that the numbers are now strings, not integers.


๐Ÿ”น Step 4: Use join()

"-".join(...)

The string:

"-"

acts as the separator.

It places - between every string.

So:

"1" + "-" + "2" + "-" + "3"

becomes:

"1-2-3"

๐Ÿ”น Step 5: Execute print()

The final string is:

"1-2-3"

So:

print("1-2-3")

produces:

1-2-3


107 Pattern Plots Using Python 


Friday, 7 August 2026

Python Coding Challenge - Question with Answer (ID 070826)

 


Explanation:

๐Ÿ”น Line 1: Call print()
print("42".isdecimal())

Before print() displays anything, Python first evaluates:

"42".isdecimal()

๐Ÿ”น Step 1: Create the String
"42"

Python creates a string containing two characters.

Memory Representation

Index:   0   1
        ┌───┬───┐
Value:  │ 4 │ 2 │
        └───┴───┘

Notice that these are characters, not integer values.

๐Ÿ”น Step 2: Call the isdecimal() Method
"42".isdecimal()

The isdecimal() method checks whether every character in the string is a decimal digit (0–9).

General Syntax

string.isdecimal()

It returns:

True → If all characters are decimal digits and the string is not empty.
False → Otherwise.

๐Ÿ”น Step 3: Check Each Character

Python examines every character one by one.

First Character

'4'

Is '4' a decimal digit?

✅ Yes

Second Character

'2'

Is '2' a decimal digit?

✅ Yes

Since every character is a decimal digit, the result becomes:

True

๐Ÿ”น Step 4: Execute print()

Now Python executes:

print(True)

Output

True

Thursday, 6 August 2026

Python Coding Challenge - Question with Answer (ID 060826)

 


Explanation:

๐Ÿ”น Line 1: Call print()
print("abc".split(""))

Before print() can display anything, Python first evaluates:

"abc".split("")

๐Ÿ”น Step 1: Create the String
"abc"

Python creates a string containing three characters.

Memory Representation

Index:   0   1   2
        ┌───┬───┬───┐
Value:  │ a │ b │ c │
        └───┴───┴───┘

๐Ÿ”น Step 2: Call the split() Method
"abc".split("")

The split() method divides a string into smaller parts using a separator.

General Syntax:

string.split(separator)

Examples:

"Python Java".split(" ")

Output

['Python', 'Java']

๐Ÿ”น Step 3: Check the Separator

In this code, the separator is:

""

This is an empty string.

Python checks whether the separator is valid.


๐Ÿ”น Step 4: Python Detects an Invalid Separator

An empty string cannot be used as a separator because Python would have infinitely many places where it could split the string.

For example:

|a|b|c|

Should it split:

Before every character?
After every character?
Between every character?

Since this is ambiguous, Python does not allow an empty string as a separator.

Instead, it raises an exception.


๐Ÿ”น Step 5: Exception Is Raised

Python immediately raises:

ValueError: empty separator

Because an exception occurs, print() never gets a value to display.

Final Output :
Error

Book: 100 Python Automation Projects for Smart Developers

Wednesday, 5 August 2026

Python Coding Challenge - Question with Answer (ID 050826)

 


Explanatiom:

1. print() Function
print(...)
The print() function displays the result on the screen.
Whatever value is returned by count() is printed.

2. The String
"Python"
"Python" is a string.
It contains 6 characters.
Index Character
0 P
1 y
2 t
3 h
4 o
5 n

3. The count() Method
"Python".count("")
count() counts how many times a substring appears in a string.
Here, the substring is an empty string ("").

4. Why Does It Return 7?

The empty string exists at every possible position in the string.

|P|y|t|h|o|n|

Positions:

Before P
Between P and y
Between y and t
Between t and h
Between h and o
Between o and n
After n

Since "Python" has 6 characters, there are 7 possible positions.

Therefore,

"Python".count("")

returns

7

5. Final Execution
print("Python".count(""))
count("") returns 7.
print() displays 7 on the screen.


Final Output
7

Book: 100 Python Projects — From Beginner to Expert

Tuesday, 4 August 2026

Python Coding Challenge - Question with Answer (ID 040826)

 




Explanation:

๐Ÿ”น Line 1: Call print()
print("0" * False)

Before print() displays anything, Python first evaluates the expression:

"0" * False

๐Ÿ”น Step 1: Understand the String
"0"

This is a string containing a single character.

Current Value:

"0"

Length:

1

๐Ÿ”น Step 2: Evaluate False
False

Here's the trick.

In Python, bool is a subclass of int.

So internally:

False == 0

returns

True

This means Python treats:

False

as

0

So the expression becomes:

"0" * 0

๐Ÿ”น Step 3: Multiply the String

Python now evaluates:

"0" * 0

String multiplication means:

Repeat the string N times.

Examples:

"A" * 3

Output

AAA

But here,

"0" * 0

means:

Repeat the string zero times.

So Python creates an empty string.

Result:

""

๐Ÿ”น Step 4: Execute print()

Now Python executes:

print("")

Since the string is empty, nothing visible is printed.

The output appears as:

''

Final output:
""

Monday, 3 August 2026

Python Coding Challenge - Question with Answer (ID 030826)

 


Explanation:

๐Ÿ”น Line 1: Create the First Set
{1}

Python creates a set containing one element.

Current Set:

{1}

Memory:

Set A

{1}

๐Ÿ”น Line 2: Create the Second Set
{1, 2}

Python creates another set containing two unique elements.

Current Set:

{1, 2}

Memory:

Set B

{1, 2}

๐Ÿ”น Line 3: Compare Using <
{1} < {1, 2}

This is the biggest trick.

Most developers think:

"< compares numbers."

❌ Wrong!

For sets, the < operator does not compare values numerically.

Instead, it checks whether the left set is a proper subset of the right set.

Meaning:

"Are all elements of the left set present in the right set, and does the right set have at least one extra element?"

๐Ÿ”น Step 1: Check Every Element

Python checks whether every element in:

{1}

exists inside:

{1, 2}

Check:

1 ✓ Found

All elements are present.

๐Ÿ”น Step 2: Is It a Proper Subset?

Now Python checks whether the right set has more elements.

Left Set

{1}


1 Element

-------------------

Right Set

{1,2}


2 Elements

Since:

Every element of the left set exists in the right set ✅
The right set contains an extra element (2) ✅

It is a proper subset.

Result:

True

๐Ÿ”น Step 3: Execute print()

Python now executes:

print(True)

Output:

True

Book: 100 Python Projects — From Beginner to Expert

Sunday, 2 August 2026

Python Coding Challenge - Question with Answer (ID 020726)

 


Explanation:

๐Ÿ”น 1. Creating a Tuple

(1, 2, 3)

✅ Explanation

Python creates a tuple containing three elements.

A tuple is ordered and immutable (cannot be modified after creation).


Current Memory


Tuple


Index


0 → 1


1 → 2


2 → 3


Visual Representation


      Tuple


+-----+-----+-----+

|  1  |  2  |  3  |

+-----+-----+-----+

   0     1     2


Nothing is printed yet.


๐Ÿ”น 2. Applying Slice

[1:1]

✅ Explanation


Python applies slicing using the syntax:


[start : stop]


Here,


Start Index = 1


Stop Index = 1


Important Rule:


Start index is included.

Stop index is excluded.


Current Memory


Tuple


0 → 1


1 → 2


2 → 3


Slice


Start = 1


Stop = 1

๐Ÿ”น 3. Understanding the Slice

(1, 2, 3)[1:1]

✅ Explanation


Python starts at index 1.


Index 1



2


But the stop index is also 1.


Since slicing stops before reaching the stop index, Python has no elements to collect.


Visual Representation


Tuple


+-----+-----+-----+

|  1  |  2  |  3  |

+-----+-----+-----+

   0     1     2


Start

  │

  ▼

  1


Stop

  │

  ▼

  1


No elements between them.


Result


( )


An empty tuple is returned.


๐Ÿ”น 4. Printing the Result

print((1, 2, 3)[1:1])

✅ Explanation


Python prints the sliced tuple.


Since the slice contains no elements, the output is an empty tuple.


Output


( )


๐ŸŽฏ Final Output

( )

Book: amzn.to/4pRD2M5

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (337) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (337) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (88) Coursera (302) Cybersecurity (34) data (10) Data Analysis (46) Data Analytics (31) data management (16) Data Science (420) Data Strucures (18) Deep Learning (215) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (54) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (387) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (34) Python (1360) Python Coding Challenge (1223) Python Mathematics (11) Python Mistakes (51) Python Quiz (606) Python Tips (100) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (19) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)