DEV Community

Cover image for Python for Beginners - Part 6 - Web API connection, Dictionary, Environment Variable and Decorator
LUCIANO DE SOUSA PEREIRA
LUCIANO DE SOUSA PEREIRA

Posted on • Originally published at lucianopereira.netlify.com

1 1

Python for Beginners - Part 6 - Web API connection, Dictionary, Environment Variable and Decorator

python_titulo
Last part of articles about the Microsoft's Python for Beginners series.
All the examples from the playlist are available in this GitHub repository: Python Examples.

Objective

You will learn how to make connect with web APIs, manipulate dictionaries, read environment variables and use decorators.
A Python file was created to demonstrate each example.

Topics

api/post.py

Making a POST request to a web API.
The example file was created inside of a folder named api.

code32

First, install the requests library:

pip install requests

The result will be:

code33

Then, run this command:

python .\examples\api\post.py

The result will be:

code34

dictionary.py

Creating and manipulating dictionaries.

code35

Executing the command below:

python .\examples\dictionary.py

The result is:

code36

keys.py

Reading environment variables.

code37

Create a .env file containing a secret key.

code38

Update the requirements.txt file by adding python-dotenv.

code39

Let's use a virtual environment again by executing the commands below:

python -m venv venv

.\venv\Scripts\Activate.ps1

Install the python_dotenv package inside the venv:

pip install -r requirements.txt

The result will be:

code40

Finally, run this command:

python .\examples\keys.py

The result will be:

code41

decorators.py

How to use decorators in functions.

code42

Run the command:

python .\examples\decorators.py

The result will be:

code43

Conclusion

We have finally ended this series!
Now you have the basics to start working with Python.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay