-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter3.html
More file actions
211 lines (196 loc) · 8.25 KB
/
chapter3.html
File metadata and controls
211 lines (196 loc) · 8.25 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Chapter 3 of Python Zero to Hero: deep dive into Python operators—arithmetic, comparison, logical, assignment, precedence, identity & membership." />
<meta name="keywords" content="Python, operators, arithmetic, comparison, logical, assignment, precedence, identity, membership" />
<meta name="author" content="Luca Bocaletto" />
<title>Chapter 3 – Operators | Python Zero to Hero</title>
<!-- Bootstrap 5 CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-YSa3P1QY0VSei3nHevwltF1Jxv2pT3z5z0R6x35o1XQdYzdgQc8sYC+bS9v+g3Tc"
crossorigin="anonymous"
/>
<!-- Highlight.js CSS -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css"
integrity="sha512-94jnYVzi0AuOmT1JrLJiqxGWM1ZwVz4i1oF6XD2fXZj2lq+OJ9GSeUWLeFN5svoe0WyWkDi/gAhFI8QeYQj1Rg=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<style>
body { padding-top: 4.5rem; }
pre code { font-size: .9rem; }
.operator-table th,
.operator-table td { vertical-align: middle; }
.btn-py { font-family: monospace; }
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.html">Python Zero to Hero</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navContent" aria-controls="navContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navContent">
<ul class="navbar-nav ms-auto">
<li class="nav-item"><a class="nav-link" href="chapter2.html">Chapter 2</a></li>
<li class="nav-item"><a class="nav-link" href="index.html">Index</a></li>
<li class="nav-item"><a class="nav-link" href="chapter4.html">Chapter 4</a></li>
<li class="nav-item">
<a class="nav-link" href="https://github.com/bocaletto-luca/python-zero-to-hero" target="_blank">
GitHub Repo
</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Main Container -->
<main class="container">
<!-- Header -->
<header class="my-4">
<h1 class="display-6">Chapter 3: Operators</h1>
<p class="text-muted">Master Python’s operators: arithmetic, comparison, logical, assignment, precedence, identity & membership.</p>
<a href="src/chapter3.py" download class="btn btn-outline-primary btn-sm btn-py">
Download <code>chapter3.py</code>
</a>
<hr>
</header>
<!-- Objectives -->
<section id="objectives" class="mb-5">
<h2>Objectives</h2>
<ul>
<li>Perform calculations with arithmetic operators.</li>
<li>Compare values using comparison operators.</li>
<li>Combine Boolean expressions with logical operators.</li>
<li>Use simple and augmented assignment operators.</li>
<li>Understand operator precedence and associativity.</li>
<li>Explore identity (<code>is</code>) and membership (<code>in</code>) operators.</li>
</ul>
</section>
<!-- 1. Arithmetic Operators -->
<section id="arithmetic" class="mb-5">
<h2>1. Arithmetic Operators</h2>
<p>Basic mathematical operations:</p>
<table class="table table-sm table-bordered operator-table">
<thead>
<tr><th>Operator</th><th>Description</th><th>Example</th></tr>
</thead>
<tbody>
<tr><td><code>+</code></td><td>Addition</td><td><code>2 + 3 # 5</code></td></tr>
<tr><td><code>-</code></td><td>Subtraction</td><td><code>5 - 1 # 4</code></td></tr>
<tr><td><code>*</code></td><td>Multiplication</td><td><code>4 * 3 # 12</code></td></tr>
<tr><td><code>/</code></td><td>True division</td><td><code>7 / 2 # 3.5</code></td></tr>
<tr><td><code>//</code></td><td>Floor division</td><td><code>7 // 2 # 3</code></td></tr>
<tr><td><code>%</code></td><td>Modulus (remainder)</td><td><code>7 % 2 # 1</code></td></tr>
<tr><td><code>**</code></td><td>Exponentiation</td><td><code>2 ** 3 # 8</code></td></tr>
</tbody>
</table>
</section>
<!-- 2. Comparison Operators -->
<section id="comparison" class="mb-5">
<h2>2. Comparison Operators</h2>
<p>Evaluate relationships; result is <code>True</code> or <code>False</code>:</p>
<table class="table table-sm table-bordered operator-table">
<thead>
<tr><th>Operator</th><th>Description</th><th>Example</th></tr>
</thead>
<tbody>
<tr><td><code>==</code></td><td>Equal to</td><td><code>3 == 3 # True</code></td></tr>
<tr><td><code>!=</code></td><td>Not equal to</td><td><code>4 != 5 # True</code></td></tr>
<tr><td><code><</code></td><td>Less than</td><td><code>2 < 5 # True</code></td></tr>
<tr><td><code>></code></td><td>Greater than</td><td><code>5 > 2 # True</code></td></tr>
<tr><td><code><=</code></td><td>Less than or equal</td><td><code>3 <= 3 # True</code></td></tr>
<tr><td><code>>=</code></td><td>Greater than or equal</td><td><code>4 >= 2 # True</code></td></tr>
</tbody>
</table>
</section>
<!-- 3. Logical Operators -->
<section id="logical" class="mb-5">
<h2>3. Logical Operators</h2>
<p>Combine Boolean expressions:</p>
<ul>
<li><code>and</code>: <code>True</code> if both operands <code>True</code></li>
<li><code>or</code>: <code>True</code> if at least one operand <code>True</code></li>
<li><code>not</code>: invert a Boolean value</li>
</ul>
<pre><code class="python"># Examples
True and False # False
True or False # True
not True # False
</code></pre>
</section>
<!-- 4. Assignment Operators -->
<section id="assignment" class="mb-5">
<h2>4. Assignment & Augmented Assignment</h2>
<p>Store and update variable values:</p>
<pre><code class="python"># Standard assignment
x = 10
# Augmented assignment
x += 5 # x = x + 5 → 15
x *= 2 # x = x * 2 → 30
x %= 7 # x = x % 7 → 2
</code></pre>
</section>
<!-- 5. Precedence & Other Operators -->
<section id="precedence" class="mb-5">
<h2>5. Precedence & Other Operators</h2>
<p>Order of operations (highest → lowest):</p>
<ol>
<li><code>**</code> (exponentiation)</li>
<li><code>*, /, //, %</code></li>
<li><code>+, -</code></li>
<li><code><, <=, >, >=</code></li>
<li><code>==, !=</code></li>
<li><code>not</code></li>
<li><code>and</code></li>
<li><code>or</code></li>
</ol>
<p>Identity & membership:</p>
<pre><code class="python"># Identity
a = [1,2,3]
b = a
c = a[:]
a is b # True
a is c # False
# Membership
2 in a # True
5 not in a # True
</code></pre>
</section>
<!-- Exercises -->
<section id="exercises" class="mb-5">
<h2>Exercises</h2>
<ol>
<li>Compute <code>(3 + 5) * 2 ** 3 / 4 % 3</code>, print each intermediate step.</li>
<li>Write a Boolean expression to check if <code>n</code> is strictly between 1 and 10.</li>
<li>Demonstrate difference between <code>is</code> and <code>==</code> with two identical lists.</li>
<li>Starting from <code>x = 5</code>, use augmented assignments to reach <code>x = 120</code>.</li>
</ol>
</section>
<!-- Navigation -->
<nav class="d-flex justify-content-between mb-5">
<a href="chapter2.html" class="btn btn-outline-secondary">← Chapter 2</a>
<a href="chapter4.html" class="btn btn-primary">Chapter 4 →</a>
</nav>
</main>
<!-- Footer -->
<footer class="text-center py-4 border-top">
<small>
© 2025 Python Zero to Hero •
<a href="https://github.com/bocaletto-luca/python-zero-to-hero" target="_blank">
bocaletto-luca/python-zero-to-hero
</a>
</small>
</footer>
</body>
</html>