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

Experiment 4 Program

The document contains multiple Python programs demonstrating various functionalities including converting decimal numbers to binary, octal, and hexadecimal, importing and displaying a calendar, creating a basic calculator, and using functions with keyword arguments and lambda functions. Each program includes example outputs for user inputs, showcasing how the functions operate. Additionally, it illustrates the use of global variables and variable-length arguments in Python.

Uploaded by

aj7355149
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)
5 views

Experiment 4 Program

The document contains multiple Python programs demonstrating various functionalities including converting decimal numbers to binary, octal, and hexadecimal, importing and displaying a calendar, creating a basic calculator, and using functions with keyword arguments and lambda functions. Each program includes example outputs for user inputs, showcasing how the functions operate. Additionally, it illustrates the use of global variables and variable-length arguments in Python.

Uploaded by

aj7355149
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/ 8

Experiment No: 4

Program 1 : Python Program to Convert Decimal to Binary, Octal and


Hexadecimal
# First, we will define the function to convert decimal to binary

def decimal_into_binary(decimal_1):

decimal = int(decimal_1)

# then, print the equivalent decimal

print ("The given decimal number", decimal, "in Binary number is: ", bin(decimal))

# we will define the function to convert decimal to octal

def decimal_into_octal(decimal_1):

decimal = int(decimal_1)

# Then, print the equivalent decimal

print ("The given decimal number", decimal, "in Octal number is: ", oct(decimal))

# we will define the function to convert decimal to hexadecimal

def decimal_into_hexadecimal(decimal_1):

decimal = int(decimal_1)

# Then, print the equivalent decimal

print ("The given decimal number", decimal, " in Hexadecimal number is: ", hex(decimal))

# Driver program

decimal_1 = int (input (" Enter the Decimal Number: "))

decimal_into_binary(decimal_1)

decimal_into_octal(decimal_1)

decimal_into_hexadecimal(decimal_1)
Output:

Enter the Decimal Number: 10

The given decimal number 10 in Binary number is: 0b1010

The given decimal number 10 in Octal number is: 0o12

The given decimal number 10 in Hexadecimal number is: 0xa

>

Program 2 : Python Program to Import the Calendar


# First import the calendar module

import calendar

# ask of month and year

yy = int(input("Enter year: "))

mm = int(input("Enter month: "))

# display the calendar

print(calendar.month(yy,mm))

Output :

Enter year: 2024

Enter month: 2

February 2024

Mo Tu We Th Fr Sa Su

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29

Program 3 : Python Program for Calculator Using Function


def add(P, Q):

# This function is used for adding two numbers

return P + Q
def subtract(P, Q):

# This function is used for subtracting two numbers

return P - Q

def multiply(P, Q):

# This function is used for multiplying two numbers

return P * Q

def divide(P, Q):

# This function is used for dividing two numbers

return P / Q

# Now we will take inputs from the user

print ("Please select the operation.")

print ("a. Add")

print ("b. Subtract")

print ("c. Multiply")

print ("d. Divide")

choice = input("Please enter choice (a/ b/ c/ d): ")

num_1 = int (input ("Please enter the first number: "))

num_2 = int (input ("Please enter the second number: "))

if choice == 'a':

print (num_1, " + ", num_2, " = ", add(num_1, num_2))

elif choice == 'b':

print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))

elif choice == 'c':

print (num_1, " * ", num_2, " = ", multiply(num_1, num_2))

elif choice == 'd':

print (num_1, " / ", num_2, " = ", divide(num_1, num_2))


else:

print ("This is an invalid input")

Output:

1. Addition:

Please select the operation.

a. Add

b. Subtract

c. Multiply

d. Divide

Please enter choice (a/ b/ c/ d): a

Please enter the first number: 2

Please enter the second number: 2

2 + 2 = 4

2. Subtraction:

Please select the operation.


a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): b
Please enter the first number: 8
Please enter the second number: 4
8 - 4 = 4

3. Multiplication:

Please select the operation.


a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): c
Please enter the first number: 4
Please enter the second number: 4
4 * 4 = 16
4. Division:

Please select the operation.


a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): d
Please enter the first number: 8
Please enter the second number: 4
8 / 4 = 2.0
>

Program 4 : Python Program to Print Age and Name


def printinfo( name, age= 35 ):

print("This prints a passed info into this function")

print("Name: ", name)

print("Age ", age)

return;

# Now you can call printinfo function

printinfo( age=25, name="Riya" )

printinfo( name="Rohan" )

printinfo( age=41, name="Pranoti" )

Output:
This prints a passed info into this function

Name: Riya

Age 25

This prints a passed info into this function

Name: Rohan

Age 35

This prints a passed info into this function

Name: Pranoti
Age 41
>
Program 5 : Keywords Arguments

def printinfo( name, age ):

print("This prints a passed info into this function")

print ("Name: ", name)

print ("Age ", age)

return;

# Now you can call printinfo function

#printinfo("Raghav",5)

printinfo(5,"Raghav")

printinfo( age=25, name="Riya" )

Output:
This prints a passed info into this function

Name: 5

Age Raghav

This prints a passed info into this function

Name: Riya

Age 25

>

Program 6 : Lamda Function

sum = lambda arg1, arg2: arg1 / arg2;

# Now you can call sum as a function

print("Value of total : ", sum( 10, 20 ))

print("Value of total : ", sum( 20, 20 ))


Output:
Value of total : 0.5

Value of total : 1.0

>

Program 7 : Global variable

total = 0; # This is global variable.

# Function definition is here

def sum( arg1, arg2 ):

# Add both the parameters and return them."

total = arg1 + arg2; # Here total is local variable.

print ("Inside the function local total : ", total)

return total;

# Now you can call sum function

sum( 10, 20 );

total=sum( 20, 20 )

print ("Outside the function global total : ", total )

Output:
Inside the function local total : 30

Inside the function local total : 40

Outside the function global total : 40

>

Program 8 : Variable length

# Python program to illustrate

# *args with first extra argument

def myFun(x,y, *argv):

print ("First argument :", x)


#print ("Second argument :", y)

for i in argv:

print("Next argument through *argv :",i)

myFun('Hello', 'Welcome', 'to','Atharva', 'Engineering', 'College')

Output:
First argument : Hello

Next argument through *argv : to

Next argument through *argv : Atharva

Next argument through *argv : Engineering

Next argument through *argv : College

>

You might also like