Python Variables, Comments, Indentation and other basic staff

In the last post we showed how to get started in Python. So let’s start today by discussing variables.

Variables in Python

Below is a simple example:

Example of a variable in Python.
(click on the image to open in a new tab)


When I press enter, nothing happens, but now the variable myName holds the value ‘Lambros’ which is a String.

So if I type myName and press enter it returns the string ‘Lambros’.

Variable names can be made up of letters, or they could be made up of a mixture of letters and numbers, or even use the underscore character.

So you could name a variable year325 and assign the string value “First Council of Nicaea” to it and you’d be ok.

Variables in Python can include alphanumeric characters.
(click on the image to open in a new tab)

But you couldn’t do the reverse.

Putting the number at the beginning would result in a syntax error:

Putting a number at the beginning causes a Syntax Error.
(click on the image to open in a new tab)

You can include numbers in a variable provided that this variable doesn’t start with one.

You can use the character underscore, as we previously mentioned, and there is no limitation on its index place.

Underscores can be used in a variable name, even in the beginning.

Other characters, except alphanumerics and underscore, are not legal.

For example, an exclamation mark would cause an error:

The exclamation mark causes an Invalid Syntax message.
(click on the image to open in a new tab)

One other thing to consider is there are some reserved keywords that we cannot use for variable names.

For example:

Continue is a reserved word in Python thus the Syntax Error.

You don’t have to memorize these preserved keywords since both VSCode and Python will let us know.

Editors make spotting reserved words easier.
(click on the image to open in a new tab)

See how the editor color the two variable names differently.

Expressions vs Statements

No matter what language you use, browsing through online forums or programming articles, you may get the impression that these terms mean the same.

This is not accurate since the two terms are distinctive, and their difference is important.

Reading the documentation, we see that an expression is a piece of code that evaluates to a value, while a statement is a block of code, that can be either an expression or a keyword-based construct.

Some examples of python statements.
(click on the image to open in a new tab)

Technically speaking, all code lines and blocks are considered statements. Expressions are included; they represent a special kind of statement. Expression statements.

An expression is anything that produces a value.

You can think of it as something that can be evaluated as a result.

Example of Python expressions.
(click on the image to open in a new tab)

Python Comments

Comments are notes we leave for ourselves or for other programmers inside our code.

They don’t impact code functionality but improve clarity.

Python uses the hash symbol, #, to create comments in code.

Example of a comment in Python.
(click on the image to open in a new tab)

Comments improve understanding of our code but have no impact on execution.

Indentation

Another basic concept of Python is indentation.

In other programming languages, like PHP, white space is ignored.

In Python, white space plays its role.

Let’s see an example:

White (empty) space in Python does matter.
(click on the image to open in a new tab)

We see an error message, ‘Unexpected indentation” and a red wavy line under that indentation.

The same code in PHP wouldn’t result in any error message or warning.

In other languages, like PHP, indentation is not crucial.
(click on the image to open in a new tab)

Although my blog doesn’t support comments, feel free to reply via email or X.