Python by example

Variables in Python

Variables are explicitly declared and dynamically typed in Python. There is no way to declare the variable, they are created when a value is assigned to them.

foo = 42
bar = "Hello, World!"

Because python’s variables are dynamically typed, their type can change at any time.

foo = 42
foo = "Hello, World!" # foo is now a string

To check the type of a variable, we can use the type() function.

foo = 42
print(type(foo)) # Output: <class 'int'>

There are no ways to declare constants in Python, so the best practice to simulate a constant is to use a variable and use an uppercase naming as stated in constant section of PEP8.

PI = 3.14159
Next example: Operators