Python Variables
Table of Content:
- What is variable?
- Declaring and assigning value to a variable.
- Changing the value of a variable
- Assigning multiple values to multiple variables.
- Assign the same value to multiple variables at once
- Rules and Naming Convention for Variables and constants.
What is variable
Variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value.
It is helpful to think of variables as a container that holds data that can be changed later in the program.
In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type.
Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.
It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables.
Declaring and assigning value to a variable
- The equal (=) operator is used to assign value to a variable.
Code:
number = 0 product_weight = 2.1 price_per_kilogram = 10 filename = 'file.txt' trace = False sentence = "this is an example" print(number) print(product_weight) print(price_per_kilogram) print(trace) print(sentence)
Output:
0 2.1 10 False this is an example
Changing the value of a variable
Code:
website = "google.com" print(website) # assigning a new value to website website = "atnyla.com" print(website)
Output:
google.com atnyla.com
Assigning multiple values to multiple variables
variable1, variable2, variable3 = 5, 3.2, "Hello" print (variable1) print (variable2) print (variable3)
Output:
5 3.2 Hello
If we want to assign the same value to multiple variables at once, we can do this as:
x = y = z = "Kolkata" print (x) print (y) print (z)
Output:
Kolkata Kolkata Kolkata
Rules and Naming Convention for Variables and constants
- The first character of the variable must be an alphabet or underscore ( _ ).
- All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore, or digit (0-9).
- Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
- Identifier name must not be similar to any keyword defined in the language.
- Identifier names are case sensitive; for example, myname, and MyName is not the same.
- Create a name that makes sense. For example, my_age = 25 makes more sense than m = 25.
- Examples of valid identifiers: a123, _n, n_9, etc.
- Examples of invalid identifiers: 1a, n%4, n 9, etc.
Constant
Assigning value to constant in Python
In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.
Declaring and assigning value to a constant
constant.py
PI = 3.14 INTREST = 2.8
main.py
import constant print(constant.PI) print(constant.INTREST)