computer science 101 if statement. python relational operators
DESCRIPTION
Primitive Conditions Conditions are expressions which evaluate to true or false. Conditions are expressions which evaluate to true or false. One kind of condition is the comparison of two numerical values of the same type. One kind of condition is the comparison of two numerical values of the same type. Examples: Examples: Payrate > 10 Payrate > 10 (x+10) == (y*z -8) (x+10) == (y*z -8)TRANSCRIPT
Computer Science Computer Science 101101
If StatementIf Statement
Python Relational Operators Python Relational Operators Operator Meaning
< Less than> Greater than
== Equal<= Less than or equal>= Greater than or equal!= Not equal
Primitive ConditionsPrimitive Conditions ConditionsConditions are expressions which are expressions which
evaluate to evaluate to truetrue or or falsefalse..
One kind of condition is the One kind of condition is the comparison of two numerical values comparison of two numerical values of the same type.of the same type.
Examples:Examples: Payrate > 10Payrate > 10 (x+10) == (y*z -8)(x+10) == (y*z -8)
The If-StatementThe If-Statement The syntax for the If-Statement isThe syntax for the If-Statement is
if <condition> :if <condition> : <list of statements> <list of statements>
• <condition> would be replaced by actual <condition> would be replaced by actual condition, etc.condition, etc.
• The colon is requiredThe colon is required
• The list of statements, must be indented – The list of statements, must be indented – part of the syntax for Pythonpart of the syntax for Python
The If-StatementThe If-Statement
The semantics are as expected – The semantics are as expected –
• the condition is evaluatedthe condition is evaluated
• if the condition is true, the list of if the condition is true, the list of statements is executedstatements is executed
If-Statement SemanticsIf-Statement Semantics
T
F
Cond
If-Statement examplesIf-Statement examples if yearsWorked > 10 :if yearsWorked > 10 :
bonus = 1000 bonus = 1000
if age >= 65 :if age >= 65 : total = 0.85 * total total = 0.85 * total numSeniors = numSeniors + 1 numSeniors = numSeniors + 1
The If-else-StatementThe If-else-Statement The syntax for the If-Else-Statement isThe syntax for the If-Else-Statement is
if <condition> : if <condition> : <list of statements> <list of statements>
else : else : <list of statements> <list of statements>
Note colon after elseNote colon after else The semantics are as expected - The semantics are as expected -
• the condition is evaluatedthe condition is evaluated• if the condition is true, then the first list of if the condition is true, then the first list of
statements is executed; otherwise, the statements is executed; otherwise, the second list is executedsecond list is executed
If-Else-Statement examplesIf-Else-Statement examples if yearsWorked > 10 :if yearsWorked > 10 : bonus = 1000bonus = 1000
else : else : bonus = 500 bonus = 500
if age >= 65 :if age >= 65 : price = 0.85 * price price = 0.85 * price numSeniors = numSeniors + 1 numSeniors = numSeniors + 1 else : else : nonSeniors = nonSeniors + 1 nonSeniors = nonSeniors + 1
Python SessionPython Session
Python Session (cont)Python Session (cont)