🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

DS242-Full Summary-3.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

‫‪DS 242‬‬ ‫تحذير هام‬ ‫هذا الملف للمشتركين في موسى اكاديمي فقط‬ ‫وغير مسموح بنشره او التعامل به لغير المشتركين‬ ‫ونشكركم لحفظكم االمانة وحقوق الملكية‬ ‫فريق عمل موسى اكاديمي‬ ‫‪Moussa Academy‬‬ ‫‪00201007153601‬‬...

‫‪DS 242‬‬ ‫تحذير هام‬ ‫هذا الملف للمشتركين في موسى اكاديمي فقط‬ ‫وغير مسموح بنشره او التعامل به لغير المشتركين‬ ‫ونشكركم لحفظكم االمانة وحقوق الملكية‬ ‫فريق عمل موسى اكاديمي‬ ‫‪Moussa Academy‬‬ ‫‪00201007153601‬‬ ‫‪WWW.MOUSSAACADEMY.COM‬‬ Week 2: Understanding the Five Areas of Data Science DS242 Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. About Data Science..................................................................................................................................... 2 II. Working with Big Data................................................................................................................................. 2 III. The Five-Step Process of Data Science.................................................................................................... 3 IV. Data Science vs Data Analytics................................................................................................................ 4 V. Definitions................................................................................................................................................... 5 1|Page www.MoussaAcademy.com 00201007153601 I. About Data Science  is the domain of study that deals with vast volumes of data using modern tools and Data Science techniques  Find Unseen patterns in data. Uses of Data  Derive meaningful information from data. Science  Make business decisions Examples of  Search Engine Suggestions Data Science  Medical Diagnoses  Python + tools and libraries Tools Used II. Working with Big Data What is Big  complex datasets that are large for conventional data-processing software to Data? handle. The Three Three V’s Concepts of Big Data in  Volume, Variety, and Velocity Industry 1. Volume  refers to the size of the dataset. Data can be in Different Forms  Photos and images 2. Variety  Measurements  Voice Data  how fast the data is changing and how fast it is being added to the data piles. 3. Velocity  Low Velocity: not changing at all  High velocity: change is fast. Why use the  The Three V’s concept is used to describe the data and give you an idea of the data parameters. three V’s Concept? Data the process of gaining insight in data. Analytics 2|Page www.MoussaAcademy.com 00201007153601 III. The Five-Step Process of Data Science STEPS OF CAPTURING DATA 1) Capture the 1. Define your goals Data 2. Collect data from many sources (such as company records, public databases, and so on) 3. Decide what to include in the project  Clean and process (outliers, duplicates, missing entries, inconsistent) data 2) Process Data Be careful not to remove IMPORTANT data!  Finding correlations between data types  Asking the right question is the key in data analysis. 3) Analyzing Data Helpers for professional data analysis  Linear and logistic regression  Naïve Bayes.  presenting the analysis results in different formats. 4) Communicating Two Major Python Packages to Communicate Results Most People Understand the data the results  R – Language better when they see it in graphical format  Matplotlib: used to display data graphics  Archive and document information in order to restart the project quickly or reuse parts of the project for a new one. What information to save? 5) Maintain the Data 1. Data and sources 2. Models used to modify data 3. Queries and results 3|Page www.MoussaAcademy.com 00201007153601 IV. Data Science vs Data Analytics Data Science Data Analytics  the process of working out  the process of gaining insight insights from large datasets of unstructured in data. data.  Uses Predictive analytics, statistics, and  Use and create statistical analysis for existing machine learning to achieve insights on the sets of data to achieve insights on that data. data. STEPS INCLUDED STEPS INCLUDED Capture data Analyze data Process data Communicate results Analyze data Maintain the data Communicate results Maintain the data Data Science Data Analaytics 4|Page www.MoussaAcademy.com 00201007153601 V. Definitions Term Definition Is the domain of study that deals with vast volumes of data using modern tools and Data Science techniques Big Data Complex datasets that are large for conventional data-processing software to handle. Volume Refers to the size of the dataset. Velocity How fast the data is changing and how fast it is being added to the data piles. Data Analytics The process of gaining insight in data. 5|Page www.MoussaAcademy.com 00201007153601 Week 3: Speeding Along with Lists and Tuples DS242 Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Basics of Lists............................................................................................................................................... 2 II. Using Lists.................................................................................................................................................... 3 III. Adding and Removing Items.................................................................................................................... 4 IV. More Specific Function to Use................................................................................................................ 7 V. All together.................................................................................................................................................. 9 VI. Definitions............................................................................................................................................... 9 1|Page www.MoussaAcademy.com 00201007153601 Data items, separated by commas, inside square I. Basics of Lists brackets  the simplest data collection in python. Lists List of Numbers 1] List of Strings: You can Use Single or Double Quotes 2] List of Numbers; Define a List Don’t Use Quotes with Numbers 3] Mixed List  use Print (List Name): Your List's Name  Printing a list is the same as printing a variable  Example Code Print / Display a List Name of the List we defined  Output  Index of list starts from ZERO Index 0 Index 1 Index 2 Index 3  Format: Access and Print List List name [x] Item by Position  Example Code to Print Third Item from List: Output 2|Page www.MoussaAcademy.com 00201007153601 II. Using Lists  We use the FOR loop  Format: Variable X will point to items in For any variable in list name: list Loop Through List  Example code: Output Variable to save search result Searching for Check if there Anita are item in list or not Search Result is Boolean meaning it is True or False  Format: Len (List Name)  Example Code Output Get Length of a List  Index is not Length Print length of List  Length = N named Students  Index = N-1 N starts from 1 N=1 N=2 N=3 N=4 N=5 Index = 0 Index = 1 Index = 2 Index = 3 Index = 4 3|Page www.MoussaAcademy.com 00201007153601 III. Adding and Removing Items This Method add items to the end of list 1. Using append () Method:  Format: List Name. append (variable or string or number) Two Items Added to end of the list Adding Items to List 2. Using Insert () Method:  Format: List name. insert (position, item) Add an item to first index in list Output  You can change an item in list using the = operator  Format: The new value The item you want Change Item to replace in a list  Example Code: The Fourth Item is changed from "Anita " to "Hobart" 4|Page www.MoussaAcademy.com 00201007153601  Use the Extend Method to combine Lists  Format: List1 will be the combined List List1.extend (List2)  Example Code: Combining Lists List1 will contain names of List1 + List2 1. Using Remove () Method:  You can remove any item from list using this method  This method removes only the first occurrence of the item.  Format: List name. remove(item)  Example code: Only the first occurrence of letter "C" will be removed from list Remove List Items 2. Using Pop () Method:  You can remove any item using its position/index  Format: if pop () method doesn’t take any arguments List name. Pop (index of item) then it will remove the last item in the list  Example code: Removes First item which is letter "A" Removes Last item which is letter "G" 5|Page www.MoussaAcademy.com 00201007153601  You can save the removed item in a variable: Save removed item to variable named first_removed 3. Using Del () Method:  You can remove any item from list using this method  Format: del list name [index of item]  Example Code: Removing the THIRD item from the list  This method clears the content but does not delete the list itself  Format: List name. Clear ()  Example code: Clearing content of a list 6|Page www.MoussaAcademy.com 00201007153601 IV. More Specific Function to Use  Format: List name. Count (item or variable)  Example Code: Here B is a letter not a variable Counting how many times an item Here the item we appears in a are looking for is list stored in a variable   Format: List name. index (item) Index search  Example Code: result is saved in this variable The error generated here because letter F is not in the list Finding Index of an Item Here we used IF condition to prevent error from happening 7|Page www.MoussaAcademy.com 00201007153601 Sorting a List  The sort () method, arrange item in ascending order or A to Z or earliest to latest for dates  Format: List name. sort ()  Example code This is not the same as Sort ()  If you want to sort in descending order, then you can use this format: List name. sort (reverse = True)  The Reverse () method is used to reverse the order of items in a list NOT reverse sorting.  Example code: Reversing a List  This method is used to make a copy of a list and save the copy in another list  Format: New list = your list. Copy ()  Example code: Here we save a copy of the list names "names" in a Copying a new list named List "backward_names" Here we reversed the order of items in the new list using the reverse () method 8|Page www.MoussaAcademy.com 00201007153601 V. All together VI. Definitions Term Definition Lists The simplest data collection in python. 9|Page www.MoussaAcademy.com 00201007153601 Week 4: Speeding Along with Lists and Tuples (Part DS242 2) Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Tuples.......................................................................................................................................................... 2 II. Sets.............................................................................................................................................................. 3 III. Set vs Tuple vs List................................................................................................................................... 4 IV. Definitions............................................................................................................................................... 5 1|Page www.MoussaAcademy.com 00201007153601 I. Tuples  Is a list but can't be changed after its defined Basics  You can not use the functions of lists with tuples.  Does not allow duplicate values. 1. Use len () method to get the length of the tuple: Print the length of tuple 2. Use count () method to see how many times an item appears: Specific Methods Print number of times the number 4.95 appears in 3. Use in () to whether item exist or not: tuple Print True or False  use For loop: Display each item with a leading dollar sign and two digits for the Pennie Looping through tuples  You can't change the value of item using the following syntax: You will get a TypeError Type Error  This error means you can not use the = operator  The tuples are immutable (their content cannot be changed) When to use  If you want to show data to users without giving them any means to change any Tuples? information 2|Page www.MoussaAcademy.com 00201007153601 II. Sets  A mean of organizing data in python  Items in a set have no specific order Basics  Items don’t get index number in sets  You can't change an item already in set.  You can't change order of items in a set.  You can't use sort () or reverse () to change order of a set. 1. Use len () method to get the length of the set 2. Use in () to whether item exist or not 3. Use add () to add single new item to set: Adds 11.23 to set Specific Methods If you add a number multiple times, the set will contain only one copy of the number 4. Use update () to add multiple items to set: Numbers must be defined 1. Use copy () method to get copy a set: as a list in square brackets When you print the copy of the list, items won't be in the Copy a Set same order as original set 3|Page www.MoussaAcademy.com 00201007153601 In the code below the floating point is set to be 2 number after decimal point Formatting.2f means print only two Output number after decimal point III. Set vs Tuple vs List Point of Comparison Set Tuple List 1. Definition Syntax  {1,2,3}  (1,2,3)  [1,2,3] 2. Data types  Heterogenous  Heterogenous  Heterogenous 3. Ordering  Unordered  Ordered  Ordered  Changeable  Unchangeable  Changeable 4. Changeability/Mutability (add/remove) 5. Indexing  Not Indexed  Indexed (by position)  Indexed (by position)  No duplicates  Allows duplicates  Allows duplicates 6. Duplicates allowed 4|Page www.MoussaAcademy.com 00201007153601 IV. Definitions Term Definition Tuple Is a list but can't be changed after its defined Sets A mean of organizing data in python 5|Page www.MoussaAcademy.com 00201007153601 Week 5: Cruising Massive Data with Dictionaries (Part DS242 1) Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Understanding Data Dictionaries................................................................................................................ 2 II. Creating Data Dictionary............................................................................................................................. 2 III. Specific Methods on Dictionaries............................................................................................................ 3 IV. Loop through a Dictionary....................................................................................................................... 4 1|Page www.MoussaAcademy.com 00201007153601 I. Understanding Data Dictionaries Dictionary is similar to List, except each item has unique key  Other name: Associative arrays  Like Lists but each item is identified by key not position or index. Basics  KEY can be a string or number. Enclosed in quotation marks  KEY is unique to each item.  Value associated with a key can be: number, string, list, tuple, dictionary Unique Key Associated Value Real Example on Data Dictionary II. Creating Data Dictionary  Curled braces are used to define data dictionary  Colon is used to separate a key from the associated value  Comma is used to separate the key:value pairs from each other  Syntax:  More Readable Syntax: Each Key:value pair is in Creating a Data separate line Dictionary  Example on creating a data dictionary  2|Page www.MoussaAcademy.com 00201007153601 III. Specific Methods on Dictionaries 1. Print a copy of the dictionary:  Syntax: Print (dictionary name)  Example: 1. Accessing DATA 2. Print specific value:  Syntax: Print (Dictionary name [key]) Dictionary name = people  Example Key = zmin  Syntax: len (dictionary name) 2. Getting length of Saves the length of the a dictionary  Example: people dictionary in a variable named length  Use the word in  Syntax: Return True or False 3. Check existence of Key in dictionary name a key  Example: Check whether a name called hajackson is in the dictionary or not  Syntax: 4. Retrieve data Dictionary name. get(key) The get () function returns using get ()  Example: None if the key doesn’t exist function 3|Page www.MoussaAcademy.com 00201007153601  Syntax: Dictionary name [key] = new value 5. Change value of  Example: a key If the key doesn’t exist then it  Syntax: will be added as a new Dictionary name. update ({key, value}) key:value pair to the 6. Add or Change dictionary dictionary data  Example:  Syntax: print (dictionary name. Keys ()) 7. Print keys of the dictionary  Example: Print keys of a dictionary named people IV. Loop through a Dictionary  Use the FOR loop  Syntax: 1. Loop through a for any variable in dictionary name dictionary  Example: Print the value associated with each key in the dictionary  Syntax: for any variable in dictionary name. values () 2. Loop through values of a  Example: Print the value dictionary associated with each key in the dictionary   Syntax: 3. Loop through for any variable in dictionary name. items () values and keys  Example: Print each key:value pair of a dictionary in the dictionary 4|Page www.MoussaAcademy.com 00201007153601 Week 6: Cruising Massive Data with Dictionaries (Part DS242 2) Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Deleting Dictionary Items............................................................................................................................ 2 II. Using multi-key dictionaries........................................................................................................................ 3 III. Using the mysterious from keys and set default methods...................................................................... 5 1|Page www.MoussaAcademy.com 00201007153601 I. Deleting Dictionary Items  Remove items based on its key  Syntax:  del dictionary_name[key]  Example: 1] The Del () Method  Note: If you forget to include a specific key with the del function and specify only the dictionary name, the entire dictionary is deleted, even its name  Used to remove all key pairs without deleting the entire dictionary  Syntax:  Dictionary_name. Clear ()  Example: 2] The Clear () Method 2|Page www.MoussaAcademy.com 00201007153601  You can store the removed key in a variable  Syntax:  Dictionary_name. Pop (key)  Example 3] The Pop () Method II. Using multi-key dictionaries 1. One key - one value ‫رﺑﻂ ﻣﺎ ﺑﯿﻦ ﻗﯿﻤﺔ وﻣﻔﺘﺎح‬  Example: 2. One key - many values ‫وﺻﻒ ﻛﺎﻣﻞ ﻟﻜﯿﺎن ﻣﺎ واﺣﺪ‬  Phonebook = {name: [number, city]} Can be a list, tuple, or set  OR {name: {number: city: :...}} Types of Data  Example: Dictionary 3. Many values - one key ‫رﺑﻂ ﻣﺎ ﺑﯿﻦ ﻗﯿﻤﺔ وﻋﺪة ﻣﻔﺎﺗﯿﺢ‬  {(John, Doe) : ID}  Example: 3|Page www.MoussaAcademy.com 00201007153601 4. Many values - Many keys ‫وﻛﺎﻣﻞ ﻟﻌﺪد ﻣﻦ اﻟﻜﯿﺎﻧﺎت ﻣﻦ ﻧﻔﺲ اﻟﻨﻮع‬  {(John, Doe) : [name, city]}  Example:  Syntax:  Dictionary_name[key]  Example: Displaying Dictionary Data Descriptive text used to show what is being printed Getting Fancy by Descriptive text 4|Page www.MoussaAcademy.com 00201007153601 III. Using the mysterious from keys and set default methods  It allows you to initialize a dictionary without having to specify values.  The values will be set to None by default. Can be a list  Syntax: Optional  New_dictionary_name= dict. fromkeys (iterable [, value])  Example: 1] Fromkeys () Method  Used to set a default value for a key if that key does not exist  Used to avoid a Key Error if trying to access a nonexistent key  Syntax: 2] Set default () Method  Example: 5|Page www.MoussaAcademy.com 00201007153601 Week 7: Wrangling Bigger DS242 Chunks of Code Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Creating your own function and including a comment in a function.......................................................... 2 II. Pass Information to a Function................................................................................................................... 3 III. Passing Multiple Parameters,Iterables , Scope of Variables................................................................... 4 IV. Returning values from a function............................................................................................................ 6 V. Understanding anonymous (lambda) functions.......................................................................................... 6 1|Page www.MoussaAcademy.com 00201007153601 I. Creating your own function and including a comment in a function  Provide a way to compartmentalize your code into small tasks than can be called from multiple places in an app What is a Function?  Using functions makes you don’t repeat block of codes  Functions can be called like built-in function like Print ()  Syntax: Def = definition Def function name():  Example: This function doesn’t do anything Creating a  Operating function: This function prints Function "Hello" Indentation is a must to  Note: make sure the code is inside the function Functions are not executed until they are called  Syntax:  Function name ()  Example: Calling a function Here the function hello is called  You can add a # to start writing a comment in python  Docstring is a text enclosed in triple quotation marks used to describe any code  Example code: A comment Commenting a Docstring to describe Function the function Calling the function  Note: Comments don’t affect the code 2|Page www.MoussaAcademy.com 00201007153601 II. Pass Information to a Function  Syntax: Def function name(paramater name)  Notes: Parameter name must start with a letter or underscore followed by a letter, underscore, number Parameters of a functions are local only to that function Parameters passed to a function are called Local variables Parameters influence stay inside the fnction this is called Local Scope Variables defined inside a function doesn’t exist when the function stops running Passing Parameters Kwargs is a short for keyword arguments and its another way to pass data to a function  Example: Parameter named "name" is passed to the function The function prints the name passed to it Not passing the parameter will create a type error "name" is a placeholder for whatever is being passed Function This function is returning Printing a value the value passed to it Passing Data in the form of a variable  You can make passing paramaters optional by setting default values for those parameters  Syntax: Default Default username is Arguments nobody 3|Page www.MoussaAcademy.com 00201007153601 III. Passing Multiple Parameters,Iterables , Scope of Variables  Syntax: Def function name(paramater one, parameter two, ….) Passing Multiple  Example: Parameters to a function  Syntax: Def function name(paramater one, parameter two = value, ….)  Example: Passing Multiple Positional Parameters Arguments with default arguments to a function Keyword Arguments (same keyword used in function definition) 4|Page www.MoussaAcademy.com 00201007153601 1. Local Variables:  Used only inside a function 2. Global Variables:  Used inside and outside the function  Example: Scope of Global Variable variables Local Variable  Iterable is anything that python can loop through to get values  Function doesn’t receive original list in a mutable format, only a pointer for the list  The function can do anything it likes with its own copy of the list, but the original list remains unchanged.  Example: Original List remain unchanged Passing Iterable to functions  Passing any number of variables to the function  The variables passed to the function are saved in the function as a tuple (immutable)  To change the data passed to the function convert data to a list first Passing  Syntax: Arbitrary Def function_name(*args) Number of Arguments  Example: Printing all arguments passed 5|Page www.MoussaAcademy.com 00201007153601 IV. Returning values from a function  return allows use to save the returned value in a variable if we want to. Return the summation of the two variables passed Returns 15 Returning Values from Functions Saving the sorted list in a variable V. Understanding anonymous (lambda) functions  Used to define functions with no name but can assign name if you want to.  Syntax:  Example: Lambda Functions 6|Page www.MoussaAcademy.com 00201007153601 Week 8: Doing Python with DS242 Class Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Mastering Classes and Objects.................................................................................................................... 2 II. Creating a Class & Instance......................................................................................................................... 2 III. Instance Attributes & Class Attributes.................................................................................................... 3 IV. Instance Methods, Class Methods & Static Methods............................................................................. 4 V. Instance Variable vs Class Variables............................................................................................................ 6 VI. Instance Methods vs Class Methods vs Static Methods.......................................................................... 7 1|Page www.MoussaAcademy.com 00201007153601 I. Mastering Classes and Objects What is a Class?  A piece of code from which you can generate a unique object. What is an object  One unit of data plus code generated from a class as an instance of that class. (instance) ?  Each instance of a class is called an object Example on Class and object Instances of the Car Class  A characteristic of an object that contains information about the object  Attribute name is preceded by a dot Attribute/Property  Example: An Attribute Member.username  A python function associated with the class. What is a  Defines an action that an object can perform Method?  Example on Calling a method: Member.apply() Calling a method named apply () II. Creating a Class & Instance  Important Notes 1. You can create your own classes 2. Class name must start with a letter or underscore 3. Class name cannot have spaces or punctuations Syntax Create class syntax  Example on Creating a Class: Word class followed by the class name Docstring to describe the class  Creating a class without completing its code causes an error  To tell python that you want to create an empty class use the word pass: Creating an empty class called Car Empty Class 2|Page www.MoussaAcademy.com 00201007153601  Init method is used to make the class capable of creating objects  Init is short for initialize  The init method must follow strict naming format which is two underscores followed by init followed by two more underscores Def = definition  Example: Whenever you create an object from this class, you must pass values for  Self is just a variable name and is used to refer to the objectthe being created make two variables at theand moment year __init__ method -  You can use the name of your own choosing instead of self constructor  When def __init__ line executes, you have an empty object named self inside the class  Example 2: The Attributes of the object you will create will take the values you pass when creating the object Any object to be created will have two attributes: 1. Make 2. year  Syntax: Create an  Example: instance from Creating Three Instances from class class Car III. Instance Attributes & Class Attributes Means object from class Car  From the previous Example: Printing Instance Attributes Output  Syntax: Changing Value of Attribute  Example: 3|Page www.MoussaAcademy.com 00201007153601  Syntax: Printing type of the instance  Example: Default value for the attribute  Example: year = -1 Setting Default values for attributes Default values can be changed the same way as changing value of attribute IV. Instance Methods, Class Methods & Static Methods  Method names are distinguished from attribute names for an object by the pair of parentheses that follow the name  Syntax:  Example: Reference to the object being defined by the class.  Example 2: Class Methods The activate method take True or False as parameters Passing parameters to methods 4|Page www.MoussaAcademy.com 00201007153601  Syntax:  Example: Calling class methods by class name  Specific to all objects of same class  Applied to all new instances of the class that haven’t been created yet.  Example: Free_days is a class variable Class Variables  Same as class variables, they can be used by any instance.  Don’t use the word self while defining a class method  To define a class method, use: Called " decorator " Class Methods  Example:  We use static methods in case we have the same name of the method defined in another class  Don’t use the word self or cls because static method isn’t tied to a class or object  Example: Static Method 5|Page www.MoussaAcademy.com 00201007153601 Complete Example V. Instance Variable vs Class Variables Point of Comparison Instance Variable Class Variable Syntax  self. attribute  ClassName.attribute Why Use  Represents unique data per object  Stores shared data across all objects Example  my_dog.name = "Buddy"  Dog.type_of_object = "Mammal" 6|Page www.MoussaAcademy.com 00201007153601 VI. Instance Methods vs Class Methods vs Static Methods Point of Comparison Instance Methods Class Methods Static Methods  def method (self,...): @classmethod @staticmethod Syntax def method (cls,...):  def method (...,...):  Defines specific  Same behavior between  Defines utility object behaviors all objects of same class functions. Why Use  Changes class variables  Cannot change class variables Example  my_dog. bark ()  Dog.change_type ()  Dog.show_info () 7|Page www.MoussaAcademy.com 00201007153601 Week 10: Doing Python with Class and Sidestepping DS242 Errors Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Class Inheritance and Subclasses................................................................................................................ 2 II. Errors and Exceptions.................................................................................................................................. 4 III. Definitions............................................................................................................................................... 8 1|Page www.MoussaAcademy.com 00201007153601 I. Class Inheritance and Subclasses  Inheritance is like a son having the characteristics of his father  The class that other classes inherit their characteristics from is called the main or the base or the parent class  Parent class is just like any other class  All class are unique even if they inherit their characteristics from the same main class  Methods of the parent class works for the subclasses as well What is inheritance?  Example: "ANIMAL" is the main or base class Each of these classes is unique despite of inheriting from the same parent/base/main class  Just like any other class syntax Creating the base class  Syntax: Inheriting from a  Example: Class "Dog" will inherit all the class (creating a characteristics of class "Animal" subclass) "pass" is used to avoid error saying that the class is empty  You can create an object from the parent class directly using the following syntax: Object_name = Classname(values)  Example: Create an object from the parent class directly Created and object directly from the parent  2|Page www.MoussaAcademy.com 00201007153601 1. Method 1: parent_class_name.__init__ Calling the Calling a temporary object of the parent's class parent class 2. Method 2: super () constructor Calling a temporary object of the parent class  The subclasses will inherit all the characteristics of the parent class as well as the values in it  We can override these values in subclasses by re-initialize them  Example: Overriding values Here the value of "age" will be overridden  Python always get the methods from the specific class that called the method or value  If the method does not exist in the subclass, python will get the method from the super class  Example: Overriding Here the method methods "sound" is overridden  3|Page www.MoussaAcademy.com 00201007153601  Syntax: Extra Parameters  Example: Adding Extra parameters to Class "Dog" has one extra the subclass parameter called "color"  1. Less code & reusability Benefits of 2. Different values for same attribute Inheritance 3. Different behavior for same method 4. New attributes and methods II. Errors and Exceptions  Types of Errors: 1. Exceptions (Sometimes called Runtime errors.) a. User Defined Exceptions (Are unchecked by language) b. Built-in Exceptions  Checked Exceptions (Compile-time)  Unchecked Exceptions (Runtime) 2. Logic Errors (Runtime) 3. Syntax Errors (Compile Time) What are Errors 4. Semantic Errors (Runtime and Compile Time) and Exceptions?  An Exception it’s an error in the real world that prevents the program from running properly Traceback  Is a reference to the fact that if there were multiple exceptions 4|Page www.MoussaAcademy.com 00201007153601 1. TypeError: operation is performed on an incorrect/unsupported object type  Example: print (4 + “Hi”) 2. Key Error: the key does not exist  Example: my_set.remove(1.5) 3. Value Error: you are trying to use a wrong/empty value as argument for a function. Or Types of value not found. Unchecked  Example: Errors list.index(20) 4. Attribute Error: accessing an object attribute/method that does not exist  Example: dict.getttt(“Ali”) 5. Name Error: happens when a variable is not found in the local or global scope.  Example: dictttt[“Ali”] 1. Try – Except:  Example: If code couldn’t convert num1 and num2 into Handling errors integers then it will display an error message 5|Page www.MoussaAcademy.com 00201007153601 2. Try – Except – Except …... (specificity):  This method is used when you expect specific errors that you want to handle  Python will move through the exception you gave and if the error doesn’t match any of them it will print the standard error message  Example: Specific error Specific error 3. Try – Except – Except…..general purpose:  In this method there is an exception for general purpose telling python what to do if there is no error from the specific ones occurred  Example: Handling errors Specific error Specific error General 4. Try – Except – Else:  This method is used to continue the flow of the program if there is no error occurred  Example: If no error occurred then print num3 6|Page www.MoussaAcademy.com 00201007153601 5. Try – Except – Else – Finally:  This method is used to make the code execute something no matter what happened in the try block  Example: Handling errors  Creating your own exception handling method Checking Whether the variable is integer or not Raising Exceptions Defining Exceptions User-defined exceptions and raising in methods Using the defined exceptions in methods 7|Page www.MoussaAcademy.com 00201007153601 1. Code does not terminate (fatal exceptions) Benefits of error 2. We handle each exception one-by-one handling 3. We can make exceptions for our code to handle any runtime errors III. Definitions Term Definition Traceback  Is a reference to the fact that if there were multiple exceptions An Exception  it’s an error in the real world that prevents the program from running properly 8|Page www.MoussaAcademy.com 00201007153601 Week 11: Working with DS242 External Files Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Files and Directories.................................................................................................................................... 2 II. Open and Closing Files in Python................................................................................................................ 2 III. Reading File's Contents........................................................................................................................... 4 IV. CSV Files................................................................................................................................................... 5 V. Definitions................................................................................................................................................... 7 1|Page www.MoussaAcademy.com 00201007153601 I. Files and Directories Text Files: Contain plain text characters In text editor, appears like the human readable characters on your keyboard. Binary Files: Contain information in the form of Bytes. In text editor, not human readable Types of Files Types of Binary and text Files II. Open and Closing Files in Python If file not in same directory … specify a Syntax: path to the file using Basic Syntax Specify the kind of access to the file r:(Read): Is the default mode Used if you don’t specify a mode Open file but doesn’t allow to make changes If file doesn’t exist then the following error will appear: Modes of Files r+:(Read/Write): Allows read and write (make changes) a:(Append): Allow to add more content but doesn’t make change to the current content Add the content to the end of the file If the file doesn’t exist then it creates the file 2|Page www.MoussaAcademy.com 00201007153601 w:(Write): allow changes to be made to the file if the file doesn’t exist, it creates the file x:(Create): if the file doesn’t exist, it creates the file if the file exist then the following error will appear: t:(Text): open the file as text file allows read and write (text only) b:(Binary): open the file as binary file Variable name for allow read and write (bytes only) the file Method 1: Method 2: Starts with the word "with" The variable name for the file is assigned at the end Using Open () You don’t need to close the file at the end, python will close it automatically Variable name for the file Used to close files Syntax: "f" is the variable Using Close () assigned to be used as the file name For a plain text file (ASCII), you can use r or nothing as the mode. Important Notes For a binary file, you must specify b in the mode. on Opening and Closing Files For a text file with fancy characters, you most likely need to open it as a text file with encoding set to utf-8 in the open () statement. 3|Page www.MoussaAcademy.com 00201007153601 UTF-8 is a short for Unicode Transformation Format, 8-bit WHAT IS UTF- Is a standardized way to represent letter and number on computers 8? Made because the ASCII set of characters are not enough to cover all languages' characters, numbers, and letters III. Reading File's Contents read([size]): if the square brackets are empty, it read the whole file if the size parameter is set to number, it reads this number of characters or bytes from the file readline (): read one line of the content from a text file the line ends whenever there is: "\n" read the entire file at once Calling the read the file as one big chunk parent's class constructor readlines (): Reads all the lines of a text file into a list. Reads the file one line at a time Stores each line as an item in a list Looping with readline: Use if you don’t know the size of the file Use if the device is running out of RAM Used for Large Files and Small Files Only one line will be in the memory at a time Looping Example: Through a File 4|Page www.MoussaAcademy.com 00201007153601 Writing and Writing in a file meaning you will overwrite (replace) the content of the file Appending Appending to a file means adding more content to the existing contents in the file Python read files from up to down and from left to right Python keeps a pointer pointing at the character position of the next line in file Tell () function is used to show the pointer location At start of the file, the pointer position is zero Tell () Used to reposition of the pointer Seek () Syntax: Because the binary files most of the time are huge, they can make the device run out of memory The best way to read binary file is to read it in chunks Use. read () function with specified size Reading and Copying a Binary File IV. CSV Files CSV (Comma Separated Values) is a widely used format for sorting and transporting tabular What is a CSV data File? Tabular means the data can be displayed in table format 5|Page www.MoussaAcademy.com 00201007153601 No different than any other file Include "utf-8" if the file has special characters Opening CSV Using reader built in function Files Count rows as you Using Enumerate (): read the file Counting Rows Loop through line at a time Using IF condition: Removing Header Row Convert to String: Converting to Integers: Imported datetime module as dt Type Converting to Date: Conversions Converting to Boolean: Converting to Floats: Python floats cannot contain dollar sign or comma so we replace them 6|Page www.MoussaAcademy.com 00201007153601 You need to organize the data in the csv file and then return them to your code so as to be placed in objects and dictionaries Use python's csv module Converting from CSV to Objects and Dictionaries V. Definitions Term Definition UTF-8  Is a standardized way to represent letter and number on computers 7|Page www.MoussaAcademy.com 00201007153601 Week 12: Libraries, Packages, And Modules DS242 Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Understanding the Python Standard Library............................................................................................... 2 II. Exploring Python Packages.......................................................................................................................... 3 III. Importing Python Modules...................................................................................................................... 3 IV. Making your Own Module....................................................................................................................... 4 V. Definitions................................................................................................................................................... 4 1|Page www.MoussaAcademy.com 00201007153601 I. Understanding the Python Standard Library The Python Standard  All the stuff you get when you get the Python language Library  Identifies the type of a piece of data. The Type () Function  Example: Prints the type which is integer  Displays a list of all the attributes associated with a type.  Example: Dunder-named items  Output: The Dir () Function  Regular function do not have double underscores  Returns information about the topic in the parentheses.  Syntax:  Example Information about  Output the Integer Class The Help () Function 2|Page www.MoussaAcademy.com 00201007153601 II. Exploring Python Packages Modular  Program is broken down into smaller, more manageable components, or Programming modules. PyPi  A good resource for finding packages  A package manager used to explore, update, and remove existing packages.  Used in Terminal on Mac or CMD on windows  Checking the version of pip on device: Type this command in pip command prompt  Check the installed packages on device III. Importing Python Modules  Libraries contain Packages and Packages contain Modules Python Libraries  Use the Import Command: Import Modules  The help () and dir () functions work on imported modules only, otherwise you get an error  To import everything within the module use "*": 3|Page www.MoussaAcademy.com 00201007153601 IV. Making your Own Module  Libraries contain Packages and Packages contain Modules  A module is a.py extension that contain python code  Any code in a py file is a module Python Modules  Example for creating a module: Module named "to_date" Importing your Module V. Definitions Term Definition pip  A package manager used to explore, update, and remove existing packages. Modular Programming  Program is broken down into smaller, more manageable components, or modules. 4|Page www.MoussaAcademy.com 00201007153601 Week 13: Juggling Json Data DS242 Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. JSON Data.................................................................................................................................................... 2 II. Serialization and Deserialization................................................................................................................. 3 III. Data to & from JSON File......................................................................................................................... 4 IV. Definitions............................................................................................................................................... 5 1|Page www.MoussaAcademy.com 00201007153601 I. JSON Data  (Java Script Object Notation) is a common marshalling format for object- oriented data. What is JSON?  Saves Data like Objects Written in JavaScript  Just like Data Dictionaries in Python syntax not the language  The data is easy as tabular data in excel sheets Marshalling  Format used to send data from one computer to another. 1. Tabular Data:  Saved in csv files  Written in python  Can be converted to JSON formate 2. JSON Data: Data Formats  Saved in JSON files  Can be written in Python  Save data in the form of objects  Can be converted to csv formate 2|Page www.MoussaAcademy.com 00201007153601 Keyed Non keyed  Contained in curled praces  Contaned in square prackets  In the form of nested dictionaries  In the form of array with index Types of JSON Files II. Serialization and Deserialization  Is the process of converting an object into stream of bytes or characters than can be sent across a wire  Used to save the objects in a way that can be retrieved easily on any other computer Serialization Deserialization  The process of converting the information back to an object Methods for serialization and deserialization Conversions between JSON and data types in python 3|Page www.MoussaAcademy.com 00201007153601 III. Data to & from JSON File Dumping Data into JSON 1. Import Json Module 2. Open the file just like any file in python Steps for Loading data  The data will be converted to be a nested dictionary  Example Code: Looping Through Keyed JSON File  Data will be converted into array  Example Code: Looping Through Non Keyed JSON File 4|Page www.MoussaAcademy.com 00201007153601  Just like changing data in python dictionary 1. Changing data: Changing and Removing JSON Data 2. Remove Data: IV. Definitions Term Definition Deserialization  The process of converting the information back to an object Serialization  Is the process of converting an object into stream of bytes or characters than can be sent across a wire Marshalling  Format used to send data from one computer to another. JSON  Java Script Object Notation) is a common marshalling format for object-oriented data. 5|Page www.MoussaAcademy.com 00201007153601 Week 14: Interacting With the Internet DS242 Advanced data science programming Moussa Academy 00201007153601 WWW.MOUSSAACADEMY.COM Table of Contents I. Web, URLs, HTTP......................................................................................................................................... 2 II. Opening Web Using Coding......................................................................................................................... 3 III. Web Scraping........................................................................................................................................... 3 IV. Definitions............................................................................................................................................... 5 1|Page www.MoussaAcademy.com 00201007153601 I. Web, URLs, HTTP  Flow of a Simple Web Request – Response Process: 1. The user/ client type the URL 2. The user's computer sends a request to the internet 3. The internet directs the request to the appropriate server 4. The appropriate server receives the request 5. The server send a response back to the user's computer How Web Works? Rules used to govern the communication  The request and response process is done using protocol called HTTP or HTTPS  When web page return value this is called query string  is a key part of accessing a web page ‫ﻋﻧوان اﻟﺻﻔﺣﺔ‬  Mostly, URLs begin with HTTP or HTTPS  Structure of the URL: URL 1. Protocol 2. Subdomain 3. Domain  Hypertext Transfer Protocol HTTP  Used to set rules of communication between devices on the internet HTTPS  HTTP + "S" means secure in that data is encrypted (converted to secret code)  Responses you often get from servers: Common HTTP Status Codes 2|Page www.MoussaAcademy.com 00201007153601 II. Opening Web Using Coding Two Main Modules 1. urllib (for request and response) URL Library = urllib for accessing the 2. BeautifulSoup (for web scraping) Web Programmatically 1. Import necessary module Steps for using web 2. Start coding using python Open the webpage Example Web Usage HTTP doesn’t support spaces  Fixing the mistake: Failure Example %20 is the ASCII code for spaces III. Web Scraping

Use Quizgecko on...
Browser
Browser