Python Programming Primer

Syntax and Variables


Basic concepts

The Python Programming Primer introduces basic concepts of the Python programming language. The primer briefly introduces some of the key concepts of the language, with the assumption that you have prior experience from some programming language.

Information about the tools and environment needed for the course can be found in the course practicalities section.

Note that you are allowed — and encouraged — to use large language models when working on the exercises.

Syntax

Just like every language has its grammar, Python has its syntax that governs how Python code is written and interpreted. Here are the very basics:

  • Indentation dictates the structure of the code (unlike C, Java, etc, curly brackets are not used). Consistent indentation is required. As an example, the following program would not work.
Run the program to see the output
  • New lines terminate statements (i.e., a semicolon is not needed). Semicolons are used to separate statements put on the same line. The following program does work.
Run the program to see the output
  • Character casing matters. For instance, print is a built-in function, whereas Print is undefined by default. As an example, the following program does not work.
Run the program to see the output
  • Anything after a # is treated as a comment. The following program works (even though it doesn’t do anything).
Run the program to see the output
Loading Exercise...

Variables and assignment

Python has the basic data types that one has become to expect from programming languages. These include integers, floats, strings, and booleans.

There is no explicit declaration of variables beyond the usual variable = value that introduces a variable and assigns a value to it. Declaration happens automatically when you assign a value to a variable — that is, just assign a value and the variable is added to the memory, if it isn’t there already.

The following outlines the different types of variables and how to assign values to them.

Run the program to see the output

Python has the basic arithmetic operators, including +, -, *, /, and %. It also has assignment operators that combine arithmetic operators with assignment, including =, +=, -=, *=, /=, and %=.

Run the program to see the output

The * operator has a special meaning when used with strings: it repeats a string a given number of times. In addition, the + operator concatenates strings, which is a more common feature in programming languages.

The following provides an example with * and + operators and strings.

Run the program to see the output

Data types

Typing in Python is dynamic, meaning you can reassign variables to different data types. The type of a variable can be checked using the type() function.

Run the program to see the output

Duck typing

Python uses a concept called duck typing. This means that the type of an object is determined by what it can do, rather than what it is — “if it walks like a duck and quacks like a duck, then it must be a duck”.


Loading Exercise...