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 !!!





Related Blogs
How to extract Speech from Video using Python?
Author: neptune | 16th-Jun-2023
#Python #Projects
Simple and easy way to convert video into audio then text using Google Speech Recognition API...

How to download video from youtube using python module ?
Author: neptune | 15th-Jun-2023
#Python
We will let you know how you can easily download the Youtube high quality videos along with subtitle, thumbnail, description using python package..

Best Python package manager and package for virtual environment ?
Author: neptune | 18th-Jun-2023
#Python #Pip
We will explore the options of Pip, Virtualenv, Anaconda, and also introduce Pyenv as a helpful tool...

Deploy Django project on AWS with Apache2 and mod_wsgi module.
Author: neptune | 18th-May-2024
#Python #Django
In this blog I use the AWS Ubuntu 18.22 instance as Hosting platform and used Apache2 server with mod_wsgi for configurations. We create a django sample project then configure server...

Mostly asked Python Interview Questions - 2023.
Author: neptune | 30th-May-2023
#Python #Interview
Python interview questions for freshers. These questions asked in 2022 Python interviews...

Core Python Syllabus for Interviews
Author: neptune | 26th-Jul-2023
#Python #Interview
STRING MANIPULATION : Introduction to Python String, Accessing Individual Elements, String Operators, String Slices, String Functions and Methods...

How to reverse string in Python ?
Author: neptune | 16th-May-2022
#Python
We are going to explore different ways to reverse string in Python...

Python Built-in functions lambda, map, filter, reduce.
Author: neptune | 15th-Jun-2023
#Python
We are going to explore in deep some important Python build-in functions lambda, map, filter and reduce with examples...

Python 3.9 new amazing features ?
Author: neptune | 26th-Jul-2023
#Python
Python 3.9 introduces new features such as dictionary union, string methods to remove prefixes and suffixes, type hinting, and speed improvements for built-in functions...

5 Languages that Replace Python with Proof
Author: neptune | 13th-Apr-2023
#Python
Julia, Rust, Go, Kotlin, and TypeScript are modern languages that could replace Python for specific use cases...

10 Proven Ways to Earn Money Through Python
Author: neptune | 11th-Apr-2023
#Python
Python offers numerous earning opportunities from web development to teaching, data analysis, machine learning, automation, web scraping, and more...

Monkey Patching in Python: A Powerful Yet Controversial Technique
Author: neptune | 01st-Aug-2023
#Python
Monkey patching in Python is a dynamic technique to modify code at runtime. It can add/alter behavior, but use it judiciously to avoid maintainability issues...

Building a Simple Chatbot with Python and openpyxl
Author: neptune | 25th-Jun-2024
#Python #Projects
This chatbot reads questions and answers from an Excel file and provides responses based on user input...

Best Practices for Managing Requests Library Sessions When Interacting with Multiple APIs ?
Author: neptune | 22nd-Aug-2024
#Python
When working with Python's `requests` library, managing sessions is crucial, especially when your application interacts with multiple APIs...

How to Update XML Files in Python?
Author: neptune | 01st-Jul-2024
#Python
Handling XML files in Python is straightforward with the `xml.etree.ElementTree` module...

How to Ensure Proper Namespace Handling in XML with Python's lxml Library
Author: neptune | 01st-Jul-2024
#Python
By using `lxml`, you can effectively manage XML namespaces and ensure that your XML structure remains intact during updates...

View More