L3b - Basic
L3b - Basic
1
Boolean expressions
"Boolean expressions" are expressions where the type of the result is a
Boolean (True or False).
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:
2
Relational operators
x < 10 (x=2)
X < 10
True
Result
Logical operators
4
Type Conversions
5
Exercise
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
12