3.1. Exercises

Range Questions

numericals = []
for j in range(30,60,5):
    numericals.append(j)
numericals

Coding questions

Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Practice Iterating using Range

Let’s make a loop again but this time let’s practice using the range() verb.

Tasks:

  • Iterate over a range from 50 to 10, stepping down 4 integers at a time.
  • Append the square of each number for each iteration to square_list.
  • Display the value of square_list.
Hint 1
  • Are you using range(50,10,-4)?
  • Are you starting your loop with for and using a colon for the first line?
  • Are you indenting each line of code in the loop?
Fully worked solution:



Applying Range with Dataframes

Let’s read in multiple dataframes together and concatenate them vertically to one large dataframe using a loop and the range() function.

Tasks:

  • There are 4 dataframes named pkm1.csv to pkm4.csv. that we wish to load in and vertically concatenate together.
  • Fill in the blanks so the code reads in each dataframe according to their differing file name and concatenates them together.
  • Display the final dataframe.
Hint 1
  • Are you using pd.concat()?
  • Are you adding .csv to the string object?
  • Are you using pd.read_csv()?
  • Are you indenting each line of code in the loop?
Fully worked solution: