Skip to main content

Variables In Python

In previous chapter we had discussed all about how to create first python project by using PyCharm. Now in this chapter we will take a tour of all about python variables.

Variable in Python

A Python variable is a named unit of data that may be assigned a value or we can say that a Python variable is a booked memory location to store specific values. Python Variable also called as identifier and used to keep a value.

Once a variable is define, the space is reserved in memory. In Python we can defined variable by using a combination of letters and digits and the underscore.

Define Variable

We can define Python variable by using variable name and assign a value on it.

Ex: Way to define string variable

myStr = “logixn”

print(myStr)

In our first chapter we had discussed about Python is a dynamically-typed language, so we don’t need to specify the type of a variable while declaring on it.

Ex:

Re-Define a Variable

In Python, A defined variable can be re-defined.

Ex:

Define a variable

A = 15

Print(A)

Re-Define a variable

A = “logixn”

Print(A)

In this example we have defined an integer variable (A =15) once it is defined we can re-defined it as a string variable (A = “logixn”).

Multiple Variables Assignment

In Multiple Variables Assignment, we assign a value to multiple variables in one line.

1. Multiple variables with single value

a = b = c = “logixn”

2. Multiple variables with Multiple value

a, b, c = “Tutorial”, “By”, “Logixn

Deleting a variable

In Python we can also delete a defined variable by using the ‘del’ Keyword.

Ex:

In above example first we define variable ‘A’ (A=15) then delete it by using ‘del’ keyword. If we try to print the variable after deleting we can get error like ‘variable name is not defined’.

Concatenating Variables

By using plus symbol (+) we can concatenate or join multiple strings together. If we have to join number (or other datatype) with string then we should define number (or other datatype) as string otherwise it will give TypeError.

Ex 1: Concatenating integer with string (TypeError)

Ex 2: define integer as string then concatenating with string

Local Variable in Python

A variable created inside a function and only be used inside that function is called local variable.

Suppose we want to define a variable in a particular function then in this case we use a local variable.

Ex:

Global Variable in Python

A variables which one created outside of a function are called as global variables.

Or if we want to used outside define variable for whole of our program we declare it as global variable.

We can declare global variables inside or outside of our program. By using ‘global’ keyword we can declare global variables inside a function.

Ex: – Global Variable created outside of a function

In this above example we can call global variable ‘A’ inside the function.

Ex: – Global Variable created Inside of a function by using ‘global’ Keyword

In this above example we can define global variable ‘A’ inside the function but called from outside of the function.Variables Naming Rules in Python

Variables Naming Rules in Python

  1. The variable name must start with an alphabet or underscore ‘_’ character.
  2. The First character of the variable cannot start with a number.
  3. We can only define variable name by using alpha-numeric characters and underscores (A-z, 0-9, and _).
  4. Variable names are case-sensitive (a = 15 and A = 15 are two different variables).
  5. Python reserved keywords cannot be used as variable.

In a nutshell:

  1. Python variable is a booked memory location to store specific values.
  2. Don’t need to specify the type of a variable while declaring on it.
  3. Variables can be re-define even after we have declared before.
  4. We uses “del” keyword to delete the variable.
  5. We used “global” keyword inside the function to define global variable.

Leave a Reply

Your email address will not be published. Required fields are marked *