Değişkenler

Variables are containers for holding data and they're defined by a name and value.

# Integer variable
x = 5
print (x)
print (type(x))
5
<class 'int'>

We can change the value of a variable by simply assigning a new value to it.

# String variable
x = "hello"
print (x)
print (type(x))
hello
<class 'str'>

There are many different types of variables: integers, floats, strings, boolean etc.

# int variable
x = 5
print (x, type(x))
5 <class 'int'>
# float variable
x = 5.0
print (x, type(x))
5.0 <class 'float'>
# text variable
x = "5" 
print (x, type(x))
5 <class 'str'>
# boolean variable
x = True
print (x, type(x))
True <class 'bool'>

We can also do operations with variables.

# Variables can be used with each other
a = 1
b = 2
c = a + b
print (c)
3

We should always know what types of variables we're dealing with so we can do the right operations with them. Here's a common mistake that can happen if we're using the wrong variable type.

# int variables
a = 5
b = 3
print (a + b)
8
# string variables
a = "5"
b = "3"
print (a + b)
53

Lists

Lists are an ordered, mutable (changeable) collection of values that are comma separated and enclosed by square brackets. A list can be comprised of many different types of variables (below is a list with an integer, string and a float).

# Creating a list
x = [3, "hello", 1.2]
print (x)
[3, 'hello', 1.2]
# Length of a list
len(x)
3

You can add to a list by using the append function.

# Adding to a list
x.append(7)
print (x)
print (len(x))
[3, 'hello', 1.2, 7]
4
# Replacing items in a list
x[1] = "bye"
print (x)
[3, 'bye', 1.2, 7]
# Operations
y = [2.4, "world"]
z = x + y
print (z)
[3, 'bye', 1.2, 7, 2.4, 'world']

Indexing and Slicing

Indexing and slicing from lists allow us to retrieve specific values within lists. Note that indices can be positive (starting from 0) or negative (-1 and lower, where -1 is the last item in the list).

# Indexing
x = [3, "hello", 1.2]
print ("x[0]: ", x[0])
print ("x[1]: ", x[1])
print ("x[-1]: ", x[-1]) # the last item
print ("x[-2]: ", x[-2]) # the second to last item
x[0]:  3
x[1]:  hello
x[-1]:  1.2
x[-2]:  hello
# Slicing
print ("x[:]: ", x[:]) # all indices
print ("x[1:]: ", x[1:]) # index 1 to the end of the list
print ("x[1:2]: ", x[1:2]) # index 1 to index 2 (not including index 2)
print ("x[:-1]: ", x[:-1]) # index 0 to last index (not including last index)
x[:]:  [3, 'hello', 1.2]
x[1:]:  ['hello', 1.2]
x[1:2]:  ['hello']
x[:-1]:  [3, 'hello']

Tuples

Tuples are collections that are ordered and immutable (unchangeable). You will use these to store values that will never be changed.

# Creating a tuple
x = (3.0, "hello") # tuples start and end with ()
print (x)
(3.0, 'hello')
# Adding values to a tuple
x = x + (5.6, 4)
print (x)
(3.0, 'hello', 5.6, 4)
# Try to change (it won't work and you'll get an error)
x[0] = 1.2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-d0da6f639f74> in <module>()
----> 1 x[0] = 1.2

TypeError: 'tuple' object does not support item assignment

Dictionaries

Dictionaries are an unordered, mutable and indexed collection of key-value pairs. You can retrieve values based on the key and a dictionary cannot have two of the same keys.

# Creating a dictionary
person = {'name': 'Goku',
          'eye_color': 'brown'}
print (person)
print (person['name'])
print (person['eye_color'])
{'name': 'Goku', 'eye_color': 'brown'}
Goku
brown
# Changing the value for a key
person['eye_color'] = 'green'
print (person)
{'name': 'Goku', 'eye_color': 'green'}
# Adding new key-value pairs
person['age'] = 24
print (person)
{'name': 'Goku', 'eye_color': 'green', 'age': 24}
# Length of a dictionary
print (len(person))
3

If statements

We can use if statements to conditionally do something. The conditions are defined by the words if, elif (which stands for else if) and else. We can have as many elif statements as we want. The indented code below each condition is the code that will execute if the condition is True.

# If statement
x = 4
if x < 1:
    score = 'low'
elif x <= 4: # elif = else if
    score = 'medium'
else:
    score = 'high'
print (score)
medium
# If statement with a boolean
x = True
if x:
    print ("it worked")
it worked

Loops

For Loops

A for loop can iterate over a collection of values (lists, tuples, dictionaries, etc.) The indented code is executed for each item in the collection of values.

# For loop
veggies = ["carrots", "broccoli", "beans"]
for veggie in veggies:
    print (veggie)
carrots
broccoli
beans

When the loop encounters the break command, the loop will terminate immediately. If there were more items in the list, they will not be processed.

# `break` from a for loop
veggies = ["carrots", "broccoli", "beans"]
for veggie in veggies:
    if veggie == "broccoli":
        break
    print (veggie)
carrots

When the loop encounters the continue command, the loop will skip all other operations for that item in the list only. If there were more items in the list, the loop will continue normally.

# `continue` to the next iteration
veggies = ["carrots", "broccoli", "beans"]
for veggie in veggies:
    if veggie == "broccoli":
        continue
    print (veggie)
carrots
beans

While Loops

A while loop can perform repeatedly as long as a condition is True. We can use continue and break commands in while loops as well.

# While loop
x = 3
while x > 0:
    x -= 1 # same as x = x - 1
    print (x)
2
1
0

Functions

Functions are a way to modularize reusable pieces of code. They're defined by the keyword def which stands for definition and they can have the following components.

# Define the function
def add_two(x):
    """Increase x by 2.""" # explains what this function will do
    x += 2
    return x

Here are the components that may be required when we want to use the function. we need to ensure that the function name and the input parameters match with how we defined the function above.

# Use the function
score = 0
new_score = add_two(x=score)
print (new_score)
2

A function can have as many input parameters and outputs as we want.

# Function with multiple inputs
def join_name(first_name, last_name):
    """Combine first name and last name."""
    joined_name = first_name + " " + last_name
    return joined_name
# Use the function
first_name = "Goku"
last_name = "Mohandas"
joined_name = join_name(first_name=first_name, 
                        last_name=last_name)
print (joined_name)
Goku Mohandas

NOTE: It's good practice to always use keyword argument when using a function so that it's very clear what input variable belongs to what function input parameter. On a related note, you will often see the terms *args and **kwargs which stand for arguments and keyword arguments. You can extract them when they are passed into a function. The significance of the * is that any number of arguments and keyword arguments can be passed into the function.

def f(*args, **kwargs):
    x = args[0]
    y = kwargs.get('y')
    print (f"x: {x}, y: {y}")
f(5, y=2)
x: 5, y: 2

Classes

Classes are object constructors and are a fundamental component of object oriented programming in Python. They are composed of a set of functions that define the class and it's operations.

__init__() function

The init function is used when an instance of the class is initialized.

# Creating the class
class Pet(object):
    """Class object for a pet."""
  
    def __init__(self, species, name):
        """Initialize a Pet."""
        self.species = species
        self.name = name
# Creating an instance of a class
my_dog = Pet(species="dog", 
             name="Scooby")
print (my_dog)
print (my_dog.name)
<__main__.Pet object at 0x7fe487e9c358>
Scooby

__str()__ function

The print (my_dog) command printed something not so relevant to us. Let's fix that with the __str()__ function.

# Creating the class
class Pet(object):
    """Class object for a pet."""
  
    def __init__(self, species, name):
        """Initialize a Pet."""
        self.species = species
        self.name = name
 
    def __str__(self):
        """Output when printing an instance of a Pet."""
        return f"{self.species} named {self.name}"
# Creating an instance of a class
my_dog = Pet(species="dog", 
             name="Scooby")
print (my_dog)
print (my_dog.name)
dog named Scooby
Scooby

NOTE: Classes can be customized with magic functions like, __str__, to enable powerful operations. We'll be exploring additional built-in functions in subsequent notebooks (like __iter__ and __getitem__) but if you're curious, here is a tutorial on more magic methods.

Object methods

# Creating the class
class Pet(object):
    """Class object for a pet."""
  
    def __init__(self, species, name):
        """Initialize a Pet."""
        self.species = species
        self.name = name
 
    def __str__(self):
        """Output when printing an instance of a Pet."""
        return f"{self.species} named {self.name}"
        
    def change_name(self, new_name):
        """Change the name of your Pet."""
        self.name = new_name
# Creating an instance of a class
my_dog = Pet(species="dog", 
             name="Scooby")
print (my_dog)
print (my_dog.name)
dog named Scooby
Scooby
# Using a class's function
my_dog.change_name(new_name="Scrappy")
print (my_dog)
print (my_dog.name)
dog named Scrappy
Scrappy

Inheritance

Inheritance allows you to inherit all the properties and methods from another class (the parent). Notice how we inherited the initialized variables from the parent Pet class like species and name. We also inherited the change_name function. But for the __str__ function, we define our own version to overwrite the Pet class' __str__ function.

class Dog(Pet):
    def __init__(self, species, name, breed):
        super().__init__("dog", name)
        self.breed = breed
    
    def __str__(self):
        return f"{self.breed} named {self.name}"
scooby = Dog(species="dog", breed="Great Dane", name="Scooby")
print (scooby)
Great Dane named Scooby
scooby.change_name('Scooby Doo')
print (scooby)
Great Dane named Scooby Doo

Decorators

Recall that functions allow us to modularize code and reuse them. However, we'll often want to add some functionality before or after the main function executes and we may want to do this for many different functions. Instead of adding more code to the original function, we can use decorators!

  • decorators: augment a function with pre/post-processing. Decorators wrap around the main function and allow us to operate on the inputs and or outputs.

Suppose we have a function called operations which increments the input value x by 1.

def operations(x):
    """Basic operations."""
    x += 1
    return x
operations(x=1)
2

Now let's say we want to increment our input x by 1 before and after the operations function executes and, to illustrate this example, let's say the increments have to be separate steps. Here's how we would do it by changing the original code:

def operations(x):
    """Basic operations."""
    x += 1 
    x += 1
    x += 1
    return x
operations(x=1)
4

We were able to achieve what we want but we now increased the size of our operations function and if we want to do the same incrementation for any other function, we have to add the same code to all of those as well ... not very efficient. To solve this, let's create a decorator called add which increments x by 1 before and after the main function f executes.

Creating a decorator function

The decorator function accepts a function f which is the function we wish to wrap around (in our case, it's operations). The output of the decorator is its wrapper function which receives the arguments and keyword arguments passed to function f.

Inside the wrapper function, we can extract the input parameters [line 5] passed to function f and make any changes we want [line 6]. Then the function f is executed [line 7] and then we can make changes to the outputs as well [line 8]. Finally, the wrapper function will return some value(s) [line 9] which is what the decorator returns as well since it returns wrapper.

# Decorator
def add(f):
    def wrapper(*args, **kwargs):
        """Wrapper function for @add."""
        x = kwargs.pop('x') # .get() if not altering x
        x += 1 # executes before function f
        x = f(*args, **kwargs, x=x)
        x += 1 # executes after function f
        return x
    return wrapper

We can use this decorator by simply adding it to the top of our main function preceded by the @ symbol.

@add
def operations(x):
    """Basic operations."""
    x += 1
    return x
operations(x=1)
4

Suppose we wanted to debug and see what function actually executed with operations.

operations.__name__, operations.__doc__
('wrapper', 'Wrapper function for @add.')

The function name and docstring are not what we're looking for but it appears this way because the wrapper function is what was executed. In order to fix this, Python offers functools.wraps which carries the main function's metadata.

from functools import wraps
# Decorator
def add(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        """Wrapper function for @add."""
        x = kwargs.pop('x') 
        x += 1
        x = f(*args, **kwargs, x=x)
        x += 1
        return x
    return wrap
@add
def operations(x):
    """Basic operations."""
    x += 1
    return x
operations.__name__, operations.__doc__
('operations', 'Basic operations.')

Awesome! We were able to decorate our main function operation to achieve the customization we wanted without actually altering the function. We can reuse our decorator for other functions that may need the same customization!

This was a dummy example to show how decorators work but we'll be using them heavily during our production ML lessons. A simple scenario would be using decorators to create uniform JSON responses from each API endpoint without including the bulky code in each endpoint.

Callbacks

Decorators allow for customized operations before and after the main function's execution but what about in between? Suppose we want to conditionally/situationally do some operations. Instead of writing a whole bunch of if-statements and make our functions bulky, we can use callbacks!

  • callbacks: conditional/situational processing within the function.

Our callbacks will be classes that have functions with key names that will execute at various periods during the main function's execution. The function names are up to us but we need to invoke the same callback functions within our main function.

# Callback
class x_tracker(object):
    def __init__(self, x):
        self.history = []
    def at_start(self, x):
        self.history.append(x)
    def at_end(self, x):
        self.history.append(x)

We can pass in as many callbacks as we want and because they have appropriately named functions, they will be invoked at the appropriate times.

def operations(x, callbacks=[]):
    """Basic operations."""
    for callback in callbacks:
        callback.at_start(x)
    x += 1
    for callback in callbacks:
        callback.at_end(x)
    return x
x = 1
tracker = x_tracker(x=x)
operations(x=x, callbacks=[tracker])
2
tracker.history
[1, 2]

Putting it all together

decorators + callbacks = powerful customization before, during and after the main function’s execution without increasing its complexity. We will be using this duo to create powerful ML training scripts that are highly customizable in future lessons.

from functools import wraps
# Decorator
def add(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        """Wrapper function for @add."""
        x = kwargs.pop('x') # .get() if not altering x
        x += 1 # executes before function f
        x = f(*args, **kwargs, x=x)
        # can do things post function f as well
        return x
    return wrap
# Callback
class x_tracker(object):
    def __init__(self, x):
        self.history = [x]
    def at_start(self, x):
        self.history.append(x)
    def at_end(self, x):
        self.history.append(x)
# Main function
@add
def operations(x, callbacks=[]):
    """Basic operations."""
    for callback in callbacks:
        callback.at_start(x)
    x += 1
    for callback in callbacks:
        callback.at_end(x)
    return x
x = 1
tracker = x_tracker(x=x)
operations(x=x, callbacks=[tracker])
3
tracker.history
[1, 2, 3]

Additional resources

  • Python 3: This was a very quick look at Python but it's good enough for practical machine learning and we'll be learning more in future lessons. If you want to learn more, check out this free Python3 course.

Share and discover ML projects at Made With ML.