Task 1 - Introduction to Python#

In this lab you will be working on python basics.

I have recorded a video to get you started with this lab: Click here to view the video. The lab is slightly different and updated, but the idea is very similar.

This lab must be completed individually.

Where provided, try your best to match the Sample Output as best as you can.

Accept the lab#

To accept this lab on GitHub Classroom, go to Canvas –> Course Content –> GitHub Classroom Links.

Objectives#

  1. Practice Python loops and conditions

  2. Practice Python lists and dictionaries

  3. Practice string manipulation in Python

  4. Practice importing and using the Pandas module

Task 1: Python Fundamentals I#

The following problems are practice problems that will help you learn some basic python syntax. They have been adapted from Tomas Beuzen’s Python Programming for Data Science textbook here under a Creative Commons license.

The solutions to the problems are available at the link, but I encourage you to try doing the problems on your own before you check the solutions!

1.1 Splitting Strings#

Split the following string into a list by splitting on the space character:

s = "Learning Python is so much fun!"

# Your answer here.

1.2 Substitutions with F-strings#

Given the following variables:

thing = "light"
speed = 299792458  # m/s

Use f-strings to print:

The speed of light is 2.997925e+08 m/s.
thing = "light"
speed = 299792458  # m/s

# Your answer here.

1.3 List Indexing#

Given this nested list, use indexing to grab the word “COSC301”:

l = [10, [3, 4], [5, [100, 200], [23, ["COSC301"], 27], 11], 1, 7]
l = [10, [3, 4], [5, [100, 200], [23, ["COSC301"], 27], 11], 1, 7]

# Your answer here.

1.4 Dictionary Indexing#

Given this nest dictionary grab the word “COSC 301”:

d = {
    "outer": [
        1,
        2,
        3,
        {"inner": ["this", "is", "inception", {"inner_inner": [a, b, c, "COSC 301", 1, 2, 3]}]},
    ]
}
d = {
    "outer": [
        1,
        2,
        3,
        {"inner": ["this", "is", "inception", {"inner_inner":  ['a', 'b', 'c', "DATA 301", 1, 2, 3]}]},
    ]
}
# Your answer here.

1.5 Conditional Statements#

Given the variable language which contains a string, use if/elif/else to write a program that:

  • return “I love coffee!” if language is "java" (any kind of capitalization)

  • return “Are you a snake?” if language is "python" (any kind of capitalization)

  • else return “What is language?” if language is anything else.

language = "java"

# Your answer here.