Data Analytics with Python - MGM University - PDF
Document Details
Uploaded by Deleted User
MGM University
Dr. Shazia Shaikh
Tags
Summary
This document is a syllabus for a Data Analytics course with Python, offered in Semester III at MGM University. It covers the foundational concepts of Python programming, including data types, variables, operators, control structures, functions, and modules, alongside the broader principles of data science and analytics.
Full Transcript
Semester III z Minor Data Analytics Dr. Shazia Shaikh Asst. Professor Dr. G.Y. P. College of Computer Science & IT, MGM University z...
Semester III z Minor Data Analytics Dr. Shazia Shaikh Asst. Professor Dr. G.Y. P. College of Computer Science & IT, MGM University z Syllabus UNIT I - Introduction To Python (6 Hours) Structure of Python Program Underlying mechanism of Module Execution Branching and Looping Problem Solving Using Branches and Loops Functions Lists and Mutability Problem Solving Using Lists and Functions z Data Science Definition: Data Science is an interdisciplinary field that uses scientific methods, processes, algorithms, and systems to extract knowledge and insights from structured and unstructured data. It involves - Data Collection: Gathering raw data from various sources. Data Cleaning: Preparing and cleaning data for analysis. Data Exploration: Understanding data through statistical analysis and visualization. Modeling: Building predictive models using machine learning algorithms. Interpretation: Translating results into actionable insights z Data Analytics Data Analytics is the process of examining data sets to draw conclusions about the information they contain, often with the aid of specialized systems and software. Types of Data Analytics: Descriptive Analytics: What happened? Diagnostic Analytics: Why did it happen? Predictive Analytics: What will happen? Prescriptive Analytics: What should we do about it? z Data Science VS Data Analytics z Data Science VS Data Analytics z Python What is Python? Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting. z What can Python do? Python can be used on a server to create web applications. Python can be used alongside software to create workflows. Python can connect to database systems. It can also read and modify files. Python can be used to handle big data and perform complex mathematics. Python can be used for rapid prototyping, or for production- ready software development. z Python Syntax compared to other programming languages Python was designed for readability, and has some similarities to the English language with influence from mathematics. Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses. Python relies on indentation, using whitespace, to define scope; such as the scope of loops, functions and classes. Other programming languages often use curly-brackets for this purpose. z Structure of Python Program 1. Comments: Comments are non-executable statements that are used to explain the purpose of the code or to make notes for other programmers. They start with a ‘#’ symbol and are ignored by the interpreter. 2. Import Statements: Import statements are used to import modules or libraries into the program. These modules contain predefined functions that can be used to accomplish tasks. 3. Variables: Variables are used to store data in memory for later use. In Python, variables do not need to be declared with a specific type. 4. Data Types: Python supports several built-in data types including integers, floats, strings, booleans, and lists. https://geekysession.medium.com/structure-of-python-program-594c108e6165 z Structure of Python Program 5. Operators: Operators are used to perform operations on variables and data. Python supports arithmetic, comparison, and logical operators. 6. Control Structures: Control structures are used to control the flow of a program. Python supports if-else statements, for loops, and while loops. 7. Functions: Functions are used to group a set of related statements together and give them a name. They can be reused throughout a program. 8. Classes: Classes are used to define objects that have specific attributes and methods. They are used to create more complex data structures and encapsulate code. 9. Exceptions: Exceptions are used to handle errors that may occur during the execution of a program. https://geekysession.medium.com/structure-of-python-program-594c108e6165 z A simple python program to print the string “Hello world” Code: 1. print("Hello, World!") Output: Hello, World! z Comments Comments in Python are annotations in the code that are not executed by the interpreter. They are used to explain and document the code, making it easier for humans to understand. Comments can describe what the code does, why certain decisions were made, or any other information that might be helpful for someone reading the code later. z Types of Comments in Python Single Line Comments Created using the hash (#) symbol. # This is a single-line comment print("Hello, world!") # This prints a message Everything after the # on that line is considered a comment. Multi Line Comments # This is a multi-line comment # that spans multiple lines. While Python does not have a distinct print("Hello, world!") syntax for multi-line comments, you can use multiple single-line comments or use """ This is a multi-line comment that spans triple quotes (''' or """) for block multiple lines. comments. """ print("Hello, world!") z Variables z Using Variables z Checking data type of a variable z Checking data type of a variable Code: Output: z A simple python program to demonstrate comments, variables and data types Code: Output: Which elements of the basic structure do you find in this code? z Example: Using import statement Importing the math Module Code: Output: A simple python program to demonstrate z the data types Code: Output: z Rules for naming variables z z List, tuple, set and dictionary List: A list in Python is an ordered collection of items. You can think of it as a sequence of elements that can be changed or modified i.e. a list is mutable Lists are defined by enclosing a comma-separated sequence of items within square brackets [ ]. For example: "ordered" means that the items in the list maintain their positions, which allows you to access and modify them based on their index. z List Example z List functions z Tuple Tuple: A tuple is similar to a list but is immutable, meaning its elements cannot be changed once it's created. Tuples are defined by enclosing a comma-separated sequence of items within parentheses ( ). For example: A tuple’s immutability means you cannot change its elements once it is created, making tuples suitable for storing fixed data that should not be altered. z Tuple Example z Slicing Slicing in Python uses the syntax: Tuple_name [start : stop] start is the index where the slice begins (inclusive). stop is the index where the slice ends (exclusive). So, my_tuple[1:4] means: Start at index 1, which corresponds to the element 6. Stop at index 4, which corresponds to the element 9 but does not include it. Another Example z Set Set: A set in Python is an unordered collection of unique items. It's useful when you want to store multiple items without caring about their order. Sets are defined by enclosing a comma-separated sequence of items within curly braces { }. For example: z Dictionary Dictionary: A dictionary in Python is a collection of key-value pairs. It allows you to store and retrieve data using keys instead of indexing like lists. Each key-value pair in a dictionary is separated by a colon : and the pairs are separated by commas. Dictionaries are defined by enclosing key-value pairs within curly braces { }. For example: z Summary Lists [ ]: Ordered, changeable(mutable), allows duplicate items. Tuples ( ): Ordered, immutable, allows duplicate items. Sets { }: Unordered, mutable (can add/remove items), does not allow duplicate items. Dictionaries { }: Unordered, mutable, stores data in key- value pairs, keys must be unique. z Exercise: List Question: Create a list named my_list containing three favorite fruits. Print each fruit name on a new line. z Exercise: Tuple Question: Create a tuple named my_tuple with four elements: your first name, last name, age, and favorite hobby. Print the tuple. z Exercise: Dictionary Question: Create a dictionary named result with keys ‘name’, ‘subject’ ‘test1', ‘test2‘, and ‘total' storing your information. Print the dictionary. z Taking input and printing output Code: Output: z Using single quotes or double quotes for taking the input. Inputs can be taken using single quotes or double quotes. Both the lines of code are correct. z Code: Program in Python Idle Output: The f before the string in the print function is used to embed expressions inside string literals, using curly braces {}. This allows for more readable and concise string formatting. z Check your knowledge 1 2 What was one of the design goals of How does Python define the scope of loops, Python? functions, and classes? A) High performance A) Using curly-brackets B) Readability B) Using semicolons C) Compatibility with other programming C) Using parentheses languages D) Using indentation and whitespace D) Low-level memory manipulation 3 Which feature does Python use to complete a command that is different from many other programming languages? A) Curly-brackets B) Semicolons C) New lines D) Parentheses z Check your knowledge 4 5 Comments are executable statements Which of the following statements is correct in a program. regarding comments in Python? A) True A. Can be written using double quotes. B) False B. Can be written using single quotes. C. Can be written using # symbol D. All of the above 6 What is the data type of the variable x in the following code? x = 3.14 A. int B. str C. float D. complex z Check your knowledge 7 8 Which of the following is a valid Which of the following statements about variable name in Python? Python variable names is true? A. 1variable A. A variable name can start with a number. B. variable1 B. A variable name can contain spaces. C. variable-1 C. Variable names are case-sensitive. D. variable.1 D. A variable name can be any of the Python keywords. 9 Identify the invalid variable name among the following: A. _variable B. variable_name C. variableName D. variable-name z Modules in Python A module is a file containing Python code that can define functions, classes, and variables. It can also include runnable code. Modules help in organizing and reusing code by breaking it into smaller, manageable pieces. z Why Use Modules? Reusability: Write a function once and use it in multiple programs. Organization: Keep related functions and classes together in one file. Maintainability: Easier to maintain and update code when it is organized into modules. z Creating and Using a Module Example 1: A Simple Math Module named mymath.py with a few basic math functions. Module Main Program z Creating and Using a Module Main Program Output z Python keywords Keywords: Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers There are 35 keywords in python. https://www.w3sc hools.com/python /python_ref_keyw ords.asp z Installing and Running Python https://www.w3schools.com/python/python_ref_keywords.asp z Branching Branching in Python is the process of making decisions in your code based on certain conditions. What code is executed is decided from the result of the condition checking. The result can be either True or False. The primary way to perform branching is by using if, elif, and else statements. z If statement The if statement allows you to execute a block of code only if a certain condition is true. z else The else statement allows you to execute a block of code if the condition in the if statement is false. z elif The elif (short for else if) statement allows you to check multiple conditions. In this example, the program checks the value of score. Since score is 85, it meets the condition score >= 80 and prints "Grade: B." Task: What will be the output when score = 95 score = 79 score = 65 Score = 50 z Nested Branching Nested branching refers to placing one or more if, elif, or else statements inside another if, elif, or else block. This allows you to check additional conditions based on the outcome of the outer conditions This also provides a way to handle more complex decision-making scenarios in your code. z Nested Branching Nested if Program to check is num is bigger bigger than 5 and less than equal to 15 z Nested Branching We can also nest if statements within other if statements to check more complex conditions. Program to check if a number is positive/ not positive and if a positive number is even/odd. z Nested Branching Program to determine the value of the variable letter (if it is A, B, or C) and print an output. Task: What will be the output when letter = “B” letter = “C” letter = “J” z Exercise Question: Write a Python program that asks the user to input an integer. The program should then determine if the number is even or odd and print an appropriate message. z Exercise Question: Create a Python program that prompts the user to enter a number. The program should check if the number is positive, negative, or zero and print the result. z Exercise Question: Create a Python program that asks the user for their age. The program should check if the user is eligible to vote (must be 18 or older) and print a message accordingly. z Exercise Question: Create a Python program that asks the user for their age. The program should check if the user is eligible to vote (must be 18 or older) and print a message accordingly. z Exercise Question: Write a Python program that asks the user to enter a temperature in Celsius. The program should convert it to Fahrenheit and print the result. Use the formula: F=C×9/5+32 z Exercise Question: Write a Python program that asks the user to enter a temperature in F. The program should convert it to celsius and print the result. z Looping Using loops involves executing a block of code multiple times based on certain conditions. This approach is essential for tasks that require repetitive actions or iteration through collections of data. In Python, there are two primary types of loops: the for loop and the while loop. Each type has its own use cases and characteristics. General form: z for Loops A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. Example: Iterating Over a List: z Repeating a list for a particular number of times z Using range() to Generate Numbers: This will print numbers from 0 to 4. The range() function generates a sequence of numbers. z Iterating Over a String: This will print each letter in the string. z The break Statement With the break statement we can stop the loop before it has looped through all the items: z Loops Exit the loop when x is "banana", but this time the break comes before the print: z The continue Statement With the continue statement we can stop the current iteration of the loop, and continue with the next: z The range() Function To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Note that range(6) is not the values of 0 to 6, but the values 0 to 5. z The range() function The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6): z The range() function The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3): z else in for Loop The else keyword in a for loop specifies a block of code to be executed when the loop is finished: Important : The else block will NOT be executed if the loop is stopped by a break statement. z Nested Loops A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop": z Exercise Write a program to print this output using loops Good Girl Good Boy Good Students Bad Girl Bad Boy Bad Students z While loop With the while loop we can execute a set of statements as long as a condition is true. Here i is an increment variable Note: remember to increment i, or else the loop will continue forever. z The break statement With the break statement we can stop the loop even if the while condition is true: z The continue Statement With the continue statement we can stop the current iteration, and continue with the next: Note that number 3 is missing in the result z The else Statement With the else statement we can run a block of code once when the condition no longer is true: z Functions A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. z Creating a Function and calling a function In Python a function is defined using the def keyword: Calling a Function To call a function, use the function name followed by parenthesis: Lets try to run the program in Python Idle z Arguments Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. z Another example In this example, we pass two arguments to the function my_function z Another Example Lets run this program in Idle z Parameters or Arguments? The terms parameter and argument can be used for the same thing: information that are passed into a function. From a function's perspective: A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called. z Number of Arguments By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less. CORRECT INCORRECT z Lists and Mutability Python lists are a fundamental data structure in the language A list in Python is an ordered collection of items, which can be of any type—integers, strings, objects, or even other lists. Lists are defined by enclosing elements in square brackets [ ], with items separated by commas. Known for their mutability, which means that their contents can be changed after the list has been created. Example: z List Mutability Mutability refers to the ability of an object to be changed after it is created. In Python, lists are mutable, which means you can change their contents (add, remove, or modify elements) without changing the list’s identity (i.e., its memory address). Actions that can be taken on Lists (Mutability) Adding Elements: You can use methods like append() or extend() to add elements to a list. Removing Elements: You can use methods like remove() or pop() to remove elements from a list. Modifying Elements: You can directly assign new values to elements at specific indices. z Examples of Python List Mutability Adding Elements: You can add elements to a list using methods like append() or extend() z Removing Elements Elements can be removed using remove() or pop(): z Modifying Elements You can change the value of an element directly: z Example 1: Nested Lists Mutability also applies to nested lists, where you can modify elements of inner lists: matrix is a list that contains two inner lists: [1, 2] and [3, 4]. The outer list has two elements (the inner lists) matrix and matrix refer to these inner lists.4 Each of the inner lists have items indexed 0 onwards. Index Numbers of list items Index Numbers of 0 1 0 1 2 Inner Lists Item with index no 1 in the first First Inner List index 1 3 4 inner List z Example 1: Nested Lists z Aliasing and Mutability When you assign one list to another variable, they both reference the same list. Changes to one will affect the other z END OF UNIT I