🐍 First Steps in Python

5th Grade 💻 Coding

Python is one of the most popular programming languages in the world, and it’s also one of the easiest to start with. You write it with simple English words, and the code looks almost like regular instructions. In this lesson we write a first program, do some math, store information in a list and learn the rules that are important to know.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

A language named after comedians

Python was created by a Dutch programmer named Guido van Rossum, who released the first version in 1991. Despite the name, it wasn’t named after the snake: van Rossum was a fan of a British comedy TV show called “Monty Python’s Flying Circus,” and he picked a short, slightly mysterious name. His goal was a language that’s easy to read — so the code would make sense even to someone who didn’t write it.

Today Python is used in almost every field: building websites, analyzing data in science and medicine, artificial intelligence, simple games, automating boring tasks and much more. Many schools around the world teach it as a first language. To write Python you need software that runs it — you can install it on a computer with a parent’s permission, and there are also websites that run Python right in the browser.

Your first program

Programmers have a tradition: the first program in any language shows “Hello, World!” In Python it’s a single line: print("Hello, World!"). Run it — and the sentence appears on the screen. The text between the quotation marks is called a string, and you can put anything in it.

Python is also a great calculator. print(2 + 3) shows 5. An asterisk means multiply: print(6 * 7) shows 42. A slash means divide: print(10 / 4) shows 2.5. Two slashes divide without the remainder: print(7 // 2) shows 3, and the percent sign gives the remainder: print(7 % 2) shows 1. Two asterisks mean “to the power of”: print(2 ** 10) shows 1024. Notice that inside print, numbers are written without quotation marks.

Variables, input and lists

To store information you create a variable: age = 11 stores the number 11 in the variable age. You can ask the user for input: name = input("What is your name? ") and then print("Hello " + name). And if you want to store lots of values together, you use a list, which is written with square brackets: fruits = ["apple", "banana", "grape"].

Every item in a list sits in a numbered spot called an index, and counting starts at 0. So print(fruits[0]) shows “apple,” and fruits[2] is “grape.” len(fruits) gives back the number of items — 3. To add an item you write fruits.append("mango"), and now the list has four items. And you can go through the whole list with a loop: for f in fruits: print(f) shows each fruit on its own line.

Important rules

Python’s most unusual rule is indentation — spaces at the start of a line. After a line that ends with a colon, like if, for or def, the lines inside the block must start with some spaces, usually four. Other languages use curly braces to mark a block; Python uses spaces. If the indentation isn’t consistent, Python shows an error message. It’s annoying at first, but it makes everyone write neat code.

More rules: Python cares about uppercase and lowercase letters, so Print won’t work — only print. Parentheses and quotation marks always come in pairs. And a line that starts with a hash sign (#) is a comment — the computer skips it, because it’s meant for people: # here we calculate the score. Good programmers write comments that explain why the code does something, so that a month from now they’ll still understand it.

Key facts

  • Python was created by Guido van Rossum of the Netherlands, and the first version was released in 1991.
  • The name Python comes from the comedy show “Monty Python’s Flying Circus,” not from the snake.
  • In Python, / is division, // is division without the remainder, % is the remainder and ** is “to the power of.”
  • In a Python list, the first item is at index 0.
  • In Python, indentation — spaces at the start of a line — shows which lines belong to a block.

Quick quiz

  1. Who created Python?

  2. What was Python named after?

  3. What does print(7 // 2) show?

  4. What does print(7 % 2) show?

  5. If fruits = ["apple", "banana", "grape"], what is fruits[0]?

  6. What does len(fruits) do?

  7. What does a line that starts with # mean?

  8. Why is Python so strict about indentation?

💭 Think and talk about it

  1. In your head, write a Python program that asks for someone’s name and age and shows how many years are left until they turn 18. What do you need to remember about input?
  2. Why do you think Python’s creator wanted the code to be easy to read?
  3. Make a list of your five favorite foods. What is the index of the last one?

Questions kids ask

Is Python good for kids?

Yes, especially from about age 10–12 and up, after getting to know the ideas of programming (for example, in Scratch). It’s written with simple words, and there are lots of free learning materials. Only install software with a grown-up’s permission.

How do you run Python?

Install Python from its official website (python.org) or use a code editor in the browser. Write your code in a file whose name ends in .py and run it. The code on this page is just for reading — it doesn’t run here.

What’s the difference between Python and Scratch?

In Scratch you drag blocks, and in Python you type code. The ideas are the same — sequence, conditions, loops, variables and functions — but Python lets you build bigger, more complex things, and professional programmers use it too.

🔎 Ask Thinkapedia another question