Python Programs: For Beginner 1.0
Python Programs: For Beginner 1.0
com
PYTHON PROGRAMS
FOR BEGINNER 1.0
DEEPAK BHINDE
2019
Task points
No. Program
1. Task: Python program to add two numbers
2. Task: Python Program for simple interest
3. Task: Python Program for compound interest
4. Task: Program to find area of a circle.
5. Task: Program to print “Hello World “.
6. Task: Program to find the Square Root.
7. Task: Python Program to find the area of triangle.
8. Error! Reference source not found.
9. Error! Reference source not found.
10. Error! Reference source not found..
11. Error! Reference source not found.
12. Task: Python Program to Check Leap Year.
13. Task: Python Program to Find the Largest Among Three Numbers.
14. Task: Python Program to Find the Factorial of a Number.
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 2
Program: 01 Date:
Note: Given two numbers num1 and num2. The task is to write a Python program to find the addition of
these two numbers.
Examples:
Input: num1 = 5, num2 = 3
Output: 8
Input: num1 = 13, num2 = 6
Output: 19
Code:
# 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))
Output:
Sum of 15 and 12 is 27
Output:
First number: 13.5 Second number: 1.54
The sum of 13.5 and 1.54 is 15.04
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 3
Program: 02 Date:
Code :
# Python3 program to find simple interest for given principal amount, time and rate of interest.
EXAMPLE2:
Input : P = 3000
R=7
T=1
Output :210
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 4
Program: 03 Date:
Code :
# Driver Code
# Remove comment
#compound_interest(10000, 10.25, 5) EXAMPLE:
# or Input : Principle (amount): 1200
Out put :
Compound interest is 16288.946267774416
OR Output :
enter principal amount- 10000
enter rate- 10.25
enter time amount- 5
Compound interest is 16288.946267774416
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 5
Program: 04 Date:
Area = pi * r2
Where
r is radius of circle
Code :
def findArea(r):
PI = 3.142
return PI * (r*r);
# Driver method
# remove comment
#print("Area is %.6f" % findArea(5));
# OR
# input mentod
# Comment when input method not used
radius = float (input("enter radius -"))
area=findArea(radius)
print("Area is %.6f" % area);
Out put :
Area is 78.550000
OR Output :
enter radius -5
Area is 78.550000
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 6
Program: 05 Date:
“Hello world!”
Code:
Out put :
Hello World !
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 7
Program: 06 Date:
Sqroot = num**0.5
Where
num is any number
Code:
Out put :
The square root of 9.000 is 3.000
Output :
Enter a number: 27
The square root of 27.000 is 5.196
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 8
Program: 07 Date:
Code:
a=8
b=6
c=9
Out put :
The area of the triangle is 23.53
Out put :
Enter first side: 8
Enter second side: 6
Enter third side: 9
The area of the triangle is 23.53
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 9
Program: 08 Date:
Code:
# Solve the quadratic equation ax**2 + bx + c = 0
# import complex math module
import cmath
a=1
b=2
c=3
Out put :
The solution are (-1-1.4142135623730951j) and (-1+1.4142135623730951j)
Out put :
Enter a: 1
Enter b: 2
Enter c: 3
The solution are (-1-1.4142135623730951j) and (-1+1.4142135623730951j)
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 10
Program: 09 Date:
Using if...elif...else
Code:
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Code:
Output :
Enter a number: 2
Positive number
Enter a number: 0
Zero
Enter a number: -1
Negative number
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 11
Program: 10 Date:
A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the
remainder operator % to compute the remainder. If the remainder is not zero, the number is odd.
Code: Method 1
# Python program to check if the input number is odd or even.
# A number is even if division by 2 give a remainder of 0. If remainder is 1, it is odd number.
Code: Method 2
#input any number
num = int(input("Enter a number: "))
Output:
Enter a number: 43
43 is Odd
Enter a number: 18
18 is Even
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 12
Program: 11 Date:
A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime
number. 2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime (it is
composite) since, 2 x 3 = 6.
Code:
# Python program to check if the input number is prime or not
Output:
Enter a number: 7
7 is a prime number
or
100 is not a prime number
2 times 50 is 100
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 13
Program: 12 Date:
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a
leap year only if it is perfectly divisible by 400.
Code:
# Python program to check if the input year is a leap year or not
# 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))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
Output:
Enter a year: 2024
2024 is a leap year
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 14
Program: 13 Date:
The three numbers are stored in num1, num2 and num3 respectively. We've used
the if...elif...else ladder to find the largest among the three and display it.
Code:
# Python program to find the largest number among the three input numbers
# change the values of num1, num2 and num3 for a different result
Output:
Enter first number: 45
Enter second number: 23
Enter third number: 90
The largest number between 45.0 , 23.0 and 90.0 is 90.0
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 15
Program: 14 Date:
The factorial of a number is the product of all the integers from 1 to that number.
For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers
and the factorial of zero is one, 0! = 1.
Code:
# Python program to find the factorial of a number provided by the user.
factorial = 1
Output:
Enter a number: 8
The factorial of 8 is 40320
DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 16