PEB

Phil Elliott Blog

Data Types in Python

Main Article Image

In order to be a competent programmer, you must understand data types and how to use them. In this article, you will find the basics of data types as well as enough information to get started using them.

What is an int?

An integer it basically a whole number with no decimal places. This quantity can be either positive or negative, and it can be as long as you want.

# an integer is a whole number
age = 35

# change the type to ineger
number = '5'
new_number = int(number)

print(type(number), type(new_number))

# output
# <class 'str'> <class 'int'>

The figure above depicts an instance of how to how to utilize the int() method to convert a number into an int.

What is a float?

# a float is a number that uses a decimal point
money = 4.62

# change the type to float
number = '5'
new_number = float(number)

print(new_number)

print(type(number), type(new_number))

# output
#5.0
# <class 'str'> <class 'float'>

A float is a number with decimal points. The code snippet above demonstrates an example as well as how to use the float() function to convert a number into a float.

What is a bool?

# a boolean has a result of either true or false
hungry = True
tired = False

print(type(tired))
# output = <class 'bool'>

print(1 + 1 == 2)
# output = True

A boolean is a type of data that either has a true or false value. A boolean can only have the values true or false. An example and how to use the bool() method are shown in the figure above.

What is a string?

# a string contains characters wrapped in parenthesis
greeting = 'Hello World'

print(greeting)
#output = Hello World


# you can change other data types into a string

number = 5
string_number = str(number)

print(type(string_number))
# output = <class 'str'>

A string is a sequence of characters. You can create a string by surrounding it with single or double quotes. In the picture above, you can see an example as well as how to use the str() method to turn something into a string.

What is a list?

# you can store multiple items in a list

colors_list =  ['blue', 'red', 'orange', 'green', 'brown']

scores_list = [85, 52, 88, 97, 100, 17]

print(type(colors))

#output = <class 'list'>

# items in a list can be changed

colors[0] = 'black'

print(colors)

# output = ['black', 'red', 'orange', 'green', 'blown']

A list is a way to bundle many things in one variable. The items may be enclosed in square brackets [ ], with commas between them, to construct a list.

What is a tuple?

# you can store multiple items in a tuple, but they can't be changed

colors_tuple =  ('blue', 'red', 'orange', 'green', 'brown')

scores_tuple = (85, 52, 88, 97, 100, 17)

print(type(colors))

#output = <class 'tuple'>

Tuples and lists are both data structures that can be used to store multiple items. The main difference between tuples and lists is that tuples are immutable, while lists are mutable. This means that once a tuple is created, it cannot be changed. Tuples are wrapped in parentheses ().

What is a set?

# you can store multiple items in a set, but they will be unordered 

colors_set =  {'blue', 'red', 'orange', 'green', 'brown'}

print(colors_set)

#output = {'brown', 'blue', 'green', 'orange', 'red'}


# sets don't allow for any duplicates

drink_set = {'soda', 'water', 'milk', 'soda', 'coffee'}

print(drink_set)

#output = {'water', 'coffee', 'soda', 'milk'}

A set is similar to a list, but it differs in several ways. It's not ordered like a list, and there are no duplicates. The set is represented by curly brackets { }.

What is a dict?

# you can store multiple keys and values in a dictionary

fav_movies = {
  'Horror': 'The Shining',
  'Comedy': 'Step Brothers', 
  'Drama': 'Moonlight'
}

print(fav_movies)

#output = {'Horror': 'The Shining', 'Comedy': 'Step Brothers', 'Drama': 'Moonlight'}

print(type(fav_movies))

#output = <class 'dict'>


# you can't have duplicates in a dictionary 

class_scores = {
  'math': 87,
  'english': 74,
  'music' : 92,
  'math': 87
}

print(class_scores)

# output = {'math': 87, 'english': 74, 'music': 92}

Python has a specific way to handle ordered collections of data known as a dictionary. A collection of data is divided into key:value pair, and it's called a dictionary. Dictionaries are enclosed in curly brackets { }.