×

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

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Compound Interest GUI Calculator using PyQt5 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...

6 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 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.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.

Python memoryview() Function

Python memoryview() Function The memoryview() function in Python returns a memory view object from a specified object. Syntax memoryview(obj) Parameter obj: This parameter represents a Bytes object or a bytearray object. Return This function returns a “memory view” object...

1 minute read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Run exec python from PHP

PHP (which is a recursive abbreviation for PHP Hypertext Preprocessor) is one of the most broadly involved web improvement innovations on the planet. PHP code utilized for creating sites and...

4 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.