You are on page 1of 3

1.With Python programming examples to each 3.What is the need of role of precedence?

Illustrate the rules of


explain the syntax and control flow diagrams of? precedence in python with .
break Ans: Precedence is the priority for grouping different types of
while True: operators with their operands. Associativity is the left-to-right or
print('Please type your name.') right-to-left order for grouping operands to operators that have
name = input() the same precedence.
if name == 'your name': Operator operation example evaluates
break ** Exponent 2 ** 3 8
print('Thank you!') % Modulus/remainder 22 % 8 6
// Integer division 22 // 8 2
/ Division 22 / 8 2.75
continue Statements: * Multiplication 3*5 15
while True: - Subtraction 5-2 3
print('Who are you?') + Addition 2+2 4
name = input() 4. Explain local scope and global scope with example
u if name != 'Joe': Local Scope:A variable created inside a function belongs to the
continue local scope of that function. It is only accessible from the point at
print('Hello, Joe. What is the password? (It is a which it is defined until the end of the function.
fish.)') def my_function():
password = input() a_local_variable = 30
if password == 'swordfish': print(a_local_variable)
break Global Scope:A variable defined outside of all functions has a
print('Access granted.') global scope, meaning it can be accessed anywhere in your code,
including inside functions
global_number = 10

def print_global():
print(“Global number inside print_global:”global_number)

def modify_global():
global global_number
global_number = 20
print("Global number inside modify_global:", global_number)

def use_local():
global_number = 30
2. Write a Python program to check whether the given print("Local 'global_number' inside use_local:", global_number)
number is positive, negative or zero
def check_number(num): print_global() # Should print 10
if num > 0: modify_global() # Modifies the global variable and prints 20
print("The number is positive.") print_global() # Should now print 20, reflecting the modification
elif num < 0: use_local() # Demonstrates that this does not affect the global
print("The number is negative.") variable, prints 30
else: print_global() # Still prints 20, showing the global variable was not
print("The number is zero.") changed by use_local
5.Develop a program to generate Fibonacci sequence of A flow control statement is a programming statement that
length(N),read N from the console controls the flow of execution in a program. It allows the
num=int(input(“enter the fibonacci sequence length”)) program to make decisions and choose different paths of
fterm=0 execution based on certain conditions. The two most common
sterm=0 flow control statements in Python are if and if-else.
print(“The fibonacci series with”,num,”term is:”) Simple if: If statements are control flow statements that help us
print(fterm,sterm,end=””) to run a particular code, but only when a certain condition is met
for I in range(2,num): or satisfied. A simple if only has one condition to check
curterm=fterm+sterm EXAMPLE:
print(curterm,end=””) n = 10
fterm=sterm if n % 2 == 0:
sterm=curterm print("n is an even number")
print() if-else: The if-else statement evaluates the condition and will
6. Define exception handling? How exceptions are handled in execute the body of if if the test condition is True, but if the
Python? Write a Python program to solve divide by zero condition is False, then the body of else is executed.
exception EXAMPLE:
Solu: Exception handling is the process of responding to n=5
unwanted or unexpected events when a computer program if n % 2 == 0:
runs. Exception handling deals with these events to avoid the print("n is even")
program or system crashing, and without this process, else:
exceptions would disrupt the normal operation of a program. print("n is odd")
Key Concepts of Exception Handling:
Exception: An error that arises during the execution of a
program. Exceptions can occur for various reasons, including
trying to divide by zero, accessing a file that doesn't exist,
attempting to access an array element out of its bounds, and
more.
Try Block: A block of code that might cause an exception. The try
block is monitored for exceptions that it can potentially throw
during execution.
Except Block: A block of code that is executed if an exception
occurs in the try block. Each except block can handle a specific
type of exception or a tuple of exceptions. You can have multiple
except blocks under a single try block to handle different
exceptions differently.
Finally Block: An optional block that executes regardless of
whether an exception is thrown or not. It's typically used for
cleaning up resources, such as closing files or network
connections, that were opened in the try block.
Raise: In many languages, you can also manually throw (raise) an
exception, either re-throwing an caught exception or creating a
new one. This is useful for signaling that an error condition has
occurred.
7.What is a flow control statement?. Discuss if and if else
statements with flow chart.

You might also like