Skip to content

Commit fca2c2f

Browse files
committed
Chapter 3
1 parent 2c110c6 commit fca2c2f

42 files changed

Lines changed: 316 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
foo = 10
2+
foo += 10 # value is now 20 (10 + 10)
3+
foo -= 5 # value is now 15 (20 - 5)
4+
foo *= 16 # value is now 240 (15 * 16)
5+
foo //= 5 # value is now 48 (240 // 5)
6+
foo /= 4 # value is now 144.0 (48 / 4)
7+
foo **= 2 # value is now 144.0 (12.0 ** 2)
8+
foo %= 51 # value is now 42.0 (144.0 % 15)

Ch3/bitwise_operations.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
print(9 & 8) # bitwise AND, yields 8
2+
print(9 | 8) # bitwise OR, yields 9
3+
print(9 ^ 8) # bitwise XOR, yields 1
4+
print(~8) # unary bitwise ones complement (flip), yields -9
5+
print(1 << 3) # bitwise left shift, yields 8
6+
print(8 >> 3) # bitwise right shift, yields 1

Ch3/boolean_identity_operators.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
spam = True
2+
eggs = False
3+
potatoes = None
4+
5+
if spam is True: # Evaluates to True
6+
print("We have spam.")
7+
8+
if spam is not False: # Evaluates to True
9+
print("I DON'T LIKE SPAM!")
10+
11+
if spam: # Implicitly evaluates to True (preferred)
12+
print("Spam, spam, spam, spam...")
13+
14+
15+
if eggs is False: # Evaluates to True
16+
print("We're all out of eggs.")
17+
18+
if eggs is not True: # Evaluates to True
19+
print("No eggs, but we have spam, spam, spam, spam...")
20+
21+
if not eggs: # Implicitly evaluates to True (preferred)
22+
print("Would you like spam instead?")
23+
24+
25+
if potatoes is not None: # Evaluates to False (preferred)
26+
print("Yum") # We never reach this...potatoes is None!
27+
28+
if potatoes is None: # Evaluates to True (preferred)
29+
print("Yes, we have no potatoes.")
30+
31+
if eggs is spam: # Evaluates to False (CAUTION!!!)
32+
print("This won't work.")

Ch3/cheese_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cheeses = ["Red Leicester", "Tilsit", "Caerphilly", "Bel Paese"]
2+
print(cheeses[1]) # prints "Tilsit"
3+
cheeses[1] = "Cheddar"
4+
print(cheeses[1]) # prints "Cheddar"

Ch3/cheese_shop.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
in_stock = 0
2+
# print("This cheese shop has " + str(in_stock) + " types of cheese.")
3+
print(f"This cheese shop has {in_stock} types of cheese.")

Ch3/comments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This is a comment
2+
print("Hello, world!")
3+
print("How are you?") # This is an inline comment
4+
# Here's another comment
5+
# And another
6+
# And...you get the idea

Ch3/comparison_operators.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
score =98
2+
high_score = 100
3+
4+
print(score == high_score) # equals, evaluates to False
5+
print(score != high_score) # not equals, evalutes to True
6+
print(score < high_score) # less than, evaluates to True
7+
print(score <= high_score) # less than or equals, evalutes to True
8+
print(score > high_score) # greater than, evaluates to False
9+
print(score >= high_score) # greater than or equals, evaluates to False

Ch3/concat_strings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
greeting = "Hello"
2+
name = "Jason"
3+
# message = greeting + ", " + name + "!" # value is "Hello, Jason!"
4+
message = "".join((greeting, ", ", name, "!")) # value is "Hello, Jason!"
5+
print(message)

Ch3/conditional_greet.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
command = "greet"
2+
3+
if command == "greet":
4+
print("Hello!")
5+
elif command == "exit":
6+
print("Goodbye")
7+
else:
8+
print("I don't understand.")

Ch3/docstrings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def make_tea():
2+
"""Will produce a concoction almost,
3+
but not entirely unlike tea."""
4+
# ... function logic ...
5+
6+
#print(make_tea.__doc__)
7+
help(make_tea)

0 commit comments

Comments
 (0)