Tuesday, 13 August 2019

Built in core datatypes in python | What are the basic data types in Python 3?



 Built in core datatypes in python | What are the basic data types in Python 3?


Python supported built-in core datatypes :
  1. Numbers
  2. Strings
  3. List
  4. Tuple
  5. Dictionary

1.Numbers: Number data type is used to store numeric values in python. It can be of
  
-Integers
-Floating Point Numbers
-Complex Numbers
  • Integer: Types of integers in python

                                              - Integers(signed)
                                              - Boolean


                Integer: Integers are whole numbers such as 6,34,562,0 etc. They have no floating point
                values. It means, in python integer values represented as numbers with no decimal point.                      Integers can be positive (+12,390) and negative (-56, -7).
                                  
                              Integers(signed): Integers can be of any length, it is only limited by the                                                memory available. In a signed representation, the integers can be positive as                                          well as negative.
          
     Boolean: These represent the truth values False and True. The Boolean type                  is a subtype of plain integers and Boolean values False and True behave like
      the values 0 and 1 respectively. To get the Boolean equivalent of 0 or 1, you                  can type bool (0) or bool (1) python will return False or True respectively.
               
  • Floating-Point Numbers: A number having fractional part is a floating-point number. For Example 5.67543 is a floating-point number. The decimal point numbers are floating-point numbers not integers. The number 23 is an integer but 23.0 is a floating-point number. Floating-Point  operations are usually slower than integer operations.                                          Floating-Point number can be written in two forms:
                                                                Fractional Form
                                                                Exponent Form

      * In python the floating-point numbers have double precision means the precision of 15 digits. 

  • Complex Numbers: In mathematically, a complex number is a number of the form A+Bi where i is the imaginary number, equal to the square root of -1. A Complex number is made up of both real and imaginary components. In complex number A+Bi, A and B are real numbers and i is imaginary. If we have a complex number z, where z=a+bi then a would be the real component and b would represent the imaginary component of z. For example, real component of z=4+3i is 4 and the imaginary component would be 3.               

Complex number in python
Python represents complex numbers in the form A+Bj. Python uses j or J in place of i

For example:

a=0+3.1j
b=1.5+2j

The above complex number a has real component as 0 and imaginary component as 3.1 and  b has real component as 1.5 and imaginary component as 2. Python represents complex numbers as a pair of floating-point numbers. When you display complex numbers, python displays complex numbers in parenthesis when they have a nonzero real part.



2.Strings

A string is sequence of characters and each character can be individually accessed using its index. In python, strings are stored as individual characters in contiguous location with two-way index for each location.

For example:

PYTHON

name [0] =’P’ = name [-6]
name [1] =’Y’ = name [-5]
name [2] =’T’ = name [-4]
name [3] =’H’ = name [-3]
name [4] =’O’ = name [-2]
name [5] =’N’ = name [-1]

To access the first character of string name, you will write name [0], because the index of first character is 0. You may also write as name [-6].

We can determine the length of the string using len (string).

You cannot change the individual letters of a string in place by assignment because strings are immutable and hence item assignment is not supported.

For example:

Name =’hello’
Name [0] =’p’

Here, you cannot assign individual letter.

But,

You can assign to a string another string or an expression that returns a string using assignment.

For example:

Name=’hello’
Name=’new’

3.List 

The list and tuples are Python’s compound datatypes. We have taken them together in one section because they are basically the same types with one difference. Lists can be changed/modified. List is mutable. But tuples cannot be modified or changed because tuples are immutable.
In Python, a list represents a list of comma-separated values of any datatype between square brackets.

For examples:

[1,5,6,4]
[‘a’,’e’,’u’,’i’]
[‘Meena’,45,67.5]

As lists are mutable, so you can change the item or value in a list.

Suppose

X= [1,2,3,4]
X [0] =10
X= [10,2,3,4]

4.Tuples

In Python, Tuples are represented as list of comma-separated values of any datatype within parentheses.

For example:

P= (2,4,6,7,9)
R= (‘a’,’e’,’u’,’i’)
Q= (7,8,’a’,’u’)

You cannot change, delete or add a list’s element because these are immutable.

5.Dictionary

In Python, the dictionary is an unordered set of comma-separated key: value pairs within curly-braces {}, with the requirement that within a dictionary, no two keys can be same. There are unique keys within a dictionary.

For example

Vowels = {‘a’:1,’e’:2,’i’:3,’o’:4,’u’:5}
Vowels[‘a’]
1
Vowels [‘u’]
5

Here ‘a’,’e’,’i’,’o’,’u’ are the keys of dictionary vowels; 1,2,3,4,5 are values for these keys respectively.



    



No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...