0% found this document useful (0 votes)
29 views14 pages

PPS Lab 10 Experiments

The document outlines various Python programming exercises, including dynamic typing, arithmetic operations, relational and logical operators, decision-making structures, and functions. It also covers list operations, command line arguments, and vector processing using tuples. Each section includes an aim, algorithm, program code, sample output, and results demonstrating the successful execution of the tasks.

Uploaded by

rameshkumar.m
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)
29 views14 pages

PPS Lab 10 Experiments

The document outlines various Python programming exercises, including dynamic typing, arithmetic operations, relational and logical operators, decision-making structures, and functions. It also covers list operations, command line arguments, and vector processing using tuples. Each section includes an aim, algorithm, program code, sample output, and results demonstrating the successful execution of the tasks.

Uploaded by

rameshkumar.m
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
You are on page 1/ 14

1.

Variable Types

Aim

To demonstrate how Python handles dynamic typing and type changes of variables.

Algorithm

1. Start the program.


2. Assign an integer value (10) to variable a.
3. Print the type of variable a.
4. Reassign a floating-point value (10.20) to variable a.
5. Print the type of variable a.
6. Reassign a string value ("welcome") to variable a.
7. Print the value of variable a.
8. End the program.

Program

a = 10
print(type(a))

a = 10.20
print(type(a))

a = "welcome"
print(type(a))

Sample Output

<class 'int'>
<class 'float'>
<class ‘string’>

Result

The program shows that in Python, a variable can hold different types of data (int, float,
string), proving that Python uses dynamic typing.
2. Computational problems using variables and type conversion

Aim

To perform arithmetic operations using variables and apply type conversion for user input.

Algorithm

1. Start the program.


2. Read the value of a from the user and convert it into an integer.
3. Read the value of b from the user and convert it into an integer.
4. Perform and display the result of addition (a+b).
5. Perform and display the result of subtraction (a-b).
6. Perform and display the result of multiplication (a*b).
7. Perform and display the result of division (a/b).
8. Perform and display the result of modulus (a%b).
9. Perform and display the result of exponentiation (a**b).
10. End the program.

Program:

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


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

print(f"addition of a + b = {a+b}")
print(f"subtraction of a - b = {a-b}")
print(f"multiplication of a * b = {a*b}")
print(f"division of a / b = {a/b}")
print(f"remainder of a % b = {a%b}")
print(f"a power of b = {a**b}")

Sample Output

Enter the value of a: 5


Enter the value of b: 2
addition of a + b = 7
subtraction of a - b = 3
multiplication of a * b = 10
division of a / b = 2.5
remainder of a % b = 1
a power of b = 25

Result

The program successfully takes user input, performs arithmetic operations (addition,
subtraction, multiplication, division, modulus, power), and displays the results.
3. Operators and expression

Aim

To understand the use of relational and logical operators in Python.

Algorithm

1. Start the program.


2. Read input value for variable a.
3. Read input value for variable b.
4. Compare values of a and b using relational operators (==, >, <, !=).
5. Apply logical operators (or, not) on given expressions.
6. Display results of all operations.
7. End the program.

Program:

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


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

print("a is equal to b ", a == b)


print("a is greater than b ", a > b)
print("a is less than b ", a < b)
print("a is not equal to b ", a != b)
print("a>10 or b<5 ", a > 10 or b < 5)
print("a>5 or b<5 ", a > 5 or b < 5)
print("a is not equal to b", not (a == b))

Sample Output

Enter the value of a: 7

Enter the value of b: 3

a is equal to b False
a is greater than b True
a is less than b False
a is not equal to b True
a>10 or b<5 True
a>5 or b<5 True
a is not equal to b True

Result

The program demonstrates relational and logical operators in Python.


4. if-then-else Structure

Aim

To demonstrate decision making using if-elif-else structure.

Algorithm

1. Start the program.


2. Read input number x.
3. If x < 0, print "x is Negative value".
4. Else if x > 0, print "x is non negative and greater than zero".
5. Else if x == 0, print "x is zero".
6. Otherwise, print "x is not a number".
7. End the program.

Program :

x = int(input("Enter a number:- "))

if x < 0:
print("x is Negative value")
elif x > 0:
print("x is non negative and greater than zero")
elif x == 0:
print("x is zero")
else:
print("x is not a number")

Sample Output

Enter a number:- -5
x is Negative value
Enter a number:- 10
x is non negative and greater than zero
Enter a number:- 0
x is zero

Result

The program successfully classifies a number as negative, positive, or zero.


5. Sum of series

Aim

To find the sum of the first n natural numbers using a for loop.

Algorithm

1. Start the program.


2. Read input number x.
3. Initialize sum = 0.
4. Repeat from i = 1 to x.
5. Add each number i to sum.
6. Display the final sum.
7. End the program.

Program:

x = int(input("Enter a value:- "))


sum = 0

for i in range(1, x+1):


sum += i

print(f"Sum of series {x} is: {sum}")

Sample Output

Enter a value:- 5
Sum of series 5 is: 15

Result

The program correctly calculates the sum of the first n natural numbers.
6. Prime Number using Non-Fruitful Function

Aim

To write a non-fruitful function that checks whether a number is prime and prints the result.

Algorithm

1. Start the program.


2. Define a function is_prime(n) that sets a global flag variable.
3. Inside the function:
o If n <= 1, set flag = 0.
o Otherwise, loop from 2 to n//2:
 If divisible, set flag = 0 and break.
o If no divisors are found, set flag = 1.
4. In the main program, read input num.
5. Call the function is_prime(num).
6. Use the flag in main to decide whether to print prime or not prime.
7. End the program.

Program

flag = 0 # global flag

def is_prime(n):
global flag
if n <= 1:
flag = 0
else:
flag = 1
for i in range(2, (n // 2) + 1):
if n % i == 0:
flag = 0
break

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

if flag == 1:
print(f"{num} is a Prime Number")
else:
print(f"{num} is not a Prime Number")
Sample Output 1

Enter a number: 17
17 is a Prime Number

Sample Output 2

Enter a number: 21
21 is not a Prime Number

Result

The program successfully demonstrates a non-fruitful function that prints whether a given
number is prime or not.
7. Factorial using Fruitful Function

Aim

To write a fruitful function that returns the factorial of a given number.

Algorithm

1. Start the program.


2. Define a function factorial(n) to compute factorial.
3. Initialize fact = 1.
4. Use a loop from 1 to n, multiplying each value with fact.
5. Return the final factorial value.
6. In the main program, read an integer from the user.
7. Call the function and print the result.
8. End the program.

Program

def factorial(n):
fact = 1
for i in range(1, n+1):
fact *= i
return fact

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


print(f"Factorial of {num} is: {factorial(num)}")

Sample Output

Enter a number: 5
Factorial of 5 is: 120

Result

The program successfully demonstrates a fruitful function that returns the factorial of a
number.
8. Command Line Arguments in Python

Aim

To write a Python program that demonstrates the use of command line arguments.

Algorithm

1. Start the program.


2. Import the sys module to access command line arguments.
3. Read all arguments using sys.argv.
4. Remember:
o sys.argv[0] → program name
o sys.argv[1] → first argument
o sys.argv[2] → second argument, etc.
5. Display all arguments passed by the user.
6. Optionally, perform operations on them (e.g., addition if numeric).
7. End the program.

Program (cmd_args.py)

import sys

print("Total arguments passed:", len(sys.argv))


print("Arguments list:", sys.argv)

print("Script name:", sys.argv[0])

for i in range(1, len(sys.argv)):


print(f"Argument {i}:", sys.argv[i])

if len(sys.argv) >= 3:
a = int(sys.argv[1])
b = int(sys.argv[2])
print("Sum of first two arguments:", a + b)

Execution (Command Line)

python cmd_args.py 10 20 hello

.
Sample Output

Total arguments passed: 4


Arguments list: ['cmd_args.py', '10', '20', 'hello']
Script name: cmd_args.py
Argument 1: 10
Argument 2: 20
Argument 3: hello
Sum of first two arguments: 30

Result

The program successfully demonstrates the use of command line arguments in Python using
the sys.argv list.
9. List Operations (Cloning, Comprehension, Processing)

Aim

To perform different list operations in Python such as cloning, comprehension, and


processing.

Algorithm

1. Start the program.


2. Cloning a list:
o Create a list list1 with some integer values.
o Clone the list using slicing ([:]) and store it in list2.
o Print both the original and cloned list.
3. List Comprehension:
o Use the range() function to generate numbers from 1 to 5.
o For each number x, calculate the square (x**2).
o Store the squared values inside a new list squares using list comprehension.
o Print the squares list.
4. List Processing:
o Create another list nums with integer values.
o Use the sum() function to calculate the total of all elements.
o Use the max() function to find the largest number.
o Use the sorted() function to arrange the list in ascending order.
o Print the results.
5. Stop the program.

Program

list1 = [10, 20, 30, 40]


list2 = list1[:] # clone
print("Original List:", list1)
print("Cloned List:", list2)

squares = [x**2 for x in range(1, 6)]


print("\nSquares using List Comprehension:", squares)

nums = [12, 5, 8, 20, 3]


print("\nProcessing List:", nums)
print("Sum:", sum(nums))
print("Maximum:", max(nums))
print("Sorted:", sorted(nums))
Sample Output

Original List: [10, 20, 30, 40]


Cloned List: [10, 20, 30, 40]

Squares using List Comprehension: [1, 4, 9, 16, 25]

Processing List: [12, 5, 8, 20, 3]


Sum: 48
Maximum: 20
Sorted: [3, 5, 8, 12, 20]

Result

The program executed successfully and demonstrated list cloning, list comprehension, and
list processing operations in Python.
10. Vector Processing using Tuples

Aim

To write a Python program that performs vector operations (addition, subtraction, dot
product, and magnitude) using tuples.

Algorithm

1. Start the program.


2. Define two vectors as tuples (v1, v2).
3. Perform vector addition by adding corresponding elements.
4. Perform vector subtraction by subtracting corresponding elements.
5. Compute dot product by multiplying corresponding elements and summing the
results.
6. Compute magnitude of a vector using the formula:

7. Display all results.


8. Stop the program.

Program

import math

v1 = (2, 4, 6)
v2 = (1, 3, 5)
add = tuple(a + b for a, b in zip(v1, v2))
sub = tuple(a - b for a, b in zip(v1, v2))
dot = sum(a * b for a, b in zip(v1, v2))
mag_v1 = math.sqrt(sum(a**2 for a in v1))
mag_v2 = math.sqrt(sum(b**2 for b in v2))
print("Vector 1:", v1)
print("Vector 2:", v2)
print("Addition:", add)
print("Subtraction:", sub)
print("Dot Product:", dot)
print("Magnitude of Vector 1:", round(mag_v1, 2))
print("Magnitude of Vector 2:", round(mag_v2, 2))
Sample Output

Vector 1: (2, 4, 6)
Vector 2: (1, 3, 5)
Addition: (3, 7, 11)
Subtraction: (1, 1, 1)
Dot Product: 44
Magnitude of Vector 1: 7.48
Magnitude of Vector 2: 5.92

Result

The program executed successfully and demonstrated vector addition, subtraction, dot
product, and magnitude operations using tuples.

You might also like