0% found this document useful (0 votes)
65 views

Python3 Lesson 03

The document discusses boolean expressions and comparison operators in Python. It explains that boolean expressions return True or False and compare two values. It also covers comparison operators like ==, !=, <, >, <=, >= and logical operators like and, or, not. The document then discusses conditional statements like if/else and how to check if a value is in a list using in.

Uploaded by

Ananda Saikia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Python3 Lesson 03

The document discusses boolean expressions and comparison operators in Python. It explains that boolean expressions return True or False and compare two values. It also covers comparison operators like ==, !=, <, >, <=, >= and logical operators like and, or, not. The document then discusses conditional statements like if/else and how to check if a value is in a list using in.

Uploaded by

Ananda Saikia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Alvaro Sebastián Yagüe

www.sixthresearcher.com

@SixthResearcher
Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher
Data comparison: boolean expressions

A boolean expression is a comparison between two data values and returns True
or False. True and False values are from another data type: boolean type.

“True”
“False”

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: boolean expressions

A boolean expression is a comparison between two data values and returns True
or False. True and False values are from another data type: boolean type.

‘=‘ and ‘==‘ are not the same thing

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher
Data comparison: operators

The comparison operators provided by Python are:

x == y x is equal than y
x != y x is different than y
x<y x is lower than y
x>y x is greater than y
x <= y x is lower or equal than y
x >= y x is greater or equal than y

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: operators

The comparison operators provided by Python are:

x == y x is equal than y
x != y x is different than y
x<y x is lower than y
x>y x is greater than y
x <= y x is lower or equal than y
x >= y x is greater or equal than y

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: logical operators

The logical operators allow to combine multiple comparisons (boolean expressions):

“and” “or” “not”

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: logical operators

The logical operators allow to combine multiple comparisons (boolean expressions):

“and” “or”

A B A and B == 1 A or B == 1
1 1
1 0
0 1
0 0

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: logical operators

The logical operators allow to combine multiple comparisons (boolean expressions):

“and” “or”

A B A and B == 1 A or B == 1
1 1 1 1
1 0 0 1
0 1 0 1
0 0 0 0

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher
Data comparison: conditional statements

A conditional statement decides about the execution or not of a block of code


based in some data comparison:

if <condition> :
<code1>
else:
<code2>

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: conditional statements

A conditional statement decides about the execution or not of a block of code


based in some data comparison:

Code must
be indented
4 spaces

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: conditional statements

if <condition1> :
<code1>
else if <condition2> :
<code2>
else if <condition3> :
<code3>
else if <condition4> :
<code4>
else:
<code5>

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: looking for list values

if <value> in <list>:

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Data comparison: nested conditionals

This is a big mess

Using nested conditionals


we can improve code
readability

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher
Exercise: What’s the time?

Write a Python code to tell the hour in English or Polish formal way.

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Exercise: What’s the time?

A little help…

else if…
else if…
else …

Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher


Alvaro Sebastián Yagüe www.sixthresearcher.com @SixthResearcher

You might also like