<jupyter_start><jupyter_text>Python basicsPython is a dynamic and strongly-typed language. Here are some sorting algorithm implementations:<jupyter_code>data = [64, 1, 5, 7, 4, 3, 2, 1, 9, 0, 10, 43]
print(data)
def bubble_sort(seq):
    
    seq = seq.copy()
        
    n = len(seq)
    for i in range(n):
        for j in range(1, n - i):
            if seq[j] < seq[j - 1]:
                seq[j - 1], seq[j] = seq[j], seq[j - 1]

    return seq
bubble_sort(data)
def merge_sort(seq):
    
    seq = seq.copy()
    
    def merge(left, right):
        
        seq = []
        left_idx, right_idx = 0, 0

        while left_idx < len(left) and right_idx < len(right):
            if left[left_idx] <= right[right_idx]:
                seq.append(left[left_idx])
                left_idx += 1
            else:
                seq.append(right[right_idx])
                right_idx += 1

        for i in range(left_idx, len(left)):
            seq.append(left[i])

        for i in range(right_idx, len(right)):
            seq.append(right[i])

        return seq
    
    if len(seq) <= 1:
        return seq
    
    mid = len(seq) // 2
    left, right = merge_sort(seq[mid:]), merge_sort(seq[:mid])

    return merge(left, right)
merge_sort(data)
def quick_sort(seq):
    
    seq = seq.copy()
    
    if len(seq) <= 1:
        return seq
    
    pivot = seq[len(seq) // 2]
    left = [x for x in seq if x < pivot]
    middle = [x for x in seq if x == pivot]
    right = [x for x in seq if x > pivot]
    
    return quick_sort(left) + middle + quick_sort(right)
quick_sort(data)<jupyter_output><empty_output><jupyter_text>Python version<jupyter_code>import platform

print(platform.python_version())<jupyter_output><empty_output><jupyter_text>Data types Numbers<jupyter_code>x = 42
type(x)
x
x + 1
x - 1
x * 2
x ** 2
x += 1
x
x *= 2
x
y = 42.0
type(y)
print(y, y + 1, y * 2, y ** 2)<jupyter_output><empty_output><jupyter_text>Booleans<jupyter_code>t = True
f = False
type(t)
t and f
t or f
not t
t != f<jupyter_output><empty_output><jupyter_text>Strings<jupyter_code>text = "Hello"
print(text)
len(text)
text + ", FMI!"
x = 42

f"Hello {x}"
text.upper()<jupyter_output><empty_output><jupyter_text>Collections  Lists<jupyter_code>xs = [42, 12, 36]
len(xs)
xs[0]
xs[-1]
xs.append(1)
xs
xs[1:3]
xs[:-1]
for x in xs:
    print(x)
for idx, x in enumerate(xs):
    print(f"{idx}: {x}")
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
squares<jupyter_output><empty_output><jupyter_text>Dictionaries / maps<jupyter_code>fruit = dict(banana=10, apple=5, strawberry=3)
len(fruit)
fruit["banana"]
fruit["blueberry"] = 25
fruit
for item, qty in fruit.items():
    print(f"{qty} of {item}")
xs = [0, 1, 2, 3, 4]
nums = {x: x ** 2 for x in xs}

nums<jupyter_output><empty_output><jupyter_text>Sets<jupyter_code>fruits = {"banana", "apple"}
fruits
len(fruits)
fruits.add("strawberry")
fruits
for f in fruits:
    print(f)
xs = [0, 1, 2, 3, 4]
nums = {x ** 2 for x in xs}
nums<jupyter_output><empty_output><jupyter_text>Type system<jupyter_code>x = "hello"
type(x)
x + " " + "world"
x = 42
type(x)
x += "world"<jupyter_output><empty_output><jupyter_text>Functions<jupyter_code>def speak(text, shout=False):
    if(shout):
        print(text.upper())
    else:
        print(text)
speak("Hello")
speak("Hello", shout=True)<jupyter_output><empty_output><jupyter_text>ExerciseWrite a program which can compute the factorial of a given numbers. The results should be printed in a comma-separated sequence on a single line. Suppose the following input is supplied to the program:> 8Then, the output should be:> 40320<jupyter_code>x = 8
fact(x)<jupyter_output><empty_output><jupyter_text>Classes<jupyter_code>class Announcer:
    
    def __init__(self, text):
        self.text = text
        
    def speak(self, shout=False):
        if(shout):
            print(self.text.upper())
        else:
            print(self.text)
a = Announcer("Hello")
a.speak()
a.speak(shout=True)<jupyter_output><empty_output><jupyter_text>ExerciseDefine a class named Circle that can be constructed by a radius. The Circle class has a method which can compute its area.<jupyter_code>c = Circle(2)
print(c.area())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1384287742546143, 'fraction_numerical': 0.02106010411736867, 'mean_word_length': 3.274014155712841, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 56, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '266073', 'license': 'MIT', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1607, 'n_words': 409, 'path': 'labs/1.Python_basics.ipynb', 'repo_name': 'Data-Science-FMI/introduction-to-ml-2018', 'size': '4226'}