Python Forum
Intro to Python for Data Science with Datacamp
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Intro to Python for Data Science with Datacamp
#1
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
Reply
#2
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 :)
Reply
#3
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
Reply
#4
(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.)
Reply
#5
Thanks a lot Micseydel, your answer has been very helpful! Will try to come up with experiments. Good suggestion :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Interesting Intro to python problem I can't solve. Honestworker 5 12,296 Mar-04-2021, 02:05 AM
Last Post: BashBedlam
  Intro python using lists Rustypotatoes 2 3,850 Sep-17-2020, 08:30 PM
Last Post: jefsummers
  Python Assignment 3 - Applied Data Science - 2.3 eyavuz21 8 4,971 Jun-06-2020, 08:59 AM
Last Post: eyavuz21
  Applied Data Science with Python - homework 2.2 (Weather plotting) eyavuz21 4 3,385 Jun-03-2020, 07:09 PM
Last Post: eyavuz21
  Foundations of Data Science Data 8.2x tryingtolearnpython 2 3,663 Jul-23-2018, 02:48 AM
Last Post: tryingtolearnpython
  Question code from datacamp I don't understand fad3r 3 3,930 Feb-04-2018, 08:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020