[Introduction to Udemy Python3 + Application] 8. Variable declaration

** * 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. ** **

■ Check the type of the value (object) assigned to the variable

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 ().


■ What happens if you assign a variable of a different 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.


■ Convert 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.


■ Things that cannot be used as variables

◆ Numbers at the beginning

error_1


1num = 1

print(num)

result


    1num = 1
       ^
SyntaxError: invalid syntax

OK if the number comes at the end, like num1.

◆ Words already reserved

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

[Introduction to Udemy Python3 + Application] 8. Variable declaration
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Udemy Python 3 + Application] 10. Numerical values
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 65. Exception handling
[Introduction to Udemy Python3 + Application] 11. Character strings
[Introduction to Udemy Python3 + Application] 44. range function
[Introduction to Udemy Python3 + Application] 46. Zip function
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Introduction to Udemy Python3 + Application] 29. Set method
[Introduction to Udemy Python3 + Application] 16. List type
[Introduction to Udemy Python3 + Application] 61. Dictionary comprehension
[Introduction to Udemy Python 3 + Application] 22. Tuple unpacking
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Udemy Python3 + Application] 23. How to use tuples
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Introduction to Udemy Python 3 + Application] 38. When judging None
[Introduction to Udemy Python3 + Application] 40.while else statement
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Udemy Python3 + Application] 43. for else statement
[Introduction to Udemy Python3 + Application] 67. Command line arguments
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
[Introduction to Udemy Python3 + Application] 14. Character substitution 15.f-strings
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[Introduction to Udemy Python 3 + Application] 66. Creating your own exceptions
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
Introduction to Python language
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Introduction to Python 3 taught by an active Silicon Valley engineer + application + American Silicon Valley style code style 8 Variable declaration
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
[Introduction to Udemy Python3 + Application] 37. Techniques for determining that there is no value
Introduction to Python For, While
Introduction to Ansible Part ④'Variable'
Introduction to Python Numerical Library NumPy