|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "metadata": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "source": [ |
| 7 | + "# Introduction to Python!\n", |
| 8 | + "In today's lesson, we will be discussing the following questions: What is Python? How do I use it, and more importantly, why should I use it?\n", |
| 9 | + "\n", |
| 10 | + "Python is a high level programming language, meaning that you are dealing more with concepts and ideas rather than manipulating the bits and bytes of a computer. This makes it an easy language for beginners, and when coupled with its intuitive syntax is a great language for people first learning how to code. But fear not, for if you are not a beginner, Python is popularly used in real-life professions, ranging from ML/Data Analysis to mathematics and developing desktop applications.\n", |
| 11 | + "\n", |
| 12 | + "Python is designed to be easy to read and write, which makes it a great language for beginners. It does away with curly braces and semicolons, and instead uses indentation and whitespace to define code blocks. This makes it easier to read and understand, especially for those who are new to programming. For this set of lessons, we will be using Python 3, which is the latest version of the language. Although Python 2 is still in use in some legacy systems, it only receives security patches.\n", |
| 13 | + "\n", |
| 14 | + "## Your First Python Script\n", |
| 15 | + "\n", |
| 16 | + "For now, let's start with the basics. The first \"tool\" under your toolbelt will be printing output to the console. While you will not find a lot of command-line applications for popular use written in Python (except tools for other developers!), printing is still an essential tool for debugging your programs and in the first stages of development. To print to the console, type (or run the following code cell):\n", |
| 17 | + "\n" |
| 18 | + ], |
| 19 | + "id": "5cfc0bb57c4e4fb9" |
| 20 | + }, |
| 21 | + { |
| 22 | + "metadata": { |
| 23 | + "collapsed": true |
| 24 | + }, |
| 25 | + "cell_type": "code", |
| 26 | + "outputs": [], |
| 27 | + "execution_count": null, |
| 28 | + "source": "print(\"Hello, World!\")", |
| 29 | + "id": "initial_id" |
| 30 | + }, |
| 31 | + { |
| 32 | + "metadata": {}, |
| 33 | + "cell_type": "markdown", |
| 34 | + "source": "Wonderful! You have just written your first Python script. Well, I actually wrote it. Let's take the magnifying glass to this code. The `print` function is a built-in function in Python that takes in a string (a sequence of characters) and outputs it to the console. The string is enclosed in quotation marks, which tells Python that it is a string and not a variable or some other type of data. Now, let's try printing something of your choice. Run the following code cell, replacing the text in quotation marks with some text of your choosing.", |
| 35 | + "id": "f97b19198e4677fd" |
| 36 | + }, |
| 37 | + { |
| 38 | + "metadata": {}, |
| 39 | + "cell_type": "code", |
| 40 | + "outputs": [], |
| 41 | + "execution_count": null, |
| 42 | + "source": "print(\"I'm lazy and can't change this small block of text!\")", |
| 43 | + "id": "1f40c56b228dc899" |
| 44 | + }, |
| 45 | + { |
| 46 | + "metadata": {}, |
| 47 | + "cell_type": "markdown", |
| 48 | + "source": [ |
| 49 | + "## Python syntax\n", |
| 50 | + "\n", |
| 51 | + "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:" |
| 52 | + ], |
| 53 | + "id": "b1031d38b70e2065" |
| 54 | + }, |
| 55 | + { |
| 56 | + "metadata": {}, |
| 57 | + "cell_type": "code", |
| 58 | + "outputs": [], |
| 59 | + "execution_count": null, |
| 60 | + "source": [ |
| 61 | + "if 2 + 2 > 3:\n", |
| 62 | + " print(\"1 + 1 is greater than 3\")" |
| 63 | + ], |
| 64 | + "id": "54b20809fb8db9df" |
| 65 | + }, |
| 66 | + { |
| 67 | + "metadata": {}, |
| 68 | + "cell_type": "markdown", |
| 69 | + "source": "This indentation is crucial in Python, as it tells the interpreter which lines of code belong to the `if` statement. In this case, the line `print(\"1 + 1 is greater than 3\")` is indented, which means that it will only be executed if the condition `2 + 2 > 3` is true. If we were to remove the indentation, we would get an error. Try it out by running the following code cell:", |
| 70 | + "id": "bdee50a075aa5335" |
| 71 | + }, |
| 72 | + { |
| 73 | + "metadata": {}, |
| 74 | + "cell_type": "code", |
| 75 | + "outputs": [], |
| 76 | + "execution_count": null, |
| 77 | + "source": [ |
| 78 | + "if 1 + 1 > 3:\n", |
| 79 | + "print(\"1 + 1 is greater than 3\")" |
| 80 | + ], |
| 81 | + "id": "13574443eb9907fa" |
| 82 | + }, |
| 83 | + { |
| 84 | + "metadata": {}, |
| 85 | + "cell_type": "markdown", |
| 86 | + "source": [ |
| 87 | + "Removing the indentation means that a line no longer belongs to the `if` statement, and thus will always be executed. However, since Python expects an indented block after the `if` statement, it raises an `IndentationError`. This is a common mistake for beginners, so be sure to always check your indentation when writing Python code.\n", |
| 88 | + "This following code cell is an example of correct indentation. Run it to see that it works." |
| 89 | + ], |
| 90 | + "id": "408b27d45c29e58a" |
| 91 | + }, |
| 92 | + { |
| 93 | + "metadata": {}, |
| 94 | + "cell_type": "code", |
| 95 | + "outputs": [], |
| 96 | + "execution_count": null, |
| 97 | + "source": [ |
| 98 | + "if 1 + 1 > 3:\n", |
| 99 | + " print(\"1 + 1 is greater than 3\")\n", |
| 100 | + "print(\"This line is not indented, so it will always be executed.\")" |
| 101 | + ], |
| 102 | + "id": "8e23c5c5a5850055" |
| 103 | + }, |
| 104 | + { |
| 105 | + "metadata": {}, |
| 106 | + "cell_type": "markdown", |
| 107 | + "source": [ |
| 108 | + "## Variables in Python\n", |
| 109 | + "\n", |
| 110 | + "Variables are a way to store data in Python. They allow you to give a value a name, which you can then use to refer to that value later. It's also a great way to make your code more readable and maintainable, as you can store user data in variables and use them throughout your code. In Python, you can create a variable by simply assigning a value to it using the `=` operator. For example, you can create a variable called `x` and assign it the value of `5` like this:" |
| 111 | + ], |
| 112 | + "id": "8454f87370a3242d" |
| 113 | + }, |
| 114 | + { |
| 115 | + "metadata": { |
| 116 | + "ExecuteTime": { |
| 117 | + "end_time": "2025-08-12T15:10:22.174124Z", |
| 118 | + "start_time": "2025-08-12T15:10:22.153190Z" |
| 119 | + } |
| 120 | + }, |
| 121 | + "cell_type": "code", |
| 122 | + "source": [ |
| 123 | + "x = 5\n", |
| 124 | + "print(x)\n", |
| 125 | + "pi = 3.1415962\n", |
| 126 | + "print(\"I love \" + str(pi))" |
| 127 | + ], |
| 128 | + "id": "695b335ef1c2d239", |
| 129 | + "outputs": [ |
| 130 | + { |
| 131 | + "name": "stdout", |
| 132 | + "output_type": "stream", |
| 133 | + "text": [ |
| 134 | + "5\n", |
| 135 | + "I love 3.1415962\n" |
| 136 | + ] |
| 137 | + } |
| 138 | + ], |
| 139 | + "execution_count": 2 |
| 140 | + }, |
| 141 | + { |
| 142 | + "metadata": {}, |
| 143 | + "cell_type": "markdown", |
| 144 | + "source": [ |
| 145 | + "As you can see below, I'm naming variables using the `snake_case` convention (haha, because its Python, which is a snake). This means that you use lowercase letters and underscores to separate words in a variable name. For example, `my_variable` is a valid variable name, while `MyVariable` is not. This is just a convention, but it helps make your code more readable and consistent.\n", |
| 146 | + "\n", |
| 147 | + "You can also create variables that store strings, which are sequences of characters. You can store all sorts of things other than strings and numbers, like booleans (true/false), objects, or functions! For now, let's create a variable called `name` and assign it the value of `\"John\"` like this:" |
| 148 | + ], |
| 149 | + "id": "e9120204536ff49f" |
| 150 | + }, |
| 151 | + { |
| 152 | + "metadata": {}, |
| 153 | + "cell_type": "code", |
| 154 | + "outputs": [], |
| 155 | + "execution_count": null, |
| 156 | + "source": [ |
| 157 | + "his_name = \"John\"\n", |
| 158 | + "print(\"His name is \" + his_name)" |
| 159 | + ], |
| 160 | + "id": "386473072062d87b" |
| 161 | + }, |
| 162 | + { |
| 163 | + "metadata": {}, |
| 164 | + "cell_type": "markdown", |
| 165 | + "source": [ |
| 166 | + "Now, try creating your own variables and printing them out. How about you make a few variables that store your `name`, `age`, and `favorite_icecream_flavor`? Then print out a sentence that includes them all!\n", |
| 167 | + "###### _A little tip: Just remember to use the `+` operator to concatenate strings, and use `str()` to convert non-string variables to strings when necessary. In Python, you cannot add a string and a number together, because they are different data types. If you try to do that, you will get a `TypeError`._\n" |
| 168 | + ], |
| 169 | + "id": "37cd9ed0e9ca1cc3" |
| 170 | + }, |
| 171 | + { |
| 172 | + "metadata": {}, |
| 173 | + "cell_type": "markdown", |
| 174 | + "source": "", |
| 175 | + "id": "9e23604b2828ea85" |
| 176 | + }, |
| 177 | + { |
| 178 | + "metadata": {}, |
| 179 | + "cell_type": "code", |
| 180 | + "outputs": [], |
| 181 | + "execution_count": null, |
| 182 | + "source": "## Your turn :3", |
| 183 | + "id": "7a37bd8062631e39" |
| 184 | + }, |
| 185 | + { |
| 186 | + "metadata": {}, |
| 187 | + "cell_type": "markdown", |
| 188 | + "source": "### Types in Python", |
| 189 | + "id": "a9646bc085e9b372" |
| 190 | + }, |
| 191 | + { |
| 192 | + "metadata": {}, |
| 193 | + "cell_type": "markdown", |
| 194 | + "source": [ |
| 195 | + "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", |
| 196 | + "- Text Type: `str` (string, a sequence of characters)\n", |
| 197 | + "- Numeric Types: `int`, `float`, `complex` (whole numbers, decimal numbers, and complex numbers)\n", |
| 198 | + "- Sequence Types: `list`, `tuple`, `range` (ordered collections of items)\n", |
| 199 | + " - `list`: mutable (can be changed), ordered collection of items\n", |
| 200 | + " - `tuple`: immutable (cannot be changed), ordered collection of items\n", |
| 201 | + " - `range`: represents a sequence of numbers, often used in loops\n", |
| 202 | + "- Mapping Type: `dict` (key-value pairs)\n", |
| 203 | + "- Set Types: `set`, `frozenset` (unordered collections of unique items)\n", |
| 204 | + " - `set`: mutable (can be changed), unordered collection of unique items\n", |
| 205 | + " - `frozenset`: immutable (cannot be changed), unordered collection of unique items\n", |
| 206 | + "- Boolean Type: `bool` (`True`/`False`)\n", |
| 207 | + "- Binary Types: `bytes`, `bytearray`, `memoryview` (we will not be covering these)\n", |
| 208 | + "- None Type: `NoneType` (`None`, used to represent the absence of a value)\n", |
| 209 | + "\n", |
| 210 | + "If you ever want to check the type of a variable, you can use the `type()` function. For example:" |
| 211 | + ], |
| 212 | + "id": "34cedd8a8575b74b" |
| 213 | + }, |
| 214 | + { |
| 215 | + "metadata": {}, |
| 216 | + "cell_type": "code", |
| 217 | + "outputs": [], |
| 218 | + "execution_count": null, |
| 219 | + "source": [ |
| 220 | + "print(type(5)) # This will print <class 'int'>\n", |
| 221 | + "print(type(\"3.14\")) # This will print <class 'str'>" |
| 222 | + ], |
| 223 | + "id": "84cf565e5b64f5ab" |
| 224 | + }, |
| 225 | + { |
| 226 | + "metadata": {}, |
| 227 | + "cell_type": "markdown", |
| 228 | + "source": [ |
| 229 | + "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", |
| 230 | + "For example of type conversion, you can convert a string to an integer like this:" |
| 231 | + ], |
| 232 | + "id": "e63d1426ca778288" |
| 233 | + }, |
| 234 | + { |
| 235 | + "metadata": { |
| 236 | + "ExecuteTime": { |
| 237 | + "end_time": "2025-08-14T16:46:31.590578Z", |
| 238 | + "start_time": "2025-08-14T16:46:31.586635Z" |
| 239 | + } |
| 240 | + }, |
| 241 | + "cell_type": "code", |
| 242 | + "source": [ |
| 243 | + "print(int(\"5\")) # This will print 5 as an integer\n", |
| 244 | + "print(type(int(\"5\"))) # This will print <class 'type'>, which is the type of the int class itself" |
| 245 | + ], |
| 246 | + "id": "29b4d8bb4505105f", |
| 247 | + "outputs": [ |
| 248 | + { |
| 249 | + "name": "stdout", |
| 250 | + "output_type": "stream", |
| 251 | + "text": [ |
| 252 | + "5\n", |
| 253 | + "<class 'int'>\n" |
| 254 | + ] |
| 255 | + } |
| 256 | + ], |
| 257 | + "execution_count": 4 |
| 258 | + }, |
| 259 | + { |
| 260 | + "metadata": {}, |
| 261 | + "cell_type": "markdown", |
| 262 | + "source": [ |
| 263 | + "## Comments in Python\n", |
| 264 | + "\n", |
| 265 | + "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", |
| 266 | + "In Python, you can create a comment by using the `#` symbol. For example:" |
| 267 | + ], |
| 268 | + "id": "eedd4c11032bb426" |
| 269 | + }, |
| 270 | + { |
| 271 | + "metadata": {}, |
| 272 | + "cell_type": "code", |
| 273 | + "outputs": [], |
| 274 | + "execution_count": null, |
| 275 | + "source": [ |
| 276 | + "# The following code prints out 2\n", |
| 277 | + "print (1 + 1)" |
| 278 | + ], |
| 279 | + "id": "3f44a0cb05c950dd" |
| 280 | + }, |
| 281 | + { |
| 282 | + "metadata": {}, |
| 283 | + "cell_type": "code", |
| 284 | + "outputs": [], |
| 285 | + "execution_count": null, |
| 286 | + "source": [ |
| 287 | + "# try commenting out the line below by adding a `#` at the beginning of the line\n", |
| 288 | + "# print(\"This line is commented out and will not be executed.\")\n", |
| 289 | + "print(\"Comment out this line!\")" |
| 290 | + ], |
| 291 | + "id": "b18a664c7e3bdf06" |
| 292 | + } |
| 293 | + ], |
| 294 | + "metadata": { |
| 295 | + "kernelspec": { |
| 296 | + "display_name": "Python 3", |
| 297 | + "language": "python", |
| 298 | + "name": "python3" |
| 299 | + }, |
| 300 | + "language_info": { |
| 301 | + "codemirror_mode": { |
| 302 | + "name": "ipython", |
| 303 | + "version": 2 |
| 304 | + }, |
| 305 | + "file_extension": ".py", |
| 306 | + "mimetype": "text/x-python", |
| 307 | + "name": "python", |
| 308 | + "nbconvert_exporter": "python", |
| 309 | + "pygments_lexer": "ipython2", |
| 310 | + "version": "2.7.6" |
| 311 | + } |
| 312 | + }, |
| 313 | + "nbformat": 4, |
| 314 | + "nbformat_minor": 5 |
| 315 | +} |
0 commit comments