-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_comparison_operators.py
More file actions
60 lines (53 loc) · 2.73 KB
/
Copy path09_comparison_operators.py
File metadata and controls
60 lines (53 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#======================================================================================
# COMPARISON OPERATORS
#--------------------------------------------------------------------------------------
# Comparison operators compares two values and return True or False based on the result.
#======================================================================================
#----------------------------------------
# Operator | Meaning |
#----------------------------------------
# == | equal to |
# != | not equal to |
# > | greater than |
# < | less than |
# >= | greater than or equal |
# <= | less than or equal |
#----------------------------------------
#--------------------------------------------------------------------------------------
# Value Operator Value ---> True/False
#--------------------------------------------------------------------------------------
# Value (3 > 2 ) --->True
# Variable(x=5) (x < 2 ) --->False
# Expression (2-1 != 2) --->True
# Function (len("hi")==3) --->False
#--------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------
# Basic Comparison operators
#--------------------------------------------------------------------------------------
print(10 == 10)
print(10 != 10)
print(7 > 3)
print(7 >= 3)
print(3 < 7)
print(7 <= 7)
#--------------------------------------------------------------------------------------
# We can also compare strings
#--------------------------------------------------------------------------------------
print("a" == "a")
print("a" == "A") # python is case-sensitive("a" and "A" are treated differently)
print("a" < "b")
#--------------------------------------------------------------------------------------
# assignment(=) vs comparison(==)
#--------------------------------------------------------------------------------------
x = "a" # assignment
print(x == "a") # True(comparison)
#--------------------------------------------------------------------------------------
# chained comparison--check multiple conditions in one line, just like math
#--------------------------------------------------------------------------------------
print(1<4<6)
#--------------------------------------------------------------------------------------
# chained comparisons work like SQL's BETWEEN They check if a value is between two bounds
#--------------------------------------------------------------------------------------
# Is age between 19 and 30 ?
age = 20
print(18 <= age <= 30)