<jupyter_start><jupyter_text>Intro To Python=====In this notebook, we will explore basic Python:- data types, including dictionaries- functions - loops Please note that we are using Python 3.    (__NOT__ Python 2! Python 2 has some different functions and syntax)<jupyter_code># Let's make sure we are using Python 3
import sys
print(sys.version[0])<jupyter_output>3<jupyter_text>1. Basic Data Types: Numbers, Booleans, and Strings 1.1 Numbers<jupyter_code>a = 5
# Note: use the `type()` function to get the type of a variable
# Numbers can be integers ('int'), such as 3, 5 and 3049, or floats
# ('float'), such as 2.5, 3.1, and 2.34938493
print(type(a))
print(a)

list = [1,2]
print(type(list))<jupyter_output><class 'int'>
5
<class 'list'><jupyter_text>Mathematical Operators: +, -, *, /, **Mathematical operators allow you to perform math operations on numbers in Python.<jupyter_code>b = a + 1
print(b)
c = a - 1
print(c)
d = a * 2
print(d)
e = a / 2
print(e)
# Note: ** is the exponention operator
f = a ** 2
print(f)<jupyter_output>6
4
10
2.5
25<jupyter_text>Shorthand mathematical operators`a += 1` is shorthand for `a = a + 1`<jupyter_code>a += 1
print(a)
a *= 2
print(a)<jupyter_output>6
12<jupyter_text>1.2 Booleans & Logic Operators<jupyter_code>im_true = True
im_false = False

print(type(im_true))<jupyter_output><class 'bool'><jupyter_text>Equality operatorsEquality operators (== and !=) allow you to compare the values of variables on the left and right hand side.<jupyter_code>print(im_true == im_false) # Equality operator
print(im_true != im_false)<jupyter_output>False
True<jupyter_text>The `and` operator requires that the variables on each side of the operator are equal to true.<jupyter_code>print(im_true and im_false)<jupyter_output>False<jupyter_text>The `or` operator only requires the ONE of the variables on each side of the operator to be true.<jupyter_code>print(im_true or im_false)<jupyter_output>True<jupyter_text>1.3 StringsYou can use single or double quotes for strings.<jupyter_code>my_string = 'delta'
my_other_string = "analytics"
print(my_string, my_other_string)<jupyter_output>delta analytics<jupyter_text>String methodsConcatenating strings:<jupyter_code>another_string = 'hello, ' + my_string + " " + my_other_string
print(another_string)<jupyter_output>hello, delta analytics<jupyter_text>Get the length of the string:<jupyter_code>print(len(another_string))<jupyter_output>22<jupyter_text>2. Container Data Types 2.1 ListsA Python `list` stores multiple elements, which can be different types<jupyter_code>my_list = ['a', 'b', 'c', 3485]
print(my_list)<jupyter_output>['a', 'b', 'c', 3485]<jupyter_text>You can access an element in a list with the following syntax:Note: the first element in a list has an index of zero.<jupyter_code>print(my_list[2])
print(my_list[0])<jupyter_output>c
a<jupyter_text>Reassigning elements in a list:<jupyter_code>my_list[0] = 'delta'
print(my_list)<jupyter_output>['delta', 'b', 'c', 3485]<jupyter_text>Adding/removing elements from a list:<jupyter_code>my_list.append('hello')
print(my_list)

my_list.pop()
print(my_list)<jupyter_output>['delta', 'b', 'c', 3485, 'hello']
['delta', 'b', 'c', 3485]<jupyter_text>Accessing multiple elements in a list:<jupyter_code>print(my_list[0:2]) # Access elements in index 0, 1 and 2
print(my_list[2:]) # Access elements from index 2 to the end
print(my_list[:2]) # Access elements from the beginning to index 2<jupyter_output>['delta', 'b']
['c', 3485]
['delta', 'b']<jupyter_text>2.2 DictionariesDictionaries hold key/value pairs and are useful for storing information.<jupyter_code>my_dict = { 'key_one': 'value_one', 'name': 'mike' }<jupyter_output><empty_output><jupyter_text>Access a value from a dictionary by a key:<jupyter_code>print(my_dict['key_one'])
print(my_dict['name'])<jupyter_output>value_one
mike<jupyter_text>Looping over values of a dictionary:<jupyter_code>for key in my_dict:
    print("The key is " + key)
for key, value in my_dict.items():
    print("The key is " + key + ", and the value is " + value)<jupyter_output>The key is key_one, and the value is value_one
The key is name, and the value is mike<jupyter_text>2.3 SetsSets are similar to lists, but can only contain distinct values.<jupyter_code>my_set = {1, 2, 3, 'hello'}
print(my_set)<jupyter_output>{1, 2, 3, 'hello'}<jupyter_text>When defining a set with the same value present multiple times, only one element will be added to the set. For example:<jupyter_code>multiple = {1, 2, 2, 2, 2, 2, 3, 'hello'}
print(multiple) # This will return {1, 2, 3, 'hello'}<jupyter_output>{1, 2, 3, 'hello'}<jupyter_text>3. FunctionsA function is a block of reusable code that performs a certain action. Once you've defined a function, you can use it anywhere in your code! Defining a function:<jupyter_code>def am_i_happy(happiness_level):
    if happiness_level >= 10:
        return "You're very happy."
    elif happiness_level >= 5:
        return "You're happy."
    else:
        return "You're not happy."<jupyter_output><empty_output><jupyter_text>Calling a function:<jupyter_code>print(am_i_happy(0))
print(am_i_happy(5))<jupyter_output>You're happy.<jupyter_text>4. Control Flow 4.1 If/Else If/Else<jupyter_code>sleepy = True
hungry = False

if sleepy and hungry:
    print("Eat a snack and take a nap.")
elif sleepy and not hungry:
    print("Take a nap")
elif hungry and not sleepy:
    print("Eat a snack")
else:
    print("Go on with your day")<jupyter_output>Take a nap<jupyter_text>4.2 Loops 4.2.1 'while' loops<jupyter_code>counter = 0
while (counter < 10):
    print("You have counted to", counter)
    counter = counter + 1 # Increment the counter
    
print("You're finished counting")<jupyter_output>You have counted to 0
You have counted to 1
You have counted to 2
You have counted to 3
You have counted to 4
You have counted to 5
You have counted to 6
You have counted to 7
You have counted to 8
You have counted to 9
You're finished counting<jupyter_text>4.2.2 'for' loopsLoop over a list:<jupyter_code>cool_animals = ['cat', 'dog', 'lion', 'bear']

for animal in cool_animals:
    print(animal + "s are cool")<jupyter_output>cats are cool
dogs are cool
lions are cool
bears are cool<jupyter_text>Loop over a dict:<jupyter_code>animal_sounds = {
    'dog': 'bark',
    'cat': 'meow',
    'pig': 'oink'
}

for animal, sound in animal_sounds.items():
    print("The " + animal + " says " + sound + "!")<jupyter_output>The dog says bark!
The cat says meow!
The pig says oink!
{'fraction_non_alphanumeric': 0.11789181692094314, 'fraction_numerical': 0.023270149483741717, 'mean_word_length': 5.838777660695469, 'pattern_counts': {'":': 0, '<': 88, '<?xml version=': 0, '>': 89, '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': '27', 'dir': 'jupyter-structured-clean-dedup', 'id': '20451', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2307, 'n_words': 712, 'path': 'module_1_introduction/1_1 intro_to_python.ipynb', 'repo_name': 'iElasri/machine_learning_for_good', 'size': '6489'}