DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING [Link]. (IT) VII SEMESTER
PYTHON PROGRAMMING LAB
Exercise:1 1 Steps to install python on windows and Unix platform
Basics 2 Running instructions in Interactive interpreter and a Python Script
3 Write a program to purposefully raise Indentation Error and Correct
it
Exercise: 2 4 Write a program that takes 2 numbers as command line arguments
programs and prints its sum
on 5 Implement python script to show the usage of various operators
operators available in python language
& I/O 6 Implement python script to read person’s age from keyboard and
operations. display whether he is eligible for voting or not.
7 Implement python script to check the given year is leap year or not.
Exercise 3: 8 Write a program for checking the given number is even or odd.
programs 9 Using a for loop, write a program that prints the decimal
on basic equivalents of 1/2, 1/3, 1/4,.. 1/10
control 10 Write a program for displaying reversal of a number.
structures 11 Write a program for finding biggest number among 3numbers.
&loops. 12 Write a program using a while loop that asks the user for a number,
and prints a countdown from that number to zero.
13 Develop a program that will read a list of student marks in the
range 0 ... 50 and tell you how many students scored in each range
of 10. how many scored 0 - 9, how many 10 -19, 20 - 29 ... and so
on.
Input: Enter list of Marks: 11 42 33 42 13 3 43
Output:
No of Students Between 1-10: 1
No of Students Between 11-20: 2
No of Students Between 21-30: 0
No of Students Between 31-40: 1
No of Students Between 41-50: 1
Exercise 4: 14 Implement Python Script to generate first N natural numbers
programs 15 Implement Python Script to check given number is palindrome or
on Python not
Script 16 Implement Python script to print factorial of a number
17 Implement Python Script to print sum of N natural numbers
18 Implement Python Script to check given number is Armstrong or
not.
19 Implement Python Script to generate prime numbers series up to n
1. Steps to install python on windows and Unix platform
Download Python 3.10.x
[Link]
How to install Python:
Python installation is pretty simple, you can install it on any operating system such as
Windows, Mac OS X, Ubuntu etc. Just follow the steps
Local Environment Setup: Open a terminal window and type "python" to
find out if it is already installed and which version is installed.
Getting Python for Windows platform: Binaries of latest version of Python 3
(Python 3.10) are available in [Link]
The following installation options are available.
Windows x86-64 executable installer
Windows x86 executable installer
Download the software and save to hard drive.
Double click on the executable file and install software as per instructions.
By default, IDLE will install in to the system as default IDE (Integrated development
Environment).
After installing software, it is very important to set the path if path is not set by
default. Above 3 versions are not necessary to set the path. It will set automatically
by default
Installation under Linux
Most Linux distributions already contain Python in them.
To Check the python installed or not; run the command
$ python3 –version
To install the python run the command
$ sudo apt-get install python3.8
2. Running instructions in Interactive interpreter and a Python Script
Two Modes of working with Python.
Interactive Shell Mode
Scripting Mode
1. Run the python script
$ python [Link]
2. Open up the Interpreter
$ python
>>> print("hello")
Hello
>>> 3+5
8
>>> 4-2
2
>>> 8/2
4.0
3. Write a program to purposefully raise Indentation Error and Correct
it Program:
n1=int(input("enter n1 value"))
n2=int(input("enter n2 value"))
if n1>n2:
print("n1 is big")
else:
print("n2 is big")
Output:
Error: Excepted an indented block
n1=int(input("enter n1 value"))
n2=int(input("enter n2 value"))
if n1>n2:
print("n1 is big")
else:
print("n2 is big")
Output:
enter n1 value10
enter n1 value20
n2 is big
4. Write a program [Link] that takes 2 numbers as command line arguments
and prints its sum.
Program:
step 1: open notepad and write the below
program import sys;
n1=int([Link][1]);
n2=int([Link][2]);
print (n1+n2)
step 2: SAVE
step 3: Open DOS SHELL and go to file saved location (D:\)
step 4: python [Link]
argument1 argument2
Output:
python [Link]
10 20
30
5. Implement python script to show the usage of various operators available
in python language
Program:
Example 1: Arithmetic operators in Python
x = 15
y=4
print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y =',x**y)
Output
x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625
Example 2: Comparison operators in Python
x = 10
y = 12
print('x > y is',x>y)
print('x < y is',x<y)
print('x == y is',x==y)
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)
Output
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True
Example 3: Logical Operators in Python
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Output
x and y is False
x or y is True
not x is False
Example 4: Identity operators in Python
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
print(x1 is not y1)
print(x2 is y2)
print(x3 is y3)
Output
False
True
False
Example 5: Membership operators in Python
x = 'Hello world'
y = {1:'a',2:'b'}
print('H' in x)
print('hello' not in x)
print(1 in y)
print('a' in y)
Output
True
True
True
False
6. Implement python script to read person’s age from keyboard and display whether
he is eligible for voting or not.
Program:
age=int(input(“Enter age:”))
if age>=18:
print(“Eligible for voting”)
else:
print(“ Not eligible for voting”)
Output:
Enter age: 18
Eligible for voting
Enter age: 17
Not eligible for voting
7. Implement python script to check the given year is leap year or not.
Program:
year =int(input(“Enter year”)
if year % 4 == 0:
print("Year is Leap")
else:
print("Year is not Leap")
Output:
Enter year:2000
Year is Leap
Enter year:2001
Year is not Leap
8. Write a Program for checking whether the given number is an even number or
not Program:
n=int(input("Enter a number:"))
if n % 2 == 0:
print ("EVEN Number")
else:
print ("ODD Number")
Output:
Enter a number:10
EVEN Number
Enter a number:11
ODD Number
9. Using a for loop, write a program that prints the decimal equivalents of 1/2,
1/3, 1/4,.. 1/10
Program:
i=1;
for j in range(2,10):
print("i:",i, "j:",j)
print(i, "/=", j)
print (i/j);
Output :
i: 1 j: 2
1/2
0.5
i: 1 j: 3
1/3
0.3333333333333333
i: 1 j: 4
1/4
0.25
i: 1 j: 5
1/5
0.2
i: 1 j: 6
1/6
0.16666666666666666
i: 1 j: 7
1/7
0.14285714285714285
i: 1 j: 8
1/8
0.125
i: 1 j: 9
1/9
0.1111111111111111
10. Write a program for displaying reversal of a number
Program:
num = 1234
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: " + str(reversed_num))
Output
4321
11. Write a program for finding biggest number among 3numbers
Program:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
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 :
Enter first number: 10
Enter second number: 12
Enter third number: 14
The largest number is 14.0
Enter first number: -1
Enter second number: 0
Enter third number: -3
The largest number is 0.0
12. Write a program using a while loop that asks the user for a number, and prints
a countdown from that number to zero
Program:
num=int(input("Enter your number"))
while(num>=0):
print(num)
num=num-1
Output :
Enter your number 10
10
9
8
7
6
5
4
3
2
1
0
13. Develop a program that will read a list of student marks in the range 0 ... 50 and
tell you how many students scored in each range of 10. how many scored 0 - 9, 10 -
19, 20 - 29 ... and so on.
Input: Enter list of Marks: 11 42 33 42 13 3 43
Output:
No of Students Between 1-10: 1
No of Students Between 11-20: 2
No of Students Between 21-30: 0
No of Students Between 31-40: 1
No of Students Between 41-50: 1
Program:
marks=input("Enter list of marks").split()
print(marks)
count1=count2=count3=count4=count5=0
for i in marks:
x=int(i)
if x<=10:
count1 += 1
elif x<=20:
count2 += 1
elif x<=30:
count3 +=1
elif x<=40:
count4 +=1
else:
x<=50
count5+=1
print("No of Students Between 1-10:",count1)
print("No of Students Between 11-20:",count2)
print("No of Students Between 21-30: ",count3)
print("No of Students Between 31-40:", count4)
print("No of Students Between 41-50:",count5)
Output:
Enter list of marks 11 42 33 42 13 3 43
['11', '42', '33', '42', '13', '3', '43']
No of Students Between 1-10: 1
No of Students Between 11-20: 2
No of Students Between 21-30: 0
No of Students Between 31-40: 1
No of Students Between 41-50: 3
14. Implement Python Script to generate first N natural numbers
Program:
num = int(input("Enter any number : "))
print("\nNatural numbers from 1 to", num)
for i in range(1, num + 1):
print(i, end=" ")
Output
Enter any number : 10
Natural numbers from 1 to 10 1 2 3 4 5 6 7 8 9 10
15. Implement Python Script to check given number is palindrome or not.
Program:
num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if(temp==rev):
print("The number is palindrome!")
else:
print("Not a palindrome!")
Output:
Enter a number:121
The number is palindrome!
16. Implement Python script to print factorial of a number
Program:
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print(" Factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Output:
Enter a number: 10
The factorial of 10 is 3628800
17. Implement Python Script to print sum of N natural numbers.
Program:
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)
output:
Enter a number: 100
The sum is 5050
18. Implement Python Script to check given number is Armstrong or not
Program:
# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output:
Enter a number: 663
663 is not an Armstrong number
Enter a number: 407
407 is an Armstrong number
19. Implement Python Script to generate prime numbers series up to n
Program:
lower = int(input("Enter the lower value:"))
upper = int(input("Enter the upper
value:")) for number in
range(lower,upper+1):
if number>1:
for i in range(2,number):
if (number%i)==0:
break
else:
print(number)
Output:
Enter the lower value:0
Enter the upper value:20
2
3
5
7
11
13
17
19