What are Decision Control Structures?
Normally, the program flows along line by line in the order in which it appears in the source code. But, it is sometimes required to execute a particular portion of code only if a certain condition is true, or false i.e. you have to decide on the program.
General Format
if test1: # if test
statements1 # Associated block
elif test2: # Optional elifs
statements2
else: # Optional else
statements3
The indentation (blank whitespace all the way to the left of the two nested statements here) is the factor that defines which code block lies within the condition statement. Python doesn‘t care how indents can be inserted (either spaces or tabs may be used), or how much a statement can be indented
(any number of spaces or tabs can be used). In fact, the indentation of one nested block can be different from that of another. The syntax rule is only that for a given single nested block, all of its statements must be indented the same distance to the right. If this is not the case, a syntax error will
appear, and the code will not run until its indentation is repaired to be consistent. Python almost forces programmers to produce uniform, regular, and readable code. The one new syntax component in Python is the colon character (:). All Python compound statements that have other statements nested inside them—follow the same general pattern of a header line terminated in a colon, followed by a nested block of code usually indented underneath the header line .
Q1. Write a Python program that requests an integer value from the user. If the value is between 1 and 100 inclusive, print “OK”; otherwise, do not print anything.
num = int(input("Enter any Integer: ")) if 1<num<100: print("OK")
Q2. Write a Python program that requests an integer value from the user. If the value is between 1 and 100 inclusive, print “OK”, otherwise, print “Out of range”.
num = int(input("Enter any Integer: ")) if 1<num<100: print("OK") else: print("Out of range")
Q3. Write a Python program to check if a character entered by the user is an alphabet or not. If the user enters more than one character as input, the program prints some appropriate error message and exit.
char = input("Enter any character: ") if len(char) != 1: print("Enter only 1 character: ") else: if "A" <= char <= "Z" or "a" < char <= "z": print("It's an alphabet!") else: print("It's not an alphabet!")
Q4. Write a Python program to check if a character entered by the user is an uppercase alphabet or a lowercase alphabet. If the user enters more than one character or any character other than an alphabet as input, the program prints appropriate error messages and exit.
char = input("Enter any character: ") if len(char) != 1: print("Please Enter only 1 character: ") elif "a" < char <= "z": print("Lowercase") elif "A" <= char <= "Z": print("Uppercase") else: print("The character is not an alphabet!")
Q5. Write a Python program to check if a character entered by the user is an alphabet or a digit or a special character. If the user enters more than one character as input, the program prints some appropriate error message and exit.
char = input("Enter any character: ") if len(char) != 1: print("Please Enter only 1 character: ") elif "A" <= char <= "Z" or "a" < char <= "z": print("It's an alphabet!") elif "0" <= char <= "9": print("It's a digit") else: print("It's a special character!")
Q6. Write a Python program that requests five integer values from the user. It then prints the maximum and minimum values entered. If the user enters the values 3, 2, 5, 0, and 1, the program would indicate that 5 is the maximum and 0 is the minimum. Your program should handle ties properly; for example, if the user enters 2, 4, 2, 3, and 3, the program should report 2 as the minimum and 4 as maximum.
n = int(input("Enter Integer no 1: ")) g = n s = g for i in range(2,6): n = int(input(f"Enter Integer no {i}: ")) if n>g : g = n else: if n<s: s=n print("The Greatest number is :", g) print("The Smallest number is :", s)
Q7. Write a Python program that requests five integer values from the user. It then prints one of two things: if any of the values entered are duplicates, it prints “DUPLICATES”; otherwise, it prints “ALL UNIQUE”.
num1 = int(input("Enter First integer: ")) num2 = int(input("Enter Second integer: ")) num3 = int(input("Enter Third integer: ")) num4 = int(input("Enter Fourth integer: ")) num5 = int(input("Enter Fifth integer: ")) if num1 != num2 and num1 != num3 and num1 != num4 and num1 != num5 \ and num2 != num3 and num2 != num4 and num2 != num5 and num3 != num4 \ and num3 != num3 and num4!= num5: print("ALL UNIQUE") else: print("DUPLICATESs")
Q8. Write a Python program to check whether the triangle is equilateral, isosceles or scalene triangle. In equilateral triangle all three sides are equal, in isosceles triangle any two sides are equal, and in scalene triangle none of the three are equal.
len_1 = float(input("Enter length of First side: ")) len_2 = float(input("Enter length of Second side: ")) len_3 = float(input("Enter length of Third side: ")) if len_1 != len_2 != len_3 != len_1: print("Scalene") elif len_1 == len_2 == len_3 == len_1: print("Equilateral") else: print("Isosceles")
Q9. Write a Python program to input basic salary of an employee. It calculates and prints the gross and net salary according to following rules.
Gross Salary = Basic Salary (BS) + House Rent Allowance (HRA) + Dearness Allowance (DA)
Net Salary = Gross Salary (GS) – Deductions (DD)
BS <= 10000 : HRA = 20% of BS, DA = 80% of BS, DD = 2% of BS
BS <= 20000 : HRA = 25% of BS, DA = 90% of BS, DD = 4% of BS BS > 20000 : HRA = 30% of BS, DA = 95% of BS, DD = 10% of BS
BS = float(input ("Enter basic salary:")) if 0 <BS < 10000: HRA = 0.2 * BS DA= 0.8 * BS DD = 0.02 * BS elif 10000 <BS<= 20000: HRA=0.25 * BS DA = 0.9 * BS DD = 0.04 * BS else: HRA = 0.3 * BS DA = 0.95 * BS DD = 0.1 * BS GS = BS + HRA + DA print("Gross Salary = ", GS, "\nNet Salary = ", GS-DD)