×

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose values can be changed or varied.

In this article, we will discuss how to declare a variable in python.

Declaring a variable in Python programming language is a demonstration of the steps that we discuss in the algorithms.

They are the containers that can hold values of any datatype and occupy a space in the memory.

Let us have a look at the naming convention of our variables that makes them an authentic one-

Following are the rules that we must take care of-

  1. The name of our variable can have an underscore ( _ ).
  2. The name of the variable should not contain any special character.
  3. The name of the variable should not be a keyword.

We will discuss the given examples that will help us to understand the different ways of declaring a variable.

  1. A simple assignment of values to variables.
  2. Assigning values to variables in the same line.
  3. Assigning a single value to three variables at a time.

Let us have a look at the first example where we have assigned an integer value, a float value, and a string value to the variables a,b, and c respectively.

Following program illustrates the same-

INPUT-

 #assigning values to variables
 a=2
 b=2.5
 c='Python'
 print(a)
 print("The type of a is {}".format(type(a)))
 print(b)
 print("The type of b is {}".format(type(b)))
 print(c)
 print("The type of c is {}".format(type(c))) 

OUTPUT-

How to Declare a Variable in Python?

In the output, we can observe the following things-

  1. The values of a,b and c are displayed.
  2. The data type is also printed since we have used type() here.

In the second example, we have type casted the values of a,b and c and then printed their data types.

Let us see what happens when we execute the program given below-

#Typecasting

INPUT-

 #assigning values to variables
 a=2
 b=2.5
 c='15'
 print(a)
 print("The type of a is {}".format(type(a)))
 print(b)
 print("The type of b is {}".format(type(b)))
 print(c)
 print("The type of c is {}".format(type(c)))
 #typecasting
 x=float(a)
 y=int(b)
 z=int(c)
 print(x)
 print("The type of x is {}".format(type(x)))
 print(y)
 print("The type of y is {}".format(type(y)))
 print(z)
 print("The type of z is {}".format(type(z))) 

OUTPUT-

How to Declare a Variable in Python?

In the output, we can observe the following things-

  1. The values of a,b and c are displayed.
  2. The data type is also printed since we have used type() here.
  3. The next set of values are the ones that are typecast as float and integer.
  4. We have verified their data types using type() here.

The second method that we will discuss here illustrates how we can assign values to two variables at a time.

The following program shows how we can implement this-

INPUT-

 #assigning values to variables
 a,b=4,5
 print("The sum of a and b is {}".format(a+b))
 print("The difference of a and b is {}".format(a-b))
 print("The product of a and b is {}".format(a*b))
 print("The division of a and b is {}".format(a/b)) 

OUTPUT-

How to Declare a Variable in Python?

In the output, we can observe the following things-

  1. The variables a and b hold the values 4 and 5 respectively.
  2. The expected results are displayed when the four basic arithmetic operations are applied.

In the last method, we will see how we can assign a single value to the three variables a,b and c at a time.

The following program illustrates the same-

INPUT-

 a=b=c=9
 print(a)
 print(b)
 print(c) 

OUTPUT-

How to Declare a Variable in Python?

In the output, we can observe that a,b and c hold the same value which is 9.

In the next program, we will see what are the results when we apply arithmetic operations on them.

Let’s see what happens when we execute this-

INPUT-

 a=b=c=9
 print("The sum of a and b is {}".format(a+b))
 print("The difference of a and b is {}".format(a-b))
 print("The product of a and b is {}".format(a*b))
 print("The division of a and b is {}".format(a/b)) 

OUTPUT-

How to Declare a Variable in Python?

In the output, we can observe the following things-

  1. The variables a and b hold the same value which is 9.
  2. The expected results are displayed when the four basic arithmetic operations are applied.

You can try and check the results when we apply these operations on a and c or a, b and c together.

So, here we discussed the rules for declaring a variable in Python and how we can declare it.


Related Topics

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 minutes read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

Python MongoDB Tutorial

An Introduction to MongoDB MongoDB is a document-oriented database application. It is an Open-source and platform-independent program. MongoDB is similar to few NoSQL databases that store the data in the documents...

17 minutes read.

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.

The Calendar Module of Python

The calendar module provides calendar features, including printing functions for a specific month or year. Default is the first day of the week on Monday and the last day on Sunday...

3 minutes read.

Is Python Case-sensitive when Dealing with Identifiers

Yes, Python is a case-sensitive language while dealing with identifiers. Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. It is a case-sensitive...

6 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Python hex() function

Python hex() function The hex() function in Python  converts the specified number into a hexadecimal value. Syntax hex(x) Parameter x: The parameter represents an Integer value. Return This function Returns hexadecimal string. Example 1 # Python Program explaining # the hex() function print("The...

1 minute read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.