Define exception handling. How exceptions are handled in python?
Write a program to solve divide by zero
exception
Ans)
Exception Handling
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.
For example,
• A ZeroDivisionError happens whenever we try to divide a number by zero. From the line number given in the
error message, we know that the return statement in spam() is causing an error.
• Errors can be handled with try and except statements.
• The code that could potentially have an error is put in a try clause. The program execution moves to the start
of a following except clause if an error happens.
• We can put the previous divide-by-zero code in a try clause and have an except clause contain code to
handle what happens when this error occurs.
• Note that any errors that occur in function calls in a try block will also be caught. Consider the following
program, which instead has the spam() calls in the try block:
• The reason print(spam(1)) is never executed is because once the execution jumps to the code in the except
clause, it does not return to the try clause. Instead, it just continues moving down as normal.
Define a Python function with suitable parameters to generate prime numbers between two integer values. Write a
Python program which accepts two integer values m and n (note: m>0, n>0 and m < n) as inputs and pass these values
to the function. Suitable error messages should be displayed if the conditions for input values are not followed.
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?
Local and Global Scope
• Parameters and variables that are assigned in a called function are said to exist in that function’s local scope
and is called a local variable
Variables that are assigned outside all functions are said to exist in the global scope and is called a global
variable.
• 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:
Consider this program, which will cause an error when you run it:
• This happens because “eggs” variable is destroyed when the function ends.
2. However, a local scope can access global variables:
Consider the following program:
• Since there is no eggs variable in spam function it assigns to global variable automatically
3. Code in a function’s local scope cannot use variables in any other local scope.
Consider the following program:
• It gives a name error as local variable of spam cannot be accessed inside bacon function
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 we need to modify a global variable from within a function, use the global statement.
• If we have a line such as global eggs at the top of a function, it tells Python, “In this function, eggs refers
to the global variable, so don’t create a local variable with this name.”
For example:
• Since global statement is used the value in “eggs” is overridden.
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. Below table lists the
comparison operators.
Examples:
Boolean Operators
• The three Boolean operators (and, or, and not) are used to compare Boolean values.
• “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.
“Not” operator: The not operator operates on only one Boolean value (or expression). The not operator simply
evaluates to the opposite Boolean value.
List and explain the syntax of all flow control statements with example.
if Statements:
The most common type of flow control statement is the 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
In Python, an if statement consists of the following:
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: Flow Chart:
else Statements:
An if clause can optionally be followed by an else statement. The else clause is executed only when the if statement’s
condition is False.
An else statement doesn’t have a condition, and in code, an else statement always consists of the
following:
1. The else keyword
2. A colon
3. Starting on the next line, an indented block of code (called the else clause)
Example: Flow Chart:
elif Statements:
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.
The elif statement is an “else if” statement that always follows an if or another elif statement.
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:
1. The elif 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 elif clause)
Example:
Flow Chart:
while loop Statements:
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.
In code, a while statement always consists of the following:
1. The while 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 while clause)
Example:
Flow Chart:
break Statements:
If the execution reaches a break statement, it immediately exits the while loop’s clause.
In code, a break statement simply contains the break keyword.
Example:
Flow chart:
Continue statements
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.
Example and Output
for loops and the range() function:
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
6. Starting on the next line, an indented block of code (called the for clause)
Example and Output:
The 3 integers passed to range function are (Initial_value, final_value, Step length)
Illustrate the use of break and continue with a code snippet
break Statements:
If the execution reaches a break statement, it immediately exits the while loop’s clause.
In code, a break statement simply contains the break keyword.
Example:
Flow chart:
Continue statements
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.
Example and Output
What are user defined functions? How can we pass parameters in user defined functions? Explain with suitable
example.
A function a block of code which is used to perform a specific task.
Example:
We can also define our own functions that accept arguments.
Example with output:
The value that a function call evaluates to is called the return value of the function.
Ex: len(‘Hello’) -->Return values is 5
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.
Explain global statement with example.
The Global Statement
• If we need to modify a global variable from within a function, use the global statement.
• If we have a line such as global eggs at the top of a function, it tells Python, “In this function, eggs refers
to the global variable, so don’t create a local variable with this name.”
For example:
• Since global statement is used the value in “eggs” is overridden.
What is the need for role of precedence? Illustrate the rules of precedence in python with example.
The order of operations (also called precedence) of Python math operators is similar to that of mathematics.
The ** operator is evaluated first; the *, /, //, and % operators are evaluated next, from left to right; and
the + and - operators are evaluated last (also from left to right). We can use parentheses to override the
usual precedence if you need to.
Example: