Python IDLE - Module 4

advertisement
CMPD124 Introduction to Programming and Problem Solving Techniques
MODULE 4
The Python IDLE (GUI)
LESSON 1
DECISION LOGIC STRUCTURE
Use If/Then/Else instructions.
Flowchart Diagram of the Decision Structure.
Double selection.
CMPD124 Introduction to Programming and Problem Solving Techniques
Multiple Selection.
EXPRESSIONS AND CONDITIONS THAT CAN BE USED WITH DECISION LOGIC
STRUCTURE
Condition Expressions
There are several tests that can be done in a condition expression. Below is the list of symbols of
the different tests:
Symbols
<
<=
>
>=
==
!=
<>
Test Description
Less than
Less than or equal to
Greater than
Greater than or equal to
Equal to
Not equal to
Not equal to
CMPD124 Introduction to Programming and Problem Solving Techniques
Boolean Expressions
Boolean expressions are expressions that have a truth value. When evaluated, its end value is
either True or False. The type of the value is bool. True is represented by integer 1 and False is
represented by integer 0.
Boolean expressions are composed of operands and operators just like arithmetical expressions.
Boolean operators are comparison operators and logical operators.
Comparison Operators
As the name suggests, comparison operators compare two values in terms of magnitude. They’re
shown below.
Operator
Explanation
Expression
Truth Value
==
!=
<
<=
>
>=
is equal to
is not equal to
is less than
is less or equal to
is greater than
is greater or equal to
2 == 3
2 != 3
2 < 3
2 <= 3
2 > 3
2 >= 3
False
True
True
True
False
False
Logical Operators
Logical operators are used to combine two Boolean expressions into one. They’re shown below.
Operator
Explanation
Expression
Truth Value
and
both expressions must
be true
or
at least one expression
must be true
not
inverse the truth value of
the expression
True and True
True and False
False and True
False and False
True or True
True or False
False or True
False or False
not True
not False
True
False
False
False
True
True
True
False
False
True
CMPD124 Introduction to Programming and Problem Solving Techniques
1.1 Single-selection Statements
This is the simplest form of conditional statements. It allows you to execute one statement or
more if a condition is fulfilled. The condition is usually written as a Boolean expression.
if condition:
Python Syntax
instruction
Example Python codes:
if test_marks >= 50:
print “Passed\n”
1.2 Double -selection Statements
if you want your program to do something if a condition is not fulfilled? You use a doubleselection statement.
Python Syntax
if condition:
true instruction
else:
false instruction
Example Python codes:
if x > y :
print (“x is greater than y”)
else:
print (“ x is smaller than y”)
CMPD124 Introduction to Programming and Problem Solving Techniques
if test_marks >= 50:
print “Passed\n”
else:
print “Failed\n”
1.3 Multiple-selection Statements
If you’re still not satisfied with the double-selection statement, then there’s the multiple-selection
statement which is used to branch into any one of a range of outcomes.
Syntax
if condition:
statement
elif condition:
statement
else:
statement
Example Python codes:
test_marks = input(“Enter marks: ”)
if test_marks >= 80:
grade = “A”
elif test_marks >= 70:
grade = “B”
elif test_marks >= 60:
grade = “C”
elif test_marks >= 50:
grade = “D”
else:
grade = “F”
print “Grade: ”, grade
CMPD124 Introduction to Programming and Problem Solving Techniques
Example of Pseudo codes convert to Python Codes
Begin
Input value1, value
If value1 > value 2
Print value1 is
Else
Print value2 is
Endif
End
Python codes:
2
then
greater than value2
greater than value1
CMPD124 Introduction to Programming and Problem Solving Techniques
Example of Flowchart convert to Python Codes
Python codes:
Download