RisingCoder Full course โ†’
Day 1Day 2Day 3Day 4
๐ŸŒ
STUDENT NOTES ยท DAY 15 OF 24

Talking to the Internet ๐ŸŒ

Weather, cricket scores, memes โ€” the internet is full of data. An API is the polite way for YOUR code to ask for it.

1What is an API?

๐Ÿ
Py: An API is a restaurant menu for programs: the service says "here's what you can ask for and how" โ€” your code orders, the server delivers.
๐Ÿค–
Bittu: I ask the weather API for Pune. It answers in 0.2 seconds. No umbrella needed for negotiations.

2requests.get โ€” placing the order

๐Ÿฆ‰
Professor Hoot: Status codes: 200 = OK โœ…, 404 = not found ๐Ÿคท, 500 = server having a bad day ๐Ÿ”ฅ. Learn the big three.

3JSON โ€” the language servers speak

Servers reply in JSON โ€” which looks almost exactly like Python dicts and lists! .json() converts it:

Pretend Weather API โ€” pick a city!

โ€ฆpress a city to send the requestโ€ฆ

4Keep your key secret! ๐Ÿ”‘

๐Ÿž
Bugly: Post your API key on GitHub and strangers will use YOUR quota within minutes. Hehehe. I love careless coders.
๐Ÿ
Py: Treat keys like ATM PINs: never share, never paste in public code. We'll use environment variables in class.

5Quick quiz! ๐Ÿง 

The server replies with status 404. Meaning?
๐Ÿ’ก 200 OK, 404 not found, 500 server error โ€” the holy trinity of status codes.
r = requests.get(url). How do you turn the JSON reply into Python data?
๐Ÿ’ก .json() parses the reply into dicts and lists you can use immediately.

6Your mission ๐Ÿš€

Mission 1: In class-shared code, call the weather API for YOUR city and print the temperature.
Mission 2: Print the status code. Then break the URL on purpose and see what code you get.
Mission 3: Explore the JSON reply: print data.keys() to see what else the server sent you.

Day 15 done โ€” the internet answers you now! ๐ŸŒ

Next: put it all together โ€” build a real Weather App.

Go to Day 16: Weather App โ†’

๐ŸŽฎ Or test yourself in the Python Arcade โ†’