Mod 1 Solutions
Mod 1 Solutions
MODULE 1 SOLUTION
1. Explain elif, for, while statements in Python with examples for each.
a) elif
While only one of the if or else clauses will execute, we may have a case where we want
one of many possible clauses to execute.
It provides another condition that is checked only if all of the previous conditions were
False.
In code, an elif statement always consists of the following:
• The elif keyword
• A condition (that is, an expression that evaluates to True or False)
• A colon
• Starting on the next line, an indented block of code (called the elif clause)
b) for
If we want to execute a block of code only a certain number of times then we can do this with
a for loop statement and the range() function.
2. A variable name
3. The in keyword
5. A colon
6. Starting on the next line, an indented block of code (called the for clause)
c) while loop
We can make a block of code execute over and over again with a while statement
➢ The code in a while clause will be executed as long as the while statement‘s condition is True.
3. A colon
4. Starting on the next line, an indented block of code (called the while clause)
Ex:
spam = 0
print('Hello, world.')
spam = spam + 1
Variables that are assigned in a called function are said to exist in that
function’s “local scope”.
Variables that are assigned outside all functions are said to exist in the
“global scope”. A variable must be one or the other; it cannot be both local
and global.
When a scope is destroyed, all the values stored in the scope’s variables are
forgotten.
There is only one global scope, and it is created when your program begins.
When your program terminates, the global scope is destroyed, and all its
variables are forgotten.
A local scope is created whenever a function is called. Any variables
assigned in this function exist within the local scope. When the function
returns, the local scope is destroyed, and these variables are forgotten.
Scopes matter for several reasons:
1. Code in the global scope cannot use any local variables.
2. However, a local scope can access global variables.
3. Code in a function’s local scope cannot use variables in any other
local scope.
4. We can use the same name for different variables if they are in
different scopes. That is, there can be a local variable named spam
and a global variable also named spam.
The global Statement If you need to modify a global variable from within a function, use the
global statement. If you have a line such as global eggs at the top of a function, it tells Python,
“In this function, eggs refer to the global variable, so don’t create a local variable with this
name.”
Example
def spam():
global eggs
eggs = 'spam'
spam()
print(eggs) When you run this program, the final print() call will output this: spam
The print() function displays the string value inside the parentheses on
the screen or any other standard output device.
The line print('Hello world!')` means Print out the text in the string
`'Hello world!'`. When Python executes this line, you say that Python is
calling the `print()` function and the string value is being passed to the
function.
Ex:
print ("Hello, Python!")
print () # This prints a blank line
ii. input()
Example:
iii. len()
print(len("together")) # Output: 7
print(len("Python")) # Output: 6
print(len("Hello world")) # Output: 11 (includes the space)
print(len(" ")) # Output: 1 (just the space)
5. What are Functions? Explain python functions with parameter and return statement
Function is a block of code that only runs when called. It is like a mini-program. The
idea is to use the same block of code repeatedly during programming in terms of
function whenever required.
When you call the len () function and pass it an argument such as 'Hello', the function call
evaluates to the integer value 5, which is the length of the string you passed it.
In general, the value that a function call evaluates to is called the return value of the
function. When creating a function using the def statement, you can specify what the
return value should be with a return statement.
A return statement consists of the following:
Example: Output:
def square(num): 16
result = square(4)
print(result)
Program
Output
21.0
3.5
None
42.0
7. Define a Comparison Operator and List its Types. Give the difference between = = and
= operator
a)
The comparison operators are also called relational operators. Comparison
operators compare two values and evaluate them down to a single
Boolean value. The table below lists the comparison operators
b)
Expression
3 / 2 * 4 + 3 + (10 / 4) ** 3 - 2
String concatenation:
HelloWorld
String replication:
Replicating the same string “n” several times is called string replication.
Use the * operator on a string to repeat the string a provided number of
times.
The syntax for the operation is: result = ‘string’ * integer number
>> ‘Alice’ * 3
AliceAliceAlice
10. List and Explain with examples Different types of Comparison and Boolean
Operator
a)
The comparison operators are also called relational operators. Comparison
operators compare two values and evaluate them down to a single
Boolean value. The table below lists the comparison operators
b) Boolean Operator
and
or
not
These logical operators are used to compare Boolean values, as shown below:
and operator: The and operator evaluates an expression to True if both Boolean values are
True; otherwise, it evaluates to False.
or operator: The or operator valuates an expression to True if either of the two Boolean values
is True. If both are False, it evaluates to False
The not operator operates on only one Boolean value (or expression). The not operator simply
evaluates to the opposite Boolean value
11. What are user defined functions? How can we pass the arguments to the functions?
Explain with suitable example
➢ The program execution enters the function, and the variable name is automatically set to
'Alice', which is what gets printed by the print() statement
➢ One special thing to note about parameters is that the value stored in a parameter is
forgotten when the function returns
Program Output
2. A colon
b) Break
Break in Python is a loop control statement. It is used to control the
sequence of the loop.
The break keyword is used to break out a for loop, or a while loop.
If the execution reaches a break statement, it immediately exits the while
loop’s clause.
n code, a break statement simply contains the break keyword.
Example: for break statement
c) Continue
The continue keyword is used to end the current iteration in a for
loop (or a while loop) and continues to the next iteration. It is used inside
the loop.
When the program execution reaches a continue statement, the program
execution immediately jumps back to the start of the loop and reevaluates
the loop’s condition.
Program
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue
print('Hello, Joe. What is the password? (It is a fish.)')
password = input()
if password == 'swordfish':
break
print('Access granted.')