Login

Please fill in your details to login.





initialising variables in python

This page is mainly about initialising variables in python
In this post, we'll look at some of the simpler reasons why you might want to initialise a variable and a quick shortcut you can use to initialise multiple variables as once.



In this tutorial, I'll be using Python 3.8 with the IDLEX extensions installed.

There are a number of reasons why we might want to initialise a variable value …

It may well be that we want to increment or decrement it. If we wanted to initialise a variable that we could increment, we may start it off on a fairly low value, like 0. If we wanted to initialise a variable that we could decrement, we may start it off on a slightly higher value, maybe even 100.
We may also want to initialise a variable that we can append values to. In that case, we'd normally initialise it as an empty list.
Finally, we may want to initialise a variable to use as a flag. A flag is used to indicate whether something has, or hasn't, happened yet.

It's quite normal to initialise variables one at a time, so for instance ...

>>> total = 0
>>> shopping = []
>>> finished = False


But if you have to initialise more than one variable that we wanted to initialise with the same function, we could initialise them like this ...

>>> total = maximum = 0
>>> shopping = bought = []
>>> finished = found = False


Nice, eh?
Last modified: May 17th, 2023
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning