Python If Condition Programs | Set 4

Set 1    Set 2    Set 3    Set 4   Set 5  Set 6  Set 7  Set 8  Set 9

Question: 16

Write a python script to take input for a number if the number is <=10 calculate and print its square?

Sol:

a=int(input("Enter any no "))
if(a<=10):
    b=a*a
    print("Square = ",b)

Output:

Enter any no 5
Square = 25
>>>

Question: 17

Write a python script to take input for a number is the number is <=10 calculate and print its cube otherwise print its square?

Sol:

a=int(input("Enter any no "))
 if(a<=10):
     b=a*a*a
     print("Cube = ",b)
 else:
     b=a*a
     print("Square = ",b)

Output:

Enter any no 5
Cube = 125
>>>
Enter any no 12
Square = 144
>>>

Question: 18

Write a python script to take input for a number check and print whether the number is +ve , -ve or zero?

Sol: (1st method)

a=int(input("Enter any no "))
if(a==0):
    print("number is zero")
else:
    if(a>0):
        print("number is +ve")
    else:
        print("number is -ve") 

Sol: (2nd method):

a=int(input("Enter any no "))
if(a==0):
    print("number is zero")
elif(a>0):
    print("number is +ve")
else:
    print("number is -ve") 

Output:

Enter any no 25
number is +ve
>>>
Enter any no -96
number is -ve
>>>
Enter any no 0
number is zero
>>>

Question : 19

Write a python script to take input for a number check and print whether the number is even, odd or zero?

Sol:( Method:1)

a=int(input("Enter any no "))
if(a==0):
    print("Number is zero")
else:
    if(a%2==0):
        print("Number is even")
    else:
        print("Number is odd")

Sol: (Method:2)

a=int(input("Enter any no "))
if(a==0):
    print("Number is zero")
elif(a%2==0):
    print("Number is even")
else:
    print("Number is odd")

Output:

Enter any no 25
Number is odd
>>>
Enter any no 0
Number is zero
>>>
Enter any no 12
Number is even
>>>

Question: 20

Write a python script to take input for three number check and print the largest number? (Method:1)

Sol:

a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
c=int(input("Enter 3rd no "))
if(a>b and a>c):
    print("max no = ",a)
else:
    if(b>c):
        print("max no = ",b)
    else:
        print("max no = ",c)

using elif 

a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
c=int(input("Enter 3rd no "))
if(a>b and a>c):
    print("max no = ",a)
elif(b>c):
    print("max no = ",b)
else:
    print("max no = ",c)

Output:

Enter 1st no 25
Enter 2nd no 63
Enter 3rd no 98
max no = 98
>>>
Enter 1st no 85
Enter 2nd no 25
Enter 3rd no 45
max no = 85
>>>

Set 1    Set 2    Set 3    Set 4   Set 5  Set 6  Set 7  Set 8  Set 9

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling