4. Loops

Loops are very important,and they serve many purposes.
what is a loop? The name preety much speaks for it's self.
A loop is something repeated over and over again. suppose you need to write something 100000000000 times it will take you forever to do so, but with a little skill you can do it in seconds. This loop is the while loop.

while 3 < 5:
     print "yay"


running it:


yay
yay
yay
yay
yay
.
.
.


This program will output infinitly many yay's. here's why:
When we wrote while 3 < 5, do something, since 3 is always less than 5 the something is always going to be done.

Notice that this wouldnt work with out the "tab" after the "while" the "tab" means that everything under the tab follows whats not... you will understand it better later when dealing with programs. Here is a different program


s=0
while s < 4:
      s = s+1
      print s


running it will look like this


1
2
3
4

Try to explain why this happened on your own.
here are some helpful symbols:
input name
!= does not equal to
== equal to
elif else if


("=" means equal and "= =" means equal to - they are not the same thing , if i were to say: if 2 equals to x, what i mean is: if 2 = = x:
think about it

Suppose that you want to write down all the numbers from 1 to 100. thats a very long list but as i said it can be done in seconds.


a=0
while a != 100:
      a = a+1
      print a


Try it out!, the program will write all the numbers from one to 100!
here is what i did.
a equals 0. while a does not equal 100, it equals one more than itself. Print a. lets do some of it.
when a =0, it doesnt equal 100 so the next number is a+1 which is 2. When a reaches 100, the loop stops, ending the progra.

Without looking at the answer below, try and create a program that will write only the even the numbers from 1 to 100

answer:


x = 0
while x !=100:
      x = x+2
      print x


now we kinda understand the while loop,i'll ad more to it soon so keep on checking this section.
"IF" statement

the "if" statement is preety straight forward, suppose i said, " if I will have a good day today, I will buy a piece of cake ,else, if i will have a bad day i will not but a piece of cake.
That was an example, i supposed something will happen and if it will then do operation 1. if something else will happen then do oeration 2. Lets look at the following program.


a = 11
if a > 20:
      print a, "is greater than 20"
else:
      print a, "is less than 20"


running it will look like this


11 is smaller that 20!


Since a is less than 22, whats under "else" is done.
here is another program:


a= 50
if a < 60:
      print a, "is less than 60"
elif a%2=0: #elif means else if
      print a, "is even"
elif a >70:
      print a, "is greater than 70"
running it wil look like this:
50 is less than 60
50 is even


you can use elif as many times as you want, but can olny use else one time.

5. Input - interaction with human part 1

By now you probably cant wait to start making a program that will interact with humans, well you are finally ready to do so.
The command "input= ()" is what we will begin with.
Here is a program that will ask you to give it a number.


a = input("tell me a number")


when you run it you will get this-


tell me a number


After you told it a number the program quit.

Nice huh?
well suppose you want the program not to quit but keep asking you number forever all you need to do is loop the program so that when it comes to the end, it will start from the beginning.


While 1:
      a = input("tell me a number")


The program will keep on repeating it's self.
Now i want to make a program that asks me for numbers, and if i ever answer "4", the program will say "good-bye".
Thats really not hard to do. We want to tell the computer: ask for numbers, when number = 4; print good-bye.
putting it as a program would look like this.


while 1:
     a = input("tell me a number")
     if a ==4:
          print "goodbye"



Now lets make a program that will tell us if the number we give it is even or odd. (Try to do this program yourself, before looking at how it's done)
Here's how:


while 1:
     a = input("tell me a number\n")      # the "\n" just means skip to the next line (and it comes before the "))
     if a % 2 ==0:
          print a,"is even"
     else:
          print a,"is odd"


exs: make a program that tells you if the number you give it is positive or negative.
Now lets make the following program.
You keep on giving the program numbers, and after every number you give the program, It will tell you the largest number it got so far.
Think about how i did it, its not a easy program.


x = 0
max = 0
number = 0

while 1:
     number = input("tell me a number")
     x = number

     if max==0:

          if x > number:                max = x

               print max, "is the largest number"

          else:

               max = number
               print max, "is the largest number"

     else:

          if number > max:
               print number, "is the largest number"

               max = number

          else:

               print max, "is the largest number"



When you run this pogram you should get something like this-


tell me a number2
2 is the largest number
tell me a number16
16 is the largest number
tell me a number5
16 is the largest number
tell me a number9
16 is the largest number
tell me a number
.
.
.


Try somethings out,make sure you understand what i did and how every steo of this program works...if you do, you will become much better at making simple programs.



i will add more to this section very soon, till then read about Tkinter

Tkinter-Page 3
or go to string tutorial