Skip to content

Instantly share code, notes, and snippets.

@beshad
Last active January 17, 2020 02:25
Show Gist options
  • Select an option

  • Save beshad/854226d6582a2582d4e786e5177d559b to your computer and use it in GitHub Desktop.

Select an option

Save beshad/854226d6582a2582d4e786e5177d559b to your computer and use it in GitHub Desktop.
Coding for kids Python - Lesson 1
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Python Coding for beginners - Lesson 1 ",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/beshad/854226d6582a2582d4e786e5177d559b/coding-for-kids-python-lesson-1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nN9JytU-9ZiH",
"colab_type": "text"
},
"source": [
"# Print(\"Hello\")\n",
"\n",
"One of the most used lines of code in Python is the Print() function. We use it everywhere. \n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4I_1wbQ1-oFk",
"colab_type": "code",
"outputId": "8a76cd3c-75b4-45fd-984c-ea08d801165e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(\"Hi Python!\")\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Hi Python!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1IKUBACe-yC4",
"colab_type": "text"
},
"source": [
"At its core, the **print()** function is used when we want to output a string. A string is a collection of characters, or what we know as text. Strings are a type. Just like it sounds, a type is a way for the computer to undestand what kind of input we're giving it. There are other types of inputes like intergers, Booleans, and lists but don;t worry about them yet.\n",
"\n",
"The **print()** function takes a few parameters, which are pieces of information (input) you give a function to do something with. For now, we'll only use one parameter, which is the part you put inside the double quotes. The **print()** function will take this piece and print it out to the console window. \n",
"\n",
"Print your name on the screen."
]
},
{
"cell_type": "code",
"metadata": {
"id": "XRKhDUaZAe__",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZekQbr82Asrg",
"colab_type": "text"
},
"source": [
"Comments are pieces of code that do not get translated by the computer. You use them as helpful messages you leave for yourself within your code. You can create a comment by outting a hash character (**#**) before the line you would like the comouter to ignore. It's also called *commenting out* a line."
]
},
{
"cell_type": "code",
"metadata": {
"id": "zWdnihsNBnE2",
"colab_type": "code",
"outputId": "ee40e114-767c-4c3d-ff2b-4e322865f1c0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"#print(\"I should not be printed\")\n",
"print(\"I should be printed\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"I should be printed\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pfyGrmwLBxzx",
"colab_type": "text"
},
"source": [
"There are also times where you just need comments to help you remember or understand what your code is doing. These types of comments will become useful when you start writing longer programs."
]
},
{
"cell_type": "code",
"metadata": {
"id": "N_2psWsICFe1",
"colab_type": "code",
"outputId": "079498aa-22ab-4e7a-8ff1-26a82531f872",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# This code prints out text to the shell\n",
"print(\"Hello there!\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Hello there!\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "V-KHUGvECQk6",
"colab_type": "text"
},
"source": [
"In code, there are special characters called escape characters that allow us to give the computer a heads-up when we're going to pass some tricky information to it For Python, this character is the backslash (**\\**) character. \n",
"\n",
"To use we simply type a backslash before the tricky character. For example, try printing this to your console window. "
]
},
{
"cell_type": "code",
"metadata": {
"id": "0W9Vr56kFp0e",
"colab_type": "code",
"outputId": "83616fc1-ca63-4fab-fa61-63c67f0a885c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(\"\\\"Wellington\\\" is the capital of New Zealand\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"\"Wellington\" is the capital of New Zealand\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VfCK9NyKxIYR",
"colab_type": "text"
},
"source": [
"Now try printing this on the screen:\n",
"\n",
"\"Auckland\" is the largest city of New Zealand."
]
},
{
"cell_type": "code",
"metadata": {
"id": "Yawnc0RQxoJg",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "tj-vh81uF62E",
"colab_type": "text"
},
"source": [
"Another troublemaker that the **print()** function has a hard time with is multiple lines. How can we print the following sentence, exactly as it appears?\n",
"\n",
"Here is\n",
"\n",
"a sentence \n",
"\n",
"on many\n",
"\n",
"different lines.\n",
"\n",
"Well, we have a special escape character that the computer undestands as a new line. in programming, we sometimes call a new line a \"line break\". It's a backslach (\\) and a lowercase letter 'n' put together. It looks like this: \\n."
]
},
{
"cell_type": "code",
"metadata": {
"id": "TY-JWeERHFGp",
"colab_type": "code",
"outputId": "2dfad3ee-6f57-48d6-9693-4566662f442b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
}
},
"source": [
"print(\"Here is \\na sentence \\non many \\ndifferent lines.\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Here is \n",
"a sentence \n",
"on many \n",
"different lines.\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zi4jvmrCHY_N",
"colab_type": "text"
},
"source": [
"Now it's great time to talk about **variables**. A variable is a way to keep track of information. When we code, we use variables to hold pieces of information for us. Variables can hold many kind of information, such as strings, numbers, lists, and more.\n",
"\n",
"So how do we create one? Let's make a variable to keep track of the name of this book's author. We would create a variable like this:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "SzK4RClHHs0g",
"colab_type": "code",
"colab": {}
},
"source": [
"author = \"Adrienne\""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "g5G0ZRhRJvZN",
"colab_type": "text"
},
"source": [
"That's all there is to it! The variable author is now a tag for string \"Adrienne\". Here's what's happening: When we create the variable, we give it a name: **author**.\n",
"\n",
"This helps us remember what the information is about. Next, we type an equal sign (=). This tells the computer that we'r giving the **author** variable some information it should keep. This is called *assignment*, or assigning a variable, in programming. Finally, we type out the information our variable is supposed to keep track of. In this example it's the name of the author \"Adrienne\".\n",
"\n",
"Now let's get your name involved! We'll create another variable called **reader**. Go ahead and assign your name to the variable. For this example, we'll use the name Casey. Then, on the next line use the **print()** function to write your variable to the console. Your final code should look something like this:\n",
"\n",
"**reader = \"Casey\"**\n",
"\n",
"**print(reader)**\n",
"\n",
"Now, try printng your name in the code cell below using the variable \"reader\":"
]
},
{
"cell_type": "code",
"metadata": {
"id": "VBavIRNWOLru",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "UoBNQT-wO7Lz",
"colab_type": "text"
},
"source": [
"Now here's the coolest part about variables: Let's say you share this book with your friend Alex. Obviously, our **reader** variable would now be incorrect. It should be yor friend's name! Go ahead and change the **reader** variable so it's assigned to your friend's name, but change nothing else. with the change, your code should look like this:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ebci5oCmRLfs",
"colab_type": "code",
"colab": {}
},
"source": [
"reader = \"Alex\"\n",
"print(reader)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Whwl9yp6RtLE",
"colab_type": "text"
},
"source": [
"Did your friend's name print out this time? It did! How exactly does the computer do this? When a computer sees a variable, it says, \"Ooh, Human wants me to remember this piece of data. I better make some room in my *register* and store this data. I should also mark where I'm storing this data so I can quickly get it if Human needs it again.\"\n",
"\n",
"The register is basically a place within the computer's central processing unit that holds information. You can think of it as a big, grid-like bookshelf with many different cubbies to place things in. This grid system is a way that the computer marks the location of any data it stores so it can quickly remember where to get it if we need it again.\n",
"\n",
"The variables we just used held strings (text), but like we mentioned earlier, variables can also hold other data types. If we wanted to create a variable to hold our favorite number, how would we do it?"
]
},
{
"cell_type": "code",
"metadata": {
"id": "qdOA9qm3TI6C",
"colab_type": "code",
"colab": {}
},
"source": [
"favorite_number = 3"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "l2U1rjeCT7QH",
"colab_type": "text"
},
"source": [
"We create this in a similar way:\n",
"\n",
"* We give our variable a name: **favorite_number**\n",
"* Then we assign it to a piece of information. In this case it's the number **3**.\n",
"\n",
"Did you notice that we didn't use quotes around our number this time? Just as we use string types to tell the computer that we are giving it text input, we use integer types to tell the computer that we're using whole numbers. In Python, whole numbers are known as **integers**.\n",
"\n",
"Whenever we deal with integers, we just type them out as a plain number, like we're used to seeing. You don't use quotes around them, as that will confuse the computer into thinking you are using a string! To see what I mean, let's use a piece of code from Python called **type()**. This code will tell us the data type of the input we give it. \n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "BHH3K22wZG7S",
"colab_type": "code",
"outputId": "243ef857-3a3f-467b-d30d-2684b7f17df6",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"favorite_number = 3\n",
"type(favorite_number)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"int"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5QS9D9XbZOGL",
"colab_type": "text"
},
"source": [
"**Int** is an abbreviation for integer. Now, let's see what happens if you store your favorite number within quotes:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "_2Qxc3gLZufG",
"colab_type": "code",
"outputId": "45181dc8-5d7c-413e-d0ed-9761e73ac738",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"favorite_number = \"3\"\n",
"type(favorite_number)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"str"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RedEE4sZZ1q5",
"colab_type": "text"
},
"source": [
"What type is it now? A **Str**? Oh no! We've tricked the computer into thinking we were saving a string variable! This is why we don't use quotes when working with integers. So remember: we don't need quotes around integers."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tugT7GiO5F9n",
"colab_type": "text"
},
"source": [
"# Good things to know about variables:\n",
" \n",
"\n",
"**Variables can't start with a number**\n",
"\n",
"When naming a variable, you want to be as descrptive as possible, but also follow the rules of Python. One of these rules is that variable names can't start with a number."
]
},
{
"cell_type": "code",
"metadata": {
"id": "3Ze79ecJcMeQ",
"colab_type": "code",
"outputId": "fb2593af-fb62-493c-c46c-294d32157345",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 130
}
},
"source": [
"100_days_of_code = 100"
],
"execution_count": 0,
"outputs": [
{
"output_type": "error",
"ename": "SyntaxError",
"evalue": "ignored",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-9-6f9c79a16496>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 100_days_of_code = 100\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid token\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NGbc_W7EcRvz",
"colab_type": "text"
},
"source": [
"Did you get a syntax error? This is because when the computer starts translating, it immediately sees the number and assumes the rest of the code will be a number. So, when it finds that there's more to it and you are actually creating a variable, it gets really confused."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "i4vHYpO26i7A",
"colab_type": "text"
},
"source": [
"**Variables should have the same styling**\n",
"\n",
"There are all sort of ways to write your variables. The most important thing to remember is to pick one way and stick to it. For example:\n",
"\n",
"* favorite_number\n",
"* favoriteNumber\n",
"* FavoriteNumber\n",
"* ...\n",
"\n",
"As you have seen so far, I write my variables using all lowercase letters, and if I need to use more than one word to name one, I seperate the words using an underscore so it's easier to read. There is no naming method that is the best. Just choose the one that makes the most sense to you and stick to it. Why is this important? The computer doesn't recognise variable names unless they are exactly as you typed them. Sp if you suddenly write **FavoriteNumber** instead of **favorite_number**, you will get an error message, as the computer sees this code as two different variable!"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9O6QOEHuAbxt",
"colab_type": "text"
},
"source": [
"**Variables should have meaning**\n",
"\n",
"Lastly, variable names should be as descriptive as possible. This means that when you read your code, you should know right away what your variable is and what kind of data is storing. You should be able to understand it!"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0PTqPlTgDV5Y",
"colab_type": "text"
},
"source": [
"# Formatted string literals\n",
"\n",
"In Python, we can use formatted string literals, or **f-strings**, to produce formatted strings, which are like normal strings, but set up in a specific way or pattern. Using f-strings gives us an easy way to replace parts of a string or change their order. To do this, we first escape our entire string with the letter **f** and then use special characters known as **braces**, which look like this: **{ }**, to do the replacing or reordering. Here is an example:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "QRA1-hYZC4T4",
"colab_type": "code",
"outputId": "97341847-21b2-4e65-ba04-b04870e744eb",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"favorite_number = 3\n",
"print(f\"My favorite number is {favorite_number}.\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"My favorite number is 3.\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0owjr4NfzeXb",
"colab_type": "text"
},
"source": [
"Now, make a variable called age and store your age in that variable. Print your age using f-strings:\n",
"\n",
"I'm {age} years old."
]
},
{
"cell_type": "code",
"metadata": {
"id": "XBlJKlwhz8WR",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment