Python Variables

Introduction:

Variables are an important concept in any programming language, including Python. In simple language, a variable is a named location in memory that stores a value. In Python, you can use variables to store any type of data, including numbers, strings, and even complex data structures like lists and dictionaries.

Creating Variables in Python:

To create a variable in Python, you need to give it a name and assign a value to it. Here’s an example:

Name = “ Omkar “
Age = 25
Profession = “Software Engineer “

In the example above, we created three variables: Name, Age, and Profession.
     1.  The name variable is a string.
     2.  The age variable is an integer.
     3.  The Profession variable is also a string.

Python Variable Naming Rules:

When creating variables in Python, there are a few rules that you need to follow:
1). Variable names must start with a letter or underscore (_), followed by any combination of letters, digits, and underscores.
2). Variable names are case-sensitive, which means name and Name are two different variables.
3). A variable name cannot start with a number.
4). A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

Python Variables Types:

1. Numbers – Consists of integers, floating-point numbers, and complex numbers.
2. Strings – Consists of characters in a quotes.
3. Booleans – Consists of True or False values.
4. Lists – Consists of ordered sequences of elements.
5. Tuples – Consists of ordered, immutable sequences of elements.
6. Sets – Consists of unordered collections of unique elements.
7. Dictionaries – Consists of unordered collections of key-value pairs.

Checking Types of the Variable:

First we will create some variables.

Name = “ Omkar “
Age = 25
Height = 5.8 ft

To check the type of the variable use the type() function.

print(type(Name))    # Output: <class ‘str’>
print(type(Age))          # Output: <class ‘int’>
print(type(Height))      # Output: <class ‘float’>

To check the address of the variable use the id() function.

print(id(Name))      # Output: 2623848788368
print(id(Age))       #  Output: 140718085367336
print(id(Height))    # Output: 2623846066576

Value Assignment:

To assign the value to a variable use equal sign ( = ).
Example:
A = 10
We can also assign same value to multiple variables.
A = B = C = 10

Multiple Value Assignment:

We can assign different values to different variables at the same time. We separate the variables and their values by commas ‘,’.

Example:
a, b, c = 40, 50, 60
Here,
Value of variable a is 40 , b is 50 and c is 60.

Scope of Variable:

In Python, the scope of a variable determines where in the code the variable can be accessed and used. The scope of a variable is defined by where the variable is created and assigned a value.
There are two types of variable scopes in Python: global scope and local scope.

1. Global Scope:
Variables created outside of any function or class have a global scope. This means that they can be accessed and modified from anywhere in the code, including within functions and classes.
Example:

number = 100         # global variable

def print_number():
    print(number)    # accessing global variable

def modify_number():
    global number    # declaring number as global variable
    number = 200     # modifying global variable

print_number()       # Output: 100
modify_number()
print_number()       # Output: 200

In the example above, the variable number is declared outside of any function, so it has a global scope. The function print_number() can access and print the value of number, and the function modify_number() can modify the value of number by declaring it as a global variable using the global keyword.

2. Local Scope:
Variables created inside a function or class have a local scope. This means that they can only be accessed and modified within that function or class.
Example:

def my_function():
    number = 100  # local variable
    print(number)  # accessing local variable

my_function()  # Output: 100
print(number)  # NameError: name 'number' is not defined

Basic Mathematical Operations Using Variables:

Creating variables and assigning value to them:
a, b = 10 , 20

Performing operations and printing output:

print(a+b) # Addition       Output: 30
print(a-b) # Subtraction    Output: -10
print(a*b) # Multiplication Output: 200
print(a/b) # Division       Output: 0.5

Conclusion:

In conclusion, variables are an important concept in Python programming language. They allow you to store and manipulate data in your code, and are important for writing effective programs. By understanding how to create, name, and use variables, you can write Python code that is easy to read, maintain.

Leave a Comment