OrgPad logo

Python mindmap (Ahmad Mateen)

Created by Ahmad Mateen

Python mindmap (Ahmad Mateen)

1. Basics

Welcome to the basic module of learning Python 🎉.

This module covers the most important basic concepts you need to know to get started with Python (next to Googling, of course 😉).

FGJ7f16XoAA 2 i

Python Packages

Python packages (or almost interchangeably, libraries) are collections of modules that are often developed by open-source community.

Working Directory

The working directory is the default file path in your computer (or on the cloud) that Python reads or saves files into. For example, 'C:\thuvu\Desktop\Data Science\'

# Import the operating system package

import os

# Print the current working directory

print os.getcwd()

# Change working directory to new path

os.chdir(new_path)

virtualenv

virtualenv is a third party alternative (and predecessor) to venv.

1) Create a new virtual environment

It is not installed for the Anaconda modules. If you would like to work within a virtual environment using virtualenv with one of the Anaconda modules, you will need to install it.

Install virtualenv with pip:

pip install --user virtualenv 

Create a virtual environment for a project:

$ cd project_folder 
$ virtualenv <env_name>

2) Activate a virtual environment:

$ source myenv/bin/activate 

The name of the current virtual environment will now appear in parenthesis to the left of the prompt to let you know that it’s active.

3) Install packages

You can also use pip (similar to venv).

4) Deactivate a virtual environment

When done working in the virtual environment, simply deactivate it:

(myvenv) $ deactivate

venv

venv is the standard Python tool for creating virtual environments, and has been part of Python since version 3.3.

1) Create a new virtual environment

$ python -m venv /path/to/new/virtual/environment 

Alternatively, you can first go to the directory of the project you are working on and simply provide a name for the virtual environment in place of the full path:

$ cd /path/to/my/project 
$ python -m venv myenv

 

These commands will create a folder in the current directory which will contain the Python executable files and a copy of the pip library which you can use to install other packages. You will see these files in your project folder (mine is speech_recognition folder):

Screenshot 2024-05-07 at 3.20.32 PM

2) Activate a virtual environment:

$ source myenv/bin/activate

However, if you are not in your project directory, then you must provide the full path to the virtual environment:

$ source /path/to/my/project/myenv/bin/activate

 

3) Install packages

At this point, you would use the pip command to install any needed packages. For example:

$ pip install pandas

4) Deactivate a virtual environment

When you are finished working in the environment, deactivate a virtual environment by typing:

$ deactivate

conda

conda is both a package installer, like pip, and an environment manager, like venv and virtualenv. While pipvenv, and virtualenv are for Python, conda is language agnostic and works with other languages as well as Python.

1) Create a new virtual environment

$ conda create --name conda-env python 

where conda-env can be replaced with whatever name you choose for your virtual environment. Also, -n can be used in place of --name. This environment will use the same version of Python as your current shell’s Python interpreter. To specify a different version of Python, specify the version number when creating your virtual environment as follows:

$ conda create -n conda-env python=3.7

2) Install packages

You can install additional packages when creating an environment, by specifying them after the environment name. You can also specify which versions of packages you’d like to install.

$ conda create -n conda-env python=3.7 numpy=1.16.1 requests=2.19.1 

It’s recommended to install all packages that you want to include in an environment at the same time in order to avoid dependency conflicts.

3) Activate conda environment

You can then activate your conda environment. The name of the current virtual environment will now appear in parenthesis to the left of the prompt to let you know that it’s active:

$ conda activate conda-env 
(conda-env) $

4) Deactivate conda environment

$ conda deactivate

Java Galaxy

0011010101010110010010101010101011111010101010010101101a5ec0442-f313-4e08-a5dd-f0ceff118696

Create virtual environment & Install packages

Why use virtual environment?

While it is tempting to install all packages directly into your global Python environment, it is NOT advised to do so.

Sometimes one application needs a particular version of a package but a different application needs another version. Since the requirements conflict, installing either version will leave one application unable to run.

Here comes the necessity for virtual environments:

A virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application or for a particular project.

We will explore a few alternatives as to how to create virtual environments: env, virtualenv and conda.

NOTE: I personally only use env on my Macbook, as it is a simple and light-weight option that is standard in Python rather than created by a third-party.

Interactive charts

Interactive charts are, well, interactive! 😉

This is an example of the same chart made with Plotly:

CleanShot 2024-07-03 at 10.07.06

Dashboarding

There are many ways to create a custom visualisation dashboard with Python:

For the local option, you can choose from a wide range of cool Python libraries (sorry for the small image, you can zoom in to see):

5f99e10dafbd69a99c875340 C8 qX8dvzv60T4LVZ9GftX-ZH-VJzq3sjUroWWH5XSWw8RFHnCCPPrC6jB3EFVuQdwiqhoEMQKFV-dFz7t6fqaRpSZGvBKI0i1Utj38 j9a54GXMuzi1BiepdIMjOK4ATVdF2131-3033846131

FYI: I have a few tutorial videos featuring Panel you can check out:

Why choose Python?

print("Hello World)

public

But should I still learn Python if ChatGPT can also code?

Well, that's a very legitimate concern.

LLMs are only as good as the data they're trained on. Since Python is a very popular language, there is a wealth of publicly available code examples (e.g. on Github). That means LLMs like GPT4 got pretty good at writing Python code.

top-programming-languages-2023

https://github.blog/2023-11-08-the-state-of-open-source-and-ai/

However, there are some challenges:

Therefore, being proficient in Python can help you leverage the opportunities created by AI, while being able to avoid the pitfalls.

Static charts

Static charts are simple to make with Python libraries such as Matplotlib.

This is an example static chart made with Matplotlib:

CleanShot 2024-06-25 at 23.26.24

Import a package or module

You can import a Python library using the import statement. For example:

# Import a package without an alias

import pandas

# Import a package with an alias

import pandas as pd

# Import a specific module from a package (for example the time package):

from time import clock

NOTE: If the package is not part of the standard library, you’ll need to install it first, before you can import it. You can install a library with a package manager such as pip or conda:

e.g. pip install pandas

integer

As you already know, they are numbers like:

1
2
3
4

float

These are numbers with decimals like:

5.5
3.1495820

Managing packages

There are several ways that Python packages can be installed and managed.

python environment

Cartoon by Randall Munroe, https://xkcd.com/1987/

Common tools used for Python package installation and environment management include:

NOTE: You should pick one method and stick with it to avoid confusions later.

complex

Data visualization

Python has several powerful data visualisation libraries that support creating both static and interactive graphs.

Here's an overview of 5 popular data viz libraries:

 Library Interactive featuresMain characteristics and use case
 MatplotlibLimitedLow level, highly customized plots
 seabornLimitedFast, presentatable reports
 BokehYesFlexible, interactive viz of big datasets
 AltairYesData exploration & interactive reports
 PlotlyYesCommercial applications and dashboards

I personally enjoy using Plotly due to its high-level syntax (I don't need to write too many lines of code to create a bar chart), fast development and the ability to create interactive charts.

A Brief History of Python 🐍

Python is a widely used general-purpose, high-level programming language.

Numeric types 🔢

represents the data that has a numeric value.

string

A string is a collection of one or more characters put in a single quote, double-quote, or triple-quote.

hello_msg = 'Hello from Thu'

or,

hello_msg = "Hello from Thu"

Screenshot 2024-05-07 at 11.03.00 AM

list

A list is an ordered, changeable sequence of elements.

When to use:

When you want to store multiple items in a single variable.

Create a list:

Lists are wrapped around by square brackets.

>>> x = [1, 2, 3, 4]
>>> y = ['apple', 'banana', 'pear']

List operations:

https://docs.python.org/3/tutorial/datastructures.html

Data analyses

pandas is arguably the most important Python package for data analysis (for tabular data).

pip install pandas

Machine learning

Integrated Development Environments (IDEs) for Python

What is an IDE?

An integrated development environment (IDE) is a program dedicated to software development. As the name implies, IDEs integrate several tools specifically designed for software development. These tools usually include:

Most common IDEs for Python:

  1. Visual Studio Code: https://code.visualstudio.com/
  2. PyCharm: https://www.jetbrains.com/pycharm/
  3. Spyder: https://www.spyder-ide.org/
  4. Jupyter/ JupyterLab (probably not considered an IDE, but widely used for data science projects): https://jupyter.org/

Working with files

Webscraping

Useful packages

There are lots of useful packages in Python. For Data Science, some of the most commonly-used packages are:

Screenshot 2024-05-07 at 11.49.43 AM

tuple

Attributes

Git Version Control

Data Types

1) How types work in Python:

Since Python is an interpreted language, the data type is automatically set when you assign a value to a variable:

x = 1
# x is of type int
y = 2.8
# y is of type float
z = "hello"
# z is of type str

However,  if you want to specify the data type of a variable, this can be done with casting.

x = str(3)    # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0

2) Check data type of a variable:

To verify the type of any object in Python, use the type() function:

print(type(x))

3) Type conversion:

We can change a variable's type after it has been set. This can be done easily, for example:

FunctionWhat it doesExampleResult
str()Convert variable to string

str(3.14)

'3.14'

int()Convert variable to integerint(3.14)3
float()Convert variable to floatfloat(3)3.0
bool()Convert variable to booleanbool(3)True

Text type 💬

Represents the data that have text value.

Classes

Python for Data Science

Sequence types ♻︎

This is the ordered collection of similar or different Python data types. Sequences allow storing of multiple values in an organized and efficient fashion.

range

PYTHON Galaxy

python

Hello there! Thanks for visiting this mindmap.

My name is Ahmad Mateen. A while ago, I created this mindmap.

dictionary

A dictionary in Python is an unordered collection of data values, used to store data values like a map. It is best to think of a dictionary as a set of key: value pairs

When to use:

When you want to have a collection of items that have key-value pair, and you want to quickly look up/ manipulate the values based on keys or vice versa.

Create a dictionary:

>>> example_dict = {'name': 'Thu', 'age': 25}

Dictionary operations:

https://docs.python.org/3/tutorial/datastructures.html#dictionaries

Methods

Objects

Object-Oriented Programming

2. Intermediate

None type

Mapping type ↔️

This data type associate a value to a unique key. This data type is useful and efficient for quickly look up and access an item in a collection.

Set types

set

A set is an unordered collection with no duplicate elements.

When to use:

When you want to create a unique list of elements, and you may want to use mathematical operations on it like union, intersection, difference, and symmetric difference.

Create a set:

Screenshot 2024-05-07 at 4.28.59 PM

Set operations:

https://realpython.com/python-sets/

Why use decorators?

Some examples of when decorates are useful:

Logging

Caching

Python Statements

Python statement is an instruction that the Python interpreter can execute. So simply speaking, statements tell Python what to do 😉. 

Boolean type

Binary types

frozenset

Decorators

Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behaviour of a function or class.

Variables

Variables are containers for storing data values.

A variable is created the moment you first assign a value to it, for example:

x = 5
y = "John"

boolean

Booleans represent one of two values: True or False.

*args and **kwargs

Generators

Operators

Operators are special symbols that perform operations on variables and values. For example:

Screenshot 2024-05-07 at 11.37.44 AM

memoryview

bytes

Debugging

Error Handling

Type-hinting

Type hinting let others know the expected data types for variables, function arguments, and return values.

Here’s how you can add type hints to a function:

For example, a function that sums two integers and returns an integers:

def add_numbers(num1: int, num2: int) -> int:
return (num1 + num2)

There are a lot more features that you can find in the official doc:
https://docs.python.org/3/library/typing.html

Expression statements

if-else, elif

Syntax:

if: executes a block of code only if a given condition is true.Screenshot 2024-05-07 at 11.24.05 AM

if-else:  extends the if statement by providing an alternative block of code to execute when the condition is false.

Screenshot 2024-05-07 at 11.26.25 AM

if-elif-else: checks multiple conditions and execute different blocks of code accordingly. The elif keyword is short for "else if" and can be used multiple times.

Screenshot 2024-05-07 at 11.27.45 AM

Example:

Screenshot 2024-05-07 at 11.32.08 AM

Conditional Statements

Conditional statements are used to make decisions based on certain conditions.

del

del can be used to delete entire variables:

del a

It can also remove an item from a list given its index instead of its value:

Screenshot 2024-05-07 at 4.25.32 PM

Python built-in functions

bytearray

3. Advanced

Loop Statements

assert

The assert statement is used to test conditions and trigger an error if the condition is not met.

It is often used for debugging and testing purposes.

Syntax:

Screenshot 2024-05-07 at 4.12.37 PM

Example:

Screenshot 2024-05-07 at 4.12.19 PM

try-except

Try and Except statement is used to handle these errors within our code in Python. 

Syntax:

try:
Some code
except:
Executed if error in the try block

Arithmetic

Arithmetic operators are used with numeric values to perform common mathematical operations:

Operator  NameExampleResult
+Addition 3 + 2 5
-Subtraction 3 - 2 1
*Multiplication 3 * 2 6
/Division 3 / 2 1.5
%Modulus 3 % 2 1 
**Exponentiation 3 ** 2 9
//Floor division 3 // 2 1

Functions

Functions take in inputs, use it to run a set of code and returns an output.

Screenshot 2024-05-07 at 4.36.09 PM

Bitwise

Bitwise operators are used to compare (binary) numbers. These operators operates on the bitwise representation of integers, hence they can be a bit less intuitive than other operators. Read more here.

OperatorName

Description

Example

ANDSets each bit to 1 if both bits are 1x & y
|ORSets each bit to 1 if one of two bits is 1x | y
^XORSets each bit to 1 if only one of two bits is 1x ^ y
~NOTInverts all the bits~x
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall offx << 2
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offx >> 2

Developing Applications

for loop

for loop is used to iterate over a sequence, such as a string, list, or tuple.

Syntax:

Screenshot 2024-05-07 at 11.52.02 AM

Example:

Screenshot 2024-05-07 at 12.28.27 PM

Output:

Screenshot 2024-05-07 at 12.28.50 PM

Create a custom function

Create a Python function:

Returns a value:

Screenshot 2024-05-07 at 4.48.28 PM

Does not return anything:

Screenshot 2024-05-07 at 4.49.09 PM

while loop

A while loop repeatedly executes a block of code as long as a given condition is true.

Syntax:

Screenshot 2024-05-07 at 11.29.59 AM

Example: Printing numbers from 1 to 5

Screenshot 2024-05-07 at 11.39.57 AM

Output:

Screenshot 2024-05-07 at 11.40.19 AM

Loop Control Statements

Assignment

Assignment operators are used to assign values to variables:

Operator

Example

Same As

 =

x = 5

x = 5

 +=x += 3x = x + 3
 -=x -= 3x = x - 3
 *=x *= 3x = x * 3
 /=x /= 3x = x / 3
 %=x %= 3x = x % 3
 //=x //= 3x = x // 3
 **=x **= 3 x = x ** 3
 &=x &= 3 x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
:=print(x := 3)x = 3

print(x)

Membership

Membership operators are used to test if a sequence is presented in an object:

OperatorDescription

Example

inReturns True if a sequence with the specified value is present in the object
print("a" in "apple")
→ Return True
not inReturns True if a sequence with the specified value is not present in the object

print("b" not in "apple")

→ Return True

pass

pass: Acts as a placeholder and does nothing. It is commonly used when a statement is required syntactically but no action is needed.

Example:

Screenshot 2024-05-07 at 4.14.25 PM

Output:

Screenshot 2024-05-07 at 4.14.57 PM

Comparison

Comparison operators are used to compare two values:

OperatorName

Example

==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Logical

Logical operators are used to combine conditional statements:

OperatorDescriptionExample
andReturns True if both statements are truex < 5 and x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverse the result, returns False if the result is truenot(x < 5 and x < 10)

Identity

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

OperatorDescriptionExample
is Returns True if both variables are the same objectx is y
is notReturns True if both variables are not the same objectsame object x is not y

Working with APIs

break

break: Terminates the loop prematurely and transfers control to the next statement after the loop.

Example:

Screenshot 2024-05-07 at 4.14.25 PM

Output:

Screenshot 2024-05-07 at 4.14.57 PM

continue

continue: Skips the current iteration of the loop and moves to the next iteration.

Example:

Screenshot 2024-05-07 at 4.14.25 PM

Output:

Screenshot 2024-05-07 at 4.14.57 PM