Fundamentals of Python Programming
A-Z about Python Variables
Python Variables 101
The management of several values is required while creating a program. Variables are used to store values. A variable in Python is a label to which a value may be assigned. And a value is always connected to a variable. Consider this:
- "message" is a variable in this instance. It contains a string that is printed to the console using the print() method.
- The text "Good bye!" is assigned to the message variable which overwrites its previous value. Since we use the print() statement again, the new value is printed to the console.
- The variable message might have a range of values at various points in time. Additionally, it's worth could shift throughout the duration of the program.
Creating variables
The following is the syntax for declaring a variable:
The assignment operator is =.
You provide the variable name with a value using this syntax. The value you provide for the variable can be anything, including an integer, a string, etc. The following establishes a variable called "counter" and gives it the value 1:
Naming variables
There are some guidelines you must follow when naming a variable. You will encounter an error if you don't. The variable laws that you need to remember are as follows:
- Only letters, digits, and underscores (_) are permitted in variable names. Instead of a number, they may begin with a letter or an underscore ( ).
- Spaces are not permitted in variable names. You use underscores, like sorted list, to divide words in variables.
- In Python, built-in functions, reserved words and keywords cannot share the same name as a variable.
You may define appropriate variable names using the following recommendations:
- Variable names ought to be clear and illustrative. The active user variable, for instance, provides more information.
- In the variable names, use underscores (_) to denote word breaks.
- Because they resemble the numbers 1 and 0, the letter l and the capital letter O should not be used.
Summary
- The name of a variable is a placeholder for a value.
- The value of a variable could vary while the programme is running.
- Use the syntax variable name = value to create a variable.
- The variable names should ideally be short and descriptive.
- They should also adhere to Python's rules for variable naming.