Learn Python - Bit by bit #4

in steem •  7 years ago 

python-logo-master-v3-TM.png
<< Learn Python - Bit by bit #3 - input()

Today we are gonna talk about Data Types. But I told you guys that I will be teaching you python bit by bit. So I am gonna break down Data Types into subtopics. And today you are going to learn:

Data Types - Numbers

The number category deals with 3 types of data:

  • int (integer)
  • float (fraction)
  • complex (numbers expressed as a sum of the real part and an imaginary part)

Don't scratch your head on the complex number stuff. You probably will not use it unless you are a mathematician or physicist. And if you are one of them then you will love python.

In python, you don't need to specify the data type of the variable and that is why python is categorized as a dynamically-typed language. It will detect the type by looking at the value. Since dynamically typed languages detect type automatically if we need a way to check the type of data manually, in order to make sure everything is going according to our plan (just in case) python provides a handy function called type(). All we need to do is pass the value into the type() function.

int
Go to repl.it and choose Python3 as the language. Inside main.py type:

num = 5
print(type(num))

Output: <class 'int'>

float

num = 5.0
print(type(num))

Output: <class 'float'>

complex

num = 5+2j
print(type(num))

Output: <class 'complex'>

In the complex number 5+2j, 5.0 is the real part and 2.0 is the imaginary part. You need to use the letter j to symbolise the imaginary part.

And as I said, "don't scratch your head on the complex number stuff" you probably won't use it. And when the need arises you will be skilled enough to understand how it works.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!