0% found this document useful (0 votes)
0 views11 pages

Bitp Python4

The document is a Jupyter notebook that contains various Python functions demonstrating basic programming concepts such as function definitions, argument handling, arithmetic operations, and lambda functions. It includes examples of a mini calculator, formatting names, and sorting products using lambda expressions. Additionally, it handles errors like division by zero and showcases the use of list comprehensions.

Uploaded by

aaditij090
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)
0 views11 pages

Bitp Python4

The document is a Jupyter notebook that contains various Python functions demonstrating basic programming concepts such as function definitions, argument handling, arithmetic operations, and lambda functions. It includes examples of a mini calculator, formatting names, and sorting products using lambda expressions. Additionally, it handles errors like division by zero and showcases the use of list comprehensions.

Uploaded by

aaditij090
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

PYTHON4_BITP.ipynb - Colab [Link]

1 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

def

return

None

()

def introduction():
print("Hello, this is my first function")

# Call function
introduction()

Hello, this is my first function

# Single argument
def introduction(fname):
print(fname + " is my first name")

introduction("John")

John is my first name

def introduction(fname, lname):


print("My first name is " + fname + " and my last name is " + lname)

2 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

print("My first name is " + fname + " and my last name is " + lname)

introduction("John", "Alter")

My first name is John and my last name is Alter

def introduction(fname, lname):


print("My first name is " + fname + " and my last name is " + lname)

introduction("John")

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-ee1f09e0e8cc> in <cell line: 4>()
2 print("My first name is " + fname + " and my last name is " +
lname)
3
----> 4 introduction("John")

TypeError: introduction() missing 1 required positional argument: 'lname'

def introduction(fname, lname = ' '):


print("My first name is " + fname + " and my last name is " + lname)

introduction("John")

My first name is John and my last name is abc

def add_numbers(number_1, number_2):


sum = number_1 + number_2

result = add_numbers(5, 3)
print("The sum is:", result)

The sum is: None

add_numbers
result
None return

def add_numbers(number_1, number_2):

3 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

add_numbers(number_1, number_2):
sum = number_1 + number_2
return sum

result = add_numbers(5, 3)
print("The sum is:", result)

The sum is: 8

add_numbers
return
result

return

return

def My_Mini_Calculator():
number_1 = float(input("Enter the first number: "))
number_2 = float(input("Enter the second number: "))

# Addition
result = number_1 + number_2
print("The sum is:", result)

# Subtraction
result = number_1 - number_2
print("The difference is:", result)

# Multiplication
result = number_1 * number_2
print("The product is:", result)

# Division
result = number_1 / number_2
print("The quotient is:", result)

# Call the function


My_Mini_Calculator()

4 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

Enter the first number: 5


Enter the second number: 0
The sum is: 5.0
The difference is: 5.0
The product is: 0.0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
/tmp/[Link] in <cell line: 0>()
20
21 # Call the function
---> 22 My_Mini_Calculator()

/tmp/[Link] in My_Mini_Calculator()
16
17 # Division
---> 18 result = number_1 / number_2
19 print("The quotient is:", result)
20

ZeroDivisionError: float division by zero

def My_Mini_Calculator():
number_1 = float(input("Enter the first number: "))
number_2 = float(input("Enter the second number: "))

# Addition
result = number_1 + number_2
print("The sum is:", result)

# Subtraction
result = number_1 - number_2
print("The difference is:", result)

# Multiplication
result = number_1 * number_2
print("The product is:", result)

# Division
if number_2 != 0:
result = number_1 / number_2
print("The quotient is:", result)
else:
print("Oops there is an error: Division by zero is not allowed.")

# Call the function


My_Mini_Calculator()

Enter the first number: 4

5 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

Enter the second number: 0


The sum is: 4.0
The difference is: 4.0
The product is: 0.0
Oops there is an error: Division by zero is not allowed.

def My_Mini_Calculator():
number_1 = float(input("Enter the first number: "))
number_2 = float(input("Enter the second number: "))

# Addition
add_result = number_1 + number_2
#return add_result
# Subtraction
diff_result = number_1 - number_2

# Multiplication
mult_result = number_1 * number_2

# Division
div_result = number_1 / number_2

return {
"Addition": add_result,
"Substraction": diff_result,
"Multiplication": mult_result,
"Division": div_result,
}

# Call the function


results = My_Mini_Calculator()

print("The sum is:", results["Addition"])


print("The difference is:", results["Substraction"])
print("The product is:", results["Multiplication"])
print("The quotient is:", results["Division"])

Enter the first number: 4


Enter the second number: 2
The sum is: 6.0
The difference is: 2.0
The product is: 8.0
The quotient is: 2.0

6 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

def format_name():
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")

# Capitalize the first letter of the first name


formatted_first_name = first_name.capitalize()

# Capitalize the first letter of the last name


formatted_last_name = last_name.capitalize()

# Print the formatted name


print("Candidate name is:", formatted_first_name, formatted_last_name)

# Call the function


format_name()

Enter your first name: shraddha


Enter your last name: nikam
Candidate name is: Shraddha Nikam

def print_even_numbers():
n = int(input("Enter the range limit: "))
even_numbers = []
for num in range(1, n + 1):
if num % 2 == 0:
even_numbers.append(num)

print("Even numbers up to", n, "are:", even_numbers)

# Call the function


print_even_numbers()

Enter the range limit: 8


Even numbers up to 8 are: [2, 4, 6, 8]

7 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

lambda

return

map() filter() reduce()


if else

# lambda arguments: expression

add = lambda x, y: x + y
print(add(5, 3)) # Output: 8

# Define a lambda function that takes three arguments and returns their sum
add_three_numbers = lambda x, y, z: x + y + z

# Use the lambda function


result = add_three_numbers(5, 3, 8)
print(result) # Output: 16

16

square = lambda x: x ** 2
print(square(5)) # Output: 25

25

concat = lambda x, y: x + y
print(concat("Hello", " World")) # Output: "Hello World"

Hello World

8 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

even_or_odd = lambda x: 'even' if x%2==0 else 'odd'


print(even_or_odd(7)) # Output: 'odd'

odd

max_num = lambda x, y: x if x > y else y


print(max_num(5, 10)) # Output: 10

10

add = lambda a, b: a + b
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print(add(num1, num2))

Enter first number: 4


Enter second number: 2
6

add = lambda: int(input("Enter first number: ")) + int(input("Enter second number: "
print(add())

Enter first number: 4


Enter second number: 5
9

lambda_function = lambda x: [i ** 2 for i in x]


print(lambda_function([1, 2, 3, 4, 5])) # Output: [1, 4, 9, 16, 25]

[1, 4, 9, 16, 25]

map() filter() reduce()


sorted()

9 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

# A list of dictionaries is created, where each dictionary represents a product.


# Each product has two properties: 'name' and 'price'.

products = [
{'name': 'Product1', 'price': 50},
{'name': 'Product2', 'price': 30},
{'name': 'Product3', 'price': 40},
{'name': 'Product4', 'price': 20},
]

# The products list and a lambda function are passed to the sorted() method.
# The sorted() function's key parameter accepts a function as an argument.
# This function generates a'sorting key' for each entry in the list.

# In this case, the lambda function lambda x: x['price'] takes an argument (a dictionar
# These pricing values are used by the sorted() method to compare products and determin

sorted_products = sorted(products, key=lambda x: x['price'])

for product in sorted_products:


print(product)

{'name': 'Product4', 'price': 20}


{'name': 'Product2', 'price': 30}
{'name': 'Product3', 'price': 40}
{'name': 'Product1', 'price': 50}

10 of 11 17-03-2026, 11:55
PYTHON4_BITP.ipynb - Colab [Link]

11 of 11 17-03-2026, 11:55

You might also like