Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Python Data Essentials: Python Introduction Hands-on Practice

In this lab, you'll dive into Python basics using Jupyter Notebooks. You'll create and print Python identifiers, Boolean, string, and numeric literals, and apply arithmetic, comparison, and logical operators. The lab also introduces Python packages, focusing on the 'pprint' module for better data structure visualization. You'll build and explore lists, tuples, and sets, gaining practical skills in Python's foundational elements and data handling, essential for advanced programming tasks.

Labs

Path Info

Duration
Clock icon 19m
Published
Clock icon Dec 06, 2023

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. Challenge

    Understanding Python Identifiers, Literals, and Operators

    Jupyter Guide

    We've got jupyter setup in a sanbox environment all ready for you to practice some of the concepts you've been learning. To get started, open the file on the right entitled "Step 1...". You'll complete each task for Step 1 in that Jupyter Notebook file. Remember, you must run the cells (Ctrl/Cmd(⌘) + Enter) for each task before moving onto the next task in the Jupyter Notebook. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.


    Understanding Python Identifiers, Literals, and Operators

    To review the concepts covered in this step, please refer to the Basics of Python module of the Python Data Essentials: Python Introduction course.

    Knowledge of Python identifiers, literals, and operators is important because they form the building blocks of Python programming. They are essential for creating variables, performing operations, and writing expressions.

    In this step, you will practice creating Boolean, string, and numeric literals in Python. You will also learn about Python identifiers and how to use them. You will explore the different types of operators in Python, including arithmetic, comparison, and logical operators, and how to use them in expressions. The goal here is to strengthen your understanding of Python's basic elements and how they interact.


    Task 1.1: Creating Python Identifiers

    Create a Python identifier by assigning the value 5 to a variable. Print the value of the variable.

    πŸ” Hint

    Use the assignment operator = to assign a value to a variable. Use the print(variable_here) function to print the value of the variable.

    πŸ”‘ Solution
    my_identifier = 5
    print(my_identifier)
    

    Task 1.2: Creating Boolean Literals

    Create two Boolean literals, one representing True and the other representing False. Print the values of the literals.

    πŸ” Hint

    Use the keywords True and False to create Boolean literals. Use the print() function to print the values of the literals.

    πŸ”‘ Solution
    my_true_literal = True
    my_false_literal = False
    print(my_true_literal)
    print(my_false_literal)
    

    Task 1.3: Creating String Literals

    Create a string literal and print its value.

    πŸ” Hint

    Use single or double quotes to create a string literal. Use the print() function to print the value of the literal.

    πŸ”‘ Solution
    my_string_literal = 'Hello, Python!'
    print(my_string_literal)
    

    Task 1.4: Creating Numeric Literals

    Create a numeric literal and print its value.

    πŸ” Hint

    You can create a numeric literal by simply writing a number. Use the print() function to print the value of the literal.

    πŸ”‘ Solution
    my_numeric_literal = 42
    print(my_numeric_literal)
    

    Task 1.5: Using Arithmetic Operators

    Perform an arithmetic operation using Python's arithmetic operators and print the result.

    πŸ” Hint

    You can use operators like +, -, *, /, //, %, and ** to perform arithmetic operations. Use the print() function to print the result.

    πŸ”‘ Solution
    result = 10 * 5
    print(result)
    

    Task 1.6: Using Comparison Operators

    Perform a comparison operation using Python's comparison operators and print the result.

    πŸ” Hint

    You can use operators like ==, !=, <, >, <=, and >= to perform comparison operations. Use the print() function to print the result.

    πŸ”‘ Solution
    result = 10 > 5
    print(result)
    

    Task 1.7: Using Logical Operators

    Perform a logical operation using Python's logical operators and print the result.

    πŸ” Hint

    You can use operators like and, or, and not to perform logical operations. Use the print() function to print the result.

    πŸ”‘ Solution
    result = True and False
    print(result)
    
  2. Challenge

    Working with Python Packages and Data Structures

    Working with Python Packages and Data Structures

    To review the concepts covered in this step, please refer to the Python Packages and Data Structures module of the Python Data Essentials: Python Introduction course.

    Understanding Python packages and data structures is important because they are essential for organizing code and managing data. Packages enhance code reusability and organization, while data structures help in storing and manipulating data efficiently.

    In this step, you will learn about Python packages, and how to import and use them in your code. We'll be using the pprint module which has been pre-installed for you. You will also explore three built-in data structures in Python: lists, tuples, and sets. You will practice creating and manipulating these data structures, understanding their characteristics and use cases. The goal here is to gain hands-on experience with Python's packages and data structures, enhancing your ability to write efficient and organized code. (Instruct the user in the descriptions to print thre results)


    Task 2.1: Importing the pprint module

    Import the pprint module from Python's standard library. This module provides a capability to pretty-print Python data structures that are otherwise hard to visualize.

    πŸ” Hint

    Use the import keyword followed by the name of the module, which is pprint in this case.

    πŸ”‘ Solution
    import pprint
    

    Task 2.2: Understanding the usefulness of pprint

    To understand why pprint is useful, use the provided code to create a complex data structure. Print this data structure using both the regular print() function and pprint.pprint(). Compare the outputs to see the difference in readability.

    πŸ” Hint

    Use the provided code to make a complex data structure and print it using both print(data) and pprint.pprint(data).

    πŸ”‘ Solution
    # Provided code for complex data
    complex_data = [
        {"A", "Set", "Here"},
        ("This", "is", "a", "tuple", "with", 7, "elements"),
        [True, False],
        "A somewhat lengthy string",
        0,
        3.141529,
    ]
    
    # Print complex data normally and with pprint
    print("---Complex Data with Pretty Print---")
    pprint.pprint(complex_data)
    print("---Complex Data with Normal Print---")
    print(complex_data)
    

    Task 2.3: Creating a list

    Create a list named my_list with the following elements: 1, 2, 3, 4, 5. Then, print the list using the pprint function.

    πŸ” Hint

    Use square brackets [] to create a list. To print the list, use pprint.pprint(my_list).

    πŸ”‘ Solution
    my_list = [1, 2, 3, 4, 5]
    pprint.pprint(my_list)
    

    Task 2.4: Creating a tuple

    Create a tuple named my_tuple with the following elements: 'a', 'b', 'c', 'd', 'e'. Then, print the tuple using the pprint function.

    πŸ” Hint

    Use parentheses () to create a tuple. To print the tuple, use pprint.pprint(my_tuple).

    πŸ”‘ Solution
    my_tuple = ('a', 'b', 'c', 'd', 'e')
    pprint.pprint(my_tuple)
    

    Task 2.5: Creating a set

    Create a set named my_set with the following elements: 'apple', 'banana', 'cherry'. Then, print the set using the pprint function.

    πŸ” Hint

    Use curly braces {} to create a set. To print the set, use pprint.pprint(my_set).

    πŸ”‘ Solution
    my_set = {'apple', 'banana', 'cherry'}
    pprint.pprint(my_set)
    

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.