A boolean variable is a variable that holds either the value true or false. It is not a string, nor is it a number. We can declare and initialize the variables like this:
Line 2: pythonIsAwesome is a boolean variable set to True. Notice that True is blue, this is because it's a keyword.
Line 3: pythonIsLame is a variable set to False. False is also a keyword.
Line 5-6: Output the true and false values.
Unlike math operators we can't add true and false. We can have other operators though, for instance, and, or, and not. Not just inverts it, not true is false and not false is true. And needs both to be true for the result to be true. Or only needs one or the other to be true (or both).
Okay but here's how you can code it:
Line 2-3: Set result to True and True which is True then prints it
Line5-6: Set result to True and False which is False because both need to be true to get true. Prints it.
Line 9-10: Set result to False or True, which is true since one is true. Prints it.
Line 12-13: Set result to False or False, which is false because neither is true. Prints it.
Something you learned about in elementary school math can also be used to compare a number to give us a true or false statement. Here's the number comparisons we can use in Python:
Here's what it may look like in code:
Line 2: prints true because 5 is less than 6
Line 3: prints false because 5 is not less than 4
Line 4: prints false because 5 is not less than 5
Line 5: prints true because 5 is greater than or equal to 5
Line 6: prints true because 5 is not equal to 8
Line 7: saves true in result because 5 is equal to 5.
Line 8: prints True
Note: You could use a number variable and compare it too.
We can take this concept and combine it with our Boolean operators!
Line 2: prints true because 5 is less than 6 and it only needs one condition to be true.
Line 3: prints false because 4 is not greater than 8 and thus the and statement makes this False
Line 4: prints True, because 5 ==6 is false, and not false is True... Ouch my brain
Okay now we can go one final step further. We can use strings and check order alphabetically, or see if they're equal (or not) using the exact same comparison operators. Keep in mind that A is less than B because it comes earlier in the alphabet.
Line 2: outputs True because A comes before B in the alphabet
Line 3: outputs false because Kenny doesn't come after Kyle alphabetically
Line 5-6: Stores Kenny ins nameOne and Kyle in nameTwo
Line 7: Prints out True because Kenny is before Kyle.
Note <, >, order matters. You can also use == to see if a string is equal or != to see if they're not equal.
Being able to state true or false is neat but this is only the beginning. If statements are used to make decisions. These exist in just about every program. You can use if statements to make a menu, do a decision tree, skill tree, questions, and add a ton of interactivity.
Line 1: condition is true
Line 3: checks if condition is true, if it is then it runs the indented code
Line 5-6: prints since condition is true, if it was false it wouldn't print and skip these lines.
Line 8: prints regardless because it's not indented in the code.
Often we don't use a boolean variable in the if statement instead we check the condition in the if statement. Here's an example of how it may be structured:
What if you want your program to do something if the condition is true and something else if the condition is false?
The else statement activates if the above condition(s) is false.
Line 10: coin is set to heads
Line 11: Checks if coin is tails, it isn't so it skips the indented code.
Line 13: Else statement
Line 14: prints the coin came up heads (this would be skipped if line 11 was true)
Line 16: prints regardless.
Note you can use and/or/not operators in the if statements.
What if we want to check multiple cases? This is where the elif statement comes in. It's called else if statement but in python we get lazy and write it as elif. Here's how it works:
Line 1: number is set to 8
Line 3: check if number is less than 5, it's not so it skips to the next elif
Line 6: checks if number is less than 7, it's not so it skips to next elif
Line 8: checks if number is less than or equal to 10, it is so it runs the next line:
Line 9: prints, then skips the else code since we found a true elif/if
Note: The order matters, you have to be careful because if you have multiple true statements the first true condition is the one that the computer will use.
Line 1: sets mark to 6
Line 3: checks if mark is less than 5, it's not so it goes to the next elif.
Line 4: checks if mark is less than 8, it is so it prints "B" which is a logical error.
This illustrates how you have to be careful of the order and conditions.
While loops are if statements that run code that is indented over and over until the condition is no longer true.
Line 1: sets the counter to 0
Line 4: while statement, checks if counter is less than 7 if it is then it runs indented code
Line 5: prints counter value
Line 6: adds one to the counter then it jumps back up to line 4 to see if it is less than 7.
Line 8: prints.
Break statements can exit the while loop sooner than intended. If we add in an if statement we can make an early exit condition.
This is essentially the same program, I added the if statement
Line 4: checks if counter is 3 if it is it breaks out of the while loop.
Continue statement works like the break except it jumps to the start of the loop again. You have to be careful of the order because you could end up with an endless loop, I'll change break to continue and move the counter+=1 above the if statement.
Notice that the output skipped 3, because the loop continues if counter equals 3.
Because I increased the counter before printing it starts at 1 and goes up to 8.
For loops are like condensed while loops. While they're less lines there's a lot going on.
Line 2: for statement. The i takes on each value from 0 up to 7(not including 7) and loops that many times.
Line 3: prints i
Line 6: prints end of loop
NOTE: in the case of for loops you can use one letter variables i, j, and k.
You can choose to use two numbers in range, starting value and upper limit. If you use a third you choose how much i counts up by.
Starts at 0, stops when i is greater than or equal to 7, and counts up by 2.
Starts at 7, stops when i is less than or equal to 0, and counts down by 2 because the third number is negative.
While we don't have to cover functions it makes learning the next two units much easier because it helps you make modular programs. Functions are like mini programs within your program.
Line 1: defines the function countToTen, the indented code will run every time the function is called later.
Line 2-3: for loop prints 1 to 10 inside the function
Line 5: prints
Line 6: calls the countToTen function
Line 7:prints.
Here's the output:
Error, this screenshot is from an older program. This output starts at 0, the code starts at 1
Functions can do neat things with data too. Functions can pull in data and then manipulate it or output something. Here's how to pass data to a function using arguements:
Line 1: declares the function, put x and y in the brackets, these are our arguments. We need to pass data to them.
Line 2: prints the arguments added together
Line 4: calls the function then outputs 9 because x = 2, then y = 7 in our function and 2+7 is 9
We can also pass variable values:
Reuses the last example's functions
Line 4 and 5: sets the variables to 5 and 3
Line 7: passes the variable values into x and y respectfully to have them added together.
Functions are best used to do multi step processes, they're efficient since you can reuse them over and over. We didn't even cover return functions (<-maybe a challenge).
Functional coding is a way of sectioning your code using functions. We may use this for our project but it's not necessary.
Some functions are fairly small and are used to take in arguments and return a simple expression or calculation. For these you can use Lambdas:
This is if you'd like to go above and beyond.