0% found this document useful (0 votes)
18 views19 pages

Adobe Scan 02 Dec 2024

Ok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views19 pages

Adobe Scan 02 Dec 2024

Ok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

numbers, both positive and

negative, without a fractional


part.

• Examples: o, -42, 2024

• Uses:

0
Performing arithmetic
operations (addition,
subtraction, multiplication,
etc.).

o Representing counts, indices,


or any discrete value.

o Used in loops, conditions, and


mathematical calculations.
• Example Code:

Python
11 :52 ~ W 1.00
V' KB/5
Vo
lTE
:sGII
II
0 90'¾0

ChatGPT
• 1:.xamp1e l;oae:

Python LI Copy code

X = 10
y = -5
result= x + y # result is 5

2. float (Floating-point
number)

• Description: Represents real


numbers, including those with
fractional parts . It supports
decimal points and scientific
notation.

• Examples : 3.14, -0.001, 1.2e3


(which equals 1200. o)

• Uses :

0 Performing calculations with


decimal precision, such as
measurements, weights, or
prices .

e Message
11:53 : ~ 21 .0 Vo <SGII
V KB/S lTE II
Q 893/c0

ChatGPT

• Uses:

o Performing calculations with


decimal precision, such as
measurements, weights, or
prices.

0 Used in scientific and


engineering computations.

• Example Code:

Python LI Copy code

pi= 3.14159
radius= 5.0
area= pi * radius ** 2 #
area is 78 . 53975

3. boo/ (Boolean)

• Description: Represents truth


values . It has two possible
values: True and False.

• Examples: v alse
•· -- --

G Message
This creates a copy that
others can chat with

3. boo/ (Boolean)

• Description : Represents truth


values. It has two possible
values: True and False .

• Examples : True, False

• Uses:

0 Used in logical operations and


control flow, such as if
statements and loops.

o Results from comparison


operations(==,>,<, etc.) .

o Often used in conjunction with


logical operators (and, or , not).

• Example Code:

Python L) Copy code

is_valid = True
if is _ vali~ ·
prin t ' 55 granted" )
# This p V f is valid is
True.

Message
11:53 ~ W 4 .00
W' KB/S
Vo
lTE
:sG
II
II 0 89'¾0

ChatGPT

• Example Code:

Python LI Copy code

is_valid = True
if is _valid :
print ( ''Access granted'' )
# This prints if is valid is
True.

4. None (NoneType)

• Description : Represents the


absence of a value or a null value.
It is a special singleton object in
Python .

• Examples : None

• Uses:

0 Indicates that a variable has


no value assigned yet.

0 Used as ' Ult argument in


V
functior,

e Message
e a link to chat.
creates a copy that
s can chat with

uses:
o Indicates that a variable has
no value assigned yet .

o used as a default argument in

functions.
o Helps manage optional or
missing data in a program-

Example code:
CJ copY code

python
result= None # Indicates no

result
def yet (value= None ) :
compute
if value is None :
return "NO input

provided"
return value* 2
int

float

bool

None

• Message
Discu ss the role of condi tional
state ment s in pytho n

Role of Conditional
Stat eme nts in Python

Cond itiona l statem ents are a


funda ment al part of progr ammi ng in
Pytho n . They allow progr ams to
make decis ions based on speci fic
condi tions, enabl ing dynam ic and
flexib le behav ior. Cond itiona l
state ment s evalu ate logica l
expre ssion s and execu te differe nt
block s of code based on wheth er the
cond itions are True or False.

Key Roles of Conditional


state men ts

1. Decis ion V J:
ontc ,:,lln•M
-'"tinn, :,I ct,:,tom
0 r,-,nn•
W 16.0 Vo :SGII
V KB/S LIE I I
Q 390

ents

ision Making:

Conditional statements allow


programs to choose between
different paths of execution
based on conditions.

Example:

Python L) Copy code

age = 18
if age >= 18 :
print ( "You are
eligible to vote." )
else :
print ( ''You are not
eligible to vote." )

ntrol Flow:

They determine the program's


flow by executing specific
code blocks while skipping
others, based on logical
expressions.

Examp v
flow by executing specific
code blocks while skipping
others, based on logical
expressions.
0
Example:

Python
Ll Copy code

number = 1 o
if number > 0 :
print ( "Positive
number" )
elif number < 0 :
print ( "Negative
number" )
else :
print ( "Zero" )

3. Error Handling and Validation:


0
Used to verify inputs or
conditions before proceeding
with computations or
operati

0
Examp h.

G Message
Python
LI Copy code

value= input ( "Enter a


number: " )
if value . isdigit():
print ( f''Square of
{value} is
{int (value)** 2} '' )
else :
print ( ''Invalid input.
Please enter a number." )

ration and Loop Control:

Conditional statements are


often used within loops to
break, continue, or modify the
loop's execution.
11 :56 ~ W 5.00
W' KB/5
Vo
lTE
:sG
II
II 0 89'¾0

Share a link to chat?


This creates a copy that
others can chat with

4. Iteration and Loop Control :

° Conditional statements are


often used within loops to
break, continue, or modify the
loop's execution.

o Example:

Python L) Copy code

for i in range ( 10 ) :
if i % 2 == 0 :
continue # Skip
even numbers
print (i) # Prints
only odd numbers

5. Dynamic Functionality:

0 They allow Python programs


to adapt to user inputs,
system states, or data
conditions.

0 Example:

Pytho r Ll Copy code

e Message
11:56 ~ ~ 016
V
Vo
KEHS LTE
SG
111
IQ 89o/c0

ChatGPT

continue # Skip
even numbers
print (i) # Prints
only odd numbers

5. Dynamic Functionality:

o They allow Python programs


to adapt to user inputs,
system states, or data
conditions.

o Example:

Python □ Copy code

if temp < O:
return ''Freezing''
elif O <=temp <= 20 :
return "Cold"
elif 20 < temp <= 30 :
return "Warm"
else :
return "Hot"
print (classify_temperatur
e( 25 )) # Output: Warm

Message
WO 26 V 5G I'\
V KB/S Lli 1111 v 88%

hatGPT

Describe exception handling


in python using try, except,
finally and else blocks . How
does it improve program
reliability

xception Handling in Python

xception handling in Python is a


echanism for detecting and
andling errors or exceptions that
ccur during program execution .
Python uses the constructs try,
except, else, and finally to
manage exceptions gracefully,
allowing the program to recover
from errors without crashing.

Key Components of Exception


Handling

1 . try Block

essage
11:59 : ~ O 47 Vo
v KB/s m SG I
• 1I ,V, 883/co

ChatGPT

Key Components of Exception


Handling

1. try Block:

° Contains the code that might


raise an exception.

0 If no exception occurs, the


try block executes normally.

0 If an exception occurs, the


execution jumps to the
corresponding except block.

Example:

Python LI Copy code

try :
x = int ( input ( ''Enter a
number: " ))

2. except Block:

o Defines how to handle


specific exceptions.

o Can ha · 'le or more


V
except. es, or use a
nPnPrir- h:::inrllPr fnr :::inu

G Message
Python L) Copy code

try :
X = 1 / 0
except ZeroDivisionError:
print ( ''Division by
zero is not allowed.")

3. else Block (Optional):

o Executes code if no exception


occurs in the try block.

o Useful for running code that


should only execute when the
try block is successful.
11 :59 ~ W 7.00
V' KB/S
Vo : 5G
lTE II
I I l) 88o/c0

ChatGPT

3. else Block (Optional):

0 Executes code if no exception


occurs in the try block.

0 Useful for running code that


should only execute when the
try block is successful.

o Example:

Python L) Copy code

try :
x = int ( input ( ''Enter
a number : " ))
except ValueError:
print ( "Invalid input.
Please enter a number . " )
else :
print ( f''You entered :
{ x} " )

4. finally Block (Optional):

0 Executes code regardless of


whether an exception occurs
or not.

0 Often u~- -' for cleanup tasks


like clc v es or releasing
resource _

e tv,essage
11 :59 ~ W 3.00
V' KB/S
Vo
lTE
SG
II
II l) 88'¾0

ChatGPT

4. finally Block (Optional):

0 Executes code regardless of


whether an exception occurs
or not.

0 Often used for cleanup tasks


like closing files or releasing
resources .

o Example:

Python Ll Copy code

try :
file=
open ( ''data.txt'' , ''r'' )
data= file.read()
e xcept FileNotFoundError :
print ( "File not
found . " )
finally :
file.close() #
Ensures the file is
always closed

How Excep v Jandling


lmnrnvp~ Prnnr::am RPli::ahilitv

e Message
12:00 : ~ 792 Vo .SG
V' KB/S LTE II II 'V, 883/c0

ChatGPT

How Exception Handling


Improves Program Reliability

1. Prevents Crashes :

0 Without exception handling,


an error causes the program
to terminate abruptly.
Exception handling allows the
program to recover gracefully.

2. Improves Debugging :

0 Exceptions provide detailed


error messages and stack
traces, making it easier to
identify and fix issues .

3. Ensures Resource Cleanup :

o The finally block ensures


that resou rces like files or
database connections are
released properly, preventing
resource leaks.

4. Handles r v cted Scenarios :

o Prnnr::imc:: l"'::ln ::intil"'in::itA ::inrl

4I Message

You might also like