Python Syntax Basics
Learn some Python syntax basics
We have already learnt that you can name a variable as you wish.
These are all valid variable names:
num
a_b
_num
num_
NUM
num123
nUm
However, there are certain restrictions.
A variable name cannot start with a number. Let's see what happens if we try:
1_num = 1
print(1_num)
Python will also not accept any special characters like .-+:,;()/#*'"?
in your variable names. They are reserved for other functionalities.
Also, be aware that variable names are case sentive.
The following are 2 different variables.
num=10
NUM=5
print(num)
print(NUM)
What will be the output?
num_ONE = 1
print(num_ONE)
What will be the output?
num?one = 1
print(num?one)
Another important aspect of the Python syntax is indentation.
Indentation refers to the whitespace at the beginning of a code line.
num=10
num_with_indentation=10
Your Python program will throw an error if it encounters unexpected whitespace at the beginning of a code line. Let's see what happens if we use unnecessary indentation:
num_with_indentation=10
print(num_with_indentation)
What will be the output?
num = 0
print(num)