all

Python Dictionaries

Introduction Dictionaries are used to store values in key: value pairs,

  1. A simple dictionary, simple_dict = { "age": 29, "country": "Ke", "gender": "Female" } Print the dictionary: print(simple_dict) # outputs {'age': 29, 'country': 'Ke', 'gender': 'Female'} 2. Access the dictionary items Print “age” value: print(simple_dict["age"]) # outputs: 29 Duplicate keys are not allowed. simple_dict = { "age": 29, "age": 30, "country": "Ke", "gender": "Female" } print(simple_dict) # outputs: {'age': 30, 'country': 'Ke', 'gender': 'Female'} 3.
2 minutes to read

How to set up a virtual environment for a Python project

Introduction The venv module supports creating lightweight “virtual environments” with their own independent set of Python packages installed in their site directories.

  1. Creating a virtual environment cd project-path/ python -m venv <name-of-virtual-envrionment> Replace <name-of-virtual-environment> with the name of choice, a convention is usually venv, my-env, virtual-env
  2. Activate the virtual environment To activate the virtual environment, from the current project directory, in our case(project-path) cd project-path/ # create the virtual environment with the name "my-venv" python -m my-venv # activate the virtual environment source venv/bin/activate Running this command creates the target directory and places pyvenv.
One minute to read