Python Unit-1: Important 2-Marks and 7-Marks Questions with Answers
Q1. Write the benefits of Python.
- Easy to learn and use
- Interpreted language
- Large standard library
- Portable and platform-independent
- Supports object-oriented and functional programming
Q2. Define floor division with example.
Floor division (//) gives the largest integer less than or equal to the result.
Example: 7 // 2 = 3
Q3. What type of language is Python?
Python is a high-level, interpreted, dynamically typed, and general-purpose programming language.
Q4. How is Python an interpreted language?
Python code is executed line-by-line by the Python interpreter without compiling into machine code.
Q5. Define the type() function.
The type() function returns the data type of the object.
Example: type(10) returns <class 'int'>
Q6. What will be the output of the following code?
i=0
while i < 3:
print(i)
i += 1
else:
print(0)
Output:
0
1
2
0
Python Unit-1: Important 2-Marks and 7-Marks Questions with Answers
Q7. What happens if you put a semicolon at the end of a Python statement?
It does nothing harmful. Statements can end with a semicolon, but it's not required in Python.
Q8. What are rules for naming variables in Python?
- Start with a letter or underscore
- Can have letters, digits, and underscores
- Cannot start with a digit
- Case-sensitive
- Cannot use Python keywords
Q9. Explain the role of precedence with an example.
Precedence defines which operator is evaluated first.
Example: 2 + 3 * 4 = 14 (multiplication is done before addition).
Q10. List the categories of operators.
- Arithmetic
- Relational
- Logical
- Assignment
- Bitwise
- Membership
- Identity
Q1. What do you mean by Data type in Python?
Data type defines what kind of data a variable holds.
Types:
- int, float, complex
- str
- bool
- list, tuple, range
- set, frozenset
- dict
- None
Q2. Discuss various categories of operators in Python.
Python Unit-1: Important 2-Marks and 7-Marks Questions with Answers
- Arithmetic: +, -, *, /, //, %, **
- Relational: ==, !=, >, <, >=, <=
- Logical: and, or, not
- Assignment: =, +=, -=
- Bitwise: &, |, ^, ~
- Membership: in, not in
- Identity: is, is not
Q3. Explain the local and global variables in Python.
Local: Declared inside function, used only there
Global: Declared outside all functions, used anywhere
Use 'global' keyword to modify global inside function
Q4. Write a Python program to swap two numbers.
# Using third variable
a, b = 5, 10
temp = a
a=b
b = temp
print(a, b)
# Without third
a, b = a + b, a - b
a=a-b
# Pythonic way: a, b = b, a
Q5. What do you mean by operator precedence and associativity?
Precedence defines which operator executes first.
Associativity defines the direction (left or right) when same precedence.
Example: 2 ** 3 ** 2 = 2 ** (3 ** 2) = 512
Q6. Write a program to check leap year.
year = int(input())
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print('Leap year')
else:
Python Unit-1: Important 2-Marks and 7-Marks Questions with Answers
print('Not leap year')
Q7. Write a program to find sum of natural numbers.
n = int(input())
sum = 0
for i in range(1, n+1): sum += i
print(sum)
Q1. What is dynamic typing?
Dynamic typing means variable type is decided at runtime.
x=5
x = 'hello'
Advantages: Easy to use
Disadvantages: Runtime errors possible
Q2. Write program to swap values.
a = 5; b = 10
# Using third: temp = a; a = b; b = temp
# Arithmetic: a = a + b; b = a - b; a = a - b
# Pythonic: a, b = b, a
Q3. What is type conversion?
Changing data type of a value.
Implicit: Python handles automatically
Explicit: Manually using int(), float(), str() etc.
Q4. What is data type? List types in Python.
Data type defines value type.
Types: int, float, complex, str, bool, list, tuple, set, dict, None
Q5(i). Solve a & b << 2 // 5 ** 2 + c ^ b (a=3,b=5,c=10)
Step-by-step:
5 << 2 = 20
20 // 25 = 0
Python Unit-1: Important 2-Marks and 7-Marks Questions with Answers
3&0=0
0 + 10 = 10
10 ^ 5 = 15
Answer: 15
Q5(ii). Solve a**2 << 2 >> b**2 ^ c**3 (a=3,b=5,c=10)
a**2 = 9
9 << 2 = 36
b**2 = 25
36 >> 25 = 0
c**3 = 1000
0 ^ 1000 = 1000
Answer: 1000