top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Data Types in Python


Python Data Types Flow Chart


Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python remains one of the most popular programming languages in the world since it is easy to learn, flexible, powerful, and has a fantastic community. It is the go-to programming language for people learning about analytics and data science because of its core analytics capabilities and the thousands of community-driven libraries. Before getting into the complexities of data analysis, visualizations, and machine learning, it’s best to develop a foundational knowledge of the programming language and understand Python data types.


So, let's get started with the following sequence

  1. What is Python data type

  2. What are the different python data types

  • Boolean

  • Numeric

  • Sequence

  • Set

  • Dictionary

1. What is a Python data type?

Python data types are built into the programming language. At the most basic level, a Python data type is metadata, or a categorization, about a variable that defines what operations can be performed on the variable. Under the hood, data types are Python classes, and variables are the objects of the classes. We can use the type () function in Python to see a variable’s data type.


2. What are the different Python data types?

Now, let’s get into the different Python data types. We will look at the boolean, numeric, sequence, set, and dictionary data types.


Python data types: Boolean

The boolean data type in Python is based on boolean logic and is used to evaluate whether something is true or false. In Python, we must use capital T for True and capital F for False when utilizing the boolean data type.


Boolean Arithmetic

Boolean arithmetic is the arithmetic of true and false logic. A boolean or logical value can either be True or False. Boolean values can be manipulated and combined with boolean operators. Boolean operators in Python include and, or, and not. The common boolean operators in Python are below:

  1. or

  2. and

  3. not

  4. == (equivalent)

  5. ! = (not equivalent)

#Examples of Boolean data type. Boolean is either True or False.

  • print (type (True))

  • print (type (False))

#Boolean data type can return when evaluating an expression too.

  • var_bool = 4==4 print(type(var_bool))


Few Examples for Boolean Data Type


Whether or not 4 equals 4 is True or False, since our code is evaluating one variable with another in a way that can only produce an answer of True or False, the resulting data type of the variable assigned to the expression is a bool.

The output for the first 3 examples will be <class ‘bool’> indicating the data is an object of the bool class.


Integers and Floats as Booleans

Integers and floating-point numbers can be converted to the boolean data type using Python’s bool () function. An int, float or complex number set to zero returns False. An integer, float or complex number set to any other number, positive or negative, returns True.



Example for integers, floats as boolean


Python data types: Numeric

There are three numeric data types in Python that are used to categorize variables that have a numeric value. Numeric data types include int, float, or complex.


* Int

The Python data type int represents integers, which are positive or negative whole numbers that don’t include decimals. Let’s look at a few different int values using Python.


#Examples of the Integer Data type.

  • print (type (2))

#Int values can be very long.

  • print (type (648165234234234234363466184))

#Int values can be assigned to variables

  • var = 5489

  • print(type(var))


Examples of the integer data type


* Float

The Python data type float represents a real number with a floating point (decimal place). The float class can make use of the character ‘e’ to represent scientific notation.


#Examples of the Float Data type.

  • print (type (2.58))

#Float values can use e for scientific notation.

  • var_float = 578.34e10

  • print(type(var_float))


Example of float data type

The output for each example will be <class ‘float’> indicating the data is an object of the float class.


* Complex

The Python data type complex is used to represent imaginary numbers and is returned by assigning ‘j’ to an integer or float value.


#Examples of the complex Data type.

  • print(type(4j))

#Float values can also be used in complex numeric data types

  • var_complex = 45 + 5.5j

  • print(type(var_complex))


Example of complex data type


The output for each example will be <class ‘complex’> indicating the data is an object of the complex class.


Numeric Python data types takeaways:

  • The three numeric data types are int, float, and complex.

  • Floats can utilize scientific notation by assigning e to the float value.

  • Complex numbers are represented by assigning j to the int or float value.

Python data types: Sequence

The Python data types that represent sequences are str, list, and tuple. These data types are sequences because they represent an indexed, ordered collection of various data types. When you start getting into more advanced Python content, you’ll quickly see how important it is to understand how to manipulate sequences of data.


Str

The Python data type str represents strings, which are arrays of unicode characters. In Python, strings are one or more characters within single quotes, double quotes, or triple quotes.


#Example of string (str) data types.

  • print(type(‘single quotes’))

  • print(type(“double quotes”))

  • print(type(‘’’triple quotes’’’))


Example of string Data Type


The output for each example will be <class ‘str’> indicating the data is an object of the string class.


Strings have an index, meaning it’s possible to call a particular position within the string. In Python, indexes always start with 0, not 1. To select a position, use brackets [] and the index number. For example, we’d find the second character of the string using [1]. Let’s look at some code examples to illustrate this concept.

We can access a particular position in the string by calling its index value.


string = ‘Career_rocks!’

#This will return the 3rd character in a string.

  • print(string[2])

#A range of values can be returned. This is called a substring.

  • print(string[0:6])

#Negative numbers can be used to return the end of the string first.

#-1 would return the last character in the string

  • print(string[-1])

#You can even print a string in reverse!

  • print(string[::-1])


Example of String Data type


The output for each example is r, Career, !, and !skcor_reeraC. Understanding how to manipulate strings is very important for data analysis and data science because so much data is text.


List

The Python data type list is represented by a sequence of data inside brackets [] and is mutable. You can also use the list() function to transform objects into a list. The data in the list do not need to be the same data types. Lists are used heavily in Python and can be used efficiently, transforming an operation that would sometimes require multiple lines.


#Example of Python data type list

  • print(type([1,3,’g’, 5, ‘z’]))

#You can have lists within lists!

  • print(type([‘a’, ‘b’, 3, [123, 9, 2], ‘y’]))

The output for each example will be <class ‘list’> indicating the data is an object of the list class.

Just like strings, lists have indexes. When a list contains another iterable object, a second set of brackets with the index number can be used to access the sub-item.


#Lists have indexes

var_list = [‘a’, ‘b’, 3, [123, 9, 2], ‘y’]

  • print(var_list[0])

#The sublist index can be accessed with an additional []

  • print(var_list[3][1])


Example of List Data Type


The output of each example will be a and 9. In the second example, we get 9 because we’re accessing index value 3, which is a list within a list. Then we access the item at index value 1, which is 9. Remember that indexes always start with 0 in Python.


Tuple

The Python data type tuple is similar to a list in that it is an ordered sequence of objects. The differences are a tuple is represented using parenthesis () and it is immutable. That means tuples cannot be changed after they are created. You can also use the tuble() function to transform objects into a tuple.


#Example of Python data type tuple

  • print(type((1,3,’g’, 5, ‘z’)))

#You can have tuples within tuples!

  • print(type((‘a’, ‘b’, 3, (123, 9, 2), ‘y’)))

The output for each example will be <class ‘tuple’> indicating the data is an object of the tuple class. Just like you can do with strings and lists, items within the tuple can be selected by referring to the index number.


Sequence Python data types takeaways:

  • The Python data types that represent sequences are str, list, and tuple.

  • Sequences are ordered collections.

  • Strings (str) are sequences of characters inside single, double, or triple quotes.

  • Lists are sequences of objects inside brackets [] and are mutable.

  • Tuples are sequences of objects inside parenthesis () and are immutable.


Python data types: Set

Sets in Python represent a distinct, unordered collection of objects. That means every item in a set has a unique value. Since sets are unordered, they have no index. To create a set Python data type, use the set () function, putting an iterable like a list inside the function.

#Example of Python data type set

  • var_set_list = [1,1,2,3,4,4,5,’a’,’a’,’b’,’c’]

  • var_set = set(var_set_list)

  • print(type(var_set))

The output for the example will be <class ‘set’> indicating the data is an object of the set class.

Notice we assign a list with several duplicate values to the variable var_set_list, and then transform the variable into a set using the set() function. If we print the set, you might notice something unusual.

  • print(var_set)

The output might look something like this {1, 2, 3, 4, 5, ‘c’, ‘b’, ‘a’}. Notice the values aren’t in the same order as they were in the list. That’s because sets do not guarantee the data remain in any particular order.


Set Python data type takeaways

  • Sets are collections of distinct objects and are unordered.

  • Create a set by using the set() function, wrapping an iterable collection like a list.

  • Sets have no index


Python data types: Dictionary

As you get into more advanced programming, you’ll see the dict Python data type is used a lot. A dictionary holds data in key: value pairs and is considered an unordered collection. That means it doesn’t have an index. A dictionary can be created by placing the key:value pair inside curly brackets {} or by using the dict() function. Remember to use a colon to separate the key from the value, and use commas to separate each pair from one another.


#Example of Python data type dictionary

  • var_dictionary = {‘person1’:’Kinjal’, ‘person2’:’Krisna’}

  • print(type(var_dictionary))

The output for the example will be <class ‘dict’> indicating the data is an object of the dictionary class.

There are several ways to access items in a dictionary. We can use the keys() function to output all of the keys.

  • print(var_dictionary.values())

Since the dictionary has no index, you can access a specific value by passing the key to the dictionary variable similar to how you’d use an index number to look up an item in a list.

  • print(var_dictionary[‘person1’])

  • print(var_dictionary[‘person2’])


Example of Dictionary Data type


Dictionary Python data type takeaways:

  • The dict data type stores a collection of objects as key: value pairs.

  • Dictionaries are unordered and don’t have indexes.

  • Return a list of keys using the keys () function.

  • Return a list of values using the values () function.

  • Return a specific value by calling a key from the dictionary like you would an item’s index in a list.

Key Points

There are several different Python data types you’ll need to master as you begin your journey into Python programming.

  • Booleans allow you to evaluate whether an expression is true or false.

  • The numeric data types represent things like whole numbers, decimals, and even complex numbers.

  • The sequence data types are used to represent things like strings, lists and tuples.

  • Sets are a useful data type when you need a distinct collection of values, and

  • Dictionaries are useful when you need to map values in a key: value pair.

91 views0 comments

Recent Posts

See All
bottom of page