Data Types and Conversion

PythonPythonBeginner
Practice Now

Introduction

In the previous lab we learned the basics of Python. In this lab we will learn the basic Python data types and how to convert between them.

Achievements

  • Data Types
  • type() Function
  • Type Conversion

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/type_conversion -.-> lab-290726{{"`Data Types and Conversion`"}} python/build_in_functions -.-> lab-290726{{"`Data Types and Conversion`"}} end

Data Types

Data types are the building blocks of programming languages, and Python offers several fundamental types. Let's explore them using the Python interpreter.

Let's open the Python interpreter by typing python3 in the terminal.

lab-data-types-and-conversion-1-1

Python's primary data types include integers, floats, Booleans, and strings. The type() function helps identify the type of a value, it will output the type of value we pass to it.

String

A string is a sequence of characters enclosed in either double or single quotes:

>>> print("Hello World")
Hello World
>>> print('Hello World')
Hello World
>>> type("Hello World")
<class 'str'>               ## it means it's a string

Number

Numbers in Python can be integers or floats. Integer is a whole number. Float is a number with a decimal point. For example, 42 is an integer, and 3.14 is a float.

>>> print(42)
42
>>> print(3.14)
3.14
>>> type(42)
<class 'int'>              ## it means it's an integer
>>> type(3.14)
<class 'float'>            ## it means it's a float

Boolean

A boolean is a value that can be either True or False. It is often used to represent the result of a comparison. For example, 1 < 2 is True, and 1 > 2 is False.

>>> print(1 < 2)
True
>>> print(1 > 2)
False
>>> type(True)
<class 'bool'>            ## it means it's a boolean

Understanding Python's data types is essential for working with diverse values and performing operations tailored to their characteristics.

Type Conversion

In Python, type conversion involves changing the data type of a variable. This is useful for operations specific to certain data types or when ensuring a variable has a particular type.

Explicit Type Conversion

Explicit type conversion or type casting is achieved using built-in functions:

  • int(x): Converts x to an integer.
  • float(x): Converts x to a float.
  • str(x): Converts x to a string.
  • bool(x): Converts x to a boolean.
>>> x = 10.5
>>> int_x = int(x)      ## explicit conversion to integer
>>> str_x = str(x)      ## explicit conversion to string
>>> bool_x = bool(x)    ## explicit conversion to boolean

>>> print(int_x)
10
>>> print(str_x)
10.5
>>> print(bool_x)
True

Here, the float x is explicitly converted to an integer, string, and boolean using respective conversion functions.

Implicit Type Conversion

Implicit type conversion or type coercion occurs automatically when Python encounters operands of different types in an expression. It tries to convert them to a common type before performing the operation.

>>> x = 10           ## integer
>>> y = 3.14         ## float
>>> result = x + y   ## implicit conversion of x to float
>>> print(result)
13.14

Here, the integer x is implicitly converted to a float before addition.

Type Conversion Error

Not all conversions are valid. Attempting to convert incompatible types can result in an error:

>>> x = input()
i
>>> type(x)
<class 'str'>           ## By default, `input()` treats all input as strings.
>>> int_x = int(x)      ## error: cannot convert string to integer

Data loss is a consideration; converting between types may lead to precision or information loss:

>>> x = 3.14
>>> int_x = int(x)      ## data loss: decimal part is discarded
>>> print(int_x)
3

Summary

Congratulations! You have completed this Lab.

In this lab, you learned:

  • Basic data types in Python.
  • type() function.
  • Type conversion.

Other Python Tutorials you may like