What are Python Data Types? Day – 3

Published by Anshul Verma on

Data types are an essential concept in the python programming language. In Python, every value has its own data type. The classification of data items or to put the data value into some sort of data category is called Data Types in Python. It helps to understand what kind of operations can be performed on a value.

In the Python Programming Language, everything is an object. Data types in Python represents the classes. The objects or instances of these classes are called variables. Let us now discuss the different kinds of data types in Python.

https://intellipaat.com/mediaFiles/2019/02/pyhon3.png

Built-in Data Types in Python

  • Boolean Type: bool
  • Set Types: frozenset (in Python 3.9) , set
  • Mapping Type: dict
  • Sequence Types: range, tuple, list
  • Numeric Types: complex, float, int
  • Text Type: str
  • Binary Types: memoryview, bytearray, bytes

Python Numbers

We can find complex numbers, floating-point numbers, and integers in the category of Python Numbers. Complex numbers are defined as a complex class, floating-point numbers are defined as float, and integers are defined as an int in Python. There is one more type of datatype in this category, and that is long. It is used to hold longer integers. One will find this datatype only in Python 2.x which was later removed in Python 3.x.

“Type()” function is used to know the class of a value or variable. To check the value for a particular class, “isinstance()” function is used.

  • Integers:
    • There is no maximum limit on the value of an integer. The integer can be of any length without any limitation which can go up to the maximum available memory of the system.
  • For example,
  • 1| x = 200
  • 2 | y = 424
  • 3 | z = 488
  • Integers can look like this:
    • >>> print(123123123123123123123123123123123123123123123123123 + 1)

123123123123123123123123123123123123123123123123124

  • Floating Point Number:
    • The difference between floating points and integers is decimal points. Floating point number can be represented as “1.0”, and integer can be represented as “1”. It is accurate up to 15 decimal places.
  • For example,
  • 1 | x = 11.55
  • 2 | y = 13.45
  • 3 | z= 55.66
  • Complex Number:
    • “x + yj” is the written form of the complex number. Here y is the imaginary part and x is the real part.
  • For example,
  • 1 | y = 12 + 6j
  • 2 | x = 15 +7j
  • 3 | z = 77 + 88j

2. Python List

An ordered sequence of items is called List. It is a very flexible data type in Python. There is no need for the value in the list to be of the same data type. The List is the data type that is a highly used data type in Python. List datatype is the most exclusive datatype in Python for containing versatile data. It can easily hold different types of data in Python.

It is effortless to declare a list. The list is enclosed with brackets and commas are used to separate the items.

A list can look like this:

>>> a = [5,9.9,’list’]

One can also alter the value of an element in the list.

Method Name and its Property

  • reverse() – returns the reversed list
  • sort() – sorts the list
  • remove() – removes the item with the specified value
  • pop() – removes the element from the specified position
  • index() – returns the index of the element
  • count() – returns the number of elements of the specified value
  • extend() – add the elements of the List to the end of the current List
  • copy() – returns a copy of the list
  • clear() – removes all the elements from the list

3. Python Tuple

A Tuple is a sequence of items that are in order, and it is not possible to modify the Tuples. The main difference list and tuples are that tuple is immutable, which means it cannot be altered. Tuples are generally faster than the list data type in Python because it cannot be changed or modified like list datatype. The primary use of Tuples is to write-protect data. Tuples can be represented by using parentheses (), and commas are used to separate the items.

Tuples can look like this:

>>> t = (6,’tuple’,4+2r)

In the case of a tuple, one can use the slicing operator to extract the item, but it will not allow changing the value.

4. Python Strings

A String is a sequence of Unicode characters. In Python, String is called str. Strings are represented by using Double quotes or single quotes. If the strings are multiple, then it can be denoted by the use of triple quotes “”” or ”’. All the characters between the quotes are items of the string.

One can put in as many as the character they want with the only limitation being the memory resources of the machine system. Deletion or Updation of a string is not allowed in python programming language because it will cause an error. Thus, the modification of strings is not supported in the python programming language.

A string can look like this:

>>> s = “Python String”

>>> s = ”’a multi-string

For example:

name = ‘python’

name[5]

#this will give you the output as ‘o’

The nature of the string is immutable because the string cannot be changed after it is once replaced.

Command-line input for strings

1 y = input()

2 | print( ‘bye’ , y)

Operations using strings

1 | name = ‘python’

2 | name.upper()

3 | #this will make the letters to uppercase

4 | name.lower()

5 | #this will make the letters to lowercase

6 | name.replace(‘p’) = ‘P’

7 | #this will replace the letter ‘p’ with ‘P’

8 | name[2: 5]

9 | #this will return the strings starting at index 2 until the index 5.

Strings are also immutable like tuples and items can be extracted using slicing operators [].

If one wants to represent something in the string using quotes, then they will need to use other types of quotes to define the string in the beginning and the ending.

Such as:

>>> print(“This string contains a single quote (‘) character.”)

This string contains a single quote (‘) character.

>>> print(‘This string contains a double quote (“) character.’)

This string contains a double quote (“) character.

5. Python Set

The Collection of Unique items that are not in order is called Set. Braces {} are used to defined set and a comma is used to separate values. One will find that the items are unordered in a set data type.

clear() clears the items from a set
copy() returns the copy of the set
difference() returns a set with the difference between the two sets
isdisjoint() returns if the sets have an intersection
issubset() returns if the set is a subset
symmetricdifference() returns a set with the symmetric difference
update() update the sets with union of the set

Duplicates are eliminated in a set and set only keeps unique values. Operations like intersection and union can be performed on two sets.

Python set will look like this:

>>> a = {4,5,5,6,6,6}

>>> a

{4, 5, 6}

The slicing operator does not work on set because the set is not a collection of ordered items, and that is why there is no meaning to the indexing of set.

6. Python Dictionary

Dictionary is a type of python data type in which collections are unordered, and values are in pairs called key-value pairs. This type of data type is useful when there is a high volume of data. One of the best functions of Dictionaries data type is retrieving the data for which it is optimized. The value can only be retrieved if one knows the key to retrieve it.

Braces {} (curly brackets) are used to define dictionaries data type in Python. A Pair in the dictionary data type is an item which is represented as key: value. The value and the key can be of any data type.

Python Dictionary can look like this:

>>> d = {3:’key’,4:’value’}

pythondictionary = { ‘tensorflow’ : ‘ ML’, ‘data’ ” ‘ python’ }

copy() returns a copy of the dictionary
clear() clears the dictionary
items() returns a list containing tuple of key value pairs
keys() returns a list containing all the keys
update() updates the dictionary with all the key-value pairs
values() returns a list of all the values in a dictionary
setdefault() returns the value of a specified key

7. Boolean Type

There can be only two types of value in the Boolean data type in Python, and that is True or False.

It can look like this:

>>> type(True)

<class ‘bool’>

>>> type(False)

<class ‘bool’>

For example,

1 | num = 6 > 2

2 | >>>num is boolean variable

3 | type(num)

4 | >>>the output will be boolean

5 | print(num)

6 | >>>this will print true

The true value in the Boolean context is called “truthy”, and for false value in the Boolean context, it is called “falsy”. Truthy is defined by the objects in boolean, which is equal to True, and in the same way, Falsy is defined by the objects equal to falsy. One can also evaluate Non-Boolean objects in a Boolean context.

Definition of Variables in Python

The Values in Data Type and Variables keep varying. The values are stored in the memory location of a variable in a programming language. According to the specifications, the value stored can be changed.

When a value is allocated to a variable, a python variable is declared. There is no need to give any extra command to create a variable in Python. Let us look at the regulations and rules to create a variable and how its declaration is made.

Definition and Declaration of Variable

There is no need to give any command for the declaration of a variable in Python other than just providing a value. So, a variable is implicitly declared after the assignation of value.

Rules for Declaration of Variable

  1. No Special Characters Allowed:
  2. There is no special character allowed in a variable.
  3. Only Underscores and Alpha-Numeric characters can be allowed in a variable.
  4. The value in the variable of Python is case sensitive.

Reserved words (keywords) cannot be used as identifier names.

and def False import not True
as del finally in or try
assert elif for is pass while
break else from lambda print with
class except global None raise yield
continue exec if nonlocal return

Writing “Hello World” Program in Python :

Print function is used to display any message..

 

In order if you wish to know more about any function, just type:

help(function_name) —>>> help(print) # for the working of function

&

help(function()_name) —>>> help(print()) # for the object like function as shown :

Following the same way, you can get detail for each and every keyword, function, data type, and even variables. That’s all about Data types in Python.

That’s all for today. In our next blog, we’ll be learning and doing some basic problems and using conditional statements…..

Till then,

Stay Safe, Stay Happy & Keep Coding

IF YOU WANT TO KNOW ABOUt DATA SCIENCE click here.

CONNECT WITH THE AUTHOR ON LINKEDIN.

VISIT OUR WEBSITE BRIGHTERBEES FOR MORE INTERESTING STUFFS ABOUT LEARNING.


0 Comments

Leave a Reply

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

STAY CONNECT WITH US