Conditions in Python - ProgrammerTech


Conditions in Python

  • Share this:
Conditions in Python

Conditions in Python

Conditions are one of the most important elements that must be available in any programming language. They play a major role in every programming language, such as building materials. If you want to design a house, we consider it as a control method, meaning we are the ones who determine the decision for the program to control it, to control the The program must use the terms and conditions of the program.


if statement in Python

The if statement specifies the path of the program under certain conditions, which means if and is used only if you want to execute a specific code according to a specific condition.

Example 1

name = "data"
if "d" in name:
. . . print("yes found")
. . .
>>>yes found

We set a condition that if the letter d is present in the variable named name, type the word "yes found" for us, and of course this letter is present in the variable.

Example 2

a = 15
b = 25
if b > a:
    print ("good")
>>> good

We assign a value to variable a to 15 and assign a value to variable b to 25 then we set a condition that if variable b is greater than the value of variable a then print the word “good” and when running the code, “good” is printed for us because the value of variable b is 25 and it is greater than a which is 15.

Example 3

Write a program that prints the largest number between two numbers.

a = 77
b = 90
if b > a:
    print("b Check condition")

We created two variables, a, whose value is 77, and another variable, b, whose value is 90. Then we created a conditional sentence. We wrote if the variable b is greater than the variable a, then print the b Check condition statement, and when running this code, the condition was checked and printed for us the required result.

b Check condition

Example 4

Write a program that tests a if greater than b and if c is greater than a.

a = 150
b = 40
c = 350
if a > b and c > a:
    print("conditions True")

We create three variables, a, whose value is 150, variable b, whose value is 40, and the third variable, c, whose value is 350. Then we create a conditional statement if a is greater than b and c is greater than a, print condition True.

And when running the code, the program found that the condition was true and printed the result for us

condition True


else statement in Python

The else clause is used if the condition of the first if clause is not fulfilled, and the else clause cannot exist without the if clause preceded by it, and the else clause in the Arabic language means “anything else and it is used if we want to execute a specific code if the result of all The conditions before it are false.

Example 5

Write a program that asks a student to enter his mark and then prints his result if he is successful or failed.

mark=int(input("enter your mark:"))
if mark>=50:

We have defined a variable which is mark, and this variable receives numerical data from the user, enters it into the program through the input function, and the user enters your mark message. Anything less than 50 is an Unsuccessful Fail.

And when the user enters, for example, the tag 87, it will print for him successful as follows:

enter your mark:87

Example 6

Write a program that asks the user for a number and then checks if the number is positive or negative.

number=int(input("enter number: "))
if number>=0:
#run code
#Turn on a negative number
enter number: -6
#Play to a positive number
enter number:4

Example 7

Write a program that asks the user for a number and then checks if the number is even or odd.

number=int(input("enter number: "))
if number%2==0:
#run code
#even number
enter number:4
#odd number
enter number:7


elif statement in Python

The elif clause is used if you want to put more than one possibility of more than one condition and the elif clause or clauses are placed in the middle, i.e. between the if and else clauses.

Example 8

name = "Muhammad Alush"
if name == "Ahmed Kaissar":
    print ("i am Ahmed Kaissar")
elif name == ("Muhammad Alush"):
    print ("i am Muhammad Alush")
elif name == ("Saja Alsadig")
    print ("yes i am Saja Alsadig")
    print ("no"ا
the result is
>>> i am Muhammad Alush

In this example, we used elif, which means an abbreviation for else if, we set a variable called name and gave it a value Muhammad Alush. The conditions would have been printed for us that we put at the end of the example to print when none of the conditions were met.

Example 9

Write a program that prints the larger number between two numbers, but the code is more accurate than the previous one.

a = 90
b = 40
if b > a:
    print("b is greater than a")
elif a == b:
    print("a and b are equal")
    print("a is greater than b")
#The result
a is greater than b

Example 10

Write a program that compares two numbers and then prints the largest number.

a = 75
b = 75
if b > a:
    print("b greater than a")
elif a == b:
    print(" equal")
#The result

Example 11

Write a program that reads the number of a day of the week and then prints the name of the day.

The_day=int(input('enter number the day, from 1 to 7 :'))
if The_day == 1:
elif The_day == 2:
elif The_day == 3:
elif The_day == 4:
elif The_day == 5:
elif The_day == 6:
elif The_day == 7:

You don't have to or need to use the three conditional statements for every condition you put in the program, but you are forced to use the if statement with any condition.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.