Python Data Types

Understand Python data types and learn to index string elements.

Python data types

Python is an open-source programming language created by Dutch programmer Guido van Rossum in the late 1980s. It’s a powerful language with plenty of libraries to load, process, analyze, visualize, and manipulate huge amounts of data easily. The data can be numeric, textual, or mixed. Python stores data in different types.

A data type specifies the type of data an object can store and the type of operations it can support. Python supports numerous data types, including:

  • Numeric

    • Integer

    • Float

    • Complex

  • String

  • Boolean

  • None

We discuss these data types below.

Numeric

The numeric data type includes int, float, and complex.

  • We use the int data type to store integers (whole numbers). For example, x = 5 creates the variable x and initializes it to the value 5. The data type of the variable depends on the data being stored. In this case, x is an integer.

  • We use the float data type to store real numbers. For example, y = 10.5 creates the variable y and initializes it to the value 10.5. Its data type is float.

  • Complex numbers have real and imaginary parts. For instance, z = 2+5j creates a variable z of a complex data type. It’s initialized with 2 as a real part and 5 as an imaginary part. Note that the imaginary part of a complex number is suffixed with the letter j.

Press + to interact
x = 5
y = 10.5
z = 2+5j
print(f'The variables x belongs to {type(x)}',
f'\nThe variables y belongs to {type(y)}',
f'\nThe variables z belongs to {type(z)}.')

Boolean

The bool data type can either store True or False as its value. For example, x = True creates a variable of the bool data type. It’s initialized with the logical value True. Moreover, the result of a comparison results in a boolean output. For instance, the expression bool_var = 8 > 4 returns the logical True, whereas the expression bool_var = 8 < 4 returns the logical False. Expressions that give boolean outputs can be employed to make decisions.

Press + to interact
bool_var1 = 8 > 4
bool_var2 = 8 < 4
print(bool_var1,
'\n',
bool_var2)

String

The variables of the string data type store textual data, which includes numbers, alphabets, spaces, and special characters, such as commas, full stops, and colons. Unicode, an international standard for text representation in various world languages, encodes string characters. Python stores strings by enclosing characters in single or double quotes. 'This is a string' and "A string in a double quote" are two examples of strings in Python.

String indexing

The string elements, individual characters, or substrings, can be accessed using positive and negative indices.

Let’s declare a string.

my_string = "Hello World"
Declaring a string in Python

In the code given above, my_string[6] returns the character W, and my_string[-10] returns the character e, as shown in the figure above.

String slicing

Extracting a substring from a string is known as string slicing. A string indexing of the form my_string[i:j] returns the substring from the index i to just before j. For instance, my_string[6:10] returns Worl (without the character d at index 10).

Note: The string element at the ending index is not part of the result.

To get every kkth character in the string, we specify a stride or a step in string indexing. A stride or a step determines the size of the step between string characters. We add a colon in string indexing to use a stride. For example, we specify a step in my_string[0:10:2] to retrieve every second character of my_string, starting from index 0 to just before index 10, i.e., HloWr.

Press + to interact
# This code demonstrates string indexing and slicing using different strides
my_string = "Hello World" # play around with my_string
print(f'The string to be tested is \n {my_string} \n')
# positive indexing
i = 1
j= 6
print(f'Characters at index {i} and index {j} are \n {my_string[i]} and {my_string[j]}, respectively \n')
# negative indexing
i = -1
j= -6
print(f'Characters at index {i} and index {j} are \n {my_string[i]} and {my_string[j]}, respectively \n')
#string indexing with a stride
i, j = 0, 6
stride = 2 # change the stride to observe the change in the output
print(f'Characters from index {i} to just before index {j} with stride = {stride} are \n {my_string[i:j:stride]} \n')

None

The keyword None defines a null object whose data type is NoneType. The None data type is not equal to 0, an empty string, or False. For example, comparing None to other data types returns False.

Press + to interact
# Compare a None object to 0, empty string, and False
print(None == 0)
print(None == '')
print(None == False)

Conclusion

In this lesson, we’ve summarized the Python data types that store numeric, string, and boolean data.