Python Basics Part 2

Silver
3 min readMar 13, 2019

--

This blog will teach you what if statements are and how to use them in Python.

Image result for python
LINK: Image of Python’s logo.

What is an If statement?

An if statement is a feature used in programming languages, which will either execute or not execute certain blocks of code depending on the outcome of the comparison statement. This will make more sense as you continue to read.

If statements are used in Python and a lot of other coding languages, but the syntax for it might be a little bit different depending on the language. In Python, if statements look like this:

if (comparison statement): code that will run.

The parentheses will hold a comparison statement that will use a comparison operator. The comparison operators are >, <, and ==. The comparison statement will look something like this: (5 > 3). This comparison statement will come out as true, so whatever code that is after the colon will be executed. If that statement came out as false, then the code after the colon will not be executed and everything will just continue to move on. Sounds really simple right? Well, it actually is really simple, but there is still more to if statements.

Elif and else statements:

LINK: Image showing if, elif, and else statements in use.

There are elif and else statements that can be attached to if statements. These are attached to the end of an if statement to do more comparisons statements to run a different block of code, or to just run a different block of code if none of the comparison statements came out as true.

Elif is just short for else if. You can link this off of an if statement and it is like you just made a whole new if statement, but the difference is that if the if statement is True, then the elif statements below will not activate, even if the comparison comes out as True. You can link a bunch on elif statements onto an if statement, but once one of them came out as true, none of the others will even be looked at. That’s why it’s sometimes needed instead of just making multiple if statements.

LINK: Image showing how if statements go from if, to elif, then else.

Else statements are attached to if and elif statements, but it is always at the very bottom. If statement on top, elif statements in middle, else statement on the bottom. An else statement does not require a comparison statement, only the colon and the code after that wants to be executed. The reason why it’s on the bottom and does not require a comparison statement is because it will only run if none of the comparison statements from the if and elif statements are True. If they are all false, then it will automatically go to the else statement and run what every block of code you put after the colon.

That’s the basics for if statements, but there are more features and tools that I did not cover. If you want to go more in depth and see more examples you should check out w3schools.com. It is a very good resource to learn different coding languages.

___________________________________________________________________

Thanks for taking the time to read my blog :)

If you have more time, check out my other blogs in the links down below.

--

--