ExNo:1 COMMAND LINE INTERFACE
Aim
To write a Python program that reads command-line arguments using [Link].
Algorithm
Step 1. Import the sys module.
Step 2. Use len([Link]) to count the number of arguments passed.
Step 3. Use [Link] to display the list of arguments.
Step 4. Print the results.
Coding:[Link]
import sys
print("Number of arguments:", len([Link]))
print("Arguments list:", [Link])
In Text Editor Window:
OUTPUT:
ExNo:2 CALCULATOR PROGRAM
Aim
To develop a simple calculator that performs addition, subtraction, multiplication, and
division.
Algorithm
1. Define four functions: add, sub, mul, and div.
2. Display operation choices to the user.
3. Accept the user's choice.
4. Input two numbers.
5. Based on the choice: Call the respective function. Display the result.
6. If choice is invalid, show an error message.
Coding:[Link]
def add(x, y): return x + y
def sub(x, y): return x - y
def mul(x, y): return x * y
def div(x, y): return x / y
print("Select operation: [Link] [Link] [Link] [Link]")
choice = input("Enter choice: ")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if choice == '1':
print(a, "+", b, "=", add(a, b))
elif choice == '2':
print(a, "-", b, "=", sub(a, b))
elif choice == '3':
print(a, "*", b, "=", mul(a, b))
elif choice == '4':
print(a, "/", b, "=", div(a, b))
else:
print("Invalid choice")
In Text Editor Window:
OUTPUT:
ExNo:3 STRING FUNCTIONS
Aim
To demonstrate various string functions in Python.
Algorithm
1. Create a string variable.
2. Use built-in functions: lower(), upper(),title(), replace(),len(),split()
3. Print all outputs.
Coding:[Link]
text = "Python Programming"
print([Link]())
print([Link]())
print([Link]())
print([Link]("Python", "Java"))
print("Length:", len(text))
print("Split:", [Link]())
In Text Editor Window:
OUTPUT:
ExNo:4 SELECTION SORT
Aim
To sort an array of elements using the selection sort technique.
Algorithm
1. Start with the first element as the minimum.
2. Compare it with all other elements to find the actual minimum.
3. Swap the minimum element with the first position.
4. Move to the next position and repeat the process.
5. Continue until the entire list is sorted.
6. Display the sorted list.
Coding:[Link]
def selection_sort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
arr = [64, 25, 12, 22, 11]
selection_sort(arr)
print("Sorted array:", arr)
In Text Editor Window:
OUTPUT:
ExNo:5 STACK
Aim
To implement stack operations (push and pop) using a Python list.
Algorithm
1. Initialize an empty list as a stack.
2. Define push(): Input an element. Append it to the stack.
3. Define pop(): Check if stack is empty. Remove the top element using pop().
4. Provide menu options: push, pop, exit.
5. Continuously accept user choice until exit.
Coding:[Link]
stack = []
def push():
element = input("Enter element: ")
[Link](element)
print(stack)
def pop():
if not stack:
print("Stack is empty")
else:
print("Removed element:", [Link]())
print(stack)
while True:
print("\[Link] [Link] [Link]")
choice = int(input("Enter choice: "))
if choice == 1:
push()
elif choice == 2:
pop()
else:
break
In Text Editor Window:
OUTPUT:
ExNo:6 READING AND WRITING OPERATIONS IN A FILE
Aim
To write data into a file and then read the same data using Python.
Algorithm
1. Open a file in write mode.
2. Write a line of text using write(). Close the file.
3. Open the file in read mode.
4. Read all contents using read(). Print the file contents.
5. Close the file.
Coding:[Link]
f = open("[Link]", "w")
[Link]("This is a Python file handling example.\n")
[Link]()
f = open("[Link]", "r")
print([Link]())
[Link]()
In Text Editor Window:
OUTPUT:
ExNo:7 REGULAR EXPRESSION
Aim
To search for an email pattern in a given string using regular expressions.
Algorithm
1. Import the re module.
2. Store a text string containing an email.
3. Define a regex pattern for email.
4. Use [Link]() to find a match.
5. If a match exists: Display the matched email.
6. Otherwise show “No match found”.
Coding:[Link]
import re
text = "Email me at test123@[Link]"
pattern = r"[a-zA-Z0-9]+@[a-z]+\.[a-z]+"
match = [Link](pattern, text)
if match:
print("Email found:", [Link]())
else:
print("No match found")
In Text Editor Window:
OUTPUT:
ExNo:8 SIMULATION OF ROBOT ANTENNA
Aim
To simulate a robot whose antenna can be activated.
Algorithm
1. Create a class Robot with attributes name and signal.
2. Initialize signal as "Off" in the constructor.
3. Define activate_antenna() to set signal to "On".
4. Create an object of the Robot class.
5. Call the function to activate the antenna.
Coding:[Link]
class Robot:
def __init__(self, name):
[Link] = name
[Link] = "Off"
def activate_antenna(self):
[Link] = "On"
print(f"{[Link]}'s antenna activated.")
robot1 = Robot("Robo1")
robot1.activate_antenna()
In Text Editor Window:
OUTPUT:
ExNo:9 HOSTING A BLOG ON RASPBERRY PI
Aim
To host a simple blog using an inbuilt HTTP server module.
Algorithm
1. Import HTTPServer and SimpleHTTPRequestHandler.
2. Set a port number (e.g., 8080).
3. Initialize the HTTP server.
4. Print a message indicating the server is running.
5. Use serve_forever() to continuously host the blog.
Coding:[Link]
from [Link] import SimpleHTTPRequestHandler, HTTPServer
port = 8080
server = HTTPServer(('', port), SimpleHTTPRequestHandler)
print("Hosting blog on Raspberry Pi at port", port)
server.serve_forever()
In Text Editor Window:
OUTPUT:
ExNo:10 INTERFACING LED ON RASPBERRY PI
Aim
To control a blinking LED using Raspberry Pi GPIO pins.
Algorithm
1. Import GPIO and time modules.
2. Set GPIO mode and configure LED pin as output.
3. Use a loop to: Turn LED ON. Wait for 1 second. Turn LED OFF. Wait for 1 second.
4. Repeat blinking for 5 cycles.
5. Clean up GPIO settings.
Design
Coding: [Link]
import [Link] as GPIO
import time
led = 11
[Link]([Link])
[Link](led, [Link])
print("LED blinking...")
for i in range(5):
[Link](led, True)
[Link](1)
[Link](led, False)
[Link](1)
[Link]()
ExNo:11 HOME AUTOMATION USING RASPBERRY PI
Aim
To automate home appliances (light and fan) using Raspberry Pi.
Algorithm
1. Import GPIO and time modules.
2. Set GPIO mode and configure pins for light and fan.
3. Enter a loop to:
Accept commands (light_on, light_off, fan_on, fan_off).
Turn respective devices ON/OFF.
If 'exit', break loop.
4. Cleanup GPIO before program ends.
Design
Coding: [Link]
import [Link] as GPIO
import time
light = 11
fan = 13
[Link]([Link])
[Link](light, [Link])
[Link](fan, [Link])
print("Home Automation System")
while True:
cmd = input("Enter command (light_on/off, fan_on/off, exit): ")
if cmd == "light_on":
[Link](light, True)
elif cmd == "light_off":
[Link](light, False)
elif cmd == "fan_on":
[Link](fan, True)
elif cmd == "fan_off":
[Link](fan, False)
elif cmd == "exit":
[Link]()
break
else:
print("Invalid command")