Variables in Python
Variables are used to hold data for processing during the program execution. These variables are created in main memory called RAM. Unlike other famous high level languages such like java, C++ and C, python has different way of declaration and usage of variables.
Python Variable Data Type
In python programming there is no need to specify data type of variable before assigning it value. Python is a loosely typed programming language. We are only required to specify variable name called identifier and its value.Python will automatically set variable type according to contents it hold. After assignment of data we can also check the data type of variable using type() function.
Type Casting in Python
We can also change/cast data type of the defined variables in python. for this purpose we have to use type functions provided to the variable name. Some useful type casting functions are as follows.
Sr. | Function Name | Use |
1 | str() | This function will convert data type input variable into string format. |
2 | int() | This function is used to convert the data type input variable into integer format. |
3 | float() | By using this function data type of input variable will be converted into float/decimal format. |
Defining Variables in Python
We can define a variable in python by just specifying its name and value.
variable_name = value
Python Integer Type Variable
For defining an integer variable we just have to store natural number in variable using assignment statement. Python will automatically treat this identifier as integer.
Integer Example
x = 10
print(type(x))
Output
<class 'int'>
Python Decimal Type Variable
For defining an decimal or floating point variable we are required to store number with decimal point in variable using assignment statement. Python will automatically treat this identifier as floating point variable.
Floating Point Example
x = 10.0
print(type(x))
Output
<class 'float'>
Python StringType Variable
String type variables can also be defined by justing storing string/series of characters enclosed in single or double quotes in variable using assignment statement. Python will automatically treat this identifier as floating point variable. This string may also contain numbers or special symbols.
String Example
x = "10.0"
print(type(x))
Output
<class 'str'>
Python Case Sensitive
Python is a case srnsitive high level programming language. It means that it can differentiate between upprcase and lowercase identifiers. THese identifiers may be the name of variable, constant, label, function or class.