How to Define Variables in Python

Variables in Python are easy to define, unlike some other languages. There is no rule to specify the data type. We can define and assign a value to a variable with one simple statement.
var_name = <value>
As earlier mentioned, you don’t need to specify a data type. The same way you define a variable to store a number is the same way you would do so to store a string, list, boolean, etc.
my_num = 542
text1 = “zuri forever!”
my_list = [54, “happy”, 0]
Quite simple and straightforward!
However, there are some things you should take note of when choosing a variable name. Violating some of these will throw an error or cause your program to behave incorrectly.
  1. A variable name has to start with a letter or underscore character. It cannot start with a number!
    • my_variable, _var, x1 are all valid variable names.
    • 21name, 1char, $var are all invalid variable names.
  2. A variable name can only contain alphanumeric characters (A-Z, a-z, 0-9) and underscores. No spaces allowed.
    • var_1, user_choice, myNum, v1, are all valid variable names.
    • var-1, user choice are invalid.
  3. Variable names are case sensitive:
    • name is different from Name 
    • myVar is different from MYVAR
  4. Variable names should not interfere with reserved keywords in python. That is, avoid using variable names such as: int, str, input, etc.


Variables in Python have been defined here. However, there are caveats you must avoid in order to prevent bugs in your code. Furthermore, all variable names used in your program should be a little descriptive of the value they store. This is to enable readability.

What's your reaction?

In Love
Not Sure

Leave a reply

Your email address will not be published. Required fields are marked *

Next Article:

0 %