Python Theory Question and Answer 1
Python Theory Question and Answer 1
2 Mark question:-
a) What is a multiline statement?
A multiline statement in Python is a statement that spans multiple
lines. You can create one using the backslash (\) at the end of a line
or by enclosing the statement in parentheses (), brackets [], or braces
{}.
h) Define Algorithm.
An algorithm is a step-by-step procedure or set of rules designed to
solve a specific problem or perform a task.
4 or 5 mark question:-
a) Explain the operators in Python:
Bitwise Operators
These operators work on the binary representation of integers:
& (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift)
Example:
python
a=5 # 0101
b=3 # 0011
print(a & b) # Output: 1
Logical Operators
Used to combine conditional statements:
and, or, not
Example:
python
x = True
y = False
print(x and y) # Output: False
Membership Operators
Used to check if a value is a member of a sequence (like list, string,
etc.):
in, not in
Example:
python
x = [1, 2, 3]
print(2 in x) # Output: True
Identity Operators
Used to compare the memory location of two objects:
is, is not
Example:
python
a = [1, 2]
b=a
print(a is b) # Output: True
b) What is the difference between list and tuple in Python (with
example)?
Feature List Tuple
Mutable (can be Immutable (cannot be
Mutability
changed) changed)
Syntax [] ()
Performance Slightly slower Faster than lists
When items need to
Use case When fixed data is needed
change
Example:
python
my_list = [1, 2, 3]
my_list[0] = 10 # Allowed
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # Not allowed (will raise an error)
# Perimeter
perimeter = a + b + c
# Semi-perimeter
s = perimeter / 2
10 mark question:-
OR
b)
i) Python Programs
a) Program to Find the Sum of n Natural Numbers
n = int(input("Enter a number: "))
sum_n = n * (n + 1) // 2
print("Sum of first", n, "natural numbers is:", sum_n)
b) Program to Convert Celsius to Fahrenheit
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)