Типи даних змінних

Вступ до Python для фінансів

Adina Howe

Professor

Типи даних у Python

Типи змінних Приклад
Рядки 'hello world'
Цілі числа 40
Дробові (float) 3.1417
Булеві True або False
Вступ до Python для фінансів

Типи змінних

Типи змінних Приклад Скорочення
Рядки 'Tuesday' str
Цілі числа 40 int
Дробові (float) 3.1417 float
Булеві True або False bool
Вступ до Python для фінансів

Який тип має змінна: type()

Щоб визначити тип, скористайтеся функцією type():

type(variable_name)
pe_ratio = 40
print(type(pe_ratio))
<class 'int'>
Вступ до Python для фінансів

Операції зі змінними

x = 5
print(x * 3)
15
print(x + 3)
8
y = 'stock'
print(y * 3)
'stockstockstock'
print(y + 3)
TypeError: must be str, not int
Вступ до Python для фінансів

Зміна типів змінних

pi = 3.14159
print(type(pi))
<class 'float'>
pi_string = str(pi)
print(type(pi_string))
<class 'str'>
print('I love to eat ' + pi_string + '!')
I love to eat 3.14159!
Вступ до Python для фінансів

Давайте потренуємось!

Вступ до Python для фінансів

Preparing Video For Download...