Python Data Types

Why So Many Different Data Types

All programs have to deal with Variables of different types (integers, strings, lists, tuples, dictionary). These data types have special rules for treating them.

You saw on the Python Arithmetic page that for arithmetic computations, you dealt with numbers. For programs related to text or messages, you use a data type called string.

On this page, we go over the details of all the different data types that are available in Python, and how they are handled in the programs.

We will explain the syntax of these different Python data types by writing example programs using the facts about our solar system. Refer to the Enrichment: Astronomy Related section at the end of this chapter for solar system facts.

Literals, Identifiers, and Variables

Before we start to explore different types of Python Data Types, let us first understand what literals, Identifiers, and Variables are.

Literals

Literals are expressions (sequence of characters or numbers) like “15” or “How are you?”. Literal’s value is directly visible from their names.

Identifiers

Identifiers: Identifiers are names used to identify Python objects (variable, function, class, module, etc.). The sequence of characters used to form a variable name and names for other Python entities is called an identifier. This sequence of characters is called an identifier. Identifiers identify a Python variable or other Python entities. The Identifier’s sequence of characters may only contain letters, digits, and the underscore symbol.  They cannot start with a digit and must start with a letter. Identifiers cannot have any spaces. Identifiers are case-sensitive.

Variables

Variables are identifiers that are used to name Python data. Variables are a sequence of characters, but they refer to a memory location, where their value is stored. Thus, unlike literals, their value is not directly available from their name. In the following sections, we will discuss the rules for naming identifiers and variables.

How Are Variables and Labels Related

Python variable is a reserved memory location that has a label (name) that stores the value of the variable. In other words, a label reserves a place in the memory to store data (variable) value, which may be any of the data types such as numbers, text, strings, lists, dictionary, etc. (We will discuss these data types a bit later in this chapter).

The computer’s CPU accesses (reads) the variable’s value stored in the memory to do the computations specified in the instructions in your program. The CPU can write the result of the calculation back to the memory location. 

Python supports several kinds of data type variables discussed later in this page.

How Do We Declare a Variable?

A variable can be declared by any name of your choosing composed of numbers, alphabets, and the underscore ­(_) symbol, or a combination of the above.

Rules for defining a variable:

  • The first character cannot be a number. It must start with a letter.
  • There cannot be any blank in a variable’s name.
  • A variable can have any number of characters.
  • Punctuation symbols are not permitted in the identifier name.

Lower-case and upper-case characters to define an identifier are treated as different names. For example, ‘fred’,  ‘Fred’, and ‘fRed’ are different identifiers.

Here are a few examples:

The example below declares a variable with the label “earth_moon_distance_in_miles” and assigns it a value of 250000 to the variable.

PROGRAM EXAMPLE: VARIABLE NAME DECLARATION EXAMPLE

>>> Earth_moon_distance_in_miles = 250000

Another example of a variable name:

>>> earth_diameter_is_8000_miles

Python has some reserved words that you cannot use as your own identifiers.  The reserved words (keywords) are listed on Appendix page.

Python Supports Many Data Types

As mentioned earlier, Python supports several data types. You may ask: what is a data type? What is their use?

When you write a program, your program must deal with many different data types – numbers, characters, strings, tables, etc. In previous pages, you have already come across a few data types: integers, floating-point numbers, and strings.  Tables may be a list of students in your class and their corresponding ages.  In any case, there are different rules to deal with each of these different data types. Below you will learn the different data types that are available in Python.

Python supports five different data types:

  • Numbers
  • Strings
  • Lists
  • Tuple
  • Dictionaries

Let us discuss them one at a time.

Number Data Types

Python Number data type stores numeric values, as in the example below. As you know, the distance from the earth to the moon is 250,000 miles. We will define a variable named “earth_moon_distance_in_miles” as a variable of data type number and assign it a value of 250000.

>>> earth_moon_distance_in_miles = 250000

There are three different numerical data types in Python −

  • int (signed integers) – Also called just integers or int. Data type Int are whole numbers with no decimal point. They can be positive or negative.
  • float (floating point real values) – Data type float is a number consisting of a whole number (integer) followed by a decimal point, followed by the fractional part of the number. float represents real numbers. 

Examples of Number Data Types

Below are a few examples of number data types.

452.0 is a floating-point data type.

3.14159 is a floating-point data type.

Floats are sometimes written in scientific notation that contains an ‘e’ followed by a number that represents the power of 10.

Thus, 452.0 in the above example can be written in scientific notation as 4.52e2 (4.52 x 102).

Another example is the speed of light, which is 300,000,000 meters per second.  This is written as 3*10^8 meters per second. In scientific notation, it is written as 3.0e8.

  • Complex numbers – we will not discuss complex numbers on this website.

The above number data types are summarized in the Table below:

NUMBER DATA TYPE EXAMPLECOMMENTS
intSigned 23
-15
Positive or negative
whole numbers.
No decimal point.
FloatDecimal
numbers
3.14159Consists of a
whole number (‘3’)
and a fractional
part after the decimal
point (.14159).
Number Data Types

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Verified by MonsterInsights