Pular para o conteúdo principal
Módulo 1 · Lição 1 de 41/36 no curso~10 min
Lições do módulo (1/4)

Variáveis e tipos

In Python, a variable is simply a name we bind a value to. You don't need to declare it with a keyword like in JavaScript: just assign it something with the = symbol.

Python
saluto = "Ciao, mondo!"
anni = 30
pi_greco = 3.14
attivo = True

From that moment on, every time you write saluto Python replaces it with the string "Ciao, mondo!".

Built-in types you'll always encounter

The five basic types you'll use in the first weeks are:

QUERY LENGTH LIMIT EXCEEDED. MAX ALLOWED QUERY : 500 CHARS

You can always ask for the type of a value with the type(...) function:

Python
type(42)        # <class 'int'>
type(3.14)      # <class 'float'>
type("ciao")    # <class 'str'>
type(True)      # <class 'bool'>
type(None)      # <class 'NoneType'>

Reassignment and dynamic types

Python is dynamically typed: the same variable can point over time to values of different types. It's not a good practice to abuse it, but it's legal:

Python
x = 10        # ora x è int
x = "dieci"   # ora x è str

The last expression is displayed

In our exercises (and in the Playground) the last expression of a block is captured as the "return value" and shown next to the output panel, REPL-style. So you can simply write the name of a variable as the last line to display its value.

Conventions and strong typing

QUERY LENGTH LIMIT EXCEEDED. MAX ALLOWED QUERY : 500 CHARS

Try it yourself

Exercício#python.m1.l1.e1
Tentativas: 0Carregando…

Create a variable `greeting` with the value 'Ciao, mondo!' and then evaluate `greeting` as the last expression.

Carregando editor…
Mostrar dica

In Python you don't need a keyword: just `name = value`.

Solução disponível após 3 tentativas

Review exercise

Exercício#python.m1.l1.e2
Tentativas: 0Carregando…

Create a variable `age_type` that contains the type of the value 30 (hint: use the type function).

Carregando editor…
Mostrar dica

type(30) returns <class 'int'>.

Solução disponível após 3 tentativas

Additional challenge

Exercício#python.m1.l1.e3
Tentativas: 0Carregando…

Create a variable `height` with the float value `1.75`. Next, retrieve its type and assign it to the variable `height_type`. Finally, evaluate `height_type`.

Carregando editor…
Mostrar dica

Use type(height) to retrieve the float's type and assign it to height_type.

Solução disponível após 3 tentativas