Python Basics 2: Datatypes

Python Basics 2: Datatypes


Datatype:
Every value in Python has a datatype. The datatype is mainly the category of the data. There are basically 5 categories of datatypes; however, these categories have further classifications as well.

1.Numeric Type:
a) Integer (int): positive or negative whole numbers (without a fractional part). Example: 10,-3,10000

b) Floating point (float): Any real numbers with ‘decimal’ points or floating point representation.
Example: -3.14, 10.23

c) Complex Number: Combionation of real and imaginary number. Example: 2+3i [we don’t use this that much]

2.Boolean Type(bool): Data with one of the two built-in values True or False. Often used in the comparison operations of logical statements.
Example:

x=10
y=15
print(y>x)
#output: True
Enter fullscreen mode

Exit fullscreen mode

3.Sequence Type: A sequence is an ordered collection of similar or different data types.

a)String(str): It is a sequence of ordered characters (alphabets-lower case, upper case, numeric values, special symbols). String is represented with quotation marks (single(‘), double(”), triple(‘ ‘ ‘, ” ””)).

For example:
using single quotes (‘python’),
double quotes (“python”) or
triple quotes (”’python”’ or “””python”””)

b)List: It is an ordered collection of elements where the elements are separated with a comma (,)and enclosed within square brackets [].

The list can have elements with more than one datatypes.

For example:
i) list with only integers; x= [1, 2, 3] and

ii) a list with mixed data types; y= [110, “CSE110”,12.4550, [], None]. Here, 110 is an integer, “CSE110” is a string, 12.4550 is a float, [] is a list, None is NoneType.

c)Tuple: It is an ordered collection of elements where the elements are separated with a comma (,) and enclosed within parenthesis().

Example: x= (“apple”, “banana”, Cherry”)

List and Tuple are used to contain multiple values in a single variable.

Basic difference between List and Tuple: You can change the values of a list(mutable) but you can’t do the same with tuple(immutable) [more about this in later series]

4) Mapping Type:

Dictionary: It’s an unordered collection of data. It’s written as key:value pair form.
Example:

 cardict= {'brand' : 'Lamborghini' , 'model': 'Aventader', 'year'=2018}
            #key        #value

 print (cardict['model'])
 #Output:Avantader

Enter fullscreen mode

Exit fullscreen mode

5)Nonetype(None):It refers to a null value or no value at all. It’s a special data type with a single value, None.

Type Function:
Type() function returns the type of argument(object) passed as a parameter. It’s mainly used for debugging(Code correcting) purpose.
To know the type of a variable just write type(variable name)

text="Python is awesome"
print(type)
#output:<class 'str'>
Enter fullscreen mode

Exit fullscreen mode

Example:
type(“Hello python”) #output: str
type(2024) #output: int
type(3.14) #output: float
type(True) #output: bool
type(None) #output: NoneType



Source link
lol

By stp2y

Leave a Reply

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

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.