omegazero

if_else.py

Jun 21st, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. """
  2. Alright, so we covered basic variable assignment and some simple data types.
  3. Today, we move onto basic logic and control structures.
  4.  
  5. We will largely be playing with booleans and integers.
  6. Below are the two variables we will be using.
  7. """
  8.  
  9. aBoolean = True
  10. anotherBoolean = False
  11.  
  12. """
  13. This is one of the most basic (and useful) control structures in Python.
  14. An "if" statement is very straight forward.
  15. if ( this statement is true ) then do something
  16. The double equals sign asks if the two values are exactly the same
  17. A single equals sign assigns variable values.
  18. e.g. myInt = 5 sets the variable "myInt" to the value 5.
  19. myInt == 5 would ask if myInt's value IS 5 (and in this case it would be True)
  20. """
  21.  
  22. print "1. Checking if aBoolean == True"
  23. if ( aBoolean == True ):
  24.     """
  25.     Indentation is how Python knows what statements to execute
  26.     Think of having "blocks" around these statements that are connected
  27.     to the if statement.
  28.     As soon as we unindent, we are outside of the "if"
  29.     """
  30.     print "  aBoolean's value is "+str(aBoolean)
  31.     print "  anotherBoolean's value is "+str(anotherBoolean)
  32.    
  33. """
  34. "\n", when evaluated by a computer, usually makes a new line.
  35. Backslashes (\) are used as "escape" characters. They usually sit in front
  36. of other characters to make sure you can print them out.
  37. For example, putting \" lets you print an actual quote in Python
  38. (e.g. print "Tyrell said, \"More human than human is our motto.\"")
  39.  
  40. Also notice that you can put entire statements into a print, not just variables.
  41. """
  42.  
  43. print "The value of (aBoolean == True) is "+str(aBoolean == True)+"\n"
  44.  
  45. #Now we're going to use some key words to make our if statements more complex
  46. #You can say "and" to combine two boolean statements. This means BOTH must be True.
  47. #You can also use the word "or" - meaning if either of them are true, then execute the if block.
  48. print "2. Checking if aBoolean == True and anotherBoolean == False"
  49. if ( aBoolean == True and anotherBoolean == False ):
  50.     print "  aBoolean's value is "+str(aBoolean)
  51.     print "  anotherBoolean's value is "+str(anotherBoolean)
  52. print "The value of (aBoolean == True and anotherBoolean == False) is "+str(aBoolean == True and anotherBoolean == False)+"\n"
  53.  
  54. #Now, let's change it's value and see what happens
  55. aBoolean = False
  56. print "3. Checking if aBoolean == True, else"
  57.  
  58. """
  59. Nothing inside the aBoolean == True block should print, since aBoolean is not True.
  60. In the first example (aBoolean == True) evaluates to True == True - which is true!
  61. Here you ask False == True? False will never equal True, so the result is false
  62. and the body of the if statement will not be executed.
  63. """
  64.  
  65. if ( aBoolean == True ):
  66.     print "  aBoolean's value is "+str(aBoolean)
  67.     print "  anotherBoolean's value is "+str(anotherBoolean)
  68. else:
  69.     #Here we have introduced the 2nd control structure, an else.
  70.     #This means if the "if" is false, then do this instead.
  71.     print "  FALSE STATEMENT"
  72. print "The value of (aBoolean == True) is "+str(aBoolean == True)+"\n"
  73.  
  74.  
  75. print "4. Checking if aBoolean == True, elif aBoolean != True, else"
  76. """
  77. Now you have "elif" - meaning else if.
  78. Reading aloud, it sounds something like
  79. "if this is true, then do a. If not and this other thing is true, then do b. Otherwise, do c.
  80.  
  81. Here's a new comparison operator as well. == compares for equality, but a bang (we don't call it an exclamation point)
  82. asks if they are not equal.
  83. """
  84. if ( aBoolean == True ):
  85.     print "  aBoolean's value is "+str(aBoolean)
  86.     print "  anotherBoolean's value is "+str(anotherBoolean)
  87. elif ( aBoolean != True ):
  88.     print "  aBoolean is not True."
  89.     print "  aBoolean's value is "+str(aBoolean)
  90.     print "  anotherBoolean's value is "+str(anotherBoolean)
  91. else:
  92.     print "Hey, it can only be True or just NOT True! This can never be reached."
  93. 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