×

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

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

_name_ in Python

Introduction: The code at level 0 indentation is to be performed when the command to run a Python program is supplied to the interpreter because Python does not have a main() function...

4 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Python Dictionary popitem() method

Python Dictionary popitem() method The dictionary.popitem() method in Python removes the item that was last inserted into the dictionary. Syntax dictionary.popitem() Parameter NA Return This method returns an arbitrary element (key, value) pair from the given dictionary...

1 minute read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

Python coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.