Python Variables

Python Variables, Creating Python Variables, Assigning Values to Variables, Variable Types in Python, Global and Local Variables in Python.

Variables in Python

What is a Variable?

• Variable is a named memory location to store the temporary data within a program.

• We have two types of memory in a computer environment (Temporary Memory- RAM and Permanent Memory – ROM), variables store in the Temporary memory (RAM).

• In Python, We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.

• A variable is a name given to a memory location. It is the basic unit of storage in a program.

Rules for creating variables in Python:

• A variable name must start with a letter or the underscore character.

• A variable name cannot start with a number.

• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

• Variable names are case-sensitive.

• The reserved words(keywords) cannot be used to name the variable.

Creating Variables

Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

Example:

# declaring the variables
x = 123
y = “G C Rddy”
z=10.234

# Print variables
print(x)
print(y)
print(z)

Output:
123
G C Reddy
10.234

Assigning a single value to multiple variables:

Example:

a = b = c = 10

print(a)
print(b)
print(c)

Output:
10
10
10

Assigning different values to multiple variables:

Example:

a, b, c = 1, 20.2, “GCReddy Info”

print(a)
print(b)
print(c)

Output:
1
20.2
GCReddy Info

Get the Type

We can get the data type of a variable with the type() built-in function.

Example:

x = 100
y = “India”
z= 10.234

print(type(x))
print(type(y))
print(type(z))

Output:
<class ‘int’>
<class ‘str’>
<class ‘float’>

Global and Local Variables in Python:

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.

This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions.

Example:

x = 100 # Global variable

def func():
y=200 # Local variable

print(x+y)

func()

Output:
300

Delete a Variable

We can also delete a variable using del command.

a=123;
print (a); #123

del(a);
print(a); # It will show error

Variable type in Python:

Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instances (objects) of these classes.

Python has five standard data types −

1. Numbers
2. String
3. List
4. Tuple
5. Dictionary


Python Step by Step Tutorial

1) In Which sequence should We learn Python?

2) Introduction to Python

3) Download & Install Python

4) Variables and Data Types in Python

5) Python Operators

6) Python Conditional Statements

7) Python Loops…

8) String Handling in Python

9) Python Lists

Follow me on social media: