Introdução a Python¶

Aula 1¶

Sumário¶

  • Instalação e setup
  • Interpretador
  • Tipos e variáveis
  • Operações aritméticas
  • Operações de atribuição
  • Operações de comparação
  • Exercícios

Instalação e setup¶

Interpretador¶

Para verificar a versão do Python:

$ python --version

Para entrar no interpretador de Python:

$ python

Para obter mais informações acerca de uma função ou de um módulo X:

>>> help(X) #clicar em d para ver mais e q para sair deste modo

Para sair do interpretador de Python:

>>> exit()

Tipos e variáveis¶

Tipos¶

str (sequências de caracteres): "Ana", "carro", '123', etc.

In [1]:
type("Ana")
Out[1]:
str

int (números inteiros): 1, 2, 3, etc.

In [2]:
type(1)
Out[2]:
int

float (números decimais): 1.5, 2.75, etc.

In [3]:
type(1.5)
Out[3]:
float

complex (números imaginários): 2+i, i, etc.

In [4]:
type(complex(2,1))
Out[4]:
complex

bool (booleanos): True, False

In [5]:
type(True)
Out[5]:
bool

Casting¶

In [6]:
str(1)
Out[6]:
'1'
In [7]:
int("23")
Out[7]:
23

Variáveis¶

In [8]:
a = "João"

a
Out[8]:
'João'
In [9]:
a = "Sara"

a
Out[9]:
'Sara'
In [10]:
A = "Maria"

a, A
Out[10]:
('Sara', 'Maria')
$$IMC = \frac{peso}{altura^2}$$
In [11]:
peso = 48 #se eventualmente o peso alterasse, apenas seria necessário mudar este valor 
altura = 1.58 

imc = peso/altura**2

print(f"O meu IMC corresponde a {round(imc,2)}.")
O meu IMC corresponde a 19.23.

Operações aritméticas¶

+: Adição

In [12]:
1 + 1
Out[12]:
2

-: Subtração

In [13]:
5 - 2
Out[13]:
3

*: Multiplicação

In [14]:
2 * 3
Out[14]:
6

/: Divisão

In [15]:
4 / 2
Out[15]:
2.0

//: Divisão inteira

In [16]:
10 // 3
Out[16]:
3

%: Divisão modular

In [17]:
7 % 3
Out[17]:
1

**: Exponenciação

In [18]:
2 ** 3
Out[18]:
8

sqrt: Raíz quadrada

In [19]:
import math

math.sqrt(16)
Out[19]:
4.0

Diferentes tipos = diferentes comportamentos¶

In [20]:
2 + 3
Out[20]:
5
In [21]:
'ab' + 'cd'
Out[21]:
'abcd'
In [22]:
2 * 10
Out[22]:
20
In [23]:
"*" * 10
Out[23]:
'**********'

Operações de atribuição¶

=

$x = 7$

+=

$x += 3 <=> x = x + 3$

-=

$x -= 1 <=> x = x - 1$

*=

$x *= 2 <=> x = x * 2$

/=

$x /= 5 <=> x = x / 5$

//=

$x //= 6 <=> x = x // 6$

%=

$x \%= 8 <=> x = x \% 8$

**=

$x **= 4 <=> x = x ** 4$

Operações de comparação¶

==: Igual

In [24]:
x, y = 5, 5

x == y
Out[24]:
True

!=: Diferente

In [25]:
x, y = 3, 6

x != y
Out[25]:
True

>: Maior

In [26]:
x, y = 2, 4

x > y
Out[26]:
False

<: Menor

In [27]:
x, y = 1, 1

x < y
Out[27]:
False

>=: Maior ou igual

In [28]:
x, y = 9, 9

x >= y
Out[28]:
True

<=: Menor ou igual

In [29]:
x, y = 7, 10

x <= y 
Out[29]:
True

Exercícios¶