Learning Python: Part 5: Conditional Statements

Learning Python

Python is a general-purpose interpreted programming language. It emphasizes readability through the use of whitespace and supports multiple programming paradigms such as functional and object-oriented models.


Conditional Statements

Similar to assignment statements (where values are set) and expression statements (where something is “expressed”), there are also conditional statements.

If Python, a statement is conditional if the result of it is either True or False. These are Boolean values, named after George Boole.

If

The keyword if tests if some conditional statement is true or not. If it is True, some code is run. If it is not, the code is skipped.

If… else

The else statement runs if the if statement is not true. This works as an alternative to the if statement.

Comparisons

When testing if a conditional statement is True or not, there are a number of different comparisons.

  • Less-than (<)
  • Greater-than (>)
  • Equal (==)
  • Not equal (!=)
  • Less-than-or-equal-to (<=)
  • Greater-than-or-equal-to (>=)

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.