Lesson-3: Python Conditional Expressions, if, elif, else…
Python Conditional Expressions, if, elif, else..
Conditional statements refer to the work to be done if a certain condition occurs. In our previous lesson, we said that True or False values are assigned to a Boolean variable after comparison. As a result of these booen expressions thrown in conditional expressions, separate lines of code work for true and False probabilities.
CONDITIONAL STATEMENTS STRUCTURE
lines of code
lines of code
lines of code
lines of code
EXAMPLE – 1:
grade1 = float(input("1. Grade : ")) grade2 = float(input("2. Grade : ")) finalGrade = float(input("Final Grade :")) # Let's calculate 40% of the average Visa score and 60% of the Final score . avarage= (((grade1+grade2)/2)*0.4) + (finalGrade*0.6) print("Your Avarege : ", avarege) #In the condition part, let's stipulate that the final grade is greater than 50 . if finalGrade >= 50: if (avarage>= 85 and finalGrade >= 50): print("Your Score AA") elif avarage>= 75 and finalGrade < 85: print("Your Score BA") elif avarage>= 70 and finalGrade < 75: print("Your Score BB") elif avarage >= 65 and finalGrade < 70: print("Your Score CB") elif avarage>= 60 and finalGrade < 65: print("Your Score CC") elif avarage >= 55 and finalGrade < 60: print("Your Scorez DC") elif avarage>= 50 and finalGrade < 55: print("Your Score DD") else: print("Your Score FF .")
In the example above, the user is asked for vize1, vize2, and final notes. The overall average is 40% of the average of vize1 and vize2, and 60% of the final.
This example has the use of nested if. If the final grade is greater than 50 and 50, the other if structure in the note printing section is activated. If the Final grade is less than 50, the screen reads “your letter grade is FF . ” writing. The second if cycle works if the final grade is greater than or equal to 50.
As can be seen from the example, we can use as many “elif” as we want. the value in the” general average ” variable is compared and has a range of.
print ("1-->collection process, 2-->extraction process, 3-->multiplication process, 4-->division process") a=int (input ("enter the first number:")) b=int (input ("enter the second number:")) c=int (input ("select your transaction : ")) if (c==1): result= a+b print (result) elif (c==2): result= a-b print (result) elif (c==3): result= a*b print (result) elif (c==4): result= a/b print (result) else: print ("incorrect entry...")
In the example above, a simple calculator codes are given. The user is asked to enter two numbers, and these numbers are assigned to the variables “a” and “b”. Then, you are asked to select a transaction, and this value is assigned to the variable “c”. For entries 1,2,3,4, addition, subtraction, multiplication and division operations are defined in order.
EXAMPLE – 2:
In the If structure, the value in the variable “c” is checked. If the variable C is 1, the codes for Addition, Subtraction if 2, multiplication if 3, and division if 4 are written separately. Finally, the code of the else structure will work “incorrectly” because in any case where there are no 1,2,3,4 entries, the code under the else structure will work.
An important information to note is that only one of the conditions in the If structure generates a value of TRUE at a time, and its codes work, and the “result” variable is assigned the result of the operation and printed on the screen.
EXAMPLE – 3:
numb1 = int(input('Number: ')) if (numb1 > 0): if (numb1 % 2 ==0): print('the number entered is a positive even number.') else: print('the number entered is positive, but the number is odd.') else: print ('the number entered is a negative number.')
In the above example, the user is asked for a number, and the status of this number being positive, negative, odd or even is printed on the screen. This example also has nested IF structures.
In the external If structure, a positive, negative, or zero state of the number is controlled. if the variable” number ” is greater than zero, another If structure is opened and the state of the number being odd or even is questioned. “numb1%2 “contains the rest of the number with 2 and is passed to the variable” remaining”. If the remaining 0 is an odd number, if the number is not even.