Python Practice Codes
Python Practice Codes
Complete all codes from the given link for more hands on with python
https://www.programiz.com/python-programming/examples
https://www.sanfoundry.com/python-problems-solutions/
https://codescracker.com/python/program/
https://www.w3schools.com/python/python_examples.asp
https://www.geeksforgeeks.org/python-programming-examples/
1
# Python3 program to add two numbers
num1 = 15
num2 = 12
# Adding two nos
sum = num1 + num2
# printing values
print("Sum of {0} and {1} is {2}" .format(num1, num2, sum))
2
# Python3 program to add two numbers
number1 = input("First number: ")
number2 = input("\nSecond number: ")
# Adding two numbers
# User might also enter float numbers
sum = float(number1) + float(number2)
# Display the sum
# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1, number2, sum))
def factorial(n):
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
4
# Python3 program to find simple interest
# for given principal amount, time and
# rate of interest.
# We can change values here for
# different inputs
P=1
R=1
T=1
# Calculates simple interest
SI = (P * R * T) / 100
5
# Python3 program to find compound
# interest for given values.
def compound_interest(principle, rate, time):
6
# Python program to find Area of a circle
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
print("Area is %.6f" % findArea(5));
7
# Python program to print all
# prime number in an interval
start = 11
end = 25
for val in range(start, end + 1):
8
# Python program to check if
# given number is prime or not
num = 11
# If given number is greater than 1
if num > 1:
# Iterate from 2 to n / 2
for i in range(2, num//2):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
10
1. # Python Program to find the area of triangle
2.
3. a = 5
4. b = 6
5. c = 7
6.
7. # Uncomment below to take inputs from the user
8. a = float(input('Enter first side: '))
9. b = float(input('Enter second side: '))
10. c = float(input('Enter third side: '))
11.
12. # calculate the semi-perimeter
13. s = (a + b + c) / 2
14.
15. # calculate the area
16. area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
17. print('The area of the triangle is %0.2f' %area)
11
1. # Python program to find the factorial of a number provided by the user.
2.
3. # change the value for a different result
4. num = 7
5.
6. # To take input from the user
7. num = int(input("Enter a number: "))
8.
9. factorial = 1
10.
11. # check if the number is negative, positive or zero
12. if num < 0:
13. print("Sorry, factorial does not exist for negative numbers")
14. elif num == 0:
15. print("The factorial of 0 is 1")
16. else:
17. for i in range(1,num + 1):
18. factorial = factorial*i
19. print("The factorial of",num,"is",factorial)
12
1. # Taking kilometers input from the user
2. kilometers = float(input("Enter value in kilometers: "))
3.
4. # conversion factor
5. conv_fac = 0.621371
6.
7. # calculate miles
8. miles = kilometers * conv_fac
9. print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))
13
1. # Program to check if a number is prime or not
2.
3. num = 407
4.
5. # To take input from the user
6. num = int(input("Enter a number: "))
7.
8. # prime numbers are greater than 1
9. if num > 1:
10. # check for factors
11. for i in range(2,num):
12. if (num % i) == 0:
13. print(num,"is not a prime number")
14. print(i,"times",num//i,"is",num)
15. break
16. else:
17. print(num,"is a prime number")
18.
19. # if input number is less than
20. # or equal to 1, it is not prime
21. else:
22. print(num,"is not a prime number")
14
1. # Python program to find the largest number among the three input numbers
2.
3. # change the values of num1, num2 and num3
4. # for a different result
5. num1 = 10
6. num2 = 14
7. num3 = 12
8.
9. # uncomment following lines to take three numbers from user
10. num1 = float(input("Enter first number: "))
11. num2 = float(input("Enter second number: "))
12. num3 = float(input("Enter third number: "))
13.
14. if (num1 >= num2) and (num1 >= num3):
15. largest = num1
16. elif (num2 >= num1) and (num2 >= num3):
17. largest = num2
18. else:
19. largest = num3
20.
21. print("The largest number is", largest)
15
1. # Program make a simple calculator
2.
3. # This function adds two numbers
4. def add(x, y):
5. return x + y
6.
7. # This function subtracts two numbers
8. def subtract(x, y):
9. return x - y
10.
11. # This function multiplies two numbers
12. def multiply(x, y):
13. return x * y
14.
15. # This function divides two numbers
16. def divide(x, y):
17. return x / y
18.
19. print("Select operation.")
20. print("1.Add")
21. print("2.Subtract")
22. print("3.Multiply")
23. print("4.Divide")
24.
25. # Take input from the user
26. choice = input("Enter choice(1/2/3/4): ")
27.
28. num1 = float(input("Enter first number: "))
29. num2 = float(input("Enter second number: "))
30.
31. if choice == '1':
32. print(num1,"+",num2,"=", add(num1,num2))
33.
34. elif choice == '2':
35. print(num1,"-",num2,"=", subtract(num1,num2))
36.
37. elif choice == '3':
38. print(num1,"*",num2,"=", multiply(num1,num2))
39.
40. elif choice == '4':
41. print(num1,"/",num2,"=", divide(num1,num2))
42. else:
43. print("Invalid input")
Complete all codes from the given url for more hands on with python
https://www.programiz.com/python-programming/examples
https://www.sanfoundry.com/python-problems-solutions/
https://codescracker.com/python/program/
https://www.w3schools.com/python/python_examples.asp