• Python Data Types

    Python Data Types

    In this article, we will see “Python Data Types”. It will help you in implementing different data types in Python.

    Data types in Python

    Each and every value in Python has a datatype and these data types determine what operations can be performed on that particular data.

    In Python, everything is an object. Data types are classes and variables are objects (instance of the classes)

    Python has various built-in data types. Let’s see the most important data types of Python here. 

    Python Numbers (Numeric)

    Python has three categories of numbers such as integer, float, and complex number.

    type() function is used to find which class a variable or a value belongs to.
    isinstance() function is used to find if an object belongs to a particular class.

    Integers – int – Positive or negative whole numbers (without a fractional part). There is no limit for integers. The only limit is due to memory availability. A floating-point number is accurate up to 15 decimal places. Example: 1 is an integer.
    Floating-point numbers – float – Any real number with a floating-point representation (with a fractional part). Example: 1.0 is a float-point number.
    Complex numbers – complex classes in Python – x+yj is represented as a real and imaginary component. x and y are floats and j is -1(square root of -1 called an imaginary number). 

    Output:

    imaginary number)

    Python List

    Python list is an ordered sequence of data items. 

    All the data items in a list not necessarily of the same type. Data items in the list are separated by commas and enclosed within brackets [].

    Index in python starts from 0. Let’s use the slicing operator [] to extract a data item or a range of data items from a list. 

    Output

    Lists are mutable whereas Strings are not mutable. Learn Strings in Python here. 

    In the lists, we can change the value of elements. 

    Output

    Python Tuple

    Python tuple is an ordered sequence of data items. It is the same as List but tuples in python are immutable. Once you create a tuple you cannot modify it. 

    All the data items in a tuple not necessarily of the same type. Data items in the tuple are separated by commas and enclosed within parentheses ().

    Tuples are used to write-protect data.

    Tuples are faster than lists because tuples once created cannot be changed dynamically.

    We can use the slicing operator [] to extract items but we cannot change its value.

    Output

    Python Strings

    In Python, Strings are a collection of one or more characters enclosed by either single quotation marks or double quotation marks or triple quotation marks. Python treats both single and double quotes as same. Multiline Strings can be denoted using triple quotes, ”’ or “””.

    Output

    Let’s apply a slicing operator [] to the strings as what we did for List & Tuple. 

    Note: Strings are immutable. 

    Output

    Python Set

    We have seen an ordered sequences like List & Tuple. Now we will see the unordered collection of objects. 

    Set is an unordered collection of objects which are unique. 

    All the data items in a Set not necessarily of the same type. Data items in the Set are separated by commas and enclosed within braces {}.

    Output

    We can perform mathematical set operations like union, intersection on two sets, etc., 

    The set has unique values and eliminates duplicates. 

    Output

    Note: As mentioned earlier, sets are the unordered collection. So the slicking operator [] doesn’t work with Set in Python.

    Python Dictionary

    Dictionary object is an unordered collection fo data in a key:value pairs. A collection of such pairs is enclosed in curly brackets {} with each item being a pair in the key:value form.

    key and value can be of any data type.

    We use Dictionary in python when we have a huge amount of data. 

    Dictionary in python is used to retrieve the data but we must know the key to retrieve the value.

    To retrieve the value we use a key.

    Output

    Conversion between data types

    We can use type conversion functions like int(), float(), str(), etc., to convert between different data types. 

    Conversion from float to int:

    Conversion from int to float: 

    Convert one sequence to another.

    Convert to a dictionary: Each element must be a pair

    Also Read:

    Sharing is caring.

    Share on facebook
    Facebook
    Share on twitter
    Twitter
    Share on linkedin
    LinkedIn

    Like This Post?

    We have a lot more where that came from?

    We only send really good stuff occasionally, promise.

    Rajkumar SM

    Leave a Comment

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

    Share on facebook
    Share on google
    Share on twitter
    Share on linkedin
    Share on whatsapp

    Recent Posts:

    Scroll to Top
    API Testing eBook

    DOWNLOAD FOR FREE