In a decade dominated by AI and Machine Learning, what jobs you will be doing by 2030?
The work you will be doing over the next 10 to 20 years will be dependable on your learning skills, on your adapting skills. People that ignore this, will struggle to find a job, countries that ignore this will be left behind, by the countries with a young population adopting this tech.
This is a guide of the main concepts to code on Python.
1. Introduction
2. Variables and types
3. Operators and comparisons
4. Compound types: Strings, List and dictionaries
5. Conditional statements
6. Loops
7. Functions
8. Libraries
9. Jupyter Notebook and Google Colab
Introduction
Conceived in the late 1980s as a teaching and scripting language, Python has since become an essential tool for many programmers, engineers, researchers, and data scientists across academia and industry. I’ve found Python to be a near-perfect fit for the types of problems I face day to day. The appeal of Python is in its simplicity and beauty, as well as the convenience of the large ecosystem of domain-specific tools that have been built on top of it.
Variables and types
Variables
In Python, variables are used to store information that can be referenced and manipulated in a program. They are created by simply assigning a value to a name.
# variable assignments
x = 1.0
my_variable = 12.2
print(my_variable)
12.2
Assigning data to variables
You can assign various types of data to variables, such as numbers, strings, and lists.
number = 5
text = "Hello, World!"
print(number, text)
5 Hello, World!
Data Types
Python has a variety of basic data types like integers, floats (decimal numbers), and strings (text). You can use the type()
function to determine the type of a variable.
# integers
x = 1
type(x)
<class 'int'> <class 'float'> <class 'str'>
# float
x = 1.0
type(x)
True
# boolean
b1 = True
b2 = False
type(b1)
# complex numbers: note the use of `j` to specify the imaginary part
x = 1.0 - 1.0j
type(x)
Operators and comparisons
Most operators and comparisons in Python work as one would expect:
Arithmetic operators
+
,-
,*
,/
,//
(integer division), ’**’ power
1 + 2, 1 - 2, 1 * 2, 1 / 2
(3, -1, 2, 0.5)
1.0 + 2.0, 1.0 - 2.0, 1.0 * 2.0, 1.0 / 2.0
(3.0, -1.0, 2.0, 0.5)
# Integer division of float numbers
3.0 // 2.0
1.0
# Note! The power operators in python isn't ^, but **
2 ** 2
4
Comparison operators
>
,<
,>=
(greater or equal),<=
(less or equal),==
equality,is
identical.
2 > 1, 2 < 1
(True, False)
2 > 2, 2 < 2
(False, False)
# equality
[1,2] == [1,2]
True
Compound types: Strings, List and dictionaries
Strings
Strings are the variable type that is used for storing text messages.
s = "Hello world"
type(s)
str
# length of the string: the number of characters
len(s)
11
print("str1", "str2", "str3") # The print statement concatenates strings with a space
str1 str2 str3
print("str1" + "str2" + "str3") # strings added with + are concatenated without space
str1str2str3
print("value = %f" % 1.0) # we can use C-style string formatting
value = 1.000000
# this formatting creates a string
s2 = "value1 = %.2f. value2 = %d" % (3.1415, 1.5)
print(s2)
value1 = 3.14. value2 = 1
# alternative, more intuitive way of formatting a string
s3 = 'value1 = {0}, value2 = {1}'.format(3.1415, 1.5)
print(s3)
value1 = 3.1415, value2 = 1.5
List
Lists are very similar to strings, except that each element can be of any type.
The syntax for creating lists in Python is [...]
:
l = [1,2,3,4]
print(l)
[1, 2, 3, 4]
# create a new empty list
l = []
# add an elements using `append`
l.append("A")
l.append("d")
l.append("d")
print(l)
['A', 'd', 'd']
Dictionaries
Dictionaries are also like lists, except that each element is a key-value pair. The syntax for dictionaries is
{key1 : value1, ...}
:
params = {"parameter1" : 1.0,
"parameter2" : 2.0,
"parameter3" : 3.0,}
print(type(params))
print(params)
<class 'dict'>
{'parameter1': 1.0, 'parameter2': 2.0, 'parameter3': 3.0}
print("parameter1 = " + str(params["parameter1"]))
print("parameter2 = " + str(params["parameter2"]))
print("parameter3 = " + str(params["parameter3"]))
parameter1 = 1.0
parameter2 = 2.0
parameter3 = 3.0
Conditional statements
The Python syntax for conditional execution of code uses the keywords
if
,elif
(else if),else
:
statement1 = False
statement2 = False
if statement1:
print("statement1 is True")
elif statement2:
print("statement2 is True")
else:
print("statement1 and statement2 are False")
statement1 and statement2 are False
statement1 = statement2 = True
if statement1:
if statement2:
print("both statement1 and statement2 are True")
both statement1 and statement2 are True
Loops
In Python, loops can be programmed in a number of different ways. The most common is the
for
loop, which is used together with iterable objects, such as lists. The basic syntax is:
for x in [1,2,3]:
print(x)
1
2
3
for word in ["scientific", "computing", "with", "python"]:
print(word)
scientific
computing
with
python
List comprehensions: Creating lists using for
loops:
l1 = [x**2 for x in range(0,5)]
print(l1)
[0, 1, 4, 9, 16]
while loops:
i = 0
while i < 5:
print(i)
i = i + 1
print("done")
0
1
2
3
4
done
Functions
A function in Python is defined using the keyword
def
, followed by a function name, a signature within parentheses()
, and a colon:
. The following code, with one additional level of indentation, is the function body.
def func0():
print("test")
func0()
test
def square(x):
"""
Return the square of x.
"""
return x ** 2
square(4)
16
Libraries
One of the most important concepts in good programming is to reuse code and avoid repetitions.
The idea is to write functions and classes with a well-defined purpose and scope, and reuse these instead of repeating similar code in different part of a program (modular programming). The result is usually that readability and maintainability of a program is greatly improved. What this means in practice is that our programs have fewer bugs, are easier to extend and debug/troubleshoot.
Python supports modular programming at different levels. Functions and classes are examples of tools for low-level modular programming. Python modules are a higher-level modular programming construct, where we can collect related variables, functions and classes in a module. A python module is defined in a python file (with file-ending .py
), and it can be made accessible to other Python modules and programs using the import
statement.
Consider the following example: the file mymodule.py
contains simple example implementations of a variable, function and a class:
import random
print(random.randint(1, 100))
1
import math
print(math.sqrt(16)) # Output: 4.0
4.0
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr.mean()) # Output: 3.0
3.0
import pandas as pd
data = {'Name': ['John', 'Anna'], 'Age': [28, 22]}
df = pd.DataFrame(data)
print(df)
Name Age
0 John 28
1 Anna 22
Other Popular Python libraries for machine learning
Python’s ecosystem is replete with libraries tailored for data science tasks. Apart from numpy
, pandas
, and Matplotlib
, other notable libraries include seaborn
for advanced visualization, scikit-learn
for machine learning, and fastai
& pytorch
for deep learning. These tools have collectively positioned Python as a leading language in the data science domain.
Working on Notebooks
Jupyter Notebook
A notebook is a document made of cells. You can write in some of them (markdown cells) or you can perform calculations in Python (code cells) and run them like this:
The Jupyter Notebook is an interactive computing environment that enables users to author notebook documents that include: - Live code - Interactive widgets - Plots - Narrative text - Equations - Images - Video
Enables code and text combination for user convenient.
Notebook documents contain the only the input and output of the interactive session. When you run the notebook web application on your computer, notebook documents are just files on your local filesystem with a .ipynb
extension. This format will serve us when submitting the HWs. These files are JSON files, which are rendered in the web browser
Google Colab
Google Colab is a cloud-based Jupyter notebook environment from Google
Colab, or “Colaboratory”, allows you to write and execute Python in your browser, with - Zero configuration required - Access to GPUs free of charge - Easy sharing
Go to colab.research.google.com
Sign in with your Google credentials
Further reading
http://www.python.org/
https://www.programiz.com/python-programming
https://www.w3schools.com/python/