The document provides an extensive overview of Python for data science, AI, and development, detailing key libraries such as NumPy, Pandas, and TensorFlow. It covers fundamental concepts including data types, typecasting, string operations, data structures (tuples, lists, dictionaries, sets), programming fundamentals (conditions, loops, functions), and working with data (file handling, data manipulation with Pandas and NumPy). Additionally, it discusses APIs and data collection methods, emphasizing the use of REST APIs for communication and data access.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
Python for Data Science, AI & Development
The document provides an extensive overview of Python for data science, AI, and development, detailing key libraries such as NumPy, Pandas, and TensorFlow. It covers fundamental concepts including data types, typecasting, string operations, data structures (tuples, lists, dictionaries, sets), programming fundamentals (conditions, loops, functions), and working with data (file handling, data manipulation with Pandas and NumPy). Additionally, it discusses APIs and data collection methods, emphasizing the use of REST APIs for communication and data access.
Typecasting: When you change the type of a variable in python.
Note: When we cast a boolean to an int, we get a 0 or a 1 and vice versa.
Format strings in Python:
String interpolation (f-strings)
Raw String (r’’)
String operations lab in python
Python Basics Summary
1. Python can distinguish among data types such as integers, floats, strings, and Booleans. 2. Integers are whole numbers that can be positive or negative. 3. Floats include integers as well as decimal numbers between the integers. 4. You can convert integers to floats using typecasting and vice-versa. 5. You can convert integers and floats to strings. 6. You can convert an integer or float value to True (1) or False (0). 7. Expressions in Python are a combination of values and operations used to produce a single result. 8. Expressions perform mathematical operations such as addition, subtraction, multiplication, and so on. 9. We can use // to perform integer division, which results in an integer value by discarding the fractional part. 10. Python follows the order of operations (BODMAS) to perform operations with multiple expressions. 11. Variables store and manipulate data, allowing you to access and modify values throughout your code. 12. The assignment operator "=" assigns a value to a variable. 13. ":" denotes the value of the variable within the code. 14. Assigning another value to the same variable overrides the previous value of that variable. 15. You can perform mathematical operations on variables using the same or different variables. 16. Modifying the value of one variable will affect other variables only if they reference the same mutable object. 17. Python string operations involve manipulating text data using tasks such as indexing, concatenation, slicing, and formatting. 18. A string is usually written within double quotes or single quotes, including letters, white space, digits, or special characters. 19. A string attaches to another variable and is an ordered sequence of characters. 20. Characters in a string identify their index numbers, which can be positive or negative. 21. We use strings as a sequence to perform sequence operations. 22. You can input a stride value to perform slicing while operating on a string. 23. Operations like finding the length of the string, combining, concatenating, and replicating, result in a new string. 24. You cannot modify an existing string; they are immutable. 25. You can use escape sequences with a backslash (\) to change the layout of a string. (For example, \n for a new line, \t for a tab, and \\ for a backslash, etc.) 26. In Python, you perform tasks such as searching, modifying, and formatting text data with its pre-built string methods functions. 27. You apply a method to a string to change its value, resulting in another string. 28. You can perform actions such as changing the case of characters in a string, replacing items in a string, finding items in a string, and so on using pre-built string methods.
Python basics cheat sheet
Or Check “pythonBasicsCheatsheet.png” in Resources folder Python Data Structure Summary 1. In Python, we often use tuples to group related data together.Tuples refer to ordered and immutable collections of elements. 2. Tuples are usually written as comma-separated elements in parentheses “()". 3. You can include strings, integers, and floats in tuples and access them using both positive and negative indices. 4. You can perform operations such as combining, concatenating, and slicing on tuples. 5. Tuples are immutable, so you need to create a new tuple to manipulate it. 6. Tuples, termed nesting, can include other tuples of complex data types. 7. You can access elements in a nested tuple through indexing. 8. Lists in Python contain ordered collections of items that can hold elements of different types and are mutable, allowing for versatile data storage and manipulation. 9. A list is an ordered sequence, represented with square brackets "[]". 10. Lists possess mutability, rendering them different from tuples. 11. A list can contain strings, integers, and floats; you can nest lists within it. 12. You can access each element in a list using both positive and negative indexing. 13. Concatenating or appending a list will result in the modification of the same list. 14. You can perform operations such as adding, deleting, splitting, and so forth on a list. 15. You can separate elements in a list using delimiters. 16. Aliasing occurs when multiple names refer to the same object. 17. You can also clone a list to create another list. 18. Dictionaries in Python are key-value pairs that provide a flexible way to store and retrieve data based on unique keys. 19. Dictionaries consist of keys and values, both composed of string elements. 20. You denote dictionaries using curly brackets. 21. The keys necessitate immutability and uniqueness. 22. The values may be either immutable or mutable, and they allow duplicates. 23. You separate each key-value pair with a comma, and you can use color highlighting to make the key more visible. 24. You can assign dictionaries to a variable. 25. You use the key as an argument to retrieve the corresponding value. 26. You can make additions and deletions to dictionaries. 27. You can perform an operation on a dictionary to check the key, which results in a true or false output. 28. You can apply methods to obtain a list of keys and values in a dictionary. 29. Sets in Python are collections of unique elements, useful for tasks such as removing duplicates and performing set operations like union and intersection. Sets lack order. 30. Curly brackets "{}" are helpful for defining elements of a set. 31. Sets do not contain duplicate items. 32. A list passed through the set function generates a set containing unique elements. 33. You use “Set Operations” to perform actions such as adding, removing, and verifying elements in a set. 34. You can combine sets using the ampersand "&" operator to obtain the common elements from both sets. 35. You can use the Union function to combine two sets, including both the common and unique elements from both sets. 36. The sub-set method is used to determine if two or more sets are subsets.
Python Programming Fundamentals
1. Python conditions use “if” statements to execute code based on true/false conditions created by comparisons and Boolean expressions. 2. Comparison operations require using comparison operators equal to "=", greater than ">", less than "<". 3. An exclamation mark "!" is used to define inequalities of a variable. 4. You can compare integers, strings, and floats. 5. Python branching directs program flow by using conditional statements (for example, if, else, elif) to execute different code blocks based on conditions or tests. 6. You can use the "if" statement with conditions to define actions if true. 7. To perform actions based on true or false output, you can use the "else" statement with conditions. 8. The elif statement allows for additional checks only if the initial condition is false. 9. To execute various operations on Boolean values, we use Boolean logic operators. 10. Python loops are control structures that automate repetitive tasks and iterate over data structures like lists or dictionaries. 11. The range() function generates a sequence of numbers with a specified start, stop, and step value for loops in Python. 12. A for loop in Python iterates over a sequence, such as a list, tuple, or string, and executes a block of code for each item in the sequence. 13. A while loop in Python executes a block of code as long as a specified condition remains true. 14. Python functions are reusable code blocks that perform specific tasks, take input parameters, and often return results, enhancing code modularity and reusability. 15. You may or may not have written the codes that are often included in functions. 16. Python has a set of built-in functions such as "len" to find the length of a sequence or "sum" to find the total sum of a sequence. 17. The "sorted" function creates a new sorted list, while "sort" sorts items in the original list. 18. You can also create your own functions in Python. 19. To ensure clarity and organization and facilitate understanding and maintenance of the code, developers must document functions using a documentation string enclosed in three quotes. 20. The help command will return the documentation defined for a particular function. 21. A function can have multiple parameters. 22. “No return” statement in the function means that the function will return nothing. 23. The "No work" function does not execute any task. You can use the "pass" keyword to meet the requirement of a non-empty body. 24. A function will usually perform more than one task. 25. In Python, the scope of a variable determines where you can access or modify that variable. Global scope allows access from anywhere, while local scope restricts it to a block or function. 26. In Python, a programmer defines a local variable within a specific block or function, which can only be accessed or modified within that block or function. 27. In Python, a global variable is a variable defined at the top level of a program that any part of the code can access or modify. 28. Exception handling in Python is a mechanism for managing and responding to errors and exceptions that may occur during program execution, preventing them from crashing the program. 29. In Python, you use the "try-except" statement to attempt a block of code and specify alternative actions to execute if an error occurs, allowing you to handle exceptions. 30. In Python, you use the "try-except-else" statement to attempt a block of code, handle exceptions in the "except" block, and execute code in the "else" block when no exceptions occur. 31. Python developers use the "try-except-else-finally" statement to attempt a block of code, catch exceptions in the "except" block, execute code in the "else" block when no exceptions occur, and ensure that the "finally" block always runs, regardless of whether an exception raised or not. 32. In Python, objects are instances of classes that encapsulate data and behavior, serving as the foundation for creating and working with various data types and custom data structures. 33. To determine the type of an object in Python, you can use the `type()` command. 34. Any changes made within the method of the object may result in a change in object type. 35. Classes in Python are blueprints for creating objects, defining their attributes and methods, enabling code organization, and object-oriented programming. 36. Function "init" is a special method used to initialize data attributes. 37. We can create instances of a class in Python. 38. Data attributes consist of the data defining the objects. 39. Methods are functions that interact and change the data attributes. 40. The method has a function that requires the self as well as other parameters. Working with Data in Python Summary 1. Python uses the open() function and allows you to read and write files, providing access to the content within the file for reading. It also allows overwriting it for writing and specifies the file mode (for example, r for reading, w for writing, a for appending). 2. To read a file, Python uses an open function along with r. 3. Python uses theopen with function to read and process a file attribute, that is, from open to close. 4. In Python, you use the open method to edit or overwrite a file. 5. To write a file, Python uses the open function along with w. 6. In Python, "a" indicates that the program has appended to the file. 7. In Python, “\n” signifies that the code should start on a new line. 8. Python uses various methods to print lines from attributes. 9. Pandas is a powerful Python library for data manipulation and analysis, providing data structures and functions to work with structured data like data frames and series. 10. You import the file (panda) by using the import command followed by the file name. 11. In Python, you use the as command to provide a shorter name for the file. 12. In Pandas, you use a data frame (df) to specify the files to read. 13. DataFrames consist of rows and columns. 14. You can create new DataFrames by using the column or columns of a specific DataFrame. 15. We can work with data in DataFrames and save the results in different formats. 16. In Python, you use the Unique method to determine unique elements in a column of the DataFrames. 17. You use the inequality operator along with df to assign a Boolean value to the selected column in DataFrames. 18. You save a new DataFrame as a different DataFrame, which may contain values from an earlier DataFrame. 19. NumPy is a Python library for numerical and matrix operations, offering multidimensional array objects and a variety of mathematical functions to work with data efficiently. 20. NumPy is a basis for Pandas. 21. A NumPy array or ND array is similar to a list, usually of a fixed size with the same kind of element. 22. A one-dimensional NumPy array is a linear sequence of elements with a single axis, like a traditional list, but optimized for numerical computations and array operations. 23. You can access elements in a NumPy using an index. 24. You use the attribute dtype to get the data type of the array elements. 25. You use nsize and ndim to get the size and dimension of the array, respectively. 26. You can use indexing and slicing methods in NumPy. 27. Vector additions are widely used operations in Python. 28. Representing vector addition with line segments or arrows is useful. 29. NumPy codes work much faster, which is helpful with lots of data. 30. You perform vector subtraction by replacing the addition sign with a negative sign. 31. Multiplying an array by a scalar in Python entails multiplying each element of the array by the scalar value, leading to a new array in which each element scales by the scalar. 32. Hadamard product refers to the element-wise multiplication of two arrays of the same shape, resulting in a new array where each element is the product of the corresponding elements in the input arrays. 33. The dot product in Python is the sum of the element-wise products of two arrays, often used for vector and matrix operations to find the scalar result of multiplying corresponding elements and summing them. 34. When working with NumPy, it is common to utilize libraries like Matplotlib to create graphs and visualizations from numerical data stored in NumPy arrays. 35. A two-dimensional NumPy array is a grid-like structure with rows and columns suitable for representing data as a matrix or a table for numerical computations. 36. In NumPy, "shape" refers to an array's dimensions (number of rows and columns), indicating its size and structure. 37. You use the attribute "size" to obtain the size of an array. 38. You use rectangular attributes to access the various elements in an array. 39. You use a scalar to multiply elements in NumPy.
APIs and Data Collection
● Simple APIs in Python are application programming interfaces that provide straightforward and easy-to-use methods for interacting with services, libraries, or data, often with minimal configuration or complexity. ○ An API lets two pieces of software talk to each other. ○ Using an API library in Python entails importing the library, calling its functions or methods to make HTTP requests, and parsing the responses to access data or services provided by the API. ○ Pandas API processes the data by communicating with the other software components. ○ An Instance forms when you create a dictionary and then use the DataFrames constructor to create a Pandas object. ○ Method “head()” will display the mentioned number of rows from the top (default 5) of DataFrames, while method “mean()” will calculate the mean and return the values ● Rest APIs allow you to communicate through the internet, taking advantage of resources like storage, access more data, AI algorithms, and so on. ○ HTTP methods transmit data over the internet. ○ An HTTP message typically includes a JSON file with instructions for operations. ○ HTTP messages containing JSON files are returned to the client as a response from web services. ○ Dealing with time series data involves using the Pandas time series function. ○ You can get data for daily candlesticks and plot the chart using Plotly with the candlestick plot. ● The HTTP (HyperText Transfer Protocol) transfers data, including web pages and resources, between a client (a web browser) and a server on the World Wide Web. ○ The HTTP protocol is commonly used for implementing various types of REST APIs. ○ An HTTP response includes information like the type of resource, length of resource, and so on ○ Uniform resource locator (URL) is the most popular way to find resources on the web. ○ URL is divided into three parts: scheme, internet address or base URL, and route ○ The GET method is one of the popular methods of requesting information. Some other methods may also include the body. ○ Response method contains the version and body of the response. ○ POST submits data to the server, PUT updates data already on the server, DELETE deletes data from the server ● Requests is a Python library that allows you to send HTTP/1.1 requests easily ○ You can modify the results of your query with the GET method. ○ You can obtain multiple requests from a URL like name, ID, and so on with a Query string. ● Web scraping in Python involves extracting and parsing data from websites to gather information for various applications, using libraries like Beautiful Soup and requests. ○ HTML comprises text surrounded by blue text elements enclosed in angular brackets called tags. ○ You can select an HTML element on a web page to inspect the webpage. ○ Web pages may also contain CSS and JavaScript along with HTML elements. ○ Each HTML document is like an HTML Tree, which may contain strings and other tags. ○ Each HTML table is comprised of table tags and is structured with elements such as rows, headers, body and so on. ● Tabular data can also be extracted from web pages using the `read_html` method in Pandas. ● Beautiful Soup in Python is a library for parsing and navigating HTML and XML documents, making extracting, and manipulating data from web pages more accessible. ● To parse a document, pass it through the Beautiful Soup constructor to get a beautiful soup object representing the document as a nested data structure. ● Beautiful soup represents HTML as a set of tree-like objects with methods to parse the HTML. ● Navigable string is like a Python string that supports beautiful soup functionality. ● find_all is a method used to extract content based on the tag’s name, its attributes, the text of a string, or some combination of these. ● The find_all method looks through a tag’s descendants and retrieves all descendants that match your filters. ● The result is a Python iterable like a list. ● File formats refer to the specific structure and encoding rules used to store and represent data in files, such as .txt for plain text or .csv for comma-separated values. ● Python works with different file formats such as CSV, XML, JSON, xlsx, and so on ● The extension of a file name will let you know what type of file it is and what it needs to open with. ● To access data from CSV files, we can use Python libraries such as Pandas. ● Similarly, different methods help parse JSON, XML, and other files.