Total Pageviews

Saturday, April 18, 2020

IF ELSE IN PYTHON

1. LARGER OF TWO NUMBERS - CLICK
2. POSITIVE NEGATIVE NUMBER CHECKING - CLICK
3. Python Program to Check if a Number is Odd or Even   CLICK
4.Python Program to Find the Largest Among Three Numbers CLICK
5. Python Program to Check Leap Year CLICK

1. Check Whether a Number is Positive or Negative

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

if num >=
0:
    print("Positive Number")
else:
    print("Negative Number")

2. Check Whether a Number is Even or Odd

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

if num %
2 == 0:
    print("Even Number")
else:
    print("Odd Number")

3. Find Larger of Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
    print("Largest =", a)
else:
    print("Largest =", b)

4. Check Voting Eligibility

age = int(input("Enter age: "))

if age >=
18:
    print("Eligible for Voting")
else:
    print("Not Eligible for Voting")

5. Check Whether a Number is Divisible by 5

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

if num %
5 == 0:
    print("Divisible by 5")
else:
    print("Not Divisible by 5")

6. Find Smaller of Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a < b:
    print("Smaller =", a)
else:
    print("Smaller =", b)

7. Check Leap Year

year = int(input("Enter year: "))

if year %
4 == 0:
    print("Leap Year")
else:
    print("Not a Leap Year")

8. Check Pass or Fail

marks = int(input("Enter marks: "))

if marks >=
40:
    print("Pass")
else:
    print("Fail")

9. Check Whether Character is Vowel

ch = input("Enter a character: ")

if ch in "AEIOUaeiou":
    print("Vowel")
else:
    print("Consonant")

10. Find Greatest of Three Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a > b and a > c:
    print("Greatest =", a)
elif b > c:
    print("Greatest =", b)
else:
    print("Greatest =", c)

11. Check Whether Number is Zero

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

if num ==
0:
    print("Zero")
else:
    print("Not Zero")

12. Check Driving Eligibility

age = int(input("Enter age: "))

if age >=
18:
    print("Eligible for Driving")
else:
    print("Not Eligible")

13. Find Absolute Value

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

if num <
0:
    num = -num

print("Absolute Value =", num)

14. Check Number is Multiple of 10

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

if num %
10 == 0:
    print("Multiple of 10")
else:
    print("Not a Multiple of 10")

15. Check Whether Number is Three-Digit

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

if
100 <= num <= 999:
    print("Three-Digit Number")
else:
    print("Not a Three-Digit Number")

16. Calculate Grade

marks = int(input("Enter marks: "))

if marks >=
90:
    print("Grade A")
elif marks >=
75:
    print("Grade B")
elif marks >=
50:
    print("Grade C")
else:
    print("Grade D")

17. Check Whether a Number is Divisible by 2 and 3

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

if num %
2 == 0 and num % 3 == 0:
    print("Divisible by 2 and 3")
else:
    print("Not Divisible by 2 and 3")

18. Find Largest Among Three Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:
    print("Largest =", a)
elif b >= c:
    print("Largest =", b)
else:
    print("Largest =", c)

19. Check Whether a Character is Alphabet

ch = input("Enter a character: ")

if ('A' <= ch <= 'Z') or ('a' <= ch <= 'z'):
    print("Alphabet")
else:
    print("Not an Alphabet")

20. Calculate Electricity Bill

units = int(input("Enter units consumed: "))

if units <=
100:
    bill = units *
2
else:
    bill = units *
5

print("Electricity Bill =", bill)
 

21. Check Triangle Validity

a = int(input("Enter side 1: "))
b = int(input("Enter side 2: "))
c = int(input("Enter side 3: "))

if a + b > c:
    if b + c > a:
        if c + a > b:
            print("Valid Triangle")
        else:
            print("Invalid Triangle")
    else:
        print("Invalid Triangle")
else:
    print("Invalid Triangle")

22. Check Character Type

ch = input("Enter a character: ")

if 'A' <= ch <= 'Z' or 'a' <= ch <= 'z':
    if ch in "AEIOUaeiou":
        print("Vowel")
    else:
        print("Consonant")
else:
    print("Not an Alphabet")
 

 

INPUT AND OUTPUT ( PYTHON )


PYTHON VARIABLE AND OPERATOR

1. BASIC INPUT OUTPUT -  CLICK
2. ADDITION - CLICK
3. BASIC - CLICK

1)

a = 5
print(a)

Output:

5

2)

a = 5
print("A =", a)

Output:

A = 5

3)

a = 5
b = 7
c = a + b

print(c)

Output:

12

4)

a = 5
b = 7
c = a + b

print("sum =", c)

Output:

sum = 12

5) Sum and Average of Two Numbers

a = 5
b = 7

c = a + b
d = c / 2

print("sum =", c)
print("Average =", d)

Output:

sum = 12
Average = 6.0

6) Sum and Average of 3 Numbers

a = 2
b = 4
c = 12

s = a + b + c
av = s / 3

print("sum =", s)
print("average =", av)

Output:

sum = 18
average = 6.0

7) WAP in Python to Do Arithmetic Operations

a = 12
b = 4

sum = a + b
sub = a - b
mul = a * b
div = a / b

print("sum =", sum)
print("sub =", sub)
print("mul =", mul)
print("div =", div)

Output:

sum = 16
sub = 8
mul = 48
div = 3.0

 

 

 

 

8) Division (/) and Floor Division (//)

a = 5
b = 2

c = a / b
d = a // b

print("Division =", c)
print("Floor Division =", d)

Output:

Division = 2.5
Floor Division = 2

9) Exponent Operator (**)

a = 5
b = 2

c = a ** b

print(c)

Output:

25

or

a = 5
b = 2

c = b ** a

print(c)

Output:

32

10) Find First and Last Digit of a Two-Digit Number

n = 37

a = n % 10
b = n // 10

print("Last Digit =", a)
print("First Digit =", b)

Output:

Last Digit = 7
First Digit = 3

11) Sum of Digits of a Two-Digit Number

n = 37

a = n % 10
b = n // 10

sum = a + b

print("Sum of digits =", sum)

Output:

Sum of digits = 10

11) Input Value from User and Display

a = input("Enter a value: ")

print(a)

Output:

Enter a value: 5
5

12) Input Your Name and Surname and Display

name = input("Enter your name: ")
sname = input("Enter your surname: ")

print("Name =", name)
print("Surname =", sname)

Output:

Enter your name: Bijan
Enter your surname: Paul

Name = Bijan
Surname = Paul

13) String Concatenation Using +

a = input("Enter a value: ")
b = input("Enter second value: ")

c = a + b

print(c)

Output:

Enter a value: 3
Enter second value: 5

35

Note: + acts as a Concatenation Operator for strings.


14) Addition of Two Numbers Using User Input

a = int(input("Enter a value: "))
b = int(input("Enter 2nd value: "))

c = a + b

print(c)

Output:

Enter a value: 3
Enter 2nd value: 5

8

15) Sum of Two Float Numbers

a = float(input("Enter 1st float number: "))
b = float(input("Enter 2nd float number: "))

c = a + b

print(c)

Output:

Enter 1st float number: 5.5
Enter 2nd float number: 2.5

8.0

16) Sum and Average of Three Integers

a = int(input("Enter 1st number: "))
b = int(input("Enter 2nd number: "))
c = int(input("Enter 3rd number: "))

sum = a + b + c
avg = sum / 3

print("Sum =", sum)
print("Average =", avg)

Output:

Enter 1st number: 10
Enter 2nd number: 20
Enter 3rd number: 30

Sum = 60
Average = 20.0

17) Centigrade to Fahrenheit

Formula:

F=9C5+32F = \frac{9C}{5} + 32F=59C+32

c = float(input("Enter the centigrade value: "))

f = ((9 * c) / 5) + 32

print("Fahrenheit value =", f)

Output:

Enter the centigrade value: 25

Fahrenheit value = 77.0

18) Fahrenheit to Centigrade

Formula:

C=5(F−32)9C = \frac{5(F - 32)}{9}C=95(F32)

f = float(input("Enter the temperature in °F: "))

c = (5 * (f - 32)) / 9

print("Centigrade value =", c)

Output:

Enter the temperature in °F: 98.6

Centigrade value = 37.0

19) Convert Hours, Minutes and Seconds into Total Seconds

h = int(input("Enter hours: "))
m = int(input("Enter minutes: "))
s = int(input("Enter seconds: "))

time = (h * 60 * 60) + (m * 60) + s

print("Time in seconds =", time)

Output:

Enter hours: 1
Enter minutes: 20
Enter seconds: 30

Time in seconds = 4830

20) Convert Years, Months, Weeks and Days into Total Days

year = int(input("Enter years: "))
month = int(input("Enter months: "))
week = int(input("Enter weeks: "))
day = int(input("Enter days: "))

total_days = (year * 365) + (month * 30) + (week * 7) + day

print("Total days =", total_days)

Output:

Enter years: 1
Enter months: 2
Enter weeks: 1
Enter days: 3

Total days = 435

 

 

 

 1. Addition of Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

sum = a + b

print("Sum =", sum)


2. Subtraction of Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

difference = a - b

print("Difference =", difference)


3. Multiplication of Two Numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

product = a * b

print("Product =", product)


4. Division of Two Numbers

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))

quotient = a / b

print("Quotient =", quotient)


5. Find Remainder

a = int(input("Enter dividend: "))
b = int(input("Enter divisor: "))

remainder = a % b

print("Remainder =", remainder)


6. Calculate Square of a Number

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

square = n * n

print("Square =", square)


7. Calculate Cube of a Number

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

cube = n ** 3

print("Cube =", cube)


8. Calculate Area of Rectangle

length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))

area = length * breadth

print("Area =", area)


9. Calculate Perimeter of Rectangle

length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))

perimeter = 2 * (length + breadth)

print("Perimeter =", perimeter)


10. Calculate Area of Circle

radius = float(input("Enter radius: "))

area = 3.14 * radius * radius

print("Area =", area)


11. Calculate Circumference of Circle

radius = float(input("Enter radius: "))

circumference = 2 * 3.14 * radius

print("Circumference =", circumference)


12. Calculate Simple Interest

p = float(input("Enter Principal: "))
r = float(input("Enter Rate: "))
t = float(input("Enter Time: "))

si = (p * r * t) / 100

print("Simple Interest =", si)


13. Calculate Average of Three Numbers

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

average = (a + b + c) / 3

print("Average =", average)


14. Convert Celsius to Fahrenheit

celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = (celsius * 9/5) + 32

print("Fahrenheit =", fahrenheit)


15. Convert Fahrenheit to Celsius

fahrenheit = float(input("Enter temperature in Fahrenheit: "))

celsius = (fahrenheit - 32) * 5/9

print("Celsius =", celsius)


16. Calculate Percentage of Marks

m1 = float(input("Enter marks of Subject 1: "))
m2 = float(input("Enter marks of Subject 2: "))
m3 = float(input("Enter marks of Subject 3: "))
m4 = float(input("Enter marks of Subject 4: "))
m5 = float(input("Enter marks of Subject 5: "))

total = m1 + m2 + m3 + m4 + m5
percentage = total / 5

print("Percentage =", percentage)


17. Calculate Discount Amount

price = float(input("Enter product price: "))
discount = float(input("Enter discount percentage: "))

discount_amount = (price * discount) / 100

print("Discount Amount =", discount_amount)


18. Calculate Final Price After Discount

price = float(input("Enter product price: "))
discount = float(input("Enter discount percentage: "))

final_price = price - (price * discount / 100)

print("Final Price =", final_price)


19. Swap Two Numbers Using Arithmetic Operations

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

a = a + b
b = a - b
a = a - b

print("After Swapping:")
print("a =", a)
print("b =", b)


20. Calculate Power of a Number

base = int(input("Enter base: "))
exponent = int(input("Enter exponent: "))

result = base ** exponent

print("Result =", result)