Module 1 Important Questions With Answers
Module 1 Important Questions With Answers
2. With Python programming examples to each, explain the syntax and control
flow diagrams of break and continue statements.
4. How can you prevent a python program from crashing? Discuss different
ways to avoid crashing.
7. What are functions? Explain python function with parameters and return
statements.
Programs
3. Write a function named DivExp which takes TWO parameters a, b and returns
a value c (c=a/b). Write suitable assertion for a>0 in function DivExp and
raise an exception for when b=0. Dev p a Python program which reads two
values from the console and calls a function DivExp.
4. Develop a python program to calculate the area of circle and triangle print the
result.
1. What is a flow control statement? Discuss if and if else statements with
flow chart.
Ans: A program's control flow is the order in which the program's code executes.
The control flow of a Python program is regulated by conditional statements,
loops, and function calls.
if Statement
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.
if name == 'Alice’:
print('Hi, Alice.')
if else statement
The if-else statement is used to execute both the true part and the false part of a
given condition. If the condition is true, the if block code is executed and if the
condition is false, the else block code is executed
if name == 'Alice’:
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
else:
print('You are neither Alice nor a little kid.')
2. With Python programming examples to each, explain the syntax and control
flow diagrams of break and continue statements.
while True:
print('Please type your name.')
name=input()
if name == 'your name':
break
print('Thank you!’)
Continue statement:
Like break statements, continue statements are used inside loops. When the
program reaches a continue statement, the program execution immediately jumps
back to the start of the loop and reevaluates the loop’s condition.
execution
while True:
print('Who are you?’)
if name != 'Joe’:
continue
name = input()
print('Hello, Joe. What is the password? (It is a fish.)’)
password = input()
if password == 'swordfish’:
break
print('Access granted.')
import random
for i in range(5):
print(random.randint(1, 10))
When you run this program, the output will look something like this:
4
1
8
4
1
The random.randint() function call evaluates to a random integer value
between the two integers that you pass it. Since randint() is in the random
module, you must first type random. in front of the function name to tell
Python to look for this function inside the random module.
Here’s an example of an import statement that imports four different
modules:
4. How can you prevent a python program from crashing? Discuss different
ways to avoid crashing.
Exception Handling
Right now, getting an error, or exception, in your Python program means the
entire program will crash. You don’t want this to happen in real-world
programs. Instead, you want the program to detect errors, handle them, and
then continue to run.
For example, consider the following program, which has a “divide-byzero”
error.
def spam(divideBy):
return 42 / divideBy
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
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.
You 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.
def spam(divideBy):
try:
return 42 / divideBy
except ZeroDivisionError:
print('Error: Invalid argument.')
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
5. Explain FOUR scope rules of variables in Python.
The reason Python has different scopes instead of just making everything a
global variable is so that when variables are modified by the code in a
particular call to a function, the function interacts with the rest of the program
only through its parameters and the return value.
Parameters and 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 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
Local scope
def myfunc():
x = 300 ----- Local scope
print(x)
myfunc()
def myfunc():
print(x)
myfunc()
print(x
def hello(name):
print('Hello ' + name)
hello('Alice')
hello('Bob')
String Concatenation
When + is used on two string values, it joins the strings as the string
concatenation operator.
String Replication
The * operator is used for multiplication when it operates on two integer or
floating-point values. But when the * operator is used on one string value and
one integer value, it becomes the string replication operator.
>>> 'Alice' * 5
'AliceAliceAliceAliceAlice'