Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (2 votes)
2K views

Python LAB Solution PRAYOSHA GTU

This document contains summaries of 10 Python programming practices. Each practice demonstrates a basic Python programming concept such as taking user input, performing calculations, conditional checking, and more. The practices include programs to find the sum of two numbers, determine if a number is even or odd, calculate simple and compound interest, and build a basic calculator.

Uploaded by

Nihar Chaudhari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views

Python LAB Solution PRAYOSHA GTU

This document contains summaries of 10 Python programming practices. Each practice demonstrates a basic Python programming concept such as taking user input, performing calculations, conditional checking, and more. The practices include programs to find the sum of two numbers, determine if a number is even or odd, calculate simple and compound interest, and build a basic calculator.

Uploaded by

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

PRAYOSHA ENGINEERING CLASSES

Subject name: Python Programming


Subject Code: 4311601
Branch: IT
Semester: 1st

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 1. Prepare flowchart and algorithm for a given problem.

i. Find the sum of two given numbers.

STEP 1 : START

STEP 2 : Input Number1

STEP 3 : Input Number2

STEP 4 : Sum=Number1+Number2

STEP 5 : DISPLAY Sum

STEP 6 : STOP

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

ii. Find a maximum out of two given numbers.

Step 1: Start

Step 2: Input two numbers A and B from


user .

Step 3: Check condition (A > B) ,


If true then
print A is maximum.
If false, then
print B is maximum.

Step 4: End

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

iii. Find whether a given number is odd or even.

Step 1: Start

Step 2: Input numbers from user.

Step 3: Check condition (number%2==0)


If true then
Print Even Number.
If false, then
Print Odd Number.

Step 4: End

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

iv. Find a maximum out of three given numbers.

Step 1:Star

Step 2:Read three numbers A,B & C

Step 3:If A>B, then go to step 6

Step 4:If B>C, then print B & go to step 8

Step 5:print C is greatest & go to step 8

Step 6:If A>C, then print A is greatest &


go to step 8

Step 7:Print C is greatest

Step 8:end

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 2. Install& configure python software.

Python is a very easy-to-learn and powerful programming language. And that's why it's gaining so
much popularity among people looking to start programming.

Python doesn't have complicated syntax or difficult rules. At its core, Python is a practical
programming language that's used in:

 Web development
 Data analysis
 Artificial intelligence
 Natural language processing
 Scientific computing

You can also build lots of different web, desktop, or Smartphone apps in Python. With its various
modules, Python makes it easy to learn new programming concepts and build increasingly
complex applications.

Step-by-step Python installation:


Let's start by installing Python. You can download it for free from the OFFICIAL PYTHON
WEBSITE. Python can be installed on:

Windows
Mac OS
Linux

Installing Python on Windows:

If you're on Windows, there's more than one way to install Python. You can download the 32-bit
or 64-bit version of Python using a(n):

 Web installer
 Executable (.exe) installer
 Zip file

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

We'll download Python 3.7.0 using the executable installer (python-3.7.0-amd64.exe). Why this
kind of installer? Because it has flexible user interface and contains all we need to write and run
our scripts in Python.

Once you download the installer, run it and follow the steps below.

In the first installation window, we can choose type of installation: Install Now or Customize. If
you don't want to change the installed tools or settings, choose the first option. Also, be sure to
check the Add Python 3.7 to PATH box at the bottom of the window to inform your OS where it
can find the Python executable so that it may be invoked from the command line.

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

The installation process starts.

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

On the last window, click the Close button.

In your Start menu, you should see a list of Python 3.7 tools.

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 3. Create a program to print your name, mobile number, and date ofbirth.[CO2]

print("Your Name: Amit Panchal")


print("Your Mobile No: 9999999999")
print("Your Birth Date: 15/10/2000")

OUTPUT: Your Name: Amit Panchal


Your Mobile No: 9999999999
Your Birth Date: 15/10/2000

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 4. Develop a program to identify data-types in python. [CO2]

a=5
print(a, "is of type", type(a))

a = 2.0
print(a, "is of type", type(a))

a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))

OUTPUT: 5 is of type <class 'int'>


2.0 is of type <class 'float'>
(1+2j) is complex number? True
Your Birth Date: 15/10/2000

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 5. Create a program to read three numbers from the user and find
the average of the numbers. [CO2]

num1 = float(input('Enter first number: '))


num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))

# calculate average
avg = (num1 + num2 + num3)/3

# display result
print('The average of numbers = ‘,avg)

OUTPUT: Enter first number: 10


Enter second number: 20
Enter third number: 30
The average of numbers = 20.00

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 6. Develop a program that can calculate simple interest and


compound interest on given data.

# Simple and Compound Interest

# Reading principal amount, rate and time


principal = float(input('Enter amount: '))
time = float(input('Enter time: '))
rate = float(input('Enter rate: '))

# Calcualtion
simple_interest = (principal*time*rate)/100
compound_interest = principal * ( (1+rate/100)**time - 1)

# Displaying result
print('Simple interest is: %f' % (simple_interest))

OUTPUT: Enter amount: 5000


Enter time: 2
Enter rate: 18
Simple interest is: 1800.000000
Compound interest is: 1962.000000

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 7. Write a program to convert temperature from Fahrenheit to


Celsius unit using eq: C=(F-32)/1.8

Fahrenheit= 54
Celsius = ((Fahrenheit-32)/1.8)
print("Temperature in Celsius is: ");
print(Celsius);

OUTPUT: Temperature in Celsius is: 12.22222

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 8. Identify whether the scanned number is even or odd and print an appropriate
message.

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


mod = num % 2
if mod > 0:
print("This is an odd number.")
else:
print("This is an even number.")

OUTPUT: Enter a number: 5


This is an odd number.

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 9. Create a program to find a maximum number among the given three
numbers.

# for a different result


num1 = 10
num2 = 14
num3 = 12

# uncomment following lines to take three numbers from user


#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)


OUTPUT: The largest number is 14

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 10. Develop a program to demonstrate the basic functionalities of a standard


calculator.

# Python program for simple calculator

print("Please select operation -\n" \


"1. Add\n" \
"2. Subtract\n" \
"3. Multiply\n" \
"4. Divide\n")

# Take input from the user


select = int(input("Select operations form 1, 2, 3, 4 :"))

number_1 = int(input("Enter first number: "))


number_2 = int(input("Enter second number: "))

if select == 1:
print(number_1, "+", number_2, "=", number_1+number_2)

elif select == 2:
print(number_1, "-", number_2, "=", number_1-number_2)

elif select == 3:
print(number_1, "*", number_2, "=", number_1*number_2)

elif select == 4:
print(number_1, "/", number_2, "=", number_1/number_2)
else:
print("Invalid input")

OUTPUT: Please select operation -


1. Add
2. Subtract

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

3. Multiply
4. Divide

Select operations form 1, 2, 3, 4 : 3


Enter first number: 10
Enter second number: 20
10 * 20 = 200

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 11. Write a python program to print 1 to 10 numbers using loops.

# Python Program to Print Natural Numbers within a range

minimum = int(input("Please Enter the Minimum integer Value : "))

maximum = int(input("Please Enter the Maximum integer Value : "))

print("The List of Natural Numbers from {0} to {1} are".format(minimum,

maximum))

for i in range(minimum, maximum + 1):

print (i, end = ' ')

OUTPUT: Please Enter the Minimum integer Value : 1


Please Enter the Maximum integer Value : 10
The List of Natural Numbers from 1 to 10 are
1 2 3 4 5 6 7 8 9 10

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 12. Develop a program to find odd and even numbers from 1 to N numbers.
(Where N is an integer number).

#Python program to print even numner from 1 to n


max=int(input("Please Enter the Maximum value: "))
for num in range(1, max+1):
if(num % 2 == 0):
print(num)
#Python program to print odd numner from 1 to n
max=int(input("Please Enter the Maximum value: "))
for num in range(1, max+1):
if(num % 2 != 0):
print(num)
OUTPUT: Please Enter the Maximum value: 10
2
4
6
8
10
Please Enter the Maximum value: 10
1
3
5
7
9

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 13. Write a program to show whether the entered number is prime or not.

# To take input from the user


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

# define a flag variable


flag = False
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True


if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
OUTPUT: Enter a number: 51
51 is not a prime number

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 14. Create a program to display the following patterns.


A. B.
1 *****
12 ****
123 ***
1234 **
12345 *

A. B.
rows = 6; rows = 5
# outer loop b=0
for i in range(rows): # reverse for loop from 5 to 0
# nested loop for i in range(rows, 0, -1):
for j in range(i): b += 1
# display number for j in range(1, i + 1):
print(j+1, end=' ') print("*", end=' ')
# new line after each row print('\r')
print('')
OUTPUT: 1 *****
12 ****

123 ***

1234 **

12345 *

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 15. Create a program to find a maximum number among the given three
numbers.

# Function for nth Fibonacci number

def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==0:
return 0
# Second Fibonacci number is 1
elif n==1:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)

num=int(input(print("Enter Number for Fibonacci Series:")));


print(Fibonacci(num))

OUTPUT: Enter Number for Fibonacci Series:10


55

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 16. Develop a user-defined function to find the factorial of a given number.

def factorial(num):#function definition


fact=1
for i in range(1, num+1):#for loop for finding factorial
fact=fact*i
return fact #return factorial

number=int(input("Please enter any number to find factorial: "))


result=factorial(number)#function call and assign the value to variable result
print("The factorial of %d = %d"%(number, result))

OUTPUT: Please enter any number to find factorial: 5


The factorial of 5 = 120

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 17. Write a program using the function that reverses the entered value.

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


revr_num = 0 # initial value is 0. It will hold the reversed number
def recur_reverse(num):
global revr_num # We can use it out of the function
if (num > 0):
Reminder = num % 10
revr_num = (revr_num * 10) + Reminder
recur_reverse(num // 10)
return revr_num

revr_num = recur_reverse(num)
print("n Reverse of entered number is = %d" % revr_num)
OUTPUT: Enter the number: 12345
n Reverse of entered number is = 54321

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 18. Write a program that determines whether a given number is an Armstrong
number or a palindrome using a user-defined function.

Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))


# Initializing Sum and Number of Digits
Sum = 0
Times = 0

# Calculating Number of individual digits

Temp = Number
while Temp > 0:
Times = Times + 1
Temp = Temp // 10

# Finding Armstrong Number

Temp = Number
while Temp > 0:
Reminder = Temp % 10
Sum = Sum + (Reminder ** Times)
Temp //= 10
if Number == Sum:
print("\n %d is Armstrong Number.\n" %Number)

else:

print("\n %d is Not a Armstrong Number.\n" %Number)

OUTPUT: Please Enter the Number to Check for Armstrong: 153


153 is Armstrong Number.

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 19. Write a program to find the length of a string.

# User inputs the string and it gets stored in variable str


str = input("Enter a string: ")

# using len() function to find length of str


print("Length of the input string is:", len(str))

OUTPUT: Enter a string: Chaitanya


Length of the input string is: 9

N.S.PATEL 7600031265 N.K.PATEL 9429235711


PRAYOSHA ENGINEERING CLASSES

Practical 20. Write a program to reverse words in a given sentence.

## initializing the string


string = "I am a python programmer"
## splitting the string on space
words = string.split()
## reversing the words using reversed() function
words = list(reversed(words))
## joining the words and printing
print(" ".join(words))
OUTPUT: programmer python a am I

N.S.PATEL 7600031265 N.K.PATEL 9429235711


Practical 21. Write a program to check if a substring is present in a given string.

def check(str1, sstr):


if (str1.find(sstr) == -1):
print(sstr,"IS NOT PRESENT IN THE GIVEN STRING")
else:
print(sstr,"IS PRESENT IN THE GIVEN STRING")
# Driver code
str1 = input("Enter the string ::>")
sstr=input("Enter Substring ::>")
check(str1, sstr)

OUTPUT: Enter the string ::> python program


Enter Substring ::> program
program IS PRESENT IN THE GIVEN STRING
Enter the string ::> python program
Enter Substring ::> programming
programming IS NOT PRESENT IN THE GIVEN STRING

You might also like