Topics

Data Types in Python

Welcome to another tutorial, here you will know more about the data types in Python. When working with features in programming, you will understand it better with more practice.

 

Why Python? 

Just as the name implies; programmers are provided with different ways of representing data, such as a person's name, marks world population, and so on, so anything that holds raw information is referred to as data type. There are numerous data types the python compiler understands, it is quite similar to the way computers understand only binary numbers (0 and 1).

 

Features of Python 

There are about 5 types of Data.

  • Numbers
  • None
  • Sequences
  • Sets
  • Mappings

Below are some examples, so you can see how the different data types are used:

#include<iostream>
using namespace std;
int main(){
    int x;
    cin >> x;
    return 0;
}

 

Numbers

As it was used with mathematical operators, in function, with variables, and in input-output tutorials. You can recall the previous tutorials mentioned above, we discussed integers and floating-point numbers, but in this session, we will learn a new thing, which is the difference between them.

Numbers are further divided into 3 types. They are discussed briefly.

import java.util.Scanner;
class Test{
    Scanner input = new Scanner(System.in);
    public static void main(String args[]){
        int x;
        x = input.nextInt();
    }
}

 

Integers and Boolean

This type of number consists of integers such as 1,2,3,…0,-1,-2,-3,…; and the Boolean values which are true or false(i.e. 1 or 0 ).

Floating Point Numbers

This type of number deals with numbers that have decimal point values or real numbers. E.g.,2.3, 7.22434 are floating-point numbers.

 

Complex Numbers

Python has a data type for complex numbers, which is not the same as other programming languages. Let's try it by typing 1+2j or 1+2J in our IDLE, it will return an error because it is a perfectly valid complex number in Python. In our normal mathematics, we use iota(or i) to represent complex numbers, however, python opted for j, rather than i. 

 

None

Let’s imagine a situation where we don’t want the variable to have any value. Now, what is the solution to this? It is because we cannot use 0, even though it is a number. Therefore, this is the point where none comes in

 

+ Sequence

A Sequence can be seen as an ordered collection of items, indexed by positive integers. Also, it is a combination of mutable (i.e. a variable whose value can be changed) and immutable (i.e. a variable whose value cannot be changed) data types. Python language has three types of sequence data types, and they include the following:

  • Strings
  • Lists
  • Tuples

 

String

A string is an ordered sequence of letters or characters. It is primarily enclosed in single quotes (' ') or double quotes (" "). However, these quotes surrounding the string are not part of the string, but they notify the compiler about where the string begins and ends. A string can have any character or sign, with space in them, and they are immutable. For instance, a string with length 1 represents a character in python. Just as you have learned in the previous tutorial, strings help to store and implement literature words in Python, e.g. “Saharsh”, ‘saharsh’, “123”, ‘123’ are regarded as strings.

 

Lists

In Python, a List is a sequence of values of any type. The values in the list are known as items or elements. Also, they are mutable and indexed or ordered. It must be enclosed in square brackets[ ]. Take for example, [3,2,4,5,6] or ["Python", "Java", "C++"] are all lists. Therefore, a list is numbers or strings that are kept together inside the square bracket, and sequentially. Every element in a list can be accessed by just using its index number.

 

Tuples

A Tuple is a sequence of values of any type and is indexed by integers. They are enclosed in curved brackets ‘( )'. They are quite similar to lists, but they are immutable, and therefore, if you assign values to them, the values cannot be changed. However, it is faster in implementation when compared to the list.

 

Sets

This is an unordered collection of values of any type with no duplicate entry. Sets are immutable, that is, values cannot be changed after the mean is assigned.

 

Mapping

In Python, Mapping is an unordered and mutable data type. However, Dictionaries fall under Mappings.

 

Dictionaries

Dictionaries are a data type that can store any number of python objects. They typically store key-value pairs, that can be accessed using the key. Dictionaries is enclosed in curly brackets ‘{ }’.