Good day everyone!
Hope you're doing great!! Welcome back to my Python programming class. Today is the first day of the Python Programming class week 3. If you have followed my week 1 & 2 till the end, you must have acquired some basic Python programming skills that can help you start your programming career. However, if you did not follow my week 1 & 2 classes, please visit my blog now @anyiglobal to see all my week 1 & 2 classes.
I brought out this idea of teaching python on blurt because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on blurt communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.
This is my first time of posting in this community, and I believe this community will treat me right!
Note: The English language used in this class is literally understandable by a layman. So let's begin...
Since Python is a language used to communicate to the computer like we use English language to communicate in real life, it has some conditions for decision-making function like we have in English language. In our everyday life, we make decisions based on some set of conditions on some certain phenomena of life. Decision making structures are very useful in making decisions based on some conditions specified. In Python programming language, we write a set of statements that executes when a certain condition evaluates to True or False. To achieve this conditional statements, Python made use of various forms of "if" statements.
The various forms of Python if statements include:
1. if
2. if-else
3. if-elif-else
Let's look at the various forms of the conditional statements and their examples one after the other.
IF STATEMENT
In the "if statement", the statement in the "if" block executes if the condition evaluates to True otherwise the statement outside the "if" block executes. Don't worry we will see some examples below very soon. Here, Python supports the use of logical conditions from mathematics to compare between two or more variables.
- Equals: a == b
- Not Equals: a != b
- Greater than: a > b
- Greater than or Equal to: a >= b
- Less than: a < b
- Less than or Equal to: a <= b
We will be seeing each and every one of the comparison operators in action in the if statements sooner in this class.
If Statement Syntax:
if expression:
statement(s)
For Example,
Let's compare between two integer variables using the if statement.
a = 25
b = 80
if b > a:
print("b is greater than a")
Output
b is greater than a
From the above code, we saw that the statement in the if block executes because the condition stated there is True, i.e, "b is obviously greater than a" as we have seen. Let's see it in the screenshot below:
Python if statement
From the screenshot above, we can see how we used the if statement to compare between two variables. Please take note of the "Indentation" after the ":" of the if statement.
NB: Copy the code in the example above and try it out in your text editor. Also note that there are many other examples of "if statements" you can try out in your leisure time. Just browse through the Google and learn more about Python conditional and if statements. Learn more about Python conditional and if statements here.
IF-ELSE STATEMENT
In Python, when the statement in the if block results to False another block known as "else block" will execute the statement in it's block. It goes like this; if this does not happen, otherwise, this will happen! The else block comes right after the if block. We will see an example to this below.
For Example
a = 100
b = 40
if b > a:
print("b is greater than a")
else:
print("a is greater than b")
Output
a is greater than b
From our output, we saw that the statement outside the if block executed because the condition in the if statement is False, however, the statement in the else block executed because the statement is True. Please make sure to maintain the "Indentation" in the if-else blocks because they're very important in Python. If you don't know what indentation is in Python, please refer this our previous class, Python Syntax.
Let's see the screenshot to our example below:
Python if-else statement
From the screenshot below, we can see that the statement in the else block executed because the statement in the if block evaluates to False. There are so many examples you can practice using if-else statement but because of time and space, we will not go deeper with other examples. However, if you want to learn more about Python conditional and if statements click here.
NB: Copy the code in the example above and try it out in your text editor. Practice makes perfect!
IF-ELIF-ESLE STATEMENT
In this scenario, we can compare more than two variables using the if-elif-else statement. Here, we make use of "elif statement" to compare the other variables if the first variable comparison in the if block evaluates to False, and if the elif block evaluates to False, the next block will execute, and so on... until the condition is met. Conversely, the if-elif-else structure allows to check multiple expressions for True, executing any of the expression that evaluates to True. However, if none of the expressions evaluates to True, then the else block will be executed.
If-elif-else Syntax:
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
For Example,
Let's compare three integer variables:
a = 10
b = 7
c = 32
if a > c:
print("a is greater than c")
elif b > a:
print("b is greater than a")
elif c > b:
print("c is greater than b")
Output
c is greater than b
The last elif block was executed because the expression evaluated to True as we have seen in the example above.
From the screenshot above, we can see that the first and second expressions did not execute because the expression evaluates to False. Thus, the last expression executed because the expression evaluates to True. Please make sure to try out other examples you can formulate. However, if you want to learn more about Python conditional and if statements click here.
NB: Please make sure to copy the code in the example above and try it out in your text editor. Practicing is the best and easiest way to learn Programming very fast and quick. hence, make sure you practice along with the teacher.
Question of the Day: Write a Python program that compares any two integer variables using the if-else conditional statement.?
Prizes:
first position: 15 blurt
second position: 10 blurt
Rules:
- Use your text editor to write the program
- Show the screenshot of the program in the comment section
- Prizes will be sent after the payout of this post
Conclusion
Conditional statements is an integral part of any programming language. They help in decision making in the language. It is more like making decisions in real life by human beings. Please use the article provided in this class to read more on Python conditions and if statements. In this class, we discussed the use of the various forms of if statements. The various forms include: if statement, if-else statement, and if-elif-else statement. The expression evaluates to True if the condition specified in the conditional statement is met. In our next class, we will look at "Python Loop Statements". Thanks for being part of this class, I appreciate your coming!!
This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed.
Please do well to follow this blog, and also don't forget to reblurt this post so that it can reach larger number of blurtians who wants to learn this skill.
Congratulations, your post has been curated by
Follow #gamestateconnect for more updates on gamestate.
you can also delegate to @gamestateconnect to support curation.
Thanks! I am sorry I have not been active for a while now!
Greetings,
Also, keep in touch with Blurtconnect-ng family on Telegram and Whatsapp
Peace
Thanks! I am sorry I have not been active for a while now!