Variable Type Conversion

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to convert variables between different data types in Python. We will start with simple examples and gradually move on to more complex ones.

Achievements

  • str() function
  • int() function
  • float() function
  • type() function
  • tuple() function
  • list() function
  • dict() function
  • set() function
  • map() function

Converting Integers to Strings

The first step in type conversion is converting integers to strings.

Strings are a sequence of characters. The str() function can be used to convert integers to strings.

Open up a new Python interpreter.

python3

Type the following code:

## Example 1:
x = 5
y = str(x)
print(y) ## prints "5"
print(type(y)) ## prints <class 'str'>

## Example 2:
x = 15
y = str(x)
print(y) ## prints "15"
print(type(y)) ## prints <class 'str'>

Converting Strings to Integers

The next step is converting strings to integers.

Integers are whole numbers. The int() function can be used to convert strings to integers.

## Example 1:
x = "5"
y = int(x)
print(y) ## prints 5
print(type(y)) ## prints <class 'int'>

## Example 2:
x = "15"
y = int(x)
print(y) ## prints 15
print(type(y)) ## prints <class 'int'>

Converting Strings to Floats

The next step is converting strings to floats.

Floats are numbers with a decimal point. The float() function can be used to convert strings to floats.

## Example 1:
x = "5.5"
y = float(x)
print(y) ## prints 5.5
print(type(y)) ## prints <class 'float'>

## Example 2:
x = "15.45"
y = float(x)
print(y) ## prints 15.45
print(type(y)) ## prints <class 'float'>

Converting Lists to Tuples

A tuple is a collection that is ordered and unchangeable. In Python, tuples are written with round brackets.

The tuple() function can be used to convert a list to a tuple.

## Example 1:
x = [1, 2, 3, 4, 5]
y = tuple(x)
print(y) ## prints (1, 2, 3, 4, 5)
print(type(y)) ## prints <class 'tuple'>

## Example 2:
x = ["apple", "banana", "orange"]
y = tuple(x)
print(y) ## prints ("apple", "banana", "orange")
print(type(y)) ## prints <class 'tuple'>

Converting Lists of Tuples to Dictionaries

A dictionary is a collection that is unordered, changeable and indexed. In Python, dictionaries are written with curly brackets, and they have keys and values.

The dict() function can be used to convert a list of tuples to a dictionary, where the first item in each tuple is the key and the second item is the value.

## Example 1:
x = [("a", 1), ("b", 2), ("c", 3)]
y = dict(x)
print(y) ## prints {"a": 1, "b": 2, "c": 3}
print(type(y)) ## prints <class 'dict'>

## Example 2:
x = [("fruit", "apple"), ("color", "red")]
y = dict(x)
print(y) ## prints {"fruit": "apple", "color": "red"}
print(type(y)) ## prints <class 'dict'>

Converting Tuples to Sets

In this step, we will learn how to convert tuples to sets. Sets are a collection that is unordered and unindexed. In Python, sets are written with curly brackets.

The set() function can be used to convert tuples to sets.

Type the following code:

## Example 1:
x = (1, 2, 3)
y = set(x)
print(y) ## prints {1, 2, 3}
print(type(y)) ## prints <class 'set'>

## Example 2:
x = (1, 2, 3, 1, 2, 3)
y = set(x)
print(y) ## prints {1, 2, 3}
print(type(y)) ## prints <class 'set'>

Advanced Examples

In this step, we will look at some more advanced examples of type conversion. We will learn how to convert lists to strings, tuples to lists, and more.

## Example 1: Converting a list of integers to a string
x = [1, 2, 3, 4, 5]
y = "".join(map(str, x))
print(y) ## prints "12345"

## Example 2: Converting a tuple of strings to a list
x = ("apple", "banana", "orange")
y = list(x)
print(y) ## prints ["apple", "banana", "orange"]

Summary

In this lab, we have learned how to convert variables between different data types in Python. We have looked at examples of converting integers to strings, strings to integers, strings to floats, lists to tuples, and lists of tuples to dictionaries.

We have also looked at some more advanced examples of type conversion, such as converting a list of integers to a string, and a tuple of strings to a list.

With this knowledge, you should be able to convert variables between different data types in your Python code and use them accordingly. Understanding and mastering the skill of type conversion is essential to write more effective and efficient Python code.

Other Python Tutorials you may like