0% found this document useful (0 votes)
528 views18 pages

Python Practical

The document contains code snippets for various Python programming practical problems related to lists, strings, functions, conditionals, loops etc. Some key problems include: 1. Writing functions to check if a number is even/odd, generate Fibonacci series, reverse a number, check for Armstrong and palindrome numbers, and calculate factorial of a number. 2. Checking if a sentence is a pangram, printing elements of a list that are less than 5, checking if two lists have a common member. 3. Removing specified elements from a list, cloning a list.

Uploaded by

Divya Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
528 views18 pages

Python Practical

The document contains code snippets for various Python programming practical problems related to lists, strings, functions, conditionals, loops etc. Some key problems include: 1. Writing functions to check if a number is even/odd, generate Fibonacci series, reverse a number, check for Armstrong and palindrome numbers, and calculate factorial of a number. 2. Checking if a sentence is a pangram, printing elements of a list that are less than 5, checking if two lists have a common member. 3. Removing specified elements from a list, cloning a list.

Uploaded by

Divya Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

S.Y.BSc.

IT
SEMESTER-III
PYTHON PROGRAMMING PRACTICAL
Practical no:-1
A. Create a program that asks the user to enter their name and
their age. Print out a message addressed to them that tells
them the year that they will turn 100 years old.
Code:-
import datetime
name = input("Please enter your name: ")
print(name)
age = int(input("Enter your age: "))
year_now = datetime.datetime.now()
#print(year_now.year)
print("You will turn 100 in " + str(int(100-age) +
int(year_now.year)))
Output:-
B. Enter the number from the user and depending on whether
the number is even or odd, print out an appropriate message
to the user.
Code:-
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Output:-
C. Write a program to generate the Fibonacci series.
Code:-
a=0
b=1
c = a+b
d = int(input("Enter a number until you want to print fibonacci
series\n"))
while(d > c):
print("Fibonacci series is: ", c)
a=b
b=c
c = a+b
Output:-
D. Write a function that reverses the user defined value.
Code:-
def reverseNum(num) :
rev_num = 0
while(num) :
rem = num % 10
rev_num = rev_num* 10 + rem
num //= 10
return rev_num
num = int(input('Enter a number: '))
print('Reverse number is: ', reverseNum(num))

Output:-
E. Write a function to check the input value is Armstrong and
also write the function for Palindrome.
Code:-
Amstrong number:-
num = int(input("Enter a number :"))
sum = 0
temp = num
while temp>0:
digit = temp % 10
sum = sum+(digit*digit*digit)
temp = temp // 10
if num == sum:
print(num,"is an amstrong number")
else:
print(num,"is not an amstrong number")
Output:-
Palindrome number:-
n=int(input("Enter a number : "))
a=n
sum=0
while (n>0):
x=n%10
sum=(sum*10)+x
n//=10
if (a==sum):
print("the givem number is palindrome")
else:
print("the given number is not palindrome")
Output:-
F. Write a recursive function to print the factorial for a given
number.
Code:-
def factorial(x):
if(x) == 1:
return 1
else:
return (x*factorial(x-1))
num = int(input('Enter a number: '))
print("The factorial of",num,"is",factorial(num))
Output:-
Practical No.2
A. Write a function that takes a character (i.e. a string of length
1) and returns True if it is a vowel, False otherwise.
Code:-
def find_vowel(s):
l=['a','e','i','o','u']
if s in l:
print('True')
else:
print('False')
find_vowel('u')
Output:-
B. Define a function that computes the length of a given list or
string.
Code:-
def len_s(s):
count=0
for i in s:
count=count+1
print(count)
z=input("Enter a string : ")
len_s(z)
Output:-
C. Define a procedure histogram() that takes a list of integers
and prints a histogram to the screen. For example,
histogram([4, 9, 7]) should print the following:-
****
*********
*******
Code:-
def histogram(lst):
for i in lst:
print(i * '*')

lst=[]
ln=int(input("Enter the list length:- "))
print("Enter Integer ",ln)
for i in range(ln):
data=int(input())
lst.append(data)

histogram(lst)

Output:-
Practical No.3
Write the program for the following: (by using list)
A. A pangram is a sentence that contains all the letters of the
English alphabet at least once, for example: The quick brown
fox jumps over the lazy dog.
Your task here is to write a function to check a sentence to see
if it is a pangram or not.

Code:-
import string, sys
if sys.version_info[0] < 3:
input = raw_input
def ispangram(sentence, alphabet=string.ascii_lowercase):
alphaset = set(alphabet)
return alphaset<= set(sentence.lower())
print ( ispangram(input('Sentence: ')) )

Output:-
B. Take a list, say for example this one: a = [1, 1, 2, 3, 5, 8, 13,
21, 34, 55, 89] and write a program that prints out all the
elements of the list that are less than 5.

Code:-
l1=[1,1,2,3,5,8,13,21,34,55,89]
l2=[]
for i in l1:
if i <5:
l2.append(i)
print (l2)

Output:-

Practical No.4
Write the program for the following: (by using list)
A. Write a program that takes two lists and returns True if they
have at least one common member.

Code:-
l1=[1,2,3,4,5,6,]
l2=[11,12,13,14,15,6]
for i in l1:
for j in l2:
if i==j:
print ('The 2 list have at least one common element')

Output:-

B. Write a Python program to print a specified list after removing the


0th, 2nd, 4th and 5th elements.
Code:-
list=[0,1,2,3,4,5,6,7,8]
print("Original list :- " , list)

for i in list:
if i not in (0,2,4,5):
print (i,end=" ")

Output:-

C. Write a Python program to clone or copy a list.

Code:-
l1=[2, 4, 7, 8, 9, 0]
print ("Original List is", l1)
l2=l1
print ("Clone List is ",l2)

Output:-

You might also like