Python Programming 4

Think about some of the things a computer does. After winning a race in a video game, clicking a link to another page, or sending information to the correct place the computer makes a decision. Computers making decisions is where the real power of computers come from. This chapter is all about conditionals, the way you program a computer to think.

Check out this video and think about how AI might work.

Python conditionals are fairly straight forward but there are some tricky points. To start, in order to make a conditional, you need something to condition. Before we code that, Python has something called whitespacing. The reason python doesn't have parenthesis, braces, and semicolons like other languages is because of whitespacing. Whitespacing means that the code needs to be properly organized. After colons from conditionals and loops and functions etc. you need to bump the next line of code (as long as its connected to the loop/conditional) out 4 spaces or 1 tab. I personally use tabs.

//it is important to note that the lines (__) are actually 4 spaces or a tab.

x = 5

if x == 5:

____print("x is equal to 5")

else:

____print("x is not equal to 5")

Let's walk through this code. We created a variable that is equal to 5. The next line starts off with what we call an if statement. It starts any conditional. After the if word is the conditional, x==5. Why can't we just write x = 5? When testing an expression we need to write two equal signs, if you don't the program will break. After the colon come anything we want the computer to do if the expression is true. So after the colon we have a tab and a print(). Because this expression is true, this will print. If it were false it would skip the print and move on. When we are done doing whatever we want with the conditional, we begin typing the code back at front without any spaces. It reads like plain english, if x is equal to 5 then do this. Next we have an else statement. The else statement is linked with the if statement and cannot exist without it. It reads "if the condition in the if statement is false run the code in the else statement."

Try this out for yourself. Break the code intentionally and see what errors you get.

You can condition with strings as well.

name = "Tom"

if name == "Tom"

____print("Your name is Tom")

else:

____print("Your name is not Tom")

Time for you to work on a project. Your own project. Take what you've learned so far about conditionals and play with the code.


You can do more than just see if something is equal in an expression. Check out this code.

if 7 > 5:

____print("obviously 7 is greater than 5")

else:

____print("it would be a crime against math if this statement appeared")

You can also use > or < to determine if values are greater than or less than. You can use

if 6 >= 6:

too. This expression would evaluate to true because 6 is greater than or equal to 6. We can also test if something is not equal.

//Find people who are not named 'Jack'

name = "Jack"

if name != "Jack":

____print("Oh good your name is not Jack")

else:

____print("Oh no, you are Jack")

What if we want to test for multiple conditions? We can use the elif statement. Check out this code from Harrison Kinsley:

x = 5

y = 10

z = 22

if x > y:

____print('x is greater than y')

elif x < z:

____print('x is less than z')

else:

____print('if and elif never ran...')

Here is the elif in action. The elif statement runs because it is the true statement in this list of conditionals. It's important to know that the if, elif, and else are all connected. If the if statement was true in the scenario only 'x is greater than y' would be printed.

You can also nest if statements inside each other to check several things in a row:

name = 'Anthony Thompson'

position = 'quarterback'

college = 'NIU'

if name == "Anthony Thompson":

____if position == 'quarterback':

||||||||if college == 'NIU':

||||||||||||print('You were a Mount Carmel QB')

else:

____print('Who are you?')

the lines (|) represent another level of tabs or 4 spaces. They are 8 and 12 spaces or 2 and 3 tabs respectively.

Because all of these statements are true, the code keeps going down the if statement path. If the first condition were false, it would run the else statement. If any following condition runs false, the code will not print anything out. Only the first level if statement and else are tied together.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Video tutorials Video 1 Video 2


Design a program that acts as a terminal to get into a bank account. Make the user create and then use a username and password to get access. Once the user is in, let them make a choice to take out or put in money. If they fail to get the password out, make the program stop.

Now make a cool program of anything you like using your new skills. When finished upload it to reddit.

results matching ""

    No results matching ""