Booleans If Loops Lambda Functions

Booleans

If

Loops

Lambda

Functions

Logical and Function Constructions

K. Cooper1

1Department of Mathematics Washington State University

2021

Cooper Logic/Functions

Washington State University

Booleans

If

Loops

Lambda

Functions

Boolean Values

Many languages (including) Python use Boolean values: True/False.

The invocation of Boole's name always carries an identification of "True" with 1, False with 0. This is binary ? computers are good at that. Thus, computers can carry out actions based on simple decisions.

In most computer languages, 0 represents "False"; any other number is actually interpreted as "True" ? not just 1.

Cooper Logic/Functions

Washington State University

Booleans

If

Loops

Lambda

Functions

Logical Operators

x=2 print(x==2) results in True.

Remember that = denotes assignment: evaluate the expression on the right and put that value in the variable named on the left. The symbol == is a binary test: it asks the for the truth value of the statement, "the thing on the left is the same as the thing on the right."

dog = 'cat' print(dog=='cat') produces True.

Cooper Logic/Functions

Washington State University

Booleans

If

Loops

Lambda

Functions

Logical Operators

The test for equality is just one of many possibilities.

Symbol == != < > =

Test equal to not equal to less than greater than less than or equal to greater than or equal to

There are also the usual conjunctions, and and or. Use lots of parentheses when you use and or or.

Cooper Logic/Functions

Washington State University

Booleans

If

Loops

Lambda

Functions

Bitwise Logicals

and and or test whether numbers are zero or nonzero. There are other operators that look familiar that actually do Boolean logic on binary representations of integers.

Symbol & | ^

Test bitwise and bitwise or bitwise xor

6

&

7

0110 0111

6

8

&

7

1000 0111

0

6

|

7

0110 0111

7

8

|

7

1000 0111

15

Cooper Logic/Functions

Washington State University

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download