Variables in Python
Variables in Python
Python variables are a core concept in Python programming. They allow to save data that
may be accessed and modified throughout the code. This gives a versatile and effective
approach to managing and working with data, making the programs more dynamic and
powerful.
Any data type in Python, such as an integer, string, or list, can be used as the value.
Variables are used to store information that will be needed during the program.
A variable's value can be changed at any moment. This will override its previous value.
Variable naming conventions in Python are rules for defining meaningful and consistent
variable names that improve code readability and maintainability. While Python does not
require strict naming conventions, following them is commonly supported among Python
programmers.
General Rules:
Begin with a letter (A-Z, a-z) or an underscore (_): Variable names should begin with a letter
(A-Z, a-z) or an underscore (_). A variable name cannot begin with a number.
Use lowercase: Lowercase names for variables are preferred. This is a standard Python
convention that helps readability.
Use underscores to separate words: When multiple words combine to make a variable
name, use underscores instead of spaces to separate them. This is referred to as a
snake_case.
Avoid reserved words: Do not use words reserved by Python for keywords, functions, or
built-in data types, such as if, elif, else, def, class, import, and so on.
Maintain descriptive names: Select variable names that effectively reflect their purpose and
meaning. Avoid ambiguous or general titles such as transient or data.
Unless they are common, avoid abbreviations: Only use abbreviations when they are well-
known and obvious. For example, the count is superior to cnt.
Keep your naming consistent across your code: Maintain consistency in your codebase's
naming approach. This simplifies the code's understanding and maintenance.
Declaring and initializing variables in Python is a simple operation that comprises assigning a
value to a variable using the assignment operator (=). Python, unlike other programming
languages, does not require explicit variable declarations.
Declaration
Declaring a variable in Python is simply giving it a name. This name can be any valid
identifier that follows naming rules, such as beginning with a letter or underscore and
containing alphanumeric characters, underscores, and digits.
Initialization
Initialization is the process of providing a variable with an initial value. This value can be of
any valid data type, including integers, strings, floats, booleans, and more complicated data
structures such as lists, dictionaries, and sets.
variable_name = value
EXAMPLE
age = 25
name = "Ram"
Output
Assign a value: To assign a value to the variable, use the python's assignment operator (=).
Any data type, such as integers, texts, floats, or booleans, can be used as the value.
Use variables: Once you've created a variable, you can use it to store and retrieve data
throughout your program. To get the variable's value, refer to its name.
The Python plus operator + makes it easy to add a value if it is a number or concatenate a
string. If a variable has already been created, the new value is assigned to the same variable.
The print() method in Python can be used to print variables. To do so, simply put the
variable name inside the print() function's parentheses.
In Python, the del keyword deletes variables and objects. It removes the object's reference
from the program's memory.
The ability to assign numerous values to various variables in a single line of Python code is
referred to as multiple assignment. This works by placing a comma-separated list of
variables on the left side of the assignment operator (=) in Python and a corresponding list
of values on the right.
a,b,c = 1,2,"Student"
print (a)
print (b)
print (c)
Output
Student
Local and Global Variables in Python
There are two types of variables in the Python programming language. Those variable
names in Python are
It exists simply within the function and cannot be accessed from anywhere else.
When the function is called, local variables are generated and removed when the function
returns.
def sum(x,y):
sum = x + y
return sum
print(sum(5, 10))
In this example in the Python Online Compiler, the function "sum" is defined to take two
parameters, add them, store the result in the variable "sum" and then return that sum. With
the values 5 and 10, the function is run, and the output (15) is displayed on the console.
Output
15
A global variable in Python is a variable with a global scope, which means it can be accessed
from anywhere in the program, including within functions.
Global variables are declared outside of any Python function and are normally defined at the
program's start.
counter = 0
def increment_counter():
global counter
counter += 1
increment_counter()
increment_counter()
increment_counter()
print(counter)
Explanation
Output