<?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=195.99.136.50</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=195.99.136.50"/>
	<link rel="alternate" type="text/html" href="https://ideawaza.com/wiki/Special:Contributions/195.99.136.50"/>
	<updated>2026-09-29T11:51:34Z</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=38209</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=38209"/>
		<updated>2007-12-07T13:37:30Z</updated>

		<summary type="html">&lt;p&gt;195.99.136.50: /* For concept in lesson: learn(concept) */&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>195.99.136.50</name></author>
	</entry>
	<entry>
		<id>https://ideawaza.com/index.php?title=Archive:Python/Operators&amp;diff=38146</id>
		<title>Archive:Python/Operators</title>
		<link rel="alternate" type="text/html" href="https://ideawaza.com/index.php?title=Archive:Python/Operators&amp;diff=38146"/>
		<updated>2007-12-07T11:48:43Z</updated>

		<summary type="html">&lt;p&gt;195.99.136.50: /* Indexing */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Operations, The Format Specifier, and Other Commands=&lt;br /&gt;
&lt;br /&gt;
==Operations==&lt;br /&gt;
&lt;br /&gt;
There are certain things that you can do with all of the &#039;&#039;types&#039;&#039;.  You can &#039;&#039;do simple math, index, slice, and check for membership.&#039;&#039;  You can also use the Format Specifier in &#039;&#039;strings&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
===Simple Math===&lt;br /&gt;
&lt;br /&gt;
Math is very straight forward in Python.  + adds, - subtracts, / divides, and believe it or not * multiplies.  The main thing to comment on is %.  % divides a problem and then returns the remainder. This is called the modulo operation.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;9 % 3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
0&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;9 % 2&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
1&lt;br /&gt;
&lt;br /&gt;
The % command can be used to find out if something is even or odd or if it is a multiple of some number.  Here is some code that is a little advanced for you right now.  Just type it in and now that within a couple of lessons you will know what you did.  When you type &#039;&#039;&#039;&amp;quot;print a is even&amp;quot;&#039;&#039;&#039; it must be 4 spaces to the right of the &#039;&#039;&#039;i&#039;&#039;&#039; in &#039;&#039;&#039;if&#039;&#039;&#039;, I am having trouble working with Wikiversity and can&#039;t get it to do that on this page without forming some kind of box.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;a = 4&#039;&#039;&#039;&lt;br /&gt;
 &amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;if a % 2 == 0:&#039;&#039;&#039;&lt;br /&gt;
 ...     &#039;&#039;&#039;print &amp;quot;a is even&amp;quot;&#039;&#039;&#039;&lt;br /&gt;
 &#039;&#039;&#039;a is even&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can also use variables, and elements and values in simple math.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = 1&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;y = 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x + y&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;6&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
That is how the variable works.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = [1, 2, 3]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x[1] + x[2]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
That is how you use elements from a tuple or list to do math.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = {&#039;a&#039;:1, &#039;b&#039;:2}&#039;&#039;&#039;&lt;br /&gt;
=You can also add and multiply strings, tuples, and lists.&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x[&#039;a&#039;] + x[&#039;b&#039;]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;3&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
That is how you do math with values from a dictionary.  Don&#039;t forget to use quotations around the keys unless you use &#039;&#039;integers&#039;&#039; as the keys.  Spend a couple of minutes messing around with this stuff, its fun and it&#039;ll help you remember it better.  You may also add strings, tuples, and lists.&lt;br /&gt;
&lt;br /&gt;
====Concatenating====&lt;br /&gt;
&lt;br /&gt;
To add two strings together. to do this you just type the first string, an addition sign, the second string.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;&amp;quot;Python&amp;quot; + &amp;quot; is awesome&amp;quot;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You can do the same with variables refering to strings.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = &amp;quot;Python&amp;quot;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;y = &amp;quot; is awesome&amp;quot;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x + y&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;Python is awesome&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You can do the same with tuples.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = (&#039;I&#039;, &#039;Love&#039;)&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;y = (&#039;Wikiversity&#039;,)&#039;&#039;&#039;   &#039;&#039;DON&#039;T FORGET THE COMMA AFTER &#039;&#039;&#039;WIKIVERSITY&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;print x + y&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;(&#039;I&#039;, &#039;Love&#039;, &#039;Wikiversity&#039;)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
It works the same with &#039;&#039;lists&#039;&#039;.  What you cannot do is combine two different kinds of &#039;&#039;types&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = (&#039;I&#039;, &#039;Love&#039;)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;y = &#039;Python&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;print x + y&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Traceback (most recent call last):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;File &amp;quot;&amp;lt;pyshell#91&amp;gt;&amp;quot;, line 1, in ?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;print x + y&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;TypeError: can only concatenate tuple (not &amp;quot;str&amp;quot;) to tuple&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
====Multiplying &#039;&#039;Types&#039;&#039;====&lt;br /&gt;
&lt;br /&gt;
Multiplying is very easy and straight forward.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = &#039;dog&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x * 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;dogdogdogdogdog&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now with a tuple.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = (&#039;dog&#039;,)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x * 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;(&#039;dog&#039;, &#039;dog&#039;, &#039;dog&#039;, &#039;dog&#039;, &#039;dog&#039;)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now with a dictionary.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = {&#039;dog&#039;:1}&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x * 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Traceback (most recent call last):&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;File &amp;quot;&amp;lt;pyshell#5&amp;gt;&amp;quot;, line 1, in ?&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;x * 5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;TypeError: unsupported operand type(s) for *: &#039;dict&#039; and &#039;int&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You can&#039;t do it with dictionaries, that would make more than one of the same key, remember.&lt;br /&gt;
&lt;br /&gt;
===Indexing===&lt;br /&gt;
You have already learned how to index in lesson 2.  When you typed x[3] you were indexing.  You may also index a string without first making a variable represent it.  &lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;&amp;quot;I Love Wikipedia&amp;quot;&#039;&#039;&#039;[5]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;e&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
simple enough.&lt;br /&gt;
&lt;br /&gt;
===Slicing===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Slicing&#039;&#039; is used to access a range of elements the way that &#039;&#039;indexing&#039;&#039; accessess one element.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = &amp;quot;Wikipedia and Python are awesome&amp;quot;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;print x[14:32]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Python are awesome&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The numbers that you enter after the variable (the [14:32]) are called &#039;&#039;indices&#039;&#039;.  The way the last one works is hard to understand at first.  I&#039;ll do another example to make it clear.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = &#039;1234&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now type:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;print x[1:3]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;23&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
It prints out the 2 because it starts numbering at 0, but the reason it prints out the 3 instead of the 4 is because it counts the &#039;&#039;&#039;[3]&#039;&#039;&#039; that you entered as the end point and does not include it.  Seems wierd at first but easy to get used to.&lt;br /&gt;
&lt;br /&gt;
You may also use negative numbers as the indices &#039;&#039;&#039;(x[-12:-])&#039;&#039;&#039;.  If you only use one negative indice &#039;&#039;&#039;(x[-4:])&#039;&#039;&#039; Python will print starting with the element selected and go until the end of the string, tuple or list.  This works for positive indices as well &#039;&#039;&#039;(x[:3])&#039;&#039;&#039;.  Python will print the first 2 elements of &#039;&#039;&#039;x&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Another thing that you can do when &#039;&#039;splicing&#039;&#039; is enter the &#039;&#039;step&#039;&#039; indice.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x = &amp;quot;0123456789&amp;quot;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x[1:-1:3]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039; &#039;147&#039; &#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You may also just use the step indice.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x[::3]&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;0369&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
You can do this to find every other element or every third or whatever your needs require.  You may also make the step indice negative.  By doing so you make the elements return starting with the last one.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x=&#039;0123456789&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Now Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;x[-2:1:-1]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;8765432&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
When the step indice is negative it is important to remember to make sure that the first indice is calling an element that comes later in the string, tuple or list than the second element.  Just as in the last example &#039;&#039;&#039;-2&#039;&#039;&#039; is representing &#039;&#039;&#039;8&#039;&#039;&#039; while &#039;&#039;&#039;1&#039;&#039;&#039; is representing 2.&lt;br /&gt;
&lt;br /&gt;
I&#039;ll have to finish this lesson some other time, my bed is calling me.&lt;br /&gt;
&lt;br /&gt;
===The Format Specifier===&lt;br /&gt;
&lt;br /&gt;
You are going to use this versatile command constantly.&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &#039;&#039;&#039;print &#039;I Love %s&#039; % &#039;Linux&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;I Love Linux&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
What the percent sign does is hold the place in the string and let you tell Python what to put there at the end of the string.  The &#039;&#039;&#039;s&#039;&#039;&#039; behind the &#039;&#039;&#039;%&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[Category:Computer science]]&lt;br /&gt;
[[Category:Programming]]&lt;br /&gt;
[[Category:Python]]&lt;/div&gt;</summary>
		<author><name>195.99.136.50</name></author>
	</entry>
</feed>