Introduction
In this lab, you will learn how to check if a variable is a list in Python. This involves understanding the fundamental data structure of lists, which are ordered and mutable collections of items.
The lab guides you through creating different types of lists using VS Code in the LabEx environment, including lists of numbers, strings, and mixed data types. You'll then explore accessing elements within a list using their index. The lab will further demonstrate how to identify lists using built-in Python functions like type() and isinstance().
Understand Lists
In this step, you will learn about lists, one of the most versatile and fundamental data structures in Python. Lists are used to store collections of items, which can be of any data type. They are ordered, meaning the items have a specific sequence, and mutable, meaning you can change their contents after creation.
Let's start by creating a simple list:
Open the VS Code editor in the LabEx environment.
Create a new file named
lists_example.pyin the~/projectdirectory.~/project/lists_example.pyAdd the following code to the file:
## Creating a list of numbers numbers = [1, 2, 3, 4, 5] print("List of numbers:", numbers) ## Creating a list of strings fruits = ["apple", "banana", "cherry"] print("List of fruits:", fruits) ## Creating a list of mixed data types mixed_list = [1, "hello", 3.14, True] print("List of mixed data types:", mixed_list)Here, we've created three different lists:
numberscontaining integers,fruitscontaining strings, andmixed_listcontaining a mix of data types.Run the script using the following command in the terminal:
python ~/project/lists_example.pyYou should see the following output:
List of numbers: [1, 2, 3, 4, 5] List of fruits: ['apple', 'banana', 'cherry'] List of mixed data types: [1, 'hello', 3.14, True]
Now, let's explore some common list operations:
Accessing elements: You can access elements in a list using their index (position). The index starts at 0 for the first element.
Add the following code to
lists_example.py:fruits = ["apple", "banana", "cherry"] print("First fruit:", fruits[0]) ## Accessing the first element print("Second fruit:", fruits[1]) ## Accessing the second element print("Third fruit:", fruits[2]) ## Accessing the third elementRun the script again:
python ~/project/lists_example.pyYou should see the following output:
First fruit: apple Second fruit: banana Third fruit: cherryModifying elements: You can change the value of an element in a list by assigning a new value to its index.
Add the following code to
lists_example.py:fruits = ["apple", "banana", "cherry"] fruits[1] = "grape" ## Changing the second element print("Modified list of fruits:", fruits)Run the script again:
python ~/project/lists_example.pyYou should see the following output:
Modified list of fruits: ['apple', 'grape', 'cherry']Adding elements: You can add elements to the end of a list using the
append()method.Add the following code to
lists_example.py:fruits = ["apple", "banana", "cherry"] fruits.append("orange") ## Adding an element to the end print("List with added fruit:", fruits)Run the script again:
python ~/project/lists_example.pyYou should see the following output:
List with added fruit: ['apple', 'banana', 'cherry', 'orange']
Understanding lists and how to manipulate them is crucial for writing effective Python programs.
Use type() to Identify
In this step, you will learn how to use the type() function in Python to identify the data type of a variable. Understanding data types is crucial for writing correct and efficient code. The type() function returns the type of an object.
Let's create a new Python file to explore the type() function:
Open the VS Code editor in the LabEx environment.
Create a new file named
type_example.pyin the~/projectdirectory.~/project/type_example.pyAdd the following code to the file:
## Using type() to identify data types number = 10 print("Type of number:", type(number)) floating_point = 3.14 print("Type of floating_point:", type(floating_point)) text = "Hello, LabEx!" print("Type of text:", type(text)) is_true = True print("Type of is_true:", type(is_true)) my_list = [1, 2, 3] print("Type of my_list:", type(my_list)) my_tuple = (1, 2, 3) print("Type of my_tuple:", type(my_tuple)) my_dict = {"name": "Alice", "age": 30} print("Type of my_dict:", type(my_dict))In this code, we're using
type()to determine the data type of different variables, including integers, floating-point numbers, strings, booleans, lists, tuples, and dictionaries.Run the script using the following command in the terminal:
python ~/project/type_example.pyYou should see the following output:
Type of number: <class 'int'> Type of floating_point: <class 'float'> Type of text: <class 'str'> Type of is_true: <class 'bool'> Type of my_list: <class 'list'> Type of my_tuple: <class 'tuple'> Type of my_dict: <class 'dict'>
The output shows the data type of each variable. For example, <class 'int'> indicates that the variable is an integer, <class 'str'> indicates a string, and so on.
Understanding the data type of a variable is essential for performing operations correctly. For example, you can't add a string to an integer directly without converting the string to an integer first. The type() function helps you identify these potential issues in your code.
Confirm with isinstance()
In this step, you will learn how to use the isinstance() function in Python to confirm if an object is an instance of a particular class or type. This is a more robust way to check data types compared to using type() directly, especially when dealing with inheritance.
Let's create a new Python file to explore the isinstance() function:
Open the VS Code editor in the LabEx environment.
Create a new file named
isinstance_example.pyin the~/projectdirectory.~/project/isinstance_example.pyAdd the following code to the file:
## Using isinstance() to confirm data types number = 10 print("Is number an integer?", isinstance(number, int)) floating_point = 3.14 print("Is floating_point a float?", isinstance(floating_point, float)) text = "Hello, LabEx!" print("Is text a string?", isinstance(text, str)) is_true = True print("Is is_true a boolean?", isinstance(is_true, bool)) my_list = [1, 2, 3] print("Is my_list a list?", isinstance(my_list, list)) my_tuple = (1, 2, 3) print("Is my_tuple a tuple?", isinstance(my_tuple, tuple)) my_dict = {"name": "Alice", "age": 30} print("Is my_dict a dictionary?", isinstance(my_dict, dict))In this code, we're using
isinstance()to check if each variable is an instance of the expected data type. Theisinstance()function returnsTrueif the object is an instance of the specified type, andFalseotherwise.Run the script using the following command in the terminal:
python ~/project/isinstance_example.pyYou should see the following output:
Is number an integer? True Is floating_point a float? True Is text a string? True Is is_true a boolean? True Is my_list a list? True Is my_tuple a tuple? True Is my_dict a dictionary? True
Now, let's consider a scenario with inheritance:
Add the following code to
isinstance_example.py:class Animal: pass class Dog(Animal): pass animal = Animal() dog = Dog() print("Is animal an Animal?", isinstance(animal, Animal)) print("Is dog a Dog?", isinstance(dog, Dog)) print("Is dog an Animal?", isinstance(dog, Animal)) print("Is animal a Dog?", isinstance(animal, Dog))Run the script again:
python ~/project/isinstance_example.pyYou should see the following output:
Is animal an Animal? True Is dog a Dog? True Is dog an Animal? True Is animal a Dog? FalseAs you can see,
isinstance()correctly identifies that aDogis also anAnimalbecauseDoginherits fromAnimal. This is whereisinstance()is more powerful than directly comparing types withtype().
Using isinstance() makes your code more flexible and robust when dealing with different data types and inheritance.
Summary
In this lab, you learned about lists, a fundamental data structure in Python used to store ordered and mutable collections of items of various data types. You created lists containing numbers, strings, and mixed data types, and printed them to the console.
The lab also introduced accessing list elements using their index, starting from 0 for the first element. You accessed and printed specific elements from a list of fruits to demonstrate this concept.



