<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://ideawaza.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=213.141.151.0%2F24</id>
	<title>IdeaWazaWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://ideawaza.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=213.141.151.0%2F24"/>
	<link rel="alternate" type="text/html" href="https://ideawaza.com/wiki/Special:Contributions/213.141.151.0/24"/>
	<updated>2026-09-29T13:58:11Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://ideawaza.com/index.php?title=Truth_and_flow_control_in_Python&amp;diff=38208</id>
		<title>Truth and flow control in Python</title>
		<link rel="alternate" type="text/html" href="https://ideawaza.com/index.php?title=Truth_and_flow_control_in_Python&amp;diff=38208"/>
		<updated>2007-08-17T09:46:33Z</updated>

		<summary type="html">&lt;p&gt;213.141.151.220: /* While */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Boolean Expressions==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
===The bool() Function===&lt;br /&gt;
If you need to convert something into a boolean value, use the bool() function. For example:&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(1)&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(0)&lt;br /&gt;
 False&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(500)&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(-1)&lt;br /&gt;
 True&lt;br /&gt;
In Python, the integer 0 evaluates to False. All other integers will evaluate to True. As a general rule, try to avoid using Floating Point numbers in boolean expressions: use the int() operator to turn them into integers first.&lt;br /&gt;
&lt;br /&gt;
Strings behave similarly: an empty string evaluates to False, all other strings evaluate to True.&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(&amp;quot;Hello World!&amp;quot;)&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(&amp;quot;&amp;quot;)&lt;br /&gt;
 False&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(&amp;quot;True&amp;quot;)&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(&amp;quot;False&amp;quot;)&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; bool(&amp;quot;0&amp;quot;)&lt;br /&gt;
 True&lt;br /&gt;
Note that what is &#039;&#039;in&#039;&#039; the string does not matter: It can be &amp;quot;0&amp;quot;, &amp;quot;False&amp;quot; or &amp;quot;ReallyReallyFalse&amp;quot;: Everything that is not simply an empty string will evaluate to true. &lt;br /&gt;
&lt;br /&gt;
==Comparison Operators==&lt;br /&gt;
Python has a number of built-in comparison operators. They are very versatile and can often be used across data types. &lt;br /&gt;
=== Equality===&lt;br /&gt;
The Python operator for Equality is ==. Let&#039;s try it out:&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; 1==1&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &amp;quot;1&amp;quot;==&amp;quot;1&amp;quot;&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; 2+4 == 24&lt;br /&gt;
 False&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &amp;quot;2&amp;quot;+&amp;quot;4&amp;quot; == &amp;quot;24&amp;quot;&lt;br /&gt;
 True&lt;br /&gt;
&lt;br /&gt;
=== Conjunction ===&lt;br /&gt;
The Python operator for conjunction is &#039;&#039;&#039;&#039;&#039;and&#039;&#039;&#039;&#039;&#039;.&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; True and False&lt;br /&gt;
 False&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; True and True&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; False and False&lt;br /&gt;
 False&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; False and True&lt;br /&gt;
 False&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;and&#039;&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &amp;quot;a&amp;quot; and &amp;quot;b&amp;quot;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&#039;b&#039;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &amp;quot;&amp;quot; and &amp;quot;b&amp;quot;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; True and 7&lt;br /&gt;
 7&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; True and [] and &amp;quot;a&amp;quot;&lt;br /&gt;
 []&lt;br /&gt;
&lt;br /&gt;
=== Disjunction ===&lt;br /&gt;
The &#039;&#039;&#039;&#039;&#039;or&#039;&#039;&#039;&#039;&#039; operator works similar to the &#039;&#039;&#039;and&#039;&#039;&#039; operator. It returns the first value to be true, or the final value if there is no true value.&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; False or (&#039;a&#039;, &#039;b&#039;) or &#039;c&#039;&lt;br /&gt;
 (&#039;a&#039;, &#039;b&#039;)&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; False or True&lt;br /&gt;
 True&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt; or False&lt;br /&gt;
 False&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &amp;lt;nowiki&amp;gt;&#039;&#039;&amp;lt;/nowiki&amp;gt; or False or ()&lt;br /&gt;
 ()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*Todo: Add other operators (not, is, in...)&lt;br /&gt;
&lt;br /&gt;
==Flow Control==&lt;br /&gt;
Now that you know all these great ways to work with booleans, let&#039;s use them!&lt;br /&gt;
&lt;br /&gt;
==If, Elif, and Else==&lt;br /&gt;
Python uses a simple keyword called &amp;quot;if&amp;quot; to determine &#039;&#039;if&#039;&#039; it should execute a section of code. You can tell which lines are in this section by the level of indentation. The general format of if goes like this:&lt;br /&gt;
 if [Boolean Expression]:&lt;br /&gt;
     some code&lt;br /&gt;
 rest of code&lt;br /&gt;
This allows for very simple logic. What if you want to have two options, similar to &amp;quot;case&amp;quot; in other languages? Instead of Case, python uses &amp;quot;elif&amp;quot;. &lt;br /&gt;
 if [Boolean Expression]:&lt;br /&gt;
     code&lt;br /&gt;
 elif [Boolean Expression]:&lt;br /&gt;
     More code&lt;br /&gt;
 else:&lt;br /&gt;
     Even more code&lt;br /&gt;
 rest of program&lt;br /&gt;
One thing that&#039;s important to realize is that if the boolean expression for the first &amp;quot;if&amp;quot; is True, nothing in the elif section will execute, nor will anything in the else section. If there are multiple things that could evaluate to True, use multiple if statements. &lt;br /&gt;
&lt;br /&gt;
That code sample also introduced the &amp;quot;else&amp;quot; statement. Else gets executed if the &amp;quot;if&amp;quot; statement&#039;s boolean expression, and the boolean expressions for all elifs (if there are any), evaluate to False.&lt;br /&gt;
&lt;br /&gt;
So, how about some actual code? Here&#039;s a simple program; try entering this into IDLE and running it. &lt;br /&gt;
 name = raw_input(&amp;quot;What is your name?&amp;quot;)&lt;br /&gt;
 if name == &amp;quot;&amp;quot;:&lt;br /&gt;
     print &amp;quot;Hey! You didn&#039;t enter a name!&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 elif name == &amp;quot;spam&amp;quot; or name == &amp;quot;eggs&amp;quot;:&lt;br /&gt;
     print &amp;quot;A moose once bit my sister!&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 else:&lt;br /&gt;
     print &amp;quot;Nice to meet you, %s.&amp;quot; % name&lt;br /&gt;
===While===&lt;br /&gt;
While is our first example of a loop. While evaluates a boolean expression, and if that expression is True, it runs some section of code. It then re-evaluates that boolean expression: if it still evaluates to True, then it runs again. It keeps running - possibly forever! - until that loop evaluates to False, at which point the program continues with whatever comes after the while loop. &lt;br /&gt;
&lt;br /&gt;
Let&#039;s take our previous example. That program ran once, and even if nothing was entered, it was done. Let&#039;s suppose we instead want a program that &amp;quot;won&#039;t take no&amp;quot; for an answer: We&#039;ll use a while loop to make the prompt show up again and again until we get some response:&lt;br /&gt;
 name = raw_input(&amp;quot;What is your name?&amp;quot;)&lt;br /&gt;
 while name == &amp;quot;&amp;quot;:&lt;br /&gt;
     print &amp;quot;Hey! You didn&#039;t enter a name!&amp;quot;&lt;br /&gt;
     name = raw_input(&amp;quot;Okay, REALLY tell me your name this time:&amp;quot;)&lt;br /&gt;
 if name == &amp;quot;spam&amp;quot; or name == &amp;quot;eggs&amp;quot;:&lt;br /&gt;
     print &amp;quot;A moose once bit my sister!&amp;quot;&lt;br /&gt;
 else:&lt;br /&gt;
     print &amp;quot;Nice to meet you, %s&amp;quot; % name&lt;br /&gt;
That&#039;s even better. However, there are a couple of things I don&#039;t like about it. First, I don&#039;t like that first boolean expression. After all, we know that:&lt;br /&gt;
 bool(&amp;quot;&amp;quot;)==False&lt;br /&gt;
Therefore, wouldn&#039;t it make more sense just to test if name is False than to test if it&#039;s &#039;&#039;True&#039;&#039; that it&#039;s &#039;&#039;equal&#039;&#039; to &amp;quot;&amp;quot;?&lt;br /&gt;
 while bool(name) == False:&lt;br /&gt;
And do we really want to test if it&#039;s True than to Test that it&#039;s &#039;&#039;True&#039;&#039; that it&#039;s equal to &#039;&#039;False&#039;&#039;?&lt;br /&gt;
 while not bool(name):&lt;br /&gt;
That&#039;s right! Python has a &amp;quot;not&amp;quot; keyword, which acts a lot like ! in other languages. Python as ! too, but not is often clearer when working with long boolean expressions. &lt;br /&gt;
&lt;br /&gt;
We&#039;re missing one last thing: whatever comes after the &amp;quot;while&amp;quot; is automatically evaluated as a boolean expression. That means that in many cases (this one included) we can leave out the &amp;quot;bool().&amp;quot; (There will still be cases where it&#039;s needed, though, so remember that it exists!)&lt;br /&gt;
 while not name: # This is exactly the same as &#039;while name == &amp;quot;&amp;quot;:&#039;, but a lot clearer. &lt;br /&gt;
Okay, good? Cool. It&#039;s time to add a new feature: The for loop.&lt;br /&gt;
&lt;br /&gt;
===For concept in lesson: learn(concept)===&lt;br /&gt;
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:&lt;br /&gt;
 for [variable name] in [sequence]:&lt;br /&gt;
     code&lt;br /&gt;
The for loop will execute enough times that each element of &amp;quot;sequence&amp;quot; is assigned to &amp;quot;variable&amp;quot; once. For example:&lt;br /&gt;
 for i in [1,2,3]:&lt;br /&gt;
    print i, # 1 2 3&lt;br /&gt;
Of course, any data type could be within the sequence. While we&#039;re at it, let&#039;s use a descriptive variable name:&lt;br /&gt;
 for letter in [&#039;a&#039;,&#039;b&#039;,&#039;c&#039;]&lt;br /&gt;
    print letter # output is a b c&lt;br /&gt;
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&#039;s compare doint that with a for loop and a while loop:&lt;br /&gt;
 c = 0&lt;br /&gt;
 while c &amp;lt;10:&lt;br /&gt;
     someFunction(c)&lt;br /&gt;
     # code, code code&lt;br /&gt;
     c +=1&lt;br /&gt;
While this is legal Python code, it&#039;s not really descriptive. Furthermore, when working with large code blocks, it&#039;s easy to forget to add 1 to the variable (in this case &#039;c&#039;) at the end. Fortunately, we can do the same thing with a for loop, in a clearer fashion. &lt;br /&gt;
 for i in range(10):&lt;br /&gt;
      someFunction(i)&lt;br /&gt;
      # More code as needed&lt;br /&gt;
Note how we don&#039;t have to worry about declaring i, or automatically incrementing it. It&#039;s also significantly faster, although most of the time your code will run fast enough anyway. &lt;br /&gt;
&lt;br /&gt;
Note that while I used &#039;i&#039; as the variable name, you could use anything else. AS a general rule, when using the variable as a counter, simply use &#039;i&#039;; 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:&lt;br /&gt;
 aListOfDates = someFunction()&lt;br /&gt;
 for date in aListOfDates:&lt;br /&gt;
     doSomethingWith(date)&lt;br /&gt;
[[User:AdamG|I]] can personally say that I rarely use while loops. The main reason I use them at all, in fact, is when I need to repeatedly prompt a user until I get valid input. &lt;br /&gt;
&lt;br /&gt;
Simply put, use while when you don&#039;t know or can&#039;t calculate how many times a loop will run. If you can, use for.&lt;br /&gt;
&lt;br /&gt;
[[Category:Python]]&lt;br /&gt;
[[Category:Programming]]&lt;/div&gt;</summary>
		<author><name>213.141.151.220</name></author>
	</entry>
</feed>