0% found this document useful (0 votes)
167 views13 pages

Python Program - Mca - 3 Sem

The document outlines a Python programming lab course covering various programming concepts like data types, arithmetic operations, area calculations, solving equations, random number generation, and more. It provides example code snippets for 15 different programming problems students would complete as part of the course.

Uploaded by

Cicada Limit
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)
167 views13 pages

Python Program - Mca - 3 Sem

The document outlines a Python programming lab course covering various programming concepts like data types, arithmetic operations, area calculations, solving equations, random number generation, and more. It provides example code snippets for 15 different programming problems students would complete as part of the course.

Uploaded by

Cicada Limit
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/ 13

MCA

III SEMESTER
PYTHON PROGRAMMING LAB
Total Hours: 40 per batch
Hours/Week: 4
Max Marks: 100 Credits: 2
Choose any 15 Programs
PART A
1. Write a Python program to demonstrate basic data type in python
>>> x = 20 #int
>>> print(x)
20
>>> print(type(x))
<class 'int'>
>>> x = 20.5 #float
>>> print(x)
20.5
>>> print(type(x))
<class 'float'>
>>> x = 1j #complex
>>> print(x)
1j
>>> print(type(x))
<class 'complex'>
>>> x = "ACYTECH.COM" #String
>>> print(x)
ACYTECH.COM
>>> print(type(x))
<class 'str'>
>>> x = ["ACT", "TECH", "COMPANY"] #list
>>> print(x)
['ACT', 'TECH', 'COMPANY']
>>> print(type(x))
<class 'list'>
>>> x = ("ACT", "TECH", "COMPANY") #tuple
>>> print(x)
('ACT', 'TECH', 'COMPANY')
>>> print(type(x))
<class 'tuple'>
>>> x = {"name" : "anu", "age" : 36} #dict
>>> print(x)
{'name': 'anu', 'age': 36}
>>> print(type(x))
<class 'dict'>
>>> x = True #bool
>>> print(x)
True
>>> print(type(x))
<class 'bool'>
>>> x = b"Hello" #bytes
>>> print(x)
b'Hello'
>>> print(type(x))
<class 'bytes'>
>>> x = {"APPLE", "ORGANGE", "BANANA"} #set
>>> print(x)
{'ORGANGE', 'BANANA', 'APPLE'}
>>> print(type(x))
<class 'set'>
>>> x=frozenset({"APPLE", "ORGANGE", "BANANA"})#frozenset
>>> print(x)
frozenset({'ORGANGE', 'BANANA', 'APPLE'})
>>> print(type(x))
<class 'frozenset'>

2. Write a Python program to do arithmetical operations


# Store input numbers:
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)
# Subtract two numbers
min = float(num1) - float(num2)
# Multiply two numbers
mul = float(num1) * float(num2)
#Divide two numbers
div = float(num1) / float(num2)
#Divide floor two numbers
divf = float(num1) // float(num2)
#Modulus two numbers
Modul = int(num1) % int(num2)
#Power or Exponent two numbers
expo = int(num1) ** int(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
# Display the subtraction
print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min))
# Display the multiplication
print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))
# Display the division
print('The division of {0} and {1} is {2}'.format(num1, num2, div))
# Display the division floor
print('The division floor of {0} and {1} is {2}'.format(num1, num2, divf))
# Display the Modulus
print('The Modulus of {0} and {1} is {2}'.format(num1, num2, Modul))
# Display the Power or Exponent
print('The exponent or power of {0} and {1} is {2}'.format(num1, num2, expo))

Output:
Enter first number: 3
Enter second number: 2
The sum of 3 and 2 is 5.0
The subtraction of 3 and 2 is 1.0
The multiplication of 3 and 2 is 6.0
The division of 3 and 2 is 1.0
The division floor of 3 and 2 is 1
The Modulus of 3 and 2 is 1
The power of 3 and 2 is 6

3. Write a Python program to find area of triangle


# Three sides of the triangle is a, b and c:
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

Output:
Enter first side: 5.0
Enter second side: 6.0
Enter third side: 7.0
The area of the triangle is 14.70

4. Write a Python program to solve quadratic equation

# Python program to find roots of quadratic equation


import math
# function for finding roots
def findRoots(a, b, c):
dis_form = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis_form))
if dis_form > 0:
print(" real and different roots ")
print((-b + sqrt_val) / (2 * a))
print((-b - sqrt_val) / (2 * a))
elif dis_form == 0:
print(" real and same roots")
print(-b / (2 * a))
else:
print("Complex Roots")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)
a = int(input('Enter a:'))
b = int(input('Enter b:'))
c = int(input('Enter c:'))

# If a is 0, then incorrect equation


if a == 0:
print("Input correct quadratic equation")

else:
findRoots(a, b, c)

Output:
Enter a:7
Enter b:5
Enter c:2
Complex Roots
-0.35714285714285715 + i 5.5677643628300215
-0.35714285714285715 - i 5.5677643628300215

5. Write a Python program to swap two variables


P = int( input("Enter value for P: "))
Q = int( input("Enter value for Q: "))
# To swap the value of two variables
# we will user third variable which is a temporary variable
temp_1 = P
P=Q
Q = temp_1

print ("The Value of P after swapping: ", P)


print ("The Value of Q after swapping: ", Q)

Output:
Please enter value for P: 25
Please enter value for Q: 03
The Value of P after swapping: 03
The Value of Q after swapping: 25

6. Write a Python program to generate a random number


import random
rand_list = []
for i in range(0,10):
n = random.randint(1,50)
rand_list.append(n)
print(rand_list)
Output:
[10, 49, 16, 31, 45, 21, 19, 32, 30, 16]

7. Write a Python program to convert kilometres to miles


Method: 1 kilometre equals 0.62137 miles.
Miles = kilometre * 0.62137
And,
Kilometre = Miles / 0.62137

def kilometre_1(kmeter):
conversion_ratio_1= 0.621371
miles_1 = kmeter * conversion_ratio_1
print ("The speed value of car in Miles: ", miles_1)
kmeter = float (input ("Enter the speed of car in Kilometre as a unit: "))
kilometre_1(kmeter)

Output:
Enter the speed of car in Kilometre as a unit: 16
The speed value of car in Miles: 9.941936

8. Write a Python program to convert Celsius to Fahrenheit


T(°Fahrenheit)=(T(°Celsius)*1.8)+32
T(°Fahrenheit)=(T(°Celsius)*9/5)+32
celsius_1 = float(input("Temperature value in degree Celsius: " ))
Fahrenheit_1 = (celsius_1 * 1.8) + 32
# print the result
print('The %.2f degree Celsius is equal to: %.2f Fahrenheit'
%(celsius_1, Fahrenheit_1))
celsius_2 = float (input("Temperature value in degree Celsius: " ))
Fahrenheit_2 = (celsius_2 * 9/5) + 32
# print the result
print ('The %.2f degree Celsius is equal to: %.2f Fahrenheit'
%(celsius_2, Fahrenheit_2))
Output:
Temperature value in degree Celsius: 34
The 34.00 degree Celsius is equal to: 93.20 Fahrenheit
Temperature value in degree Celsius: 23
The 23.00 degree Celsius is equal to: 73.40 Fahrenheit

9. Write a Python program to display calendar


import calendar
yy = int(input("Enter year: "))
Enter year: 2023
mm = int(input("Enter month: "))
Enter month: 4
print(calendar.month(yy,mm))

Output:
April 2023
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 30

10.Write a Python program to Check if a Number is Positive, Negative or Zero


num = float(input("Input a number: "))
if num > 0:
print("It is positive number")
elif num == 0:
print("It is Zero")
else:
print("It is a negative number")

Output:
Input a number : 150
It is positive number
Input a number : -150
It is negative number
Input a number : 0
It is Zero
Part- B

11. Python program to check if the input number is odd or even

num = int(input("Enter a number: "))


if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

Output:
Enter a number: 15
15 is Odd

12.Write a Python program to Check Leap Year


year = int(input('Enter year : '))
if (year%4 == 0 and year%100 != 0) or (year%400 == 0) :
print(year, "is a leap year.")
else :
print(year, "is not a leap year.")

Output:
Enter year : 2023
2023 is not a leap year.

13.Write a Python program to Check Prime Number


def PrimeChecker(a):
# Checking that given number is more than 1
if a > 1:
# Iterating over the given number with for loop
for j in range(2, int(a/2) + 1):
# If the given number is divisible or not
if (a % j) == 0:
print(a, "is not a prime number")
break
# Else it is a prime number
else:
print(a, "is a prime number")
# If the given number is 1
else:
print(a, "is not a prime number")
# Taking an input number from the user
a = int(input("Enter an input number:"))
# Printing result
PrimeChecker(a)

Output:
Enter an input number: 17
17 is a prime number

14.Write a Python program to Print all Prime Numbers in an Interval


# First, we will take the input:
lower_value = int(input ("Enter the Lowest Range Value: "))
upper_value = int(input ("Enter the Upper Range Value: "))

print ("The Prime Numbers in the range are: ")


for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)

Output:
Enter the Lowest Range Value: 14
Enter the Upper Range Value: 60
The Prime Numbers in the range are:
17
19
23
29
31
37
41
43
47
53
59
15.Write a Python program to Find the Factorial of a Number
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print(" Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output:
Enter a number: 10
The factorial of 10 is 3628800

16.Write a Python program to Display the multiplication Table

num = int(input ("Enter the number of which the user wants to print the multipli
cation table: "))
for i in range(1, 11):
print(num, 'x', i, '=', num*i)

Output:
Enter the number of which the user wants to print the multiplication table: 12
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
17.Write a Python program to Print the Fibonacci sequence
n_terms = int(input ("Enter the limit "))
# First two terms
previous= 0
current = 1
count = 0
# Now, we will check if the number of terms is valid or not
if n_terms <= 0:
print ("Please enter a positive integer, the given number is not valid")
# if there is only one term, it will return n_1
elif n_terms == 1:
print ("The Fibonacci sequence of the numbers up to", n_terms, ": ")
print(previous)
# Then we will generate Fibonacci sequence of number
else:
print ("The fibonacci sequence of the numbers is:")
while count < n_terms:
print(previous)
new_ele = previous+ current
# At last, we will update values
previous = current
current = new_ele
count += 1
Output:
Enter the limit 7
The fibonacci sequence of the numbers is:
0
1
1
2
3
5
8
18.Write a Python program to Check Armstrong Number
number = int(input("Enter the number"))
Enter the number1245
digits = len(str(number))
temp = number
add_sum = 0
while temp != 0:
# get the last digit in the number
k = temp % 10
# find k^digits
add_sum += k**digits
# floor division
# which updates the number with the second last digit as the last digit
temp = temp//10
if add_sum == number:
print('Given number is an Armstrong Number')
else:
print('Given number is not a Armstrong Number')

Output:
Enter the number1245
Given number is not a Armstrong Number

19.Write a Python program to Find Armstrong Number in an Interval


lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
for num in range(lower,upper + 1):
sum = 0
temp = num
L=len(str(num)
while temp > 0:
digit = temp % 10
sum += digit ** 3 L
temp //= 10
if num == sum:
print(num)
Output:
Enter lower range: 50
Enter upper range: 100
64
20.Write a Python program to Find the Sum of Natural Numbers
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)

Output:
Enter a number: 50
The sum is 1275

You might also like