0% found this document useful (0 votes)
2 views12 pages

L3b - Basic

This document covers the basics of Boolean expressions, relational operators, and logical operators in Python. It includes examples of how to use these operators, type conversions, and exercises to practice creating programs that utilize these concepts. Additionally, it introduces membership operators to check if an object is contained within another.

Uploaded by

tanqueray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

L3b - Basic

This document covers the basics of Boolean expressions, relational operators, and logical operators in Python. It includes examples of how to use these operators, type conversions, and exercises to practice creating programs that utilize these concepts. Additionally, it introduces membership operators to check if an object is contained within another.

Uploaded by

tanqueray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lesson 3b: Python Basics

1
Boolean expressions
"Boolean expressions" are expressions where the type of the result is a
Boolean (True or False).

Relational operators allow defining Boolean expressions.

Relational operators
<, <=, >, >=, ==, !=
Applicable to operands of any kind.
If the comparison is true, the result is True.
If the comparison is false, the result is False.

Examples:

Let's assume x is 3 (x=3):

x < 5 à True x >= 3 à True


x <= 2 à False x == 3 à True
x > 3 à False x != 3 à False

2
Relational operators

x < 10 (x=2)

Operand1 Operation Operand2

X < 10
True
Result

In Python this is Although in many languages


Ok 0 < x < 10 it is not possible. 3
Logical operators
They allow defining complex statements from relational operators.

Logical operators

Operation Symbol Example Result

AND and (5 < 3) and (10 < 20) False

OR or (5 < 3) or (10 < 20) True

NOT not not(5 < 3) True

and True False or True False not

True True False True True True True False

False False False False True False False True

4
Type Conversions

• Just as we could do type conversions between str, int and float


types, we can also do conversions with the bool type.

• The bool function converts a value or variable to False or True.


If the value or variable is equal to 0, it returns False, otherwise
returns True.

5
Exercise

Write the following program:


1- Ask the user for an integer value.
2- Display True if the value is between 0 and 10. Otherwise, display False.

0 < x < 10

0 10
( )
x>0 x<10

6
Solution
Remember that input() always reads as string (str)

x = int(input())
print((x > 0) and (x < 10))

7
Exercise
Write a program that ask the user to introduce two integers that are the x and y
coordinates of a point in a 2D plane.

The program must print True if the point is inside a square of width and height
10. In case the point is out of the square, the program must print False.

0,0

10,10

8
Solution 1

x = int(input("Coordinate x: "))
y = int(input("Coordinate y: "))
print((x >= 0 and x <= 10) and (y >=0 and y<=10))

9
Solution 2
XMIN = 0
YMIN = 0
XMAX = 10
YMAX = 10

x = int(input("Coordinate x: "))
y = int(input("Coordinate y: "))
print((x >= XMIN and x <= XMAX) and (y >=YMIN and y<=YMAX))

10
Solution 3
XMIN = 0
YMIN = 0
XMAX = 10
YMAX = 10

x = int(input("Coordinate x: "))
y = int(input("Coordinate y: "))
print((XMIN <= x <= XMAX) and (YMIN <= y <= YMAX))

11
Membership operators
They are operators to assess whether one object is inside another one.

Membership operators

Operation Symbol Example Result

Included in “a” in “Hello” False

Not included not in “la” not in “Hello” True

12

You might also like