4. Type conversion

Quiz: 0/6

In Python, different kinds of data exist, like numbers, text, and more. These types of data are called data types. For example:

  • Integers int: Whole numbers like 5, 10, -3
  • Floats float: Decimal numbers like 3.14, 5.0
  • Strings str: Text like "hello", "Python"

Sometimes, you might want to convert one data type to another. This process is called type conversion. Python allows you to convert data from one type to another, which can be helpful when performing operations with mixed types.

How do you convert types?

You can convert a value to a different type using type casting functions. Here are the most common ones:

int(): Converts a value to an integer (whole number).

x = "10"
y = int(x)  # Converts string "10" to integer 10
print(y)  # Output: 10

float(): Converts a value to a float (decimal number).

x = "3.14"
y = float(x)  # Converts string "3.14" to float 3.14
print(y)  # Output: 3.14

str(): Converts a value to a string (text).

x = 100
y = str(x)  # Converts integer 100 to string "100"
print(y)  # Output: "100"

Why do we need type conversion?

Sometimes, Python might automatically convert types for us, but there are cases where you might need to manually convert them. For example:

User input: When a user enters data (like from the keyboard), Python reads it as a string, even if it’s a number. So if you want to do math with it, you need to convert it to an integer or float.

age = input("Enter your age: ")  # This is a string
age = int(age)  # Now it's an integer, so you can use it in math

Performing operations: If you want to add a number and a string, you can’t do that directly. You might need to convert the string to a number first.

A simple example

Let’s say you want to add a number to a string that represents a number:

x = "5"  # This is a string
y = 10   # This is an integer

# To add them, you need to convert the string to an integer
result = int(x) + y
print(result)  # Output: 15

Summary

Type conversion helps you work with different data types by turning one type into another. It’s like changing a tool in your toolbox to fit the job you need to do! Python makes this easy with functions like int(), float(), and str(). Just remember to use the correct function for the job!

Hands-On Practise

Exercise: Type conversion in action

You are given some variables, but their types need to be adjusted to perform the correct operations. Your task is to fix the code by using the appropriate type conversion functions.

# Given variables
name = "Alex"
age = "25"  # This is a string
current_year = 2024  # This is an integer

# Calculate the year when Alex will turn 100
years_to_100 = 100 - age  # Fix this line
year_turns_100 = current_year + years_to_100  # Fix this line

# Create the final message
message = "Hello " + name + "! You will turn 100 years old in the year " + year_turns_100  # Fix this line

# Print the result
print(message)

Your task

  1. Identify where type conversions are needed.
  2. Fix the code so it runs without errors.
  3. Run the corrected code to display the expected output.
Output:

Quizzes: 0/6

Question 1:

What is type conversion in Python?

  • Changing a variable's value.
  • Automatically detecting data types in Python.
  • Manually or automatically converting data from one type to another.
  • Removing a data type from a variable.

Question 2:

Which function is used to convert a string to an integer?

  • float()
  • str()
  • int()
  • bool()

Question 3:

What will be the output of the following code? `python x = "3.14" y = float(x) print(y) `

  • 3.14
  • "3.14"
  • Error
  • 3

Question 4:

Why is type conversion necessary when dealing with user input?

  • Because user input is always an integer.
  • Because user input is always a string.
  • Because Python cannot read user input directly.
  • Because user input is always a float.

Question 5:

What will be the output of the following code? `python x = "5" y = 10 result = int(x) + y print(result) `

  • Error
  • 510
  • 15
  • "15"

Question 6:

Which of these is NOT a valid reason to use type conversion?

  • To perform calculations with user input.
  • To change a string to a list.
  • To concatenate a number and a string.
  • To add a string and an integer directly.

Feedback

Share your thoughts about this lesson

Discussion

Please log in to create a discussion.
Previous lessonData typesNext lessonArithmetic Operators