Variables in Python
Variables in Python
In Python, variables are used to store values. These values can be of different data types like integers, floats, strings, and more. Python is dynamically typed, which means you do not need to explicitly declare the type of a variable before using it. Python automatically infers the type based on the assigned value.
1. Declaring Variables
In Python, you can create a variable by simply assigning a value to it using the = (assignment) operator.
Syntax:
variable_name = valueExample:
name = "Alice" # String variableage = 25 # Integer variableheight = 5.6 # Float variableIn this example,
nameis a variable storing a string,agestores an integer, andheightstores a float.
2. Variable Naming Rules
When naming variables in Python, you must follow these rules:
A variable name must begin with a letter (a-z, A-Z) or an underscore (_).
The rest of the variable name can contain letters, digits (0-9), and underscores.
Variable names are case-sensitive, so
ageandAgewould be treated as different variables.Variable names cannot be Python keywords (e.g.,
if,else,def,class).
Examples of valid variable names:
age = 30first_name = "John"_height = 1.75Examples of invalid variable names:
2nd_name = "Alice" # Invalid: starts with a digitclass = "Math" # Invalid: 'class' is a reserved keyword3. Assigning Multiple Variables
Python allows you to assign values to multiple variables in a single line.
Example:
x, y, z = 10, 20, 30print(x) # Output: 10print(y) # Output: 20print(z) # Output: 30Here, the values
10,20, and30are assigned to variablesx,y, andz, respectively.
4. Reassigning Variables
Variables in Python are mutable. You can reassign a new value to a variable at any time.
Example:
age = 25print(age) # Output: 25age = 30print(age) # Output: 30In this example, the value of
ageis reassigned from25to30.
5. Type of a Variable
You can check the type of a variable using the type() function.
Example:
a = 10b = "Hello"c = 3.14print(type(a)) # Output: <class 'int'>print(type(b)) # Output: <class 'str'>print(type(c)) # Output: <class 'float'>type()returns the type of the variable, showing whether it's an integer (int), string (str), float (float), etc.
6. Constants in Python
Although Python does not have a built-in constant type, by convention, variables that are not meant to be modified are written in uppercase letters, which is used to represent constants.
Example:
PI = 3.14159 # ConstantThis is a convention, not a rule enforced by Python. It’s common to use uppercase variable names for values that shouldn't change.
7. Global and Local Variables
Local Variables: Defined within a function and can only be accessed inside that function.
Global Variables: Defined outside any function, accessible throughout the program.
Example of Local Variable:
def my_function(): local_var = 100 print(local_var)my_function() # Output: 100local_var is only accessible inside
my_function().
Example of Global Variable:
global_var = 50def my_function(): print(global_var)my_function() # Output: 50global_var is accessible both inside and outside of the function.
To modify a global variable inside a function, you must use the global keyword.
Example of Modifying a Global Variable:
x = 10def modify_global(): global x x = 20modify_global()print(x) # Output: 208. Multiple Assignment with a Single Value
Python also allows you to assign the same value to multiple variables in a single line.
Example:
a = b = c = 50print(a, b, c) # Output: 50 50 50Here,
a,b, andcall share the same value50.
9. The del Keyword
The del keyword can be used to delete a variable.
Example:
x = 10del x# print(x) # This will raise an error because x is deletedAfter
del x, the variablexno longer exists.
10. Conclusion
Variables in Python are used to store data.
Python is dynamically typed, meaning you don't need to declare the variable type explicitly.
Python variables are easy to work with and follow simple naming conventions.
You can easily change the value of a variable during the program execution.
By understanding how to create and manage variables, you can store and manipulate data efficiently in your Python programs.