Skip to content

01-Python Crash Course

Updated
17 Sept 2026
2 min read
13 sections
Page 3 of 117
Python & Machine Learning

Data types

Numbers

python
1 + 1
Out · result
2
python
1 * 3
Out · result
3
python
1 / 2
Out · result
0.5
python
2 ** 4
Out · result
16
python
4 % 2
Out · result
0
python
5 % 2
Out · result
1
python
(2 + 3) * (5 + 5)
Out · result
50

Variable Assignment

python
# Can not start with number or special characters
name_of_var = 2
python
x = 2
y = 3
python
z = x + y
python
z
Out · result
5

Strings

python
'single quotes'
Out · result
'single quotes'
python
"double quotes"
Out · result
'double quotes'
python
" wrap lot's of other quotes"
Out · result
" wrap lot's of other quotes"

Printing

python
x = 'hello'
python
x
Out · result
'hello'
python
print(x)
Out · stdout
hello
python
num = 12
name = 'Sam'
python
print('My number is: {one}, and my name is: {two}'.format(one=num,two=name))
Out · stdout
My number is: 12, and my name is: Sam
python
print('My number is: {}, and my name is: {}'.format(num,name))
Out · stdout
My number is: 12, and my name is: Sam

Lists

python
[1,2,3]
Out · result
[1, 2, 3]
python
['hi',1,[1,2]]
Out · result
['hi', 1, [1, 2]]
python
my_list = ['a','b','c']
python
my_list.append('d')
python
my_list
Out · result
['a', 'b', 'c', 'd']
python
my_list[0]
Out · result
'a'
python
my_list[1]
Out · result
'b'
python
my_list[1:]
Out · result
['b', 'c', 'd']
python
my_list[:1]
Out · result
['a']
python
my_list[0] = 'NEW'
python
my_list
Out · result
['NEW', 'b', 'c', 'd']
python
nest = [1,2,3,[4,5,['target']]]
python
nest[3]
Out · result
[4, 5, ['target']]
python
nest[3][2]
Out · result
['target']
python
nest[3][2][0]
Out · result
'target'

Dictionaries

python
d = {'key1':'item1','key2':'item2'}
python
d
Out · result
{'key1': 'item1', 'key2': 'item2'}
python
d['key1']
Out · result
'item1'

Booleans

python
True
Out · result
True
python
False
Out · result
False

Tuples

python
t = (1,2,3)
python
t[0]
Out · result
1
python
t[0] = 'NEW'
Out · error
TypeError: 'tuple' object does not support item assignment
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-44-97e4e33b36c2> in <module>()
----> 1 t[0] = 'NEW'
2 more lines

TypeError: 'tuple' object does not support item assignment

Sets

python
{1,2,3}
Out · result
{1, 2, 3}
python
{1,2,3,1,2,1,2,3,3,3,3,2,2,2,1,1,2}
Out · result
{1, 2, 3}

Comparison Operators

python
1 > 2
Out · result
False
python
1 < 2
Out · result
True
python
1 >= 1
Out · result
True
python
1 <= 4
Out · result
True
python
1 == 1
Out · result
True
python
'hi' == 'bye'
Out · result
False

Logic Operators

python
(1 > 2) and (2 < 3)
Out · result
False
python
(1 > 2) or (2 < 3)
Out · result
True
python
(1 == 2) or (2 == 3) or (4 == 4)
Out · result
True

if,elif, else Statements

python
if 1 < 2:
    print('Yep!')
Out · stdout
Yep!
python
if 1 < 2:
    print('yep!')
Out · stdout
yep!
python
if 1 < 2:
    print('first')
else:
    print('last')
Out · stdout
first
python
if 1 > 2:
    print('first')
else:
    print('last')
Out · stdout
last
python
if 1 == 2:
    print('first')
elif 3 == 3:
    print('middle')
else:
    print('Last')
Out · stdout
middle

for Loops

python
seq = [1,2,3,4,5]
python
for item in seq:
    print(item)
Out · stdout
1
2
3
4
5
python
for item in seq:
    print('Yep')
Out · stdout
Yep
Yep
Yep
Yep
Yep
python
for jelly in seq:
    print(jelly+jelly)
Out · stdout
2
4
6
8
10

while Loops

python
i = 1
while i < 5:
    print('i is: {}'.format(i))
    i = i+1
Out · stdout
i is: 1
i is: 2
i is: 3
i is: 4

range()

python
range(5)
Out · result
range(0, 5)
python
for i in range(5):
    print(i)
Out · stdout
0
1
2
3
4
python
list(range(5))
Out · result
[0, 1, 2, 3, 4]

list comprehension

python
x = [1,2,3,4]
python
out = []
for item in x:
    out.append(item**2)
print(out)
Out · stdout
[1, 4, 9, 16]
python
[item**2 for item in x]
Out · result
[1, 4, 9, 16]

functions

python
def my_func(param1='default'):
    """
    Docstring goes here.
    """
    print(param1)
python
my_func
Out · result
<function __main__.my_func>
python
my_func()
Out · stdout
default
python
my_func('new param')
Out · stdout
new param
python
my_func(param1='new param')
Out · stdout
new param
python
def square(x):
    return x**2
python
out = square(2)
python
print(out)
Out · stdout
4

lambda expressions

python
def times2(var):
    return var*2
python
times2(2)
Out · result
4
python
lambda var: var*2
Out · result
<function __main__.<lambda>>

map and filter

python
seq = [1,2,3,4,5]
python
map(times2,seq)
Out · result
<map at 0x105316748>
python
list(map(times2,seq))
Out · result
[2, 4, 6, 8, 10]
python
list(map(lambda var: var*2,seq))
Out · result
[2, 4, 6, 8, 10]
python
filter(lambda item: item%2 == 0,seq)
Out · result
<filter at 0x105316ac8>
python
list(filter(lambda item: item%2 == 0,seq))
Out · result
[2, 4]

methods

python
st = 'hello my name is Sam'
python
st.lower()
Out · result
'hello my name is sam'
python
st.upper()
Out · result
'HELLO MY NAME IS SAM'
python
st.split()
Out · result
['hello', 'my', 'name', 'is', 'Sam']
python
tweet = 'Go Sports! #Sports'
python
tweet.split('#')
Out · result
['Go Sports! ', 'Sports']
python
tweet.split('#')[1]
Out · result
'Sports'
python
d
Out · result
{'key1': 'item1', 'key2': 'item2'}
python
d.keys()
Out · result
dict_keys(['key2', 'key1'])
python
d.items()
Out · result
dict_items([('key2', 'item2'), ('key1', 'item1')])
python
lst = [1,2,3]
python
lst.pop()
Out · result
3
python
lst
Out · result
[1, 2]
python
'x' in [1,2,3]
Out · result
False
python
'x' in ['x','y','z']
Out · result
True

Great Job!