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

Python Lab Manual

division by zero

Uploaded by

karimunisa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Python Lab Manual

division by zero

Uploaded by

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

Vishnupur, Bhimavaram

I - MCA II - SEMESTER

PYHTON LAB MANUAL

DEPARTMENT OF MCA
INDEX

S. No Name of Programs
1 Write python a program that takes inputs and prints its sum,
Multiplication, subtraction, division, modulus..etc
2 Write a python program to find the square root of a number
by Newton’s Method.
3 Write a python program to biggest of three numbers?

4 Write a python program to find sum of digits of a given number

5 Write a python program to find GCD of two numbers?

6 Write a python program to print the following pattern.


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
7 Write a python program to find factorial of a given number

8 Write a python program to print all the prime numbers below the given
number
9 Write a python program to count the number of characters in the string
using loop?
10 Write a python program to read a string from the user and print lowercase
character in uppercase and uppercase character in lowercase.
11 Write a python program to perform linear Search

12 Write a python program to perform Binary Search

13
Write a python program to perform Bubble Sort

14 Write a python program to perform Selection Sort

15 Write a python program to demonstrate try with multiple exception


Statements
Program: 1
Write python a program that takes inputs and prints its sum, multiplication,
subtraction, division, modulus..etc
Arithmetic Operators are used to perform mathematical operations like addition, subtraction,
multiplication and division.
1. Addition Operator : In Python, + is the addition operator. It is used to add 2 values.
2. Subtraction Operator : In Python, – is the subtraction operator. It is used to subtract the second
value from the first value.
3. Multiplication Operator : In Python, * is the multiplication operator. It is used to find the product
of 2 values.
4. Division Operator : In Python, / is the division operator. It is used to find the quotient when first
operand is divided by the second.
5. Modulus Operator : In Python, % is the modulus operator. It is used to find the remainder when
first operand is divided by the second.
6. Exponentiation Operator : In Python, ** is the exponentiation operator. It is used to raise the first
operand to power of second.
7. Floor Division : In Python, // is used to conduct the floor division. It is used to find the floor of the
quotient when first operand is divided by the second.
OUTPUT:

Program: 2
Write a python program to find the square root of a number by Newton’s Method.
The square root of a number is defined as the value, which gives the number when
it is multiplied by itself. The radical symbol √ is used to indicate the square root. For
example, √16 = 4.

Newton’s Method: Let N be any number then the square root of N can be given by the
formula: root = 0.5 * (X + (N / X))
def newton_method(number, number_iters = 100):

a = float(number)

for i in range(number_iters):

number = 0.5 * (number + a / number)

return number

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

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

print("Square root of first number:",newton_method(a))

print("Square root of second number:",newton_method(b))


OUTPUT:

Program :3
Write a python program to biggest of three numbers?

num1 = int(input(“enter the value”))


num2 = int(input(“enter the value”))
num3 = int(input(“enter the value”))
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:

Program: 4
Write a python program to find sum of digits of a given number

def getSum(n):

sum=0

for digit in str(n):

sum+=int(digit)

return sum

n=12345
print(getSum(n))
OUTPUT:

15

Program: 5
Write a python program to find GCD of two numbers?

def hcf(a, b):

if(b == 0):

return a

else:

return hcf(b, a % b)

a = 60

b = 48

print("The gcd of 60 and 48 is : ", end="")

print(hcf(60, 48))
OUTPUT:

The gcd of 60 and 48 is : 12

Program: 6
Write a python program to print the following pattern.
1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

n=int(input())

for i in range(1,n+1):

left-space p=” “+(n-i)


numbers=” “

for j in range(1,i+1):

numbers=numbers + (str(i)+” “)

print(left-space + numbers)
OUTPUT:
1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

Program: 7
Write a python program to find factorial of a given number

n=23

fact=1

for i in range(1,n+1):

fact=fact*i

print("The factorial of 23 is : ",end=" ")

print(fact)
OUTPUT:

The factorial of 23 is : 2.58520

Program: 8
Write a python program to print all the prime numbers below the given number

lower_value = int(input("Enter the lower_value : "))

upper_value = int(input("Enter the upper_value : "))

for n in range(lower_value,upper_value + 1):

if n > 1:

for i in range(2,n):
if (n % i) == 0:

break

else:

print(n)
OUTPUT:

Enter the lower_value :2

Enter the upper_value : 20

2 3 5 7 11 13 17 19

Program: 9
Write a python program to count the number of characters in the string
using loop?

str1 = input("Please Enter your Own String : ")

total = 0

for i in str1:

total = total + 1

print ("Total Number of Characters in this String = ", total)


OUTPUT:

Please Enter your Own String: BVRAJUCOLLEGE

Total Number of Characters in this String =13

Program: 10
Write a python program to read a string from the user and print lowercase
character in uppercase and uppercase character in lowercase.

str1="Great Power"

newStr = ""

for i in range(0, len(str1)):

if str1[i].islower():

newStr += str1[i].upper()
elif str1[i].isupper():

newStr += str1[i].lower()

else:

newStr += str1[i]

print("String after case conversion : " + newStr)


OUTPUT:

String after case conversion : gREAT pOWER

Program: 11
Write a python program to perform linear Search

def linearSearch(array, n, x):

# Going through array sequencially

for i in range(0, n):

if (array[i] == x):

return i

return -1

array = [2, 4, 0, 1, 9]

x=1

n = len(array)

result = linearSearch(array, n, x)

if(result == -1):

print("Element not found")

else:

print("Element found at index: ", result)


OUTPUT:

Element found at index:3


Program: 12
Write a python program to perform Binary Search

nums = []
print("Enter 10 Numbers (in ascending order):")
for i in range(10):
nums.insert(i, int(input()))
print("Enter a Number to Search:")
search = int(input())
first = 0
last = 9
middle = (first+last)/2
middle = int(middle)
while first <= last:
if nums[middle]<search:
first = middle+1
elif nums[middle]==search:
print("The Number Found at Position:")
print(middle+1)
break
else:
last = middle-1
middle = (first+last)/2
middle = int(middle)
if first>last:
print("The Number is not Found in the List")
OUTPUT:
Program: 13
Write a python program to perform Bubble Sort
def bubblesort(elements):

swapped = False

for n in range(len(elements)-1, 0, -1):

for i in range(n):

if elements[i] > elements[i + 1]:

swapped = True

elements[i], elements[i + 1] = elements[i + 1], elements[i]

if not swapped:

return

elements = [39, 12, 18, 85, 72, 10, 2, 18]

print("Unsorted list is,")

print(elements)

bubblesort(elements)

print("Sorted list is, ")

print(elements)
OUTPUT:

Unsorted list is,

[39,12,18,85,72,10,2,18]

Sorted list is,

[2,10,12,18,18,39,72,85]
Program: 14
Write a python program to perform Selection Sort

def selectionSort(array, size):

for ind in range(size):

min_index = ind

for j in range(ind + 1, size):

if array[j] < array[min_index]:

min_index = j

(array[ind], array[min_index]) = (array[min_index], array[ind])

arr = [-2, 45, 0, 11, -9,88,-97,-202,747]

size = len(arr)

selectionSort(arr, size)

print('The array after sorting in Ascending Order by using selection sort is:')

print(arr)
OUTPUT:

The array after sorting in Ascending Order by using selection sort is:

[-202, -97, -9, -2, 0, 11, 45, 88, 747]


Program: 15
Write a python program to demonstrate try with multiple exception

Statements

Eg:1

print('Hi,This is python')

print('This exception program')

a=int(input('Enter a value'))

b=int(input('Enter b value'))

print(a/b)

print(a+b)

print(a-b)

print("thank you")

OUTPUT:1

Hi,This is python

This exception program

Enter a value25

Enter b value5

5.0

30

20

thank you

OUTPUT:2

Hi,This is python

This exception program

Enter a value52

Enter b value0
Traceback (most recent call last):

File "F:/ecc22.py", line 5, in <module>

print(a/b)

ZeroDivisionError: division by zero

Eg:2

print('Hi,This is python')

print('This exception Handling program')

a=int(input('Enter a value'))

b=int(input('Enter b value'))

try:

print(a/b)

print(a+b)

print(a-b)

except Exception as e:

print(e)

print("thank you")

OUTPUT:1

Hi,This is python

This exception Handling program

Enter a value25

Enter b value3

8.333333333333334

28

22

thank you
OUTPUT:2

Hi,This is python

This exception Handling program

Enter a value25

Enter b value0

division by zero

thank you

Eg:3

try:

import fabric

except TypeError:

print('Adding number and string is not possible')

except NameError:

print('Variable not defined')

except ZeroDivisionError:

print('Division with zero is not possible')

except ModuleNotFoundError:

print("please install fabric module to use it")

except Exception as e:

print(e)

finally:

print("This will execute always")

OUTPUT:1

please install fabric module to use it

This will execute always


Eg:4

try:

print(5/0)

except TypeError:

print('Adding number and string is not possible')

except NameError:

print('Variable not defined')

except ZeroDivisionError:

print('Division with zero is not possible')

except ModuleNotFoundError:

print("please install fabric module to use it")

except Exception as e:

print(e)

finally:

print("This will execute always")

OUTPUT:1

Division with zero is not possible

This will execute always

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy