60 likes | 188 Vues
This guide explores the basics of Boolean values, conditionals, and logical expressions in programming. We'll cover the two Boolean values, True and False, and how they interact with arithmetic operations. You'll learn how to use conditional statements to execute code based on Boolean evaluations. Additionally, we'll introduce the concepts of logical operators such as "and" and "or," which help combine Boolean expressions. Examples will illustrate how these concepts function within programming constructs, enhancing your coding skills and logical reasoning.
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)