# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="bOChJSNXtC9g" colab_type="text"
# # Introduction to Python

# + [markdown] id="OLIxEDq6VhvZ" colab_type="text"
# <img src="https://raw.githubusercontent.com/GokuMohandas/practicalAI/master/images/logo.png" width=150>
#
# In this lesson we will learn the basics of the Python programming language (version 3). We won't learn everything about Python but enough to do some basic machine learning.
#
# <img src="https://raw.githubusercontent.com/GokuMohandas/practicalAI/master/images/python.png" width=350>
#
#
#

# + [markdown] id="VoMq0eFRvugb" colab_type="text"
# #  Variables

# + [markdown] id="qWro5T5qTJJL" colab_type="text"
# Variables are objects in python that can hold anything with numbers or text. Let's look at how to make some variables.

# + id="0-dXQiLlTIgz" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="13d53c5b-6941-41ac-de44-edb4419158fb"
# Numerical example
x = 5
print (x)

# + id="5Ym0owFxTkjo" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="2a99cb83-5cc5-4728-9b4a-a97a46afa7c5"
# Text example
x = "hello"
print (x)

# + id="1a4ZhMV1T1-0" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="8775f617-e7d3-4a95-aa8b-7f15ed6fca6f"
# Variables can be used with each other
a = 1
b = 2
c = a + b
print (c)

# + [markdown] id="nbKV4aTdUC1_" colab_type="text"
# Variables can come in lots of different types. Even within numerical variables, you can have integers (int), floats (float), etc. All text based variables are of type string (str). We can see what type a variable is by printing its type.

# + id="c3NJmfO4Uc6V" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 153} outputId="619ba3a4-9589-466f-ff0d-179de822f4b5"
# int variable
x = 5
print (x)
print (type(x))

# float variable
x = 5.0
print (x)
print (type(x))

# text variable
x = "5" 
print (x)
print (type(x))

# boolean variable
x = True
print (x)
print (type(x))

# + [markdown] id="6HPtavfdU8Ut" colab_type="text"
# It's good practice to know what types your variables are. When you want to use numerical operations on then, they need to be compatible. 

# + id="8pr1-i7IVD-h" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="409eab3a-4767-4333-c5dc-48965fd2eae2"
# int variables
a = 5
b = 3
print (a + b)

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

# + [markdown] id="q4R_UF6PVw4V" colab_type="text"
# #  Lists

# + [markdown] id="LvGsQBj4VjMl" colab_type="text"
# Lists are objects in python that can hold a ordered sequence of numbers **and** text.

# + id="9iPESkq9VvlX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="0ba3299d-a49c-41a8-be8f-ea292d07099b"
# Making a list
list_x = [3, "hello", 1]
print (list_x)

# + [markdown] id="0xC6WvuwbGDg" colab_type="text"
#

# + id="7lbajc-zV515" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="dc63b23c-1b9f-4668-e40a-dd3168e20d4e"
# Adding to a list
list_x.append(7)
print (list_x)

# + id="W0xpIryJWCN9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 102} outputId="ff4556f8-bc22-47ad-880f-4e5b9f99193e"
# Accessing items at specific location in a list
print ("list_x[0]: ", list_x[0])
print ("list_x[1]: ", list_x[1])
print ("list_x[2]: ", list_x[2])
print ("list_x[-1]: ", list_x[-1]) # the last item
print ("list_x[-2]: ", list_x[-2]) # the second to last item

# + id="VSu_HNrnc1WK" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 85} outputId="46c5d289-f7b1-4360-c832-c1332a7783ec"
# Slicing
print ("list_x[:]: ", list_x[:])
print ("list_x[2:]: ", list_x[2:])
print ("list_x[1:3]: ", list_x[1:3])
print ("list_x[:-1]: ", list_x[:-1])

# + id="dImY-hVzWxB4" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="def2a30f-00e1-402c-bbed-2ec5476a0494"
# Length of a list
len(list_x)

# + id="3-reXDniW_sm" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6afcd0d4-03eb-427f-9cdb-7e4053e5ba30"
# Replacing items in a list
list_x[1] = "hi"
print (list_x)

# + id="X8T5I3bjXJ0S" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="388ff773-e62f-4ee6-b7a5-c67713f83d41"
# Combining lists
list_y = [2.4, "world"]
list_z = list_x + list_y
print (list_z)

# + [markdown] id="ddpIO6LLVzh0" colab_type="text"
# # Tuples

# + [markdown] id="CAZblq7oXY3s" colab_type="text"
# Tuples are also objects in python that can hold data but you cannot replace values (for this reason, tuples are called immutable, whereas lists are known as mutable).

# + id="G95lu8xWXY90" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6350ad63-0f99-482f-ad6d-41b037e058e6"
# Creating a tuple
tuple_x = (3.0, "hello")
print (tuple_x)

# + id="kq23Bej1acAP" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="9c763fff-9862-40ef-d045-b0cd9b6dce13"
# Adding values to a tuple
tuple_x = tuple_x + (5.6,)
print (tuple_x)

# + id="vyTmOc6BXkge" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 164} outputId="972c571e-c530-4826-f485-1d850383b12b"
# Trying to change a tuples value (you can't)
tuple_x[1] = "world"

# + [markdown] id="UdlJHkwZV3Mz" colab_type="text"
# # Dictionaries

# + [markdown] id="azp3AoxYXS26" colab_type="text"
# Dictionaries are python objects that hold key-value pairs. In the example dictionary below, the keys are the "name" and "eye_color" variables. They each have a value associated with them. A dictionary cannot have two of the same keys. 

# + id="pXhNLbzpXXSk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="405c35a0-2791-42ac-fd94-59567f4b6800"
# Creating a dictionary
goku = {"name": "Goku",
        "eye_color": "brown"}
print (goku)
print (goku["name"])
print (goku["eye_color"])


# + id="1HXtX8vQYjXa" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="d0a6d036-dcc7-43bc-a099-2562d770be15"
# Changing the value for a key
goku["eye_color"] = "green"
print (goku)

# + id="qn33iB0MY5dT" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="76304793-cd3d-4b22-a2d0-847a9bfcf02c"
# Adding new key-value pairs
goku["age"] = 24
print (goku)

# + id="g9EYmzMKa9YV" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="ffe8ec5f-09b3-45af-c880-812428f291a5"
# Length of a dictionary
print (len(goku))å

# + [markdown] id="B-DInx_Xo2vJ" colab_type="text"
# # If statements

# + [markdown] id="ZG_ICGRGo4tY" colab_type="text"
# You can use if statements to conditionally do something.

# + id="uob9lQuKo4Pg" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="8fb241ae-d1e1-4457-92e7-019ec3737b48"
# If statement
x = 4
if x < 1:
    score = "low"
elif x <= 4:
    score = "medium"
else:
    score = "high"
print (score)

# + id="vwsQaZqIpfJ3" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="0ff2a53b-28e5-468c-9b20-75eee0bb528f"
# If statment with a boolean
x = True
if x:
    print ("it worked")

# + [markdown] id="sJ7NPGEKV6Ik" colab_type="text"
# # Loops

# + [markdown] id="YRVxhVCkn0vc" colab_type="text"
# You can use for or while loops in python to do something repeatedly until a condition is met.

# + id="OB5PtyqAn8mj" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="5bbd3474-486a-4652-c82c-f63372da1f61"
# For loop
x = 1
for i in range(3): # goes from i=0 to i=2
    x += 1 # same as x = x + 1
    print ("i={0}, x={1}".format(i, x)) # printing with multiple variables

# + id="6XyhCrFeoGj4" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="6004d886-596a-426e-c72f-8adde0a34189"
# Loop through items in a list
x = 1
for i in [0, 1, 2]:
    x += 1
    print ("i={0}, x={1}".format(i, x))

# + id="5Tf2x4okp3fH" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 68} outputId="3423016a-b8b5-479c-f979-8ab8253d1d04"
# While loop
x = 3
while x > 0:
    x -= 1 # same as x = x - 1
    print (x)


# + [markdown] id="gJw-EDO9WBL_" colab_type="text"
# # Functions

# + [markdown] id="hDIOUdWCqBwa" colab_type="text"
# Functions are a way to modularize resuable pieces of code. 

# + id="iin1ZXmMqA0y" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="eaf42827-db58-4ec7-e038-7d2743e666cc"
# Create a function
def add_two(x):
    x += 2
    return x

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


# + id="DC6x3DMrqlE3" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="c9360480-1553-4358-a646-4ecf74064fff"
# Function with multiple inputs
def join_name(first_name, 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)


# + [markdown] id="lBLa1n54WEd2" colab_type="text"
# # Classes

# + [markdown] id="mGua8QnArAZh" colab_type="text"
# Classes are a fundamental piece of object oriented Python programming.

# + id="DXmPwI1frAAd" colab_type="code" colab={}
# Create the function
class Pets(object):
  
    # Initialize the class
    def __init__(self, species, color, name):
        self.species = species
        self.color = color
        self.name = name

    # For printing  
    def __str__(self):
        return "{0} {1} named {2}.".format(self.color, self.species, self.name)

    # Example function
    def change_name(self, new_name):
        self.name = new_name


# + id="ezQq_Fhhrqrv" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="585493c2-1fcf-4b68-d4fb-d3f00c4c7433"
# Making an instance of a class
my_shiba = Pets(species="dog", color="orange", name="Guiness",)
print (my_shiba)
print (my_shiba.name)

# + id="qTinlRj1szc5" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="1f9e75b8-e43a-484d-ea3e-c0123374382a"
# Using a class's function
my_shiba.change_name(new_name="Charlie")
print (my_shiba)
print (my_shiba.name)

# + [markdown] id="kiWtd0aJtNtY" colab_type="text"
# # Additional resources

# + [markdown] id="cfLF4ktmtSC3" colab_type="text"
# This was a very quick look at python and we'll be learning more in future lessons. If you want to learn more right now before diving into machine learning, check out this free course: [Free Python Course](https://www.codecademy.com/learn/learn-python)

StarcoderdataJupyterScriptsDedupFiltered