Python Tutorial #5 - The if-Statement

in #programming7 years ago

Welcome back to the Python Tutorial! Today we're gonna take a look at if-Statements.

With if-Statements we basically tell the Computer to check whether a condition is true or false. If it's true, the computer executes the code, if it's false, it doesn't.

Let's look at a little example:

a = 0

if a == 0:
      print("a equals 0!")

In this case, the Output would always be a equals 0! because we set a to 0. If we would do something like a = 1 first, nothing would happen.

There are a view other statements to look at:

a = 0

if a == 0:
      print("a equals 0!")

elif a == 1:
      print("a equals 1!")

else:
      print("a fulfils none of the specified conditions!")

The elif statement basically says the following: If the main condition is not true, try this one.
You can include an unlimited number of elifs.
else is saying: If not a single condition is fulfilled, do this.

And that's basically it! If you have any Questions please write them in the commentaries! We almost did it. After taking a look at functions in the next tutorial, we can program our own little calculator.