Data Type in Python

in #python7 years ago

Intro_Python.png

Previously we write our first program in Python. In this tutorial we will go through different data types available in Python.In python everything is a object on which we can perform different operation such as concatenation,addition etc. However objects's data types decide which operation is applicable on particular data types.

The list of data types are as follows:

Integer : It is 32 bit. The range is from -2^32 to 2^32-1.

Long Integer : It has unlimited precision,however it depends how much your computer memory can hold it.

Floating point number : It is 64 bit and also known as double precision.

Boolean : It hold only two value True or False.

Complex number: It has real and imaginary part and both the part is represented as floating number.An imaginary part is represented by 'j'. e.g 2+4j

String : It is sequences of unicode character.

Lists : Ordered sequences of values.

Tuples : It is also ordered sequence but with immutable value. which means once given a value you can't update it. You can create the new tuple using same but you can't update it.

Sets : It is an Unordered collection of values.

Dictionaries : It is an Unordered collections of key and value.

Now lets see how to create a variable of particular data type. Unlike other language in python there is no need to specify the type of variable. Python interpreter will automatically decide its type. That's why python is called as "Duck typing" language.

a = 1
Here variable a has value of 1 and its type by default is int.

b = 2.0
b is floating point.

Others examples are as follows.

c = True # Boolean
d = "Hello world" #String
e = ["Hello","World",2.0] #List
f = (1,"Hello",) #Tuple
g = set([1,2,3]) #Set
h = {1:"Value1",2:"Value2"}

To check the type of variable use type function in python as show below.

type(a) ##Check type of variable a

It is always better practice to check the type of the variable. It is important because if you use any API or any function which has some return value then it is better to check its type before performing any operation on that variable.

Hope you like this tutorial. We will see each data type in detail in upcoming tutorial till then "Happy Coding".

Please join the @labwork team and Up vote,follow and resteem.

Comments are always appreciated.

Sort:  

I'm giving you some extensions.

For booleans: True is an alias of 1 and False of 0. They area interchangeable for logical validations. Of course it will be treated differently sometimes in serialization (e.g. json).

For type checking, you also can check if that's an instance of a class, which is better for classes that has inheritance. I give you an example.

class Animal: pass
class Dog(Animal): pass
mypet = Dog()
print(isinstance(mypet, Animal))
# True
print(isinstance(mypet, Dog))
# True
print(isinstance(mypet, int))
# False

Where "type(mypet) is Animal" will be False and "type(mypet) is Dog" will be True, because type() does not check the inheritance.

Keep doing tutorials. :)

Thanks for the more information on type. I will cover it when I will discuss about the OOP's concept in Python.

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by labwork from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.15
TRX 0.16
JST 0.028
BTC 67779.88
ETH 2396.01
USDT 1.00
SBD 2.32