** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
type_function
num = 1
name = 'Mike'
is_ok = True
print(num, type(num))
print(name, type(name))
print(is_ok, type(is_ok))
result
1 <class 'int'>
Mike <class 'str'>
True <class 'bool'>
You don't have to declare the type of an object in Python.
When checking the type, it can be displayed by using the type function type ()
.
different_type
num = 1
name = 'Mike'
num = name
print(num, type(num))
result
Mike <class 'str'>
Substituting the str type name
for the int type num
It is overwritten and becomes str type.
change_type
name = '1'
new_num = name
print(new_num, type(new_num))
new_num = int(name)
print(new_num, type(new_num))
result
1 <class 'str'>
1 <class 'int'>
With new_num = name
, name
remains of type str,
By setting new_num = int (name)
, name
is converted to int type and assigned to new_num
.
error_1
1num = 1
print(num)
result
1num = 1
^
SyntaxError: invalid syntax
OK if the number comes at the end, like num1
.
error_2
if = 1
print(if)
result
1num = 1
^
SyntaxError: invalid syntax
The string ʻif` is already reserved for use in the if statement and cannot be used as a variable.
Recommended Posts