Python Practial File
Python Practial File
# Integer
x=5
print("Integer (x):", x, "Type:", type(x))
# Floating-Point Number
y = 3.14
print("Floating-Point (y):", y, "Type:", type(y))
# Complex Number
z = 2 + 3j
print("Complex (z):", z, "Type:", type(z))
# Addition
addition_result = a + b
print("Addition (a + b):", addition_result)
# Subtraction
subtraction_result = a - b
print("Subtraction (a - b):", subtraction_result)
# Multiplication
multiplication_result = a * b
print("Multiplication (a * b):", multiplication_result)
1
print("Division (a / b):", division_result)
# Modulus (remainder)
modulus_result = a % b
print("Modulus (a % b):", modulus_result)
# Exponentiation
exponentiation_result = a ** b
print("Exponentiation (a ** b):", exponentiation_result)
CONCLUSION
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:", num1, "+", num2, "=", addition_result)
# Subtraction
subtraction_result = num1 - num2
print("Subtraction:", num1, "-", num2, "=", subtraction_result)
# Multiplication
multiplication_result = num1 * num2
print("Multiplication:", num1, "*", num2, "=", multiplication_result)
4
else:
print("Integer Division by zero is not allowed.")
# Modulus (remainder)
if num2 != 0:
modulus_result = num1 % num2
print("Modulus:", num1, "%", num2, "=", modulus_result)
else:
print("Modulus by zero is not allowed.")
# Exponentiation
exponentiation_result = num1 ** num2
print("Exponentiation:", num1, "**", num2, "=", exponentiation_result)
CONCLUSION
This program takes two numbers as input from the user, performs addition,
subtraction, multiplication, division (with error handling for division by
zero), integer division, modulus, and exponentiation operations, and then
prints 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
string1 = "Hello, "
string2 = "World!"
# Concatenate strings
concatenated_string = string1 + string2
print("Concatenated String:", concatenated_string)
# Accessing Substrings
original_string = "This is a sample string."
7
print("Last Character:", last_char)
CONCLUSION
This program first creates two strings, string1 and string2, and then
concatenates them into concatenated_string. After that, it accesses
substrings from the original_string using slicing and indexing. Finally, it
prints the results.
8
OUTPUT
9
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 print the current date and time in the
specified format, including the day of the week, month, day, time,
timezone, and year.
10
OUTPUT
11
PROGRAM – 5
AIM : WRITE A PROGRAM TO CREATE, APPEND, AND
REMOVE LISTS IN PYTHON.
12
CONCLUSION
In this program –
13
OUTPUT
14
PROGRAM – 6
AIM : WRITE A PROGRAM TO DEMONSTRATE WORKING
WITH TUPLES IN PYTHON.
# Creating a tuple
my_tuple = (1, 2, 3, 4, 5)
# Slicing a tuple
print("Slice from index 1 to 3:", my_tuple[1:4]) # Note: The end index is
exclusive
# Length of a tuple
print("Length of the tuple:", len(my_tuple))
15
# Iterating through a tuple
print("Iterating through the tuple:")
for item in my_tuple:
print(item)
CONCLUSION
16
OUTPUT
17
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",
}
18
# Checking if a key exists in a dictionary
key_to_check = "country"
if key_to_check in my_dict:
print(key_to_check, "exists in the dictionary.")
else:
print(key_to_check, "does not exist in the dictionary.")
CONCLUSION
19
OUTPUT
20
PROGRAM – 8
AIM : WRITE A PYTHON PROGRAM TO FIND LARGEST OF
THREE NUMBERS.
21
CONCLUSION
In this program –
You can run this program and input three numbers to find the largest
among them.
22
OUTPUT
23
PROGRAM – 9
AIM : WRITE A PYTHON PROGRAM TO CONSTRUCT THE
FOLLOWING PATTERN, USING A NESTED FOR LOOP
*
**
***
****
*****
****
***
**
*
24
print()
CONCLUSION
This program uses two nested for loops to create the pattern. The first loop
constructs the top half of the pattern, while the second loop constructs the
bottom half in reverse order. When you run this program, it will print the
pattern as specified.
25
OUTPUT
26
PROGRAM – 10
AIM : WRITE A PYTHON SCRIPT THAT PRINTS PRIME
NUMBERS LESS THAN 20.
27
# Find and print prime numbers less than 20
print("Prime numbers less than 20:")
for i in range(2, 20):
if is_prime(i):
print(i, end=" ")
CONCLUSION
When you run this script, it will output the prime numbers less than 20:
28
OUTPUT
29
PROGRAM – 11
AIM : WRITE A PYTHON PROGRAM TO DEFINE A MODULE
TO FIND FIBONACCI NUMBERS AND IMPORT THE
MODULE TO ANOTHER PROGRAM.
30
import fibonacci_module
CONCLUSION
When you run main_program.py and input the number of Fibonacci terms,
it will import the fibonacci_module and display the Fibonacci sequence.
31
OUTPUT
32
PROGRAM – 12
AIM : WRITE A PYTHON PROGRAM TO DEFINE A MODULE
AND IMPORT A SPECIFIC FUNCTION IN THAT
MODULE TO ANOTHER PROGRAM.
33
# Import a specific function from the module
from my_module import calculate_square
CONCLUSION
When you run main_program.py, it will import the specific function and
display the square of the entered number.
34
OUTPUT
35
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 extract unique words from a text file and print them in
alphabetical order
def extract_and_print_unique_words(file_path):
unique_words = set() # Using a set to store unique words
try:
with open(file_path, 'r') as file:
# Read each line from the file
36
for line in file:
# Split the line into words
words = line.split()
except FileNotFoundError:
print(f"The file '{file_path}' was not found.")
CONCLUSION
In this program –
37
Ensure that you have a text file with the content you want to analyze, and
provide its path when prompted.
When you run this program and input the file path, it will print all unique
words from the file in alphabetical order.
OUTPUT
38
PROGRAM – 14
AIM : WRITE A PYTHON CLASS TO CONVERT AN INTEGER
TO A ROMAN NUMERAL.
39
class IntegerToRoman:
def int_to_roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4, 1
]
syms = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_numeral = ''
i=0
while num > 0:
for _ in range(num // val[i]):
roman_numeral += syms[i]
num -= val[i]
i += 1
return roman_numeral
# Example usage
number = 1994
roman_numeral = converter.int_to_roman(number)
print(f"{number} in Roman numerals is: {roman_numeral}")
CONCLUSION
40
In this code –
2. It uses two lists, val and syms, to store the values and symbols of
Roman numerals.
3. The method iterates through the values in val and adds the
corresponding symbols from syms to the roman_numeral string while
subtracting the value from the input number until the number
becomes zero.
OUTPUT
41
PROGRAM – 15
42
AIM : WRITE A PYTHON CLASS TO REVERSE A STRING
WORD BY WORD.
class StringReverser:
def reverse_words(self, s):
# Split the input string into words
words = s.split()
return reversed_string
# Example usage
input_string = "Hello World"
reversed_string = reverser.reverse_words(input_string)
print(f"Original string: {input_string}")
print(f"Reversed string: {reversed_string}")
CONCLUSION
43
In this code –
2. The input string is split into words using the split method.
4. The reversed words are joined back into a string with spaces between
them.
OUTPUT
44
45