Press ESC to close

Variables Python

0 2132

Today we will talk about variables in Python. Recall that variables are memory spaces where information is stored. This information can be numeric or string.

Variables names in Python

The variables are assigned a name, as already mentioned in the other tutorials, the names of the variables must be given with common sense, in order to make their meaning intuitive.

Also there are rules to follow:

  • A variable cannot be a number: for example 13 is not released.
  • A variable cannot start with a number: for example 5area is not while area5 or area5triangle is.
  • No spaces, instead of spaces you can insert underscores (_). In fact, for example, area_quadrato is allowed while area_square is not.
  • The variable cannot contain the period (.), Exclamation point and therefore no punctuation character.
  • It can’t even contain symbols such as $, &,% etc …
  • Furthermore, it cannot be any of the keywords of the language, listed below in the screenshot and which we will deepen further on:
keywords

Python is a case sensitive language, therefore the area variable is different from the AREA variable and again from the Area variable for example.

Examples on the use of variables in Python

We use two variables named a and b which we will use to do some operation.

So let’s look at the figure below:

In this very simple example we have assigned a and b two numerical values ​​which we then added together.

Single-line comments are inserted with this # symbol.

While, multi-line comments are obtained by using this symbol “’ (three quotes) or “” “(three quotes) for opening and the same symbol for closing.

We must however note that the comments are optional, in this case they simply served to explain what I did.

>>> a=10 #we assign the value 10 to a, using the assignment instruction =

>>>a #we ask to display the value of the variable a

10

>>>b=5 #we assign the value 5 to b

>>>b #we ask to display the value of the variable b

5

>>>a+b # add a and b, and print the value

15

Variables Python – Types of data

For the moment we will analyze some types of data:

int – integers

float – floating point (decimal)

complex – complex numbers

bool – Booleans

string – string

Ci sono altri tipi di dati che analizzeremo andando più avanti nel seguente tutorial.

Variables Python – Dynamic typing

As you can see Python is dynamically typed, variables are declared by assigning a value that determines their data type.

In the previous example we assigned an integer value, but we can change the assigned value during our program.

For example:

>>>a=10

>>>a

10

>>>a=10.2

>>>a

10.2

In the first case the variable is int, in the second case it is float.

Type function

To check the data type, Python offers the type function.

For example:

>>>a=12

>>>type(a)

<class ‘int’>

>>>a=12.5

>>>type(a)

<class ‘float’>

This is just a simple example on the use of variables in Python, in the next lesson I will propose many other examples.

Useful links

Python tutorial

1 – Python Install

Other stories

Print Python

Next Story