This Requests Module lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
requests.get fetches a URL and returns a response. response.status_code is the HTTP status, and response.text is the body.
The package is not in the standard library, and this runner cannot open network connections.
Example 1: Start here
Install requests in a virtual environment. Set a timeout so a stuck server does not hang the program.
print("import requests") # print writes this; the quotes are not shown
print("response = requests.get(url)")
print("print(response.status_code)")
Example 2: Store the idea in a name
Requests Module begins with a value you can point at. The name topic holds that text.
topic = "Requests Module" # text
print(topic)
Example 3: Check it with if
An if chooses one branch. Change the score and run it again to see the other message.
score = 11 # a whole number
if score >= 80: # run the indented lines only when this is true
print("Pass")
else: # runs when the test above was false
print("Try again")
Example 4: Repeat with a loop
A for loop runs the indented line once per step. The numbers 1, 2, and 3 are printed on their own lines.
for step in range(1, 4): # repeat once per number; range stops before the last
print("Step", step) # a comma prints each value, with a space between
