Most real-world Python programs need to do three things:
- Save or read data from a file.
- Get information from the internet using an API.
- Store and exchange data in JSON format.
Let’s explore each step with examples.
📝 If You Want to Practice the Code
If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):
- From a Python prompt in your terminal or command line – see Getting Started with Python and Virtual Environment in Python and How to Set One Up
- Using Miniconda and Jupyter Notebook – see How to Install and Set Up Miniconda and Install Jupytor Notebook Windows, How to Install and Set Up Miniconda on MacOS, How to Install and Set Up Miniconda on Linux (Ubuntu), and Getting Started with Jupyter Notebooks.
- On Google Colab (browser-based, no installation required) – see Getting Started with Google Colab.
- Through VS Code – see Setting Up VS Code for Python Development.
👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.
🔹 Working with Files
Python makes reading and writing files very simple.
Writing to a File
# open file in write mode ("w")
with open("example.txt", "w") as f:
f.write("Hello, this is my first file!\n")
f.write("Python makes it easy.")
This will create (or overwrite) example.txt.
Reading from a File
with open("example.txt", "r") as f:
content = f.read()
print(content)
👉 Output:
Hello, this is my first file!
Python makes it easy.
🔹 Working with APIs
APIs let your Python program talk to web services. For example, you can fetch weather data, currency rates, or even jokes.
We’ll use the requests library (install it first with pip install requests).
import requests
response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
if response.status_code == 200:
print("Data fetched successfully!")
print(response.text) # raw response
else:
print("Error:", response.status_code)
🔹 Working with JSON
Most APIs return data in JSON (JavaScript Object Notation), which looks like a dictionary. Python has a built-in json module.
import requests
import json
response = requests.get("https://api.coindesk.com/v1/bpi/currentprice.json")
# Convert JSON text into Python dictionary
data = response.json()
print(data["bpi"]["USD"]["rate"]) # print Bitcoin price in USD
👉 Example Output: 42,350.1234
You can also save JSON to a file:
with open("bitcoin.json", "w") as f:
json.dump(data, f, indent=4)
And load it later:
with open("bitcoin.json", "r") as f:
loaded_data = json.load(f)
print(loaded_data["chartName"]) # Bitcoin
📝 Mini Task
- Write a program that saves three favorite movies into a file.
- Use an API (like
https://api.agify.io?name=Alice) to fetch data about a name. - Save the response as JSON, then read it back and print one field.
Example:
import requests, json
name = "Alice"
response = requests.get(f"https://api.agify.io?name={name}")
data = response.json()
with open("age.json", "w") as f:
json.dump(data, f, indent=4)
print("Predicted age for", name, "is", data["age"])
✅ Summary
- Use
open()with"r"(read) or"w"(write) to work with files. - Use the
requestslibrary to fetch data from APIs. - Use the
jsonmodule to work with JSON data.
With these three skills, you can save data locally, connect your program to the internet, and exchange structured information easily.