0% found this document useful (0 votes)
3 views12 pages

Python_Lab_Manual_with_Solutions

Q & Solution

Uploaded by

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

Python_Lab_Manual_with_Solutions

Q & Solution

Uploaded by

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

UT ADMINISTRATION OF DNH & DD

GOVERNMENT POLYTECHNIC, DAMAN


DEPARTMENT OF INFORMATION TECHNOLOGY

Program Name: Diploma in Engineering


Branch: Information Technology
Course Code: DI01016011
Course Name: Python Programming

Lab Manual with Solutions


UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY

Index of Practical
1Algorithms & Flowcharts (Sum of two, Max of two, Odd/Even, Max of three)
2. Install Python & print personal details
3. Identify data types in Python
4. Average of three numbers; Fahrenheit to Celsius
5. Even/Odd; Maximum of three numbers
6. Prime number check
7. Print Odd and Even from 1..N
8. Demonstrate break, continue, pass
9. Factorial (UDF); Fibonacci (UDF)
10. Reverse value using function
11. Armstrong number using function
12. String tasks: reverse words, substring, counts
13. List: sum, min, max
14. Filter even elements from a list
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY

Practical 1: Algorithms & Flowcharts

1.1 Find the sum of two given numbers


Algorithm:

1. Start
2. Input two numbers a and b
3. Compute sum = a + b
4. Print sum
5. Stop

Flowchart:

1.2 Find the maximum of two numbers


Algorithm:

6. Start
7. Input two numbers a and b
8. If a > b, print a; else, print b
9. Stop

Flowchart:
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY

1.3 Find whether a number is odd or even


Algorithm:

10. Start
11. Input a number n
12. If n % 2 == 0, print 'Even'; else, print 'Odd'
13. Stop

Flowchart:
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY
1.4 Find the maximum of three numbers
Algorithm:

14. Start
15. Input three numbers a, b, c
16. If a > b and a > c, print a
17. Else if b > c, print b; else print c
18. Stop

Flowchart:
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY
Practical 2:
Install & configure python software and Create a program to print your
name, date of birth and mobile number.
Install Python on Windows & Run It

1. Start

2. Open a web browser

3. Go to the official Python website → https://www.python.org

4. Navigate to Downloads → Select Windows

5. Download the latest Python installer (e.g., Python 3.x.x)

6. Open the downloaded installer

7. Tick “Add Python to PATH” option

8. Click Install Now

9. Wait until installation completes

10. Open Command Prompt (cmd)

11. Type python --version → check installation

12. Type python → enter Python interactive shell

13. Write print("Hello, World!") and press Enter

14. Observe the output

15. Stop

print("Name: John Doe")


print("Date of Birth: 01-01-2000")
print("Mobile Number: 9876543210")
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY

Practical 3:
Develop a program to identify data-types in python.

x = 10
y = 10.5
z = "Hello"
a = True
b = [1, 2, 3]

print(type(x))
print(type(y))
print(type(z))
print(type(a))
print(type(b))

Practical 4:
1) Create a program to read three numbers from the user and find the
average of the numbers.
2) Create a program to convert temperature from Fahrenheit to Celsius
unit using eq: C=(F-32)/1.8

# Average of three numbers


a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
avg = (a + b + c) / 3
print("Average =", avg)

# Fahrenheit to Celsius
f = float(input("Enter temperature in Fahrenheit: "))
c = (f - 32) / 1.8
print("Celsius Temperature =", c)
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY
Practical 5:
1) Create a program to identify whether the scanned number is even or
odd and print an appropriate message.
2) Create a program to find a maximum number among the given three
numbers.
# Even or Odd
n = int(input("Enter a number: "))
if n % 2 == 0:
print(n, "is Even")
else:
print(n, "is Odd")

# Maximum of three numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Maximum number is:", max(a, b, c))

Practical 6:
Develop a program to show whether the entered number is prime or not.

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


if n > 1:
for i in range(2, n):
if n % i == 0:
print(n, "is not a Prime number")
break
else:
print(n, "is a Prime number")
else:
print(n, "is not a Prime number")
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY
Practical 7:
Develop a program to print odd and even numbers from 1 to N numbers.
(Where N is an integer number entered by the user)

N = int(input("Enter the value of N: "))

print("Even numbers:")
for i in range(1, N+1):
if i % 2 == 0:
print(i, end=" ")

print("\nOdd numbers:")
for i in range(1, N+1):
if i % 2 != 0:
print(i, end=" ")
Practical 8:
Develop a program to demonstrate the use of break, continue and pass
statements.
print("Break example:")
for i in range(1, 10):
if i == 5:
break
print(i)

print("\nContinue example:")
for i in range(1, 10):
if i == 5:
continue
print(i)

print("\nPass example:")
for i in range(1, 10):
if i == 5:
pass
print(i)
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY
Practical 9:
1) Develop a user-defined function to find the factorial of a given number.
2) Create a user-defined function to print the Fibonacci series of 0 to N
numbers. (Where N is an integer number and passed as an argument).

# Factorial Function
def factorial(n):
fact = 1
for i in range(1, n+1):
fact *= i
return fact

n = int(input("Enter a number for factorial: "))


print("Factorial:", factorial(n))

# Fibonacci Function
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

n = int(input("\nEnter number of terms for Fibonacci series: "))


fibonacci(n)

Practical 10:
Write a program using the function that reverses the entered value.
def reverse_string(s):
return s[::-1]

s = input("Enter a string or number: ")


print("Reversed:", reverse_string(s))
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY
Practical 11:
Write a program that determines whether a given number is an
Armstrong number or not using a user-defined function.

def is_armstrong(n):
order = len(str(n))
sum_val = sum(int(digit) ** order for digit in str(n))
return n == sum_val

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


if is_armstrong(n):
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")

Practical 12:
1) Write a program to reverse words in a given sentence.
2) Write a program to check if a substring is present in a given string.
3) Write a program to count and display the number of vowels,
consonants, uppercase, lowercase characters in a string.String processing
tasks.

# Reverse words
sentence = input("Enter a sentence: ")
words = sentence.split()
print("Reversed words:", " ".join(reversed(words)))

# Substring check
substring = input("Enter substring to search: ")
if substring in sentence:
print("Substring found")
else:
print("Substring not found")

# Count characters
vowels = consonants = upper = lower = 0
for ch in sentence:
if ch.isalpha():
if ch in "AEIOUaeiou":
vowels += 1
UT ADMINISTRATION OF DNH & DD
GOVERNMENT POLYTECHNIC, DAMAN
DEPARTMENT OF INFORMATION TECHNOLOGY
else:
consonants += 1
if ch.isupper():
upper += 1
elif ch.islower():
lower += 1

print("Vowels:", vowels)
print("Consonants:", consonants)
print("Uppercase letters:", upper)
print("Lowercase letters:", lower)

Practical 13:
1) Create a program to find the sum of all elements in a list using a loop.
2) Create a program to find the smallest and largest element in a given
list.List: sum, smallest and largest

a = [10, 20, 30, 40, 50]

# Sum of elements
print("Sum =", sum(a))

# Min and Max


print("Smallest element =", min(a))
print("Largest element =", max(a))

Practical 14:
Given a list saved in variable: a = [1, 8, 7, 15, 25, 36,48, 64, 81, 95]. Write a
Python program that takes this list and makes a new list that has only the
even elements of this list in it.

a = [1, 8, 7, 15, 25, 36, 48, 64, 81, 95]


even_list = [x for x in a if x % 2 == 0]
print("Even elements:", even_list)

You might also like