Versão da Linguagem Python

from platform import python_version print('Versão da Linguagem Python Usada Neste Jupyter Notebook:', python_version())

List Comprehension

``````

In [1]:

# Retornar cada caracter em uma sequência de caracteres
lst = [x for x in 'python']

``````
``````

In [2]:

# Check
lst

``````
``````

Out[2]:

['p', 'y', 't', 'h', 'o', 'n']

``````
``````

In [3]:

type(lst)

``````
``````

Out[3]:

list

``````
``````

In [4]:

lst = [x**2 for x in range(0, 11)]

``````
``````

In [5]:

lst

``````
``````

Out[5]:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

``````
``````

In [6]:

# Verificar os números pares em um range de números
lst = [x for x in range(11) if x % 2 == 0]

``````
``````

In [7]:

lst

``````
``````

Out[7]:

[0, 2, 4, 6, 8, 10]

``````
``````

In [8]:

# Converter Celsius para Fahrenheit
celsius = [0,10,20.1,34.5]

fahrenheit = [ ((float(9)/5)*temp + 32) for temp in celsius ]

fahrenheit

``````
``````

Out[8]:

[32.0, 50.0, 68.18, 94.1]

``````
``````

In [9]:

lst = [ x**2 for x in [x**2 for x in range(11)]]

``````
``````

In [10]:

lst

``````
``````

Out[10]:

[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]

``````