Python Coding Examples With Tips, Resources & More

Ryan Barone
April 03, 2020

Python is a powerful programming language used to make games, web applications, and more! And not only is Python fun to learn, but Python programming skills are a must-have for concentrations like computer science, game design engineering, and other STEM college majors.

From there, fast-growing, lucrative careers in data science, software engineering, and many more await!

Why is Python good for kids?

When it comes to Python for kids, it's a programming language that uses less punctuation and is easier to read—making it a great option among the best coding classes available.

(Python's creator, Guido van Rossum was an employee at Google, and he named the language after the comedy troupe Monty Python's Flying Circus, not the reptile. Read more about coding vocabulary.)

The pictures to the right show what some other programming languages require just to print "Hello World!"

As you can see, these languages require a lot more than just one print statement (more on print statements below).

Python Examples

To show just how clear the syntax can be, consider these definitions and examples.

Variables

Variables are used to store information, so kids can think of variables like boxes they can store items in.

ExampleAnd in order to remember what was put in the box, they assign a descriptive name, like:

number_of_doughnuts

In this case, kids now have the means to remember how many doughnuts will go in the box once that box is filled, which leads us to...

Initializing Variables
After a variable is given a name, you give it a value. Assigning this value is like filling the box.

Example: if kids had 3 doughnuts, they would set number_of_doughnuts equal to 3, like so:

number_of_doughnuts = 3

Assigning this value of “3” to the variable is called initializing.

Printing Variables
For kids to be able to see the value of their variable when they run their program, they’ll need to print it. (No, it won't send anything to the printer!). There will be more on printing below, but for now, the process would simply look like:

print(number_of_doughnuts)

Strings
Variables can hold data besides just numbers; they can hold words as well. Programmers call word variables “strings.”

Example: So if your kids named one of their boxed doughnuts, what would it be?

doughnut_name = "Doughy"
print(doughnut_name)

As you can see, when creating string variables, their value must be inside quotation marks.

Boolean Variables
You can also use variables to show if something is true or false. This type of data is called a boolean.

Example: So, you’d create a variable in this case in order to indicate whether doughnuts exist.

doughnuts_exist = True
print(doughnuts_exist)

You can also set it to be not true, as would be the case if your kid ate the doughnut and thus the doughnut wasn't there anymore.

It's as simple as change the variable to the following:

doughnuts_exist = False

To put it all together:

Kids would use variables to store different types of data that they can use later in their programs:

string_variable = "value"
number_variable = 3
boolean_variable -= True

Making a print statement

To get the output from the program, kids would use the print statement, which includes a few different procedures.

For example, kids would start by typing the word: print

After that, they'd type an opening parentheses and an opening quotation mark: print("

Then they'd write any text they want displayed: print("What? I can write anything?!

When the text is finished, they put a second quotation mark and a closing parentheses: print("What? I can write anything?!")

Once executed, the output would simply be: What? I can write anything?!

Integers
You can also use print statements to do simple math, for instance:

print(4 + 5)
print(10 - 2)
print(3 + 1 - 4)

What prints to the screen? The answers to the equations (9, 8, etc.).

Conditionals

A conditional statement helps direct the flow of a program. It does this by having some portions of the code only run under specific scenarios.

Specifically, conditional statements evaluate to either true or false, and this value determines if a specific piece of code will run.

Conditionals always use the keywords if, else, and elif (short for "else if").

For example, if your child is making a game and their player has full health or life, what should happen when that player gets hit?

To start, they’d make a variable for life and then set it equal to 3:

life = 3

Then, if the player gets hit, the player will lose life, or a life point. To subtract from life, they'd simply say:

life -= 1

The life variable is then printed:

print(life)

And code is run to see the result!

(If the above is confusing, “life -=1” is just shorthand for life = life -1, which sets life to one less then what it was before. You can write either version, depending on preference.)

Read More: How to Practice Python Coding Online

To go one step further, you can only subtract so much life, right? Meaning, a player’s life doesn’t typically drop below zero. So, a condition would need to be used to prevent that scenario.

An if statement specifically would be used to prevent life from dropping under zero.

if life > 0:
life -= 1

Here, if statements will run the code indented beneath them only when the condition after them is true. So, only when life is greater than zero will life -=1 be executed.

Elif Statements
Use an elif statement to do something when the first if statement isn't true, but you want to check for another condition.

elif life == 0:
print("The player ran out of life!")

Note the difference between “=” mentioned earlier and “==” here; life = 3  and life == 3 are very different!

Write  life = 3  when you're assigning a value to the variable life: "Life has the value 3."
Write  life == 3  in conditional statements: "Is life equal to 3?"

Else Statements
Additionally, kids would use an else statement to do something when the if and elif statements aren't true. This statement has no conditions, so it will catch anything that isn't covered by the previous statements.

While Loops

In programming, loops allow you to repeat a block of code a number of times. While loops, then, repeat code until a specific condition is met.

For example, maybe there's a question like the one to the right—how many leaves does the tree have?

Well, how many tries will it take to guess?

1 guess? 2 guesses? 4 guesses? 100 guesses?

For times when a block of code needs to run an uncertain or non-specific amount of times, you use a while loop.

While loops are made of a loop control variable (made first), a conditional statement (the conditions that must be met for the loop to run), and a loop body (the actual code that will run).

For Loops

On the other hand, for loops are used to repeat a block of code if you know how many times it needs to run. For loops help you perform an action on multiple elements, if you know how many there are.

Functions

There are many times where you'd want to perform a series of actions, and instead of writing those statements over and over every time, you can use a function!

Pretty straightforward, right?

Learning with PyCharm

All of this above might be good and dandy, but how and where do kids actually write code like the examples above to create programs?

Interpreters allow computers to break down and understand programming languages like Python, similar to dictionaries.

When a program is run, kids will see a new console window appear at the bottom of PyCharm that houses and displays the output of the program!

Getting started with Python

It’s no secret anymore that coding makes for a good career, being that it’s one of the most in-demand job skills in today’s market.

And to make it an all around great career, coding jobs pay well, and the work of a coder is extremely visible across a number of industries. From cryptography, artificial intelligence, machine learning, and much more, Python has a number of cool and relevant applications you might not have ever really thought of.

(Machine learning refers to programming techniques that use statistics to allow the program to "learn" and improve at its task. Common tasks include image recognition and even playing a game of chess. Check out these AI games for students for more info.)

For those wanting to jump in right now with the help of an iD Certified Instructor, they can do so either one-on-one in an online coding class for kids or with a Python tutor online, specifically in something like machine learning lessons, or even with a small group of other like-minded coders in our online after-school programs.

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! AI summer camps, coding classes for kids, and more!