
























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























































































Program 1 Write a Python program to print "Hello Python". In [1]: Program 2 Write a Python program to do arithmetical operations addition and division. In [2]: In [3]: Program 3 Write a Python program to find the area of a triangle. In [4]: Hello Python Enter the first number for addition: 5 Enter the second number for addition: 6 sum: 5.0 + 6.0 = 11. Enter the dividend for division: 25 Enter the divisor for division: 5 Division: 25.0 / 5.0 = 5. Enter the length of the base of the triangle: 10 Enter the height of the triangle: 15 The area of the triangle is: 75. print("Hello Python") # Addition num1 = float(input("Enter the first number for addition: ")) num2 = float(input("Enter the second number for addition: ")) sum_result = num1 + num print(f"sum: {num1} + {num2} = {sum_result}") # Division num3 = float(input("Enter the dividend for division: ")) num4 = float(input("Enter the divisor for division: ")) if num4 == 0 : print("Error: Division by zero is not allowed.") else : div_result = num3 / num print(f"Division: {num3} / {num4} = {div_result}") # Input the base and height from the user base = float(input("Enter the length of the base of the triangle: ")) height = float(input("Enter the height of the triangle: ")) # Calculate the area of the triangle area = 0.5 ***** base ***** height # Display the result print(f"The area of the triangle is: {area}")
In [8]: Program 8 Write a Python program to display calendar. In [9]: Program 9 Write a Python program to solve quadratic equation. The standard form of a quadratic equation is: where a, b and c are real numbers and The solutions of this quadratic equation is given by:
2
Enter temperature in Celsius: 37 37.0 degrees Celsius is equal to 98.6 degrees Fahrenheit Enter year: 2023 Enter month: 11 November 2023 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 celsius = float(input("Enter temperature in Celsius: ")) # Conversion formula: Fahrenheit = (Celsius * 9/5) + 32 fahrenheit = (celsius ***** 9 / 5 ) + 32 print(f"{celsius} degrees Celsius is equal to {fahrenheit} degrees Fahr import calendar year = int(input("Enter year: ")) month = int(input("Enter month: ")) cal = calendar.month(year, month) print(cal)
In [10]: Program 10 Write a Python program to swap two variables without temp variable. In [11]: Enter coefficient a: 1 Enter coefficient b: 4 Enter coefficient c: 8 Root 1: -2.0 + 2.0i Root 2: -2.0 - 2.0i After swapping: a = 10 b = 5 import math # Input coefficients a = float(input("Enter coefficient a: ")) b = float(input("Enter coefficient b: ")) c = float(input("Enter coefficient c: ")) # Calculate the discriminant discriminant = b ****** 2 - 4 ***** a ***** c # Check if the discriminant is positive, negative, or zero if discriminant > 0 : # Two real and distinct roots root1 = ( - b + math.sqrt(discriminant)) / ( 2 ***** a) root2 = ( - b - math.sqrt(discriminant)) / ( 2 ***** a) print(f"Root 1: {root1}") print(f"Root 2: {root2}") elif discriminant == 0 : # One real root (repeated) root = - b / ( 2 ***** a) print(f"Root: {root}") else : # Complex roots real_part = - b / ( 2 ***** a) imaginary_part = math.sqrt(abs(discriminant)) / ( 2 ***** a) print(f"Root 1: {real_part} + {imaginary_part}i") print(f"Root 2: {real_part} - {imaginary_part}i") a = 5 b = 10 # Swapping without a temporary variable a, b = b, a print("After swapping:") print("a =", a) print("b =", b)
Program 14 Write a Python Program to Check Prime Number. Prime Numbers: A prime number is a whole number that cannot be evenly divided by any other number except for 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers because they cannot be divided by any other positive integer except for 1 and their own value. In [15]: Program 15 ¶ Write a Python Program to Print all Prime Numbers in an Interval of 1-10. Enter a number: 27 27, is not a prime number num = int(input("Enter a number: ")) # define a flag variable flag = False if num == 1 : print(f"{num}, is not a prime number") elif num > 1 : # check for factors for i in range( 2 , num): if (num % i) == 0 : flag = True _# if factor is found, set flag to True
break # check if flag is True if flag: print(f"{num}, is not a prime number") else : print(f"{num}, is a prime number")
In [20]: Program 16 Write a Python Program to Find the Factorial of a Number. In [21]: Program 17 Write a Python Program to Display the multiplication Table. Prime numbers between 1 and 10 are: 2 3 5 7 Enter a number: 4 The factorial of 4 is 24 # Python program to display all the prime numbers within an interval lower = 1 upper = 10 print("Prime numbers between", lower, "and", upper, "are:") for num in range(lower, upper + 1 ): # all prime numbers are greater than 1 if num > 1 : for i in range( 2 , num): if (num % i) == 0 : break else : print(num) num = int(input("Enter a number: ")) factorial = 1 if num < 0 : print("Factirial does not exist for negative numbers") elif num == 0 : print("Factorial of 0 is 1") else : for i in range( 1 , num + 1 ): factorial = factorial ***** i print(f'The factorial of {num} is {factorial}')
In [23]: Program 19 Write a Python Program to Check Armstrong Number? Armstrong Number: It is a number that is equal to the sum of its own digits, each raised to a power equal to the number of digits in the number. For example, let's consider the number 153: It has three digits (1, 5, and 3). If we calculate + , we get , which is equal to. So, 153 is an Armstrong number because it equals the sum of its digits raised to the power of the number of digits in the number. Another example is 9474: It has four digits (9, 4, 7, and 4).
3
3
3
How many terms? 10 Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34 nterms = int(input("How many terms? ")) # first two terms n1, n2 = 0 , 1 count = 0 # check if the number of terms is valid if nterms <= 0 : print("Please enter a positive integer") # if there is only one term, return n elif nterms == 1 : print("Fibonacci sequence upto",nterms,":") print(n1) # generate fibonacci sequence else : print("Fibonacci sequence:") while count < nterms: print(n1) nth = n1 + n # update values n1 = n n2 = nth count += 1
If we calculate , we get , which is also equal to. Therefore, 9474 is an Armstrong number as well.
In [25]: Program 20 Write a Python Program to Find Armstrong Number in an Interval. Enter a number: 9474 9474 is an Armstrong number. num = int(input("Enter a number: ")) # Calculate the number of digits in num num_str = str(num) num_digits = len(num_str) # Initialize variables sum_of_powers = 0 temp_num = num # Calculate the sum of digits raised to the power of num_digits while temp_num > 0 : digit = temp_num % 10 sum_of_powers += digit ****** num_digits temp_num //= 10 # Check if it's an Armstrong number if sum_of_powers == num: print(f"{num} is an Armstrong number.") else : print(f"{num} is not an Armstrong number.")
Program 22 Write a Python Program to Find LCM. Least Common Multiple (LCM): LCM, or Least Common Multiple, is the smallest multiple that is exactly divisible by two or more numbers. Formula: For two numbers a and b, the LCM can be found using the formula: For more than two numbers, you can find the LCM step by step, taking the LCM of pairs of numbers at a time until you reach the last pair. Note: GCD stands for Greatest Common Divisor.
In [1]: Program 23 Write a Python Program to Find HCF. Highest Common Factor(HCF): HCF, or Highest Common Factor, is the largest positive integer that divides two or more numbers without leaving a remainder. Formula: Enter the number: 54 Enter the number: 24 The L.C.M. is 216 # Python Program to find the L.C.M. of two input number def compute_lcm(x, y): if x > y: # choose the greater number greater = x else : greater = y while ( True ): if ((greater % x == 0 ) and (greater % y == 0 )): lcm = greater break greater += 1 return lcm num1 = int(input('Enter the number: ')) num2 = int(input('Enter the number: ')) print("The L.C.M. is", compute_lcm(num1, num2))
For two numbers a and b, the HCF can be found using the formula: For more than two numbers, you can find the HCF by taking the GCD of pairs of numbers at a time until you reach the last pair. Note: GCD stands for Greatest Common Divisor.
In [2]: Program 24 Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal. How can we manually convert a decimal number to binary, octal and hexadecimal? Converting a decimal number to binary, octal, and hexadecimal involves dividing the decimal number by the base repeatedly and noting the remainders at each step. Here's a simple example: Let's convert the decimal number 27 to binary, octal, and hexadecimal.
def compute_hcf(x, y): # choose the smaller number if x > y: smaller = y else : smaller = x for i in range( 1 , smaller + 1 ): if ((x % i == 0 ) and (y % i == 0 )): hcf = i return hcf num1 = int(input('Enter the number: ')) num2 = int(input('Enter the number: ')) print("The H.C.F. is", compute_hcf(num1, num2))
In [5]: # This function adds two numbers def add(x, y): return x + y # This function subtracts two numbers def subtract(x, y): return x - y # This function multiplies two numbers def multiply(x, y): return x ***** y # This function divides two numbers def divide(x, y): return x / y print("Select operation.") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide") while True : # take input from the user choice = input("Enter choice(1/2/3/4): ") # check if choice is one of the four options if choice in ('1', '2', '3', '4'): try : num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) except ValueError: print("Invalid input. Please enter a number.") continue if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) _# check if user wants another calculation
next_calculation = input("Let's do next calculation? (yes/no): if next_calculation == "no": break else : print("Invalid Input")
Program 27 Write a Python Program to Display Fibonacci Sequence Using Recursion. Fibonacci sequence: The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In mathematical terms, it is defined by the recurrence relation ( F(n) = F(n-1) + F(n-2) ), with initial conditions ( F(0) = 0 ) and ( F(1) = 1 ). The sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. The Fibonacci sequence has widespread applications in mathematics, computer science, nature, and art. Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 1 Enter first number: 5 Enter second number: 6 5.0 + 6.0 = 11. Let's do next calculation? (yes/no): yes Enter choice(1/2/3/4): 2 Enter first number: 50 Enter second number: 5 50.0 - 5.0 = 45. Let's do next calculation? (yes/no): yes Enter choice(1/2/3/4): 3 Enter first number: 22 Enter second number: 2 22.0 * 2.0 = 44. Let's do next calculation? (yes/no): yes Enter choice(1/2/3/4): 4 Enter first number: 99 Enter second number: 9 99.0 / 9.0 = 11. Let's do next calculation? (yes/no): no
In [11]: Program 29 Write a Python Program to calculate your Body Mass Index. Body Mass Index (BMI) is a measure of body fat based on an individual's weight and height. It is commonly used as a screening tool to categorize individuals into different weight status categories, such as underweight, normal weight, overweight, and obesity. The BMI is calculated using the following formula: Alternatively, in the imperial system: BMI provides a general indication of body fatness but does not directly measure body fat or distribution. It is widely used in public health and clinical settings as a quick and simple tool to assess potential health risks associated with weight. Different BMI ranges are associated with different health categories, but it's important to note that BMI has limitations and does not account for factors such as muscle mass or distribution of fat.
Enter the number: 7 The factorial of 7 is 5040 # Factorial of a number using recursion def recur_factorial(n): if n == 1 : return n else : return n ***** recur_factorial(n - 1 ) num = int(input("Enter the number: ")) # check if the number is negative if num < 0 : print("Sorry, factorial does not exist for negative numbers") elif num == 0 : print("The factorial of 0 is 1") else : print("The factorial of", num, "is", recur_factorial(num))
In [12]: Program 30 Write a Python Program to calculate the natural logarithm of any number. The natural logarithm , often denoted as , is a mathematical function that represents the logarithm to the base , where is the mathematical constant approximately equal to
. In other words, for a positive number , the natural logarithm of is the exponent that satisfies the equation. Mathematically, the natural logarithm is expressed as: It is commonly used in various branches of mathematics, especially in calculus and mathematical analysis, as well as in fields such as physics, economics, and engineering. The natural logarithm has properties that make it particularly useful in situations involving exponential growth or decay.
Enter your height in meters: 1. Enter your weight in kg: 70 Welcome to the BMI calculator. Your BMI is: 21. Your weight is normal. def bodymassindex(height, weight): return round((weight / height ****** 2 ), 2 ) h = float(input("Enter your height in meters: ")) w = float(input("Enter your weight in kg: ")) print("Welcome to the BMI calculator.") bmi = bodymassindex(h, w) print("Your BMI is: ", bmi) if bmi <= 18.5: print("You are underweight.") elif 18.5 < bmi <= 24.9: print("Your weight is normal.") elif 25 < bmi <= 29.29: print("You are overweight.") else : print("You are obese.")