Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Variables in Python

Python variables are named memory locations that store data, allowing for dynamic and effective data management in programming. Variable naming conventions emphasize meaningful names, lowercase letters, and avoiding reserved words to enhance code readability. Variables can be created and initialized simply by assigning values, and they can be local or global, with specific rules governing their scope and accessibility.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Variables in Python

Python variables are named memory locations that store data, allowing for dynamic and effective data management in programming. Variable naming conventions emphasize meaningful names, lowercase letters, and avoiding reserved words to enhance code readability. Variables can be created and initialized simply by assigning values, and they can be local or global, with specific rules governing their scope and accessibility.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Variables

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.

What are Variables in Python

A variable is a named memory location in which a value is stored.

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.

In Python, it is not needed to define a variable before using it.

When a value is assigned to a variable, it is created.

A variable's value can be changed at any moment. This will override its previous value.

Python Variable Naming Conventions

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.

Declaration and Initialization of Variables in Python

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.

Syntax of Declaring & Initializing Variable in Python

variable_name = value

EXAMPLE

# Declare a variable named 'age'

age = 25

# Declare a variable named 'name'

name = "Ram"

# Print the values of the variables 'age' and 'name'

print("My name is", name, "and I am", age, "years old.")

Output

My name is Ram and I am 25 years old.

How to create variables in Python?

Creating variables in Python is a simple process. Python, unlike other programming


languages, does not require an explicit variable declaration. Variables are usually generated
when you apply a value to them.
Select a variable name: Variable names should be descriptive and follow Python's naming
rules. They can consist of letters, digits, and underscores, but they must not begin with a
number or contain special characters.

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.

How does the + operator work with variables?

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.

How do you print variables in Python?

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.

How to delete a variable in Python?

In Python, the del keyword deletes variables and objects. It removes the object's reference
from the program's memory.

Multiple Assignment of Python Variables

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.

Example of Multiple Assignment of Python Variables

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

Python local variable & Python global variable

Python Local Variable

A variable declared within a Python function is referred to as a local variable.

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.

Example of Python Local Variable

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

Python Global Variable

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.

Example of Python Global Variable

counter = 0

def increment_counter():

global counter
counter += 1

increment_counter()

increment_counter()

increment_counter()

print(counter)

Explanation

In this example, the counter variable is declared outside of the increment_counter()


function. It is thus a global variable that can be accessed from anywhere in the program. The
increment_counter() function uses the global keyword to access the global variable counter.
This is required because the function also creates a local variable with the same name. The
function could only access the local variable if the global keyword was not used.

Output

You might also like