Professional Documents
Culture Documents
It is
a way to store and manage data in a program. Unlike some other programming
languages, in Python, you don't need to declare the type of a variable explicitly;
Python dynamically infers the type based on the value assigned to it.
Variable Assignment:
You can assign a value to a variable using the equal sign (=). For example:
python
age = 25
name = "John"
Variable Naming Rules:
python
x = 10 # x is an integer
y = "hello" # y is a string
Reassignment:
You can reassign a variable to a new value of a different type:
python
x = 10
x = "hello"
Variable Types:
Python supports various data types, including integers, floats, strings, lists,
tuples, dictionaries, and more. The type of a variable can be checked using the
type() function:
python
age = 25
print(type(age)) # Output: <class 'int'>
Variable Scope:
The scope of a variable defines where it is accessible. Variables can have local
scope (within a function or block) or global scope (accessible throughout the
entire program).
python
def my_function():
local_var = 5 # Local variable
print(global_var) # Accessing global variable within the function
my_function()
Variables play a fundamental role in Python programming, enabling the storage and
manipulation of data within a program. Understanding how to declare, assign, and
use variables is essential for writing effective and readable Python code.