Conditions in Python
Translate terms: meansconditionsin English. We use conditions in Python to define how the program works.
Also, you can put more than one condition or as many conditions as you want in one program, and we can also put the condition inside the condition or the conditions inside each other as well.
Dear learner, do not worry that you do not understand anything about the conditions, and you will see.
General syntax for writing conditional sentences in Python
write terms(Syntax) Or the general formula for writing conditions in Python is as follows:
if condition: # If condition is true, run the code here
elif condition: # If condition is true, run the code here
else: # If no condition is met, run the code here
What you should know, dear learner, is that you do not need to use the three condition sentences in every condition you put in your program, but you, my dear, are forced to use the condition sentence ifwith any condition.
With Prisoner of Destiny, continue with the lesson to the end. You are very lucky to learn all the ways to write conditions in Python.
Conditional Sentences in Python
| Sentence | the use |
|---|---|
| IF Statement | ifIn our Arabic language, it means "if" as we learned that a while ago. And we can use it if you want to implement simple code according to a simple condition. |
| Else Statement | elseIn our Arabic language, it means "other than that or anything else". And we can use it if we want to execute a simple code in case the condition preceding it is incorrect or it is not fulfilled. It must always be in the last, because we use it if none of the conditional statements that precede it are executed. |
| Else IF Statement | elif We should also use it if we want to put more than one possibility (that is, more than one condition). The sentence or sentences the elifplace in the middle, that is, between the two sentences ifand else . |
conditional sentence if
ifIn our Arabic language it means "if".
And we can use it only if you want to implement a specific code or code according to a specific condition.
This first exercise demonstrates the idea of the if clause
• If the value of the variable Saddam is greater than 25, the sentence will be printed: Saddam is bigger than 25 .
Saddam = 0
if Saddam > 25:
print('Saddam is bigger than 25')
• The outputs upon implementation will be as follows.
• Here he said to himself once: Is the value of the variable Saddamgreater than 25 ?
Her answer was yes( false), so it did not execute the print command in the conditional statement.
Another example that illustrates the idea of the if clause in Python
• If the value of the variable Saddamis greater than 5 , the sentence will be printed: Saddam is bigger than 5.
Saddam = 30 if Saddam > 5:
print('Saddam is bigger than 5')
• We will get the following outputs upon implementation.
• Here he said to himself once: Is the value of the variable Saddamgreater than 5 ?
And his answer was yes( true),So the print statement in the if conditional statement is executed .
conditional sentenceelse
The sentence elsein our Arabic language means "other than that or anything else".
It is used only if we want to execute a specific code if the result of all the conditions before it is equal to false.
You must always include it at the end, because it is used if no conditional statement is executed before it.
So, if the program executes the statement if, else ifit will ignore the statement else.
And if it does not execute any of the sentences if, else ifit will execute the sentence else.
The first exercise in the conditional sentence else
• If the value of the variable Saddamis 5 , the sentence: Saddam is equal 5 will be printed .
• If the value of the variable Sis not equal to 5 , the sentence: Saddam is not equal 5 will be printed .
Saddam = 5
if S == 5:
print('Saddam is equal 5')
else:
print('Saddam is not equal 5')
• We will get the following result when running.
• Here he asked himself once: Is the value of the variable Saddam equal to 5 ?
The answer to the condition was yes( true) , so execute the print command in the sentence if.
Another example
• If the value of the variable Sadis 5 , the sentence will be printed: Sad is equal 5 .
• If the value of the variable Sis not equal to 5 , the sentence will be printed: Sad is not equal 5 .
Sad = 20
if S == 5:
print('Sad is equal 5')
else:
print('Sad is not equal 5')
• We will get the following output when executing.
• Here he asked himself the following: Is the value of the variable Sadequal to 5 ?
The answer to the condition was yes( false) , so execute the print command in the sentence else.
conditional sentenceelif
The sentence elifis used if you want to put more than one possibility (that is, more than one condition).
Sentence or sentences The elifare placed in the middle, that is, between the two sentences ifand else.
Example
• If the value of the variable numberis 1 the word: one.
• If the value of the variable numberis 2 , the word: two.
• If the value of the variable numberis 3 , the word: three.
• If the value of the variable numberis greater than or equal to 4 , the sentence will be printed: four or greater.
• If the value of the variable numberis less than 0 , the sentence will be printed: negative number.
number = 3
if number == 1:
print('one')
elif number == 2:
print('two')
elif number == 3:
print('three')
elif number >= 4:
print('four or greater')
else:
print('negative number')
• We will get the following output when executed.
• Here he asked himself the following: Is the value of the variable numberequal to 1 ?
The answer to the condition was yes( false) , then move on to the next condition.
• Then he asked himself the following: Is the value of the variable numberequal to 2 ?
The answer to the condition was yes( false) , then move on to the next condition.
• Then he asked himself the following: Is the value of the variable numberequal to 3 ?
The answer to the condition this time was yes( true), so he executed the printing command found in the third conditional sentence, and then skipped all the conditional sentences that came after it.
Other Ideas and Techniques for Setting Conditions in Python
The ways to put conditions in Python are many and varied in Python or any other language, and we can put conditions inside some of them and this is calledNested Conditional.
Also, dear learner, you can put more than one condition inside the conditional sentences ifor else ifby using operators logical operators.
Putting more than one condition in the if statement in Python
Dear learner, you can put more than one conditional inside the conditional sentence using the andor operator or .
The operator andwe use in case you want to implement a specific code if the answer to all the conditions set is equal to true .
The operator orwe use in case you want to execute a specific code if the answer to at least one condition is equal to true.
The first exercise shows his idea of putting more than one conditional in the if clause in Python.
• If the value of the variable is saddambetween 0 and 20 , print the sentence: acceptable number.
saddam = 14;
if saddam >= 0 and saddam <= 20: print("acceptable number")
• We will get the following output when executed.
• We note that the print command has been executed because the value of the variable is saddambetween 0 and 20
• Here he asked himself twice.
• The first time: Is the value of the variable saddam greater or equal to 0 ?
Was the answer to the first condition true.
The second time: Is the value of the variable saddam less than or equal to 20 ?
The answer to the second condition was also true.
• Since both answers were trueexecuting the print command.
exercise 2
• If the value of the variable saddam is between 0 and 20 , print the sentence: acceptable number.
saddam = 26;
if saddam <= 0 and saddam <= 20:
print('acceptable number')
• We will get the following output when executed.
• We notice that the print command is not executed because the value of the saddam variable is not between 0 and 20
• Here he asked himself twice.
• In the first time: Is the value of the variable saddam greater or equal to 0 ?
Was the answer to the first condition true.
• In the second time: Is the value of the variable saddam less or equal to 20 ?
The answer to the second condition false.
• Since one of the two answers did not trueimplement the print function.
Another exercise too
• If the value of the variable saddam is 1 , 2 or 3 , print the sentence: you choose a valid number.
saddam = 2
if saddam == 1 or saddam == 2 or saddam == 3:
print('you choose a valid number')
• We will get the following output when executed.
• We note that he executed the print command because the value of the variable saddam is 2
• Here he asked he would have asked himself three times because there are three conditions, but he asked himself only two questions.
• The first time: Is the value of the variable saddam equal to 1 ?
The answer to the first condition was, so he falsemoved to the next condition.
• In the second time: Is the value of the variable saddam equal to 2 ?
The answer to the second condition true.
• Since one of the answers was truedirectly executing the command to print and did not even consider the last condition.
Putting conditions inside a condition in Python
You can put the conditions inside each other, dear learner, and you can put the number of conditions we want.
In actual programs, the programmer puts many conditions inside some of them, according to the idea that he wants to implement in his program, and we will give you a simple example that teaches you how to think and analyze.
Example showing how to put a condition inside a condition in Python
• In the beginning we have a variable whose name represents gender sexand a variable whose name represents age age.
sex = 'female'
age = 14
if sex == 'male':
print('Gender: male')
if age <= 21:
print('he is a young boy');
elif sex == 'female':
print('Gender: female')
if age <= 21:
print('she is a young girl')
• We will get the following output when executed.
she is a young girl
• Here he asked himself the following: Is the value of the variable sexequal male?
The answer to the condition was false, so he moved to the second condition sentence elif.
• Here he asked himself the following: Is the value of the variable sexequal female?
The answer to the condition was true, so he implemented the print function in it, so he printed the sentence Gender: female.
• Then he found another condition within the previous condition, and asked himself the following: Is the value of the variable ageless than or equal to 21 ?
So the answer to the condition was true, so he implemented the print function in it, so he printed the sentence she is a young girl.
In the next lesson, you will learn about loops in Python
Previous lesson: Operators in Python.
Next lesson: Loops in Python.