Skip to content

Python Guide @ Coding:

EVERYTHING you will learn can be done in a web IDE. We recommend using online-python.com! Click Here to Visit

Every time we say "Now You Try". Please use online-python.com to demonstrate your skills.

1. Print:

Lets get started with print().

  • Print simple outputs the thing you typed it to output. For example:
print("Hello World")

The terminal should return: Hello World.

  • Make sure you always have the brackets.

2. Random and IF Statements:

Let's dip our toes in if and using the random library.

  • To use a library in python you must import it first. Example: import random
  • To get function, variable or etc from a library do: from random import randint (On the line below)

Now lets use the random library to generate a random number between 1 and 20.

import random

number =  random.randint(1,20)
print(number) # When in the brackets. You do not need to put speech marks in for variables!

To add a if statement to the code. We must decide what should happen in-case of...

  • So lets say if the number is 10 it prints "10 + 10 = 20"
import random

number = random.randint(1,20)

if number == 20:
    print("10+ 10 = 20")

3. Multiple IF Statements:

When having multiple if statements. You can replace if with elif:

import random

number = random.randomint(1,20)

if number == 1:
    print("1")
elif number == 2:
    print("2")

4. While Loops:

The while loop checks if a condition is True and executes the code below (and within it) until the condition is no longer met.

num = 0

while num < 7:
    print(num)
    num += 1

A break statement allows you to end the loop even if the condition is still met / True.

num = 0

while num < 7:
    print(num)
    num += 1
    if num == 5:
        break
        print(f"Break you reached the number")

Note

The continue statement allows you to stop the current iteration (every time the loop is carried out) and starts the next one.

Note

The else statement shown in the Basics Section can also be used in a while loop.

5. For Loops:

The for loops are used to iterate (go over) over a list, tuple, dictionary, set or a string.

list = [1,2,3,4,5,6,7]

for x in list:
    print(x)
    # Below is IF statement inside the for loop to check if the variable x is 7 and if so it prints "end".
    if x == 7:
        print("end")

Tip

We recommend you learn about Data Structures before trying out with loops. Click Here

Note

You can continue to use if, break and continue statements in for loops in the "same" way.

Secret(s) Guide:

Store project secrets safely with a .env and the dotenv library. Follow the guide below:

Example Python Code to get your Token from an .env File:

import os
from dotenv import load_dotenv

load_dotenv()

token = os.environ["Token"]

Warning

Never put your .env in your Git Repo.

Using .gitignore you can prevent your .env from appearing in your git repo.

  1. Create an .gitignore (Critical Step otherwise your secrets will be revealed!)
  2. Add .env to the file.
  3. Commit it.
  4. Add your tokens to your .env after the commit.
.env

Guide on Python Packages:

Packages are the files for adding an external modules. In Python 3.4 PIP is installed with Python by default.

How to Check?

  • Wondering if you have PIP installed? Open your terminal /console and do:
pip --version

Install Guide:

Go to https://pypi.org/project/pip/ or Click Here.

Installing a Package:

  1. To install a package do pip install then the package name.
pip install {package name}

Tip

Most packages tend to have documentation on how to install their package. We recommend you find it and follow their documentation.

Guide on Virtual Environments:

Getting an error message or a warning message about Virtual Environments? Well then you need to setup Virtual Environments. Simply enter the commands below.

$ cd your-working-directory
$ python3 -m venv bot-env
$ myenv\Scripts\activate.bat # Activate your environment (For Windows Users).
$ source myenv/bin/activate # Activate your environment.
- To activate your environment do: source myenv/bin/activate - If your are on Windows though do: $ myenv\Scripts\activate.bat

Warning

You will need to re-install your Python Packages after activating your Virtual Environment(s).

  • If you have a requirements.txt simply do: pip install -r requirements.txt.

Info

When forking this or contributing to this monorepo you should beware that IndiSpark uses uv to handle packages, virtual environments and more. Click Here to Learn More