60 likes | 178 Views
Boolean Values And Conditionals. More "Building Blocks". We've seen Numbers: 1, 2, 3, 4, 5, … Strings: 'a', ' ab ', 'aback', … Lists: [1, 2, 3], ['a', 'b', 'c'], … Now a new type Booleans : True, False. (Yes there are only two of them) First letters must be capitalized. More "Glues".
E N D
More "Building Blocks" • We've seen • Numbers: 1, 2, 3, 4, 5, … • Strings: 'a', 'ab', 'aback', … • Lists: [1, 2, 3], ['a', 'b', 'c'], … • Now a new type • Booleans: True, False. (Yes there are only two of them) • First letters must be capitalized
More "Glues" • We've seen • Arithmetic operators: +, -, *, /, **, … • Indexing and slicing: [3], [0:5], [4:], [-1], … • Now a new type (shown in expressions) • 3 == 1 + 2 • 4 == 1 + 2 • 5 < -2 • 7 != 3 • They are not statements, but are expressions that evaluate to boolean values.
Other ways to obtain boolean values • andandor can be used to combine boolean values • (4 < 5) or (6 < 3) True • (4 < 5) and (6 < 3) False • (4 < 5) or ((4 < 5) and (6 < 3)) True • not (4 < 5) False • Rule: • A or B is True if A is True, or B is True, or both. • A and B is True only if A and B are both True. • In English, the word 'or' has another use: to choose between A and B, but not both.
More Statements • We've seen • Assignment statement: a = 5 • Definition statement: def addOne(x) : return x + 1 • Now a new type • Conditional statement: if 6 > 5: print('6 is greater than 5.') else: print('6 is no greater than 5.')
More statement • Yet another new type • Iteration statement myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] total = 0 for number inmyList: total = total + number print(total)