» Quick Introduction to Python » 1. Basics » 1.3 Variables

Variables

As you may have noticed in previous sections, variables are not statically typed in Python.

x = 42
print(f"I'm a number: {x}")

x = "Whoosh, I'm a string now"
print(x)

The first print function is using f-string to format string literals.

Variable names in Python can be any length and can consist of uppercase and lowercase letters A-Z, a-z, digits 0-9, and the underscore _. Besides this, although a variable name may contain digits, the first character cannot be a digit.

In addition to that, the Python language reserves a small set of keywords for its own use. No variable or any other object can have the same name as a reserved keyword.

The keyword list can be listed like this.

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 
'try', 'while', 'with', 'yield']
>>> 

Code Challenge

Try to modify the code provided in the editor to make it print The type of variable x is <class 'set'>.

Loading...
> code result goes here
Prev
Next