|
58 | 58 | "source": "print(\"I'm lazy and can't change this small block of text!\")", |
59 | 59 | "id": "1f40c56b228dc899" |
60 | 60 | }, |
| 61 | + { |
| 62 | + "metadata": {}, |
| 63 | + "cell_type": "markdown", |
| 64 | + "source": "Now try printing out something of your own choice, like your name, and your favorite ice cream flavor. Print these out on different print statements and see what happens.", |
| 65 | + "id": "948e4f11d356ff99" |
| 66 | + }, |
| 67 | + { |
| 68 | + "metadata": {}, |
| 69 | + "cell_type": "code", |
| 70 | + "outputs": [], |
| 71 | + "execution_count": null, |
| 72 | + "source": "# try printing it out here", |
| 73 | + "id": "f491bccb1ea66088" |
| 74 | + }, |
| 75 | + { |
| 76 | + "metadata": {}, |
| 77 | + "cell_type": "markdown", |
| 78 | + "source": "You're seeing this because Python automatically adds a new line after every print statement. You'll learn how to change this in a later lesson.", |
| 79 | + "id": "21b19b44eb850e8f" |
| 80 | + }, |
61 | 81 | { |
62 | 82 | "metadata": {}, |
63 | 83 | "cell_type": "markdown", |
64 | 84 | "source": [ |
65 | 85 | "## Python Syntax\n", |
66 | 86 | "\n", |
67 | | - "As mentioned earlier, Python relies strongly on indentation and whitespace to define code blocks. For an example, we will be using `if` statements, which we will be discussing in more detail later. An `if` statement is a conditional statement that allows you to execute a block of code only if a certain condition is met. Here is an example of an `if` statement in Python:" |
| 87 | + "As mentioned earlier, Python relies strongly on indentation and whitespace to define code blocks instead of semicolons and curly braces. For an example, we will be using `if` statements, which we will be discussing in more detail later. An `if` statement is a conditional statement that allows you to execute a block of code only if a certain condition is met. Here is an example of an `if` statement in Python:" |
68 | 88 | ], |
69 | 89 | "id": "b1031d38b70e2065" |
70 | 90 | }, |
|
74 | 94 | "outputs": [], |
75 | 95 | "execution_count": null, |
76 | 96 | "source": [ |
77 | | - "if 2 + 2 > 3:\n", |
78 | | - " print(\"1 + 1 is greater than 3\")" |
| 97 | + "if 2 + 2 > 3: # if 2 plus 2 is greater than 3\n", |
| 98 | + " print(\"2 + 2 is greater than 3\")" |
79 | 99 | ], |
80 | 100 | "id": "54b20809fb8db9df" |
81 | 101 | }, |
|
117 | 137 | ], |
118 | 138 | "id": "8e23c5c5a5850055" |
119 | 139 | }, |
| 140 | + { |
| 141 | + "metadata": {}, |
| 142 | + "cell_type": "markdown", |
| 143 | + "source": "Now, write an if statement that checks if 3 + 9 is greater than 7, and if so, print out something. Afterwards, print out another equation. Make sure to indent it correctly!", |
| 144 | + "id": "d89d2f8bff2057b3" |
| 145 | + }, |
120 | 146 | { |
121 | 147 | "metadata": {}, |
122 | 148 | "cell_type": "markdown", |
|
233 | 259 | "metadata": {}, |
234 | 260 | "cell_type": "markdown", |
235 | 261 | "source": [ |
236 | | - "### Variable Scoping\n", |
237 | | - "\n", |
238 | | - "Variables have a thing called \"scope\" which determines where the variable can be accessed in your code. In Python, there are two main types of scope: global and local.\n", |
239 | | - "- **Global variables** are defined outside any function and can be accessed from anywhere in your code. They are created by simply assigning a value to a variable name at the top level of your script. You can also use the `global` keyword inside a function to modify a global variable.\n", |
240 | | - "- **Local variables** are defined inside a function and can only be accessed within that function. They are created by assigning a value to a variable name inside a function.\n", |
241 | | - "\n", |
242 | | - "Sometimes, this can lead to confusion, especially if you have a global variable with the same name as a local variable. In that case, the local variable will take precedence over the global variable within the function. This is called *shadowing* and should be avoided at all costs because it leads to very painful debugging.\n", |
243 | | - "\n", |
244 | | - "The reason why scoping exists is to prevent naming conflicts and to ensure that variables are only accessible where they are needed and make sense to be used.\n", |
| 262 | + "## Comments in Python\n", |
245 | 263 | "\n", |
246 | | - "run the following code blocks to see examples of variable scoping" |
| 264 | + "Comments are like adding notes to your code, just like how my English teacher marks up my essays for grammar mistakes! They are not executed by the interpreter, but they can be very useful for explaining what your code does or for leaving reminders for yourself. You can also \"comment out\" code that you don't want to run.\n", |
| 265 | + "In Python, you can create a comment by using the `#` symbol. For example:" |
247 | 266 | ], |
248 | | - "id": "843ff65ed733de10" |
| 267 | + "id": "eedd4c11032bb426" |
249 | 268 | }, |
250 | 269 | { |
251 | | - "metadata": { |
252 | | - "ExecuteTime": { |
253 | | - "end_time": "2025-08-20T14:13:02.782326Z", |
254 | | - "start_time": "2025-08-20T14:13:02.777681Z" |
255 | | - } |
256 | | - }, |
| 270 | + "metadata": {}, |
257 | 271 | "cell_type": "code", |
| 272 | + "outputs": [], |
| 273 | + "execution_count": null, |
258 | 274 | "source": [ |
259 | | - "x = 10 # Global variable, because it's declared at the highest level (least indentation) of the script\n", |
260 | | - "def function():\n", |
261 | | - " x = 5 # Local variable\n", |
262 | | - " print(\"Inside the function, x =\", x)\n", |
263 | | - "function()\n", |
264 | | - "print(\"Outside the function, x =\", x) # This will print the global variable x" |
265 | | - ], |
266 | | - "id": "ef1341d373acff81", |
267 | | - "outputs": [ |
268 | | - { |
269 | | - "name": "stdout", |
270 | | - "output_type": "stream", |
271 | | - "text": [ |
272 | | - "Inside the function, x = 5\n", |
273 | | - "Outside the function, x = 10\n" |
274 | | - ] |
275 | | - } |
| 275 | + "# The following code prints out 2\n", |
| 276 | + "print (1 + 1)" |
276 | 277 | ], |
277 | | - "execution_count": 1 |
| 278 | + "id": "3f44a0cb05c950dd" |
278 | 279 | }, |
279 | 280 | { |
280 | 281 | "metadata": {}, |
281 | | - "cell_type": "markdown", |
282 | | - "source": "However, if we wanted to modify the global variable `x` inside a function, we would need to use the `global` keyword. This tells Python that we want to use the global variable instead of creating a new local variable with the same name.\n", |
283 | | - "id": "4381133f3eae8cb1" |
284 | | - }, |
285 | | - { |
286 | | - "metadata": { |
287 | | - "ExecuteTime": { |
288 | | - "end_time": "2025-08-20T14:29:37.745033Z", |
289 | | - "start_time": "2025-08-20T14:29:37.738420Z" |
290 | | - } |
291 | | - }, |
292 | 282 | "cell_type": "code", |
| 283 | + "outputs": [], |
| 284 | + "execution_count": null, |
293 | 285 | "source": [ |
294 | | - "n = 3.14\n", |
295 | | - "def modify_global():\n", |
296 | | - " global n # This tells Python that we want to use the global variable n\n", |
297 | | - " n = 2.71 # This modifies the global variable n\n", |
298 | | - " print(\"Inside the function, n =\", n)\n", |
299 | | - "modify_global()\n", |
300 | | - "print(\"Outside the function, n =\", n)" |
301 | | - ], |
302 | | - "id": "c2fbbd0b8bd5b4fb", |
303 | | - "outputs": [ |
304 | | - { |
305 | | - "name": "stdout", |
306 | | - "output_type": "stream", |
307 | | - "text": [ |
308 | | - "Inside the function, n = 2.71\n", |
309 | | - "Outside the function, n = 2.71\n" |
310 | | - ] |
311 | | - } |
| 286 | + "# try commenting out the line below by adding a `#` at the beginning of the line\n", |
| 287 | + "# print(\"This line is commented out and will not be executed.\")\n", |
| 288 | + "print(\"Comment out this line!\")" |
312 | 289 | ], |
313 | | - "execution_count": 2 |
314 | | - }, |
315 | | - { |
316 | | - "metadata": {}, |
317 | | - "cell_type": "markdown", |
318 | | - "source": "### Types in Python", |
319 | | - "id": "a9646bc085e9b372" |
| 290 | + "id": "b18a664c7e3bdf06" |
320 | 291 | }, |
321 | 292 | { |
322 | 293 | "metadata": {}, |
323 | 294 | "cell_type": "markdown", |
324 | 295 | "source": [ |
325 | | - "Here's a list of the different types a variable can be in python. Each type has its own characteristics and uses, so it's important to understand them when writing Python code.\n", |
326 | | - "- Text Type: `str` (string, a sequence of characters)\n", |
327 | | - "- Numeric Types: `int`, `float`, `complex` (whole numbers, decimal numbers, and complex numbers)\n", |
328 | | - "- Sequence Types: `list`, `tuple`, `range` (ordered collections of items)\n", |
329 | | - " - `list`: mutable (can be changed), ordered collection of items\n", |
330 | | - " - `tuple`: immutable (cannot be changed), ordered collection of items\n", |
331 | | - " - `range`: represents a sequence of numbers, often used in loops\n", |
332 | | - "- Mapping Type: `dict` (key-value pairs)\n", |
333 | | - "- Set Types: `set`, `frozenset` (unordered collections of unique items)\n", |
334 | | - " - `set`: mutable (can be changed), unordered collection of unique items\n", |
335 | | - " - `frozenset`: immutable (cannot be changed), unordered collection of unique items\n", |
336 | | - "- Boolean Type: `bool` (`True`/`False`)\n", |
337 | | - "- Binary Types: `bytes`, `bytearray`, `memoryview` (we will not be covering these)\n", |
338 | | - "- None Type: `NoneType` (`None`, used to represent the absence of a value)\n", |
| 296 | + "## Math in Python\n", |
| 297 | + "Python can be used as a calculator, and it supports all the basic arithmetic operations like addition, subtraction, multiplication, and division.\n", |
| 298 | + "Most of the operation are the same as normal math. However, there isn't a division sign on the standard keyboard, so we use one forward slash (`/`) for division, two asterisks for exponentiating (because you're multiplying multiple times).\n", |
| 299 | + "\n", |
339 | 300 | "\n", |
340 | | - "If you ever want to check the type of a variable, you can use the `type()` function. For example:" |
| 301 | + "Here's the list of every operator you'll need to ever use." |
341 | 302 | ], |
342 | | - "id": "34cedd8a8575b74b" |
| 303 | + "id": "7a7153dec817dae3" |
343 | 304 | }, |
344 | 305 | { |
345 | 306 | "metadata": {}, |
346 | 307 | "cell_type": "code", |
347 | 308 | "outputs": [], |
348 | 309 | "execution_count": null, |
349 | 310 | "source": [ |
350 | | - "print(type(5)) # This will print <class 'int'>\n", |
351 | | - "print(type(\"3.14\")) # This will print <class 'str'>" |
352 | | - ], |
353 | | - "id": "84cf565e5b64f5ab" |
354 | | - }, |
355 | | - { |
356 | | - "metadata": {}, |
357 | | - "cell_type": "markdown", |
358 | | - "source": [ |
359 | | - "If you want to change the type of a variable, you can use type conversion functions like `int()`, `float()`, and `str()`. However, not all conversions will work. You cannot turn the sentence `\"Reggie the Regent\"` into a number (although it would probably be 1, because West is the Best).\n", |
360 | | - "For example of type conversion, you can convert a string to an integer like this:" |
361 | | - ], |
362 | | - "id": "e63d1426ca778288" |
363 | | - }, |
364 | | - { |
365 | | - "metadata": { |
366 | | - "ExecuteTime": { |
367 | | - "end_time": "2025-08-14T16:46:31.590578Z", |
368 | | - "start_time": "2025-08-14T16:46:31.586635Z" |
369 | | - } |
370 | | - }, |
371 | | - "cell_type": "code", |
372 | | - "source": [ |
373 | | - "print(int(\"5\")) # This will print 5 as an integer\n", |
374 | | - "print(type(int(\"5\"))) # This will print <class 'type'>, which is the type of the int class itself" |
375 | | - ], |
376 | | - "id": "29b4d8bb4505105f", |
377 | | - "outputs": [ |
378 | | - { |
379 | | - "name": "stdout", |
380 | | - "output_type": "stream", |
381 | | - "text": [ |
382 | | - "5\n", |
383 | | - "<class 'int'>\n" |
384 | | - ] |
385 | | - } |
| 311 | + "print(5 + 3) # Addition\n", |
| 312 | + "print(5 - 3) # Subtraction\n", |
| 313 | + "print(5 * 3) # Multiplication\n", |
| 314 | + "print(5 / 3) # Division\n", |
| 315 | + "print(5 // 3) # Floor Division\n", |
| 316 | + "print(5 % 3) # Modulus\n", |
| 317 | + "print(5 ** 3) # Exponentiation (raising to the power of)\n", |
| 318 | + "print(5 + 3 * 2) # Order of operations" |
386 | 319 | ], |
387 | | - "execution_count": 4 |
| 320 | + "id": "a997cca30aa3c69e" |
388 | 321 | }, |
389 | 322 | { |
390 | 323 | "metadata": {}, |
391 | 324 | "cell_type": "markdown", |
392 | | - "source": [ |
393 | | - "## Comments in Python\n", |
394 | | - "\n", |
395 | | - "Comments are like adding notes to your code, just like how my English teacher marks up my essays for grammar mistakes! They are not executed by the interpreter, but they can be very useful for explaining what your code does or for leaving reminders for yourself. You can also \"comment out\" code that you don't want to run.\n", |
396 | | - "In Python, you can create a comment by using the `#` symbol. For example:" |
397 | | - ], |
398 | | - "id": "eedd4c11032bb426" |
| 325 | + "source": "Let's try out some basic math! Try adding 6 and 7 and printing out the result.", |
| 326 | + "id": "d37d0a73164529a7" |
399 | 327 | }, |
400 | 328 | { |
401 | 329 | "metadata": {}, |
402 | 330 | "cell_type": "code", |
403 | 331 | "outputs": [], |
404 | 332 | "execution_count": null, |
405 | | - "source": [ |
406 | | - "# The following code prints out 2\n", |
407 | | - "print (1 + 1)" |
408 | | - ], |
409 | | - "id": "3f44a0cb05c950dd" |
| 333 | + "source": "# write your code here\n", |
| 334 | + "id": "4590c080d86514cb" |
410 | 335 | }, |
411 | 336 | { |
412 | 337 | "metadata": {}, |
413 | | - "cell_type": "code", |
414 | | - "outputs": [], |
415 | | - "execution_count": null, |
416 | | - "source": [ |
417 | | - "# try commenting out the line below by adding a `#` at the beginning of the line\n", |
418 | | - "# print(\"This line is commented out and will not be executed.\")\n", |
419 | | - "print(\"Comment out this line!\")" |
420 | | - ], |
421 | | - "id": "b18a664c7e3bdf06" |
| 338 | + "cell_type": "markdown", |
| 339 | + "source": "Then, try multiplying 62 and 81 and printing it out.", |
| 340 | + "id": "e1a59fe53e17a797" |
422 | 341 | }, |
423 | 342 | { |
424 | 343 | "metadata": {}, |
425 | 344 | "cell_type": "code", |
426 | 345 | "outputs": [], |
427 | 346 | "execution_count": null, |
428 | | - "source": "", |
429 | | - "id": "f7c25dd39f9be227" |
| 347 | + "source": "# multiply!\n", |
| 348 | + "id": "b8d81989b035fbfc" |
430 | 349 | }, |
431 | 350 | { |
432 | 351 | "metadata": {}, |
433 | 352 | "cell_type": "markdown", |
434 | | - "source": [ |
435 | | - "## Math in Python\n", |
436 | | - "Python can be used as a calculator, and it supports all the basic arithmetic operations like addition, subtraction, multiplication, and division. Here are some examples:" |
437 | | - ], |
438 | | - "id": "7a7153dec817dae3" |
| 353 | + "source": "Now, try adding 2 and 8, multiplying that by 9, and dividing it all by 3. Print that out.", |
| 354 | + "id": "ddd1cdc66e99ab3b" |
439 | 355 | }, |
440 | 356 | { |
441 | 357 | "metadata": {}, |
442 | 358 | "cell_type": "code", |
443 | 359 | "outputs": [], |
444 | 360 | "execution_count": null, |
445 | | - "source": [ |
446 | | - "print(5 + 3) # Addition\n", |
447 | | - "print(5 - 3) # Subtraction\n", |
448 | | - "print(5 * 3) # Multiplication\n", |
449 | | - "print(5 / 3) # Division\n", |
450 | | - "print(5 // 3) # Floor Division\n", |
451 | | - "print(5 % 3) # Modulus\n", |
452 | | - "print(5 ** 3) # Exponentiation\n", |
453 | | - "print(5 + 3 * 2) # Order of operations" |
454 | | - ], |
455 | | - "id": "a997cca30aa3c69e" |
| 361 | + "source": "# here!\n", |
| 362 | + "id": "96d57b3c206187ad" |
456 | 363 | }, |
457 | 364 | { |
458 | 365 | "metadata": {}, |
459 | 366 | "cell_type": "markdown", |
460 | 367 | "source": [ |
| 368 | + "\n", |
| 369 | + "\n", |
| 370 | + "\n", |
461 | 371 | "Python also has shorthand operators that allow you to perform an operation and assign the result to a variable in a single step. For example, instead of writing `x = x + 5`, you can write `x += 5`. This shorthand also works with strings, where applicable.\n", |
462 | 372 | "Here are some examples of shorthand operators:" |
463 | 373 | ], |
|
486 | 396 | "source": [ |
487 | 397 | "## Practice - Ask a user for their info, and format it\n", |
488 | 398 | "\n", |
489 | | - "Let's put all of this together and create a simple \"Registration\" program that asks the user for their name, age, and password, and then prints out a formatted sentence with that information. You can use the `input()` function to get input from the user, and then use string formatting to create the final output. We have little under our toolbelt yet, so let's keep it simple. " |
| 399 | + "Let's put all of this together and create a simple \"Registration\" program that asks the user for their name, age, and date of enrollment, and then prints out a formatted sentence with that information. assign these valuables to variables that you create, and print them all out.\n", |
| 400 | + "\n", |
| 401 | + "At the end, you should have a short code snippet which should neatly print the users name, age, and password. We have little under our toolbelt right now, so let's keep it simple." |
490 | 402 | ], |
491 | 403 | "id": "3230f4039fd83fb7" |
492 | 404 | }, |
| 405 | + { |
| 406 | + "metadata": { |
| 407 | + "ExecuteTime": { |
| 408 | + "end_time": "2025-09-19T01:33:43.976676Z", |
| 409 | + "start_time": "2025-09-19T01:33:40.533750Z" |
| 410 | + } |
| 411 | + }, |
| 412 | + "cell_type": "code", |
| 413 | + "source": "", |
| 414 | + "id": "cbe0fa25649c85c3", |
| 415 | + "outputs": [ |
| 416 | + { |
| 417 | + "name": "stdout", |
| 418 | + "output_type": "stream", |
| 419 | + "text": [ |
| 420 | + "joe\n" |
| 421 | + ] |
| 422 | + } |
| 423 | + ], |
| 424 | + "execution_count": 1 |
| 425 | + }, |
493 | 426 | { |
494 | 427 | "metadata": {}, |
495 | 428 | "cell_type": "markdown", |
|
0 commit comments