0% found this document useful (0 votes)
21 views

Pymodule1 (Chapter 3)

Uploaded by

shivangsahu5209
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Pymodule1 (Chapter 3)

Uploaded by

shivangsahu5209
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter 3

BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
FUNCTIONS
• De-duplicating code, which means getting rid of duplicated or copy-and pasted
code.
• Deduplication makes your programs shorter, easier to read, and easier to update.
• We can write our own functions.
• A function is like a mini- program within a program.
• A major purpose of functions is to group code that gets executed multiple times.

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
• def function_name(parameters):
• """Optional docstring""“
• # Function body
• # Code to perform the task
• return value
• Python def keyword is used to define a function, it is placed before a function
name that is provided by the user to create a user-defined function.
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
Example1 (def Statements with
Parameters)
def add_numbers(a, b): Output—
sum = a + b
print('Sum:', sum) Sum:5
add_numbers(2, 3)

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.2 Return Values and return
Statements
• A return statement consists of the following:
1. The return keyword
2. The value or expression that the function should return.
• Syntax: def fun():
• statements
• .
• .
• .
• return [expression]
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
WAP To add two number using function!
Output—
• def add(a,b):
• c=a+b enter a number:4
• return c enter a number:5
• n=int(input("enter a number:")) 9
• i=int(input("enter a number:"))
• x=add(n,i)
• print(x)
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.3 The None Value

• In Python there is a value called None, which represents the absence of a


value.
• None is the only value of the “NoneType” data type. (Other programming
languages might call this value null, nil, or undefined.)
• One of the common uses of 'None' is in function definitions as a default
parameter.

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
Example
• n= 10 Output—
• i = None
#check whether n is none or not
• if n == None:
• print(" n is None! ")
# check whether the i is none or not i is None!
• elif i == None:
• print(" i is None! ")
# if neither n nor i is none
• else:
• print("Neither n, nor the i is None")
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.4 keyword Arguments and print()

• keyword arguments are identified by the keyword put before them in the function call.
• Keyword arguments are often used for optional parameters.
• For example, the print() function has the optional parameters end and sep to specify what
should be printed at the end of its arguments and between its arguments (separating them),
respectively.
• Example— Output—
• def team(name, project):
• print(name, "is working on an", project) FemCode is working on
• team(project = "Edpresso", name = 'FemCode')
an Edpresso
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.5Python Scope of Variables
• In Python, variables are the containers for storing data values.
• Unlike other languages like C/C++/JAVA, Python is not “statically typed”. We do not need to
declare variables before using them or declare their type. A variable is created the moment we
first assign a value to it.
• Python Scope variable
• The location where we can find a variable and also access it if required is called the scope of a
variable.
• It is 2 types—
1. Local variable
2. Global variable
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.5.1.Local variable
• These are the variable which is store in local space and can be accessed only within the boundaries of the function.

• Example— Output—
a=10
def sam(): 12
a=100
print(a)
10
print(a+2) 100
print(a+2)
print(a) 102
sam()
print(a) 10

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.5.2.Global variable
• These are the variable which is store in global space and can be accessed at any part of the program.
• Example— Output—
a=10
def sam(): 12
print(a)
10
print(a+2)
print(a+2)
10
print(a) 12
sam() 10
print(a)

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.6. Exception Handling

 Errors can be handled with try and except statements.


 The code that could potentially have an error is put in a try clause.
try:
#code that may cause exception
except:
#code to run when exception occurs
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
• Example— Output—
• try:
• numerator = 10
• denominator = 0
Error: Denominator
• result = numerator/denominator cannot be 0.

• print(result)
• except:
• print("Error: Denominator cannot be 0.")

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
3.8 Program- Guess the Number
• import random
• def guess_the_number():
• print("Welcome to Guess the Number!")
• print("I've picked a random number between 1 and 100. Try to guess it!")
• secret_number = random.randint(1, 100)
• attempts = 0
• while True:
• try:
• guess = int(input("Your guess: "))
• attempts += 1

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
• if guess < secret_number:
• print("Too low! Try again.")
• elif guess > secret_number:
• print("Too high! Try again.")
• else:
• print(f"Congratulations! You've guessed the number {secret_number} "
• f"in {attempts} attempts!")
• break
• except ValueError:
• print("Please enter a valid number.")

• guess_the_number()

PREPARED BY- MIS.ITISHREE BARIK


ASST. PROFESSOR, DEPT OF CSE, SIR MVIT
Output--

• Welcome to Guess the Number!


• I've picked a random number between 1 and 100. Try to guess it!
• Your guess: 3
• Too low! Try again.
• Your guess: 78
• Too high! Try again.
• Your guess: 50
• Too low! Try again.
Output
Welcome to Guess the Number!
I've picked a random number between 1 and 100.
Try to guess it!
Your guess: 50 Too low! Try again.
Your guess: 75 Too high! Try again.
Your guess: 60 Too low! Try again.
Your guess: 70 Too high! Try again.
Your guess: 65 Too low!
Try again. Your guess: 68 Too low! Try again.
Your guess: 69 Congratulations! You've guessed the number 69 in 7 attempts!
PREPARED BY- MIS.ITISHREE BARIK
ASST. PROFESSOR, DEPT OF CSE, SIR MVIT

You might also like