Sona Python Lab With Code - 23UCSCCP01 (2)
Sona Python Lab With Code - 23UCSCCP01 (2)
COMPUTER SCIENCE
PRACTICAL RECORD
Name :
Register Number :
Year / Sem :
2 Operators in Python
3 Conditional Statements
4 Loops
Jump Statements
5
6 Functions
7 Recursion
8 Arrays
Strings
9
10 Modules
Lists
11
12 Tuples
13 Dictionaries
14 File Handling
/
Ex 1: Variables, Constants, and I/O Statements
Aim:
Algorithm:
1. Start
9. End
Program:
# Constants
PI = 3.14159
# Variables
radius = 0
area = 0
# Print welcome message
# Calculate area
result = area * 2
result += 10
result -= 5
# Print the result
Output:
Hello, John!
Result:
Aim:
Algorithm:
1. Start
2. Declare variables.
7. End
Program:
# Operators in Python
# Arithmetic Operators
x = 10
y=3
print("Arithmetic Operators:")
print("x + y =", x + y)
print("x - y =", x - y)
print("x * y =", x * y)
print("x / y =", x / y)
print("x % y =", x % y)
print("x ** y =", x ** y)
print("x // y =", x // y)
# Comparison Operators
a=5
b=8
print("a == b is", a == b)
print("a != b is", a != b)
# Logical Operators
p = True
q = False
print("p or q is", p or q)
x = 10
x += 5
print("x += 5, x =", x)
y=7
y *= 2
print("y *= 2, y =", y)
Output:
Arithmetic Operators:
x + y = 13
x- y = 7
x * y = 30
x / y = 3.3333333333333335
x%y=1
x ** y = 1000
x // y = 3
Comparison Operators:
a == b is False
a != b is True
a > b is False
a < b is True
a >= b is False
a <= b is True
Logical Operators:
p and q is False
p or q is True
not p is False
Assignment Operators:
x += 5, x = 15
y *= 2, y = 14
Result:
Aim:
Algorithm:
1. Start
2. Declare variables.
7. End
Program:
# Conditional Statements
if num > 0:
Output 1:
Enter a number: 5
Output 2:
Enter a number: -2
Output 3:
Enter a number: 0
Result:
Aim:
Algorithm:
1. Start
2. Initialize a variable.
3. Use a while loop to iterate until a condition is met.
4. Perform desired operations within the loop.
5. Update the variable.
6. Print the result.
7. End
Program:
# Loops
count = 1
print("Count:", count)
count += 1
print("Loop finished.")
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Loop finished.
Result:
Aim:
Algorithm:
1. Start
2. Use a for loop to iterate over a range of numbers.
3. Check for a condition using an if statement.
4. Use a jump statement to alter the flow of the loop.
5. Print the result.
6. End
Program:
# Jump Statements
if num % 3 == 0:
continue
print("Number:", num)
print("Loop finished.")
Output:
Number: 1
Number: 2
Number: 4
Number: 5
Loop finished.
Result:
Aim:
To understand and utilize functions in Python.
Algorithm:
1. Start
2. Define a function.
3. Call the function.
4. Pass arguments to the function.
5. Perform desired operations within the function.
6. Return a value from the function.
7. Print the result.
8. End
Program:
# Functions
def square(number):
result = number ** 2
return result
num = 5
square_result = square(num)
Output:
Square of 5 is: 25
Aim:
Algorithm:
1. Start
2. Define a recursive function.
3. Define a base case for the recursion.
4. Call the function recursively with a modified parameter.
5. Perform desired operations within the function.
6. Return a value.
7. Print the result.
8. End
Program:
# Recursion
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
num = 5
factorial_result = factorial(num)
Result:
Aim:
Algorithm:
1. Start
3. Create an array.
7. End
Program:
# Arrays
print("Array elements:")
print(num)
numbers[2] = 6
print(num)
Output:
Array elements:
Modified array:
Result:
Aim:
Algorithm:
1. Start
2. Initialize a string variable.
3. Access and modify characters in the string.
4. Perform string concatenation.
5. Perform string slicing.
6. Print the result.
7. End
Program:
# Strings
name = "Alice"
greeting = "Hello, " + name + "!"
print(greeting)
substring = message[7:12]
print("Substring:", substring)
Output:
First character: H
Hello, Alice!
Substring: World
Result:
Aim:
Algorithm:
1. Start
2. Import a module.
3. Use functions or variables from the module.
4. Print the result.
5. End
Program:
# Modules
import math
Output:
Factorial of 5: 120
Result:
Aim:
To understand and utilize lists in Python.
Algorithm:
1. Start
2. Create a list with elements.
3. Access and modify list elements.
4. Perform list operations such as appending, extending, and removing
elements.
5. Print the result.
6. End
Program:
# Lists
# Create a list
fruits[1] = "grape"
fruits.append("mango")
fruits.extend(["pineapple", "watermelon"])
fruits.remove("orange")
# Print the list
Output:
Result:
Aim:
To understand and utilize tuples in Python.
Algorithm:
1. Start
2. Create a tuple with elements.
3. Access tuple elements.
4. Perform tuple operations such as concatenation and slicing.
5. Print the result.
6. End
Program:
# Tuples
# Create a tuple
numbers = (1, 2, 3, 4, 5)
numbers_sliced = numbers[2:5]
First number: 1
Result:
Aim:
To understand and utilize dictionaries in Python.
Algorithm:
1. Start
2. Create a dictionary with key-value pairs.
3. Access dictionary values using keys.
4. Modify dictionary values.
5. Perform dictionary operations such as adding new key-value pairs and
removing items.
6. Print the result.
7. End
Program:
# Dictionaries
# Create a dictionary
student = {
"name": "Alice",
"age": 20,
student["age"] = 21
del student["major"]
Output:
Result:
Aim:
To understand file handling operations in Python.
Algorithm:
1. Start
2. Prompt the user to enter a filename.
3. Open the file in write mode.
4. Write data to the file.
5. Close the file.
6. Open the file in read mode.
7. Read and display the contents of the file.
8. Close the file.
9. End
Program:
# File Handling
file.close()
print(file.read())
file.close()
Output:
Result: