Datatypes in Python

Datatypes in Python

In programming, Datatype means the type of data, user stores in a variable or memory. In every programming languages datatypes are almost same. The data which you want to process can be of many type. It can be a numeric value or alphabetic value or a decimal value etc. In this article we are going to talk about datatypes in Python.

There are mainly 2 classifications of datatypes

  • Built-in datatypes
  • User-defined datatypes

The datatypes which are already available in a programming language are call Built-in datatypes and the datatypes which are created by the programmers are called User-defined datatypes.

Built-in datatypes in Python

  • Numeric Types
  • Sequences
  • Sets
  • None

Numeric Types
As the name suggests there are datatypes associated to numbers. There are mainly there types in it

  • int (Integer)
  • float (Float)
  • complex (Complex Numbers)

int

  • The int datatype is used for representing Integer numbers. An Integer number is a number that can be written without fractional or decimal parts.
  • Example: 1,2,3,4,-7,-20,-30 are all examples of Integer numbers.
  • Basically Integer numbers are a whole valued positive or negative number.

float

  • The float datatype is used for representing numbers with a floating point or decimal.
  • Example: 10.2, -5.6, 11.01, 40.004 are all examples of Floating points numbers.

complex

  • complex datatype is used for representing complex numbers. A complex number is a combination of a real part & an imaginary part. Complex numbers are expressed in the form or a + bi, where a and b are the real parts and i is for imaginary part representing an imaginary number.
  • Example: 4+3i, 0.1 + 5.2i etc

Storing data in a variable in other programming language like Java:

int a = 30;
float b = 10.2;

Here in the above code, as you can see 'a' and 'b' are the variable names and you have to declare the datatype which you are going to store in a variable. Where 'a' is an Integer type and 'b' is float type for floating point numbers.

Well that's not the case in Python.

Storing data in a variable, Python:

a = 30
b = 10.2
c = 4 + 6.1J

Python's Interpreter automatically detects the type of data stored in a variable.You see? It's that easy!

Sequences

  • str
  • bytes
  • bytesarray
  • list
  • tuple

str

  • In Python, str represents string datatype. A string is a group of characters. Anything within single or double quotes in Python is recogized as a string in Python
    str1 = "Hello World!"
    str2 = 'Hello World!'
    
    In the above code, the text Hello World will be considered as a string in Python.

bytes

  • The bytes datatype represents a group of byte numbers just like an array does.
  • A byte number is any positive integer from 0 to 255.
  • bytes cannot store negative numbers. For example:
    bytes_list = [1,2,3,4,5]
    a = bytes(bytes_list)
    print(a[0])
    

bytearray

  • bytearray is similar to bytes. Just the difference between bytearray and bytes is that bytes cannot be modified but bytearray can be modified.
list1 = [1,2,3,4,5]
a = bytearray(list1)
print(a[0])

a[0] = 10  #Updates element present at 0th position i.e 1 to the value 10
a[1] = 20  #Updates element present at 1st postition i.e 2 to the value 20

list

  • List in Python are similar to Arrays. A list represents a group of elements. The only difference between a list and array is that list can store heterogeneous elements.
  • Lists are dynamic in nature which means they can grow dynamically in memory.
list1 = [10,20,"Prasanna","John",30.5]
print(list1[0])
  • The above code will print the first element in list1 i.e 10

tuple

  • A tuple is similar to a list. A tuple can contain group of elements which are of heterogeneous type.
  • The key difference between a tuple and a list is that a tuple cannot be modified.
  • Tuples are immutable in nature that means a tuple is just like a read only list where you cannot make any modifications.
tuple1 = (10,20,"Prasanna",30.5)
print(tuple1[1])
  • The above code will print the second element in tuple1 i.e 20

Sets

  • A set is an unordered collection of elements just like the set we see in Mathematics.
  • A set can only contain unique values unlike lists & tuples.
  • Also the order of the elements is not maintained in sets.
set1 = {10,20,30,40,50}
print(set1) #Prints the set

Output: {10,30,50,40,20}

  • As you can see the order might be maintained or might not be maintained in sets.
  • You can also modify elements in a set.
  • There is also something called as a frozenset

frozenset

  • A frozenset is similar to a set but the difference between set and frozenset is that you cannot modify the elements in a frozenset.
    s = {10,20,30,40}
    fs = frozenset(s)
    print(fs)
    
    Output: frozenset({40, 10, 20, 30})

None Datatype

  • The None datatype represents null value.
  • In Boolean value, None datatype represents False.
    a = None
    print(a) #prints None as output
    b = bool(a) #converts value of a in boolean
    print(b) #prints false because None represents false value in boolean
    

Conclusion

This was just an overview of the different datatypes in Python. We will dive in detail into these datatypes in upcoming articles.Thank you! :)