Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Alright, so we covered basic variable assignment and some simple data types.
- Today, we move onto basic logic and control structures.
- We will largely be playing with booleans and integers.
- Below are the two variables we will be using.
- """
- aBoolean = True
- anotherBoolean = False
- """
- This is one of the most basic (and useful) control structures in Python.
- An "if" statement is very straight forward.
- if ( this statement is true ) then do something
- The double equals sign asks if the two values are exactly the same
- A single equals sign assigns variable values.
- e.g. myInt = 5 sets the variable "myInt" to the value 5.
- myInt == 5 would ask if myInt's value IS 5 (and in this case it would be True)
- """
- print "1. Checking if aBoolean == True"
- if ( aBoolean == True ):
- """
- Indentation is how Python knows what statements to execute
- Think of having "blocks" around these statements that are connected
- to the if statement.
- As soon as we unindent, we are outside of the "if"
- """
- print " aBoolean's value is "+str(aBoolean)
- print " anotherBoolean's value is "+str(anotherBoolean)
- """
- "\n", when evaluated by a computer, usually makes a new line.
- Backslashes (\) are used as "escape" characters. They usually sit in front
- of other characters to make sure you can print them out.
- For example, putting \" lets you print an actual quote in Python
- (e.g. print "Tyrell said, \"More human than human is our motto.\"")
- Also notice that you can put entire statements into a print, not just variables.
- """
- print "The value of (aBoolean == True) is "+str(aBoolean == True)+"\n"
- #Now we're going to use some key words to make our if statements more complex
- #You can say "and" to combine two boolean statements. This means BOTH must be True.
- #You can also use the word "or" - meaning if either of them are true, then execute the if block.
- print "2. Checking if aBoolean == True and anotherBoolean == False"
- if ( aBoolean == True and anotherBoolean == False ):
- print " aBoolean's value is "+str(aBoolean)
- print " anotherBoolean's value is "+str(anotherBoolean)
- print "The value of (aBoolean == True and anotherBoolean == False) is "+str(aBoolean == True and anotherBoolean == False)+"\n"
- #Now, let's change it's value and see what happens
- aBoolean = False
- print "3. Checking if aBoolean == True, else"
- """
- Nothing inside the aBoolean == True block should print, since aBoolean is not True.
- In the first example (aBoolean == True) evaluates to True == True - which is true!
- Here you ask False == True? False will never equal True, so the result is false
- and the body of the if statement will not be executed.
- """
- if ( aBoolean == True ):
- print " aBoolean's value is "+str(aBoolean)
- print " anotherBoolean's value is "+str(anotherBoolean)
- else:
- #Here we have introduced the 2nd control structure, an else.
- #This means if the "if" is false, then do this instead.
- print " FALSE STATEMENT"
- print "The value of (aBoolean == True) is "+str(aBoolean == True)+"\n"
- print "4. Checking if aBoolean == True, elif aBoolean != True, else"
- """
- Now you have "elif" - meaning else if.
- Reading aloud, it sounds something like
- "if this is true, then do a. If not and this other thing is true, then do b. Otherwise, do c.
- Here's a new comparison operator as well. == compares for equality, but a bang (we don't call it an exclamation point)
- asks if they are not equal.
- """
- if ( aBoolean == True ):
- print " aBoolean's value is "+str(aBoolean)
- print " anotherBoolean's value is "+str(anotherBoolean)
- elif ( aBoolean != True ):
- print " aBoolean is not True."
- print " aBoolean's value is "+str(aBoolean)
- print " anotherBoolean's value is "+str(anotherBoolean)
- else:
- print "Hey, it can only be True or just NOT True! This can never be reached."
- print "The value of (aBoolean == True) is "+str(aBoolean == True)+". The value of aBoolean != True is "+str(aBoolean != True)+"\n"
Advertisement
Add Comment
Please, Sign In to add comment