-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path03-operators.py
54 lines (41 loc) · 1.6 KB
/
03-operators.py
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
# Numbers can be combined using mathematical operators
x = 1 + 1
y = 2 * 3
# Variables holding numbers can be used any way numbers can be used
z = y / x
# We can prove that these computations worked out the same
# using comparison operators, specifically == to test for equality:
print('===comparing===')
print('2 == x', 2 == x)
print('6 == y', 6 == y)
print('3 == z', 3 == z)
print() # Just for a blank line in the output
# Two values can only be equal if they have the same type
print("1 == '1'", 1 == '1')
# Other common comparisons include <, <=, >, >=
print('1 < 2', 1 < 2) # True
print('10 >= 10', 10 >= 10) # True
print('10 > 10', 10 > 10) # False
print() # For a blank line in the output
# Strings are compared pseudoalphabetically for greater than / less than
print('"albert" < "bill"', "albert" < "bill") # True
# HOWEVER, in python ALL capital letters come before ANY lowercase letters
print('"B" < "a"', "B" < "a") # True
# There are additional rules for other characters like $, %, ., and so on
# that we're ignoring for now.
# Strings can also be combined with math operators, but they mean different
# things when operating on strings
x = "hello " + "world." # Concatination, x is "hello world."
y = "a" * 4 # Duplication, y = "aaaa"
print()
print(x)
print(y)
# Finally, we can combine the assignment operator and these math operations
# using the following shorthands:
x = 4
x += 3 # x = x + 3
x -= 1 # x = x - 1
x *= 2 # x = x * 2
x /= 4 # x = x / 4
# Micro-Exercise: predict the value of x. Then write a comparison statement
# involving x that evaluates to False. Print the result of that comparison.