Variables In Python and it’s Examples

1
263
Variables In Python and it's Examples

Introduction to variables in python

Variable in python are used to store the values in a particular format instead of calling value every time we call the variable where the value was stored.

Guidelines to create a variable:

  • Make sure the name of a variable is in character format.
  • You assign a number or integer for the variable name.
  • If you want to assign a symbol to a variable name you have to give a symbol before a character.

How to Create a Variables In Python

Syntax:

Variable_Name = Value

Example:

X=5

Here we create a variable X and assign 5 to it. Now the value of X is 5

Number or Integer Data Type

A number is used to store numeric values. Python creates Number objects when variety is assigned to a variable.

Syntax:

X= Pass a value which is having an integer data type

Example:

X=101

Here we pass 101 an integer value to a variable X, Now the value of X is 101.
If You want to check the Datatype of the variable X:

Syntax:

type(name of variable)

Example:

type(x)

Output:

Int.

Character or String Datatype

The string can be defined as the sequence of characters represented in the quotation(“ ”) marks. We will use single, double, or triple quotes to define a string in Python.

Syntax:

X= Pass a value which is having a String data type.

Example:

X= ”Fireblaze”

Here we pass ”Fireblaze” a String value to a variable X, Now the value of X is ”Fireblaze”.

If You want to check the Datatype of the variable X

Syntax:

type(name of variable)

Example:

type(x)

Output:

Str (string)

Boolean Data type

This Datatype is having True and False value.

Syntax:

X= Pass a value which is having a Boolean data type.

Example:

X=True

Here we pass True a Bool value to a variable X, Now the value of X is True.

If You want to check the Datatype of the variable X

Syntax:

type(name of variable)

Example:

type(x)

Output:

bool 

List Data type

Lists are similar to arrays in C. However; the list can contain data of various types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].

Syntax:

X= Pass a value which is having a List data type.

Example:

X=[1, ’a’, True]

Here we pass[1, ’a’, True]a list to a variable X, Now the value of X is [1, ’a’, True].

If You want to check the Datatype of the variable X

Syntax:

type(name of variable)

Example:

type(x)

Output:

List

Tuple Data type

A tuple is similar to the list in many ways. Like lists, tuples also contain the gathering of the things of various data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses (). A tuple may be a read-only arrangement as we will not modify the dimensions and value of the things of a tuple.

Syntax:

X=(Pass a value which is having a list of items.)

Example:

X=(10,20,30)

Here we pass (10,20,30) a Tuple to a variable X, Now the value of X is (10,20,30).

If You want to check the Datatype of the variable X

Syntax:

type(name of variable)

Example:

type(x)

Output:

Tuple

Dictionary Data type

Dictionary is an ordered set of a key-value pair of things. It is like an associative array or a hash table where each key stores a selected value. Key can hold any primitive data type and value is an arbitrary Python object.
The items within the dictionary are separated with the comma and enclosed within the curly braces {}.

Syntax:

X={ Keys: Values.}

Example:

X= {‘A’ : 100}

Here we pass {‘A’: 100} Dictionary to a variable X, Now the value of X is {‘A’:100}.

If You want to check the Datatype of the variable X

Syntax:

type(name of variable)

Example:

type(x)

Output:

Dictionary

User Input In a Variable

In a python, we can also take the input from the user in which the user can access the data by there own choice, and while creating a variables in python and you need to add the value by the user it is possible in python.

Syntax:

input()

Example:

I am learning at fireblaze.
 'Last_name = input("Enter last_name:")
 print("last_name is: " + last_name)

Output:

Last_name is

Arithmetic Operation on Variable

Arithmetic operators are used to perform a mathematical operation between the variables in python to obtain a formatted output.

Arithmetic Operation on Variables in python

Addition of two variables

Here we are going to add two variables in python by using the addition operators.

Syntax:

Variable 1 + Variable 2

Example:

X(variable 1)=100,
 Y(variable 2)=200
 X+Y

Output:

300

Subtraction of two variables

Here we are going to subtract two variables in python by using the subtract operators.

Syntax:

Variable 1 - Variable 2

Example:

X(Variable 1)=100  
 Y(Variable 2)=50
 X-Y

Output:

50

Multiplication of two variable

Here we are going to Multiply two variables by using the multiplication operator.

Syntax:

Variable 1 * Variable 2

Example:

X(Variable 1)=100  
 Y(Variable 2)=50
 X*Y

Output:

5000    

Division of two variable

Here we are going to do a division between two variables by using the divide operator.

Syntax:

Variable1 / Variable 2

Example:

X(Variable 1)=100  
 Y(Variable 2)=50
 X/Y

Output:

2.0

Modulus of two variable

Modulus operator gives you the value which is in the remainder after division.

Variable 1 % Variable 2

Example:

X(Variable 1)=100  
 Y(Variable 2)=30
 X%Y

Output:

10

Assign Power to a variable

Here we assign power to a variable to find the power of the result of the variable.

Syntax:

Variable1 ** (int)

Example:

X(Variable 1)=8
 X**2

Output:

64

Conclusion

In this blog, you will get the knowledge to create a Variable and how to access the variable, the process to check the type of a variable, and how to perform arithmetic operations on a variable.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here