Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Primitive Data Types vs Non Primitive Data Types in Python
Next article icon

Primitive Data Types vs Non Primitive Data Types in Python

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python, known for its versatility and user-friendliness, is a dynamic language that doesn't explicitly categorize data types into primitive and non-primitive as some languages like Java do. However, for clarity and understanding, it's useful to draw parallels and discuss similar concepts. In this article, we'll explore the types of data types traditionally considered "primitive" in other languages and how they compare to "non-primitive" types in Python.

What are Primitive Data Types in Python?

Primitive data types in Python are the most basic types of data. They are the building blocks for data manipulation in Python and are typically immutable, meaning their value cannot change after they are created. Here are the main primitive data types in Python:

Examples of Primitive Data Types

  • Integer (int) :
    • Represents whole numbers.
    • Example: x = 10
  • Float (float) :
    • Represents floating-point numbers (decimal values).
    • Example: y = 10.5
  • Boolean (bool) :
    • Represents True or False values.
    • Example: is_active = True
  • String (str) :
    • Represents a sequence of characters.
    • Example: name = "Alice"

Example Code

Python
x = 10              # Integer y = 10.5            # Float is_active = True    # Boolean name = "Alice"      # String  print(type(x))       print(type(y))      print(type(is_active))   print(type(name))    

Output:

<class 'int'>
<class 'float'>
<class 'bool'>
<class 'str'>

What are Non-Primitive Data Types in Python?

Non-primitive data types, also known as complex or composite data types, are data types that are derived from primitive data types. They can store multiple values or more complex structures of data. Unlike primitive types, non-primitive data types are mutable, meaning their contents can be changed.

Examples of Non-Primitive Data Types

  • List (list) :
    • Ordered collection of items, which can be of different types.
    • Example: numbers = [1, 2, 3, 4]
  • Tuple (tuple) :
    • Similar to lists, but immutable.
    • Example: coordinates = (10.0, 20.0)
  • Dictionary (dict) :
    • Collection of key-value pairs.
    • Example: person = {"name": "Alice", "age": 30}
  • Set (set) :
    • Unordered collection of unique items.
    • Example: unique_numbers = {1, 2, 3, 4}
  • String (str) :
    • While strings are technically primitive in Python, they can also be considered non-primitive when used to store complex data or collections of characters.

Example Code

Python
numbers = [1, 2, 3, 4]            # List coordinates = (10.0, 20.0)        # Tuple person = {"name": "Alice", "age": 30}  # Dictionary unique_numbers = {1, 2, 3, 4}     # Set  print(type(numbers))              print(type(coordinates)) print(type(person))               print(type(unique_numbers))        

Output:

<class 'list'>
<class 'tuple'>
<class 'dict'>
<class 'set'>

Differences Between Primitive and Non-Primitive Data Types in Python

Feature

Primitive Data Types

Non-Primitive Data Types

Mutability

Immutable

Mutable

Data Representation

Represents a single value

Can represent multiple values

Example Types

int, float, bool, str

list, tuple, dict, set

Memory Allocation

Less memory, as they are simpler

More memory, can store complex data

Methods and Operations

Limited operations and methods

Rich set of methods and operations

Copying

Copying creates new instances

Can create shallow or deep copies

Use Case

Simple data, basic calculations

Complex data structures, collections

When to Use Primitive Data Types in Python ?

Primitive data types are best used when:

  1. Simple Data Storage: When you need to store simple, single values such as integers or floats.
  2. Performance Considerations: They are typically faster and use less memory, making them ideal for high-performance applications.
  3. Basic Operations: When your operations are limited to basic arithmetic or boolean logic.

When to Use Non-Primitive Data Types in Python ?

Non-primitive data types are suitable for:

  1. Complex Data Storage: When you need to store collections of data, such as lists of items or key-value pairs.
  2. Flexibility and Mutability: They allow changes to the data without creating new objects, which is useful in scenarios where data needs to be updated frequently.
  3. Data Relationships: When you need to represent more complex relationships, such as a dictionary mapping keys to values or a set of unique items.

Conclusion

Understanding the difference between primitive and non-primitive data types in Python is fundamental for efficient coding and data management. Primitive data types are simple and immutable, making them ideal for storing single values and performing basic operations. In contrast, non-primitive data types can store multiple values and are mutable, offering greater flexibility for managing more complex data structures. By knowing when and how to use these data types, developers can optimize their code for better performance and readability, ensuring that the right tools are used for the right tasks in Python programming.


Next Article
Primitive Data Types vs Non Primitive Data Types in Python

P

prathamsahani0368
Improve
Article Tags :
  • Python
  • Python-datatype
Practice Tags :
  • python

Similar Reads

    Last Minute Notes (LMNs) – Data Structures with Python
    Data Structures and Algorithms (DSA) are fundamental for effective problem-solving and software development. Python, with its simplicity and flexibility, provides a wide range of libraries and packages that make it easier to implement various DSA concepts. This "Last Minute Notes" article offers a q
    15+ min read
    Type Casting in Python (Implicit and Explicit) with Examples
    Type Casting is the method to convert the Python variable datatype into a certain data type in order to perform the required operation by users. In this article, we will see the various techniques for typecasting. There can be two types of Type Casting in Python:Python Implicit Type ConversionPython
    5 min read
    Why Java Collections Cannot Directly Store Primitives Types?
    Primitive types are the most basic data types available within the Java language. Such types serve only one purpose — containing pure, simple values of a kind. Since java is a Statically typed language where each variable and expression type is already known at compile-time, thus you can not define
    4 min read
    How To Convert Data Types in Python 3?
    Type Conversion is also known as typecasting, is an important feature in Python that allows developers to convert a variable of one type into another. In Python 3, type conversion can be done both explicitly (manual conversion) and implicitly (automatic conversion by Python).Table of ContentTypes of
    4 min read
    Emulating Numeric types in Python
    The following are the functions which can be defined to emulate numeric type objects. Methods to implement Binary operations on same type of objects : These functions will make no change in the calling object rather they will return a new numeric object of same type after performing certain operatio
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences