PYTHON PRACTIAL FILE - Aman
PYTHON PRACTIAL FILE - Aman
# Arithmetic operations
a = 10
b=3
# Addition
addition_result = a + b
print("Addition:", addition_result)
# Subtraction
subtraction_result = a - b
print("Subtraction:", subtraction_result)
# Multiplication
multiplication_result = a * b
print("Multiplication:", multiplication_result)
# Division
division_result = a / b
1
print("Division:", division_result)
# Modulo
modulo_result = a % b
print("Modulo:", modulo_result)
CONCLUSION
This program defines variables for each of the different number data types
(integer, floating-point, and complex), and then demonstrates various
arithmetic operations like addition, subtraction, multiplication, division,
and modulo using integer values a and b.
2
OUTPUT
3
PROGRAM – 2
AIM : WRITE A PROGRAM TO PERFORM DIFFERENT
ARITHMETIC OPERATIONS ON NUMBERS IN
PYTHON.
# Addition
addition_result = num1 + num2
print("Addition:", addition_result)
# Subtraction
subtraction_result = num1 - num2
print("Subtraction:", subtraction_result)
# Multiplication
multiplication_result = num1 * num2
print("Multiplication:", multiplication_result)
# Division
if num2 != 0:
division_result = num1 / num2
print("Division:", division_result)
else:
print("Division by zero is not allowed.")
# Modulo
if num2 != 0:
modulo_result = num1 % num2
print("Modulo:", modulo_result)
else:
print("Modulo by zero is not allowed.")
4
CONCLUSION
This program takes two numbers as input from the user, then performs
addition, subtraction, multiplication, division (if the second number is not
zero), and modulo (if the second number is not zero) operations on those
numbers, and displays the results.
5
OUTPUT
6
PROGRAM – 3
AIM : WRITE A PROGRAM TO CREATE, CONCATENATE
AND PRINT A STRING AND ACCESSING SUB-STRING
FROM A GIVEN STRING.
# Create a string
str1 = "Hello, "
# Accessing a substring
substring = result_string[0:5] # Access the first 5 characters
print("Substring:", substring)
CONCLUSION
This program first creates a string str1, then concatenates it with another
string str2 to form result_string. It prints the concatenated string and
demonstrates how to access a substring from the beginning and the end of
the result_string.
7
OUTPUT
8
PROGRAM – 4
AIM : WRITE A PYTHON SCRIPT TO PRINT THE CURRENT
DATE IN THE FOLLOWING FORMAT “FRI OCT 11
02:26:23 IST2019”
import datetime
CONCLUSION
When you run this script, it will retrieve the current date and time and
format it in the "FRI OCT 11 02:26:23 IST2019" format. The output will
look something like this:
9
OUTPUT
10
PROGRAM – 5
AIM : WRITE A PROGRAM TO CREATE, APPEND, AND
REMOVE LISTS IN PYTHON.
11
# Print the list after removing an element by index
print(f"Removed element at index 1: {removed_element}")
print("List after removing element at index 1:", my_list)
CONCLUSION
In this program –
This program begins by creating an empty list called my_list and then
appends elements to it using the append() method. It demonstrates
removing an element by value using the remove() method, appending more
elements, and removing an element by index using the pop() method.
Finally, it prints the list after each operation to show the changes.
12
OUTPUT
13
PROGRAM – 6
AIM : WRITE A PROGRAM TO DEMONSTRATE WORKING
WITH TUPLES IN PYTHON.
# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
# Tuple unpacking
a, b, c, d, e = my_tuple
print("Tuple Unpacking:")
print("a =", a)
print("b =", b)
print("c =", c)
print("d =", d)
print("e =", e)
14
# Finding the length of a tuple
tuple_length = len(my_tuple)
print("Length of the tuple:", tuple_length)
CONCLUSION
15
OUTPUT
16
PROGRAM – 7
AIM : WRITE A PROGRAM TO DEMONSTRATE WORKING
WITH DICTIONARIES IN PYTHON.
# Creating a dictionary
my_dict = {
"name": "John",
"age": 30,
"city": "New York"
}
CONCLUSION
18
OUTPUT
19
PROGRAM – 8
AIM : WRITE A PYTHON PROGRAM TO FIND LARGEST OF
THREE NUMBERS.
20
CONCLUSION
In this program –
OUTPUT
21
PROGRAM – 9
AIM : WRITE A PYTHON PROGRAM TO CONSTRUCT THE
FOLLOWING PATTERN, USING A NESTED FOR LOOP
*
**
***
****
*****
****
***
**
*
22
# Create the lower part of the pattern
for i in range(num_rows - 1, 0, -1):
for j in range(1, i + 1):
print("*", end=" ")
print()
CONCLUSION
This program first creates the upper part of the pattern with increasing
numbers of asterisks in each row, and then it creates the lower part of the
pattern with decreasing numbers of asterisks in each row. When you run the
program, it will generate the requested pattern.
23
OUTPUT
24
PROGRAM – 10
AIM : WRITE A PYTHON SCRIPT THAT PRINTS PRIME
NUMBERS LESS THAN 20.
25
if is_prime(number):
print(number, end=" ")
CONCLUSION
These are the prime numbers less than 20. The script uses a is_prime()
function to check for primality and then iterates through numbers to find
and print prime numbers within the specified range.
OUTPUT
26
PROGRAM – 11
AIM : WRITE A PYTHON PROGRAM TO DEFINE A MODULE
TO FIND FIBONACCI NUMBERS AND IMPORT THE
MODULE TO ANOTHER PROGRAM.
27
n = int(input("Enter the number of Fibonacci numbers to calculate: "))
CONCLUSION
28
OUTPUT
29
PROGRAM – 12
AIM : WRITE A PYTHON PROGRAM TO DEFINE A MODULE
AND IMPORT A SPECIFIC FUNCTION IN THAT
MODULE TO ANOTHER PROGRAM.
# Function 1
def add(a, b):
return a + b
# Function 2
def subtract(a, b):
return a - b
# Function 3
def multiply(a, b):
return a * b
# Function 4
def divide(a, b):
30
if b != 0:
return a / b
else:
return "Cannot divide by zero"
CONCLUSION
31
OUTPUT
32
PROGRAM – 13
AIM : WRITE A PROGRAM THAT INPUTS A TEXT FILE.
THE PROGRAM SHOULD PRINT ALL OF THE
UNIQUE WORDS IN THE FILE IN ALPHABETICAL
ORDER.
# Function to read a text file and return unique words in alphabetical order
def get_unique_words(filename):
unique_words = set()
try:
with open(filename, 'r') as file:
for line in file:
words = line.split()
for word in words:
# Remove punctuation and convert to lowercase
cleaned_word = word.strip('.,?!;()[]{}"\'').lower()
33
if cleaned_word:
unique_words.add(cleaned_word)
except FileNotFoundError:
print("File not found.")
return sorted(unique_words)
CONCLUSION
In this program –
34
OUTPUT
35
PROGRAM – 14
AIM : WRITE A PYTHON CLASS TO CONVERT AN INTEGER
TO A ROMAN NUMERAL.
36
class IntegerToRoman:
def __init__(self):
# Define the Roman numeral symbols and their corresponding values
self.roman_symbols = {
'M': 1000, 'CM': 900, 'D': 500, 'CD': 400,
'C': 100, 'XC': 90, 'L': 50, 'XL': 40,
'X': 10, 'IX': 9, 'V': 5, 'IV': 4, 'I': 1
}
roman_numeral = ""
for symbol, value in self.roman_symbols.items():
while num >= value:
roman_numeral += symbol
num -= value
return roman_numeral
CONCLUSION
37
In this code –
OUTPUT
38
PROGRAM – 15
AIM : WRITE A PYTHON CLASS TO REVERSE A STRING
WORD BY WORD.
39
class ReverseWords:
def reverse_words(self, input_str):
# Split the input string into words
words = input_str.split()
return reversed_str
CONCLUSION
In this code –
40
OUTPUT
41
42