ASIAN INTERNATIONAL PVT SCHOOL, RUWAIS
COMPUTER SCIENCE (Grade -9)
CHAPTER 1: PYTHON RECAP
Answers of textbook exercises
Fill in the Blanks
1. H A S H is used for a single line comment in Python.
2. Python has F IVE standard data types.
3. Items in a L I S T are enclosed in square brackets.
4. L O O Pis used to execute the same block of code multiple times.
5. When a function calls itself , it is called Recursion.
State True or False
1. Comments are ignored by the Python interpreter. -True
2. Tuples are read only and cannot be updated. -True
3. The ‘elseif statement in Python allows to check multiple conditions
and execute a particular block of code. -True
4. The continue statement is used to exit a loop. -False
5. The return statement is used to exit a function. -True
Match the Following
1. Multiline comment Triple quotes
2. String Enclosed in quotes
3. Tuple A sequence of immutable values
4. break Exit loop
5. Function Reusable block of code
Multiple choice questions
1. Statement to exit a function is:
1. return
2. exit
3. pass
4. Continue
2. Which of the following is used for comments in python?
1. Hash(#)
2. Triple single quotes
3. Triple double quotes
4. All of the above
3. Which of the following functions returns a sequence of numbers,
starting from 0 by default, increments by 1 (by default), and ends at
a specified number?
1. range()
2. input()
3. for()
4. print()
4. ‘For’ loop can be used for:
1. Iterating through a list
2. Iterating through a dictionary
3. Repeating a block of code multiple times
4. All of the above
5. In a Python function, double asterisk(**) is used before:
1. Required argument
2. Variable length argument
3. Variable length keyword argument
4. Default argument
Answer in one word or one sentence
1. Write the syntax of the return statement.
return [expression list]
2. Write the syntax of the range function.
range ([start,] stop [, step])
Example :
# Numbers from 10 to 15
# start = 10
# stop = 50
# step = 5
for i in range(10, 50, 5):
print(i, end=' ')
# Output 10 15 20 25 30 35 40 45
3. What is recursion?
Recursion is a process when the function calls itself
4. What do you mean by required arguments?
Required arguments are defined as variables or parameters that the
function requires in order to execute. These variables or parameters are
specified in the function definition and must be passed as arguments
when the function is called.
5. What is a global variable?
Variables that are defined outside a function are known as global
variables.
Answer the following
1. Explain the different scopes of variables in a python program?
The scope of a variable determines the portion of the program
where you can access a particular identifier. There are two basic
scopes of variables in Python:
Global variables
Local variables
Variables that are defined inside a function body have a local scope,
and those defined outside have a global scope This means that local
variables can be accessed only inside the function in which they are
declared, whereas, global variables can be accessed throughout the
program body by all functions. When you call a function, the
variables declared inside it are brought into scope.
2. What is decision making? Explain with an example.
Decision making in Python is the process of using conditional
statements to make a decision based on a certain condition or set of
conditions. If statements are used for decision making in python.
Example:
age = int (input ("Enter your age: "))
if age>10:
print("I am older than 10")
else
print("I am less than 10 years old")
Here, the program evaluates the test expression and will execute
statement(s) only if the test expression is True. If the test expression is
False, the statement(s) is not executed and the “else” statement is
executed.
3. What is a loop? Explain with an example.
A loop is a piece of code which allows you to repeat a code more than
once, without writing it more than once. there are 2 types of loops in
python:
For loop:
‘For’ loop is generally used when you know how many times you have
to repeat a task. It is used more like an iterator. Iteration is a general term
for taking each item of something, one after another.
Syntax( for loop):
For variable in sequence_name:
Statements
. . .
Statements
Example 1 (for loop): a
n
Word =”anaconda”
a
For letter in word :
Print(letter)
c
o
n
Example 2 (for loop): d
Iterating through a list: a
memory list = ["RAM", "Hard Disk", "Virtual Memory"]
for x in memory list:
print (x)
While loop:
A ‘while loop’ statement in Python repeatedly executes a target
statement, as long as a given condition is true. We generally use this loop
when we don't know beforehand the number of times to iterate.
Syntax :
Example ( while loop ) output :
i=1 1
While (condition):
while i < 6: 2
Statement(s)
print(i) 3
i = i+1 4
5
4. Explain the different argument passing methods in python.
Required argument:
A required argument is an argument that must be passed to a
function when it is called. If a required argument is not passed, the
Python interpreter will raise an error. For example, in the function
add_numbers(a, b), both a and b are required arguments.
Keyword argument:
When you use keyword arguments in a function call, the caller
identifies the arguments by the parameter name. Python allows
functions to be called using keyword arguments. When we call
functions in this way, the order (position) of the arguments can be
changed.
Default argument:
A default argument is an argument that has a default value
assigned to it in the function signature. If a value for that argument
is not provided when the function is called, the default value is used
instead.
For example, in the function say_hello(name="world"), the default
value for the name argument is "world".
If no argument is passed when calling the function, the default
value is used.
Variable-length argument:
You may need to process a function for more arguments than you
specified, while defining the function. These arguments are called
variable-length arguments .
5. Explain Recursion with an example ?
A function that calls itself is a recursive function.
Advantages of Recursion
1. Recursive functions make the code look clean and elegant.
2. A complex task can be broken down into simpler sub-problems using recursion.
Example – To find the factorial of a number
def factorial(n):
if n==0:
return 1
else:
return n*factorial(n-1)
num = int(input("Enter a number: "))
print( factorial(num))
Extra question 6. Explain the structure of a function in python.
Function blocks begin with the keyword def followed by the
function name and parentheses.
Ex: def functionName(parameter list )
Any Input parameters (also called arguments) should be
placed within these parentheses.
You can also define parameters inside these parentheses.
The code block within every function starts with a colon(:) and
is indented.
The statement return [expression] exit a function and passes
back an expression to the calling program.
Syntax of a function:
def function name(parameters):
Statement 1
Statement 2
...
return [expression]
Above shown is a function definition which consists of the
following components.
1. Keyword def marks the start of the function header.
2. A function name to uniquely identify it.
3. Parameters through which we pass values to a function.
They are optional.
4. A colon () to mark the end of the function header.
5. One or more valid python statements that make up the
function body.
6. An optional return statement to return a value from the
function.
***************************************************************