Python Coding for Kids — Setting Up For the Adventure

Python Coding for Kids — Setting Up For the Adventure

The beginning of my 9-year-old son’s journey in the programming world can also be the beginning of yours.

Little Vin at his PC

The Background

The coronavirus outbreak recently forced most of us to implement social distance, in order to flatten the infection curve and prevent the current pandemic from worsening.

My 9-year-old son — Little Vin — had all his school classes canceled indefinitely, and I thought it would be a great opportunity to finally teach him some basic programming concepts.

As a software developer with more than 10 years of experience, I believe it would be an interesting journey for me too, to help him take his first steps in the fascinating world of coding. And why not share my strategy with other kids and https://telegra.ph/10-reasons-to-teach-your-child-programming-05-31 parents?

This is the first in a series of articles that will be published in the coming days, as we progress. In this article we will focus on the reasons for teaching python https://64774b2d3e359.site123.me/, the initial setup needed to follow the complete series and two examples of simple programs.

Why Should Kids Learn to Code?

Coding Kid Cartoon. Source: www.clipartkey.com

Steve Jobs, Apple founder, once said,

"I think everybody in this country should learn how to program a computer. Should learn a computer language, because it teaches you how to think."

And I couldn't agree more. I would just extend the quote to “everybody in this world”.

Programming fosters creativity, reasoning, and problem-solving. The programmer gets the opportunity to create something from nothing, use logic to turn programming constructs into a form that a computer can run, and, when things don’t work quite as well as expected, use problem solving to figure out what has gone wrong.

Programming is a fun, sometimes challenging — and eventually frustrating — activity, and the skills learned from it can be useful both in school and at work, even if your kid's future career has nothing to do with computers.

Why Python?

Source: UI Here

The first contact with a programming language should be as simple as possible. And Python is an easy-to-learn programming language that has some really useful features for a beginning programmer. The code is quite easy to read when compared to other programming languages, and it has an interactive shell into which you can enter your programs and see them run.

In addition to its simple language structure and an interactive shell with which to experiment, Python has some features that greatly augment the learning process and allow you to put together simple animations for creating your own games.

For example, with one line of code, it possible to create a “Hello World” program and in two lines it is possible to create a code in which the computer requests the user’s name and returns it with a length — or any other simple operation with the user’s name string. If that same code is written in Java, C or Pascal, you must use eight or nine lines of code.

Another good reason is that Kids Won’t Outgrow Python. Scientists use Python to work with large data sets. Software engineers build neural networks and other forms of artificial intelligence with this versatile language.

Also most popular kid's games, like Minecraft and Roblox, provide a good Python API that allows more advanced users to extend the game’s capabilities and resources, offering new game experiences, which, in my particular case, has increased curiosity and desire of my son learning Python.

What’s the Best Way for Kids to Learn Python?

Source: Ultimate Coders

Like everything you try for the first time, it is always best to start with the basics; therefore, start with the first articles of this series that cover the basics and resist the urge to skip to later and more advanced articles.

No one can play an orchestral symphony the first time they pick up an instrument. Student pilots do not start flying an airplane until they understand the basic controls. If you move quickly, not only will the basic ideas not enter your mind, you will also find the content of later articles more complicated than it really is. The better you understand the basics, the easier it will be to understand the more complicated ideas later. No rush at all, take your time.

Also, I just want to point out that I spent some time trying to figure out a good approach on how to start teaching my child, bought some books on the subject, watched some videos and read some other articles, before creating this strategy.

Who Should Follow This Series

It is intended for anyone interested in computer programming, whether that’s a child (+8 years old) or an adult coming to programming for the first time. If you want to learn how to write your own software rather than just use the programs developed by others, this is a great place to start.

In the rest of this article, you will find information to help you install Python, start the Python shell and code, and run your first programs.

Although I recommend installing Python on your local machine and try all the demos, to improve your personal experience, I also provide an embedded Python Interpreter that allows you to run all the source code contained in the articles directly on the browser, without needing to install anything.

What is a Program?

Source: CS Unplugged

program is a set of instructions that makes your computer do a specific task or tasks. Imagine a stopwatch app on a mobile phone. The stopwatch program has instructions that tell it what to do when you press start and stop. It also has instructions that display the time on the screen as it’s being counted. Someone programmed that stopwatch to work.

Millions of programs are used every day all around the world. All apps on your phone are programs, traffic lights are controlled by programs, robots are controlled by programs, all computer games are programs, and there is a huge list of examples not mentioned.

The instructions followed by the program is often called Algorithms. An algorithm is basically a process or set of rules to be followed in problem-solving operations, it is similar to a step-by-step recipe that people follow when cooking. The computer only does what is in the steps, no less, no more.

An algorithm logic usually is independent of the language, it can be described in English and sometimes by math, but its implementation must be written on a specific programming language.

programming language is a vocabulary and set of grammatical rules that resembles English, for instructing a computer or computing device to perform specific tasks. There are several languages out there like BASIC, C, C++, COBOL, Java, Pascal, Python and several others. In our case we are going to learn Python. A program is executed from top to bottom, line by line at a time.

The computer does not understand the programming language directly, it is a language created for us to communicate with it. So a program called compiler or interpreter — depending on the type of language we are using — convert the code you write, often called source code, to a representation that the machines understand, that is called machine code — a sequence of 1’s and 0’s that the computer understands. For example, letter A is 01000001 in machine code, but you don't have to worry with it, Python interpreter will do it for us.

Installing Python

Python Logo. Source: CLEAN PGN

Installing Python is fairly straightforward. Follow the steps for installing it on Windows, Mac OS X, or Ubuntu.

If you wish to use only the embedded Python interpreter, just jump to Your First Program section.

When installing Python, you’ll also set up a shortcut for the IDLE program, which is the Integrated DeveLopment Environment that lets you write programs for Python.

Just follow the instructions below, depending on your OS, and skip the others.

Installing Python on Windows

  1. Go to http://www.python.org/downloads/.
  2. Click the button labeled Download Python 3.8.2. (This is the latest version of Python 3 at the time of this writing, but you might see a later version. Install the most recent version.)
  3. Python will begin to download. If you’re asked to choose between saving or opening the file, select Save File.
  4. When the installer has downloaded, click it. If a dialog pops up asking whether you want to run the file, click Run.
  5. When the installer opens, select the Add Python 3.8 to Path checkbox, as shown at the bottom of the following picture. Then click Install Now.

Windows 10 Python installer Pop-up

Installing Python on Mac OS X

If you’re using a Mac, you should find a version of Python preinstalled, but it’s probably an older version of the language Python 2, which was discontinued in January 2020.

We need Python 3 to follow this and all the following articles. To install it follow the steps:

  1. Go to http://www.python.org/downloads/.
  2. Click the button labeled Download Python 3.8.2. (This is the latest version of Python 3 at the time of this writing, but you might see a later version. Install the most recent version.)
  3. When the installer has downloaded, click it. it should have a .pkg format (the filename is something similar to python-3.8.2-macosx10.9.pkg ).
  4. When the installer opens a similar popup like the image below will be shown. Just keep selecting "Continue" to proceed (at some point just the “Accept” button to accept the program license to continue). Just follow the installer instructions, it's quite straightforward.

OS X Python installer Pop-up

Installing Python on Ubuntu

This step is advanced for children, if you don’t know how to do it, ask an adult to help you.

Open the Terminal app and run

$ sudo apt-get update

$ sudo apt-get install python3 idle3

If you are using other Linux distribution, chances are you already have Python 3 pre-installed as well. If not, use your distribution’s package manager. For example on Fedora, you would use dnf:

$ sudo dnf install python3 idle3

Coding Exercise: Hello World

Hello World Robot. Author: Auksaporn Rattanamuk

Interactive Shell

If you chose to install Python on your local machine, follow this step:

  • If you are Windows you should now have an icon on your Desktop with the label IDLE, open it.
  • If you are using Mac OS X, go to Launchpad and search for an app called IDLE, open it.
  • If you’re using Ubuntu, in the Applications menu, you should see a new group named Programming with the application IDLE, open it.

Windows 10 Python Shell. MacOS X and Linux versions are identical, except for musecollectors.org the window bar.

This is the Python shell, which is part of Python’s integrated development environment (IDLE). The three greater-than signs — >>> — are called the prompt. It allows you to run python code, line by line.

Let’s enter some commands at the prompt. Make sure to include the phrase around the quotes (‘ ‘), it can also be double quotes (“ “). Press enter on your keyboard when you’re finished typing the line. If you’ve entered the command correctly, you should see something like this:

>>> print('Hello World')

Hello World

>>>

The prompt should reappear to let you know that the Python shell is ready to accept more commands.

Congratulations! You’ve just created your first Python program!

Using the Embedded Python Interpreter

If you didn't install the python environment, or even if you do, you can run the program using the embedded Python interpreter below, just hit the Play button at top of it.

If everything worked you should now see "Hello World" displayed on the output console shell.

Congratulations!

Feel free to change the sentence surrounded by the quotation marks hit play again to see what happens.

What exactly did we do?

The word print is a type of Python command called a function, and it prints out whatever is inside the parentheses to the screen.

In essence, you have given the computer an instruction to display the words “Hello World”. The instruction is converted by the Python interpreter to the language the machine understands, machine code and then it is displayed on the console.

In Python every textual information surrounded by either single quotation marks, or double quotation marks, is called a string. Strings are important Python's built-in types that we will cover in detail in later articles.

To the Python interpreter, “Hello World” and ‘Hello World’ are the same thing. The first and last quotation marks are ignored and used only to define the string.

So in this exercise, we pass to the function print the string “Hello World”.

And what is a function?

function in programming is a set of code encapsulated that can receive information, perform an operation and return new information. You don't need to know anything about the internal function code or logic to use it, you just need to know what you can pass to the function and what it returns.

Illustration of a Function. Source: Olin College of Engineering

You put something in, you get something out — It’s pretty basic like a machine, as the illustration above suggests, you put something IN, your function does something to it, and you get something OUT.

Python provides a set of functions ready to be used, called built-in functions. They are part of the programming language, like the print function — and you will learn more about others later.

From now on, every time I refer to a function, I will append parenthesis () at the end of the function name, just denote it. For example, print function will be referenced asprint(). Whenever you read print(), read it as “print function”.

And do not worry, the details of functions and how you can create your own functions will be covered in later articles.

Saving Your Python Program

The Python shell is amazing to test small python code or snippets, but programs wouldn’t be very useful if you needed to rewrite them every time you wanted to use them.

Luckily, we can save our programs for future use. To save a new program, open IDLE and choose File ▸ New Window.

An empty window will appear, with *untitled* in the menu bar. Enter the following code into the new window:

Now, choose File ▸ Save. When prompted for a filename, enter hello.py, and save the file to your Desktop area.

Then choose Run ▸ Run Module.

Your saved program should run, like this:

Congratulations, you saved your first Python program!

Our next program will be a little more interactive than just display a message.

Coding Exercise: Greeting User

Source: Nora Kramer Designs Pinterest

Now that you ran your first program let's try to add more interactivity by getting some input directly from the user.

To receive information through the keyboard, Python uses a built-in function called input().

This function has an optional parameter, called prompt, which is basically a string that will be displayed on the shell output whenever the function is called — similar to what print() does.

When input() function is called, the program flow stops until the user enters the input via the command-line shell. To actually enter the data, the user needs to press the ENTER key after inputting their string.

Then the input() returns the typed string as the output. But it did not print anything, the information is returned as a Python string, within the program.

In order to use this string we need to save it somehow, otherwise, the information will be lost when Python executes the next line of code. For that we will use a labeled container named variable, to store this value returned by input to use later. The variable allows us to retrieve later this value by referencing to its label name.

You will learn more about variables in the future lessons, right now just stick in mind that is one of the important resources provided by all programming languages, not only Python.

Check the program below and hit the Play button on the top. You will see the shell displaying the prompt string and waiting for your input. After that insert your name on the console below and hit enter.

Check the program below and press the Play button at the top. You will see the shell displaying the prompt string and waiting for your input. After that, enter your name after the question and press Enter.

If everything worked well, you will see a message "Hello (YOUR NAME)". In my case, I see “Hello Vin”.

Remember, programs always execute from top to down, line by line.

So what is happening here is:

  • The first line is being executed first.
  • When executing the first line, the right side of the equality is being executed first. It executes the input() passing a string 'What is your name? ' which is displayed on the console, and the program flow stops until the user enters the input via the command-line shell.
  • After that, the program execution continues, the value returned by input() is then saved in a variable called name, defined on the right side of the equalityThe name variable now holds the string entered by the user. Note: The variable’s name is always on the left of the equal sign, and the value you want to store is always on the right.
  • So the next line is executed, calling print() and passing it the string 'Hello' and the string variable name, separated by commas. Then print() output the 'Hello' and the value that the variable name holds, side by side, separated by a space, in the shell output.

That’s it, congratulations, you were able to greet the user by name!

Feel free to change the input prompt string and the printed message, if you want, as an exercise, and press the play button again.

Extending the example — Counting letters (Optional)

Let’s make the previous example a little more fun!

In Python, there is another built-in function called len().

len()takes a string as an input argument and returns the number of characters in that string — — including white spaces.

We will use it to count the number of letters in the given username.

Just click the play button and type your name after the question and press Enter, as you did in the previous example.

You should now be seeing something similar to the figure below

The result printed by the program when “Vinícius” is the input string.

So, what’s happening in this new example is:

  • I added a new line, below the first, which calls len() passing to it the value stored in the variable name — the string with the user’s input.
  • Then len()internally calculates the number of characters that the string stored in the name variable has and returns that value in the output. This output is now a numeric value and is stored in a new variable called name_length.
  • I also added a new line at the bottom of the program. It calls an extra print()and passes the string ‘Your name has’, the variable name_length and another string ‘letters.’, All 3 separated by commas —print() can take several arguments, separated by commas, and display on the console a composite string, adding a white space between them.

Pretty cool huh ?!

What's next?

In the last exercise you were able to get an input string, saving it to a variable and printing it to the user as a greeting message.

In the next article, Playing with Numbers and Variables, I will cover the details of the variables and the Python primitives data types, followed by practical exercises.

I hope you enjoy the learning adventure so far!

See you soon!

Leave a Reply

Your email address will not be published. Required fields are marked *