You are on page 1of 16

U.N.S.

Institute Of Engineering & Technology


VBS Purvanchal University

LABORATORY MANUAL

B.TECH SEMESTER- IV

PYTHON PROGRAMMING LAB

Subject Code- KNC-402

COMPUTER SCIENCE AND ENGINEERING

UNSIET V.B.S. PRVANCHAL UNIVERSITY JAUNPUR


COMPUTER SCIENCE AND ENGINEERING

PYTHON PROGRAMMING LAB MANUAL

List of Program

1. Write a program in Python to perform arithmetic operation.

2. Write a program to perform to implement (if -else) statement in python.

3. Write a program to perform Boolean expression in Python.


4. Write a Program to use for loop in Python.
5. Write a Program in python to perform to calculate length of string.

6. Write a program in python to perform slicing on String.


7. Write a program to implement list operation in Python.
8. Write a program to perform file input and output operation in python.
9. Write a program to perform to implement a 2-d array in Python.

10. Write a program to perform the index value of an array in python.


PROGRAM -01

Aim: Write a program in Python to perform arithmetic operation.

Objective: Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication, etc.

Program

num1 = int(input('Enter First number: '))

num2 = int(input('Enter Second number '))

add = num1 + num2

dif = num1 - num2

mul = num1 * num2

div = num1 / num2

floor_div = num1 // num2

power = num1 ** num2

modulus = num1 % num2

print('Sum of ',num1 ,'and' ,num2 ,'is :',add)

print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)

print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)

print('Division of ',num1 ,'and' ,num2 ,'is :',div)

print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div)

print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)


print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)

OUTPUT:-
PROGRAM -02

Aim:-Write a program to perform to implement (if -else) statement in python.

Objective: If statements are control flow statements which helps us to run a particular code only
when a certain condition is satisfied. if statement is the most simple decision making statement.
It is used to decide whether a certain statement or block of statements will be executed or not.

An if statement allows you to write a block of code that gets executed only if the expression after
if is true.

Syntax:

if (condition):

(statement)

CODE:

# Python program to illustrate if-elif-else ladder

#!/usr/bin/python

i = 20

if (i == 10):

print("i is 10")

elif (i == 15):

print("i is 15")

elif (i == 20):

print("i is 20")
else:

print("i is not present")

OUTPUT:

i is 20
PROGRAM -03

Aim: Write a program to perform Boolean expression in Python.

Objective: The Python Boolean type is one of Python’s built-in data types. It’s used to represent
the truth value of an expression. For example, the expression 1 <= 2 is True, while the
expression 0 == 1 is False. Understanding how Python Boolean values behave is important to
programming well in Python.

CODE:

a = 200

b = 33

if b > a:

print("b is greater than a")

else:

print("b is not greater than a")

OUTPUT:

b is not greater than a


PROGRAM -04

Aim: Write a Program to use for loop in Python.

Objective: A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

CODE:

fruits = ["apple", "banana", "cherry"]

for x in fruits:

if x == "banana":

continue

print(x)

OUTPUT:-

apple

cherry
PROGRAM-05

AIM: Write a Program in python to perform to calculate length of string.

Objective : To get the length of a string, use the len() function.

CODE:

# Python code to demonstrate string length

# using len

str = "geeks"

print(len(str))

OR

# Python code to demonstrate string length

# using for loop

# Returns length of string

def findLen(str):

counter = 0

for i in str:

counter += 1

return counter

str = "geeks"

print(findLen(str))

OUTPUT:

5.
PROGRAM -06

Aim: Write a program in python to perform slicing on String.

Objective: Python String slicing always follows this rule: s[:i] + s[i:] == s for any index 'i'. All
these parameters are optional - start_pos default value is 0, the end_pos default value is the
length of string and step default value is 1. Let's look at some simple examples of string slice
function to create substring.

CODE:

# Python program to demonstrate

# string slicing

String = 'ASTRING'

# Using slice constructor

s1 = slice(3)

s2 = slice(1, 5, 2)

s3 = slice(-1, -12, -2)

print("String slicing")

print(String[s1])

print(String[s2])

print(String[s3])

OUTPUT:

String slicing

AST
SR

GITA

Meth

PROGRAM -07

Aim : Write a program to implement list operation in Python.

Objective: The list is a data structuring method that allows storing the integers or the characters
in an order indexed by starting from 0. List operations are the operations that can be performed
on the data in the list data structure. A few of the basic list operations used in Python
programming are extend(), insert(), append(), remove(), pop(), slice, reverse(), min() & max(),
concatenate(), count(), multiply(), sort(), index(), clear(), etc.

(i). INSERT

(ii).REMOVE

(iii).REVERSE

CODE:-

(i).INSERT:-

numbers = [10, 30, 40]

# insert an element at index 1 (second position)

numbers.insert(1, 20)

print(numbers) # [10, 20, 30, 40]

OUTPUT:
[10,20,30,40]

(ii).REMOVE:-

languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the second item

del languages[1]

print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust', 'R']

# deleting the last item

del languages[-1]

print(languages) # ['Python', 'C++', 'C', 'Java', 'Rust']

# delete the first two items

del languages[0 : 2] # ['C', 'Java', 'Rust']

print(languages)

OUTPUT:-

['Python', 'C++', 'C', 'Java', 'Rust', 'R']

['Python', 'C++', 'C', 'Java', 'Rust']

['C', 'Java', 'Rust']


PROGRAM-08

Aim: Write a program to perform file input and output operation in python.

Objective: Python's built-in functions input() and print() perform read/write operations with
standard IO streams. The input() function reads text into memory variables from keyboard which
is defined as sys.stdin and the print() function send data to display device identified as sys.stdout.

CODE:

#FOR GIVING INPUT

# open a file

file1 = open("test.txt", "r")

# read the file

read_content = file1.read()

print(read_content)

OUTPUT:-

This is a test file.

Hello from the test file.

CODE:-#FOR GIVING OUTPUT

with open(test2.txt', 'w') as file2:


# write contents to the test2.txt file

file2.write('Programming is Fun.')

fil2.write('Programiz for beginners')

OUTPUT:
PROGRAM-09

Aim: Write a program to perform to implement a 2-d array in Python.

Objective: Python provides powerful data structures called lists, which can store and manipulate
collections of elements. Also provides many ways to create 2-dimensional lists/arrays. However
one must know the differences between these ways because they can create complications in
code that can be very difficult to trace out. In this article, we will explore the right way to use 2D
arrays/lists in Python.

Using 2D arrays/lists the right way

Using 2D arrays/lists the right way involves understanding the structure, accessing elements, and
efficiently manipulating data in a two-dimensional grid. When working with structured data or
grids, 2D arrays or lists can be useful. A 2D array is essentially a list of lists, which represents a
table-like structure with rows and columns.

CODE:

Student_dt = [ [72, 85, 87, 90, 69], [80, 87, 65, 89, 85], [96, 91, 70, 78, 97], [90, 93, 91, 90, 94],
[57, 89, 82, 69, 60] ]

#print(student_dt[])

print(Student_dt[1]) # print all elements of index 1

print(Student_dt[0]) # print all elements of index 0

print(Student_dt[2]) # print all elements of index 2

print(Student_dt[3][4]) # it defines the 3rd index and 4 position of the data element.

OUTPUT:-
PROGRAM -10

Aim: Write a program to perform the index value of an array in python.

Objective: Indexing in Python, and in all programming languages and computing in general,
starts at 0 . It is important to remember that counting starts at 0 and not at 1 . To access an
element, you first write the name of the array followed by square brackets. Inside the square
brackets you include the item's index number.

CODE:

# list of items

list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5]

# Will print index of '4' in sublist

# having index from 4 to 8.

print(list1.index(4, 4, 8))

OUTPUT:-

You might also like