1 / 6

Boolean Values And Conditionals

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".

serge
Download Presentation

Boolean Values And Conditionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Boolean Values And Conditionals

  2. 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

  3. 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.

  4. 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.

  5. 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.')

  6. 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)

More Related