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

Python (Lab1+2)

This document is a practical file for Python programming submitted by a student at Panjab University. It includes a list of programming tasks with corresponding code examples and outputs, covering various topics such as logical operations, trigonometric functions, Fibonacci sequences, and string manipulations. The file serves as a record of programming exercises completed during the 5th semester of the B.E. (IT) course.

Uploaded by

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

Python (Lab1+2)

This document is a practical file for Python programming submitted by a student at Panjab University. It includes a list of programming tasks with corresponding code examples and outputs, covering various topics such as logical operations, trigonometric functions, Fibonacci sequences, and string manipulations. The file serves as a record of programming exercises completed during the 5th semester of the B.E. (IT) course.

Uploaded by

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

PRACTICAL FILE

Python Programming
(IT551c)
B.E. (IT) 5th Semester

Submitted To: Submitted By:

Sukhvir Sir Shubham Kumar


UE228093
IT SECTION 2

Department Of Information Technology


University Institute Of Engineering & Technology
Panjab University,Chandigarh
List Of Programs

S.NO LIST OF PROGRAM DATE OF DATE OF REMARK


PERFORM SUBMISSION

1. Write a program to perform logical AND ,


OR and NOT operations.

2. Write a Program to print sin, cos, tan of


angle input as degrees.

3. Write a program to print an even number


until given n.

4. Write a Program to find the product of


digits of a number.

5. Write a Program to print first n terms of a


fibonacci sequence.

6. Write a Program to print first n terms of a


fibonacci sequence using a Dictionary.

7. Write a Program to find factorial without


recursion.

8. Write a Program to find factorial using


recursion.

9. Write a program to perform find, rfind and


count in strings.

10. Write a program to find indexes of all


occurances of a substring in a given
string.

11. Write a program to count all occurrences


of a substring in a given string.

12. Write a program to find sum of all natural


number between 1 to n using recursion.
13. Write a program to find the reverse of a
number.

14. Write a program to find Contact Number


Using a Dictionary.
1.Write a program to perform logical AND , OR and NOT
operations.

a = 5
b = 6
print(bool(a and b))
print(bool(a or b))
print(a and b)
print(a or b)
print(not(a and b))

OUTPUT:

2.Write a Program to print sin, cos, tan of angle input as


degrees.
import math
x = 90

print(math.sin(x))
y = math.radians(x)

print("radians",y)
print("sin",int(math.sin(y)))
print("cos",int(math.cos(y)))
print("tan",int(math.tan(y)))

OUTPUT:

3.Write a program to print an even number until given n.

n = int(input("Enter Any Number:"))


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

OUTPUT:
4.Write a Program to find the product of digits of a number.

N = int(input("Enter N:"))
multiply = 1
while N:
multiply = multiply * int(N % 10)
N = N // 10

print(f"multiplication of number are:", multiply)

OUTPUT:

5.Write a Program to print first n terms of a fibonacci


sequence.

nterms = int(input("How many terms? "))

n1, n2 = 0, 1
count = 0

if nterms <= 0:
print("Please enter a positive integer")

elif nterms == 1:
print("Fibonacci sequence upto", nterms, ":")
print(n1)

else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2

n1 = n2
n2 = nth
count += 1

OUTPUT:
6.Write a Program to print first n terms of a fibonacci
sequence using a Dictionary.

num = int(input("Enter n:"))


dic = {0: 0, 1: 1}

print("Fibonacci(", 0, ") terms =", dic[0])


print("Fibonacci(", 1, ") terms =", dic[1])
for i in range(2, num):
dic[i] = dic[i - 1] + dic[i - 2]
print("Fibonacci(", i, ") terms =", dic[i])

OUTPUT:
7.Write a Program to find factorial without recursion.

import math

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


print(math.factorial(a))

OUTPUT:

8.Write a Program to find factorial Using recursion.

def fun(n):
if n == 0:
return 1
else:
return n * fun(n - 1)

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


print(fun(num))
OUTPUT:

9.Write a program to perform find, rfind and count in strings.

Str1 = "shubhamshubhamshubham"
Str2 = "sh"

print(Str1.count(Str2))

Str1 = "UIET Panjab University UIET Chandigarh UIET"


Str2 = "UIET"

print("First Position:", Str1.find(Str2), "Count:",


Str1.count(Str2))
print(Str1.rfind(Str2))

OUTPUT:
10.Write a program to find indexes of all occurances of a
substring in a given string.

str1 = input("Enter A String:")


str2 = input("Enter A Substring That You Want To Search:")
i = 0
while True:
index = str1.find(str2, i)
if index == -1:
# print("No Match")
break
# print("Index")
print("{} occurs at index {}".format(str2, index))
i = index + 1

OUTPUT:

11.Write a program to count all occurrences of a substring


in a given string.

s = input("Enter String:")
sub = input("Enter Substring:")
print(s.count(sub))
l = []
l = s.split(" ")
count = 0
for i in l:
if i == sub:
count += 1

OUTPUT:

12.Write a program to find sum of all natural number between


1 to n using recursion.

def recur_sum(n):
if n <= 1:
return n
else:
return n + recur_sum(n - 1)

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

if num < 0:
print("Enter a positive number")
else:
print("The sum is", recur_sum(num))
OUTPUT:

13.Write a program to find the reverse of a number.

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


reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("Reversed Number: " + str(reversed_num))

OUTPUT:
14.Write a program to find Contact Number Using a
Dictionary.

name = ("Shubham", "Muskan", "Aaditya", "Saloni")


contact = (121, 123, 145, 432)
d = dict(zip(name, contact))
x = input("Enter name to find His/Her Contact Number:")
print(d[x])

Information = {
"Shubham": 121,
"Muskan": 123,
"Aaditya": 145,
"Saloni": 432
}

name = input("Enter name to find His/Her Contact Number:")


print(Information[name])

OUTPUT:

You might also like