Pages

Sunday, July 28, 2024

Python : DataTypes : Core Data Types

Python Core Data Types

It refers to Python's built in Object Types, it comes with python installation.

These are listed as below:

  1. Numbers --> (integer, float, complex numbers, rational fractions with Numerator and Denominator)
  2. Strings
  3. List
  4. Dictionaries
  5. Tuples
  6. Files
  7. Sets
  8. Other Core Types (Eg. Boolean, Types, None)
  9. Program Unit Types (Eg. Funcitons, Classses, modules)
  10. Implementation Related Types (Eg. Compiled Code, Stack Tracebacks)

The above listed Object types would be covered subsequently.

Reasons why we should use Python Built-In types : 
  • Built-in objects make programs easy to write. --- you can use them immediately. You can get a lot of work done with Python’s builtin object types alone.
  • Built-in objects are components of extensions.
  • Built-in objects are often more efficient than custom data structures.





High level overview of Python's Core Data Types are as follows:

Numbers

Numbers are immutable and subsumes

  • Integer
  • Float
  • Complex Numbers
  • Rational Fractions (with Numerators and Denominators)
Below are some examples related to numbers in Python:














NOTE: you can see an error in an example above so in order to resolve the error we need to import sys module and set the length of integer in it up to which we would like our system to allow length of numbers, please see below example.











As you can see the error is resolved now.


Integer

  • It represent Integer Data Type.
  • Used to represent integral numbers: 0,1,100, -3, -78.
  • It can be of any magnitude (subjected to the memory of your devise).
  • It has an exact representation in Python.
  • Integers can be created as shown below:
Python : integer : Creation











Float
  • It is of float type.
  • Used to represent real numbers (floating point numbers): 2.63, 3.14, -5.89
  • Only decimal point differentiates float from an int like 1 is an int but 1.0 is a float.
  • Floats can be created as shown below:
Python : Float Creation

String
  • They are sequence in Python, as they are positionally ordered collection of other objects.
  • Strings are Immutable, means they cannot be changed in place, once defined.

List
  • Most general sequence provided by Python.
  • They are positionally ordered collections of arbitrarily typed objects.
  • Have No fixed size, can dynamically shrink or expand.
  • They are mutable.
Dictionaries
  • They are also known as mappings & do not fall under the category of sequence.
  • Objects are stored in it by key instead of relative position.
  • They do not maintain any reliable left to right order.
  • They are mutable and may change in place.
  • Can grow and shrink on demand like lists.

Tuple
  • They are similar to list in python, but the only difference is that it can not be changed.
  • They are immutable like strings and numbers.
  • They are sequences and hence can be iterated over.
  • They are used mainly because of their immutability, to provide a sort of integrity constraint in very large programs.

Set
  • They are neither mappings nor sequences, but are unordered collections of unique and immutable objects
  • They can be created as shown in the example.
Boolean
  • They have a value of True or False.

Below is an
example of how to create string, list, tuple, dictionaries, sets and booleans in Python:





























No comments:

Post a Comment

Python : About : Variables

  Variables in Python Variables are used to: Name an object. Remind us about the usability of an object. Allow us to reuse same object in mu...