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

III-bsc Python Lab-Record

The document contains source code for various Python programming exercises including: 1. A program to find the area and perimeter of a circle using both procedural and OOP approaches. 2. A program to generate the Fibonacci series given the number of terms. 3. A program to compute the greatest common divisor (GCD) of two numbers. 4. A program to generate the first n prime numbers. 5. A program to find the sum of squares of n natural numbers. 6. A program to find the sum of elements in an array. 7. A program to find the largest element in an array. 8. A program to check if a string is a palindrome. 9

Uploaded by

RCW CS 18
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)
56 views

III-bsc Python Lab-Record

The document contains source code for various Python programming exercises including: 1. A program to find the area and perimeter of a circle using both procedural and OOP approaches. 2. A program to generate the Fibonacci series given the number of terms. 3. A program to compute the greatest common divisor (GCD) of two numbers. 4. A program to generate the first n prime numbers. 5. A program to find the sum of squares of n natural numbers. 6. A program to find the sum of elements in an array. 7. A program to find the largest element in an array. 8. A program to check if a string is a palindrome. 9

Uploaded by

RCW CS 18
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/ 22

III III-B.Sc.

,(CS) - PYTHON PROGRAMMING LAB

1.Write a Python program to find the area and perimeter of a circle.

SOURCE CODE WITHOUT CLASS

# Python program to find the


# area and perimeter of a circle in python

from math import pi

# Getting input from user


R = float(input("Enter radius of the circle: "))

# Finding the area and perimeter of the circle


area = (pi*R*R)
perimeter = (2*pi*R)

# Printing the area and perimeter of the circle


print("The area of circle is ", "%.2f" %area)
print("The perimeter of circle is", "%.2f" %perimeter)

SOURCE CODE WITH CLASS

import math
class circle():
def __init__(self,radius):
self.radius=radius
def area(self):
return math.pi*(self.radius**2)
def perimeter(self):
return 2*math.pi*self.radius

r=int(input("Enter radius of circle: "))


obj=circle(r)
print("Area of circle:",round(obj.area(),2))
print("Perimeter of circle:",round(obj.perimeter(),2))

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

2. Write a Python program to generate Fibonacci series

SOURCE CODE

n_terms = int(input ("How many terms the user wants to print? "))
# First two terms
n_1 = 0
n_2 = 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(n_1)
# Then we will generate Fibonacci sequence of number
else:
print ("The fibonacci sequence of the numbers is:")
while count < n_terms:
print(n_1)
nth = n_1 + n_2
# At last, we will update values
n_1 = n_2
n_2 = nth
count += 1

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

3. Write a Python program to compute the GCD of two numbers


SOURCE CODE

def GCD_Loop( a, b):


if a > b: # define the if condition
temp = b
else:
temp = a
for i in range(1, temp + 1):
if (( a % i == 0) and (b % i == 0 )):
gcd = i
return gcd
x = int(input (" Enter the first number: ") ) # take first no.
y =int (input (" Enter the second number: ")) # take second no.
num = GCD_Loop(x, y) # call the gcd_fun() to find the result
print("GCD of two number is: ")
print(num) # call num

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

4. Write a Python program to generate first n prime numbers

SOURCE CODE

prime=0
def primenum(x):
if x>=2:
for y in range(2,x):
if not(x%y):
return False
else:
return False
return True
lower_value = int(input ("Please, Enter the Lowest Range Value: "))
upper_value = int(input ("Please, Enter the Upper Range Value: "))
print ("The Prime Numbers in the range are:")
for i in range(lower_value,upper_value+1):
if primenum(i):
prime+=1
print(i)
print("We found "+str(prime)+ " prime numbers.")

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

5. Write a Python program to find the sum of squares of n natural numbers

OUTPUT

#METHOD 1

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

# calculating sum of square


sumVal = 0
for i in range(1, N+1):
sumVal += (i*i)

print("Sum of squares = ", sumVal)

#METHOD 2
N = int(input("Enter value of N: "))

# calculating sum of square


sumVal = (int)( (N * (N+1) * ((2*N) + 1))/6 )
print("Sum of squares =",sumVal)

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

6. Write a Python program to find the sum of the elements in an array


SOURCE CODE

METHOD 1 : USING LOOP

def array_summer(arr):

total = 0

for item in arr:

# shorthand of total = total + item

total += item

return total

# Test input

print(array_summer([1, 2, 3, 3, 7]))

METHOD 2 : USING PYTHON BUILT-IN FUNCTION SUM()

def array_summer(arr):

return sum(arr)

# Test input

print(array_summer([1, 2, 3, 3, 7]))

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

7. Write a Python program to find the largest element in the array


SOURCE CODE

#Python program to find the largest element of the array using 'for' loop
lst = []
num = int(input("Enter the size of the array: "))
print("Enter array elements: ")
for n in range(num):
numbers = int(input())
lst.append(numbers)
large = lst[0]
for n in range(num):
if(lst[n] > large):
large = lst[n]
print("The largest element of the given list is [USING FOR LOOP]:", large)

#Python program to find the largest element of the array using the built-in function
lst = []
num = int(input("Enter the size of the array: "))
print("Enter array elements: ")
for n in range(num):
numbers = int(input())
lst.append(numbers)
print("The largest element of the given list is [USING BUILT-IN
FUNCTION]:",max(lst))

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

8. Write a Python program to check if the given string is a palindrome or not

SOURCE CODE

#PALINDROME CHECKING USING NUMBERS


num = int(input("Enter a value:"))
temp = num
rev = 0
while(num > 0):
dig = num % 10
rev = rev * 10 + dig
num = num // 10
if(temp == rev):
print("This value is a palindrome number!")
else:
print("This value is not a palindrome number!")

#PALINDRONE CHECKING USING NAMES


string=raw_input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

9. Write a Python program to store strings in a list and print them


SOURCE CODE

# SPLIT()
str_val1 = "Let us study python programming."
#using split()
print("SPLIT()")
print("*******")
print(str_val1.split())

# SPLIT()WITH A SEPERATOR
str_val1="Let @ us @ study @ python @ programming."
str_val2="Before # we # learn # basic # knowledge # of # computers."
str_val3="So $ first $ study $ what $ is $ IPO $ cycle."
str_val4="Then % learn % about % the % generation % of % computers."
# Using split()
print("SPLIT() WITH A SEPERATOR")
print("************************")
print(str_val1.split("@"))
print(str_val2.split("#"))
print(str_val3.split("$"))
print(str_val4.split("%"))

# STRIP()
str_val1 = "Let us study python programming."
# Using list()
print("STRIP()")
print("*****")
print(list(str_val1.strip()))

# MAP()
str_val1="Let us study programming."
#using split()
str_val1 = str_val1.split()
list_str1 = list(map(list,str_val1))
#displaying the list values
print("MAP()")
print("*****")
print(list_str1)

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

SOURCE CODE

# list example in detail


emp = [ "John", 102, "USA"]

Dep1 = [ "CS",10]
Dep2 = [ "IT",11]

HOD_CS = [ 10,"Mr. Holding"]


HOD_IT = [11, "Mr. Bewon"]

print("printing employee data ...")


print(" Name : %s, ID: %d, Country: %s" %(emp[0], emp[1], emp[2]))
print("printing departments ...")
print("Department 1:\nName: %s, ID: %d\n Department 2:\n Name: %s, ID: %s"%

( Dep1[0], Dep2[1], Dep2[0], Dep2[1]))


print("HOD Details ....")

print("CS HOD Name: %s, Id: %d" %(HOD_CS[1], HOD_CS[0]))


print("IT HOD Name: %s, Id: %d" %(HOD_IT[1], HOD_IT[0]))

print(type(emp), type(Dep1), type(Dep2), type(HOD_CS), type(HOD_IT))

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

10. Write a Python program to find the length of a list, reverse it, copy it and
then clear it.
SOURCE CODE

# LENGTH OF A LIST
print("LENGTH OF A LIST")
print("****************")
test_list = [1, 4, 5, 7, 8]
print("The list is : " + str(test_list))
counter = 0
for i in test_list:
counter = counter + 1
print("Length of list using naive method is : " + str(counter))
# REVERSE A LIST
print("REVERSE OF A LIST")
print("****************")
test_list.reverse()
print(test_list)
# ORIGINAL LIST
print("ORIGINAL LIST")
print("*************")
test_list.reverse()
print(test_list)
# COPY OR CLONE A LIST
print("COPY OR CLONE A LIST")
print("********************")
def Cloning(test_list):
li_copy = test_list
return li_copy
li2 = Cloning(test_list)
print("Original List:", test_list)
print("After Cloning:", li2)
# CLEAR A LIST
print("CLEAR A LIST")
print("************")
del test_list[:]
# Updated prime_numbers List
print('List after clear():', test_list)

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM


III III-B.Sc.,(CS) - PYTHON PROGRAMMING LAB

OUTPUT

RAJESWARI COLLEGE OF ARTS AND SCIENCE FOR WOMEN, BOMMAYAPALYAM

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