a-walk-in-python

A walk in Python

View on GitHub

A walk in Python

Day 2

Variables


What are variables?

Variables in Python are containers for storing data. There is no reserved word for declaring a variables. You just go like this…

a = "Hello world!"

b = 1

c = True

As we said before, Python is dynamically typed, so the interpreter assumes the type from the type of the value assigned to that variable name. You can even change the type after they have been set.

a = "Hello world!"
print(a)

a = 1
print(a)

You must be asking yourself, why we are not using type hinting…

Ok, so when should you use type hints in assignments?

If you want to specify the type of a variable, this can be done using casting.

a = str('Hello world!')  # a will be 'Hello World!'
b = str(1)               # b will be '1'
c = int(1)               # c will be 1
d = float(1)             # d will be 1.0

To know the type of a variable you can use the reserved word type

a = "Hello world!"
print(type(a))

# output
<class 'str'>  

In Python there are lots of things that are just conventions (PEP8), this means that some things are in a certain way and all python developers should use them.

For variables names, these are the rules:

Lowercase letters are commonly used in variable names, with word separated by ‘_’ as necessary in order to improve readability.

Other conventions are:

Constants

Another one of these conventions, are constants as we just saw. There is no way to declare a variable as a constant. The convention says that if you see a variable name in uppercase, it is a constant.

CONSTANT_NAME = 1
ANOTHER_CONSTANT = 'Hello'

You can change the value of these constants, although if you see the name in uppercase, you’re not supposed to. You will be cursed by the Python Community (:D).

Global variables

All variables that are created outside of a function in Python are, by default, global variables. Global variables can be used outside and inside of a function.

name = 'Ezequiel'

def greeting() -> None:
    print('Hello ' + name)

print('My name is ' + name)
greeting()

#output
My name is Ezequiel
Hello Ezequiel

However, if a variable with the same name is declared inside the function, this variable is going to be local, available to use inside the function, and the one declared outside, will be used outside the function.

name = 'Ezequiel'

def greeting() -> None:
    name = 'Jose'
    print('Hello ' + name)

print('My name is ' + name)
greeting()

#output
My name is Ezequiel
Hello Jose

There is a way to declare a global variable inside a function though, using the reserved word ‘global’.

name = 'Ezequiel'

def greeting() -> None:
    global name = 'Jose'

greeting()
print('My name is ' + name)

#output
My name is Jose

Multiple assignment and unpacking

You can also assign multiple variables, like this…

x, y, z = 'Hello', 'beautiful', 'World'
print(x)
print(y)
print(z)

#output
Hello
beautiful
World

Note: the number of values and variable names, should match, or you will get an error.

Also, you can assign the same value to different variables in one line, like this…

x = y = z = 'Ezequiel'
print(x)
print(y)
print(z)

#output
Ezequiel
Ezequiel
Ezequiel

There is a way to extract the values of a collection into different variables. This is called unpacking

words: list[str] = ['Hello', 'beautiful', 'World']
x, y, z = words
print(x)
print(y)
print(z)

#output
Hello
beautiful
World

Data Types

Built-in Data Types

In programming in general, data types are very important. You can do different things with different data types. As in Python everything is an object, every type has its own methods and attributes.

Python has the following data types built-in by default, divided into these categories:

Text Type: str

Numeric Types: int, float, complex

Boolean Type: bool

Sequence Types: list, tuple

Mapping Type: dict

Set Types: set, frozenset

In order to get the data type from a variable you can just run:

name = 'pepe'
print(type(name))

# output
<class 'str'>

Again, if you want to set a specific data type to a variable, you need to use the casting method.

a = int(1)
print(a)
print(type(a))

b = float(1)
print(b)
print(type(b))

c = str(1)
print(c)
print(type(c))

# output
1
<class 'int'>
1.0
<class 'float'>
1
<class 'str'>

Go to Day 3