Variables and datatype in Python

Variables and datatype in Python

Hello Friends, I am Nirav. And in this tutorial I going to show you how to use Variables and Datatype in python..
So, Let's get Started



1.Data types

In python mainly three data types are used
          1) Integer                              #Numerical values
          2) String                               #Group of Characters
          3) Float                                #Point Values
          4) Boolean                           #Either true or false

2. Variables

What is variable ?

An element, feature, or factor that is liable to vary or change.

Why we use variables ?

           Every variable has a name, called the variable name, and a data type. A variable's data type indicates
what sort of value the variable represents, such as whether it is an integer, a floating-point number, or a character. The opposite of a variable is a constant. Constants are values that never chage.

Declaration of Variables

          name = "ABC"                                                            #String
          number = 10                                                                #integer
          number2 = 5.5                                                             #float


Type Conversation in data type

Why we use type conversion ?

When we wants to convert data form in one form to another form than we have to use this data conversation.
Below are some Examples.
FunctionConverting what to whatExample

int()

string, floating point → integer
>>> int('2014') 
2014 
>>> int(3.141592)
3 

float()

string, integer → floating point number
>>> float('1.99') 
1.99 
>>> float(5)
5.0 

str()

integer, float, list, tuple, dictionary → string
>>> str(3.141592) 
'3.141592' 
>>> str([1,2,3,4])
'[1, 2, 3, 4]' 

list()

string, tuple, dictionary → list
>>> list('Mary')    # list of characters in 'Mary' 
['M', 'a', 'r', 'y'] 
>>> list((1,2,3,4))   # (1,2,3,4) is a tuple
[1, 2, 3, 4] 

tuple()

string, list → tuple
>>> tuple('Mary')
('M', 'a', 'r', 'y') 
>>> tuple([1,2,3,4])   # [ ] for list, ( ) for tuple
(1, 2, 3, 4) 


Comments

Popular posts from this blog

Recursion and lambda function in python

Loop Control Statement

Boolean Expression and If..else Condition