0% found this document useful (0 votes)
13 views

Basics of Python - DPP 01

programming notes.

Uploaded by

mallicksahil5322
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Basics of Python - DPP 01

programming notes.

Uploaded by

mallicksahil5322
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

GATE

DS&AI
Python for Data Science DPP: 1
Basics of Python
Q1 The output of below python code segment is
_________ (A) A-1, B-2, C-2, D-1
a = 5.5 (B) A-2, B-2, C-1, D-1
b = 2.0 (C) A-2, B-1, C-2, D-2
c=2 (D) A-2, B-1, C-1, D-2
d = –2.0
Q5 The output of below code segment is _________
e = a // b
a = 0° 63
f = d // c
b = a<<2
print (e + f)
c = a>>3
(A) 0 (B) 0.0
print(b+c)
(C) 1.0 (D) –1.0
Q6 What will be printed by below Python Code?
Q2 The ascending order Of Precedence of below
i=0xAE1
Operators is ________
j = i & 152
1. not in
k = j | 100
2. <<
print (k)
3. not
(A) 344 (B) 00344
4. ^
(C) 0xe4 (D) 228
(A) 3, 2, 1, 4 (B) 3, 1, 2, 4
(C) 3, 1, 4, 2 (D) 2, 4, 1, 3 Q7 What will be the value of result in the below
code?
Q3 The result after evaluating the below expression
x = 0b01010110
in Python is _________
y = 0x123
result = 14 & 4 + 5 << 2 ^ 19 + 3 // 7 – 3 >> 4
z = 42
Q4 Match The Following Operators with their result=x+y-z
associativity. (A) 517 in octal
(B) 517 in decimal
LIST-I LIST-II (C) 1f4 in hexa decimal
(D) 335 in in decimal
(A) ** (Exponentiation) 1. Left To Right
Q8 What will be the value of k in the below code?
i = –13.5
(B) & (Bitwise AND) 2. Right To Left
j=5
k=i%j
(C) is not (Identity)
print (k)
(A) 0.0 (B) 1.5
(D) = (Assignment)
(C) 3.5 (D) –1.5

Android App | iOS App | PW Website

1/5
GATE

Answer Key
Q1 (C) Q5 210

Q2 (C) Q6 (D)

Q3 5 Q7 (A, D)

Q4 (D) Q8 (B)

Android App | iOS App | PW Website

2/5
GATE

Hints & Solutions


Q1 Text Solution: Precedence Levels

Compute e 1. not: Logical NOT has a higher precedence


than most operators but lower than
a = 5.5
comparison operators and some arithmetic
b = 2.0
operators.
We need to compute e = a // b, where
2. in: Membership operators have precedence
// is the floor division operator.
that is similar to comparison operators but
In Python, floor division of floating-point
lower than bitwise shift operators.
numbers results in a floating-point number
3. <<: Bitwise shift operators (<<, >>) have higher
that is floored.
precedence than bitwise XOR (^) and logical
5.5 // 2.0 evaluates to 2.0, because operators.
5.5 / 2.0 = 2.75, and the floor of 4. ^: Bitwise XOR has lower precedence than
2.75 is 2.0. bitwise shift operators but higher than logical
Compute f operators like not.

d = -2.0 Summary of Precedence


c = 2 not has the highest precedence among the
We need to compute f = d // c. given operators.
-2.0 // 2 evaluates to -2.0, because in has lower precedence than bitwise shift
-2.0 / 2 = -1.0, and the floor of operators.
-1.0 is -1.0. However, since the floor << (bitwise left shift) has higher precedence
division result for negative numbers than bitwise XOR.
using float in Python is floored to the ^ (bitwise XOR) has the lowest precedence
next smallest integer value, the result is among the given operators.
-2.0. Ascending Order of Precedence
Calculate e + f 1. ^ (lowest precedence)
e = 2.0 2. <<
f = -2.0 3. in
Adding these values together: e + f = 4. not (highest precedence)
2.0 + (-2.0) = 0.0 Therefore, the ascending order of precedence is:
^ < << < in < not
Q2 Text Solution:
Correct Answer
To determine the ascending order of precedence
(C)
of the given operators, let's first review the
precedence levels of each operator in Python: Q3 Text Solution:

1. not: Logical negation operator 1. Handle bitwise XOR and AND:


2. in: Membership operator Bitwise AND (&): 14 & 20 evaluates to 4.
3. <<: Bitwise left shift operator (In binary, 14 is 1110 and 20 is 10100. The
4. ^: Bitwise XOR operator bitwise AND results in 100 which is 4).

Android App | iOS App | PW Website

3/5
GATE

Bitwise XOR (^): 4 ^ 19 evaluates to 15. B (Bitwise AND): 1 (Left to Right)


(In binary, 4 is 100 and 19 is 10011. The C (Identity): 1 (Left to Right)
bitwise XOR results in 1111 which is 15). D (Assignment): 2 (Right to Left)

Conclusion
The result of the expression 14 & 4 + 5 << 2 ^
Q5 Text Solution:
19 + 3 // 7 - 3 >> 4 is 5.
Left Shift (a << 2):
Q4 Text Solution:
Operators and Their Associativity 63 << 2 means shifting the bits of 63
(which is 00111111 in binary) left by 2
1. Exponentiation (**)
positions.
Associativity: Right to Left Result: 11111100 in binary, which is 252 in
Explanation: Exponentiation in Python is decimal.
evaluated from right to left, meaning 2 **
Right Shift (a >> 3):
3 ** 2 is evaluated as 2 ** (3 ** 2).
63 >> 3 means shifting the bits of 63
2. Bitwise AND (&)
(which is 00111111 in binary) right by 3
Associativity: Left to Right positions.
Explanation: Bitwise AND is evaluated from Result: 00000111 in binary, which is 7 in
left to right, meaning a & b & c is decimal.
evaluated as (a & b) & c.
Q6 Text Solution:
3. Identity (is not)
1. Hexadecimal to Decimal Conversion
Associativity: Right to Left
Explanation: Identity operators like is not i = 0xAE1 represents a hexadecimal

are evaluated from left to right, but they number. Convert it to decimal:

are usually considered in terms of 0xAE1 in hexadecimal is equal to 2785 in


comparisons. For practical purposes in decimal.
Python, comparisons like is and is not
2. Bitwise AND Operation
are evaluated left to right.
j = i & 152
4. Assignment (=)
First, convert 152 to binary:
Associativity: Right to Left
152 in binary is 10011000.
Explanation: Assignment operators are
evaluated from right to left, meaning a = b Perform the bitwise AND operation
= c is evaluated as a = (b = c). between 2785 (in binary: 10101110 0001)
and 152 (in binary: 00000000 10011000):
Matching Operators with Their Associativity
In binary: 10101110 0001 & 00000000
1. ** (Exponentiation): Right to Left
10011000 = 00000000 00001000
2. & (Bitwise AND): Left to Right
(binary) = 8 in decimal.
3. is not (Identity): Left to Right
4. = (Assignment): Right to Left 3. Bitwise OR Operation

Thus, the correct match from LIST-I to LIST-II is: k = j | 100


Convert 100 to binary:
A (Exponentiation): 2 (Right to Left)

Android App | iOS App | PW Website

4/5
GATE

100 in binary is 01100100.


Q8 Text Solution:
Perform the bitwise OR operation between
8 (in binary: 00001000) and 100 (in binary: Calculate the Division
01100100): Divide i by j: -13.5 / 5 = -2.7.
In binary: 00001000 | 01100100 = Calculate the Floor of the Division
01101100 (binary) = 108 in decimal.
The floor of -2.7 is -3. This is the largest
4. Print the Result
integer less than or equal to -2.7.
The result of k is 228 in decimal.
Calculate the Product of the Floor Value and
Conclusion the Divisor
The output of the code segment is 228
Multiply the floor value by j: -3 * 5 =
Q7 Text Solution: -15.
a, d,
Calculate the Modulus
Octal Representation:
Subtract this product from i: -13.5 -
Decimal 335 in octal is 517. (-15) = -13.5 + 15 = 1.5.

Hexadecimal Representation: So, the modulus operation -13.5 % 5 yields


1.5.
Decimal 335 in hexadecimal is 0x14F.

Android App | iOS App | PW Website

5/5

You might also like