Python Forum
Intro to Python for Data Science with Datacamp - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Intro to Python for Data Science with Datacamp (/thread-12243.html)



Intro to Python for Data Science with Datacamp - Sabrusura - Aug-15-2018

Hi all,

I am currently following the course Intro to Python for Data Science with Datacamp.com. It is pretty challenging for me as someone who does not know much about coding and such things. But it is interesting to learn more about Python.

My exercise is this:
(1)Print out the second element from the areas list, so 11.25.
(2)Subset and print out the last element of areas, being 9.50. Using a negative index makes sense here!
(3)Select the number representing the area of the living room and print it out.

This is my script based on the exercise:
# Create the areas list
areas = ["hallway", 11.25, "kitchen", 18.0, "living room", 20.0, "bedroom", 10.75, "bathroom", 9.50]

# Print out second element from areas
print(areas[11.25])

# Print out last element from areas
print(areas[9.50])

# Print out the area of the living room
print(areas[20.0])
I get an error on line 5, but I don't really understand what I am doing wrong. Or if I might misunderstood the exercise.

Thanks in advance


RE: Intro to Python for Data Science with Datacamp - micseydel - Aug-15-2018

The code you've written implies that you already know the answer. But you want to write code that can figure out the answer independent of your knowledge. Do you know how to get your code to print out "11.25" using only print, areas, and a number to indicate the index? e.g. you need to print the second element, and this would print the first
print(areas[0])
As for the third exercise - it really depends on how advanced your Python is. You might want to iterate over the indexes, find "living room" and then add one to that index to get what you really want.

If you still have trouble, post back your new code and we can try to help you more form ther e :)


RE: Intro to Python for Data Science with Datacamp - Sabrusura - Aug-19-2018

Thanks for your reaction, micseydel!

So, for the third exercise, I think the code would be:
# Print out last element from areas
print(areas[-1])
Is that correct? Undecided


RE: Intro to Python for Data Science with Datacamp - micseydel - Aug-19-2018

(Aug-19-2018, 01:38 AM)Sabrusura Wrote: Is that correct?
You should be able to test it to tell, right? If you have some lack of confidence even after testing it, you should try to come up with experiments that might change your confidence, e.g. try using -1 on a list with a changed last element.

(Short answer: yes.)


RE: Intro to Python for Data Science with Datacamp - Sabrusura - Aug-21-2018

Thanks a lot Micseydel, your answer has been very helpful! Will try to come up with experiments. Good suggestion :)