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

Python Lab

The document contains various Python code snippets demonstrating arithmetic operations, data type identification, data conversion, currency conversion, bit to size conversions, geometric calculations, and control flow structures. It also includes functions for calculating factorials, generating Fibonacci sequences, and finding factors of numbers. Each section is accompanied by example outputs to illustrate the functionality.
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)
6 views7 pages

Python Lab

The document contains various Python code snippets demonstrating arithmetic operations, data type identification, data conversion, currency conversion, bit to size conversions, geometric calculations, and control flow structures. It also includes functions for calculating factorials, generating Fibonacci sequences, and finding factors of numbers. Each section is accompanied by example outputs to illustrate the functionality.
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

a = 10

b=5

addition = a + b print(f"Addition: {a} + {b} = {addition}")

subtraction = a – b print(f"Subtraction: {a} - {b} = {subtraction}")

multiplication = a * b print(f"Multiplication: {a} * {b} = {multiplication}")

division = a / b print(f"Division: {a} / {b} = {division}")

modulus = a % b print(f"Modulus: {a} % {b} = {modulus}")

exponentiation = a ** b print(f"Exponentiation: {a} ** {b} = {exponentiation}")

floor_division = a // b print(f"Floor Division: {a} // {b} = {floor_division}")

#OUTPUT

Addition: 10 + 5 = 15 Subtraction: 10 - 5 = 5 Multiplication: 10 * 5 = 50

Division: 10 / 5 = 2.0 Modulus: 10 % 5 = 0 Exponentiation: 10 ** 5 = 100000

Floor Division: 10 // 5 = 2

**********************************************************

# identifying datatypes

a=2 print(a) print(type(a))

b=10.2 print(b) print(type(b))

c="ramu" print(c) print(type(c))

d=3+5j print(d) print(type(d))

e=("apple","banana","cherry") print(e) print(type(e))

f=range(7) print(f) print(type(f))

g=dict(name="name",age="18") print(g) print(type(g))

h=set["apple","banana","cherry"] print(h) print(type(h))

i=bool(3) print(i) print(type(i))

j=bytes(6) print(j) print(type(j))

k=frozenset({"apple","banana"}) print(k) print(type(k))

****************************************************
#data conv

num = 10 float_num = float(num) print(float_num)

decimal = 5.6 int_decimal = int(decimal) print(int_decimal)

number = 42 str_number = str(number) print(str_number)

string = "123" int_string = int(string) print(int_string)

string_float = "45.67" float_string = float(string_float) print(float_string)

boolean_int = 1 bool_value = bool(boolean_int) print(bool_value)

boolean_val = True int_bool = int(boolean_val) print(int_bool)

num = 5 complex_num = complex(num) print(complex_num)

#output

10.0 5 42 123 45.67 True 1 (5+0j)

*******************************************************

#U.S. dollars to Indian rupee

conversion_rate = 82.50 # Example rate, adjust as needed

usd = float(input("Enter amount in U.S. Dollars: "))

inr = usd * conversion_rate

print(f"{usd} U.S. Dollars is equal to {inr:.2f} Indian Rupees.")

#output

Enter amount in U.S. Dollars: 85.00

85.0 U.S. Dollars is equal to 7012.50 Indian Rupees.

************************************************************
#bits to mb,gb,tb

bits = float(input("Enter the number of bits: "))

megabytes = bits / (8 * 1024 * 1024) gigabytes = bits / (8 * 1024 * 1024 * 1024)

terabytes = bits / (8 * 1024 * 1024 * 1024 * 1024)

print(f"{bits} bits is equal to {megabytes:.6f} Megabytes.")

print(f"{bits} bits is equal to {gigabytes:.6f} Gigabytes.")

print(f"{bits} bits is equal to {terabytes:.6f} Terabytes.")

#output Enter the number of bits: 10000000

10000000.0 bits is equal to 1.192093 Megabytes.

10000000.0 bits is equal to 0.001164 Gigabytes.

10000000.0 bits is equal to 0.000001 Terabytes.

***************************************************

#spuare and perimeter

import math

side = float(input("Enter the side length of the square: "))

square_area = side ** 2

square_perimeter = 4 * side

print(f"Area of the square: {square_area:.2f}")

print(f"Perimeter of the square: {square_perimeter:.2f}")

radius = float(input("Enter the radius of the cone: "))

height = float(input("Enter the height of the cone: "))

slant_height = [Link](radius ** 2 + height ** 2)

cone_volume = (1/3) * [Link] * radius ** 2 * height

cone_perimeter = 2 * [Link] * radius + slant_height

print(f"Volume of the cone: {cone_volume:.2f}")

print(f"Perimeter of the cone (base + slant height): {cone_perimeter:.2f}")

#output Enter the side length of the square: 4 Area of the square: 16.00

Perimeter of the square: 16.00 Enter the radius of the cone:

*******************************************************
#even or odd

number = int(input("Enter a number: "))

if number % 2 == 0:

print("The number is even.")

else:

print("The number is odd.")

#output

Enter a number: 4

The number is even

**********************************************************

#greatest of three

a, b, c = map(int, input("Enter three numbers separated by spaces: ").split())

if a > b and a > c:

greatest = a

elif b > c:

greatest = b

else:

greatest = c

print("The greatest number is:", greatest)

#output

Enter three numbers separated by spaces: 2 8 6

The greatest number is: 8

******************************************************
#factorial of a number

num=int(input('Enter a number'))

factorial=1

for num in range(1,num+1):

factorial*=num

print(f"factorial of {num} if {factorial}")

#output

Enter a number3

factorial of 1 if 1

factorial of 2 if 2

factorial of 3 if 6

***********************************************************

#multiplication table

for i in range(1, 6):

print(f"Multiplication Table for {i}")

for j in range(1, 11):

print(f"{i} * {j} = {i * j}")

print()

*************************************************************

#factors of a given number using functions

def print_factors(n):

for i in range(1, n + 1):

if n % i == 0:

print(i)

number = int(input("Enter a number: "))

print_factors(number)

#output

Enter a number: 6

1236
****************************************************************

#factorial using recursion

def factorial(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial(n - 1)

number = int(input("Enter a number: "))

if number < 0:

print("Factorial is not defined for negative numbers.")

else:

print(f"The factorial of {number} is {factorial(number)}")

******************************************

#fibanacci sequence usinging recursion

def fibonacci(n):

if n <= 1:

return n

return fibonacci(n - 1) + fibonacci(n - 2)

def generate_fibonacci():

i=0

while True:

fib = fibonacci(i)

if fib > 100:

break

print(fib, end=" ")

i += 1

generate_fibonacci()

#output

0 1 1 2 3 5 8 13 21 34 55 89

*****************************************************************************
#factors using loop

number = int(input("Enter a number: "))

print("Factors of", number, "are:")

for i in range(1, number + 1):

if number % i == 0:

print(i)

#output

Enter a number: 6

*************************************************************

You might also like