PYTHON SIMPLE PROGRAMS PYTHON BASICS Python Program to
PYTHON SIMPLE PROGRAMS PYTHON BASICS
Python Program to Add Two Numbers we've used the arithmetic addition operator (+) to add two numbers
ADD TWO NUMBERS # This program adds two numbers # Add two numbers num 1 = 1. 5 num 2 = 6. 3 # Display the sum = float(num 1) + float(num 2) print('The sum of {0} and {1} is {2}'. format(num 1, num 2, sum))
Add Two Numbers Provided by The User num 1 = input('Enter first number: ') num 2 = input('Enter second number: ') # Add two numbers sum = float(num 1) + float(num 2) # Display the sum print('The sum of {0} and {1} is {2}'. format(num 1, num 2, sum))
Python Program to Find the Square Root # Python Program to calculate the square root # Note: change this value for a different result num = 8 # uncomment to take the input from the user# num = float(input('Enter a number: ')) num_sqrt = num ** 0. 5 print('The square root of %0. 3 f is %0. 3 f'%(num , num_sqrt))
Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable a=int(input("Enter value of first variable: ")) b=int(input("Enter value of second variable: ")) a=a+b b=a-b a=a-b print("a is: ", a, " b is: ", b)
Python program to do arithmetical operations num 1=input("enter the first number: ") num 2=input("enter the second number: ") #addition sum = float(num 1) + float(num 2) print("addition of two numbers", sum) #subtraction min = float(num 1) - float(num 2) print("subtraction of two numbers", min) # Multiply two numbers mul = float(num 1) * float(num 2) print("mul of two numbers", mul) #Divide two numbers div = float(num 1) / float(num 2) print("division of two numbers", div)
Python program to find the area of a triangle Area of a triangle = (s*(s-a)*(s-b)*(s-c))-1/2 Here s is the semi-perimeter and a, b and c are three sides of the triangle.
Python program to find the area of a triangle # Three sides of the triangle is a, b and c: a = float(input('Enter first side: ')) b = float(input('Enter second side: ')) c = float(input('Enter third side: ')) # calculate the semi-perimeter s = (a + b + c) / 2 # calculate the area = (s*(s-a)*(s-b)*(s-c)) ** 0. 5 print('The area of the triangle is %0. 2 f' %area)
Python program to solve quadratic equation ax 2+bx+c=0 Here, "x" is unknown which you have to find and "a", "b", "c" specifies the numbers such that "a" is not equal to 0. If a = 0 then the equation becomes liner not quadratic anymore. In the equation, a, b and c are called coefficients. Let's take an example to solve the quadratic equation 8 x 2 + 16 x + 8 = 0
Python program to solve quadratic equation # import complex math module import cmath a = float(input('Enter a: ')) b = float(input('Enter b: ')) c = float(input('Enter c: ')) # calculate the discriminant d = (b**2) - (4*a*c) # find two solutions sol 1 = (-b-cmath. sqrt(d))/(2*a) sol 2 = (-b+cmath. sqrt(d))/(2*a) print('The solution are {0} and {1}'. format(sol 1, sol 2))
Even #Python Program to Check if a Number is Odd or Even num = int(input("Enter a number: ")) if (num % 2) == 0: print("{0} is Even". format(num)) else: print("{0} is Odd". format(num))
Numbers #Python Program to Find the Largest Among Three Numbers num 1 = 10 num 2 = 14 if (num 1 >= num 2): print("largest no is", num 1) else: print("largest no is", num 2)
Three Numbers #Python Program to Find the Largest Among Three Numbers num 1 = 10 num 2 = 14 num 3 = 12 # uncomment following lines to take three numbers from user #num 1 = float(input("Enter first number: ")) #num 2 = float(input("Enter second number: ")) #num 3 = float(input("Enter third number: ")) if (num 1 >= num 2) and (num 1 >= num 3): largest = num 1 elif (num 2 >= num 1) and (num 2 >= num 3): largest = num 2 else: largest = num 3 print("The largest number between", num 1, ", ", num 2, "and", num 3, "is", largest)
# Python program to check if the input year is a leap year or notyear = 2000 # To get year (integer input) from the user # year = int(input("Enter a year: ")) if (year % 4) == 0: if (year % 100) == 0: if (year % 400) == 0: print("{0} is a leap year". format(year)) else: print("{0} is not a leap year". format(year))
Octal and Hexadecimal # Python program to convert decimal number into binary, octal and hexadecimal number system # Change this line for a different result dec = 344 print("The decimal value of", dec, "is: ") print(bin(dec), "in binary. ") print(oct(dec), "in octal. ") print(hex(dec), "in hexadecimal. ")
EXERCISE 1. Evaluate the expression given below if A= 16 and B = 15. A % B // A a) 0. 0 b) 0 c) 1. 0 d) 1
2. What are the values of the following expressions: 2**(3**2) (2**3)**2 2**3**2 a) 64, 512, 64 b) 64, 64 c) 512, 512 d) 512, 64, 512
3. What is the value of x if: x = int(43. 55+2/2) a) 43 b) 44 c) 22 d) 23
4. What is the output of the code snippet shown below? X=”hi” print(“ 05 d”%X) a) 00000 hi b) 000 hi c) Hi 000 d) error
5. What is the order of precedence in python? i) Parentheses ii) Exponential iii) Multiplication iv) Division v) Addition vi) Subtraction a) i, iii, iv, v, vi b) ii, i, iii, iv, v, vi c) ii, i, iv, iii, v, vi d) i, iii, iv, vi, v
- Slides: 21