Variables in Python

Variable is a placeholder for a data value. A variable reserved space in memory, when you create a Variable. Python does not need to define Variable explicitly unlike the other programming language such as C and C++. A variable is created when you assign value to Variable. Four different data types exist in Python.

  • integer
  • float
  • string
  • complex number

 

Let’s take an example for manipulating the value of 5 apples. For that, we need to store the value in a variable for easier manipulation. Instead of using 5 every time we can a.

a = 5      #equivalent to 5 apples
print(a+2) #adding 2 more apples 
print(a*2) #Find price of 5 apples per 2$
#Output
7
10 # price of 5 apple is 10$

Note: Python is a case-sensitive programming language.

x = 5          # integer Variable
y = "Python"   # String Variable
z = 'Pandas'   # String Variable
w = 2.5        # float Varibale
c = 1+5j       # complex number
a,b,c = 2 , 'Python', "pandas"  #assign multiple variables in one line

type()

type() function is used to get the data type of a variable.

x = 3
print(type(x))
s = "python"
print(type(s))
#Output
<class 'int'>
<class 'str'>

Integers

In Python, Integers can be of any length, it is only limited by the available memory space. Python is a case-sensitive language as a variable name a(lowercase) & A(uppercase) treated differently.

# Define a variables
a = 5
A = 10    
print("The value of variable a is {} & data type is {}.".format(a,type(a)))
print("The value of variable A is {} & data type is {}.".format(A,type(A)))
#Output
The value of variable a is 5 & data type is <class 'int'>.
The value of variable A is 10 & data type is <class 'int'>.

Float

Float define the real number with floating-point representation. It is specified with a decimal point. The float number is accurate up to 15 decimal points. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.

a = 5.0000
b = 5.000000000001
print("The value of variable a is {} & data type is {}.".format(a,type(a)))
print("The value of variable b is {} & data type is {}.".format(b,type(b)))
#Output
The value of variable a is 5.0 & data type is <class 'float'>.
The value of variable b is 5.000000000001 & data type is <class 'float'>.

String

Python defines a string variable in single quotation marks or double quotation marks. A plus ‘+’ sign is used as the concatenation of strings. Python allows slicing the string by defining the start index and end index in square [] brackets separated by a colon.

# Define the string variable
x = "Hello world"
y = 'Hello Python'

print(x,type(x))
print(y,type(y))
#Output
Hello world <class 'str'>
Hello Python <class 'str'>

Complex Numbers

Complex numbers are defined in the form of a+bj, where a is the real part and b indicate the imaginary part.

x = 1+2j
y = 5+10j
print(x,type(x))
print(y,type(y))
#Output
(1+2j) <class 'complex'>
(5+10j) <class 'complex'>

.     .     .

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Tutorials