Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Statements.

Similar presentations


Presentation on theme: "Control Statements."— Presentation transcript:

1 Control Statements

2 4 Major Versions of Python
“Python” or “CPython” is written in C/C++ - Version 2.7 came out in mid-2010 - Version came out in early 2010 “Jython” is written in Java for the JVM “IronPython” is written in C# for the .Net environment Go To Website

3 Conditional Steps Yes Program: x = 5 if x < 10: print 'Smaller‘
print 'Bigger' print 'Finis' print 'Smaller' X > 20 ? Yes print 'Bigger' print 'Finis'

4 if if statement: Executes a set of commands only if a certain condition is True. Otherwise, the commands are skipped. Syntax: if condition: statements Example: gpa = input("What is your GPA? ") if gpa > 2.0: print "Your application is accepted."

5 Range Test if (3 <= Time <= 5): print “Office Hour"

6 The IF Statement Yes No x = 5 if x == 5 : print 'Is 5‘
print 'Is Still 5' print 'Third 5’ print 'Still 5' print 'Third 5' The IF Statement

7 Two-way branch using else :
X = 4 x = 4 if x > 2 : print 'Bigger' else : print 'Smaller' print 'All done' no x > 2 yes print 'Smaller' print 'Bigger' print 'All Done'

8 if/else if/else statement: Executes one set of statements if a certain condition is True, and a second set if it is False. Syntax: if condition: statements else: Example: gpa = input("What is your GPA? ") if gpa > 2.0: print "Welcome to Mars University!" print "Your application is denied." Multiple conditions can be chained with elif

9 greater than or equal to
Logic Logical expressions can be combined using logical operators: Operator Meaning Example Result == equals 1 + 1 == 2 True != does not equal 3.2 != 2.5 < less than 10 < 5 False > greater than 10 > 5 <= less than or equal to 126 <= 100 >= greater than or equal to 5.0 >= 5.0 Operator Example Result and (9 != 6) and (2 < 3) True or (2 == 3) or (-1 < 5) not not (7 > 0) False

10 Nested Decisions x = 42 if x > 1 : print 'More than one'
yes print 'More than one' no x = 42 if x > 1 : print 'More than one' if x < 100 : print 'Less than 100' print 'All done' x < 100 yes no print 'Less than 100' print 'All Done'

11 Chained Conditional if x < 2 : print 'Small' elif x < 10 :
print 'Medium' elif x < 20 : print 'Big' elif x< 40 : print 'Large' elif x < 100: print 'Huge' else : print 'Ginormous' # No Else x = 5 if x < 2 : print 'Small' elif x < 10 : print 'Medium' print 'All done'

12 Exercise Write a pay computation program that gives the employee 1.5 times the hourly rate for hours worked above 40 hours (and regular 1.0 rate for less than 40 hours) Enter Hours: 45 Enter Rate: 10 Pay: 475.0

13 Example Your city classifies a pollution index
less than 35 as “Pleasant”, 35 through 60 as “Unpleasant”, and above 60 as “Health Hazard.” Display the correct description of the pollution index value.

14 Example Display one word to describe the integer value of number as “Positive”, “Negative”, or “Zero”

15 Example Write a program to ask a student for his grades in 3 exams ( each out of 50 ) , get their total and inform the student whether he passed or failed the course.


Download ppt "Control Statements."

Similar presentations


Ads by Google