‘’’DATA TYPES COMPARISION ‘’’

Razique parwez
6 min readOct 13, 2022

--

Every value in python has a datatype. Since everything is an object in python programming, data types are classes and variables.

There are various data types in python. Some of the important data types are listed below.

Mutable objects — Where we can change the elements after they got created.

for example — set , list, dictionary

immutable objects — we can’t change elements after they got created.

examples — tuple, string, string, frozen set

1. NUMERIC TYPE

Integers, floating point numbers and complex numbers fall under python numbers category . They are defined as int , float , complex classes in python.

We will use the type() function to know which class a variable or a value belongs to.

#Integer

#float

#Complex number

complex number consists of two parts real and imaginary which is 2 and 7j respectively.

2. DICTIONARY

Dictionary is an unordered collection of key-value pairs. In python, dictionaries are defined within braces { } with each item being a pair in the form of key : value.

{1: ‘a’, 2: ‘b’, 3: ‘c’}

where 1 is key and ‘a’ is value

Allowed key datatypes — int, float, string, complex, Tuple, Boolean.

Allowed value datatypes — all

Duplication of items is not allowed.

Keys will update value if duplicated.

Values can be duplicated.

Adding/Removal/Modifying items are allowed.

Indexing is not allowed, but values can be accessed using keys.

NOTE-; Dictionary is Immutable that means we can not change the items

3. BOOLEAN

Boolean is a data type that has only of two possible values, Which is intended to represent the two truth values either True or false.

These are the few examples of Boolean type -;

4. SET

Set is an unordered collection of unique items. Set is defined by values separated by comma inside curly braces { } . Items in a set are not ordered.

We can perform set operations like union, intersection on two given sets. Sets have unique values and they eliminate duplicates. Since , set are unordered collection, indexing has no meaning. Hence, the slicing operator [ ] does work.

Not all datatypes are allowed.

2 . Indexing, Slicing is not allowed.

3. Duplicates are not allowed.

4. Adding/removal of items is allowed.

5. Cannot replace/modify items

5. SEQUENCE

Strings , List, Tuple fall under Sequence category.

#STRINGS (‘ ’) (“ ”) (“‘ ’’’)

Strings are arrays of bytes representing Unicode characters. Strings in python are surrounded by either single quotation marks, or double quotation marks.

‘hello’ or “hello”

We will use square brackets to access elements of the string.

#LIST

A Python list is an ordered sequence of items. All the the items in a list don’t need to be of same data types. Declaring a list is pretty straight forward. Items separated by commas are enclosed within square brackets.

We can use the slicing operator [ ] to extract an item or items from a list. The index starts from 0 in python. Lists are mutable, means value of elements of a list can be altered.

1. Allows all datatypes as an item

2. List has indexing, slicing

3. Addition of items is allowed.

4. Removal of items is allowed.

5. Modifying/replacing is allowed.

6. Sub items are allowed.

7. Duplicates are allowed.

#TUPLE

Tuple is an ordered sequence of items same as a list. Tuples once created can’t be modified. It is defined within parentheses ( ) where items separated by commas.

These are few examples of different types tuples.

1. All data types are allowed as an items.

2. Allows indexing, slicing .

3. Adding/removal/modifying of items are not allowed.

4. Duplications are allowed.

6. Frozen set

frozen set is just an immutable version of a python set object. While elements of set can be modified at any time, elements of the frozen set remains the same after creation. Thus frozen set can used as keys in dictionary or as an elements of another set

The syntax of frozen set function is “ ( [ ] ) ”

Allows — int, float, complex, Boolean, tuple

Not allowed — list, set, dictionary

duplication not allowed

3. Frozen set operations

Copy

--

--

Razique parwez
Razique parwez

No responses yet