Variables In Python

Variables are the values that can vary. Let’s see what are variables in Python.

What is a variable in Python?

Variables in python or in any programming language are containers🥛 that store some value which can be an integer, a floating-point number, a string, or any other data type.

In Python, a variable is a label you can assign a value to. A variable is always associated with a value. For example:

website = "python-hub.com"
print(website)

Output:

python-hub.com

Here, 'website' is a variable that stores the value 'python-hub.com'.

Anywhere in the program when you write website it would be taken as 'python-hub.com'.

You can use website without quotation marks, if you put a quotation mark, it would be considered a string and not a variable so be careful âť—.

'=' the assignment operator is used to give the value'python-hub.com' to the variable website.

You can assign another value to website:

website = 'python.org'
print(website)

Output:

python.org

The variable website can hold various values at different times. And its value can change throughout the program.

Creating variables

To define a variable, you use the following syntax:

variable_name = value

The = is the assignment operator. In this syntax, you assign a value to the variable_name.

The value can be anything like a number, a string, etc., that you assign to the variable.

The following defines a variable named rank and assigns the number 1 to it:

rank = 1

Variable Naming Rules

Python doesn’t allow you to take any name for your variable.

This is because there are a lot of words and characters reserved in python for specific uses.

For example:-

True and False for boolean.

isis notin, and not in as operators. And so on…

Because of these, there are a few options unavailable for you to use.

Let’s see what are the rules📑 you need to follow:-

  • You cannot use numbers at the start meaning a variable name cannot be —> '1python'.
  • You are free to use all the alphabets from a-z and A-Z.
  • Numbers can also be freely used. But, not in the beginning. Like a variable can be —-> 'python1' or 'p1ython'
  • Special characters like @#$%^&*()- or any other are also not allowed.
  • You can use an underscore '_' anywhere in the variable.
  • There must be no use of white spaces.
  • You cannot also use any sort of reserved words.

This is it, no more rules to bore you.

Conclusion

  • A variable is a label that you can assign a value to it. The value of a variable can change throughout the program. A variable can hold any value.
  • Use the variable_name = value to create a variable.
  • The variable names should be concise yet descriptive. Also, they should adhere to Python variable naming rules.

Leave a Reply