If Python, Then Cake! Learn Conditional Statements or Else

If Python, Then Cake! Learn Conditional Statements or Else

When I was a young whippersnapper, I didn’t get dessert unless I finished my dinner. Dinner always consisted of healthy stuff like spinach so it was very hard to finish. That was my introduction to conditional statements. Conditional statements are like saying, “If this, then that.” If I ate dinner, then I got dessert. In my example, eating dinner is the boolean condition and getting dessert is the consequent. if Python, then code?

If Python, then Cake!

We can write my example above in Python:

Sweet! I ate dinner so I get dessert. Unless the cake is a lie…

We don’t have to explicitly evaluate True or False in our condition statement. Python will interpret the value of our variable and proceed accordingly. I can rewrite the above example like this:

Because the variable dinner was assigned True, I get cake.

else

Let’s write the function, dessert(), this time with what is called an alternative.

Piece of cake, right? It gets better. Let’s combine Boolean logic operators with our variables.

If you recall, if not dinner  is the same as “if dinner is False”.

You can only get ‘So full!’ if both dinner and cake are True.

If you want to test a condition, use an if statement. If you learned nothing, start again. Else, proceed to the next tutorial: multiple conditions.

Hands-on Computer Science

Learn computer science, programming, and web development with us, your educational and entertaining super friends.

Subscribe to our YouTube Channel

If Python, Then Cake! Learn Conditional Statements or Else was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid
×