Class 3

  1. Reading
  2. Loops
  3. Homework

Reading Class 3

Read through this on loops carefully as well. Skim through this on Lists. Skim through this on tuples. Skim through this on python dictionaries Skim through Control Flow Python, as a general rule on this particular page, the closer the section is to the top of the page, the more important it is at this moment.

looping

In programmaning, arguably the most important concept is looping. Looping alows the user to write a command one time and tell the computer to run it as many times as possible. For example, on your calender you may repeat an event every thursday, it would be extremely annoying and time consuming if you were to have to manually create the same event for thursday, rather telling the computer one time what your event is and then repeating it every thursday until a set date is fast and convienent.

In python there are two main ways to loop, for loops and while loops, you can read more about them here.

A for loop usually has a set start and end, it is used generally when you know the amount of times you want the loop to run.

A while loop is generally used when you are unsure the amount of times you need the loop to run. For example, you may want a loop to run every day until it rains, here you don't know the amount of days (times) you need it to run, just when to stop.

The syntax is simple:

for loop:

''' program to output the numbers from 1 to 25 '''

for x in range(1,26): # this runs from 1 through 26 (not including 26)
    print(x) # on every iteration x will be set to the next number and then we print it

while loop:

''' An infinite loop program, this program will never stop, so if you try to run it, press control-c to stop your computer at some point. '''

x = 1
while True: # meaning run always
    print("x is now " + str(x)) # print the current value of x, 
    #need to put x in the str() function so that print understands it, don't worry about that for now 
    x = x + 1 # increment x by 1

Homework Class 3

Names and ages

Get 10 names and their assoceated ages from the user, print to the screen: the oldest persion and their age.

Hashtag triangle

Get a height from the user and print a triangle (using hashtags) to the screen using that height as the number of hashtags on the bottom row.

For example if the user enters: 3, the output should be:

   #
  ##
 ###

If the user enters 5, the output should be:

     #
    ##
   ###
  ####
 #####

and so on.

Hint: You may want to print the triangle without spaces first. For example, an input of 5 would output:

 #
 ##
 ###
 ####
 #####

Once you do this, add the spaces in to finish the project.