Introduction to Python  
 
 

Lesson 3

Lesson 3: Conditionals, Loops, and Classes
Lesson Outline:

  • Loops and conditionals
  • Discussion of method types
  • Inheritance
Lesson Goals:
  • Understand and properly use loops
  • Learn how to create your own methods as needed
  • Understand how constructors and inheritance work
Readings:
Decision Control
Conditional Statements
Loops
Object Oriented Programming

Loops:
You will start today off with exercises on loops and conditionals, which you will be using in almost every program you write. Conditionals are comparisons in which a decision is made by the computer based upon the available data. It allows you to specify what you want the computer to do in different situations.
You will begin with "if-else" conditionals, which are used to determine what code a program will execute based on previous conditions. An ╥if-else╙ statement will first check if a given statement is true. If it is True, then code held within the if statement is executed. If the statement False, this code is skipped and code held within the optional else statement, which comes immediately after the if statement, is executed.
Sample Code:

x = 5 y = 10 z = 8 if(x >= y and x >= z): print x elif(y >= x and y >= z): print y elif(z >= x and z >= y): print z else: print "Darn!" Exercise:
Write a program from scratch that uses ╥if-else╙ statements to find the largest and smallest numbers in a set of integers. Then, using ╥if-else╙ statements, create different responses for every possible scenario in which you are given two booleans.
You will now learn for and while loops. while loops are extremely simple to understand. A while loop will check a condition just as an if statement would, and as long as that condition is true, the loop will continue to run through the code inside the statement.
Sample Code:
x = 0; while(x < 10): print x x = x + 1
for loops are similar, except they only accept a very specific kind of condition. It is rather hard to explain, so first look at an example of a for loop.
Sample Code:
x = 0; while(x < 10): print x x = x + 1
The for statement here can be thought of as this sentence: For every x within the range 0 to 10, starting at 0 and incrementing x by 1 until x reaches 10, print x. Under the hood, the loop first sets x to 0 by default and then checks if the value of x is within the given range. You can set the loop to perform the loop within a specific range (eg. for x in range(-5,5) will start x at -5 and repeat until x =5). The loop then runs the code held within it and increments x by 1. The process repeats until x is no longer less than 10. At this point, x is no longer within the given range and the code within the loop will not execute. The above sample code will print 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Exercise:
You are going to create a nested for loop. This is where you place one for loop inside of another for loop. This means that every time the outer loop runs through once, the inner loop runs until it stops. Your challenge is to create a nested for loop where the outer loop counts from 0 to 30 in increments of 3(i.e. 0, 3, 6, 9, 12, etc.) and the inner loop counts from 0 to whatever the current outer loops step is. Design it so that in your terminal you see something like this.
0,1,2,3
0,1,2,3,4,5,6
0,1,2,3,4,5,6,7,8,9
etc, until it reaches 30. Good Luck.
Functions:
We are now going to cover functions in detail. As you have already done a little bit of work with them, you should be somewhat familiar with them. If you haven╒t already toyed with them, you will now learn about return values, which are essentially the output of a function.
Sample Functions:
def hello(): print "Hello!" def add(): x = 1 y = 2 if x < y: return True else: return False foo(y): x = y + 5 print x, y return x bar(): return "This return as a string"
As you can see above, these methods return (respectively) nothing, a boolean, an integer, and a string. Python does not need to be informed of what it╒s going to return, because it will dynamically allocate memory for the program╒s functions. The way to end a function and return the correct type of value, is to use the return command.
Sample Code:
add(): z = 10 + 10 return z
This lesson will conclude the function section with arguments. Arguments, or parameters, are different objects that are placed between the parentheses of your created method. Passing these objects and variables into functions allow you to put generic formulas into your functions and change the output based on the parameters you pass in.
Sample Code:
mult(x, y): answer = x*y return answer
Exercise:
Use what you have just learned to write three different functions. One that compares two integers as arguments and returns a boolean that is true if the integers are equal. The second is one that takes two strings as arguments and returns them combined as a single String. The third is a function that takes three integers, adds two of them together, multiplies the product by the third, then returns the result.

For object oriented programming with classes and class extensions, please read Object Oriented Programming.
Exercise:
For your final exercise, you will create a class that implements two different functions, one of which takes strings and combines them, and the others takes two integers and divides them. You will then write a subclass, which extends the other class and contains another function with simply prints "Hello". Create instances of each of these classes and see what functions each can perform.


Files to Be Downloaded