Data Types in Python:

Before learning about data types in python, let us see what are values. A value is one of the fundamental things a program works with. Letters and numbers are examples of values, such as 1, 2, and "Hello world".

These values belong to different data types:

  • The value consisting of a number such as 2 is an integer (int)
  • "Hello world" is a string (str)
  • Value consisting of decimal number such as 3.2 is a float

“Hello World” is called a string because it consists of a sequence of characters, and it is usually enclosed in quotation marks "".

How to know data type of a value?

To know data type of a value, you can use type function.

type('Hello, World!')
type(17)
type(3.2)

However, values like "17" or "3.2" are still strings, because they are inside quotes:

type('17')
type('3.2')

How Python is different from other languages in assigning values:

In languages like C or Java, you must first declare the name and type of a variable.
Only after that can you assign a value, which must, of course, be compatible with the declared type. In Python, it is not necessary to declare the type of a variable before using it. Simply assigning a value to a variable name automatically determines its type based on the given value.

n = 7  # Assigning the value 7 to the variable n
msg = "How are you?"  # Assigning the value "How are you?" to the variable msg
pi = 3.1415

For Example, in above example,

  • An integer 7 was stored in n, so, data type of n is integer
  • An String "How are you?" was stored in msg, so, data type of msg is string
  • An real number 3.14159 was stored in pi, so, data type of p1 is floating-point number.

Note: A real number is any number on the number line, including integers, fractions, and irrational numbers. A floating-point number is how computers represent real numbers approximately using decimal-based encoding.

This kind of variable typing is called dynamic. It is opposite to static typing, which follows fixed rules.

Static vs. Dynamic Typing

Static typing is preferable in compiled languages because it improves code optimization and results in a binary program.

Dynamic typing makes it easier to write high-level logical structures (reflection, metaprogramming), especially in object-oriented programming (OOP), supporting:

  • Polymorphism
  • Complex data structures such as lists and dictionaries

Main Types of Data

There are several types of data: integers, real numbers, and strings. Let us examine these types more closely.

Numeric Data

Numeric Data types include Ordinary integers and real numbers (also called floating-point numbers).

The Integer

Let us consider an example of Fibonacci sequence. Initially, we set the while loop condition in the second line 50 so that we obtain 49 terms. We also displaying the type of the variable.

a, b, c = 1, 1, 1
while c < 50:
  print(c, ":", b, type(b))
  a, b, c = b, a+b, c+1

The exercise we just performed allows us to understand the internal representation of numbers in the computer. You should realize that the core of a computer consists of electronic integrated circuits (silicon chips) assembled in a highly complex way, capable of performing billions of operations per second. However, binary numbers have limited size.

Currently, 32-bit systems can represent numbers in the range:
  -2147483648 to +2147483647.

Operations on integers within these limits are always very fast, because the processor can handle them efficiently. However, when dealing with larger integers or real numbers (numbers with a decimal point), interpreters and compilers must perform additional work to process them, since these exceed the native 32-bit limits.

There is no need to worry about these technical considerations. When you ask Python to process an integer, it automatically converts and handles it in a binary format (32-bit when possible) to optimize speed and memory usage.

When the values exceed these limits, Python uses a more complex representation in memory and processes them using multiple operations. All of this happens automatically, without requiring any concern from the programmer.

Thus, with Python, you can compute integers of arbitrarily large size. The only real limitation is the amount of memory available on your computer.

Calculations involving very large numbers are internally broken down into simpler operations, which may require more processing time.

Example:

a, b, c = 3, 2, 1
while c < 15:
  print(c, ": ", b)
  a, b, c = b, a*b, c+1

In the example above, the values grow very quickly, because each one results from multiplying previous values. Of course, you can continue this mathematical sequence as long as you want, but the computation speed gradually decreases as the numbers become extremely large.

Integers within the limits mentioned above each occupy 32 bits in computer memory. Very large integers occupy a variable amount of space, depending on their size.

Real Numbers

We have previously become familiar with this type of numeric data. A number of the “real type” is referred to in English as floating-point numbers, and for this reason they are called real numbers in Python.

This allows performing calculations on very large or very small numbers (scientific data, for example), with a fixed degree of precision.

To obtain numeric values in Python that are considered of floating type, it is sufficient that their representation contains a symbol such as a decimal point or an exponent (power) of 10.

For example:

3.14   10.   .001   1e100   3.14e-10

These expressions are automatically interpreted as real numbers by Python.

Let us try this type of data in a small program (inspired by the previous example):

a, b, c = 1., 2., 1    # a and b are numbers of type float
while c < 18:
  a, b, c = b, b*a, c+1
  print(b)

You have probably understood this well. Once again, we are displaying a sequence of numbers that grows very quickly, each value being the result of previous ones.

At the ninth result, Python automatically switches to scientific notation (the “e + number” form, meaning “ten raised to the power of …”).

After 15, we see that the values exceed the limit (without any error message). The numbers become extremely large, and we simply observe the expression Inf to represent infinity.

The real numbers used in our example allow processing values (positive and negative) between:

10-308  and  10308

with a certain precision.

These numbers are encoded in a specific way using 8 bytes (64 bits) in computer memory:

  • one part of the code corresponds to the significant digits
  • another part corresponds to the exponent (power of 10)

Alphabetic Data

Until now, we have only dealt with numbers, but computer programs also need to handle alphabetic characters, words, sentences, and sequences of symbols. In most programming languages, this is done using special data structures called “strings.”

For understanding strings in detail, please visit this page.

No Comments

Leave a Reply