III-bsc Python Lab-Record
III-bsc Python Lab-Record
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
OUTPUT
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
OUTPUT
OUTPUT
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.")
OUTPUT
OUTPUT
#METHOD 1
#METHOD 2
N = int(input("Enter value of N: "))
OUTPUT
def array_summer(arr):
total = 0
total += item
return total
# Test input
print(array_summer([1, 2, 3, 3, 7]))
def array_summer(arr):
return sum(arr)
# Test input
print(array_summer([1, 2, 3, 3, 7]))
OUTPUT
#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))
OUTPUT
SOURCE CODE
OUTPUT
# 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)
OUTPUT
SOURCE CODE
Dep1 = [ "CS",10]
Dep2 = [ "IT",11]
OUTPUT
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)
OUTPUT