0% found this document useful (0 votes)
2 views9 pages

Python_Lab_Manual_Unit1

The document is a laboratory manual for a Python programming course at Jawaharlal Nehru Technological University, detailing experiments for second-year B.Tech students. It includes six experiments covering basic Python concepts such as finding the largest number, displaying prime numbers, swapping variables, and demonstrating various operators. Each experiment contains objectives, descriptions, algorithms, program codes, and sample outputs.

Uploaded by

GUMPA ANAND
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Python_Lab_Manual_Unit1

The document is a laboratory manual for a Python programming course at Jawaharlal Nehru Technological University, detailing experiments for second-year B.Tech students. It includes six experiments covering basic Python concepts such as finding the largest number, displaying prime numbers, swapping variables, and demonstrating various operators. Each experiment contains objectives, descriptions, algorithms, program codes, and sample outputs.

Uploaded by

GUMPA ANAND
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY - GURAJADA -

VIZIANAGARAM
Vizianagaram – 535 003, Andhra Pradesh (India)
(Established by Andhra Pradesh Act No. 22 of 2021)

LABORATORY MANUAL
PYTHON PROGRAMMING
(Skill Enhancement Course)

UNIT – I

Programme [Link].

Year / Semester II Year – I Semester

Regulation R23

Unit – I: Basics of Python, Operators & Control


Unit Covered
Flow

Experiments Included Experiment No. 1 to Experiment No. 6

Student Name

Roll Number

Department of Computer Science and Engineering


INDEX – UNIT I EXPERIMENTS

Exp. No. Title of the Experiment Signature

1 Find the largest element among three numbers

2 Display all prime numbers within an interval

3 Swap two numbers without using a temporary variable

Demonstrate Arithmetic, Relational, Assignment, Logical, Bitwise, Ternary,


4
Membership and Identity Operators

5 Add and multiply complex numbers

6 Print the multiplication table of a given number


EXPERIMENT NO. : 1 DATE : ____________

AIM : Write a program to find the largest element among three numbers.

DESCRIPTION

This program accepts three numbers from the user and determines the largest among them using relational and
logical operators combined with an if–elif–else conditional structure. It demonstrates comparison of multiple values,
decision-making constructs and formatted output in Python. Concepts used: input(), type conversion, relational
operators, logical operators, if-elif-else statement.

ALGORITHM

1. Start
2. Read three numbers a, b and c from the user
3. Check if a ≥ b and a ≥ c; if true, assign a as the largest number
4. Else, check if b ≥ a and b ≥ c; if true, assign b as the largest number
5. Else, assign c as the largest number
6. Display the largest number
7. Stop

PROGRAM CODE

# Program to find the largest element among three numbers

a = float(input("Enter first number: "))


b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

if a >= b and a >= c:


largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c

print("The largest number is:", largest)

SAMPLE OUTPUT

Enter first number: 12


Enter second number: 45
Enter third number: 30
The largest number is: 45.0
EXPERIMENT NO. : 2 DATE : ____________

AIM : Write a program to display all prime numbers within an interval.

DESCRIPTION

This program displays all prime numbers that lie between a lower and an upper bound entered by the user. A number
is checked for primality by testing divisibility from 2 up to its square root; if no divisor is found, the number is prime.
Concepts used: nested for loops, the for–else construct, range(), modulus operator.

ALGORITHM

1. Start
2. Read the lower bound and upper bound of the interval
3. Repeat step 4 for every number n from lower bound to upper bound
4. If n > 1, check divisibility of n by every integer i from 2 to √n
5. If no value of i divides n evenly, then n is prime; display it
6. Continue until all numbers in the interval are checked
7. Stop

PROGRAM CODE

# Program to display all prime numbers within an interval

lower = int(input("Enter lower bound: "))


upper = int(input("Enter upper bound: "))

print(f"Prime numbers between {lower} and {upper} are:")

for num in range(lower, upper + 1):


if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
break
else:
print(num, end=" ")

print()

SAMPLE OUTPUT

Enter lower bound: 10


Enter upper bound: 40
Prime numbers between 10 and 40 are:
11 13 17 19 23 29 31 37
EXPERIMENT NO. : 3 DATE : ____________

AIM : Write a program to swap two numbers without using a temporary variable.

DESCRIPTION

This program interchanges the values of two variables without the help of a third (temporary) variable. Python's
simultaneous/tuple assignment feature evaluates the right-hand side completely before assignment, allowing values
to be swapped directly. Concepts used: multiple assignment, tuple packing and unpacking.

ALGORITHM

1. Start
2. Read two numbers a and b
3. Display the values of a and b before swapping
4. Swap the values using simultaneous assignment: a, b = b, a
5. Display the values of a and b after swapping
6. Stop

PROGRAM CODE

# Program to swap two numbers without using a temporary variable

a = int(input("Enter value of a: "))


b = int(input("Enter value of b: "))

print("Before swapping: a =", a, ", b =", b)

# Swapping using Python's tuple (simultaneous) assignment


a, b = b, a

print("After swapping: a =", a, ", b =", b)

SAMPLE OUTPUT

Enter value of a: 5
Enter value of b: 9
Before swapping: a = 5 , b = 9
After swapping: a = 9 , b = 5
EXPERIMENT NO. : 4 DATE : ____________

AIM : Demonstrate the following operators in Python with suitable examples: (i) Arithmetic Operators (ii)
Relational Operators (iii) Assignment Operators (iv) Logical Operators (v) Bitwise Operators (vi) Ternary Operator
(vii) Membership Operators (viii) Identity Operators.

DESCRIPTION

This program illustrates the different categories of operators available in Python by applying each of them on suitable
operands and displaying the results. It covers numeric computation (arithmetic), comparison (relational), value
updating (assignment), boolean combination (logical), bit-level manipulation (bitwise), conditional expressions
(ternary), sequence containment checks (membership) and object-identity checks (identity).

ALGORITHM

1. Start
2. Initialize two numeric operands a and b
3. Apply arithmetic operators (+, −, *, /, //, %, **) on a and b, and display results
4. Apply relational operators (==, !=, >, <, >=, <=) on a and b, and display results
5. Apply assignment operators (=, +=, −=, *=, /=) on a variable, and display the updated value
6. Apply logical operators (and, or, not) on boolean expressions, and display results
7. Apply bitwise operators (&, |, ^, ~, <<, >>) on a and b, and display results
8. Apply the ternary (conditional) operator to find the greater of a and b
9. Apply membership operators (in, not in) on a list
10. Apply identity operators (is, is not) on two variables
11. Stop

PROGRAM CODE

# Program to demonstrate different types of operators in Python

a = 15
b = 4

# i) Arithmetic Operators
print("--- Arithmetic Operators ---")
print("a + b =", a + b)
print("a - b =", a - b)
print("a * b =", a * b)
print("a / b =", a / b)
print("a // b =", a // b)
print("a % b =", a % b)
print("a ** b =", a ** b)

# ii) Relational Operators


print("\n--- Relational Operators ---")
print("a == b :", a == b)
print("a != b :", a != b)
print("a > b :", a > b)
print("a < b :", a < b)
print("a >= b :", a >= b)
print("a <= b :", a <= b)

# iii) Assignment Operators


print("\n--- Assignment Operators ---")
c = a # =
print("c = a ->", c)
c += b # c = c + b
print("c += b ->", c)
c -= b # c = c - b
print("c -= b ->", c)
c *= b # c = c * b
print("c *= b ->", c)
c /= b # c = c / b
print("c /= b ->", c)

# iv) Logical Operators


print("\n--- Logical Operators ---")
print("(a > b) and (a > 0) :", (a > b) and (a > 0))
print("(a > b) or (b > a) :", (a > b) or (b > a))
print("not(a > b) :", not (a > b))

# v) Bitwise Operators
print("\n--- Bitwise Operators ---")
print("a & b :", a & b)
print("a | b :", a | b)
print("a ^ b :", a ^ b)
print("~a :", ~a)
print("a << 2 :", a << 2)
print("a >> 2 :", a >> 2)

# vi) Ternary (Conditional) Operator


print("\n--- Ternary Operator ---")
greater = a if a > b else b
print("Greater of a, b is:", greater)

# vii) Membership Operators


print("\n--- Membership Operators ---")
numbers = [10, 15, 20, 25]
print("15 in numbers :", 15 in numbers)
print("100 not in numbers:", 100 not in numbers)

# viii) Identity Operators


print("\n--- Identity Operators ---")
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print("x is y :", x is y)
print("x is z :", x is z)
print("x is not z :", x is not z)

SAMPLE OUTPUT

--- Arithmetic Operators ---


a + b = 19
a - b = 11
a * b = 60
a / b = 3.75
a // b = 3
a % b = 3
a ** b = 50625

--- Relational Operators ---


a == b : False
a != b : True
a > b : True
...
--- Identity Operators ---
x is y : True
x is z : False
x is not z : True
EXPERIMENT NO. : 5 DATE : ____________

AIM : Write a program to add and multiply complex numbers.

DESCRIPTION

This program reads the real and imaginary parts of two complex numbers, creates complex number objects using
Python's built-in complex data type, and computes their sum and product using the standard arithmetic operators.
Concepts used: complex data type, complex() constructor, arithmetic operators on complex numbers.

ALGORITHM

1. Start
2. Read the real and imaginary parts of the first complex number (r1, i1)
3. Read the real and imaginary parts of the second complex number (r2, i2)
4. Create complex numbers c1 and c2 using the complex() function
5. Compute the sum: sum = c1 + c2
6. Compute the product: product = c1 * c2
7. Display c1, c2, sum and product
8. Stop

PROGRAM CODE

# Program to add and multiply complex numbers

r1 = float(input("Enter real part of first complex number: "))


i1 = float(input("Enter imaginary part of first complex number: "))
r2 = float(input("Enter real part of second complex number: "))
i2 = float(input("Enter imaginary part of second complex number: "))

c1 = complex(r1, i1)
c2 = complex(r2, i2)

addition = c1 + c2
multiplication = c1 * c2

print("First complex number :", c1)


print("Second complex number :", c2)
print("Sum of complex numbers =", addition)
print("Product of complex numbers =", multiplication)

SAMPLE OUTPUT

Enter real part of first complex number: 2


Enter imaginary part of first complex number: 3
Enter real part of second complex number: 1
Enter imaginary part of second complex number: 4
First complex number : (2+3j)
Second complex number : (1+4j)
Sum of complex numbers = (3+7j)
Product of complex numbers = (-10+11j)
EXPERIMENT NO. : 6 DATE : ____________

AIM : Write a program to print the multiplication table of a given number.

DESCRIPTION

This program accepts a number from the user and prints its multiplication table from 1 to 10 using a for loop and the
range() function. Concepts used: for loop, range(), formatted string output (f-strings).

ALGORITHM

1. Start
2. Read the number n whose multiplication table is required
3. Repeat step 4 for i from 1 to 10
4. Compute and display n x i = n*i
5. Stop

PROGRAM CODE

# Program to print the multiplication table of a given number

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

print(f"Multiplication table of {n}:")


for i in range(1, 11):
print(f"{n} x {i} = {n * i}")

SAMPLE OUTPUT

Enter a number: 7
Multiplication table of 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Faculty Signature: ____________________

You might also like