DataTypes in Python Part 1

1

NOTE: There are a few but true information is available.



Datatype in Python


The different - different types of data are called datatypes in programming(python). There are many datatypes in python.




Table Content.




int


int is a datatype in python which is used to store numbers in a variable. A interger will be positive, negative or zero. python's int has no maximum or minimum value like other programming languages, means you can store extermely large or small interger. Example:


PYTHON
1a = 719        # "a" is a variable which contains "719" as an integer.
2b = -786       # "b" is a variable which contains "-786" as an interger.
3
4c, d = 15, 0   # "c" contains "15" and "d" contains "0".
5    



float


float is a datatype in python which is used to store floating point numbers (decimal point numbers) in a variable. It is almost same as int but it can store numbers with decimal point. Example:


PYTHON
1a = 1.2 
2b = -8.9
3c = 0.0
4    



str


str (string) is a datatype in python which is used to store strings in a variable. Means sting is datatype with can store anything including alphabets, numbers, characteres etc. A string is a immutable, means once a string is created it cannot be changed. when you are trying to change a string it will be create a new string in memory.

It represents a sequence of characters enclosed within either single quotes ('), double quotes ("), or triple quotes (''' or """).


Example:


    a = "Hello"


    Where.

  1. a is a variable (contains the string).
  2. "Hello" is a string.

Their are many way to create a string.

  • Single quates string:
  • a = 'Hello World'

    You can generate a string including double quates like this:

    a = 'Hello' World'

    if you want to add a single quate in a string than you need to create a string with double quates, otherwise this will through an error.

  • Double quated string:
  • a = "Hello World"

    You can generate a string including single quate like this:

    a = "Hello' World"

    if you want to add a double quate in a string than you need to create a string with single quates, otherwise this will through an error.

  • Multi-line string:
  • a = '''Hello world this is my string. Hello world this is my string. Hello world this is my string. Hello world this is my string. Hello world this is my string. Hello world this is my string. '''

    In the above methods you cannot create a string in multiply lines.

    The multi-line string is only allowed in triple quates no matter it is single to double.

PYTHON
1a = 'Hello world'
2b = "Hello World"
3c = 'Hello" world'
4d = "Hello' World"
5e = '''Hello World
6        this is a "multi-line" string'''
7f = """Hello World
8        this is a 'multi-line' string"""
9    



Bool


bool is a datatype in python which is used to store truth value in a variable. The truth value is in the form of True or False.


PYTHON
1a = True
2b = False
3    



List


list is a datatype in python which is used to store multiple types of data in a single variable. A list is ordered, mutable, and Heterogeneous.. Square brackets are used to create a list.


PYTHON
1a = ["ABC", 719, True, 12.95]

A list can contains another list called nested list.


PYTHON
1a = ["ABC", [12, 854]]

you can access this list's data using indexing.


  • Mutable a list is Mutable which means you can add, remove or change its elements.
  • Heterogeneous a list is a Heterogeneous which means it can store different - different types of data in a single variable.
  • Ordered a list is ordered means you can its elements by the indexing, the indexing is started from 0 and going so on as your list's elements.
  • PYTHON
    1a = ["ABC", 719] # this list contains two elements first is "ABC" 
    2as a string and second is 719 as a int.
    3
    4# you can access this data by using:
    5# a[0] # which means you want to access the first 
    6element of list contains by variable "a".
    7
    8# use print function
    9print(a[0]) # this will print "ABC".
    10print(a[1]) # this will print 719.
    11    

    A list can contains another list called nested list.

    You can access these element by using indexing of indexing.


    PYTHON
    1a = ["ABC", [12, 854]]
    2print(a[1][0]) # it will print 12.
    3print(a[1][1]) # it will print 854.
    4    



Tuple


Tuple is a datatype in python which is ordered, indexed, and immutable. it can store different - different types of data, it is almost same as list but it is immutable and list is mutable.

Once a tuple is created you cannot modify it's elements such as add, remove, or change.

A tuple is enclosed with round brackes.

  • Ordered a tuple is ordered, which means every element in a tuple is asigned within order.
  • indexed a tuple is indexed, which means you can access its elements by indexing.
  • immutable a tuple is immutable, which means you cannot modify (add, remove, or change) its elements after its creation.

Example:

PYTHON
1a = (12, "ABC")



Set:


A set is a datatype in python which is unordered, unchangeable, and unindexed. it can store different-different types of data, it is almost same as list or tuple, with minner differences.

A set is enclosed with curly-braceses.


  • Unordered a set is a unordered, which means the elements in a set has no any asigned order
  • Unchangeable a set is a unchangeable, which means you cannot change the elements of set after its creation.
  • unindexed a set is a unindexed, which means indexing is not working in the case of set.

Example:

PYTHON
1a = {719, "GS"}


3