<jupyter_start><jupyter_text>Lighthouse Labs W01D2 Programming in Python for DSInstructor: <NAME>  September 14, 2021 **Agenda:*** Housekeeping Guidelines* Introduction to Python Programming Language    * History    * Syntax    * Basic Data Types* Python for Data Science* Popular IDEs  Housekeeping- Mics muted (to avoid background noises)- Cameras on- You can ask; unmute yourself if you want to ask something.    - You can ask me questions. I can ask you questions too. :)    - We will take a 10 min break after 45/50 minutes of class. [Download this notebook](https://downgit.github.io//home?url=https://github.com/sedv8808/LighthouseLabs/blob/main/W01D2/W01D2_PPDS.ipynb)  What is Python? - Python is a widely used general-purpose, high-level programming language. - Initially designed by <NAME> in 1991 and developed by Python Software Foundation. - Developed to allow programmers express concepts in fewer lines of code.- Python is a General Purpose object-oriented programming language, which means that it can model real-world entities. It is also dynamically-typed because it carries out type-checking at runtime.- The distinctive feature of Python is that it is an interpreted language (no need to compile).We will be working with Python 3, which was released in December 2008.  ![image](https://d2h0cx97tjks2p.cloudfront.net/blogs/wp-content/uploads/sites/2/2017/12/Features-of-python-01.jpg)  Python HistoryPython was first introduced by <NAME> in 1991 at the National Research Institute for Mathematics and Computer Science, Netherlands.[source:https://www.journaldev.com/34415/history-of-python-programming-language] Python Versions* In 1994, Python 1.0 was released with new features like lambda, map, filter, and reduce.* Python 2.0 added new features such as list comprehensions, garbage collection systems.* On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to rectify the fundamental flaw of the      language.[source:https://www.javatpoint.com/python-history]  Fun Facts* Guido van Rossum named it after the comedy show [Monty Python's Flying Circus](https://www.netflix.com/ca/title/70213238).* Python has become the most popular coding language in the world. This makes a career in Python a great choice.* Python has just turned 30, but it still has that X factor:     * Google users have searched for Python much more than they have searched for <NAME>, <NAME>, <NAME> etc.  Why Python for Data Science ?* Simple programming language to pick up, from a syntax point of view. * Python also has an active community with a vast selection of libraries and resources.* Professionals working with data science applications don’t want to be bogged down with complicated programming requirements. They want to use programming languages like Python and Ruby to perform tasks hassle-free.[source:https://www.cbtnuggets.com/blog/technology/data/why-data-scientists-love-python]  What is Jupyter?- For this Bootcamp, we will mostly be using Python via [Jupyter](https://jupyter.org/index.html)- You can think of Python like a car’s engine, while Jupyter is like a car’s dashboard  - Python is the programming language that runs computations  - Jupyter is an integrated development environment (IDE) that provides an interface by adding convenient features and tools![engine](img/python_jupyter.png)  Jupyter Notebooks- Code, plots, formatted text, equations, etc. in a single document- Run Python code interactively- Also supports R, Julia, Perl, and over 100 other languages (and counting!) - Notebooks are great for exploration and for documenting your workflow- Many options for sharing notebooks in human readable format:  - Share online with [nbviewer.jupyter.org](http://nbviewer.jupyter.org/)  - If you use Github, any notebooks you upload are automatically rendered on the site  - Convert to HTML, PDF, etc. with [nbconvert](https://nbconvert.readthedocs.io/en/latest/)  Working with NotebooksA notebook consists of a series of "cells":- **Code cells**: execute snippets of code and display the output- **Markdown cells**: formatted text, equations, images, and moreBy default, a new cell is always a code cell.  Python Data Science EcosystemThe Python libraries for data science are developed and maintained by external "3rd party" development teams- Python core + 3rd party libraries = **ecosystem** - To install and manage 3rd party libraries, you need to use a package manager such as `conda` (which comes with Anaconda/Miniconda) Some of the libraries in the Python data science ecosystem:![ecosystem_big](img/ecosystem_big.png)From [The Unexpected Effectiveness of Python in Science](https://speakerdeck.com/jakevdp/the-unexpected-effectiveness-of-python-in-science) (<NAME>) During the Bootcamp, we will be working with `pandas`, `numPy`, `seaborn`, `matplotlib`, `plotly`, `sklearn` and `keras` libraries.  2.Basic Syntax and Data Types  Built in Data Types - Values and ObjectsWe will been working with values, which are pieces of data that a computer program works with, such as a number or text.We will assign a lot of these values to objects (variables) with the assignment operator `=`.These values will always belong to a data type Here are some data types built-in to the Python language:* Integers - `int`* Floating-point numbers - `float` - NaN belongs to this group* Strings - `str`* Booleans - `bool` - two values: True and False.* Lists - `list`* Tuples - `tuple`* Sets - `set`* Dictionaries - `dict`<jupyter_code>length = 46
string = "Mulan's dragon's name is Mushu"
type(length)
type(string)<jupyter_output><empty_output><jupyter_text>String "Verbs"There are several methods/verbs to transform strings or extract information from them.<jupyter_code>len(string)
string.lower()<jupyter_output><empty_output><jupyter_text>Numerical Data Types and Casting<jupyter_code>age = 6 
type(age)
age = 6.0 
type(age)<jupyter_output><empty_output><jupyter_text>**IMPORTANT**Something that you will notice in Pandas dataframes are NaN values.This stands for **Not A Number**, and it is a special value used to represent missing data in pandas.It is considered a **float** numeric value. **Casting**- `int` to `float`:<jupyter_code>float(4)<jupyter_output><empty_output><jupyter_text>- `int` to `str`<jupyter_code>length = 40
length_string = str(length)
print(length_string)
type(length_string)<jupyter_output>40<jupyter_text>- `float` to `int`<jupyter_code>int(4.99)<jupyter_output><empty_output><jupyter_text>Python data structures: Lists, Tuples, Dictionaries and Sets  ListsSimilarly to how a string is a sequence of characters in order, a   list is a sequence of elements with a particular order.Lists can be identified by their square brackets.The elements in a list can be any objects, and they don’t all need to have the same type.<jupyter_code>string_list = string.split()
string_list<jupyter_output><empty_output><jupyter_text>By using the .split() verb, we can get a list from a string.  SlicingWe can slice lists by elements.We slice with `[]`; the start is inclusive, and the end is exclusive.So string_list[1:3] fetches elements 1 and 2, but not 3.In other words, it gets the 2nd and 3rd elements in the list.[ x, y ) <- range<jupyter_code>for i in string_list:
    print(i)
string_list[1:3]<jupyter_output><empty_output><jupyter_text>List ComprehensionWe can manipulate a whole list using list comprehension:<jupyter_code>new_list = [0,3,4,5,7,8,3,3,7,9,5,5]

other_new_list = []
for i in new_list:
    x = i+5
    other_new_list.append(x)
    
other_new_list
[x+5 for x in new_list]<jupyter_output><empty_output><jupyter_text>TuplesTuples are a data structure very similar to lists but with two main differences:They are represented with parentheses instead of square brackets, andThey are immutable<jupyter_code>my_tuple = ('I', None,  'do', 1, False)
my_tuple[1] = 'I'<jupyter_output><empty_output><jupyter_text>SetsData structure that:- are unordered, meaning there is no element 0 and element 1, and- The values contained are unique - meaning there are no duplicate entries.- Sets are made with curly brackets.<jupyter_code>my_set = {2, 1.0, 'apple', 1.0, 'Apple'}
my_set<jupyter_output><empty_output><jupyter_text>DictionariesDictionaries are unordered pairs of keys and corresponding values<jupyter_code>account_details = {'Name':'<NAME>','Account_Type':'Checking','Branch':'North York','Age':32}
account_details
account_details['Name']
account_details.values()
account_details.items()<jupyter_output><empty_output><jupyter_text>Why do we need dictionaries if we have lists?Dictionaries can have labels or keys associated with a value whereas a list only has an index. In a list the order matters, in a dictionary, the order does not matter  Summary|Data Structure	| Preserves order | Mutable | Symbol| Can contain duplicates | Can be sliced ||---------|------|------|------|------|------||str	|✓	|☓	|''  , ""|	✓|✓||list	|✓	|✓	|[] |	✓|✓||tuple	|✓	|☓	|() |	✓|✓||set	|☓	|✓	|{} |	☓|☓||dict  |✓	|✓	|{} | 	☓| ☓|  Errors and Exceptions Handling<jupyter_code>def div_func(a,b):
    '''Here write documentation'''
    result = a/b
    return result
div_func(5,2.0)
div_func(5,0.0)
def div_func(a,b):
    try:
        result = a/b
    except ZeroDivisionError:
        print('b is zero and division is not possible')
        return
    return result
div_func(5,-5)
div_func(5,0.0)
def count_letter_in_word(input_word='gulp'):
    return len(input_word)
count_letter_in_word(5)
def count_letter_in_word(input_word='gulp'):
    if type(input_word)!=str:
        raise TypeError('incorrect input type. Please provide a string input')
    return len(input_word)
count_letter_in_word('horse')
count_letter_in_word(5)
def count_letter_in_word_new(input_word='gulp'):
    result=0
    try:
        result=len(input_word)
    except Exception as e:
        print(e)
    return result
count_letter_in_word_new(7)<jupyter_output>object of type 'int' has no len()<jupyter_text>Handling Date and Time in Python **Get current date time**<jupyter_code># import datetime class from datetime module
from datetime import datetime

# get current date
datetime_object = datetime.now()
print(datetime_object)
print('Type :- ',type(datetime_object))
my_string = '2018-11-3'

# Create date object in given time format yyyy-mm-dd
my_date = datetime.strptime(my_string, "%Y-%m-%d")

print(my_date)
print('Type: ',type(my_date))
print('Month: ', my_date.month) # To Get month from date
print('Year: ', my_date.year) # To Get month from year<jupyter_output>Month:  11
Year:  2018<jupyter_text>Measuring Time Span with Timedelta Objects<jupyter_code>#import datetime
from datetime import datetime, timedelta
# get current time
now = datetime.now()
print ("Today's date: ", str(now))
#add 15 days to current date
future_date_after_15days = now + timedelta(days = 15)
print('Date after 15 days: ', future_date_after_15days)

#subtract 2 weeks from current date
two_weeks_ago = now - timedelta(weeks = 2)
print('Date two weeks ago: ', two_weeks_ago)
print('two_weeks_ago object type: ', type(two_weeks_ago))<jupyter_output>Date after 15 days:  2021-07-07 11:19:32.018820
Date two weeks ago:  2021-06-08 11:19:32.018820
two_weeks_ago object type:  <class 'datetime.datetime'><jupyter_text>Find the Difference Between Two Dates and Times<jupyter_code># import datetime
from datetime import date
# Create two dates
date1 = date(2008, 8, 18)
date2 = date(2008, 8, 10)

# Difference between two dates
delta = date2 - date1
print("Difference: ", delta.days)
print('delta object type: ', type(delta))<jupyter_output>Difference:  -8
delta object type:  <class 'datetime.timedelta'><jupyter_text>**Accessing certain date attributes**<jupyter_code>date1.month<jupyter_output><empty_output><jupyter_text>Formatting Dates: More on strftime() and strptime() strptime() is the method we used before, and you’ll recall that it can turn a date and time that’s formatted as a text string into a datetime object, in the following format:time.strptime(string, format)Note that it takes two arguments:string − the time in string format that we want to convertformat − the specific formatting of the time in the string, so that strptime() can parse it correctly<jupyter_code># import datetime
from datetime import datetime
date_string = "1 August, 2019"

# format date
date_object = datetime.strptime(date_string, "%d %B, %Y")

print("date_object: ", date_object)<jupyter_output>date_object:  2019-08-01 00:00:00<jupyter_text>Below is a list of directives used in strftime() and their meanings | Directive | Meaning||-----------|--------|| %a |Weekday abb. Sun, Mon, …, Sat (en_US)|| %A | Weekday Full Sunday,Monday (en_US)|| %w | weekday as decimal 0,1,2,3.. || %d | day of month as decimal 0,1,2,...30 ||| %b | Month Jan, Feb, …, Dec (en_US) || %B | Month January,February... || %m | Month as a zero-padded decimal || %y | Year without century as zero padded decimal 00,01,..,99 || %Y | Year with century 1970,1980 etc. || %H | our (24-hour clock) as a zero-padded decimal number.00, 01, …, 23 || %I | Hour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12 || %p | Am,PM || %M | Minute 00,01..,59 || %S | Second 00,01..,59 |  IDEs for Python and DS ![](https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python+IDEs/patolino-pernalonga-python-ide2.gif) Finding an IDE that works for you is important. If you are already familiar with an IDE, use it. Some previous LHL students have reported being happy with Visual Studio and others are happy with Jupyter Notebooks.  Python challenge  Cooking Challenge Alberto is making spaghetti tonight and he needs to make sure that if he doesn't have enough of the ingredients in his pantry, he adds them to his shopping list.- For each item in the recipe, check if the ingredient is in Alberto's pantry.- If the recipe ingredient is in the pantry, check if the recipe requires more of the ingredient than what Alberto has in storage. If so, add the name and the quantity he needs to purchase as key-value pairs in the dictionary shopping_list.- If the recipe item is not in the pantry, add the ingredient and the quantity as **key-value pairs in the dictionary** shopping_list.<jupyter_code>pantry = {'pasta': 3, 'garlic': 4,'sauce': 2,
          'basil': 2, 'salt': 3, 'olive oil': 3,
          'rice': 3, 'bread': 3, 'peanut butter': 1,
          'flour': 1, 'eggs': 1, 'onions': 1, 'mushrooms': 3,
          'broccoli': 2, 'butter': 2,'pickles': 6, 'milk': 2,
          'chia seeds': 5}

meal_recipe = {'pasta': 2, 'garlic': 2, 'sauce': 3,
          'basil': 4, 'salt': 1, 'pepper': 2,
          'olive oil': 2, 'onions': 2, 'mushrooms': 6}

# [ dishes[x] for x in crucial if dishes.has_key(x) ]

shopping_list = dict()

for key in meal_recipe:
    if key in pantry.keys():
        we_need = meal_recipe[key] - pantry[key]
        if we_need > 0:
            print("{} of {} needed!".format(we_need, key))
            shopping_list[key]=we_need
    if key not in pantry.keys():
        shopping_list[key]=we_need*(-1)
print(shopping_list)<jupyter_output>1 of sauce needed!
2 of basil needed!
1 of onions needed!
3 of mushrooms needed!
{'sauce': 1, 'basil': 2, 'pepper': 2, 'onions': 1, 'mushrooms': 3}<jupyter_text>Additional Resources[Python Documentation](https://docs.python.org/3/contents.html)  [O'Reilly Python DS Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/)  [UBC Programming in Python for DS](https://prog-learn.mds.ubc.ca/en/)  Coading Challenge II  Goal:Make a two-player Rock-Paper-Scissors game.Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game  Rules:* Rock beats scissors* Scissors beats paper* Paper beats rock Concepts Used* Loops( for,while etc.)* if else Function Template* def compare(user1_answer='rock',user2_answer='paper') Error Handling* If the user_answer input is anything other than 'rock','paper' or 'scissors' return a string stating 'invalid input' Libraries needed* sys - to accept user input.  Example on how to use the library sys for input is below<jupyter_code>import sys

age = input("What's your age?")
days_old = float(age)*365
print('you are '+ str(days_old)+' days old.')<jupyter_output>What's your age?10
you are 3650.0 days old.<jupyter_text>Basic if else<jupyter_code>if int(age) > 30 and int(age)<=100:
    print('you are an adult')
elif int(age) < 30 and int(age) >=15:
    print('you are a young adult')
elif int(age) >=0 and int(age) <15:
    print('you are a kid')
else:
    print(' I do not understand the input!')<jupyter_output>you are a kid<jupyter_text>Code Solution for challenge<jupyter_code>user1 = input("What's your name?")
user2 = input("And your name?")
user1_answer = input("%s, do yo want to choose rock, paper or scissors?" % user1)
user2_answer = input("%s, do you want to choose rock, paper or scissors?" % user2)
def compare(u1, u2):
    if ___ == _____:
        return("It's a tie!")
    ____ u1 == 'rock':
        ____ u2 == 'scissors':
            return("Rock wins!")
        ____:
            return("Paper wins!")
    ____ u1 == 'scissors':
        ____ u2 == 'paper':
            return("Scissors win!")
        ____:
            return("Rock wins!")
    ____ u1 == 'paper':
        ____ u2 == 'rock':
            return("Paper wins!")
        ____:
            return("Scissors win!")
    ____:
        return("Invalid input! You have not entered rock, paper or scissors, try again.")
        sys.exit()
print(compare(user1_answer, user2_answer))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10885167464114832, 'fraction_numerical': 0.02221462747778537, 'mean_word_length': 4.638086062941555, 'pattern_counts': {'":': 1, '<': 94, '<?xml version=': 0, '>': 94, 'https://': 14, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '74421', 'license': 'MIT', 'n_tokens_mistral': 5765, 'n_tokens_neox': 5350, 'n_words': 2035, 'path': 'w1/d2-day_2/W01D2_PPDS.ipynb', 'repo_name': 'bmskarate/lighthouseMain', 'size': '17620'}