<jupyter_start><jupyter_text>Introduction to Python 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.   Variables Variables are objects in Python that can hold anything with numbers or text. Let's look at how to create some variables.<jupyter_code># Numerical example
x = 5
print (x)
y = 2
print (y)

#TOM - explored variable additions.
x = 4
y = 5
print(x+y)

# Text example
x = "hello"
print (x)
# TOM - Explored print command - Typed I love Data Science
x="I "
y = "love "
z = "Data Science!"
print (x+y+z)
a = x + y + z
print (a)
# Variables can be used with each other
a = 1
b = 2
c = a + b
print (c)
# TOM - Explored mathematical operations on variables
a = 2
b = 2
c = a*b
print (c)<jupyter_output>4<jupyter_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.<jupyter_code># 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))
# TOM - Explored variables and type
x = 2*2
print (x)
print (type(x))

y = "Hi"
print (y)
print (type(y))

z = 2.1
print (z)
print (type(z))

a = x>z
print (a)
print (type(a))<jupyter_output>4
<class 'int'>
Hi
<class 'str'>
2.1
<class 'float'>
True
<class 'bool'><jupyter_text>It's good practice to know what types your variables are. When you want to use numerical operations on them, they need to be compatible.<jupyter_code># int variables
a = 5
b = 3
print (a + b)

# string variables
a = "5"
b = "3"
print (a + b)
# TOM - Applied operations on integers and strings.
a = 1
b = 2
print (a+b)

x = "2"
y = "3"
z = " ducklings go "
c = "quack "
d = c*2 + "quack!"
print (x+y+z+d)<jupyter_output>3
23 ducklings go quack quack quack!<jupyter_text>Lists Lists are objects in Python that can hold a ordered sequence of numbers **and** text.<jupyter_code># Creating a list
list_x = [3, "hello", 1]
print (list_x)
# TOM - Created a list

list_y = [1, 1+1, 1+2, 1+3, 1+4]
print(list_y)

# Adding to a list
list_x.append(7)
print (list_x)
# TOM - Adding to the list using append. Used Mathematical operations as well.
list_y = [1, 1+1, 1+2, 1+3, 1+4]
print(list_y)
list_y.append(1+5)
print (list_y)

list_y.append("avg")
print (list_y)

z = 1+2+3+4+5+6
list_y.append(z/6)

print (list_y)

# 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
# TOM - Accessing items at specific locations in a list. Organized the list in Odd/Even plus Ascending/Descending order.

list_y = [1, 1+1, 1+2, 1+3, 1+4]
list_y.append(1+5)
list_y.append("avg")
z = 1+2+3+4+5+6
list_y.append(z/6)
print (list_y)

print ("Even Numbers in ascending order: ", list_y[1], list_y[3], list_y[5])
print ("Odd Numbers in descending order: ", list_y[-4], list_y[-6], list_y[-8])


# 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])
#TOM - Practiced Slicing. Removed the Average calculation

list_y = [1, 1+1, 1+2, 1+3, 1+4]
list_y.append(1+5)
list_y.append("Average")
z = 1+2+3+4+5+6
list_y.append(z/6)
print (list_y[:])
print (list_y[:6])
print (list_y[6:])

# Length of a list
len(list_x)
#TOM - Calculated the length of the list

list_y = [1, 1+1, 1+2, 1+3, 1+4]
list_y.append(1+5)
list_y.append("Average")
z = 1+2+3+4+5+6
list_y.append(z/6)
print (list_y[:])
len (list_y)


# Replacing items in a list
list_x[1] = "hi"
print (list_x)
#TOM Replacing items in a list - By adding to the Average text

list_y = [1, 1+1, 1+2, 1+3, 1+4]
list_y.append(1+5)
list_y.append("Average")
z = 1+2+3+4+5+6
list_y.append(z/6)
print (list_y[:])
list_y[6]="Average of all the numbers is "
print(list_y)

# Combining lists
list_y = [2.4, "world"]
list_z = list_x + list_y
print (list_z)
#TOM Combining lists. Added the Sum portion to the Average.

list_y = [1, 1+1, 1+2, 1+3, 1+4]
list_y.append(1+5)
list_y.append("Average")
z = 1+2+3+4+5+6
list_y.append(z/6)
print (list_y[:])
list_y[6]="Average of the numbers is"
print(list_y)

a = 1+2+3+4+5+6
list_z = ["Sum of the numbers is", a]
print (list_z)

list_b = list_y + list_z
print (list_b)<jupyter_output>[1, 2, 3, 4, 5, 6, 'Average', 3.5]
[1, 2, 3, 4, 5, 6, 'Average of the numbers is', 3.5]
['Sum of the numbers is', 21]
[1, 2, 3, 4, 5, 6, 'Average of the numbers is', 3.5, 'Sum of the numbers is', 21]<jupyter_text>Tuples Tuples are also objects in Python that can hold data but you cannot replace their values (for this reason, tuples are called immutable, whereas lists are known as mutable).<jupyter_code># Creating a tuple
tuple_x = (3.0, "hello")
print (tuple_x)
#TOM - Explored Tuples
tuple_a = ("Hello", 2021)
print(tuple_a)
# Adding values to a tuple
tuple_x = tuple_x + (5.6,)
print (tuple_x)
#TOM - Added 2021 to Hello followed by 2020 and 2019

tuple_a = ("Hello", 2021)
print(tuple_a)

tuple_a = tuple_a + (2020,) + (2019,)
print(tuple_a)

# Trying to change a tuples value (you can't, this should produce an error.)
tuple_x[1] = "world"
#TOM - Error - practiced changing a tuple value

tuple_a = ("Hello", 2021)
print(tuple_a)

tuple_a = tuple_a + (2020,) + (2019,)
print(tuple_a)

tuple_a[1] = "Error"<jupyter_output>('Hello', 2021)
('Hello', 2021, 2020, 2019)<jupyter_text>Dictionaries 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.<jupyter_code># Creating a dictionary
dog = {"name": "dog",
        "eye_color": "brown"}
print (dog)
print (dog["name"])
print (dog["eye_color"])

#TOM - Created a Menu (Dictionary) 
Menu = {"Breakfast": "Eggs", "Lunch": "Vegetables", "Dinner": "Fish"}
print (Menu)
print (Menu["Breakfast"])
print (Menu["Lunch"])
print (Menu["Dinner"])
# Changing the value for a key
dog["eye_color"] = "green"
print (dog)
#TOM - Dictionary (Edit/Modify)
Menu = {"Breakfast": "Eggs", "Lunch": "Vegetables", "Dinner": "Fish"}
print (Menu)


Menu["Breakfast"] = "Blueberry Pancakes"
Menu["Lunch"] = "Crab Cakes"
Menu["Dinner"] = "Salad"
print ("New Menu")
print(Menu)
# Adding new key-value pairs
dog["age"] = 5
print (dog)
#TOM - Dictionary (Adding new key-value pairs)
Menu = {"Breakfast": "Eggs", "Lunch": "Vegetables", "Dinner": "Fish"}
Menu["Breakfast"] = "Blueberry Pancakes"
Menu["Lunch"] = "Crab Cakes"
Menu["Dinner"] = "Salad"

print (Menu)
print ("Adding Snack to the Menu")

Menu["Snack"] = "Crumb Cake"
print(Menu)
# Length of a dictionary
print (len(dog))
#TOM - Dictionary length
Menu = {"Breakfast": "Eggs", "Lunch": "Vegetables", "Dinner": "Fish"}
Menu["Breakfast"] = "Blueberry Pancakes"
Menu["Lunch"] = "Crab Cakes"
Menu["Dinner"] = "Salad"



Menu["Snack"] = "Crumb Cake"
print(Menu)

print(len(Menu))<jupyter_output>{'Breakfast': 'Blueberry Pancakes', 'Lunch': 'Crab Cakes', 'Dinner': 'Salad', 'Snack': 'Crumb Cake'}
4<jupyter_text>If statements You can use `if` statements to conditionally do something.<jupyter_code># If statement
x = 4
if x < 1:
    score = "low"
elif x <= 4:
    score = "medium"
else:
    score = "high"
print (score)
#TOM - If Statements for calculating 4 groups of numbers

x = 45
if x<=100 and x>=75:
    Percentage="75 - 100"
    
elif x<74 and x>=50:
    Percentage="50 - 75"
    
elif x<=49 and x>=25:
    Percentage="25 - 50"
    
else:
    Percentage="0-25"
    
print (Percentage)
    
# If statment with a boolean
x = True
if x:
    print ("it worked")
#Tom

x = True
if x:
    print ("You got it")
else:
    print ("Try again")<jupyter_output>You got it<jupyter_text>Loops In Python, you can use `for` loop to iterate over the elements of a sequence such as a list or tuple, or use `while` loop to do something repeatedly as long as a condition holds.<jupyter_code># 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
#TOM - For Loop - changed the range from 3 to 5 and made the x values even.

x = 0
for i in range(5):
    x = x+2 
    print ("i={0}, x={1}".format(i, x)) # printing with multiple variables   
    
# Loop through items in a list
x = 1
for i in [0, 1, 2]:
    x += 1
    print ("i={0}, x={1}".format(i, x))
#TOM - Loop through list items - Using multiples of 25

x=0
for i in [0, 1, 2, 3]:
    x=x+25
    print ("i={0}, x={1}".format(i,x))
# While loop
x = 3
while x > 0:
    x -= 1 # same as x = x - 1
    print (x)
#Tom - While Loop - Listing multiples of 7 in descending order

x = 70
while x>0:
    x = x-7
    print (x)<jupyter_output>63
56
49
42
35
28
21
14
7
0<jupyter_text>Functions Functions are a way to modularize reusable pieces of code.<jupyter_code># Create a function
def add_two(x):
    x += 2
    return x

# Use the function
score = 0
score = add_two(x=score)
print (score)
#TOM - Function - Adding 10 to a number

def Tens(x):
    x=x+10
    return x

Number = 10
Number = Tens(x=Number)
print(Number)
# 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 = "John"
last_name = "Doe"
joined_name = join_name(first_name=first_name, last_name=last_name)
print (joined_name)
#TOM - Added Middle Initial and changed the naming convention - gave it a different name within the func (full_name vs. proper_name)

def join_name(first_name, mi, last_name):
    full_name = first_name+ " " + mi + " "+ last_name
    return full_name

f_name = "Jane"
m_i = "J"
l_name = "Doe"

proper_name = join_name(first_name=f_name, mi=m_i, last_name=l_name)
print (proper_name)<jupyter_output><NAME><jupyter_text>Classes Classes are a fundamental piece of object oriented programming in Python.<jupyter_code># Creating the class
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
#Tom - Creating a Class and instance of a class - Changed dog to Vegetable - Created an instance of a class here itself. 

class Veg(object):
  
    
    def __init__(self, kind, color, name):
        self.kind = kind
        self.color = color
        self.name = name

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

    # Example function
    def change_name(self, new_name):
        self.name = new_name
        
my_veg = Veg(kind="vegetable", color="Orange", name="Pepper",)
print (my_veg)
print (my_veg.kind)
# Creating an instance of a class
my_dog = Pets(species="dog", color="orange", name="Rover",)
print (my_dog)
print (my_dog.name)
# TOM Please see 154. It includes code for 23.
# Using a class's function
my_dog.change_name(new_name="Sparky")
print (my_dog)
print (my_dog.name)
#TOM - Using a class' function on Vegetables

class Veg(object):
  
    
    def __init__(self, kind, color, name):
        self.kind = kind
        self.color = color
        self.name = name

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

    # Example function
    def change_name(self, new_name):
        self.name = new_name
        
my_veg = Veg(kind="vegetable", color="Orange", name="Pepper",)
print (my_veg)
print (my_veg.name)

my_veg.change_name(new_name="Carrot")
print(my_veg)
print(my_veg.name)<jupyter_output>Orange vegetable named Pepper.
Pepper
Orange vegetable named Carrot.
Carrot
{'fraction_non_alphanumeric': 0.14302134646962233, 'fraction_numerical': 0.032758620689655175, 'mean_word_length': 4.010695187165775, 'pattern_counts': {'":': 14, '<': 41, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '210601', 'license': 'MIT', 'n_tokens_mistral': 4890, 'n_tokens_neox': 4579, 'n_words': 1549, 'path': 'notebooks/Python-in-2-days/D1_L3_Python/__Python_Summary__.ipynb', 'repo_name': 'TinaRekhi/ml_training', 'size': '12184'}