Python Variables
Variables are named location that can be used to store data in a memory location. For example
number =20
Here, number
is a variable which points to a address in the memory and its value at that address is 20. In Future, That value of number can be changed but address will point to the same memory location.
Note: In Python, we don't actually assign values to the variables. Instead, Python gives the reference of the object(value) to the variable
Creating Variables
Python has no command for declaring a variable, a variable is created as soon as you assign value to it.
It is important to note that Python is a Type-inferred language. Which means that we do not declare the type of variable. For Example
x =5 # x is of the type int here x = "python" # x is not of type String
Python automatically knows that x is of the type int in line 1. And in line 2 python knows that now x is now of the type string.
Now, what if you don’t want casting to happen automatically, you can simply declare in the below pattern
x = str(3) # x value will be '3' y = int(3); #y value will be 3 z= float(3) #z value will be 3.0
Get the type of a variable
You can get the type of variables by using type()
function. for example,
x = 5 y = "John" print(type(x)) print(type(y))
Case sensitive
Variables in python are case sensitive. for example,
var = 4 Var = "tecnotab" VAR = 4.0
Here, we have defined three different variables although spelling of all three variables are same.
Variable Names
We have to follow certain rules in defining variable names. Here are few rules
- Variable names can not start with a number.
- Variable name must start with a number or a underscore symbol _
- A variable name can only contain alpha-numeric character and underscore.
- Variable names are case-sensitive (num, Num and NUM are three different variables)
Legal variable names
myvar = "John" my_var = "John" _my_var = "John" myVar = "John" MYVAR = "John" myvar2 = "John"
Illegal variable names
2myvar = "John" my-var = "John" my var = "John"
Remember, you should always give meaningful names to the variables. c =1
and count =1
both are valid variables but later makes more sense.
For variables with multiple words, It can be difficult to read. but, you can camel case or pascal case or Snake case to make it more readable.
Camel case:
myVariableName = "John"
Pascal Case:
MyVariableName = "John"
Snake case:
my_variable_name = "John"
Multiple variable declaration
You can also define multiple variables in one single line. For Example
x, y, z = "Orange", "Banana", "Cherry" print(x) print(y) print(z)
You can also assign same value to multiple variables in one line. For example
x = y = z = "Orange" print(x) print(y) print(z)
Python Constants
Constants are also variables but its values can not be changed.
We usually declare constants in a separate file and we import the module in our program wherever required. For example
Create a constants.py File
PI = 3.14
GRAVITY = 9.8
This is the declaration part. now to use it in a main.py file use below code
import constants
print(constant.PI)
print(constant.GRAVITY)
Output
3.14 9.8
Rules of writing constant name is same to same as variable names.
Usually we define constant names in Capital letters. For example
PI G MASS SPEED_OF_LIGHT TEMP