Python Variables
In Python, variables are used to store data that can be referenced and manipulated during
program execution. A variable is essentially a name that is assigned to a value.
Unlike Java and many other languages, Python variables do not require explicit
declaration of type.
The type of the variable is inferred based on the value assigned.
x=5
name = "Samantha"
print(x)
print(name)
Output
Samantha
Rules for Naming Variables
To use variables effectively, we must follow Python’s naming rules:
1. Variable names can only contain letters, digits and underscores (_).
2. A variable name cannot start with a digit.
3. Variable names are case-sensitive like myVar and myvar are different.
4. Avoid using Python keywords like if, else, for as variable names.
Assigning Values to Variables
Basic Assignment: Variables in Python are assigned values
using the = operator.
x = 5
y = 3.14
z = "Hi"
Multiple Assignments
Assigning Same Value: Python allows assigning the same value to multiple variables in a
single line, which can be useful for initializing variables with the same value.
a = b = c = 100
print(a, b, c)
Output
100 100 100
Assigning Different Values: We can assign different values to multiple variables
simultaneously, making the code concise and easier to read.
x, y, z = 1, 2.5, "Python"
print(x, y, z)
Output
1 2.5 Python
Type Casting a Variable
Type casting refers to the process of converting the value of one data type into another.
Python provides several built-in functions to facilitate casting, including int(), float() and str()
among others. Basic casting functions are:
int(): Converts compatible values to an integer.
float(): Transforms values into floating-point numbers.
str(): Converts any data type into a string.
s = "10"
n = int(s)
cnt = 5
f = float(cnt)
age = 25
s2 = str(age)
print(n)
print(f)
print(s2)
Output
10
5.0
25
Type of Variable
In Python, we can determine the type of a variable using the type() function. This built-in
function returns the type of the object passed to it.
n = 42
f = 3.14
s = "Hello, World!"
li = [1, 2, 3]
d = {'key': 'value'}
bool = True
print(type(n))
print(type(f))
print(type(s))
print(type(li))
print(type(d))
print(type(bool))
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'bool'>