0% found this document useful (0 votes)
30 views

Python Program

Python programming

Uploaded by

sj9399037128
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)
30 views

Python Program

Python programming

Uploaded by

sj9399037128
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
You are on page 1/ 6

#Write a program in Python to print the area of various geometric shapes

def calculate_area():

print("Choose the geometric shape whose area u want to calculate")

print("1 - circle")

print("2 - triangle")

print("3 - square")

print("4 - rectangle")

number = int(input("Enter your choice: "))

if number == 1:

radius = int(input("Enter the radius of the circle: "))

print("The area of the circle is", 3.14*radius*radius)

elif number == 2:

base = int(input("Enter the base of triangle: "))

height = int(input("Enter the height of the triangle: "))

print("The area of triangle is", 0.5*base*height)

elif number == 3:

side = int(input("Enter the side of square: "))

print("The area of square is",side*side)

elif number == 4:

length = int(input("Enter the length of the rectangle: "))

breadth = int(input(" Enter the breadth of the rectangle: "))

print("The area of the rectangle is", length*breadth)

else:

print("INVALID CHOICE")

calculate_area()
#Write a program in python to demonstrate the working of different operators used in python and
also deduce the precedence of each operator

# Arithmetic Operators

print("Arithmetic Operators:")

a = 10

b=5

print("a + b =", a + b) # Addition

print("a - b =", a - b) # Subtraction

print("a * b =", a * b) # Multiplication

print("a / b =", a / b) # Division

print("a % b =", a % b) # Modulus

print("a ** b =", a ** b) # Exponentiation

print("a // b =", a // b) # Floor Division

# Relational Operators

print("\nRelational Operators:")

print("a > b =", a > b) # Greater than

print("a < b =", a < b) # Less than

print("a == b =", a == b) # Equal to

print("a != b =", a != b) # Not equal to

print("a >= b =", a >= b) # Greater than or equal to

print("a <= b =", a <= b) # Less than or equal to

# Logical Operators

print("\nLogical Operators:")

x = True

y = False

print("x and y =", x & y) # Logical AND

print("x or y =", x | y) # Logical OR


print("not x =", not x) # Logical NOT

# Bitwise Operators

print("\nBitwise Operators:")

c = 6 # 110 in binary

d = 3 # 011 in binary

print("c & d =", c & d) # AND

print("c | d =", c | d) # OR

print("c ^ d =", c ^ d) # XOR

print("~c =", ~c) # NOT

print("c << 1 =", c << 1) # Left Shift

print("c >> 1 =", c >> 1) # Right Shift

# Assignment Operators

print("\nAssignment Operators:")

e=5

print("e =", e)

e += 3

print("e += 3:", e)

e -= 2

print("e -= 2:", e)

e *= 2

print("e *= 2:", e)

e /= 3

print("e /= 3:", e)

e %= 4

print("e %= 4:", e)

e **= 2

print("e **= 2:", e)

e //= 3

print("e //= 3:", e)


# Membership Operators

print("\nMembership Operators:")

lst = [1, 2, 3, 4, 5]

print("2 in lst:", 2 in lst)

print("6 not in lst:", 6 not in lst)

# Operator Precedence

print("\nOperator Precedence:")

# Example expressions to deduce precedence

result1 = 10 + 5 * 2

result2 = (10 + 5) * 2

result3 = 2 ** 3 ** 2

result4 = not True or False

result5 = True or False and False

print("10 + 5 * 2 =", result1) # Multiplication (*) has higher precedence than Addition (+)

print("(10 + 5) * 2 =", result2) # Parentheses () have the highest precedence

print("2 ** 3 ** 2 =", result3) # Exponentiation (**) is right-associative

print("not True or False =", result4) # not has higher precedence than or

print("True or False and False =", result5) # and has higher precedence than or

# Write a program in Python that takes numbers from users and places them in three lists
representing even, odd and prime numbers.

def is_prime(n):

if n<=1:

return False

for i in range(2,n): #isko sqrt of n tak bhi chala skte the

if n%i==0:
return False

return True

def categorize_numbers(numbers):

even = []

odd = []

prime = []

for i in numbers:

if i%2==0:

even.append(i)

else:

odd.append(i)

if is_prime(i):

prime.append(i)

return even,odd,prime

user_input = input("Enter numbers separated by spaces: ")

numbers = list(map(int, user_input.split()))

even , odd, prime = categorize_numbers(numbers)

print("Even Numbers: ", even)

print("Odd Numbers: ", odd)

print("Prime Numbers: ", prime)

# Write a program in Python that generates Fiboacci Series and stores it in a list and dictionary
simulatneously
def generate_fibonacci_series(n):

fib_list = [] # List to store Fibonacci numbers

fib_dict = {} # Dictionary to store Fibonacci numbers with their index as key

def fibonacci(m):

if m == 0:

return 0

if m == 1:

return 1

else:

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

# Generate Fibonacci series up to n terms

for i in range(n):

fib_number = fibonacci(i) # Calculate Fibonacci number

fib_list.append(fib_number) # Store in list

fib_dict[i] = fib_number # Store in dictionary

return fib_list, fib_dict

# Taking input from the user

a = int(input("Enter the number of Fibonacci numbers to generate: "))

# Generate the Fibonacci series

fib_list, fib_dict = generate_fibonacci_series(a)

# Print the results

print("Fibonacci List:", fib_list)

print("Fibonacci Dictionary:", fib_dict)

You might also like