The helicopter view of Python (for intermediate) part 1

AndReda Mind
3 min readApr 18, 2022

We all know How python is important for AI, ML, DL, and for the backend with a lot of frameworks like Django, Flask, and recently FastAPI.

So, I decided to make the helicopter view about just the important topics in Python, that is why is it’s would be preferred to have some knowledge of any programming language before you starting reading this, to have the maximum benefits from this series of articles, the other reason is that I wrote these articles to discuss critical details which might be suitable for the intermediate programmers.

so only a crash course will be very good before reading this series, let's go.

What is Python?

  • high-level, general-purpose programming language, designed philosophy emphasizes code readability. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small- and large-scale projects.

Python Paradigms:

Python is dynamically-typed and garbage-collected.

Dynamic type checking is the process of verifying the type safety of a program at runtime not at the compiled time.

Garbage-collected is a form of automatic memory management

It supports multiple programming paradigms, including:

  • Procedural Programming
    To understand this paradigm, just remember that it’s all about Execution depends on Structure, like listener who listens to a button to execute a function.
  • Object-Oriented
    like creating the same product structure from any factory, but each product may have some differences with the attribute values.
  • functional programming
    where programs are constructed by applying and composing functions.

Here are some selected details that are a little bit unique for Python:

Rule 1:

= is creating a new object

i = [13,1]
print(f'id(i) = {id(i)}')
i = [13,1]
print(f'id(i) = {id(i)}')

This will create 2 different variables, so will print different ids like:

id(i) = 2033722832576
id(i) = 2033726327232

but if you used it for equality 2 variables, it will make both of them have the same id
if you made 2 lists are =, and changed the value at index 1 for one of them:

  • that will change just the id of index [1] for both
  • won’t change the id of the 2 lists
i = [13]
x = i
print('case 1-----------------------------')
print(f'id(i) = {id(i)}')
print(f'id(x) = {id(x)}')
print(f'id(i[0]) = {id(i[0])}')

print(f'id(x[0]) = {id(x[0])}')

print('\ncase 2-----------------------------')

i[0] = 12

print(f'id(i) = {id(i)}')
print(f'id(x) = {id(x)}')

print(f'id(i[0]) = {id(i[0])}')

print(f'id(x[0]) = {id(x[0])}')
"""that will print
case 1-----------------------------
id(i) = 2033832840192
id(x) = 2033832840192
id(i[0]) = 2033826818736
id(x[0]) = 2033826818736
case 2-----------------------------
id(i) = 2033832840192
id(x) = 2033832840192
id(i[0]) = 2033826818704
id(x[0]) = 2033826818704
"""

Rule 2:

you can multiple the Strings with any number like printing Hello 30 times:

print(30 * 'Hello ')

Role 3:

the Mutable default value of the Function, will be created once and will add on it for ever see the code below, even I used list that have “Hello”, after it, it will append on the default value

def Mo(menu=[]):
menu.append('Mo')
print(menu)
Mo()
Mo()
Mo(['Hello'])
Mo()

"""
['Mo']
['Mo', 'Mo']
['Hello', 'Mo']
['Mo', 'Mo', 'Mo']
"""

The solution here is to use immutable objects for default values like int or String or just None, and check if the args is None, then make it empty like this:

def Mo(menu=None):
if menu is None:
menu = []
menu.append('Mo')
print(menu)


Mo()
Mo()
Mo(['Hello'])
Mo()

"""
['Mo']
['Mo']
['Hello', 'Mo']
['Mo']
"""

Rule 4 the scopes in Python(LEGB):

  • Local Scope
    A variable created inside a function belongs to the local scope of that function

A variable created inside a function belongs to the local scope of that function

  • Enclosing
    Function Inside Function
  • Global Scope
    A variable created in the main body of the Python code like:
x = 300

def myFun():
print(x)
  • Built-in
    like builtins modules

Note that count at the set_count is a local variable

count = 0

def set_count(c):
count = c
# count here is a local variable

set_count(10)
print(count)
"""
0
"""

to use the global count, like

def set_count(c):
global count
count = c

Rule 5
Everything is an Object, to know it’s type, use type(object) function, to know it’s attributes, use dir(object)

Follow for the next article about the Built-in collections

--

--