Professional Documents
Culture Documents
Answer Key
Answer Key
Q6. What will be the output on executing the following set of commands in the shell?
>>>str= "Hello"
>>>str[:2]
(a) He
Q7. In order to store values in terms of key and value pairs, which core data type you
should use?
(d) Dictionary
Q8. Which of the following data type is not supported in Python?
(d) Slice
b = 30
print(a%b)
(c) 10
These answers correspond to the options provided in each multiple-choice question. Each
correct answer is marked with its respective letter (a, b, c, or d).
Ans12. The operators that are used to compare two operands are called relational
operators.
Ans18. Python variables have two different scopes global and local.
- Variables in Python are used to store data values. They are created when you assign a value
to them using the assignment operator (`=`).
- Proper indentation is important in Python because it defines the structure and hierarchy
of the code. Incorrect indentation can lead to syntax errors or alter the logical flow of the
program.
OR
- Input in Python refers to the process of accepting data or information from the user or
external sources. Output in Python refers to the process of displaying results, messages, or
data to the user or external destinations.
Q26. What is operator precedence in Python? Explain with the help of example.
Operator precedence in Python determines the order in which operations are evaluated in
an expression. Operators with higher precedence are evaluated first. For example,
multiplication (`*`) has a higher precedence than addition (`+`).
Example:
result = 10 + 2 * 3
print(result) # Output: 16
Explanation:
# Multiplication (2 * 3) is evaluated first due to higher precedence,
# then addition (10 + 6) is performed.
Q27. What are the features of Python that make it suitable for diverse applications?
Q28. Explain the difference between = and == operators in Python with examples.
a=5
b=5
if a == b:
print("a and b are equal") # This condition is true, so this line will be printed
"""
This is a multi-line comment.
It can span multiple lines.
"""
Type conversion (or type casting) refers to the process of converting one data type to
another in Python. It can be implicit (automatically handled by Python) or explicit (done by
the programmer using built-in functions).
OR
Q29. How are keywords different from variable names?
Keywords in Python are reserved words that have special meanings and purposes within the
language (e.g., `if`, `else`, `for`, `while`, `def`). They cannot be used as variable names.
Variable names, on the other hand, are identifiers used to store values or data in memory.
They must follow certain rules (e.g., start with a letter or underscore, not start with a number).
Q30. Write a Python program to find the perimeter of a rectangle by taking user input (Hint:
Use input() function for taking value).
# Calculate perimeter
perimeter = 2 * (length + width)
Q31.Explain the role and usage of bitwise operators in Python. Provide scenarios where
bitwise operators are commonly used in programming?
Bitwise operators in Python (`&`, `|`, `^`, `~`, `<<`, `>>`) manipulate bits of integers at the
binary level. They are used in scenarios such as:
- Bitwise AND (`&`): Used for bit masking and checking specific bits.
- Bitwise OR (`|`): Used for setting bits or combining flags.
- Bitwise XOR (`^`): Used for flipping bits.
Example:
# Example of bitwise operators
a = 5 # Binary: 0101
b = 3 # Binary: 0011
String operators in Python primarily involve concatenation (`+`) and repetition (`*`).
- String Concatenation (`+`): Joins two or more strings together.
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: "Hello World"
String operators are fundamental for manipulating textual data in Python, such as
combining strings, formatting output, or constructing messages dynamically.
- Arithmetic operators** (`+`, `-`, `*`, `/`, `//`, `%`, `**`): Used for performing mathematical
calculations on numerical values.
a = 10
b=3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333...
print(a // b) # Output: 3 (Floor division)
print(a % b) # Output: 1 (Modulus)
print(a ** b) # Output: 1000 (Exponentiation)
- Logical operators (`and`, `or`, `not`): Used for combining conditional statements.
x = True
y = False
Arithmetic operators work with numerical data to perform calculations, while logical
operators work with boolean values to evaluate conditions and make decisions in the
program.
Q34.Discuss the operator precedence rules in Python. Explain how operator precedence
affects the evaluation of expressions with examples.
Operator precedence in Python determines the order in which operators are evaluated in
an expression. Operators with higher precedence are evaluated first. For example:
- Multiplication (`*`), division (`/`), and modulus (`%`) have higher precedence than
addition (`+`) and subtraction (`-`).
- Operators within parentheses `()` are evaluated first.
Example:
result = 10 + 2 * 3
print(result) # Output: 16
# Explanation:
# Multiplication (2 * 3) is evaluated first due to higher precedence,
# then addition (10 + 6) is performed.
Understanding operator precedence is crucial for writing correct and efficient Python
code, as it affects how expressions are evaluated and can alter the result if not used
correctly.
OR
Q33. Write a Python program that asks the user to enter the radius of a circle and calculates
its area?
import math
# Calculate area
area = math.pi * (radius ** 2)
Q34. Suppose a person wants to do an addition but doesn’t know about operators and
operands in Python. They take a number 30 and a string "world". Justify your answer as to
what is wrong with adding 30 and the string "world".
In Python (and in most programming languages), addition (`+`) is defined for adding
numerical values together or concatenating strings. Mixing different data types like a
number (`30` as an integer) and a string (`"world"`), without explicit conversion, would
result in a `TypeError`. This is because Python does not implicitly convert types when
performing operations that expect homogeneous types.
Example:
num = 30
string = "world"
To perform the addition correctly, either convert the number to a string or vice versa,
depending on the desired outcome:
num = 30
string = "world"