PYTHON PROGRAMS · 2019. 5. 27. · Note: Given two numbers num1 and num2. The task is to write a...

16
2019 PYTHON PROGRAMS FOR BEGINNER 1.0 DEEPAK BHINDE PGT COMPUTER SCIENCE www.dbgyan.wordpress.com

Transcript of PYTHON PROGRAMS · 2019. 5. 27. · Note: Given two numbers num1 and num2. The task is to write a...

Page 1: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

2019

PYTHON PROGRAMS FOR BEGINNER 1.0

DEEPAK BHINDE

PGT COMPUTER SCIENCE

www.dbgyan.wordpress.com

Page 2: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 2

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.

Task points

Page 3: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 3

Program: 01 Date:

Task: Python program to add two numbers

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:

Method 1: Simple method

# 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

Method 2: user input method # enter 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))

Output:

First number: 13.5 Second number: 1.54

The sum of 13.5 and 1.54 is 15.04

Page 4: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 4

Out put :

simple interest is 1400.0

Program: 02 Date:

Task: Python Program for simple interest

Code :

# Python3 program to find simple interest for given principal amount, time and rate of interest.

# We can change values here for different inputs

P = 5000

R = 7

T = 4

# Calculates simple interest

SI = (P * R * T) / 100

# Print the resultant value of SI

print("simple interest is", SI)

Simple interest formula is given by: Simple Interest = (P x T x R)/100 Where,P is the principle amount T is the time and R is the rate

EXAMPLE1:

Input : P = 10000

R = 5

T = 5

Output :2500

We need to find simple interest on

Rs. 10,000 at the rate of 5% for 5 units of time.

EXAMPLE2:

Input : P = 3000

R = 7

T = 1

Output :210

Page 5: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 5

Program: 03 Date:

Task: Python Program for compound interest.

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

Code :

# Program to find compound interest for given values. def compound_interest(principle, rate, time): # Calculates compound interest CI = principle * (pow((1 + rate / 100), time)) print("Compound interest is", CI) # Driver Code # Remove comment #compound_interest(10000, 10.25, 5) # or # input method p=float(input ("enter principal amount- ")) r=float (input ("enter rate- ")) t=float(input ("enter time amount- ")) compound_interest(p,r,t)

Compound Interest = P(1 + R/100)r

Where,

P is principle amount

R is the rate and

T is the time span

EXAMPLE:

Input : Principle (amount): 1200

Time: 2

Rate: 5.4

Output :

Compound Interest = 1333.099243

Page 6: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 6

Program: 04 Date:

Task: Program to find area of a circle.

Out put :

Area is 78.550000

OR Output :

enter radius -5

Area is 78.550000

Code :

# Python program to find Area of a circle 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);

Area = pi * r2 Where r is radius of circle

Page 7: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 7

Program: 05 Date:

Task: Program to print “Hello World “.

Out put :

Hello World !

Code:

# Program to print "Hello World !"

print("Hello World !");

“Hello world!”

Page 8: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 8

Program: 06 Date:

Task: Program to find the Square Root

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

Code:

# Python Program to calculate the square root

# Note: change this value for a different result

num = 9

# 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.3f is %0.3f'%(num ,num_sqrt))

Sqroot = num**0.5 Where num is any number

Page 9: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 9

Program: 07 Date:

Task: Python Program to find the area of triangle.

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

Code:

# Python Program to find the area of triangle a = 8 b = 6 c = 9 # Uncomment below to take inputs from the user # 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 area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 print('The area of the triangle is %0.2f' %area)

If a, b and c are three sides of a triangle. Then, s = (a+b+c)/2 area = √(s(s-a)*(s-b)*(s-c))

Page 10: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 10

Program: 08 Date:

Task: Python Program to Solve Quadratic Equation.

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)

Code:

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module import cmath a = 1 b = 2 c = 3 # To take coefficient input from the users # 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 sol1 = (-b-cmath.sqrt(d))/(2*a) sol2 = (-b+cmath.sqrt(d))/(2*a) print('The solution are {0} and {1}'.format(sol1,sol2))

The standard form of a quadratic equation is: ax2 + bx + c = 0, where a, b and c are real numbers and a ≠ 0

Page 11: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 11

Program: 09 Date:

Task: Python Program to Check if a Number is Positive, Negative or 0.

Output : Enter a number: 2 Positive number Enter a number: 0 Zero Enter a number: -1 Negative number

Code:

num = float(input("Enter a number: ")) if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")

Code:

num = float(input("Enter a number: ")) if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number")

Using if...elif...else

Using if... else

Page 12: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 12

Program: 10 Date:

Task: Python Program to Check if a Number is Odd or Even

Output:

Enter a number: 43 43 is Odd Enter a number: 18 18 is Even

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.

#input any number

num = int(input("Enter a number: ")) # if division by 2 give a remainder of 0. if (num % 2) == 0: print("{0} is Even".format(num)) else: print("{0} is Odd".format(num))

Code: Method 2

#input any number

num = int(input("Enter a number: ")) # if division by 2 give a remainder of 0. if (num % 2) == 1: print("{0} is Odd".format(num)) else: print("{0} is Even".format(num))

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.

Page 13: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 13

Program: 11 Date:

Task: Python Program to Check Prime Number.

Output:

Enter a number: 7 7 is a prime number or 100 is not a prime number 2 times 50 is 100

Code:

# Python program to check if the input number is prime or not # take input from the user num = int(input("Enter a number: ")) # prime numbers are greater than 1 if num > 1: # check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number") # if input number is less than or equal to 1, it is not prime else: print(num,"is not a prime number")

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.

Page 14: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 14

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.

Output: Enter a year: 2024 2024 is a leap year Enter a year: 1900 1900 is not a leap year

Task: Python Program to Check Leap Year.

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))

Page 15: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 15

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

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.

Task: Python Program to Find the Largest Among Three Numbers

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 # take three numbers from user num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):

largest = num1

elif (num2 >= num1) and (num2 >= num3):

largest = num2

else:

largest = num3

print("The largest number between",num1,",",num2,"and",num3,"is",largest)

Page 16: PYTHON PROGRAMS · 2019. 5. 27. · 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 =

DEEPAK BHINDE PGT COMPUTER SCIENCE PYTHON PROGRAMS for beginner 1.0 Page 16

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.

Output: Enter a number: 8 The factorial of 8 is 40320

Task: Python Program to Find the Factorial of a Number

Code:

# Python program to find the factorial of a number provided by the user.

# take input from the user

num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

for i in range(1,num + 1):

factorial = factorial*i

print("The factorial of",num,"is",factorial)