Python_Assignment_Full
Python_Assignment_Full
Assignment
Pip (Python's package installer) is usually installed by default. You can check by
typing pip --version in the command prompt. If it's not installed, download get-
pip.py from here, and run python get-pip.py from the command prompt.
Copy code
sudo apt update
1. To install the latest version of Python 3 (Python 3.x), use the following command:
Copy code
sudo apt install python3
Copy code
sudo apt install python3-pip
Copy code
python3 --version
bash
Copy code
pip3 --version
Copy code
sudo apt install python3-venv
Copy code
python3 -m venv myenv
Copy code
source myenv/bin/activate
On Windows, the Python installer automatically adds Python to the PATH. If it's not
added, manually add the Python directory to the PATH.
On Linux, the Python binaries are usually located in /usr/bin by default, so they
should be in the PATH automatically. If not, you may need to add
/usr/local/bin/python3 to your PATH variable in the .bashrc or .zshrc file.
Install Additional Libraries with pip:
bash
Copy code
pip install numpy
Input:
Enter a number: 7
Output:
7 is Odd.
for i in range(total_subjects):
mark = float(input(f"Enter marks for subject {i + 1}: "))
marks.append(mark)
total_marks = sum(marks)
percentage = (total_marks / (total_subjects * 100)) * 100
INPUT-
Enter Roll Number: 61
Enter Name: Rakesh Thakare
Enter marks for subject 1: 80
Enter marks for subject 2: 75
Enter marks for subject 3: 60
Enter marks for subject 4: 82
Enter marks for subject 5: 90
OUTPUT-
Roll No: 61, Name: Rakesh Thakare
Marks: [80.0, 75.0, 60.0, 82.0, 90.0]
Total Marks: 387.0
Percentage: 77.40%
Grade: B
if num == sum:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
Input:
Enter a number: 153
Output:
153 is an Armstrong number.
Input:
Enter a single character: a
Output:
Given character is Vowel.
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
Input:
Enter a number: 5
Output:
The factorial of 5 is 120.
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
Input:
Enter number of terms: 10
Output:
Fibonacci series: 0 1 1 2 3 5 8 13 21 34
Input:
Enter a number to find its square: 4
Output:
The square of 4 is 16.
def decorator_with_args(greeting):
def decorator(func):
def wrapper(name):
print(f"{greeting}, {name}!")
func(name)
return wrapper
return decorator
@decorator_with_args("Hello")
def say_name(name):
print(f"My name is {name}.")
name = input("Enter your name: ")
say_name(name)
INPUT-
Enter your name: Rakesh Thakare
OUTPUT-
Hello, Rakesh Thakare!
My name is Rakesh Thakare.
def generate_numbers(n):
for i in range(n):
yield i
num = int(input("Enter the range for generating numbers: "))
for value in generate_numbers(num):
print(value, end=" ")
Input:
Enter the range for generating numbers: 5
Output:
01234
class AgeError(Exception):
pass
try:
age = int(input("Enter your age: "))
if age < 18:
raise AgeError("Age must be 18 or above.")
print("Access granted.")
except AgeError as e:
print(e)
Input:
Enter your age: 16
Output:
Age must be 18 or above.
class BalanceError(Exception):
pass
class AttemptError(Exception):
pass
balance = 5000
attempts = 0
pin = "1234"
while True:
try:
entered_pin = input("Enter your PIN: ")
if entered_pin != pin:
attempts += 1
if attempts >= 3:
raise AttemptError("Your account is blocked for an hour due to 3
incorrect PIN attempts.")
print("Incorrect PIN. Try again.")
continue
Input:
Enter your PIN: 1234
Enter the amount to withdraw: 4500
Output:
Insufficient balance. Minimum balance of 1000 must be maintained.
import random
my_list = [1, 2, 3, 4, 5]
my_tuple = ('a', 'b', 'c', 'd', 'e')
my_string = "hello"
OUTPUT-
Random item from list: 3
Random item from tuple: a
Random character from string: e
OUTPUT-
Current date and time: 2024-12-18 21:52:49.112320
a, b = 10, 5
print(f"Sum: {ao.sum(a, b)}")
print(f"Average: {ao.average(a, b)}")
print(f"Power: {ao.power(a, b)}")
print(f"Division: {ao.division(a, b)}")
print(f"Multiplication: {ao.multiplication(a, b)}")
Output:
makefile
Copy code
Sum: 15
Average: 7.5
Power: 100000
Division: 2.0
Multiplication: 50
20. Create a package MyPackageDemo. Define two
modules like message.py and calculation.py within a
package. Import given package and show desirable
output.
Package Structure:
MyPackageDemo/
├── __init__.py
├── message.py
├── calculation.py
Module: message.py
def greeting(name):
print(f"Hello, {name}!")
Module: calculation.py
def add(a, b):
return a + b
Main Program:
from MyPackageDemo import message, calculation
message.greeting("Rakesh")
print(f"Addition: {calculation.add(10, 5)}")
print(f"Subtraction: {calculat
ion.subtract(10, 5)}")
OUTPUT-
Hello, Rakesh!
Addition: 15
Subtraction: 5
class Student:
def __init__(self, roll_no, name, age, gender):
self.roll_no = roll_no
self.name = name
self.age = age
self.gender = gender
class Test(Student):
def __init__(self, roll_no, name, age, gender, marks):
super().__init__(roll_no, name, age, gender)
self.marks = marks
def total_marks(self):
return sum(self.marks)
students = [
Test(1, "Pooja", 20, "Female", [85, 90, 78]),
Test(2, "Amol", 22, "Male", [88, 76, 95]),
Test(3, "Rakesh", 19, "Male", [80, 85, 88])
]
OUTPUT-
class ComplexNumber:
def __init__(self, real, imag):
self.real = real
self.imag = imag
num1 = ComplexNumber(3, 5)
num2 = ComplexNumber(2, 4)
result = num1 + num2
print(f"Addition of complex numbers: {result}")
Output:
Addition of complex numbers: 5 + 9i
import re
def is_valid_url(url):
pattern = re.compile(
r"^(https?://)?(www\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(/[a-zA-Z0-9#]
+/?)?$" )
return bool(pattern.match(url))
Input:
Enter a URL: https://www.GOOGLE.com
Output:
https://www.GOOGLE.com is a valid URL.
import re
def is_valid_email(email):
pattern = re.compile(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]
+$")
return bool(pattern.match(email))
Input:
Enter an email address: rbthakare2000@gmail.com
Output:
rbthakare2000@gmail.com is a valid email address.
import time
def print_numbers():
for i in range(5):
print(f"Number: {i}")
time.sleep(1)
thread = threading.Thread(target=print_numbers)
print("Thread State: NEW")
thread.start()
print("Thread State: RUNNING")
thread.join()
print("Thread State: TERMINATED")
Output:
mathematica
Copy code
Thread State: NEW
Thread State: RUNNING
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Thread State: TERMINATED
client = MongoClient("mongodb://localhost:27017/")
db = client["testdb"]
collection = db["testcollection"]
collection.delete_one({"name": "Rakesh"})
print("Data deleted.")
Output:
Data inserted.
Data from database:
{'_id': ObjectId('...'), 'name': 'Rakesh', 'age': 24, 'city': 'Malegaon'}
Data updated.
Data deleted.