Datatypes in Python.

Author: neptune | 22nd-May-2022
🏷️ #Python

Different types of Datatypes in Python

  • Numbers 

  • Strings

  • Lists 

  • Tuples

  • Dictionary

  • Set

  • Frozenset 

1. Numbers 

Python supports integers, floating-point numbers, and complex numbers. In Python, these numbers are defined as int, float, and complex classes in Python.

When we put a decimal in an Integer then it converts into a Floating-point number. 


Example: 5 is an integer while 5.0 is a floating-point number.

Points to remember :

1. Integer length is not defined it can be of any length.

2. Floating-point is valid till 15th place 16th place is invalid.

Numbers we use daily are of type decimal (Base 10) number system. But other number systems are also present like Binary (Base 2), Hexadecimal (Base 16), and Octal (Base 8).




It's simple to represent these numbers in Python.


Number System

Prefix

Binary

β€˜0b’ or  β€˜0B’

Octal

β€˜0o’ or β€˜0O’

Hexadecimal

β€˜0x’ or β€˜0X’

Example :

Complex numbers are represented in form of (x+yj)  where x is a real part while y is an imaginary part.

That’s all you need to know about numbers in Python.




2. Strings

String is a sequence of Unicode characters.

Points to remember:
1. String can be represented using single quotes β€˜Neptune’ or double quotes β€œNeptune”.

2. Multiline Strings can be represented using tripe quotes.

Examples:

3. Strings are immutable in Python.




3. Lists

List is an ordered sequence of items. It is a widely used datatype in Python.

Points to remember:

1. We can declare a list using [ ] or list class.

2. List index starts from Zero. 

Example :


3. Lists are mutable datatype means we can change the values in the existing list.

Example:

4. List is a heterogeneous datatype which means we can store different types of data in a single list.

Examples :

5. List can also be sliced using [ start index: end index - 1 ].

Examples:




4. Tuples

Tuple is the same as a list it is an ordered sequence of items. But tuple is an immutable data type which means it can’t be modified once created.

1. We can create a tuple using ( ) or tuple class.




2.Tuple is immutable in nature.

3. Tuple values can be accessed using [ ] just like a list.


5. Dictionary

Dictionary is an unordered collection of key-value pairs. It can be defined within braces { key: value } where key and value can be of any data type.

Example:




1. Using the key we can fetch value in constant time O(1).

Example:

2. Different types of keys.

Example:




6. Set

Set is an unordered collection of Unique items. It is defined using { } values separated by comma.

Example:

1. Set is an unordered datatype so there is no index.

Example:


7.FrozenSet

FrozenSet is a modified version of Set. FrozenSet values can’t be changed after creation while we can add or remove values from Set.

Example:

1. We can’t add or remove value from frozenSet.

These all are the data types in python. Hope you got something from this article.

Thanks for Reading !!!





πŸ‘‰ Read More
How to extract Speech from Video using Python?
How to download video from youtube using python module ?
Deploy Django project on AWS with Apache2 and mod_wsgi module.
Best Python package manager and package for virtual environment ?
Mostly asked Python Interview Questions - 2023.
Core Python Syllabus for Interviews
Python Built-in functions lambda, map, filter, reduce.
How to reverse string in Python ?
Python 3.9 new amazing features ?
10 Proven Ways to Earn Money Through Python
Building a Simple Chatbot with Python and openpyxl
5 Languages that Replace Python with Proof
Monkey Patching in Python: A Powerful Yet Controversial Technique
Best Practices for Managing Requests Library Sessions When Interacting with Multiple APIs ?
How to Ensure Proper Namespace Handling in XML with Python's lxml Library
How to Update XML Files in Python?
Explore more Blogs...