Python Lab Manual
Python Lab Manual
Python Lab Manual
LABORATORY MANUAL
B.TECH SEMESTER- IV
List of Program
Objective: Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication, etc.
Program
OUTPUT:-
PROGRAM -02
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:
#!/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:
OUTPUT:
i is 20
PROGRAM -03
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:
else:
OUTPUT:
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:
for x in fruits:
if x == "banana":
continue
print(x)
OUTPUT:-
apple
cherry
PROGRAM-05
CODE:
# using len
str = "geeks"
print(len(str))
OR
def findLen(str):
counter = 0
for i in str:
counter += 1
return counter
str = "geeks"
print(findLen(str))
OUTPUT:
5.
PROGRAM -06
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:
# string slicing
String = 'ASTRING'
s1 = slice(3)
s2 = slice(1, 5, 2)
print("String slicing")
print(String[s1])
print(String[s2])
print(String[s3])
OUTPUT:
String slicing
AST
SR
GITA
Meth
PROGRAM -07
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.insert(1, 20)
OUTPUT:
[10,20,30,40]
(ii).REMOVE:-
del languages[1]
del languages[-1]
print(languages)
OUTPUT:-
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:
# open a file
read_content = file1.read()
print(read_content)
OUTPUT:-
file2.write('Programming is Fun.')
OUTPUT:
PROGRAM-09
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 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[3][4]) # it defines the 3rd index and 4 position of the data element.
OUTPUT:-
PROGRAM -10
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]
print(list1.index(4, 4, 8))
OUTPUT:-