0% found this document useful (0 votes)
22 views7 pages

Python Programming Examples and Concepts

The document contains various Python programming examples, including modifications of MySQL database tables, class definitions for Circle and Rectangle with area and perimeter calculations, and exception handling. It also demonstrates file reading line by line and a GUI design for a Student Registration Form. Each section includes code snippets and example outputs.
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)
22 views7 pages

Python Programming Examples and Concepts

The document contains various Python programming examples, including modifications of MySQL database tables, class definitions for Circle and Rectangle with area and perimeter calculations, and exception handling. It also demonstrates file reading line by line and a GUI design for a Student Registration Form. Each section includes code snippets and example outputs.
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

Python Programming

PART B
1. Python program to demonstrate modification of an existing table data from MySQL
database.

BHARATESH COLLEGE OF COMPUTER APPLICATIONS 1


Python Programming
2. Python class named Circle constructed by a radius and two methods which will
compute the area and the perimeter of a circle.

import math
class Circle:
def _init_(self, radius):
[Link] = radius # instance variable

def area(self):
return [Link] * ([Link] ** 2)

def perimeter(self):
return 2 * [Link] * [Link]

# Example usage:
c1 = Circle(5) # circle with radius 5

print("Radius:", [Link])
print("Area:", round([Link](),2)) # formatted to 2 decimal places
print("Perimeter:", round([Link](),2))

************* OUTPUT **************

Area: 314.16
Perimeter: 62.83

BHARATESH COLLEGE OF COMPUTER APPLICATIONS 2


Python Programming
3. Python class named Rectangle constructed by a length and width and a method
which will compute the area and perimeter of rectangle. Inherit a class Box that
contains additional method volume. Override the perimeter method to compute
perimeter of a Box.

class Rectangle:
def __init__(self, length, width):
[Link] = length
[Link] = width
def area(self):
return [Link] * [Link]
def perimeter(self):
return 2 * ([Link] + [Link])

class Box(Rectangle): # Inheriting Rectangle


def __init__(self, length, width, height):
super().__init__(length, width) # Call parent constructor
[Link] = height
def volume(self):
return [Link] * [Link] * [Link]
# Overriding the perimeter method
def perimeter(self):
# Perimeter of a box = sum of all edges
return 4 * ([Link] + [Link] + [Link])

# Example usage
rect = Rectangle(10, 5)
print("Rectangle Area:", [Link]())
print("Rectangle Perimeter:", [Link]())

box = Box(10, 5, 3)
print("\nBox Area:", [Link]()) # Base area (from Rectangle)
print("Box Perimeter:", [Link]()) # Overridden
print("Box Volume:", [Link]())

************* OUTPUT **************

Rectangle Area: 50
Rectangle Perimeter: 30

Box Area: 50
Box Perimeter: 72
Box Volume: 150

BHARATESH COLLEGE OF COMPUTER APPLICATIONS 3


Python Programming
4. Python program to show use of Regular expressions with match(), search(),
findall(), sub() and split().

BHARATESH COLLEGE OF COMPUTER APPLICATIONS 4


Python Programming
5. Python program to demonstrate Exception handling using 'try', 'except', 'finally'
and 'else block.

def divide_numbers(a, b):


try:
print("Attempting division...")
result = a / b
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
except TypeError:
print("Error: Please enter only numbers.")
else:
# Runs only if no exception occurs
print(f"Division successful! Result = {result}")
finally:
# Always runs whether an exception occurs or not
print("Execution of try-except block completed.\n")

# Test cases
divide_numbers(10, 2) # Valid case
divide_numbers(10, 0) # ZeroDivisionError
divide_numbers(10, "a") # TypeError

************* OUTPUT **************

Attempting division...
Division successful! Result = 5.0
Execution of try-except block completed.

Attempting division...
Error: Division by zero is not allowed.
Execution of try-except block completed.

Attempting division...
Error: Please enter only numbers.
Execution of try-except block completed.

BHARATESH COLLEGE OF COMPUTER APPLICATIONS 5


Python Programming
6. Python program to read a file line by line store it into an array.

filename = "[Link]" # replace with your file name

lines = [ ] # empty list to store lines

# Open file in read mode


with open(filename, "r") as file:
for line in file:
[Link]([Link]()) # strip() removes \n at end of line

print("Lines stored in list:")


print(lines)

************* OUTPUT **************

Lines stored in list:

['Hello Python', 'Powerful Programming Language', 'Learn Python']

BHARATESH COLLEGE OF COMPUTER APPLICATIONS 6


Python Programming

7. Python GUI program to design Student Registration Form using any 5 widgets.

BHARATESH COLLEGE OF COMPUTER APPLICATIONS 7

You might also like