Created by Hien Thu Vu
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 😉).
Python packages (or almost interchangeably, libraries) are collections of modules that are often developed by open-source community.
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 is a third party alternative (and predecessor) to venv.
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>
$ 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.
You can also use pip (similar to venv).
When done working in the virtual environment, simply deactivate it:
(myvenv) $ deactivate
venv is the standard Python tool for creating virtual environments, and has been part of Python since version 3.3.
$ 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):
$ 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/activateAt this point, you would use the pip command to install any needed packages. For example:
$ pip install pandas
When you are finished working in the environment, deactivate a virtual environment by typing:
$ deactivate
conda is both a package installer, like pip, and an environment manager, like venv and virtualenv. While pip, venv, and virtualenv are for Python, conda is language agnostic and works with other languages as well as Python.
$ 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
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.
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) $
$ conda deactivate
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 are, well, interactive! 😉
This is an example of the same chart made with Plotly:
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):
FYI: I have a few tutorial videos featuring Panel you can check out:
print("Hello World)
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.
https://github.blog/2023-11-08-the-state-of-open-source-and-ai/
Therefore, being proficient in Python can help you leverage the opportunities created by AI, while being able to avoid the pitfalls.
Static charts are simple to make with Python libraries such as Matplotlib.
This is an example static chart made with Matplotlib:
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
As you already know, they are numbers like:
1
2
3
4
These are numbers with decimals like:
5.5
3.1495820
There are several ways that Python packages can be installed and managed.
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.
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 features | Main characteristics and use case |
| Matplotlib | Limited | Low level, highly customized plots |
| seaborn | Limited | Fast, presentatable reports |
| Bokeh | Yes | Flexible, interactive viz of big datasets |
| Altair | Yes | Data exploration & interactive reports |
| Plotly | Yes | Commercial 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.
Python is a widely used general-purpose, high-level programming language.
represents the data that has a numeric value.
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"
def my_function():
'''Demonstrates triple double quotes
docstrings and does nothing really.'''
return None
There are lots of useful packages in Python. For Data Science, some of the most commonly-used packages are:
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:
A list is an ordered, changeable sequence of elements.
When you want to store multiple items in a single variable.
Lists are wrapped around by square brackets.
>>> x = [1, 2, 3, 4]
>>> y = ['apple', 'banana', 'pear']
https://docs.python.org/3/tutorial/datastructures.html
pandas is arguably the most important Python package for data analysis (for tabular data).
pandas with pip in your terminal:pip install pandas
Represents the data that have text value.
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
To verify the type of any object in Python, use the type() function:
print(type(x))
We can change a variable's type after it has been set. This can be done easily, for example:
| Function | What it does | Example | Result |
str() | Convert variable to string |
| '3.14' |
int() | Convert variable to integer | int(3.14) | 3 |
float() | Convert variable to float | float(3) | 3.0 |
bool() | Convert variable to boolean | bool(3) | True |
This is the ordered collection of similar or different Python data types. Sequences allow storing of multiple values in an organized and efficient fashion.
Hello there! Thanks for visiting this mindmap.
My name is Thu Vu. A while ago, I created this mindmap as part of my Youtube video on learning Python.
If you have suggestions on how to improve/ complete this mindmap, please feel free to contact me via email:
hello@thuhienvu.com
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 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.
>>> example_dict = {'name': 'Thu', 'age': 25}
https://docs.python.org/3/tutorial/datastructures.html#dictionaries
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.
A set is an unordered collection with no duplicate elements.
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.
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}https://realpython.com/python-sets/
Some examples of when decorates are useful:
A Python statement is an instruction that the Python interpreter can execute. So simply speaking, statements tell Python what to do 😉.
Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behaviour of a function or class.
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"
Booleans represent one of two values: True or False.
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:
->) and a data type after the function to specify the return data typeFor 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
if: executes a block of code only if a given condition is true.
if condition:
# Code to execute if the condition is true
if-else: extends the if statement by providing an alternative block of code to execute when the condition is false.
if condition:
# Code to execute if the condition is true
else:
# Code to execute if the condition is false
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.
if condition1:
# Code to execute if condition1 is true
elif condition2:
# Code to execute if condition2 is true
else:
# Code to execute if all conditions are false
percentage = 75
if percentage >= 90:
print("Grade: A")
elif percentage >= 80:
print("Grade: B")
elif percentage >= 70:
print("Grade: C")
elif percentage >= 60:
print("Grade: D")
else:
print("Grade: F")
Conditional statements are used to make decisions based on certain conditions.
Operators are special symbols that perform operations on variables and values. For example:
print(5 + 6) # 11
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:
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
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.
assert condition, message
x = 5
assert x == 5, "x should be 5"
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 operators are used with numeric values to perform common mathematical operations:
| Operator | Name | Example | Result |
| + | 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 |
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.
| Operator | Name | Description | Example |
| & | AND | Sets each bit to 1 if both bits are 1 | x & y |
| | | OR | Sets each bit to 1 if one of two bits is 1 | x | y |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y |
| ~ | NOT | Inverts all the bits | ~x |
| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | x << 2 |
| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | x >> 2 |
Returns a value:
def my_function(x):
return 5 * x
Does not return anything:
def my_function():
print("Hello from a function")
A for loop is used to iterate over a sequence, such as a string, list, or tuple.
for element in sequence:
# Code to execute for each element
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
A while loop repeatedly executes a block of code as long as a given condition is true.
while condition:
# Code to execute while the condition is true
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Assignment operators are used to assign values to variables:
| Operator | Example | Same As |
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 3 | x = x * 3 |
| /= | x /= 3 | x = x / 3 |
| %= | x %= 3 | x = x % 3 |
| //= | x //= 3 | x = x // 3 |
| **= | x **= 3 | x = x ** 3 |
| &= | x &= 3 | x = x & 3 |
| |= | x |= 3 | x = x | 3 |
| ^= | x ^= 3 | x = x ^ 3 |
| >>= | x >>= 3 | x = x >> 3 |
| <<= | x <<= 3 | x = x << 3 |
| := | print(x := 3) | x = 3
print(x) |
Functions take in inputs, use it to run a set of code and returns an output.
Membership operators are used to test if a sequence is presented in an object:
| Operator | Description | Example |
| in | Returns True if a sequence with the specified value is present in the object | print("a" in "apple")→ Return True |
| not in | Returns True if a sequence with the specified value is not present in the object |
→ Return |
pass: Acts as a placeholder and does nothing. It is commonly used when a statement is required syntactically but no action is needed.
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
if fruit == 'banana':
continue
elif fruit == 'cherry':
break
else:
pass
print(fruit)
Output:
apple
Comparison operators are used to compare two values:
| Operator | Name | Example |
| == | Equal | x == y |
| != | Not equal | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
Logical operators are used to combine conditional statements:
| Operator | Description | Example |
| and | Returns True if both statements are true | x < 5 and x < 10 |
| or | Returns True if one of the statements is true | x < 5 or x < 4 |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
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:
| Operator | Description | Example |
| is | Returns True if both variables are the same object | x is y |
| is not | Returns True if both variables are not the same object | same object x is not y |
break: Terminates the loop prematurely and transfers control to the next statement after the loop.
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
if fruit == 'banana':
continue
elif fruit == 'cherry':
break
else:
pass
print(fruit)
Output:
apple
continue: Skips the current iteration of the loop and moves to the next iteration.
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
if fruit == 'banana':
continue
elif fruit == 'cherry':
break
else:
pass
print(fruit)
Output:
apple