Skip to content
Edition NigeriaGlobal
Country: Tanzania

You are in the Tanzania edition. Type your country to switch.

Set up your profile

You are learning in the Tanzania edition, with examples in shillings and from Tanzania.

Data skills: SQL and Python basics · Lesson 5 of 7

Python basics: variables, lists, loops and functions

Python is a readable programming language that many analysts use alongside SQL. This lesson covers variables and types, lists and dictionaries, loops and conditions, a small function, and how to read an error message, using the same example sales figures as the SQL lessons.

Beginner9 min read6 steps

This lesson is complete in writing. A video for it is on its way.

What you will learn

  • By the end you can create variables and say which type of value each holds.
  • You can store values in a list and a dictionary and get them back out.
  • You can write a for loop with an if inside it to total and count values.
  • You can write a small function and read a Python error message from the last line up.

Key terms in this lesson

Variable
In programming, a name that refers to a stored value, such as a number or a piece of text, so the value can be used and changed later in the code.
Data type
What kind of value a column holds, such as a whole number, a decimal, text or a date. Numbers stored as text cannot be added or averaged.
Python list
An ordered collection of values in Python, written in square brackets and separated by commas, whose items are reached by position starting from zero.
For loop
A piece of code that repeats the same steps once for each item in a collection, such as adding every number in a list to a running total.
Python dictionary
A Python collection that stores pairs of keys and values in curly brackets, so a value such as a price can be looked up by its key, such as a product code.
Traceback
The error report Python prints when code fails, listing the lines it was running and ending with the type of error and a short message explaining it.

Step 1 of 6

Variables and types

Opening this step…

Step 2 of 6

Lists: many values in order

This step opens when you finish the one above.

Step 3 of 6

Loops and decisions

This step opens when you finish the one above.

Step 4 of 6

Dictionaries: look-ups by key

This step opens when you finish the one above.

Step 5 of 6

A small function

This step opens when you finish the one above.

Step 6 of 6

Reading an error message

This step opens when you finish the one above.

Key takeaways

The key takeaways open when you finish every step.

Lesson quiz

6 questions. You need 85% to pass.

The quiz opens when you finish every step.

Stockertim Learn is education. It explains how investing works and never recommends buying or selling anything.

Up next

Skills for the AI agepandas basics and a first chartRead nowpandas basics and a first chartSkills for the AI age · Beginner · 15 minSkills for the AI ageAI as a coding partner: read, te…Read nowAI as a coding partner: read, test and fix its codeSkills for the AI age · Beginner · 15 minSkills for the AI ageSpreadsheet basics: cells, range…Read nowSpreadsheet basics: cells, ranges and formulasSkills for the AI age · Beginner · 15 minSkills for the AI ageIF, SUMIFS and COUNTIFSRead nowIF, SUMIFS and COUNTIFSSkills for the AI age · Beginner · 15 minFinancial data & analyticsLab 2: computing returns in codeRead nowLab 2: computing returns in codeFinancial data & analytics · Advanced · 15 minAI at workCoding help for non-developers a…Read nowCoding help for non-developers and developersAI at work · Beginner · 15 minFinancial data & analyticsLab 1: reading CSV files in Pyth…Read nowLab 1: reading CSV files in Python and SQLFinancial data & analytics · Advanced · 15 minSkills for the AI ageSQL basics: SELECT, WHERE and OR…Read nowSQL basics: SELECT, WHERE and ORDER BYSkills for the AI age · Beginner · 15 minSkills for the AI ageSummarising with SQL: GROUP BY, …Read nowSummarising with SQL: GROUP BY, COUNT, SUM and AVGSkills for the AI age · Beginner · 15 minSkills for the AI ageJoining tables: SQL JOIN for beg…Read nowJoining tables: SQL JOIN for beginnersSkills for the AI age · Beginner · 15 minSkills for the AI agedata is organised: tables, rows,…Read nowHow data is organised: tables, rows, columns and keysSkills for the AI age · Beginner · 15 min
English