Truth and flow control in Python: Difference between revisions

wikademia>AdamG
Add for loop section.
wikademia>Wikademia
No edit summary
 
(10 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{ntnes}}
==Boolean Expressions==
==Boolean Expressions==
Python has a Boolean type. The two values for this type are True and False. Note the capitalization: the first letter must be capitalized. Most text editors, including IDLE, will highlight True and False when properly capitalized.  
Python has a Boolean type. The two values for this type are True and False. Note the capitalization: the first letter must be capitalized. Most text editors, including IDLE, will highlight True and False when properly capitalized.  
Line 39: Line 40:
  >>> "2"+"4" == "24"
  >>> "2"+"4" == "24"
  True
  True
*Todo: Add other operators (and, not, or, is, in...)
 
=== Conjunction ===
The Python operator for conjunction is '''''and'''''.
>>> True and False
False
>>> True and True
True
>>> False and False
False
>>> False and True
False
 
The '''and''' operator works by evaluating until it runs into something false, and return the value that evaluated to false. If everything is True, it returns the last thing it evaluated.
 
>>> "a" and "b"
<nowiki>'b'</nowiki>
>>> "" and "b"
<nowiki>''</nowiki>
>>> True and 7
7
>>> True and [] and "a"
[]
 
=== Disjunction ===
The '''''or''''' operator works similar to the '''and''' operator. It returns the first value to be true, or the final value if there is no true value.
 
>>> False or ('a', 'b') or 'c'
('a', 'b')
>>> False or True
True
>>> <nowiki>''</nowiki> or False
False
>>> <nowiki>''</nowiki> or False or ()
()
 
 
*Todo: Add other operators (not, is, in...)
 
==Flow Control==
==Flow Control==
Now that you know all these great ways to work with booleans, let's use them!
Now that you know all these great ways to work with booleans, let's use them!
Line 74: Line 112:


Let's take our previous example. That program ran once, and even if nothing was entered, it was done. Let's suppose we instead want a program that "won't take no" for an answer: We'll use a while loop to make the prompt show up again and again until we get some response:
Let's take our previous example. That program ran once, and even if nothing was entered, it was done. Let's suppose we instead want a program that "won't take no" for an answer: We'll use a while loop to make the prompt show up again and again until we get some response:
  name == raw_input("What is your name?")
  name = raw_input("What is your name?")
  while name == "":
  while name == "":
     print "Hey! You didn't enter a name!"
     print "Hey! You didn't enter a name!"
Line 93: Line 131:
  while not name: # This is exactly the same as 'while name == "":', but a lot clearer.  
  while not name: # This is exactly the same as 'while name == "":', but a lot clearer.  
Okay, good? Cool. It's time to add a new feature: The for loop.
Okay, good? Cool. It's time to add a new feature: The for loop.
===For concept in lesson: learn(concept)===
===For concept in lesson: learn(concept)===
If you thought the Python idioms for the while loop were cool, the for loop is even better. A for loop takes this basic form syntax-wise:
If you thought the Python idioms for the while loop were cool, the for loop is even better. A for loop takes this basic form syntax-wise:
Line 101: Line 140:
     print i, # 1 2 3
     print i, # 1 2 3
Of course, any data type could be within the sequence. While we're at it, let's use a descriptive variable name:
Of course, any data type could be within the sequence. While we're at it, let's use a descriptive variable name:
  for letter in ['a','b','c']
  for letter in ['a','b','c']:
     print letter # output is a b c
     print letter # output is a b c
As you can see, then, for loops make it easy to iterate over a sequence,  or in simpler terms, to do something with each entry in a list. For loops are also excellent for times when you need to run a while loop a fixed number of times. Let's compare doint that with a for loop and a while loop:
As you can see, then, for loops make it easy to iterate over a sequence,  or in simpler terms, to do something with each entry in a list. For loops are also excellent for times when you need to run a while loop a fixed number of times. Let's compare doint that with a for loop and a while loop:
Line 115: Line 154:
Note how we don't have to worry about declaring i, or automatically incrementing it. It's also significantly faster, although most of the time your code will run fast enough anyway.  
Note how we don't have to worry about declaring i, or automatically incrementing it. It's also significantly faster, although most of the time your code will run fast enough anyway.  


Note that while I used 'i' as the variable name, you could use anything else. AS a general rule, when using the variable as a counter, simply use 'i'; this convention is widely followed and helps other programmers know exactly why you chose a for loop. Otherwise, use a descriptive variable name. For example:
Note that while I used 'i' as the variable name, you could use anything else. As a general rule, when using the variable as a counter, simply use 'i'; this convention is widely followed and helps other programmers know exactly why you chose a for loop. Otherwise, use a descriptive variable name. For example:
  aListOfDates = someFunction()
  aListOfDates = someFunction()
  for date in aListOfDates:
  for date in aListOfDates:
Line 122: Line 161:


Simply put, use while when you don't know or can't calculate how many times a loop will run. If you can, use for.
Simply put, use while when you don't know or can't calculate how many times a loop will run. If you can, use for.
[[Category:Python]]
[[Category:Programming]]