Menu Close

Articles

Python: variables

A variable in general designate an identifier that represents data. It may be a single value or a group of several values. In most programming languages a variable can be modified. Name of the variable though is fixed.

Data types

A variable represents data. In low level computation data is stored as bit: “0” or “1”, but a high level language like Python can manipulate abstract data types, for example: “numeric”, “string”, “boolean”.

Example:

Note: Usually you do not change a variable data type and purpose in a program. But in Python you can. In my opinion this is a bad practice and source of logical errors. Avoid if possible.

Type Inference

In Python if you do not declare data types for variables Python automatically determine the data type from data literals. In Python an identifier become variable if a value is assigned to it. The assignment is done using symbol “=” (equal). 

Example:

Note: This syntax do not use a type hint, it uses type inference.

Collections

Python collections are implemented using a comprehensive mathematical notation. This makes python easy to work with and intuitive. Python define following collection types:

Example:

In next example we create 4 collections. 

Note: Each collection is initialized using a special notation explained next.

List:

A list is a dynamic collection of elements ordered in the order of creation. You can add elements and remove elements from a list. The elements can have any data type and can be mix: strings with numbers. The element is indexed from 0 to number of elements -1. So you can use a subscript to refer to a particular element from the list.  A list is using notation: [1,2,3]. The empty list is [].

Notes:

  • elements in a list are ordered in the order of insertion,
  • element index in list start from [0] and last element is (-1),
  • negative index x represents count-x where count is the total number of elements,
  • a list do not have gaps and always starts from index = 0 even if we remove elements.

Tuple

A tuple is like a list except it is not dynamic. Once you set a tuple the number of elements and the value of elements can’t be modified. That is a tuple is an immutable structure. You can only read element value from a tuple using a subscript. A tuple is using notation: (1,2,3). Empty tuple is the unit: ().

Tuple elements can be accessed by index like a list, in order. But you can not add a new element and you can not remove elements from tuple.  However you can check if one element belong to a tuple.

Example:

Set

A set is like a list except that all elements are unique. You can’t have duplicates. The elements can be any kind of data. Elements of a set can’t be addressed by a subscript. You can test if a value is in the set using the operator “in” or “not in”. A set is using literal notation: {1,2,3}. Empty set is {}.

Note: Elements in a set are sorted, not ordered in the order of insertion. You can not access a set using index notation. If my_set = {1,2,3} then my_set[0] will not work.

Example:

Dictionary

A dictionary is a hash table. It is a pair of key and value like an index of a book. The key is unique and the value can be any kind of data type and can have duplicates. To define a dictionary we can use literal notation: {key:value, key:value}

The key can be integer, string or tuple. Python is sorting a dictionary by the key.  To get a value from dictionary you must have the key. You can use the key like a subscript index to get or set the value.

Example:

Deconstruction

One interesting feature related to collections is the ability to extract values from a collection using assign operator into multiple variables. If you combine this feature with operator * you can shorten your assign expressions considerable:

examples:

Note: Operator * is called in this case “rest” operator. It is very similar to “…” used in JavaScript. Combination “*_” is called “ignore some” and I have never seen this before in any other language.

You can test this example here: deconstruction

Spreading operator

When you have a function that is expecting multiple arguments, you can use operator “*” as prefix to a collection to spread its members into individual elements. 

example:

Test this example live: spreading

Read next: Decision