Skip to content
Module 01 of 1235 min readBeginner

Hello, Python — variables and types

The interactive REPL, basic types, the four operators, and why dynamic typing both helps and hurts.

8%

Listen along

Read “Hello, Python — variables and types” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Distinguish Python's REPL behaviour in interactive use vs script mode
  • 02Identify Python's five most common built-in types (int, float, str, bool, None) and use type() to inspect them
  • 03Understand dynamic typing: variables are names bound to values, not boxes
  • 04Articulate why Python won the data-language race over R, SAS, and Stata

Python is the most-used programming language in data analysis, scientific computing, machine learning, and (increasingly) economics. The reasons are unflashy: it's readable, the standard library is generous, and the third-party ecosystem — pandas, NumPy, statsmodels, matplotlib — covers virtually every analysis task an economist will ever need to do.

The interactive REPL

Python is interactive: you type an expression and the interpreter prints its value. In a script, you must use print() to display output. In a notebook (Jupyter, this practice page), the last expression in a cell is automatically displayed. This dual behaviour is the source of one in three beginner bugs — an expression that prints in a notebook but disappears silently in a script.

python
>>> 2 + 3
5
>>> name = 'Kenya'
>>> name
'Kenya'
>>> print(name)
Kenya

The five built-in types you'll touch in your first hour

  • int — whole numbers, arbitrary precision (Python ints don't overflow)
  • float — IEEE 754 double-precision floating point
  • str — text, immutable, single or double quotes both work
  • bool — True or False, capitalised
  • None — the explicit absence of a value

Variables are names, not boxes

x = 5 does not declare a variable of type int. It binds the name x to the integer object 5. Reassigning x = 'hello' is fine — x is now bound to a string. Python is dynamically typed: types belong to values, not to variables. This flexibility is the language's superpower and its biggest source of bugs (a typo silently creates a new variable). Modern practice uses type hints (covered in module 13) to recover some safety.

Why Python won the data race

R was the academic statistics language. SAS and Stata were the regulated-industry languages. Python won not because it was best at any one thing, but because it was good enough at everything — analysis, web scraping, automation, machine learning — that an analyst could stay in one language for an entire pipeline. The 2010s decade-long compounding of pandas, scikit-learn, TensorFlow, and Jupyter sealed the deal.

Run Python in your browser

The practice environment to the right of this course runs real CPython 3.12 compiled to WebAssembly via Pyodide. Pandas, NumPy, and matplotlib are pre-loaded. Three Kenyan datasets are pre-defined as DataFrames. Every exercise runs against real data.

Exercise

Print the string 'Hello, Kenya' (exact text, including the comma).

Key takeaways

  • Python is interactive — the last bare expression in a cell prints; in script mode you must call print()
  • Dynamic typing is the language's superpower and biggest bug source — modern practice uses type hints to recover safety
  • Five types you'll touch in the first hour: int, float, str, bool, None — and they coerce predictably
  • Pandas + NumPy + statsmodels + Jupyter make Python the default end-to-end analysis language

Further reading

  1. 01

    Python Tutorial (Official)

    Python Software Foundation

  2. 02

    Fluent Python: Clear, Concise, and Effective Programming

    Luciano Ramalho · O'Reilly · 2022

  3. 03

    Learning Python

    Mark Lutz · O'Reilly · 2013The 1,600-page comprehensive reference.

Loading progress…
LeadAfrikPublic Economics Hub