Python Data Types
Understand Python data types and learn to index string elements.
We'll cover the following
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 variablex
and initializes it to the value5
. 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 variabley
and initializes it to the value10.5
. Its data type isfloat
.Complex numbers have real and imaginary parts. For instance,
z = 2+5j
creates a variablez
of a complex data type. It’s initialized with2
as a real part and5
as an imaginary part. Note that the imaginary part of a complex number is suffixed with the letterj
.
x = 5y = 10.5z = 2+5jprint(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.
bool_var1 = 8 > 4bool_var2 = 8 < 4print(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"
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 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
.
# This code demonstrates string indexing and slicing using different stridesmy_string = "Hello World" # play around with my_stringprint(f'The string to be tested is \n {my_string} \n')# positive indexingi = 1j= 6print(f'Characters at index {i} and index {j} are \n {my_string[i]} and {my_string[j]}, respectively \n')# negative indexingi = -1j= -6print(f'Characters at index {i} and index {j} are \n {my_string[i]} and {my_string[j]}, respectively \n')#string indexing with a stridei, j = 0, 6stride = 2 # change the stride to observe the change in the outputprint(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
.
# Compare a None object to 0, empty string, and Falseprint(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.