<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)
# Text example
x = "hello"
print (x)
# Variables can be used with each other
a = 1
b = 2
c = a + b
print (c)<jupyter_output>3<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))<jupyter_output>5
<class 'int'>
5.0
<class 'float'>
5
<class 'str'>
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)<jupyter_output>8
53<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)
# Adding to a list
list_x.append(7)
print (list_x)
# 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
# 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])
# Length of a list
len(list_x)
# Replacing items in a list
list_x[1] = "hi"
print (list_x)
# Combining lists
list_y = [2.4, "world"]
list_z = list_x + list_y
print (list_z)<jupyter_output>[3, 'hi', 1, 7, 2.4, 'world']<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)
# Adding values to a tuple
tuple_x = tuple_x + (5.6,)
print (tuple_x)
# Trying to change a tuples value (you can't, this should produce an error.)
tuple_x[1] = "world"<jupyter_output><empty_output><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"])

# Changing the value for a key
dog["eye_color"] = "green"
print (dog)
# Adding new key-value pairs
dog["age"] = 5
print (dog)
# Length of a dictionary
print (len(dog))<jupyter_output>3<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)
# If statment with a boolean
x = True
if x:
    print ("it worked")<jupyter_output>it worked<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
# Loop through items in a list
x = 1
for i in [0, 1, 2]:
    x += 1
    print ("i={0}, x={1}".format(i, x))
# While loop
x = 3
while x > 0:
    x -= 1 # same as x = x - 1
    print (x)<jupyter_output>2
1
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)
# 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)<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
# Creating an instance of a class
my_dog = Pets(species="dog", color="orange", name="Rover",)
print (my_dog)
print (my_dog.name)
# Using a class's function
my_dog.change_name(new_name="Sparky")
print (my_dog)
print (my_dog.name)<jupyter_output>orange dog named Sparky.
Sparky
{'fraction_non_alphanumeric': 0.11970935513169846, 'fraction_numerical': 0.015622161671207993, 'mean_word_length': 4.387475538160469, 'pattern_counts': {'":': 2, '<': 39, '<?xml version=': 0, '>': 38, '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': '31903', 'license': 'MIT', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1884, 'n_words': 721, 'path': 'notebooks/10-steps-to-DS/02_Python.ipynb', 'repo_name': 'alexzzlin/ml_training', 'size': '5507'}