You are on page 1of 25

BTCS-407 PROGRAMMING WITH PYTHON

SHRI VAISHNAV VIDYAPEETH VISHWAVIDYALAYA, INDORE

Shri Vaishnav Institute of Information Technology

Department of Computer Science & Engineering

(CSE)

Session :- July-Dec-2021

3rd Year /5th Sem

Subject - Programming with Python

Course code - BTCS-407

Section -F

Submitted by - Nayan Sharma

Enrollment No- 19100BTCSES05384

Submitted to - Himanshu Panadiwal Sir

INDEX
19100BTCSES05384 NAYAN SHARMA
BTCS-407 PROGRAMMING WITH PYTHON

S.NO. Name of Date


Experiment Of
Submission
1. Exercise programs on basic control structures & loops. 26/11/21
1.
a) Write a program for checking the given number is even orodd.

b) Write a program for displaying reversal of anumber.

2. 2. Exercise programs on operators & I/O operations. 26/11/21


a) Write a program that takes 2 numbers as command line
arguments and prints itssum.

b) Implement python script to read person’s age from keyboard and


display whether he is eligible for voting ornot.

c) Implement python script to check the given year is leap year ornot.

3. 3. Exercise programs on Python Script. 26/11/21


a) Implement Python Script to check given number is palindrome ornot.

b) Implement Python script to print factorial of anumber.

c) Implement Python Script to print sum of N naturalnumbers.

d) Implement Python Script to generate prime numbers series up ton

4. 4. Exercise programs on Lists. 26/11/21


a) Finding the sum and average of given numbers usinglists.

b) To display elements of list in reverseorder.

c) Finding the minimum and maximum elements in thelists.

5. 5. Exercise programs on Strings. 26/11/21


a) Implement Python Script to perform various operations on string
using stringlibraries.

b) Implement Python Script to check given string is palindrome ornot.

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

c) Implement python script to accept line of text and find the number of
characters, number of vowels and number of blank spaces in it.

6. 6. Exercise programs on functions.

a) Define a function max_of_three() that takes three numbers as arguments


and returns the largest of them.

7. 7. Exercise programs on recursion & parameter passing technique. 26/11/21


a) Define a function which generates fibbonaci series upto nnumbers.

b) Implement python script for call by value & referencefunction.

8. 8. Exercise programs on Tuples. 26/11/21


a) Write a program which accepts a sequence of comma- separated numbers
from console and generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program: 34, 67, 55, 33, 12,
98. Then, the output should be: ['34', '67', '55', '33', '12', '98'] ('34',67', '55',
'33', '12', '98').

b) With a given tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), write a program to print the


first half values in one line and the last half values in oneline.

9. 9. Exercise programs on files. 26/11/21


a) Write Python script to display filecontents.

b) Write Python script to copy file contents from one file toanother.

10. 10. Exercise programs on Exception handling concepts. 26/11/21


a) Write a python program by using exception handling mechanism.

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 1

Aim: Write a program for checking the given number is even or odd.
Write a program for displaying reversal of a number.

Code:

1:

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


if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

O/P:

Enter a number: 43
43 is Odd

Enter a number: 18
18 is Even

2.

num = 1234
reversed_num = 0

while num != 0:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

digit = num % 10
reversed_num = reversed_num * 10 + digit num //= 10

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

O/P:

4321

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment - 2

Aim: Write a program that takes 2 numbers as command line


arguments and prints its sum.

Implement python script to read person’s age from keyboard and


display whether he is eligible for voting or not.

Implement python script to check the given year is leap year or not.

Code:

# Store input numbers


num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)

# Display the sum


print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

O/P:
Enter first number: 1.5
Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

2.

x = int(input("Enter age of a user:"))

if x >= 18:
print("User is valid for voting:", x)
else:
print("User is not valid for voting:", x)

O/P:
Enter age of a user:45
User is valid for voting: 45

3.
# Python program to check if year is a leap year or not

year = 2000

# To get year (integer input) from the user


# year = int(input("Enter a year: "))

if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))

O/P:
2000 is a leap year

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 3

Aim: Implement Python Script to check given number is palindrome


or not.

Implement Python script to print factorial of a number.

Implement Python Script to print sum of N natural numbers.

Implement Python Script to generate prime numbers series up to n

Code:

1.
# Program to check if a string is palindrome or not

my_str = 'aIbohPhoBiA'

# make it suitable for caseless comparison


my_str =my_str.casefold()

# reverse the string


rev_str =reversed(my_str)

# check if the string is equal to its reverse


if list(my_str) == list(rev_str):
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")

O/P:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

The string is a palindrome.

2.

# Python program to find the factorial of a number provided by the user.

# change the value for a different result


num = 7

# To take input from the user


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

factorial = 1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, 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)

O/P:

The factorial of 4 is 24

3.

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

# Sum of natural numbers up to num

num = 10

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)

O/P:
The sum is 55

4.

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


start = 0

for i in range(start, end+1):


if i > 1:
for j in range(2, i):
if(i % j == 0):
break
else:
print(i)

O/P:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 4

Aim: Finding the sum and average of given numbers using lists.

a) To display elements of list in reverseorder.

OUTPUT:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

b) Finding the minimum and maximum elements in thelists.

Output:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 5

Aim: Implement Python Script to perform various operations on


string using string libraries.

>>concatenation

>>Casemethods

>>count(str)

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

>>split()
>>replace()

>>len()

2. Implement Python Script to check given string is palindrome ornot.

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

output:-

3. Implement python script to accept line of text and find thenumber


of characters, number of vowels and number of blank spaces init.

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Output:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 6

Aim: Define a function max_of_three() that takes three numbers as


arguments and returns the largest of them.

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 7

Aim: Define a function which generates Fibonacci series


upto n numbers.

Code:
Output:

2. Implement a python script for Call-by-value and Call-


by-reference

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Output:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 8

Aim: Write a program which accepts a sequence of comma


separated numbers from console and generate a list and a tuple
which contains every number. Suppose the following input is
supplied to the program: 34, 67, 55, 33, 12, 98. Then, the output
should be: ['34', '67', '55', '33', '12', '98'] ('34',67', '55', '33', '12', '98').

Output:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

2. With a given tuple (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), write a program to


print the first half values in one line and the last half values in one
line.

Output:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 9

Aim: Write Python script to display file contents.

Output:

2 Write Python script to copy file contents from one file to another.

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Output:

19100BTCSES05384 NAYAN SHARMA


BTCS-407 PROGRAMMING WITH PYTHON

Experiment – 10

Aim: Write a python program by using Exception handling


mechanism.

Code:

Output:

--------------------------------------------End----------------------------------------------------------------------

19100BTCSES05384 NAYAN SHARMA

You might also like