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.

VERY IMPORTANT

Make sure to submit the tasks with all the outputs displayed under the cell if there’s any.

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 1A: 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.
# Sample output: ['Learning', 'Python', 'is', 'so', 'much', 'fun!']

1.2 Substitutions with F-strings#

Given the following variables:

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

Use f-strings to print the following statement exactly:

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 “DATA301”:

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

# Your answer here.
# Sample output: 'DATA301'

1.4 Dictionary Indexing#

Given this nest dictionary grab the word “DATA301”:

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

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?” where language displays the actual content in this variable if language is anything else.

language = "java"

# Your answer here.
# If language is "Java", the output would be "I love coffee!"
# If language is "PYTHON", the output would be "Are you a snake?"
# If language is anything else, the output would be anything this language is, for example, if language is "R", output is "R".
# You don't need to display the results of your test for EACH condition, just make sure your code works like this.

Task 1B: Python Fundamentals II (numpy and random)#

In this section, we will practice using the numpy library. First, import the numpy library

import numpy as np
# Your Solution here

1.6: Create a vector#

Task: Create an empty vector of size 10 filled with NaN.

Hint: you need to use empty() method or the zeros() method and fill the vector with NaN

# Your Solution here
# Sample output: array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan])

1.7: Working with Vectors#

Task: Create a random vector of size 10 and then find its mean, max, min, and sum value.

Hint: for random vector you need to use np.random.random() and for the mean, max, min, sum you need to use build-in numpy methods mean(),max(),min(),sum().

Sample output (Your numbers will be different)#

[0.66698639 0.32937943 0.12074085 0.21788496 0.75628444 0.56461791 0.38162184 0.60966053 0.00491222 0.80007239]
The max is: 0.800
The min is: 0.005
The sum is: 4.452
The mean is: 0.445

# Your Solution Here

1.8: More vectors#

Task: This is a multi-step question. Read all the directions before you start working on the question and plan out your solution.

  • First, using numpy, create a vector of size 15 which contains random values ranging from 10 to 90.

  • Then, replace the maximum value of your vector with 500.

  • Then, replace and the minimum value with -500.

  • Print the vector (sorted by ascending order) and its mean before replacement, as well as the sorted vector after replacement with the new mean.

Hint: To do this problem as intended, you may need to use the following numpy functions: copy(), sort(), argmax(), and argmin(). Also, don’t forget about f-strings and triple-quoted strings for printing.

Sample output#

Before number replacement

vector: [12 14 14 19 22 25 25 28 28 35 45 47 68 69 73]

vector_mean = 34.93

After number replacement

vector: [-500 14 14 19 22 25 25 28 28 35 45 47 68 69 500]

new_vector_mean = 29.27

# Your solution here