Python Tutorial – Variables
Contents
Python Variables ✈️
Syntax
variable name = value
Example
x = 168
y = "Hello"
Variable Naming
Rules for Python variables: ❶ Variable names "must" start with a letter or an underscore character. ❷ Variable names "cannot" start with a number. ❸ Variable names can only contain alpha (A-z) numbers (0-9) characters and underscores (_). ❹ Variable names are case sensitive (abc, Abc and ABC are three different variables). ❺ Variable names cannot be keywords.
Example
names = "Python"
na_mes = "Python"
_na_mes = "Python"
nAMES = "Python"
NAMES = "Python"
names1 = "Python"
Illegal variable names.
123x = "Python" ( X )
x-x = "Python" ( X )
_x x = "Python" ( X )
for = "Python" ( X )
Variable Naming Recommendation
Camel Case: The first word begins with a lowercase letter and the first letter after the second word is capitalized.
myHouse
Pascal Case: Capitalize the first letter of each word.
MyHouse
Snake Case: The first letter of each word is lowercase and each space is underscored ( _ ).
my_house_name
How 2 Use Variables
Example
my_name = 'python'
print(my_name)
myName = 'Python'
print(myName)
Result
python
Python