9/26/2012 1 Conditionals and Booleans CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17 1 Learning Outcomes • Will know what a block of code is. • An understanding of if statements and boolean expressions used with them. • An ability to use if-else statements • An ability to use if-elif-else statements • An understanding of conditionals used with strings • Be familiar with boolean operators • Be familiar with boolean algebra, including DeMorgan's law • Be familiar with Truth tables • Understand the use of logical and to replace nested ifs 2 Control Structures • Control Structures allow different lines of code to be executed depending on an evaluation of some expression. • The most common conditional structure is the if structure. • An if structure contains an expression and a block or body of code. • When the expression is true, the block of code associated with the if statement is executed, otherwise it's skipped. 3
13
Embed
Conditionals and Booleans Learning Outcomespark/cmsc201/fall12/...9/26/2012 1 Conditionals and Booleans CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
9/26/2012
1
Conditionals and Booleans
CMSC 201
Fall 2012
Instructor: John Park
Lecture Section 01
Discussion Sections 02-08, 16, 17 1
Learning Outcomes
• Will know what a block of code is.
• An understanding of if statements and boolean expressions used with them.
• An ability to use if-else statements
• An ability to use if-elif-else statements
• An understanding of conditionals used with strings
• Be familiar with boolean operators
• Be familiar with boolean algebra, including DeMorgan's law
• Be familiar with Truth tables
• Understand the use of logical and to replace nested ifs
2
Control Structures
• Control Structures allow different lines of code to be executed depending on an evaluation of some expression.
• The most common conditional structure is the if structure.
• An if structure contains an expression and a block or body of code.
• When the expression is true, the block of code associated with the if statement is executed, otherwise it's skipped.
3
9/26/2012
2
“if” Statements and Control Blocks
• If statements take the form : if <condition>:
<block>
• Take note of the ':' after the <condition>.
• In Python, the ':' character, colon, is used to denote the start of a code block.
• All code in the block must be tab indented after the ':'.
(For the rest of the lecture, when we refer to “tabs”, we mean “an additional level of indentation”, not an actual TAB character)
4
“if” Statements and Control Blocks
• The first line that is not tabbed-in is the first line not included in that code block.
if <condition>:
block-line-1
block-line-2
block-line-3
out-of-block-line-1
out-of-block-line-2
5
“if” Statements and Control Blocks
• Many other structures in Python also use code blocks in this same form, where all lines after the ':' are tabbed in to be part of that block. This includes functions and loops.
• The indentation formatting is critical: the lines in the block must all be indented exactly the same amount.
• Although it's possible to indent in any way (1 space, 3 spaces, 1 tab, etc..), it must be consistent, or python will return an error.
• Our CMSC 201 Coding Standard mandates an indentation of 4 spaces for each level
6
9/26/2012
3
“if” Statements and Control Blocks
• If statements and other structures that use blocks may be nested.
– This means the number of tab indents must increase with the level of nesting.
if <condition1>:
block1-line-1
block1-line-2
if <condition2>:
block2-line-1
block2-line-2
out-of-block2-line-1-in-block1-line-3
out-of-block2-line-2-in-block1-line-4
out-of-block1-line-1
out-of-block1-line-2
out-of-block1-line-3 7
Managing Indentation
• Emacs automatically handles Python indentation for you:
– Emacs analyzes your code as you type, looking for special tokens like the ‘:’ . It then infers the level of indentation
– When you start a line by hitting the <TAB> key, emacs automatically indents that line by inserting the correct number of leading spaces.
– To finish a block, hit <TAB> to indent to the current block depth, then hit <Backspace> to “unindent” a level.
8
Managing Indentation
• Since emacs indents consistently for us, using it will keep us from having this kind of error when our code is in a file. You'll still have to be careful about indentation when using the interactive interpreter though.
9
9/26/2012
4
Managing Indentation
>>> if True:
... x = 1
... y = 2
File "<stdin>", line 3
y = 2
^
SyntaxError: invalid syntax
>>>
10
Boolean Expressions
• The <condition> part of the if statement represents a boolean expression that is either true or false.
• Expressions can be formulated using mathematical symbols to compare different values.
11
Python Comparison Operators
Operation Python Operator
is less than <
is less than or equal to <=
is greater than >
is greater than or equal to >=
is equal to ==
is not equal to !=
12
9/26/2012
5
Python Comparison Operators
• The same 6 operators can be applied to strings, in which case it tests for equality or alphabetical order.
• Equality test (“==“) is strict, case-sensitive test
• For ordering, comparison is done position-by-position, stopping when a difference is found
– “less than”, (i.e., “earlier””) determined by comparing ASCII codes.