Review of Python - Exercise - 10
Programming in Python - Exercise Solutions
1. Multiple Choice Questions
| Question | Correct Answer |
|---|---|
| a. Python is developed by | iii. Guido Van Rossum |
| b. Python is .............based programming language | ii. Interpreter |
| c. Symbol used for single line comment in Python | iv. # |
| d. Valid variable name in Python | i. num1 |
| e. Correct way to assign value to variable | ii. x=y=z=3 |
| f. Conditional statement | ii. if |
| g. Loop used when number of executions is already known | ii. for |
| h. Loop used when number of iterations is not known | i. while |
| i. Statement used with for loop | ii. range |
| j. Function used to add data at the end of a list | ii. append |
| k. Data type that stores data in key-value pairs | ii. Dictionary |
| l. Function used to convert characters into uppercase | iii. upper |
| m. Function used to count the number of characters in a word | iv. len |
2. Technical Terms
| Phrase | Technical Term |
|---|---|
| The name given to memory location used to store value. | Variable |
| The set of rules that should be followed by programmers while writing programs. | Syntax |
| The words which have their own specific function in the program. | Keywords |
| The signs or symbols which perform specific operations on constants and variables. | Operators |
| The combination of variables, constants and operators. | Expression |
3. Short Answer Questions
a. Define Python programming language. Write any two characteristics.
Python is a high-level, interpreted and general-purpose programming language developed by Guido Van Rossum. It is widely used for software development, web development, data analysis and artificial intelligence.
Characteristics of Python:- Easy to learn and use.
- Platform independent.
b. Write the function of input() and print() statements.
input() function is used to receive data from the user through the keyboard.
print() function is used to display output on the screen.
Example:
name = input("Enter your name:")
print(name)
c. What is data type? List any two data types used in Python.
A data type specifies the type of value that a variable can store.
Examples of data types:- int (Integer)
- str (String)
d. What is variable?
A variable is a named memory location used to store data or values that can be changed during program execution.
Example:
age = 15
e. What is looping? List the types of looping statements in Python.
Looping is the process of executing a set of statements repeatedly until a specified condition is met.
Types of looping statements:- for loop
- while loop
f. What is list? Write any two characteristics of list.
A list is a collection of multiple items stored in a single variable. Lists are enclosed within square brackets [ ].
Characteristics of List:- Lists are ordered.
- Lists are mutable (can be modified).
fruits = ["Apple", "Mango", "Orange"]
g. What is dictionary? Write any two characteristics of dictionary.
A dictionary is a collection of data stored in key-value pairs. Dictionaries are enclosed within curly brackets { }.
Characteristics of Dictionary:- Stores data in key-value pairs.
- Keys must be unique.
student = {
"name":"Ram",
"age":15
}
h. What is string function? Write any two examples.
String functions are built-in functions used to perform operations on string data.
Examples:- upper()
- lower()
name = "python"
print(name.upper())
print(name.lower())
4. Write the output of the following program.
i. s=0
for n in range(11):
if n%2!=0:
s=s+n
print(s)
Dry Run:
| n | Condition (n%2!=0) | s |
|---|---|---|
| 1 | True | 1 |
| 3 | True | 4 |
| 5 | True | 9 |
| 7 | True | 16 |
| 9 | True | 25 |
Output:
25
ii. n=8
for i in range(1,n+1):
if n%i==0:
print(i)
Dry Run:
| i | n%i==0 | Output |
|---|---|---|
| 1 | True | 1 |
| 2 | True | 2 |
| 3 | False | - |
| 4 | True | 4 |
| 5 | False | - |
| 6 | False | - |
| 7 | False | - |
| 8 | True | 8 |
Output:
1
2
4
8
iii. n=783
s=0
while n!=0:
r=n%10
s=s*10+r
n=n//10
print(f"The result is {s}")
Dry Run:
| n | r=n%10 | s=s*10+r |
|---|---|---|
| 783 | 3 | 3 |
| 78 | 8 | 38 |
| 7 | 7 | 387 |
Output:
The result is 387
iv. p=1
n=5
for i in range(1,n+1):
p=p*i
print(f"The result is {p}")
Dry Run:
| i | p |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 24 |
| 5 | 120 |
Output:
The result is 120
v. n=7
for i in range(1,6):
print(n)
if n%2==0:
n=n/2
else:
n=n*3+1
Dry Run:
| Iteration | Printed Value | New Value of n |
|---|---|---|
| 1 | 7 | 22 |
| 2 | 22 | 11 |
| 3 | 11 | 34 |
| 4 | 34 | 17 |
| 5 | 17 | 52 |
Output:
7
22
11
34
17
vi. x="Python"
y="Programming"
a=len(x)
b=len(y)
if a>b:
print(x)
else:
print(y)
Dry Run:
a = 6
b = 11
6 > 11 → False
else block executes
Output:
Programming
vii. z="python"
t=""
for i in z:
t=i+t
print(t)
Dry Run:
| i | t=i+t |
|---|---|
| p | p |
| y | yp |
| t | typ |
| h | htyp |
| o | ohtyp |
| n | nohtyp |
Output:
nohtyp
5. Long Answer Questions
a. What is an operator? Explain the different types of operators with examples.
An operator is a symbol that performs specific operations on variables and constants to produce a result.
Types of Operators in Python:
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Purpose | Example |
|---|---|---|
| + | Addition | 5+2 = 7 |
| - | Subtraction | 5-2 = 3 |
| * | Multiplication | 5*2 = 10 |
| / | Division | 5/2 = 2.5 |
| % | Modulus | 5%2 = 1 |
2. Relational (Comparison) Operators
These operators compare two values and return either True or False.
a=10
b=5
print(a>b)
Output:
True
3. Logical Operators
Logical operators combine multiple conditions.
a=10
b=5
print(a>5 and b<10)
Output:
True
4. Assignment Operators
Assignment operators are used to assign values to variables.
x=10
y=5
Here, "=" is an assignment operator.
b. What is a conditional statement? Explain the different types of conditional statements.
A conditional statement is used to make decisions in a program based on a condition. It executes different blocks of code depending on whether the condition is True or False.
Types of Conditional Statements:
1. if Statement
Executes a block of code only when the condition is true.
age=18
if age>=18:
print("Eligible to vote")
2. if...else Statement
Executes one block when the condition is true and another block when it is false.
num=5
if num%2==0:
print("Even")
else:
print("Odd")
3. if...elif...else Statement
Used when multiple conditions need to be checked.
marks=75
if marks>=80:
print("Distinction")
elif marks>=60:
print("First Division")
else:
print("Second Division")
c. Differentiate between for loop and while loop.
| For Loop | While Loop |
|---|---|
| Used when the number of iterations is known. | Used when the number of iterations is unknown. |
| Works with a sequence or range. | Works based on a condition. |
| Initialization and increment are handled automatically. | Initialization and increment must be managed manually. |
| Generally simpler to use. | More flexible for complex conditions. |
d. Explain any three string functions with examples.
String functions are built-in functions used to perform operations on strings.
1. upper()
Converts all characters of a string into uppercase letters.
name="python"
print(name.upper())
Output:
PYTHON
2. lower()
Converts all characters of a string into lowercase letters.
name="PYTHON"
print(name.lower())
Output:
python
3. len()
Returns the total number of characters in a string.
name="Python"
print(len(name))
Output:
6