Mod1 Answers Python
Mod1 Answers Python
a. 2 * (3-1) + 3 * 3 ** 3
b. (1+1)**(5-2)
c. 16 - 2 * 5 // 3 + 1
d. (5 - 1) * ((7 + 1) / (3 - 1))
Ans:
Qu2 Explain if, if-else, elif, for, while, break and continue statements in Python with examples of
each .
➢ Flow control statements often start with a part called the condition, and all are
followedby a block of code called the clause.
If statement
➢ An if statement‘s clause (that is, the block following the if statement) will execute if
the statement‘s condition is True. The clause is skipped if the condition is False.
1. The if keyword
2. A condition (that is, an expression that evaluates to True or False)
3. A colon
4. Starting on the next line, an indented block of code (called the if clause)
➢ Example:
2. else Statements:
➢ An if clause can optionally be followed by an else statement. The else clause isexecuted
only when the if statement‘s condition is False.
➢ An else statement doesn‘t have a condition, and in code, an else statement alwaysconsists
of the following:
➢ Example:
3. elif Statements:
➢ While only one of the if or else clauses will execute, we may have a case where wewant one
of many possible clauses to execute.
➢ 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 conditionis
True.
➢ In the while loop, the condition is always checked at the start of each iteration (that is,
each time the loop is executed).
➢ If the condition is True, then the clause is executed, and afterward, the condition is
checked again.
➢ The first time the condition is found to be False, the while clause is skipped.
1. break Statements:
➢ There is a shortcut to getting the program execution to break out of a while loop‘s
clause early.
➢ If the execution reaches a break statement, it immediately exits the while loop‘s
clause
Continue statement
Like break statements, continue statements are used inside loops.
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.
➢ 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.
➢ In code, a for statement looks something like for i in range(5): and always includes
the following:
1. The for keyword
2. A variable name
3. The in keyword
4. A call to the range() method with up to three integers passed to it
5. A colon
Starting on the next line, an indented block of code (called the for clause)
Qu3. Explain print (), input (), len (), str (), int (), float () and range() functions with examples
➢ The input() function waits for the user to type some text on the keyboard and press
ENTER
len() Function
We can pass the len() function a string value (or a variable containing a string), and the
function evaluates to the integer value of the number of characters in that string.
➢ The str(), int(), and float() functions will evaluate to the string, integer, and
floating- point forms of the value you pass, respectively.
Qu 4.Explain Local and Global Scope in Python programs. What are local and global
variables? How can you force a variable in a function to refer to the global variable.
Parameters and variables that are assigned in a called function are said to exist in that function‘s
local scope.
Parameters and variables that are assigned outside function are said to exist in global scope.
A variable that exists in a local scope is called a local variable, while a variable that
exists in the global scope is called a global variable.
A variable must be one or the other; it cannot be both local and global.
Qu5 What are Comparison and Boolean operators? List all the Comparison and
Boolean operators in Python and explain the use of these operators with suitable
examples.
Comparison Operators
➢ Comparison operators compare two values and evaluate down to a single Boolean
value. Table 2-1 lists the comparison operators.
Boolean Operators
➢ The three Boolean operators (and, or, and not) are used to compare Boolean values.
not operator: The not operator operates on only one Boolean value (or expression). The
not operator simply evaluates to the opposite Boolean value.
Qu6 What is Exception Handling? How exceptions are handled in Python? Write a
Python program with exception handling code to solve divide-by-zero error situation.
If we don‘t want to crash the program due to errors instead we want the program to detect
errors, handle them, and then continue to run.
Errors can be handled with try and except statements.
The code that could potentially have an error is put in a try clause. Program execution
moves to the start of a following except clause if an error happens.
A ZeroDivisionError in Python occurs when we try to divide a number by 0. We can’t divide a number by 0 otherwise it will raise
an error
We have used an exception handler here that sets the result to 0 if such an error occurs, and it then prints the result, which will
be 0
Python3
a = 16.0
b = 0
try:
result = a / b
except ZeroDivisionError:
result = 0
print(result)
Output
0
Qu7/10/11 What are functions? Explain Python function with parameters and return
statements.
➢ Example:
➢ The first line is a def statement ❶, which defines a function named hello().
➢ The code in the block that follows the def statement ❷ is the body of the function. This
code is executed when the function is called, not when the function is first defined.
➢ When we call the print() or len() function, we pass in values, called arguments
in thiscontext, by typing them between the parentheses.
➢ The value that a function call evaluates to is called the return value of the function.
➢ When creating a function using the def statement, we can specify what the return
value should be with a return statement.
➢ A return statement consists of the following:
1. The return keyword
2. The value or expression that the function should return
Qu8 Discuss various methods of importing modules into application in Python with syntax and
suitable programming examples.
➢ All Python programs can call a basic set of functions called built-in functions,
including the print(), input(), and len() functions.
➢ Python also comes with a set of modules called the standard library.
So there's four different ways to import:
1. Import the whole module using its original name: import random
2. Import specific things from the module: from random import choice, randint
3. Import the whole module and rename it, usually using a shorter variable
name: import pandas as pd
4. Import specific things from the module and rename them as you're importing
them: from os.path import join as join_path
Qu9 Explain how to terminate program early with sys.exit() function with example.
➢ The last flow control concept is how to terminate the program. This always
happens ifthe program execution reaches the bottom of the instructions.
➢ However, we can cause the program to terminate, or exit, by calling the sys.exit()
function. Since this function is in the sys module, we have to import sys before
your program can use it.
import sys
age = 17
else:
Qu12 Explain the different keyword arguments passed to the print() function.
➢ keyword arguments are identified by the keyword put before them in the function
call.
➢ Keyword arguments are often used for optional parameters.
➢ For example, the print() function has the optional parameters end and sep to
specify what should be printed at the end of its arguments and between its
arguments (separating them), respectively.
Qu. 13 List the rules to declare a variable in Python. Demonstrate atleast three different types of
variables uses with an example program.
We can name a variable anything as long as it obeys the following three rules:
2. It can use only letters, numbers, and the underscore (_) character.
# An integer assignment
age = 45
# A floating point
salary = 1456.8
# A string
name = "John"
print(age)
print(salary)
print(name)