introduction to programming with python lecture 2

20
Introduction to Programming with Python – Class 2 SYED FARJAD ZIA ZAIDI

Upload: syed-farjad-zia-zaidi

Post on 24-Dec-2014

322 views

Category:

Software


2 download

DESCRIPTION

Slides for Lecture 2 of the course: Introduction to Programming with Python offered at ICCBS. It covers the following topics: Flow Control

TRANSCRIPT

Introduction to Programming with Python – Class 2SYED FARJAD ZIA ZAIDI

Class Objectives

Class Objective

Review Flow Control.

Understanding Loops.

Write few simple programs that uses conditionals and loops.

Class Material

•Chapter 5 - Python for Informatics: Exploring Information

Reading

Flow Control of the Program

Selection (If/Else) Repetition (Loops)

Conditional Execution In order to write useful programs, we almost always need to

check some conditions and change the program accordingly. Conditional Statements gives us this ability. The simplest form is if statement.

General Form:

if [expression1]:

body1

elif [expression2]:

body2

else:

bodyN

Python Comparison OperatorsOperator

Description

== Checks if the value of two operands are equal or not, if yes then condition becomes true.

!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

Python Logical OperatorsOperator

Description

and Called Logical AND operator. If both the operands are true then then condition becomes true.

or Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true.

not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Questions?

Python Loops Introduction:

There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times.

Python Loops General Form:

Python Loop Types

while Loop

• Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

for Loop

• Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

Loop Control Statementsbreak statement

Terminates the loop statement and transfers execution to the statement immediately following the loop.

continue statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

pass statement

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

for Loop Syntax:

for iterating_var in sequence:

statements(s)

Example:

for letter in “FirstLoop”:

print letter

while Loop Syntax:

while expression:

statement(s)

Example:while (True):

print “Anything”

Questions?

Class Exercise 1

Write a function that takes an string and prints it ten times.

Class Exercise 2

Write a function that takes an integer and prints the multiplication table of the given integer.

Class Exercise 3

Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:Score Grade>= 0.9 A>= 0.8 B>= 0.7 C>= 0.6 D< 0.6 FIf the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.

Class Exercise 4

Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use raw_input to read a string and float() to convert the string to a number. Do not worry about error checking the user input - assume the user types numbers properly.

Questions?