Variables in python

Variables are a fundamental concept in programming. Variables are used to store data. Variables are stored in a reserved memory location.
Variables in Python are case-sensitive. That means it is sensitive to lowercase and uppercase characters. That means variables like user and USER are different. Variables can contain different data values like strings, numbers (integer, float), boolean (True or False). Variables can also contain a list, tuple, and dictionary.

You will learn the following topics of variables:

  1. Rules for declaring variables
  2. Declaring variables
  3. Redeclaring variable
  4. Concatenation of variables
  5. Deleting a variable
  6. Use of local and global variables:

Rules for declaring variables:

There are some rules to declare a variable. They are as follows:

  1. You cannot name a variable starting with a number.
  2. You must start a variable name with a letter or the underscore character.
  3. You can only use alpha-numeric characters and underscores for naming a variable.

Declaring variables:

See the following example to learn how to declare variables.

Example:

str = "Hello World"
print(str)

Output:

Hello World

Redeclaring variable:

You can assign a different value to a variable even after it is declared.

Example:

str = "Hello World"
str = "Welcome to python tutorial."
print(str)

Output:

Welcome to python tutorial.

Variables storing string, interger and boolean:

Example:

name = 'John Doe'
age = 25
height = 5.10
is_Male = True

print('User name is: ', name)
print('Age is: ', age)
print('Height is: ', height)
print('Is User genger Male ? ', is_Male)

Output:

In the above example, variable values are also printed to see them in the terminal.

User name is: John Doe
Age is: 25
Height is: 5.1
Is User genger Male ? True

Variables storing list:

lists are used to store a collection of values.

Example:

names = ['John', 'Jane', 'Jim', 'Jack']
print(names)

Output:

[‘John’, ‘Jane’, ‘Jim’, ‘Jack’]

Variables storing tuple:

tuples are used to store a collection of values. But values stored in a tuple cannot be modified.

Example:

names = ('John', 'Jane', 'Jim', 'Jack')
print(names)

Output:

(‘John’, ‘Jane’, ‘Jim’, ‘Jack’)

Variables storing dictionary:

Dictionaries are used to store information in formats of key-value pairs. Dictionaries are defined using {}. keys in dictionaries must have to be unique. Values in dictionaries can be anything like strings, numbers, boolean, etc.

Example:

user = {
    "name": "John Doe",
    "age": 40,
    "email": 'john@gmail.com'
}
print(user)

Output:

{‘name’: ‘John Doe’, ‘age’: 40, ’email’: ‘john@gmail.com’}

Concatenation of variables:

You can join multiple variables using concatenation.

Example:

str1 = "Hello"
str2 = "World"
print("Concatenated string is: ", str1 + str2)

Output:

HelloWorld

Deleting a variable:

You can delete a variable by using the del command.

Syntax:

del variable_name

Example:

username = "John Doe"
del username
print(username)

Output:

In the above example. we have created a variable named username and we have deleted it using the del command. But when we print the variable after deleting it, we get the following error.

NameError: name ‘username’ is not defined

Use of local and global variables:

Global variable:

When you want to use a certain variable for the rest of your program you have to use it by declaring a global variable.

Local variable:

When you want to use the variable in a specific function or method, you have to use it by declaring a local variable.

Example:

In the following example, we have declared total as a global variable as we may need in another function than addition(). So total variable is accessible throughout the program. In function addition(), we have declared num1 and num2 as local variables. They can be only accessed in addition() function.

def addition():
    num1 = 2
    num2 = 5
    total = num1 + num2
    print(total)

addition()
print(total)

Output:

When we print variable from the function we get value 7 but we print it outside of the function we get value 0. This is because of local and local and global variables. In the global variable total, we have set its value to 0.

7
0

Conclusion:

You have learned the following key points from variables in the python article. They are as following:

  1. Variables are used to store data types like string, number, boolean, list, tuple, and dictionary.
  2. There are some rules given above which you must follow while declaring variables.
  3. You can assign a different value to a variable even after it is declared.
  4. You can delete a variable using the del command.

See Also:

Strings in python

loops in python

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments