Python Experiments
Python Experiments
1
Theory:
1. Python Scripts have the file extension .py
2. Any IDE like VS Code / Google Colab / Anaconda can be used for executing Python
Scripts.
Program :
Test Cases:
estCase-1:
T
Hello world !
Objective :Write and Execute a Simple Python Program to print Python Data Types.
heory:
T
Python Data types are the classification or categorization of data items. Data types represent
the kind of value , and operations can be performed on a particular data.Since everything is an
object in Python programming, Python data types are classes and variables are instances
(objects) of these classes. The following are the standard or built-in data types in Python:
● Numeric – int, float, complex
● Sequence Type – string, list, tuple
● Mapping Type – dict
● Boolean – bool
● Set Type – set, frozenset
● Binary Types –bytes,bytearray,memoryview
Program :
# integer numbers
a =5
print(a,type(a))
# floating numbers
b =5.0
print(b,type(b))
# complex numbers
c =2+4j
print(c,type(c))
Test Cases:
estCase-1:
T
5 <class 'int'>
5.0 <class 'float'>
(2+4j) <class 'complex'>
[1, 2, 3] <class 'list'>
(1, 2, 3) <class 'tuple'>
{1, 2, 3} <class 'set'>
{'21154-cm-001': 'suresh', '21154-cm-002': 'ramesh', '21154-cm-003': 'havesh'} <class 'dict'>
heory:
T
The standard arithmetic operators in Python are:
● : Addition
+
● -Subtraction
● \*: Multiplication
● /: Division
● //: Floor division
● %: Modulus
● **** Exponentiation
Program :
Addition
#
addition = 10 + 5
print(f"Addition (10 + 5): {addition}")
Subtraction
#
subtraction = 10 - 5
print(f"Subtraction (10 - 5): {subtraction}")
Multiplication
#
multiplication = 10 * 5
print(f"Multiplication (10 * 5): {multiplication}")
Division
#
division = 10 / 5
print(f"Division (10 / 5): {division}")
Modulus
#
modulus = 10 % 3
print(f"Modulus (10 % 3): {modulus}")
Exponentiation
#
exponentiation = 2 ** 3
print(f"Exponentiation (2 ** 3): {exponentiation}")
Floor Division
#
floor_division = 10 // 3
print(f"Floor Division (10 // 3): {floor_division}")
Test Cases:
estCase-1:
T
Addition (10 + 5): 15
Subtraction (10 - 5): 5
Multiplication (10 * 5): 50
Division (10 / 5): 2.0
Modulus (10 % 3): 1
Exponentiation (2 ** 3): 8
Floor Division (10 // 3): 3
heory:
T
Data type conversion in Python refers to the process of converting one data type to another.
The user manually converts a data type using functions likeint(),float(),str(),list(), etc. This is
necessary to ensure compatibility between operations. Proper type conversion prevents errors
and ensures accurate data handling.
Program :
Integer to Float
#
int_to_float = float(10)
print(f"Integer to Float: {int_to_float}")
Float to Integer
#
float_to_int = int(10.7)
print(f"Float to Integer: {float_to_int}")
Integer to String
#
int_to_str = str(10)
print(f"Integer to String: {int_to_str}")
String to Integer
#
str_to_int = int("10")
print(f"String to Integer: {str_to_int}")
String to Float
#
str_to_float = float("10.5")
print(f"String to Float: {str_to_float}")
Test Cases:
estCase-1:
T
Integer to Float: 10.0
Float to Integer: 10
Integer to String: 10
String to Integer: 10
String to Float: 10.5
Result :Successfully executed a Python Program involving Data type conversion in Python.
Experiment No. 5
Objective :Write and Execute a Python Program to convert US Dollars to Indian Rupee.
Theory:
1. The currency conversion process involves multiplying the amount in US dollars by the
current exchange rate of the Indian Rupee.
2. For example, if 1 USD = 85.62 INR, then 10 USD will be equivalent to 856.2 INR.
Program :
Conversion
#
inr_amount = usd_amount * exchange_rate
Output
#
print(f"{usd_amount} USD is equivalent to {inr_amount:.2f} INR.")
Test Cases:
estCase-1:
T
Enter amount in US Dollars: 20
20.0 USD is equivalent to 1712.40 INR.
estCase-2:
T
Enter amount in US Dollars: 55
55.0 USD is equivalent to 4709.10 INR.
bjective :Write and Execute a Python Program to convert bits to MegaBytes, GigaBytes and
O
TeraBytes.
heory:
T
Data is measured in bits, bytes, kilobytes, megabytes, gigabytes, and terabytes.
Conversion from bits to higher units involves dividing by specific powers of 2 or 10, depending
on the context.
In this program, we use the binary system for conversion:
1 Byte = 8 bits
1 KB = 1024 Bytes
1 MB = 1024 KB
1 GB = 1024 MB
1 TB = 1024 GB
Program :
ytes_ = bits / 8
b
kilobytes = bytes_ / 1024
megabytes = kilobytes / 1024
gigabytes = megabytes / 1024
terabytes = gigabytes / 1024
# Output
print(f" {bits}bits is equivalent to:")
print(f" Bytes{bytes}")
print(f" KiloBytes{kilobytes}")
print(f" MegaBytes{megabytes}")
print(f" GigaBytes{gigabytes}")
print(f" TeraBytes{terabytes}")
Test Cases:
estCase-1:
T
Enter the number of bits: 356489
356489 bits is equivalent to:
Bytes 44561.125
KiloBytes 43.5167236328125
MegaBytes 0.04249680042266846
GigaBytes 4.1500781662762165e-05
TeraBytes 4.052810709254118e-08
esult :Successfully Executed a Python Program to convert bits to MegaBytes, GigaBytes and
R
TeraBytes.