Python Variables
A variable is a named location used to store data in memory.
For example, create a variable called salary and assign the value 1500:
salary = 1500In addition, it is possible to replace the value of a variable at any time:
salary = 2000
salary = 1750.99Fundamental points
- Python does not have a command to declare a variable, because a variable is automatically created the moment you assign it a value.
- Python is a type-inferred language, so you don't have to explicitly define the variable type. The Python interpreter automatically knows the real time of a variable.
- Variables do not need to be declared with a particular
typeand they can also change type after being set.
Assigning values to Variables in Python
The assignment operator = is used to assign a value to a variable.
For example, assign a value tutorialreference.com to the variable tutorial_website and then print out the value assigned to tutorial_website
tutorial_website = "tutorialreference.com`"
print(tutorial_website)Changing the value of a variable
The assignment operator = is also used to change a value to a variable.
tutorial_website = "example.com"
print(tutorial_website) # print "example.com"
tutorial_website = "tutorialreference.com"
print(tutorial_website) # print "tutorialreference.com"Assigning multiple values to multiple variables
If you want to assign different value to multiple variables at once, you can do in this way:
a, b, c = 1, 2.3, "Hello World"
print(a) # print 1
print(b) # print 2.3
print(c) # print "Hello World"If you want to assign the same value to multiple variables at once, you can do in this way:
x = y = z = "same value"
print (x) # print "same value"
print (y) # print "same value"
print (z) # print "same value"Constants
A constant is a type of variable whose value cannot be changed.
Declaring and Assigning value to constant in Python
In Python, constants are usually declared and assigned in a module.
Python constants are written in all capital letters and underscores separating the words.
For example, create a new module file called constants.py and declare the following constants:
PI = 3.14
GRAVITY = 9.8
E = 2.718Then, you just need to import the constants module
import constants
print(constant.PI) # print 3.14
print(constant.GRAVITY) # print 9.8
print(constant.E) # print 2.718Rules and Naming Convention for Variables and constants
Some suggestions:
- Constant and variable names should have a combination of letters in lowercase (
atoz) or uppercase (AtoZ) or digits (0to9) or an underscore (_). For example:snake_case MACRO_CASE camelCase CapWords - Constant and variable names are case-sensitive.
- Create a name that makes sense. For example,
countermakes more sense thanc. - If you want to create a variable name with two words, use the underscore to separate them. For example:
my_variable positive_counter - To declare a constant, use capital letters if possible. For example:
PI G MASS SPEED_OF_LIGHT TEMP - Never use special symbols like
!,@,#,$,%, etc. - Do not start the name of a variable with a digit.