Python

Write an awesome doc for Python. A very nice an practical one extracted from Python official documentation.

View on GitHub

Whetting Your Appetite

We can do all of these things with Python.

[!TIP]

Wanna learn coding in Python. You can go and read their standard modules. It can give you a very good sense of direction and how you should think when you develop an app.

[!TIP]

Whenever you got stuck somewhere go and chat with Generative AI tools. E.g. ChatGPT.

Python REPL

Use it:

Learn by doing

The road ahead us

  1. Simple expressions.
  2. Statements and data types.
  3. Functions and modules.
  4. Exceptions and user-defined classes.

Ref

YouTube/Aparat

Invoking the Interpreter

Continuation lines and primary prompt

YouTube/Aparat

Ref

How computer memory works

Bit

CPU – Central Process Unit

Short-term memories

Long term memories

Memory latency

Digital storage terminology

Byte is 8 bits put together

Kilobyte is 1000 bytes put together

[!NOTE]

Digital storage uses the binary system, thus everything needs to be in powers of two.

Megabyte

Gigabyte

Ref

YouTube/Aparat

Informal introduction

Python as a Calculator

Numbers in Python

Operators

Name Plus/Addition Minus/Subtract Multiply Division Floor division Remainder/Dividend Power N/A
Operator + - * / // % ** Mixed
Example 7+3 7-3 7*3 7/3 7//3 7%3 7**3 7*3.2+1
Results 10 4 21 2.3333333333333335 2 1 343 23.400000000000002
Operator precedence

Ordered operators

Example:

An example of operator precedence order


Variables


Strings in Python

String Operators

Immutable Indices

word[0] = 'K' # or word[2:] = 'char'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Lists in Python

squares = [1, 4, 9, 16, 25]
squares[0] # 1
squares[-1] # 25
squares[-2:] # [16, 25]
squares.append(113) # [1, 4, 9, 16, 25, 111]
squares = squares + [36, 49] # [1, 4, 9, 16, 25, 111, 36, 49]
squares[-3] = -888 # [1, 4, 9, 16, 25, -888, 36, 49]

Shallow copy

books = ["Freakonomics", "The Prince", "The Pragmatic Programmer"]

my_favorite_books = books

my_favorite_books.append("The Zero Marginal Cost Society")

print(books)

Any changes to one will be seen through all other variables that refer to the same list.

books = [
  "Hands-On machine learning with Keras and Tensorflow",
  "Grokking Deep Learning"
]

# Here we are copying them
want_to_read = books[:]

want_to_read.append("Artificial Intelligence: A Guide for Thinking Humans")

print(books)

[!TIP]

To learn how to style your code which is widely adapted you can check PEP 8 – Style Guide for Python Code.

Multi Dimensional lists

Our first real program in Python

I guess you know Fibonacci series.

Fibonacci series

Here is how you can code it in Python

a, b = 0, 1
while a < 10:
    print(a)
    a, b = b, a+b
Explanation of Python syntax used in this program
  • Multiple assignment.
  • while loop.
    NOTE: In Python any non-zero integer value is true. zero is false.
    Comparison operators are:
    • < (less than).
    • > (greater than).
    • == (equal to)
    • <= (less than or equal to).
    • >= (greater than or equal to).
    • != (not equal to).
  • print()

Ref

3. An Informal Introduction to Python.

YouTube/Aparat