DATA TYPES in Python

  • Priyanshu Gupta
  • Feb 17, 2021
  • Python Programming
DATA TYPES in Python title banner

In the previous blog on Python To Represent Outputs we have learned about the representation of outputs & the cause of slow execution of python than binary.

 

Python is loved by developers due to lots of reasons. One cause is readability & fast development of code. Have you ever thought about why development in python is so fast? Let’s understand the cause of fast development in Python…

 

Why is development in Python so fast?

 

Currently, Python occupies a very large space in the Technical domain. Definitely, Python has a very large community that maintains it continuously.  Python communities are always trying to make it as simple as possible so that learning becomes easy.

 

Python contains a lot of domain-specific libraries, modules, functions which helps it to become multi-purpose.  These pre-created functions help to reduce the lines of code. For a developer, these pre-created functions become the front face while in the background there is lots of basic code working behind the scene to give the desired output.

 

Let’s move ahead with our experiences & learn things faster like Python…

 

Hands-on with Python :

 

1. Define a Variable :  

 

type(): This function used to find the identity of an entity. The class of data type in which an entity exists. It takes only a single argument.

Literals: Data itself called Literals. It is fixed. It is an Independent Entity.

 


print(786)
786


type(786)
<class 'int'>

 


print("Priyanshu Gupta")
Priyanshu Gupta

type("Priyanshu Gupta")
<class 'str'>

Variables: Variables are like containers for data. They have their own identity due to the type of data that they contain. Type of Data changed then the identity also changed. It is a Dependent Entity.


print(C)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'C' is not defined

As you can see C has no identity. We need to define its identity to operate it.

 

Assignment Operator :   (Variables = Literals) 

 

It is represented using the “ = ” sign. To define variables with Literals, we use it. 

 

(For different types of operations there are different types of operators in python. Don’t worry we will learn them later.)

 

Example: An Entity like 786, “Priyanshu Gupta” is fixed data called Literals while C can represent anything like Constant in Mathematics, Speed of Light in Physics called Variables.


C="Priyanshu Gupta"
print(C)
Priyanshu Gupta


type(C)
<class 'str'>

Here, C got an identity of Class String. It proves that  An Entity must have an identity of a Class of Data type to be operated. 


C=786
print(C)
786

type(C)
<class 'int'>

 

Let’s move to Classes of Data Types...Here,  the identity of C is changed in another integer type of data. It proves the variable nature of this type of entity.

 

2. Class of Data Types :

 

Every data belongs to a specific group. Every operation in every Class may not be performable. It will be more clear to you while we will learn about operators.

 

Integer : 

Mathematically, an integer is a number without a fractional part.  


a=3
type(a)
<class 'int'>

Example:

Convert Binary, Octal & Hexadecimal Numbers into Decimal Number:

 

1. Binary (0b) or (0B):

 

Binary numbers lie between 0 to  1.  Here, 0b is a converter.10101 = 


a=0b10101
print(a)
21

type(a)
<class 'int'>

2. Octal   (0o) or (0O):

 

Octal numbers lie between 0 to  7.  Here, 0o is a converter.


a=0o420
print(a)
272

type(a)
<class 'int'>

3. Hexadecimal (0x) or (oX):

Hexadecimal numbers lie between 0 to 15, where Here, 0x is a converter.


a=0x7AF
print(a)
1967

type(a)
<class 'int'>

Float :

Mathematically, a float is a number with a fractional part but Python only needs a single dot (.) to treat a number as float.


a=3.0
type(a)
<class 'float'>

 

Example:

1. Scientific Notations :  (E) or (e)

In calculators, many times you see “E” between digits. It appears when a very large number appears in calculations. E represents base 10 of forwarding Power digit.Light Speed = 300000000 m/s = 3E8 m/s  


a=3E8
print(a)
300000000.0

type(a)
<class 'float'>


 

String : 

In simple words, String is a pair of characters. There are 3 ways to represent an string .  Everything between quotes is data.

 

Example :

1. Single Inverted Comma :  (‘...’)

It can be called as Single quotes. 


a='KingsMan'

print(a)
KingsMan

type(a)
<class 'str'>


2. Double Inverted Comma (“...”)

 

It can be called as Double quotes.  


a="KingsMan"

print(a)
KingsMan

type(a)
<class 'str'>

 


Why do we need Single quotes & Double both?

 

Because when a need appears to use quotes between strings then programs get confused about start & end of strings. To resolve it we can use 2 ways 

  1. Use Double in Single quotes & Single in Double quotes.

  2. Use Escape Character \ before Internal quotes.

 


print("2020 brings "COVID" as Evil. ")

  File "<stdin>", line 1

    print("2020 brings "COVID" as Evil. ")

                            ^

SyntaxError: invalid syntax


print("2020 brings 'COVID' as Evil. ")

2020 brings 'COVID' as Evil.


print("2020 brings \"COVID\" as Evil. ")

2020 brings "COVID" as Evil.


print('2020 brings 'COVID' as Evil. ')

  File "<stdin>", line 1

    print('2020 brings 'COVID' as Evil. ')

                            ^

SyntaxError: invalid syntax


print(" 2020 brings 'COVID' as Evil. ")

2020 brings 'COVID' as Evil.


print('2020 brings \'COVID\' as Evil. ')

2020 brings 'COVID' as Evil.

 


 


  

3. Multiple Inverted Comma (“””...”””)

To pass Multiple lines of strings in a single function we use Triple Quotes.

 


a="""KingsMan

Priyanshu

Gupta"""


print(a)
KingsMan
Priyanshu
Gupta


type(a)
<class 'str'>


Boolean (True-False) :

 

Boolean values decide the flow of programs to move in a direction. Only things that matter in boolean is True & false. True can be represented as 1 and False as 0 in a decision making result.


a=True
print(a)
True


type(a)
<class 'bool'>


As you can see that the only 2 strings which can be used as values without any single, double or triple quotes are True & False in Python. 


a=False
print(a)
False


type(a)
<class 'bool'>


Conclusion

 

Development in Python is fast & don’t misconceptualized it with Speed of execution.

 

We will clearly understand that development depends on how developers can write source codes for a Task.   In Python Line of codes are definitely less than others for the same task.

 

To deal with various types of data we have learned about different classes of data types. It will help us to recognize the data as well as which type of operations we can perform on it. Python has a lot of classes of data types. But we have read a few classes which have a major role, rest of the classes we will recognize where it is needed. 

 

“ Python is Smart who moves ahead with the experience of others rather than researching everything from the start. ”

0%

Comments