Python Variables
Variables are most foundational and powerful building blocks of any progamming languages. Variable are used to store and manipulate the data, these are containers for storing data.
In Python, you don't need to explicitly declare the **data type** of a variable. You can create a variable simply by assigning a value to it. Here are two simple examples:
```python
age = 22
first_name = 'arya'
last_name = 'stark'
input_value_flag = True
```
Python programmers follow snake case, here are high level summary of different patterns :
1. Camel case − First letter is a lowercase, but first letter of each subsequent word is in uppercase. For example: `firstName`, `pricePerHour`
1. Pascal case − First letter of each word is in uppercase. For example: `FirstName`, `PricePerHour`
1. Snake case − Use single underscore (_) character to separate words. For example: `first_name`, `price_per_hour`
### static vs dynamic typing
In static typing variable types are explicitly declared and checked at compile-time, while in dynamic typing variables can be assigned to different types of values and the type checking is done at runtime.
1. Statically Typed Languages: Java, C++, C#,
2. Dynamically Typed Languages: Python, JavaScript, php, Perl.
**Python is dynamically typed**, which means that the type of a variable is determined at runtime based on the value assigned to it. You can change the value and type of a variable as needed.
Some basic rules of variable naming
1. Variables must contains letters (a-z, A-Z), numbers(0-9) and underscore ( _ )
1. Variales should not start with number
1. Spaces are not allowed
1. Variable cannot be keywords (e.g. break, while)
1. It is recommended to have short and decriptive are best
1. Variables are case sensitive e.g. `name` and `Name` are different.
Some other points
1. You can assign value to variable while creating it but it is not mandatory.
1. Variable type will get changed based on value.
### multi assignment
If you want to assign same value to muliple variables, you can do so in one line
```python
a = b = c = 22
print("a:",a)
print("b:",b)
print("c:",c)
```
output
```cmd
a: 22
b: 22
c: 22
```
Same way multile variables can be assigned separate values on same line as below.
```python
a, b, c = 22, 55.55, "HDFC Bank"
print("a:",a)
print("b:",b)
print("c:",c)
```
output
```cmd
a: 22
b: 55.55
c: HDFC Bank
```
### variable reuse.
If we assign a new value to an existing variable, original value will get overwritten and variable will keep latest value
```python
name = 'arya'
print('name:', name)
name = 'arya stark'
print('name:', name)
```
output
```cmd
name: arya
name: arya stark
```
### Casting
You can specify the data type of a variable using casting. This is specifically important if you are using user provided inputs.
```python
age = int(input_age)
age = int(22) # age will be 22
age = str(22) # age will be '22'
age = float(22) # age will be 22.0
```
### type()
In many a cases, it is important to check data type of variable before using in, python provided built in function `type()` to check data type.
This post is written by Pravin
Tags : Python
Share it :