01 Python 01 Programming Basics
01 Python 01 Programming Basics
Python
Multi-platform
Interpreted / Dynamic Typing
Garbage Collected
Object-Oriented & Procedural
Large Standard Library
REPL
Read, Eval, Print, Loop.
ipython
Documentation
👉 The Python 3.8 Standard Library (https://docs.python.org/3.8/library/index.html)
Built-in Types
Let's look at the important sections of the documentation (https://docs.python.org/3.8/library/stdtypes.html)
and explore the simple built-in types.
In [ ]:
type(42)
Out[ ]:
int
In [ ]:
type(3.14)
Out[ ]:
float
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 1/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
4 + 9.5
Out[ ]:
13.5
In [ ]:
abs(-5)
Out[ ]:
Booleans (https://docs.python.org/3.8/library/stdtypes.html#truth-value-
testing)
In [ ]:
type(True) # or type(False)
Out[ ]:
bool
In [ ]:
Out[ ]:
False
In [ ]:
True or False
Out[ ]:
True
In [ ]:
not True
Out[ ]:
False
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 2/13
09/05/2023 09:47 01-Python_01-Programming-Basics
Comparisons
In [ ]:
Out[ ]:
False
In [ ]:
Out[ ]:
True
In [ ]:
Out[ ]:
False
In [ ]:
Out[ ]:
True
In [ ]:
"boris"
Out[ ]:
'boris'
In [ ]:
"boris".capitalize()
Out[ ]:
'Boris'
In [ ]:
Out[ ]:
'boris'
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 3/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
type(int("42"))
Out[ ]:
int
Sequence Types
list (https://docs.python.org/3.7/library/stdtypes.html#list)
tuple (https://docs.python.org/3.7/library/stdtypes.html#tuple)
range (https://docs.python.org/3.7/library/stdtypes.html#range)
An example of list
In [ ]:
Out[ ]:
In [ ]:
Out[ ]:
In [ ]:
Out[ ]:
True
Mapping Type
There is just one in Python, dict (read dictionary)
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 4/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
Out[ ]:
In [ ]:
Out[ ]:
In [ ]:
Out[ ]:
True
Variables
In [ ]:
John
In [ ]:
Paul
In [ ]:
first_name = 'John'
last_name = 'Lennon'
In [ ]:
JohnLennon
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 5/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
list operations
In [ ]:
# Indexes: 0 1 2
beatles = ['john', 'paul', 'ringo']
In [ ]:
beatles[0] # Read
Out[ ]:
'john'
In [ ]:
In [ ]:
In [ ]:
beatles
Out[ ]:
In [ ]:
Out[ ]:
['Paul', 'ringo']
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 6/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
dict operations
In [ ]:
In [ ]:
instruments['john'] # Read
Out[ ]:
'guitar'
In [ ]:
In [ ]:
del instruments['john']
instruments
Out[ ]:
In [ ]:
instruments
Out[ ]:
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 7/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
instruments['john']
--------------------------------------------------------------------------
-
KeyError Traceback (most recent call las
t)
<ipython-input-40-4f6ae9ee253b> in <module>
----> 1 instruments['john']
KeyError: 'john'
In [ ]:
Out[ ]:
'Default instrument'
Control Flow
Once again, the documentation is very thorough (https://docs.python.org/3.8/tutorial/controlflow.html)
if statement
In [ ]:
In [ ]:
CAT
WOLF
BEETLE
List comprehension
You can achieve the same result in just one line:
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 8/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
In [ ]:
In [ ]:
0 cat
1 wolf
2 beetle
In [ ]:
instruments
Out[ ]:
In [ ]:
while loop
In [ ]:
i = 1
while i <= 4:
print(i)
i = i + 1
1
2
3
4
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 9/13
09/05/2023 09:47 01-Python_01-Programming-Basics
Functions
Let's see how we can define and use our own functions
(https://docs.python.org/3.8/tutorial/controlflow.html#defining-functions).
In [ ]:
def is_even(number):
return number % 2 == 0
In [ ]:
result = is_even(42)
print(result)
True
We called the function with the argument 42 . The returned value is stored in a result variable.
Keyword Arguments
Let's have a look at pandas.DataFrame.from_dict (https://pandas.pydata.org/pandas-
docs/stable/reference/api/pandas.DataFrame.from_dict.html) (Source (https://github.com/pandas-
dev/pandas/blob/v1.0.3/pandas/core/frame.py#L1168-L1247)). Some arguments are passed with the syntax
parameter=value .
In [ ]:
def is_odd(number=0):
return number % 2 == 1
In [ ]:
result = is_odd(number=1)
print(result)
True
In [ ]:
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 10/13
09/05/2023 09:47 01-Python_01-Programming-Basics
In [ ]:
print(full_name("john", "lennon"))
print(full_name("ringo", "starr", capitalize=True))
john lennon
Ringo Starr
Modules (https://docs.python.org/3/tutorial/modules.html)
As our programs will grow longer, we will want to split the code into many files & folders.
In [ ]:
# helpers/__init__.py
import random
import string
def generate_password(size):
"""Generate a random lowercase ASCII password of given size"""
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(size))
In [ ]:
# app.py
from helpers import generate_password # Module import here
print(generate_password(16))
python app.py
Debugging 🐛
print(your_variable)
ipython
python <file_name>
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 11/13
09/05/2023 09:47 01-Python_01-Programming-Basics
When running your code with python <file_name> you can use the IPython debugger (ipdb)
(https://ipython.readthedocs.io/en/stable/) thanks to the built-in breakpoint()
(https://docs.python.org/3/library/functions.html#breakpoint) function.
Add the debugger in the function definition and run the app.py file from the Terminal.
In [ ]:
# helpers/__init__.py
import random
import string
You can access the args , variables defined within the function ( letters ) and much more.
next - until the next line in the current function is reached or it returns
continue - only stop when a breakpoint is encountered ( breakpoint )
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 12/13
09/05/2023 09:47 01-Python_01-Programming-Basics
Your turn!
Head onto Kitt, find your buddy and start your first challenge!
Remember:
Have fun! 🚀
https://kitt.lewagon.com/camps/1173/lectures/content/01-Python_01-Programming-Basics.html 13/13