Python is known for having syntax that’s simpler and easier to write than many other kids coding languages. Python is a programming language that uses less punctuation and is easier to read, making it a great option among the best coding classes available.
Variables
Variables are used to store information, so kids can think of variables like boxes they can store items in.
Example: And 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.)
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?