Python Ut Answers
Python Ut Answers
python
for i in range(5):
if i==3:
break
print(i)
continue statement:It is
used to skip the rest of the
code inside a loop for the
current iteration, and the
loop continues with the next
iteration. This statement is
helpful when you want to skip
certain iterations of a loop
based on a condition without
terminating the loop entirely.
python
for i in range(5):
if i==3:
Continue
print(i)
python
import math
print(math.pi)
python
from math import pi
print(pi)
sitan n jastcn)
anht'etun, n im nCn'). ht C, ))
nz ma Cuint,
tn<:
emtc
o n, in
bino mial co
uen the corsole
Jha diret bêneci A u )
n nt Gunhut('em
seendnunJ
") uhlicn.
ifcuhe
cutumi mtscemd num
irt end )
i t mn Decondum
seendum Cuuum i'+)
J24 PV
Forwarded
5. *Looping Control
Statements in Python*:
Looping controlstatements
are used to alter the flow of
loops in Python. There are
two primary looping control
statements:
Example:
python
for iin range(5):
print(i)
Example:
python
i=0
while i< 5:
print(i)
i+=1
Example:
python
X= 10 # Global variable
def outer():
y=20 # Enclosingscope
def inner():
z=30 # Local scope
print(x, y, z)
inner()
outer()
In thisexample, x is in
the globalscope, y isin the
enclosing scope,and z is in
the local scope. 9:24 PM
Forwarded
of
- *Need for Role
Precedence*: Precedence
is essential for ensuring
of
the correct evaluation
expressions. It helps in avoiding
ambiguity and ensures that
expressions are evaluated
accordingto the intended order
of operations.
- *Rules of Precedence in
Python*:
- Parentheses (0 have the
highest precedence and can be
of
used to force the evaluation
expressions in aspecific order.
-Exponentiation ** has the
next highest precedence.
Multiplication *,division
1, floor division //, and
modulo % have the same
precedence and are evaluated
from left to right.
-Addition + and
subtraction - also have the
same precedence and are
evaluated from left to right.
Example:
python
result = 10 +2 **3/4 *5
my_function() # Output: 10
# print(x) # Thiswould raise
a NameError since x is not
defined in the global scope
def my function():
print(x) # Accessing
global variable
my function() # Output: 10
def my function):
global x
X=20 # Modifying global
variable
my_function)
print(%) # Output: 20
10. *Functions in Python with
Parameters and Return
Statements*:
- *Syntax of a Function*:
python
def
function_name(parametert,
parameter2, ..):
# Function body
# Perform operations
usingparameters
returnresult # Optional
- *Example of a Function
with Parameters and Return
Statement*:
python
def add_numbers(x, y):
return X + y
result = add_numbers(5, 3)
print(result) # Output: 8
python
def greet(name):
return "Hello, "+ name +
message - greet('Alice")
print(message) # Output:
Hello, Alice!
. *Definition*: Exception
handling is the process of
catching and handling errors
that occur during the execution
of a program.
-*Example of Handling
Divide by Zero Exception*:
python
try:
result = 10/0
except ZeroDivisionError:
print("Error: Division by
zero!")
-*User-Defined Functions*:
These are functions created by
the user to perform a specific
task. They are defined using
the def keyword followed
by the function name and
parameters.
Example:
python
def greet(name):
return "Hello, " + name t
message =greet("Alice")
print(message) # Output:
Hello, Alice!
Examples:
print() : Prints the
specified value(s) to the
standard output.
- input) : Reads input from
the user through the standard
input.
len): Returns the length
of an object (e.g., string, list,
tuple).
python
print("Hello, world!") #
Output: Hello, world!
length = len("Python")
print(length) # Output: 6
9:24 PM
Forwarded
13.*Exception Handling in
Python with Example*;
-*Syntax*:
python
try:
# Code that might raise
an exception
except ExceptionName:
# Code to handle the
exception
-*Example*:
python
try:
x= 10/ 0
except ZeroDivisionError:
print("Error: Division by
zero!")
-*Handling Multiple
Exceptions*
You can handle mutiple
exceptions by specifying
multiple except blocks or by
using a single block to handle
multiple exceptions.
python
try:
# Code that might raise
exceptions
except Exceptiont:
# Code to handle
Exceptiont
except Exception2:
# Code to handle
Exception2
python
try:
# Code that might raise
exceptions
except (Exceptiont,
Exception2):
# Code to handle both
Exceptiont and Exception2
Example:
python
try:
result = int(input("Enter a
number: ")/0
except ValueError:
print("Error: Invalid input!")
except ZeroDivisionError:
print("Error: Division by
zero!")
python
for num in range(0, 11, 2):
print(num)
Output:
2
4
6
8
10