Category: Python

  • Harvard CS50’s Artificial Intelligence with Python – Full University Course

    Harvard CS50’s Artificial Intelligence with Python – Full University Course

    This source explains how AI can be used for problem-solving, moving from explicit instructions to learning from data. It introduces supervised learning, where AI learns to map inputs to outputs using labeled datasets, covering classification tasks and nearest neighbor algorithms. The source also discusses linear regression, support vector machines, and techniques like perceptron learning. It transitions to reinforcement learning, where AI learns through rewards and punishments in an environment, and touches on unsupervised learning with clustering techniques like k-means. Finally, the document explores neural networks, detailing their structure, training via gradient descent and backpropagation, and their applications in various AI problems.

    Propositional Logic, Model Checking, and Beyond: A Comprehensive Study Guide

    I. Review of Key Concepts

    • Propositional Logic: A system for representing logical statements and reasoning about their truth values.
    • Propositional Symbols: Variables representing simple statements that can be either true or false (e.g., P, Q, R).
    • Logical Connectives: Symbols used to combine propositional symbols into more complex statements:
    • and (∧): Both statements must be true for the combined statement to be true.
    • or (∨): At least one statement must be true for the combined statement to be true.
    • not (¬): Reverses the truth value of a statement.
    • implies (→): If the first statement is true, then the second statement must also be true.
    • biconditional (↔): Both statements have the same truth value (both true or both false).
    • Knowledge Base (KB): A set of sentences representing facts known about the world.
    • Query (α): A question about the world that we want to answer using the KB.
    • Entailment (KB ⊨ α): The relationship between the KB and a query, meaning that the KB logically implies the query; whenever the KB is true, the query must also be true.
    • Model: An assignment of truth values (true or false) to all propositional symbols in the language. Represents a possible world or state.
    • Model Checking: An algorithm for determining entailment by enumerating all possible models and checking if, in every model where the KB is true, the query is also true.
    • Inference Algorithm: A procedure to derive new sentences from existing ones in the KB.
    • Inference Rules: Logical equivalences used to manipulate and simplify logical expressions (e.g., implication elimination, De Morgan’s laws, distributive law).
    • Soundness: An inference algorithm is sound if it only derives conclusions that are entailed by the KB.
    • Completeness: An inference algorithm is complete if it can derive all conclusions that are entailed by the KB.
    • Conjunctive Normal Form (CNF): A logical sentence expressed as a conjunction (AND) of clauses, where each clause is a disjunction (OR) of literals.
    • Clause: A disjunction of literals (e.g., P or not Q or R).
    • Literal: A propositional symbol or its negation (e.g., P, not Q).
    • Resolution: An inference rule that combines two clauses containing complementary literals to produce a new clause.
    • Factoring: Removing duplicate literals within a clause.
    • Empty Clause: The result of resolving two contradictory clauses, representing a contradiction (always false).
    • Inference by Resolution: An algorithm for proving entailment by converting the KB and the negation of the query to CNF, and then repeatedly applying the resolution rule until the empty clause is derived.
    • Joint Probability Distribution: A table showing the probabilities of all possible combinations of values for a set of random variables.
    • Inclusion-Exclusion Formula: A formula for calculating the probability of A or B: P(A or B) = P(A) + P(B) – P(A and B).
    • Marginalization: Calculating the probability of a variable by summing over all possible values of other variables: P(A) = Σ P(A and B).
    • Conditioning: Expressing the probability of A in terms of the conditional probability of A given B and the probability of B: P(A) = P(A|B) * P(B) + P(A|¬B) * P(¬B).
    • Conditional Probability: The probability of event A occurring given that event B has already occurred, denoted P(A|B).
    • Random Variable: A variable whose value is a numerical outcome of a random phenomenon.
    • Heuristic Function: An estimate of the “goodness” of a state (e.g., the distance to the goal).
    • Local Search: A class of optimization algorithms that start with an initial state and iteratively improve it by moving to neighboring states.
    • Hill Climbing: A local search algorithm that repeatedly moves to the neighbor with the highest value.
    • Steepest Ascent Hill Climbing: Chooses the best neighbor among all neighbors in each iteration.
    • Stochastic Hill Climbing: Chooses a neighbor randomly from the neighbors that are better than the current state.
    • First Choice Hill Climbing: Chooses the first neighbor with a higher value and moves there.
    • Random Restart Hill Climbing: Runs hill climbing multiple times with different initial states and returns the best result.
    • Local Beam Search: Keeps track of k best states and expands all of them in each iteration.
    • Local Maximum/Minimum: A state that is better than all its neighbors but not the best state overall.
    • Simulated Annealing: A local search algorithm that sometimes accepts worse neighbors with a probability that decreases over time (temperature).
    • Temperature (in Simulated Annealing): A parameter that controls the probability of accepting worse neighbors; high temperature means higher probability, and low temperature means lower probability.
    • Delta E (ΔE): The difference in value (or cost) between the current state and a neighboring state.
    • Traveling Salesman Problem (TSP): Finding the shortest possible route that visits every city and returns to the origin city.
    • NP-Complete Problems: A class of problems for which no known polynomial-time algorithm exists.
    • Linear Programming: A mathematical technique for optimizing a linear objective function subject to linear equality and inequality constraints.
    • Objective Function: A mathematical expression to be minimized or maximized in linear programming.
    • Constraints: Restrictions or limitations on the values of variables in linear programming.
    • Constraint Satisfaction Problem (CSP): A problem where the goal is to find values for a set of variables that satisfy a set of constraints.
    • Variables (in CSP): Entities with associated domains of possible values.
    • Domains (in CSP): The set of possible values that can be assigned to a variable.
    • Constraints (in CSP): Restrictions on the values that variables can take, specifying allowable combinations of values.
    • Unary Constraint: A constraint involving only one variable.
    • Binary Constraint: A constraint involving two variables.
    • Node Consistency: Ensuring that all values in a variable’s domain satisfy the variable’s unary constraints.
    • Arc Consistency: Ensuring that for every value in a variable’s domain, there exists a consistent value in the domain of each of its neighboring variables.
    • AC3: A common algorithm for enforcing arc consistency.
    • Backtracking Search: A recursive algorithm that explores possible solutions by trying different values for variables and backtracking when a constraint is violated.
    • Minimum Remaining Values (MRV) Heuristic: A variable selection strategy that chooses the variable with the fewest remaining legal values.
    • Degree Heuristic: A variable selection strategy that chooses the variable involved in the largest number of constraints on other unassigned variables.
    • Least Constraining Value Heuristic: A value selection strategy that chooses the value that rules out the fewest choices for neighboring variables in the constraint graph.
    • Supervised Machine Learning: A type of machine learning where an algorithm learns from labeled data to make predictions or classifications.
    • Inputs (x): The features or attributes used by a machine learning model to make predictions.
    • Outputs (y): The target variables or labels that a machine learning model is trained to predict.
    • Hypothesis Function (h): A mathematical function that maps inputs to outputs.
    • Weights (w): Parameters in a machine learning model that determine the importance of each input feature.
    • Learning Rate (α): A parameter that controls the step size during training.
    • Threshold Function: A function that outputs one value if the input is above a threshold and another value if the input is below the threshold.
    • Logistic Regression: A statistical method for binary classification using a logistic function to model the probability of a certain class or event.
    • Soft Threshold: A function that smoothly transitions between two values, allowing for outputs between 0 and 1.
    • Dot Product: A mathematical operation that multiplies corresponding elements of two vectors and sums the results.
    • Gradient Descent: An iterative optimization algorithm for finding the minimum of a function.
    • Stochastic Gradient Descent: An optimization algorithm that updates the parameters of a machine learning model using the gradient computed from a single randomly chosen data point.
    • Mini-Batch Gradient Descent: An optimization algorithm that updates the parameters of a machine learning model using the gradient computed from a small batch of data points.
    • Neural Networks: A type of machine learning model inspired by the structure of the human brain, consisting of interconnected nodes (neurons) organized in layers.
    • Activation Function: A function applied to the output of a neuron in a neural network to introduce non-linearity.
    • Layers (in Neural Networks): A level of nodes that receive input from other nodes and pass their output to additional nodes.
    • Natural Language Processing (NLP): The branch of AI that deals with the interaction between computers and human language.
    • Syntax: The set of rules that govern the structure of sentences in a language.
    • Semantics: The meaning of words, phrases, and sentences in a language.
    • Formal Grammar: A set of rules for generating sentences in a language.
    • Context-Free Grammar: A type of formal grammar where rules consist of a single non-terminal symbol on the left-hand side.
    • Terminal Symbol: A symbol that represents a word in a language.
    • Non-Terminal Symbol: A symbol that represents a phrase or category of words in a language.
    • Rewriting Rules: Rules that specify how non-terminal symbols can be replaced by other symbols.
    • Noun Phrase: A phrase that functions as a noun.
    • Verb Phrase: A phrase that functions as a verb.
    • Natural Language Toolkit (NLTK): A Python library for NLP.
    • Parsing: The process of analyzing a sentence according to the rules of a grammar.
    • Syntax Tree: A hierarchical representation of the structure of a sentence.
    • Statistical NLP: An approach to NLP that uses statistical models learned from data.
    • n-gram: A contiguous sequence of n items from a sample of text.
    • Markov Chain: A sequence of events where the probability of each event depends only on the previous event.
    • Tokenization: The process of splitting a sequence of characters into pieces (tokens).
    • Text Classification: The task of assigning a category label to a text.
    • Sentiment Analysis: Determining the emotional tone or attitude expressed in a piece of text.
    • Bag-of-Words Model: A text representation that represents a document as the counts of its words, disregarding grammar and word order.
    • Term Frequency (TF): The number of times a term appears in a document.
    • Inverse Document Frequency (IDF): A measure of how rare a term is across a collection of documents.
    • TF-IDF: A weight used in information retrieval and text mining that reflects how important a word is to a document in a corpus.
    • Stop Words: Common words that are often removed from text before processing.
    • Word Embeddings: Vector representations of words that capture semantic relationships.
    • One-Hot Representation: A vector representation where each word is represented by a vector with a 1 in the corresponding index and 0s elsewhere.
    • Distributed Representation: A vector representation where the meaning of a word is distributed across multiple values.
    • Word2Vec: A model for learning word embeddings.

    II. Short Answer Quiz

    1. Explain the difference between soundness and completeness in the context of inference algorithms. Soundness means that any conclusion drawn by the algorithm is actually entailed by the knowledge base. Completeness means that the algorithm is capable of deriving every conclusion that is entailed by the knowledge base.
    2. Describe the process of converting a logical sentence into Conjunctive Normal Form (CNF). The process involves eliminating bi-conditionals and implications, moving negations inward using De Morgan’s laws, and using the distributive law to get a conjunction of clauses where each clause is a disjunction of literals.
    3. What is the purpose of using the resolution inference rule in propositional logic? The resolution rule is used to derive new clauses from existing ones, aiming to ultimately derive the empty clause, which indicates a contradiction and proves entailment.
    4. Explain the marginalization rule and provide a simple example. Marginalization calculates the probability of a variable by summing over all possible values of other variables. For example, if you want to know the probability that someone likes ice cream, you would take the probability of them liking ice cream and liking chocolate times the probability that they like chocolate.
    5. What is the key idea behind local search algorithms? Local search algorithms start with an initial state and iteratively improve it by moving to neighboring states, based on some evaluation function, without necessarily keeping track of the path taken to reach the solution.
    6. Describe how simulated annealing helps to avoid local optima. Simulated annealing accepts worse neighbors with a probability that decreases over time, allowing the algorithm to escape local optima early in the search and converge towards a global optimum later.
    7. In linear programming, what are the roles of the objective function and constraints? The objective function is what we want to minimize or maximize, while constraints are limitations on the values of variables that must be satisfied.
    8. What is the purpose of enforcing arc consistency in a constraint satisfaction problem (CSP)? Enforcing arc consistency reduces the domains of variables by removing values that cannot be part of any solution due to binary constraints, making the search for a solution more efficient.
    9. Explain the difference between a one-hot representation and a distributed representation in NLP. A one-hot representation represents a word as a vector with a 1 in the corresponding index and 0s elsewhere, while a distributed representation distributes the meaning of a word across multiple values in a vector.
    10. How do word embedding models like Word2Vec capture semantic relationships between words? Word2Vec captures semantic relationships by training a model to predict the context words surrounding a given word in a large corpus, resulting in vector representations where similar words are located close to each other in vector space.

    III. Essay Questions

    1. Compare and contrast model checking and inference by resolution as methods for determining entailment in propositional logic. Discuss the advantages and disadvantages of each approach.
    2. Explain how local search algorithms can be applied to solve optimization problems. Discuss the challenges of local optima and describe techniques, such as simulated annealing, for overcoming these challenges.
    3. Describe the general framework of a constraint satisfaction problem (CSP). Discuss the role of variable and value selection heuristics in improving the efficiency of backtracking search for solving CSPs.
    4. Explain the process of training a machine learning model for sentiment analysis. Discuss the different text representation techniques, such as bag-of-words and TF-IDF, and the role of word embeddings.
    5. Describe the key concepts in Natural Language Processing (NLP), including syntax and semantics. Discuss how NLP techniques are used to understand and generate natural language.

    IV. Glossary of Key Terms

    • Activation Function: A function applied to the output of a neuron in a neural network to introduce non-linearity, enabling the network to learn complex patterns.
    • Arc Consistency: A constraint satisfaction technique ensuring that for every value in a variable’s domain, there exists a consistent value in the domain of each of its neighboring variables based on the problem constraints.
    • Backtracking Search: A recursive algorithm that explores possible solutions by trying different values for variables and backtracking when a constraint is violated, allowing the algorithm to systematically search the solution space.
    • Bag-of-Words Model: A text representation in NLP that represents a document as the counts of its words, disregarding grammar and word order, which helps quantify the content of texts for analysis.
    • Clause: In logic, it is the statement that combines different literals with “or” relationship.
    • Complete: An inference algorithm that can derive all conclusions entailed by the KB.
    • Conditioning: A probability rule that expresses the probability of one event in terms of its conditional probability, and this rule is used to find the probabilities that are unknown with the information given.
    • Conjunctive Normal Form (CNF): A standardized logical sentence expressed as a conjunction (AND) of clauses, where each clause is a disjunction (OR) of literals, simplifying logical deductions.
    • Constraints: Limitation to the conditions of the variables in linear programing or constraint satisfaction problems.
    • Context-Free Grammar: A type of formal grammar where rules consist of a single non-terminal symbol on the left-hand side, used to define the syntax of programming languages.
    • Delta E (ΔE): The difference in value between the current state and its neighboring states.
    • Distributed Representation: It describes the meaning of the representation of a word distributing over multiple values in vector which is the idea behind the word embedding technique.
    • Domain: The set of possible values that can be assigned to a variable.
    • Entailment (KB ⊨ α): KB logically implies that α; whenever KB is true, so does α, which is the relationship that is important when the machine needs to find if the conclusion is correct or not.
    • Formal Grammar: A set of rules for generating sentences in a language, and those rules are applied in order to find what it is that is trying to be said in language analysis.
    • Heuristic Function: It estimates the ‘goodness’ of a state (e.g., the distance to the goal), which will let machine learning models take efficient and near perfect results.
    • Hill Climbing: This iterative optimization algorithm is characterized by continuously searching to find better solution while moving to a better neighbor and also have the highest value.
    • Hypothesis Function (h): This function maps inputs to outputs and can be used to learn and predict.
    • Inclusion-Exclusion Formula: Used to find the P(A or B), in which it finds the P(A), P(B), P(A and B), and finds P(A)+P(B)-P(A and B) in result.
    • Inference Algorithm: A procedure to derive new sentences from existing ones in the KB.
    • Joint Probability Distribution: A table showing the probabilities of all possible combinations of values for a set of random variables.
    • Knowledge Base (KB): A set of sentences representing facts known about the world.
    • Layers (in Neural Networks): A level of nodes that receive input from other nodes and pass their output to additional nodes.
    • Learning Rate (α): It controls the step size during the machine learning algorithm.
    • Linear Programming: A mathematical technique for optimizing a linear objective function subject to linear equality and inequality constraints.
    • Literal: A propositional symbol or its negation (e.g., P, not Q) that describes the condition of a statement.
    • Local Maximum/Minimum: A state that is better than all its neighbors but not the best state overall.
    • Local Search: A class of optimization algorithms that start with an initial state and iteratively improve it by moving to neighboring states.
    • Logistic Regression: A statistical method for binary classification using a logistic function to model the probability of a certain class or event.
    • Marginalization: Calculating the probability of a variable by summing over all possible values of other variables: P(A) = Σ P(A and B).
    • Markov Chain: A sequence of events where the probability of each event depends only on the previous event, allowing modeling of sequences over time.
    • Model: An assignment of truth values (true or false) to all propositional symbols in the language that represents the state.
    • Model Checking: An algorithm for determining entailment by enumerating all possible models and checking if, in every model where the KB is true, the query is also true.
    • n-gram: A contiguous sequence of n items from a sample of text that helps in analyzing languages and predicting text.
    • Natural Language Processing (NLP): The field of AI that is related to the understanding of human language.
    • Noun Phrase: A phrase that functions as a noun to use for language parsing.
    • NP-Complete Problems: A class of problems for which no known polynomial-time algorithm exists.
    • Objective Function: An mathematical function to be minimized or maximized in linear programming.
    • One-Hot Representation: A vector representation where each word is represented by a vector with a 1 in the corresponding index and 0s elsewhere.
    • Parsing: This process of taking a sentence and analyzing it according to grammar rules in NLP.
    • Propositional Logic: A system for representing logical statements and reasoning about their truth values.
    • Query (α): The question that we want to answer using the KB.
    • Random Variable: A variable whose value is a numerical outcome of a random phenomenon.
    • Rewriting Rules: Rules that specify how non-terminal symbols can be replaced by other symbols.
    • Semantics: the meaning of words, phrases, and sentences in a language, which helps with extracting the insights and understanding of language.
    • Simulated Annealing: A local search algorithm that sometimes accepts worse neighbors with a probability that decreases over time (temperature).
    • Soft Threshold: A function that smoothly transitions between two values, allowing for outputs between 0 and 1.
    • Soundness: An inference algorithm is sound if it only derives conclusions that are entailed by the KB.
    • Statistical NLP: An approach to NLP that uses statistical models learned from data.
    • Steepest Ascent Hill Climbing: Chooses the best neighbor among all neighbors in each iteration.
    • Stop Words: Common words that are often removed from text before processing.
    • Syntax: The set of rules that govern the structure of sentences in a language.
    • Syntax Tree: A hierarchical representation of the structure of a sentence, used to know how a structure looks with a graphical approach.
    • Temperature (in Simulated Annealing): A parameter that controls the probability of accepting worse neighbors; high temperature means higher probability, and low temperature means lower probability.
    • Tokenization: The process of splitting a sequence of characters into pieces (tokens), which allows for language parsing and to read for machines.
    • Traveling Salesman Problem (TSP): Finding the shortest possible route that visits every city and returns to the origin city.
    • Unary Constraint: A constraint involving only one variable.
    • Verb Phrase: A phrase that functions as a verb to be analyzed in parsing.
    • Weights (w): Parameters in a machine learning model that determine the importance of each input feature, letting it know the emphasis on each feature.
    • Word Embeddings: Vector representations of words that capture semantic relationships.
    • Word2Vec: A model for learning word embeddings by knowing what words mean, learning and classifying similar words.

    AI: Reasoning, Search, NLP, and Learning Techniques

    Here’s a briefing document summarizing the main themes and ideas from the provided sources.

    Briefing Document: Artificial Intelligence – Reasoning, Search, and Natural Language Processing

    Overview:

    The sources cover several fundamental concepts in Artificial Intelligence (AI), including logical reasoning, search algorithms, probabilistic reasoning, and natural language processing (NLP). They explore techniques for representing knowledge, drawing inferences, solving problems through search, handling uncertainty, and enabling computers to understand and generate human language.

    I. Logical Reasoning and Inference:

    • Entailment and Inference Algorithms: The core idea is that AI systems should be able to determine if a knowledge base (KB) entails a query (alpha). This means: “Given some query about the world…the question we want to ask…is does KB, our knowledge base, entail alpha? In other words, using only the information we know inside of our knowledge base…can we conclude that this sentence alpha is true?”
    • Model Checking: This is a basic inference algorithm. It involves enumerating all possible models (assignments of truth values to variables) and checking if, in every model where the knowledge base is true, the query (alpha) is also true. “If we wanted to determine if our knowledge base entails some query alpha, then we are going to enumerate all possible models…And if in every model where our knowledge base is true, alpha is also true, then we know that the knowledge base entails alpha.”
    • Inference Rules: These are logical transformations used to derive new knowledge from existing knowledge. Examples include:
    • Implication Elimination: alpha implies beta can be transformed into not alpha or beta. “This is a way to translate if-then statements into or statements… if I have the implication, alpha implies beta, that I can draw the conclusion that either not alpha or beta”
    • Biconditional Elimination: a if and only if b becomes a implies b and b implies a.
    • De Morgan’s Laws: These laws relate ANDs and ORs through negation. not (alpha and beta) is equivalent to not alpha or not beta. And not (alpha or beta) is equivalent to not alpha and not beta. “If it is not true that alpha and beta, well, then either not alpha or not beta… if you have a negation in front of an and expression, you move the negation inwards, so to speak…and then flip the and into an or.”
    • Distributive Law: alpha and (beta or gamma) is equivalent to (alpha and beta) or (alpha and gamma).
    • Conjunctive Normal Form (CNF): A standard form for logical sentences where it is represented as a conjunction (AND) of clauses, where each clause is a disjunction (OR) of literals (propositional symbols or their negations). “A conjunctive normal form sentence is a logical sentence that is a conjunction of clauses…a conjunction of clauses means it is an and of individual clauses, each of which has ors in it.”
    • Resolution: An inference rule that applies to clauses in CNF. If you have P or Q and not P or R, you can resolve them to get Q or R. This involves dealing with factoring (removing duplicate literals) and the empty clause (representing a contradiction). “…if I have two clauses where there’s something that conflicts or something complementary between those two clauses, I can resolve them to get a new clause, to draw a new conclusion.”
    • Inference by Resolution: To prove that a knowledge base entails a query (alpha), we assume not alpha and try to derive a contradiction (the empty clause) using resolution. “We want to prove that our knowledge base entails some query alpha…we’re going to try to prove that if we know the knowledge and not alpha, that that would be a contradiction…To determine if our knowledge base entails some query alpha, we’re going to convert knowledge base and not alpha to conjunctive normal form”

    II. Search Algorithms:

    • Search Problems: Defined by an initial state, actions, a transition model, a goal test, and a path cost function.
    • Local Search: Algorithms that operate on a single current state and move to neighbors. They don’t care about the path to the solution.
    • Hill Climbing: A simple local search algorithm that repeatedly moves to the neighbor with the highest value (or lowest cost). It suffers from problems with local maxima/minima. “Generally, what hill climbing is going to do is it’s going to consider the neighbors of that state…and pick the highest one I can…continually looking at all of my neighbors and picking the highest neighbor…until I get to a point…where I consider both of my neighbors and both of my neighbors have a lower value than I do.”
    • Variations: Steepest ascent, stochastic, first choice, random restart, local beam search.
    • Simulated Annealing: A local search algorithm that sometimes accepts worse moves to escape local optima. The probability of accepting a worse move depends on the “temperature” and the difference in cost (delta E). “whereas before, we never, ever wanted to take a move that made our situation worse, now we sometimes want to make a move that is actually going to make our situation worse…And so how do we do that? How do we decide to sometimes accept some state that might actually be worse? Well, we’re going to accept a worse state with some probability.”
    • Linear Programming: A family of problems where the goal is to minimize a cost function subject to linear constraints. “the goal of linear programming is to minimize a cost function…subject to particular constraints, subjects to equations that are of the form like this of some sequence of variables is less than a bound or is equal to some particular value”

    III. Constraint Satisfaction Problems (CSPs):

    • Definition: Problems defined by variables, domains (possible values for each variable), and constraints.
    • Node Consistency: Ensuring that all values in a variable’s domain satisfy the unary constraints (constraints involving only that variable). “…we can pick any of these values in the domain. And there won’t be a unary constraint that is violated as a result of it.”
    • Arc Consistency: Ensuring that all values in a variable’s domain satisfy the binary constraints (constraints involving two variables). “In order to make some variable x arc consistent with respect to some other variable y, we need to remove any element from x’s domain to make sure that every choice for x, every choice in x’s domain, has a possible choice for y.”
    • AC3: An algorithm for enforcing arc consistency across an entire CSP. It maintains a queue of arcs and revises domains to ensure consistency. “AC3 takes a constraint satisfaction problem. And it enforces our consistency across the entire problem…It’s going to basically maintain a queue or basically just a line of all of the arcs that it needs to make consistent.”
    • Backtracking Search: A depth-first search algorithm for solving CSPs. It assigns values to variables one at a time, backtracking when a constraint is violated.
    • Minimum Remaining Values (MRV): A heuristic for variable selection that chooses the variable with the fewest remaining legal values in its domain. “Select the variable that has the fewest legal values remaining in its domain…In the example of the classes and the exam slots, you would prefer to choose the class that can only meet on one possible day.”
    • Degree Heuristic: A heuristic used to select what the best variable will be. “The general approach is that in cases of ties, where two or more of the classes each can only have one possible day of the exam left, we want to choose the one that is involved in the most constraints, the one that we expect to potentially have the bigger impact on the overall problem”
    • Least Constraining Value: A heuristic for value selection that chooses the value that rules out the fewest choices for neighboring variables. “Loop over the values in the domain that we haven’t yet tried and pick the value that rules out the fewest values from the neighboring variables.”

    IV. Probabilistic Reasoning:

    • Joint Probability Distribution: A table showing the probabilities of all possible combinations of values for a set of random variables.
    • Inclusion-Exclusion Principle: Used to calculate the probability of A or B: P(A or B) = P(A) + P(B) – P(A and B). Deals with the problem of overcounting when calculating probabilities.
    • Marginalization: A rule used to calculate the probability of a variable by summing over all possible values of other variables. “I need to sum up not just over B and not B, but for all of the possible values that the other random variable could take on…I’m going to sum up over j, where j is going to range over all of the possible values that y can take on. Well, let’s look at the probability that x equals xi and y equals yj.”
    • Conditioning: Similar to marginalization, but uses conditional probabilities instead of joint probabilities.

    V. Supervised Learning:

    • Hypothesis Function: A function that maps inputs to outputs. In supervised learning the input consists of a set of labeled data points, each with multiple features and one associated value, or ‘label’. The job of supervised learning is to ‘learn’ a model that correctly maps an input consisting of a data point with multiple features to a corresponding output.
    • Weights: Parameters of the hypothesis function that determine the importance of different input features. “We’ll generally call that number a weight for how important should these variables be in trying to determine the answer.”
    • Threshold Function: A function that outputs one category if the weighted sum of inputs is above a threshold and another category otherwise. “If we do all this math, is it greater than or equal to 0? If so, we might categorize that data point as a rainy day. And otherwise, we might say, no rain.”
    • Logistic Regression: Uses a logistic function (sigmoid) instead of a hard threshold, allowing for probabilistic outputs between 0 and 1. “Instead of using this hard threshold type of function, we can use instead a logistic function…And as a result, the possible output values are no longer just 0 and 1…But you can actually get any real numbered value between 0 and 1.”
    • Gradient Descent: An iterative optimization algorithm used to find the optimal weights for a model by repeatedly updating the weights in the direction of the negative gradient of the cost function. “And we can use gradient descent to train a neural network, that gradient descent is going to tell us how to adjust the weights to try and lower that overall cost on all the data points.”
    • Stochastic Gradient Descent: Updates the weights based on a single randomly chosen data point at each iteration.
    • Mini-Batch Gradient Descent: Updates the weights based on a small batch of data points at each iteration.
    • Neural Networks: A network of interconnected nodes (neurons) organized in layers. Each connection has a weight. Neural networks take an input and ‘learn’ to modify the weight of each connection to accurately map an input to an output. A simple neural network consists of an input layer and an output layer, while more complex neural networks consist of several hidden layers between input and output. “we create a network of nodes…and if we want, we can connect all of these nodes together such that every node in the first layer is connected to every node in the second layer…And each of these edges has a weight associated with it.”
    • Activation Function: A function applied to the output of each node in a neural network to introduce non-linearity. “You take the inputs, you multiply them by the weights, and then you typically are going to transform that value a little bit using what’s called an activation function.”
    • Multi-Class Classification: A classification problem with more than two categories. Can be handled using neural networks with multiple output nodes, each representing the probability of belonging to a particular class.

    VI. Natural Language Processing (NLP):

    • Syntax: The structure of language.
    • Semantics: The meaning of language. “While syntax is all about the structure of language, semantics is about the meaning of language. It’s not enough for a computer just to know that a sentence is well-structured if it doesn’t know what that sentence means.”
    • Formal Grammar: A system of rules for generating sentences in a language.
    • Context-Free Grammar (CFG): A type of formal grammar that defines rules for rewriting non-terminal symbols into terminal symbols (words) or other non-terminal symbols. “a context-free grammar is some system of rules for generating sentences in a language…We’re going to give the computer some rules that we know about language and have the computer use those rules to make sense of the structure of language.”
    • NLTK (Natural Language Toolkit): A Python library for NLP tasks.
    • N-grams: Contiguous sequences of n items (characters or words) from a sample of text.
    • Tokenization: The process of splitting a sequence of characters into pieces, such as words.
    • Markov Chain: A sequence of values where one value can be predicted based on the preceding values. Can be used for language generation. “Recall that a Markov chain is some sequence of values where we can predict one value based on the values that came before it…we can use that to predict what word might come next in a sequence of words.”
    • Text Classification: The problem of assigning a category or label to a piece of text.
    • Sentiment Analysis: A specific text classification task that involves determining the sentiment (positive, negative, neutral) of a piece of text.
    • Bag of Words: A representation of text as a collection of words, disregarding grammar and word order, but keeping track of word frequencies. “With the bag of words representation, I’m just going to keep track of the count of every single word, which I’m going to call features.”
    • TF-IDF (Term Frequency-Inverse Document Frequency): A weighting scheme that assigns higher weights to words that are frequent in a document but rare in the overall corpus.
    • One-Hot Representation: A vector representation of a word where one element is 1 and all other elements are 0. “Each of these words now has a distinct vector representation. And this is what we often call a one-hot representation, a representation of the meaning of a word as a vector with a single 1 and all of the rest of the values are 0.”
    • Distributed Representation: A vector representation of a word where the meaning is distributed across multiple values, ideally in such a way that similar words have similar vector representations.
    • Word Embeddings: Distributed representations of words that capture semantic relationships.
    • Word2Vec: A model for generating word embeddings based on the context in which words appear. “we’re going to define the meaning of a word based on the words that appear around it, the context words around it…we’re going to say is because the words breakfast and lunch and dinner appear in a similar context, that they must have a similar meaning.”

    This briefing document provides a high-level overview of the concepts covered in the sources. It highlights key definitions, algorithms, and techniques used in AI.

    NLP, ML, and Problem Solving: FAQ

    Natural Language Processing, Machine Learning and Problem Solving: FAQ

    1. What is the core concept of “entailment” in the context of knowledge bases and inference algorithms, and how does model checking help determine entailment?

    Entailment refers to whether a knowledge base (KB) logically implies a query (alpha). In other words, can you conclude that alpha is true solely based on the information within the KB? Model checking is an algorithm that answers this by enumerating all possible models (assignments of true/false to propositional symbols). If, in every model where the KB is true, alpha is also true, then the KB entails alpha. Essentially, it exhaustively checks if alpha must be true whenever the KB is true.

    2. Explain the model checking algorithm, including how it enumerates models and determines if a knowledge base entails a query.

    The model checking algorithm involves the following steps:

    1. Enumerate all possible models: List every possible combination of truth values (true or false) for all propositional symbols in the knowledge base and query.
    2. Evaluate the knowledge base in each model: Determine if the knowledge base (KB) is true or false in each of the enumerated models.
    3. Check the query in models where the KB is true: For every model where the KB is true, check if the query (alpha) is also true.
    • Determine entailment:If alpha is true in every model where the KB is true, then the KB entails alpha.
    • If there exists at least one model where the KB is true but alpha is false, then the KB does not entail alpha.

    3. What are inference rules in propositional logic, and give examples of implication elimination, biconditional elimination, and De Morgan’s laws?

    Inference rules are logical equivalences that allow you to transform logical sentences into different, but logically equivalent, forms. This is useful for drawing new conclusions from existing knowledge. Here are some examples:

    • Implication Elimination: alpha implies beta is equivalent to not alpha or beta. This replaces an implication with an OR statement.
    • Biconditional Elimination: alpha if and only if beta is equivalent to (alpha implies beta) and (beta implies alpha). This breaks down a biconditional into two implications.
    • De Morgan’s Laws:not (alpha and beta) is equivalent to not alpha or not beta. The negation of a conjunction is the disjunction of the negations.
    • not (alpha or beta) is equivalent to not alpha and not beta. The negation of a disjunction is the conjunction of the negations.

    4. Describe the conjunctive normal form (CNF) and explain the steps to convert a logical formula into CNF.

    Conjunctive Normal Form (CNF) is a standard logical format where a sentence is represented as a conjunction (AND) of clauses, and each clause is a disjunction (OR) of literals. A literal is either a propositional symbol or its negation. The steps to convert a formula to CNF are:

    1. Eliminate Biconditionals: Replace all alpha <-> beta with (alpha -> beta) ^ (beta -> alpha).
    2. Eliminate Implications: Replace all alpha -> beta with ~alpha v beta.
    3. Move Negations Inwards: Use De Morgan’s laws to move negations inward, so they apply only to literals (e.g., ~ (alpha ^ beta) becomes ~alpha v ~beta).
    4. Distribute ORs over ANDs: Use the distributive law to transform the expression into a conjunction of clauses (e.g., alpha v (beta ^ gamma) becomes (alpha v beta) ^ (alpha v gamma)).

    5. Explain the resolution inference rule and the resolution algorithm for proving entailment. What is “inference by resolution,” and how does the empty clause relate to contradiction?

    The resolution inference rule states that if you have two clauses, alpha OR beta and ~alpha OR gamma, you can infer beta OR gamma. It essentially eliminates a complementary pair of literals (alpha and ~alpha) and combines the remaining literals into a new clause. “Inference by resolution” uses this rule repeatedly to derive new clauses.

    The resolution algorithm for proving entailment involves:

    1. Negate the query: To prove KB entails alpha, assume ~alpha.
    2. Convert to CNF: Convert KB AND ~alpha into CNF.
    3. Resolution Loop: Repeatedly apply the resolution rule to pairs of clauses in the CNF formula. Add any new clauses generated back into the set of clauses. If factoring is needed, remove any duplicate literals in resulting clause.
    4. Check for Empty Clause: If, at any point, you derive the “empty clause” (a clause with no literals, representing “false”), this means you’ve found a contradiction.
    5. Determine Entailment: If you derive the empty clause, then KB entails alpha (because KB AND ~alpha leads to a contradiction, so it must be the case that if KB is true, then alpha must be true). If you can no longer derive new clauses and haven’t found the empty clause, then KB does not entail alpha.

    The empty clause signifies a contradiction because it represents a situation where both P and NOT P are true, which is impossible. Finding the empty clause through resolution proves that the initial assumption (the negated query) was inconsistent with the knowledge base.

    6. Explain the inclusion-exclusion principle and the marginalization rule in probability theory, providing examples of their application.

    • Inclusion-Exclusion Principle: This principle calculates the probability of A OR B. The formula is: P(A or B) = P(A) + P(B) – P(A and B). It is used to correct for over counting when calculating P(A or B).
    • Example: The probability of rolling a 6 on a red die (A) OR a 6 on a blue die (B). If you just add P(A) + P(B), you’re double-counting the case where both dice show 6. Subtracting P(A and B) (the probability of both dice showing 6) corrects for this.
    • Marginalization Rule: This rule allows you to calculate the probability of one variable (A) by summing over all possible values of another variable (B). The formula is: P(A) = Σ P(A and B).
    • Example: Probability of it being cloudy (A), given the joint distribution of cloudiness and raininess (B). We calculate P(cloudy) by summing P(cloudy and rainy) + P(cloudy and not rainy). We consider all possible cases that take place, and then look at the probability that the probability of A happens in each of the cases. This is useful for finding an individual (unconditional) probability from a joint probability distribution.

    7. Describe the hill climbing algorithm, including its pseudocode, and discuss its limitations (local optima). Also explain variations like stochastic hill climbing and random restart hill climbing.

    The hill climbing algorithm is a local search technique used to find a maximum (or minimum) of a function. Its pseudocode is as follows:

    1. Start with a current state (often random).
    2. Loop: a. Find the neighbor of the current state with the highest (or lowest) value. b. If the neighbor is better than the current state, move to the neighbor ( current = neighbor). c. If the neighbor is not better, terminate and return the current state.

    A major limitation of hill climbing is that it can get stuck in local optima: points that are better than their immediate neighbors but not the best overall solution.

    Variations:

    • Stochastic Hill Climbing: Randomly choose a neighbor with a better value, rather than always picking the best neighbor. This can help escape plateaus (areas of the search space with relatively equal value), but not always a local optimum.
    • Random Restart Hill Climbing: Run the hill climbing algorithm multiple times from different random starting states. Keep track of the best solution found across all runs. This increases the chance of finding the global optimum by exploring different regions of the search space.

    8. Explain the simulated annealing algorithm and how it can potentially escape local optima compared to simple hill climbing.

    Simulated Annealing is a metaheuristic optimization algorithm that can be used for finding the global minimum of a function that may possess several local minima. Simulated Annealing works by first randomly picking a state. Then the algorithm calculates the cost of the state and then makes a neighbor of the state to calculate that cost as well. If the neighbor cost is better, than the new current state becomes the new neighbor. However, simulated annealing adds a twist. Even if the neighbor cost is not better than the current state, you still have a probability of setting the current state to the new worse neighbor to try and dislodge yourself.

    This probability is based on a temperature. At the beginning, the temperature is high so there is a better probability to dislodge yourself and explore the search space even if it may lead to worse results at first. As the algorithm iterates, the temperature starts to go down, so it slowly starts to look for better neighbors instead of just exploring and dislodging.

    Simulated Annealing is thus better than simple hill climbing because simple hill climbing never goes to a state that may lead to worse results, so as a result gets stuck in local optima as described in the hill climbing algorithm, which SA doesn’t suffer from.

    Supervised Learning: Classification, Regression, and Evaluation

    Supervised learning is a type of machine learning where a computer is given access to a dataset of input-output pairs and learns a function that maps inputs to outputs. The computer uses the data to train its model and understand the relationships between inputs and outputs. The goal is for the AI to learn to predict outputs based on new input data.

    Key aspects of supervised learning:

    • Input-output pairs: The computer is provided with a dataset where each data point consists of an input and a corresponding desired output.
    • Function mapping: The goal is to find a function that accurately maps inputs to outputs, allowing the computer to make predictions on new, unseen data.
    • Training: The computer uses the provided data to train its model, adjusting its internal parameters to minimize the difference between its predictions and the actual outputs.

    Classification and regression are two common tasks within supervised learning.

    • Classification: Aims to map inputs into discrete categories. An example would be classifying a banknote as authentic or counterfeit based on its features.
    • Regression: Aims to predict continuous output values. For example, predicting sales based on advertising spending.

    Implementation and evaluation

    • Libraries such as Scikit-learn in Python provide tools to implement supervised learning algorithms.
    • The data is typically split into training and testing sets. The model is trained on the training set and evaluated on the testing set to assess its ability to generalize to new data.
    • Holdout cross-validation splits the data into training and testing sets. The training set trains the machine learning model. The testing set tests how well the machine learning model performs.
    • K-fold cross-validation divides data into k different sets and runs k different experiments.

    Machine Learning: Algorithms, Techniques, and Applications

    Machine learning involves enabling computers to learn from data and experiences without explicit instructions. Instead of programming a computer with explicit rules, machine learning allows the computer to learn patterns from data and improve its performance on a specific task.

    Key aspects of machine learning:

    • Learning from Data: Machine learning algorithms use data to identify patterns, make predictions, and improve decision-making.
    • Algorithms and Techniques: Machine learning encompasses a wide range of algorithms and techniques that enable computers to learn from data.
    • Pattern Recognition: Machine learning algorithms identify underlying patterns and relationships within data.

    Machine learning comes in different forms, including supervised learning, reinforcement learning and unsupervised learning.

    • Supervised learning involves training a model on a labeled dataset consisting of input-output pairs, enabling the model to learn a function that maps inputs to outputs.
    • Reinforcement learning involves training an agent to make decisions in an environment to maximize a reward signal.
    • Unsupervised learning involves discovering patterns and relationships in unlabeled data without explicit guidance. Clustering is a task preformed in unsupervised learning that involves organizing a set of objects into distinct clusters or groups of similar objects.

    Neural networks are a popular tool in machine learning inspired by the structure of the human brain and can be very effective at certain tasks. A neural network is a mathematical model for learning inspired by biological neural networks. Artificial neural networks can model mathematical functions and learn network parameters.

    TensorFlow is a library that can be used for creating neural networks, modeling them, and running them on sample data.

    Machine learning has a wide variety of applications including: recognizing faces in photos, playing games, understanding human language, spam detection, search and optimization problems, and more.

    Neural Networks: Models, Training, and Applications

    Neural networks are a popular tool in modern machine learning that draw inspiration from the way human brains learn and reason. They are a type of model that is effective at learning from some set of input data to figure out how to calculate some function from inputs to outputs.

    Key aspects of neural networks:

    • Mathematical Model: A neural network is a mathematical model for learning inspired by biological neural networks.
    • Units: Instead of biological neurons, neural networks use units inside of the network. The units can be represented like nodes in a graph.
    • Layers: Neural networks are composed of multiple layers of interconnected nodes or units, including an input layer, one or more hidden layers, and an output layer.
    • Weights: Connections between units are defined by weights. The weights determine how signals are passed between connected nodes.
    • Activation Functions: Activation functions introduce non-linearity into the network, allowing it to learn complex patterns and relationships in the data.
    • Backpropagation: Backpropagation is a key algorithm that makes training multi-layered neural networks possible. The backpropagation algorithm is used to adjust the weights in the network during training to minimize the difference between predicted and actual outputs.
    • Versatility: Neural networks are versatile tools applicable to a number of domains.

    There are different types of neural networks, each designed for specific tasks:

    • Feed-forward neural networks have connections that only move in one direction. The inputs pass through hidden layers and ultimately produce an output.
    • Convolutional neural networks (CNNs) are designed for processing grid-like data, such as images. CNNs apply convolutional layers and pooling layers to extract features from images.
    • Recurrent neural networks (RNNs) are designed for processing sequential data, such as text or time series. RNNs have connections that loop back into themselves, allowing them to maintain a hidden state that captures information about the sequence. Long short-term memory (LSTM) neural network is a popular type of RNN.

    Training Neural Networks:

    • Gradient descent is a technique used to train neural networks by minimizing a loss function. Gradient descent involves iteratively adjusting the weights of the network based on the gradient of the loss function with respect to the weights.
    • Stochastic gradient descent randomly chooses one data point at a time to calculate the gradient based on, instead of calculating it based on all of the data points.
    • Mini-batch gradient descent divides the data set up into small batches, groups of data points, to calculate the gradient based on.
    • Overfitting occurs when a neural network is too complex and fits the training data too closely, resulting in poor generalization to new data.
    • Dropout is a technique used to combat overfitting by randomly removing units from the neural network during training.

    TensorFlow is a library that can be used for creating neural networks, modeling them, and running them on sample data.

    Understanding Gradient Descent in Neural Networks

    Gradient descent is an algorithm inspired by calculus for minimizing loss when training a neural network. In the context of neural networks, “loss” refers to how poorly a hypothesis function models data.

    Key aspects of gradient descent:

    • Loss Function: Gradient descent aims to minimize a loss function, which quantifies how poorly the neural network performs.
    • Gradient Calculation: The algorithm calculates the gradient of the loss function with respect to the network’s weights. The gradient indicates the direction in which the weights should be adjusted to reduce the loss.
    • Weight Update: The weights are updated by taking a small step in the direction opposite to the gradient. The size of this step can vary and is chosen when training the neural network.
    • Iterative Process: This process is repeated iteratively, adjusting the weights little by little based on the data points, with the aim of converging towards a good solution.

    There are variations to the standard gradient descent algorithm:

    • Stochastic Gradient Descent: Instead of looking at all data points at once, stochastic gradient descent randomly chooses one data point at a time to calculate the gradient. This provides a less accurate gradient estimate but is faster to compute.
    • Mini-Batch Gradient Descent: This approach is a middle ground between standard and stochastic gradient descent, where the data set is divided into small batches and the gradient is calculated based on these batches.

    Understanding Neural Network Hidden Layers

    Hidden layers are intermediate layers of artificial neurons or units within a neural network between the input layer and the output layer.

    Here’s more about hidden layers and how they contribute to neural network functionality:

    • Structure and Function In a neural network, the input layer receives the initial data, and the output layer produces the final result. The hidden layers lie in between, performing complex transformations on the input data to help the network learn non-linear relationships.
    • Nodes and Connections Each hidden layer contains a certain number of nodes or units, each connected to the nodes in the preceding and following layers. The connections between nodes have weights, which are adjusted during training to optimize the network’s performance.
    • Activation Each unit calculates its output value based on a linear combination of all the inputs. The advantage of layering like this gives an ability to model more complex functions.

    Backpropagation: One of the challenges of neural networks is training neural networks that have hidden layers inside of them. The input data provides values for all of the inputs, and what the value of the output should be. However, the input data does not provide what the values for all of the nodes in the hidden layer should be. The key algorithm that makes training the hidden layers of neural networks possible is called backpropagation.

    Deep Neural Networks: Neural networks that contain multiple hidden layers are called deep neural networks. The presence of multiple hidden layers allows the network to model more complex functions. Each layer can learn different features of the input, and these features can be combined to produce the desired output. However, complex networks are at greater risk of overfitting.

    Dropout: Dropout is a technique that can combat overfitting in neural networks. It involves temporarily removing units from the network during training to prevent over-reliance on any single node.

    Harvard CS50’s Artificial Intelligence with Python – Full University Course

    The Original Text

    This course from Harvard University explores the concepts and algorithms at the foundation of modern artificial intelligence, diving into the ideas that give rise to technologies like game-playing engines, handwriting recognition, and machine translation. You’ll gain exposure to the theory behind graph search algorithms, classification, optimization, reinforcement learning, and other topics in artificial intelligence and machine learning. Brian Yu teaches this course. Hello, world. This is CS50, and this is an introduction to artificial intelligence with Python with CS50’s own Brian Yu. This course picks up where CS50 itself leaves off and explores the concepts and algorithms at the foundation of modern AI. We’ll start with a look at how AI can search for solutions to problems, whether those problems are learning how to play a game or trying to find driving directions to a destination. We’ll then look at how AI can represent information, both knowledge that our AI is certain about, but also information and events about which our AI might be uncertain, learning how to represent that information, but more importantly, how to use that information to draw inferences and new conclusions as well. We’ll explore how AI can solve various types of optimization problems, trying to maximize profits or minimize costs or satisfy some other constraints before turning our attention to the fast-growing field of machine learning, where we won’t tell our AI exactly how to solve a problem, but instead, give our AI access to data and experiences so that our AI can learn on its own how to perform these tasks. In particular, we’ll look at neural networks, one of the most popular tools in modern machine learning, inspired by the way that human brains learn and reason as well before finally taking a look at the world of natural language processing so that it’s not just us humans learning to learn how artificial intelligence is able to speak, but also AI learning how to understand and interpret human language as well. We’ll explore these ideas and algorithms, and along the way, give you the opportunity to build your own AI programs to implement all of this and more. This is CS50. All right. Welcome, everyone, to an introduction to artificial intelligence with Python. My name is Brian Yu, and in this class, we’ll explore some of the ideas and techniques and algorithms that are at the foundation of artificial intelligence. Now, artificial intelligence covers a wide variety of types of techniques. Anytime you see a computer do something that appears to be intelligent or rational in some way, like recognizing someone’s face in a photo, or being able to play a game better than people can, or being able to understand human language when we talk to our phones and they understand what we mean and are able to respond back to us, these are all examples of AI, or artificial intelligence. And in this class, we’ll explore some of the ideas that make that AI possible. So we’ll begin our conversations with search, the problem of we have an AI, and we would like the AI to be able to search for solutions to some kind of problem, no matter what that problem might be. Whether it’s trying to get driving directions from point A to point B, or trying to figure out how to play a game, given a tic-tac-toe game, for example, figuring out what move it ought to make. After that, we’ll take a look at knowledge. Ideally, we want our AI to be able to know information, to be able to represent that information, and more importantly, to be able to draw inferences from that information, to be able to use the information it knows and draw additional conclusions. So we’ll talk about how AI can be programmed in order to do just that. Then we’ll explore the topic of uncertainty, talking about ideas of what happens if a computer isn’t sure about a fact, but maybe is only sure with a certain probability. So we’ll talk about some of the ideas behind probability, and how computers can begin to deal with uncertain events in order to be a little bit more intelligent in that sense as well. After that, we’ll turn our attention to optimization, problems of when the computer is trying to optimize for some sort of goal, especially in a situation where there might be multiple ways that a computer might solve a problem, but we’re looking for a better way, or potentially the best way, if that’s at all possible. Then we’ll take a look at machine learning, or learning more generally, and looking at how, when we have access to data, our computers can be programmed to be quite intelligent by learning from data and learning from experience, being able to perform a task better and better based on greater access to data. So your email, for example, where your email inbox somehow knows which of your emails are good emails and which of your emails are spam. These are all examples of computers being able to learn from past experiences and past data. We’ll take a look, too, at how computers are able to draw inspiration from human intelligence, looking at the structure of the human brain, and how neural networks can be a computer analog to that sort of idea, and how, by taking advantage of a certain type of structure of a computer program, we can write neural networks that are able to perform tasks very, very effectively. And then finally, we’ll turn our attention to language, not programming languages, but human languages that we speak every day. And taking a look at the challenges that come about as a computer tries to understand natural language, and how it is some of the natural language processing that occurs in modern artificial intelligence can actually work. But today, we’ll begin our conversation with search, this problem of trying to figure out what to do when we have some sort of situation that the computer is in, some sort of environment that an agent is in, so to speak, and we would like for that agent to be able to somehow look for a solution to that problem. Now, these problems can come in any number of different types of formats. One example, for instance, might be something like this classic 15 puzzle with the sliding tiles that you might have seen. Where you’re trying to slide the tiles in order to make sure that all the numbers line up in order. This is an example of what you might call a search problem. The 15 puzzle begins in an initially mixed up state, and we need some way of finding moves to make in order to return the puzzle to its solved state. But there are similar problems that you can frame in other ways. Trying to find your way through a maze, for example, is another example of a search problem. You begin in one place, you have some goal of where you’re trying to get to, and you need to figure out the correct sequence of actions that will take you from that initial state to the goal. And while this is a little bit abstract, any time we talk about maze solving in this class, you can translate it to something a little more real world. Something like driving directions. If you ever wonder how Google Maps is able to figure out what is the best way for you to get from point A to point B, and what turns to make at what time, depending on traffic, for example, it’s often some sort of search algorithm. You have an AI that is trying to get from an initial position to some sort of goal by taking some sequence of actions. So we’ll start our conversations today by thinking about these types of search problems and what goes in to solving a search problem like this in order for an AI to be able to find a good solution. In order to do so, though, we’re going to need to introduce a little bit of terminology, some of which I’ve already used. But the first term we’ll need to think about is an agent. An agent is just some entity that perceives its environment. It somehow is able to perceive the things around it and act on that environment in some way. So in the case of the driving directions, your agent might be some representation of a car that is trying to figure out what actions to take in order to arrive at a destination. In the case of the 15 puzzle with the sliding tiles, the agent might be the AI or the person that is trying to solve that puzzle to try and figure out what tiles to move in order to get to that solution. Next, we introduce the idea of a state. A state is just some configuration of the agent in its environment. So in the 15 puzzle, for example, any state might be any one of these three, for example. A state is just some configuration of the tiles. And each of these states is different and is going to require a slightly different solution. A different sequence of actions will be needed in each one of these in order to get from this initial state to the goal, which is where we’re trying to get. So the initial state, then, what is that? The initial state is just the state where the agent begins. It is one such state where we’re going to start from. And this is going to be the starting point for our search algorithm, so to speak. We’re going to begin with this initial state and then start to reason about it, to think about what actions might we apply to that initial state in order to figure out how to get from the beginning to the end, from the initial position to whatever our goal happens to be. And how do we make our way from that initial position to the goal? Well, ultimately, it’s via taking actions. Actions are just choices that we can make in any given state. And in AI, we’re always going to try to formalize these ideas a little bit more precisely, such that we could program them a little bit more mathematically, so to speak. So this will be a recurring theme. And we can more precisely define actions as a function. We’re going to effectively define a function called actions that takes an input, s, where s is going to be some state that exists inside of our environment. And actions of s is going to take the state as input and return as output the set of all actions that can be executed in that state. And so it’s possible that some actions are only valid in certain states and not in other states. And we’ll see examples of that soon, too. So in the case of the 15 puzzle, for example, there are generally going to be four possible actions that we can do most of the time. We can slide a tile to the right, slide a tile to the left, slide a tile up, or slide a tile down, for example. And those are going to be the actions that are available to us. So somehow our AI, our program, needs some encoding of the state, which is often going to be in some numerical format, and some encoding of these actions. But it also needs some encoding of the relationship between these things. How do the states and actions relate to one another? And in order to do that, we’ll introduce to our AI a transition model, which will be a description of what state we get after we perform some available action in some other state. And again, we can be a little bit more precise about this, define this transition model a little bit more formally, again, as a function. The function is going to be a function called result that this time takes two inputs. Input number one is s, some state. And input number two is a, some action. And the output of this function result is it is going to give us the state that we get after we perform action a in state s. So let’s take a look at an example to see more precisely what this actually means. Here is an example of a state, of the 15 puzzle, for example. And here is an example of an action, sliding a tile to the right. What happens if we pass these as inputs to the result function? Again, the result function takes this board, this state, as its first input. And it takes an action as a second input. And of course, here, I’m describing things visually so that you can see visually what the state is and what the action is. In a computer, you might represent one of these actions as just some number that represents the action. Or if you’re familiar with enums that allow you to enumerate multiple possibilities, it might be something like that. And this state might just be represented as an array or two-dimensional array of all of these numbers that exist. But here, we’re going to show it visually just so you can see it. But when we take this state and this action, pass it into the result function, the output is a new state. The state we get after we take a tile and slide it to the right, and this is the state we get as a result. If we had a different action and a different state, for example, and pass that into the result function, we’d get a different answer altogether. So the result function needs to take care of figuring out how to take a state and take an action and get what results. And this is going to be our transition model that describes how it is that states and actions are related to each other. If we take this transition model and think about it more generally and across the entire problem, we can form what we might call a state space. The set of all of the states we can get from the initial state via any sequence of actions, by taking 0 or 1 or 2 or more actions in addition to that, so we could draw a diagram that looks something like this, where every state is represented here by a game board, and there are arrows that connect every state to every other state we can get to from that state. And the state space is much larger than what you see just here. This is just a sample of what the state space might actually look like. And in general, across many search problems, whether they’re this particular 15 puzzle or driving directions or something else, the state space is going to look something like this. We have individual states and arrows that are connecting them. And oftentimes, just for simplicity, we’ll simplify our representation of this entire thing as a graph, some sequence of nodes and edges that connect nodes. But you can think of this more abstract representation as the exact same idea. Each of these little circles or nodes is going to represent one of the states inside of our problem. And the arrows here represent the actions that we can take in any particular state, taking us from one particular state to another state, for example. All right. So now we have this idea of nodes that are representing these states, actions that can take us from one state to another, and a transition model that defines what happens after we take a particular action. So the next step we need to figure out is how we know when the AI is done solving the problem. The AI needs some way to know when it gets to the goal that it’s found the goal. So the next thing we’ll need to encode into our artificial intelligence is a goal test, some way to determine whether a given state is a goal state. In the case of something like driving directions, it might be pretty easy. If you’re in a state that corresponds to whatever the user typed in as their intended destination, well, then you know you’re in a goal state. In the 15 puzzle, it might be checking the numbers to make sure they’re all in ascending order. But the AI needs some way to encode whether or not any state they happen to be in is a goal. And some problems might have one goal, like a maze where you have one initial position and one ending position, and that’s the goal. In other more complex problems, you might imagine that there are multiple possible goals. That there are multiple ways to solve a problem, and we might not care which one the computer finds, as long as it does find a particular goal. However, sometimes the computer doesn’t just care about finding a goal, but finding a goal well, or one with a low cost. And it’s for that reason that the last piece of terminology that we’ll use to define these search problems is something called a path cost. You might imagine that in the case of driving directions, it would be pretty annoying if I said I wanted directions from point A to point B, and the route that Google Maps gave me was a long route with lots of detours that were unnecessary that took longer than it should have for me to get to that destination. And it’s for that reason that when we’re formulating search problems, we’ll often give every path some sort of numerical cost, some number telling us how expensive it is to take this particular option, and then tell our AI that instead of just finding a solution, some way of getting from the initial state to the goal, we’d really like to find one that minimizes this path cost. That is, less expensive, or takes less time, or minimizes some other numerical value. We can represent this graphically if we take a look at this graph again, and imagine that each of these arrows, each of these actions that we can take from one state to another state, has some sort of number associated with it. That number being the path cost of this particular action, where some of the costs for any particular action might be more expensive than the cost for some other action, for example. Although this will only happen in some sorts of problems. In other problems, we can simplify the diagram and just assume that the cost of any particular action is the same. And this is probably the case in something like the 15 puzzle, for example, where it doesn’t really make a difference whether I’m moving right or moving left. The only thing that matters is the total number of steps that I have to take to get from point A to point B. And each of those steps is of equal cost. We can just assume it’s of some constant cost like one. And so this now forms the basis for what we might consider to be a search problem. A search problem has some sort of initial state, some place where we begin, some sort of action that we can take or multiple actions that we can take in any given state. And it has a transition model. Some way of defining what happens when we go from one state and take one action, what state do we end up with as a result. In addition to that, we need some goal test to know whether or not we’ve reached a goal. And then we need a path cost function that tells us for any particular path, by following some sequence of actions, how expensive is that path. What does its cost in terms of money or time or some other resource that we are trying to minimize our usage of. And the goal ultimately is to find a solution. Where a solution in this case is just some sequence of actions that will take us from the initial state to the goal state. And ideally, we’d like to find not just any solution but the optimal solution, which is a solution that has the lowest path cost among all of the possible solutions. And in some cases, there might be multiple optimal solutions. But an optimal solution just means that there is no way that we could have done better in terms of finding that solution. So now we’ve defined the problem. And now we need to begin to figure out how it is that we’re going to solve this kind of search problem. And in order to do so, you’ll probably imagine that our computer is going to need to represent a whole bunch of data about this particular problem. We need to represent data about where we are in the problem. And we might need to be considering multiple different options at once. And oftentimes, when we’re trying to package a whole bunch of data related to a state together, we’ll do so using a data structure that we’re going to call a node. A node is a data structure that is just going to keep track of a variety of different values. And specifically, in the case of a search problem, it’s going to keep track of these four values in particular. Every node is going to keep track of a state, the state we’re currently on. And every node is also going to keep track of a parent. A parent being the state before us or the node that we used in order to get to this current state. And this is going to be relevant because eventually, once we reach the goal node, once we get to the end, we want to know what sequence of actions we use in order to get to that goal. And the way we’ll know that is by looking at these parents to keep track of what led us to the goal and what led us to that state and what led us to the state before that, so on and so forth, backtracking our way to the beginning so that we know the entire sequence of actions we needed in order to get from the beginning to the end. The node is also going to keep track of what action we took in order to get from the parent to the current state. And the node is also going to keep track of a path cost. In other words, it’s going to keep track of the number that represents how long it took to get from the initial state to the state that we currently happen to be at. And we’ll see why this is relevant as we start to talk about some of the optimizations that we can make in terms of these search problems more generally. So this is the data structure that we’re going to use in order to solve the problem. And now let’s talk about the approach. How might we actually begin to solve the problem? Well, as you might imagine, what we’re going to do is we’re going to start at one particular state, and we’re just going to explore from there. The intuition is that from a given state, we have multiple options that we could take, and we’re going to explore those options. And once we explore those options, we’ll find that more options than that are going to make themselves available. And we’re going to consider all of the available options to be stored inside of a single data structure that we’ll call the frontier. The frontier is going to represent all of the things that we could explore next that we haven’t yet explored or visited. So in our approach, we’re going to begin the search algorithm by starting with a frontier that just contains one state. The frontier is going to contain the initial state, because at the beginning, that’s the only state we know about. That is the only state that exists. And then our search algorithm is effectively going to follow a loop. We’re going to repeat some process again and again and again. The first thing we’re going to do is if the frontier is empty, then there’s no solution. And we can report that there is no way to get to the goal. And that’s certainly possible. There are certain types of problems that an AI might try to explore and realize that there is no way to solve that problem. And that’s useful information for humans to know as well. So if ever the frontier is empty, that means there’s nothing left to explore. And we haven’t yet found a solution, so there is no solution. There’s nothing left to explore. Otherwise, what we’ll do is we’ll remove a node from the frontier. So right now at the beginning, the frontier just contains one node representing the initial state. But over time, the frontier might grow. It might contain multiple states. And so here, we’re just going to remove a single node from that frontier. If that node happens to be a goal, then we found a solution. So we remove a node from the frontier and ask ourselves, is this the goal? And we do that by applying the goal test that we talked about earlier, asking if we’re at the destination. Or asking if all the numbers of the 15 puzzle happen to be in order. So if the node contains the goal, we found a solution. Great. We’re done. And otherwise, what we’ll need to do is we’ll need to expand the node. And this is a term of art in artificial intelligence. To expand the node just means to look at all of the neighbors of that node. In other words, consider all of the possible actions that I could take from the state that this node is representing and what nodes could I get to from there. We’re going to take all of those nodes, the next nodes that I can get to from this current one I’m looking at, and add those to the frontier. And then we’ll repeat this process. So at a very high level, the idea is we start with a frontier that contains the initial state. And we’re constantly removing a node from the frontier, looking at where we can get to next and adding those nodes to the frontier, repeating this process over and over until either we remove a node from the frontier and it contains a goal, meaning we’ve solved the problem, or we run into a situation where the frontier is empty, at which point we’re left with no solution. So let’s actually try and take the pseudocode, put it into practice by taking a look at an example of a sample search problem. So right here, I have a sample graph. A is connected to B via this action. B is connected to nodes C and D. C is connected to E. D is connected to F. And what I’d like to do is have my AI find a path from A to E. We want to get from this initial state to this goal state. So how are we going to do that? Well, we’re going to start with a frontier that contains the initial state. This is going to represent our frontier. So our frontier initially will just contain A, that initial state where we’re going to begin. And now we’ll repeat this process. If the frontier is empty, no solution. That’s not a problem, because the frontier is not empty. So we’ll remove a node from the frontier as the one to consider next. There’s only one node in the frontier. So we’ll go ahead and remove it from the frontier. But now A, this initial node, this is the node we’re currently considering. We follow the next step. We ask ourselves, is this node the goal? No, it’s not. A is not the goal. E is the goal. So we don’t return the solution. So instead, we go to this last step, expand the node, and add the resulting nodes to the frontier. What does that mean? Well, it means take this state A and consider where we could get to next. And after A, what we could get to next is only B. So that’s what we get when we expand A. We find B. And we add B to the frontier. And now B is in the frontier. And we repeat the process again. We say, all right, the frontier is not empty. So let’s remove B from the frontier. B is now the node that we’re considering. We ask ourselves, is B the goal? No, it’s not. So we go ahead and expand B and add its resulting nodes to the frontier. What happens when we expand B? In other words, what nodes can we get to from B? Well, we can get to C and D. So we’ll go ahead and add C and D from the frontier. And now we have two nodes in the frontier, C and D. And we repeat the process again. We remove a node from the frontier. For now, I’ll do so arbitrarily just by picking C. We’ll see why later, how choosing which node you remove from the frontier is actually quite an important part of the algorithm. But for now, I’ll arbitrarily remove C, say it’s not the goal. So we’ll add E, the next one, to the frontier. Then let’s say I remove E from the frontier. And now I check I’m currently looking at state E. Is it a goal state? It is, because I’m trying to find a path from A to E. So I would return the goal. And that now would be the solution, that I’m now able to return the solution. And I have found a path from A to E. So this is the general idea, the general approach of this search algorithm, to follow these steps, constantly removing nodes from the frontier, until we’re able to find a solution. So the next question you might reasonably ask is, what could go wrong here? What are the potential problems with an approach like this? And here’s one example of a problem that could arise from this sort of approach. Imagine this same graph, same as before, with one change. The change being now, instead of just an arrow from A to B, we also have an arrow from B to A, meaning we can go in both directions. And this is true in something like the 15 puzzle, where when I slide a tile to the right, I could then slide a tile to the left to get back to the original position. I could go back and forth between A and B. And that’s what these double arrows symbolize, the idea that from one state, I can get to another, and then I can get back. And that’s true in many search problems. What’s going to happen if I try to apply the same approach now? Well, I’ll begin with A, same as before. And I’ll remove A from the frontier. And then I’ll consider where I can get to from A. And after A, the only place I can get to is B. So B goes into the frontier. Then I’ll say, all right, let’s take a look at B. That’s the only thing left in the frontier. Where can I get to from B? Before, it was just C and D. But now, because of that reverse arrow, I can get to A or C or D. So all three, A, C, and D, all of those now go into the frontier. They are places I can get to from B. And now I remove one from the frontier. And maybe I’m unlucky, and maybe I pick A. And now I’m looking at A again. And I consider, where can I get to from A? And from A, well, I can get to B. And now we start to see the problem. But if I’m not careful, I go from A to B, and then back to A, and then to B again. And I could be going in this infinite loop, where I never make any progress, because I’m constantly just going back and forth between two states that I’ve already seen. So what is the solution to this? We need some way to deal with this problem. And the way that we can deal with this problem is by somehow keeping track of what we’ve already explored. And the logic is going to be, well, if we’ve already explored the state, there’s no reason to go back to it. Once we’ve explored a state, don’t go back to it. Don’t bother adding it to the frontier. There’s no need to. So here’s going to be our revised approach, a better way to approach this sort of search problem. And it’s going to look very similar, just with a couple of modifications. We’ll start with a frontier that contains the initial state, same as before. But now we’ll start with another data structure, which will just be a set of nodes that we’ve already explored. So what are the states we’ve explored? Initially, it’s empty. We have an empty explored set. And now we repeat. If the frontier is empty, no solution, same as before. We remove a node from the frontier. We check to see if it’s a goal state, return the solution. None of this is any different so far. But now what we’re going to do is we’re going to add the node to the explored state. So if it happens to be the case that we remove a node from the frontier and it’s not the goal, we’ll add it to the explored set so that we know we’ve already explored it. We don’t need to go back to it again if it happens to come up later. And then the final step, we expand the node and we add the resulting nodes to the frontier. But before, we just always added the resulting nodes to the frontier. We’re going to be a little clever about it this time. We’re only going to add the nodes to the frontier if they aren’t already in the frontier and if they aren’t already in the explored set. So we’ll check both the frontier and the explored set, make sure that the node isn’t already in one of those two. And so long as it isn’t, then we’ll go ahead and add it to the frontier, but not otherwise. And so that revised approach is ultimately what’s going to help make sure that we don’t go back and forth between two nodes. Now, the one point that I’ve kind of glossed over here so far is this step here, removing a node from the frontier. Before, I just chose arbitrarily. Like, let’s just remove a node and that’s it. But it turns out it’s actually quite important how we decide to structure our frontier, how we add and how we remove our nodes. The frontier is a data structure and we need to make a choice about in what order are we going to be removing elements. And one of the simplest data structures for adding and removing elements is something called a stack. And a stack is a data structure that is a last in, first out data type, which means the last thing that I add to the frontier is going to be the first thing that I remove from the frontier. So the most recent thing to go into the stack or the frontier in this case is going to be the node that I explore. So let’s see what happens if I apply this stack-based approach to something like this problem, finding a path from A to E. What’s going to happen? Well, again, we’ll start with A and we’ll say, all right, let’s go ahead and look at A first. And then notice this time, we’ve added A to the explored set. A is something we’ve now explored. We have this data structure that’s keeping track. We then say from A, we can get to B. And all right, from B, what can we do? Well, from B, we can explore B and get to both C and D. So we added C and then D. So now, when we explore a node, we’re going to treat the frontier as a stack, last in, first out. D was the last one to come in. So we’ll go ahead and explore that next and say, all right, where can we get to from D? Well, we can get to F. And so all right, we’ll put F into the frontier. And now, because the frontier is a stack, F is the most recent thing that’s gone in the stack. So F is what we’ll explore next. We’ll explore F and say, all right, where can we get to from F? Well, we can’t get anywhere, so nothing gets added to the frontier. So now, what was the new most recent thing added to the frontier? Well, it’s now C, the only thing left in the frontier. We’ll explore that from which we can see, all right, from C, we can get to E. So E goes into the frontier. And then we say, all right, let’s look at E. And E is now the solution. And now, we’ve solved the problem. So when we treat the frontier like a stack, a last in, first out data structure, that’s the result we get. We go from A to B to D to F. And then we sort of backed up and went down to C and then E. And it’s important to get a visual sense for how this algorithm is working. We went very deep in this search tree, so to speak, all the way until the bottom where we hit a dead end. And then we effectively backed up and explored this other route that we didn’t try before. And it’s this going very deep in the search tree idea, this way the algorithm ends up working when we use a stack that we call this version of the algorithm depth first search. Depth first search is the search algorithm where we always explore the deepest node in the frontier. We keep going deeper and deeper through our search tree. And then if we hit a dead end, we back up and we try something else instead. But depth first search is just one of the possible search options that we could use. It turns out that there’s another algorithm called breadth first search, which behaves very similarly to depth first search with one difference. Instead of always exploring the deepest node in the search tree, the way the depth first search does, breadth first search is always going to explore the shallowest node in the frontier. So what does that mean? Well, it means that instead of using a stack which depth first search or DFS used, where the most recent item added to the frontier is the one we’ll explore next, in breadth first search or BFS, we’ll instead use a queue, where a queue is a first in first out data type, where the very first thing we add to the frontier is the first one we’ll explore and they effectively form a line or a queue, where the earlier you arrive in the frontier, the earlier you get explored. So what would that mean for the same exact problem, finding a path from A to E? Well, we start with A, same as before, then we’ll go ahead and have explored A and say, where can we get to from A? Well, from A, we can get to B, same as before. From B, same as before, we can get to C and D. So C and D get added to the frontier. This time, though, we added C to the frontier before D. So we’ll explore C first. So C gets explored. And from C, where can we get to? Well, we can get to E. So E gets added to the frontier. But because D was explored before E, we’ll look at D next. So we’ll explore D and say, where can we get to from D? We can get to F. And only then will we say, all right, now we can get to E. And so what breadth first search or BFS did is we started here, we looked at both C and D, and then we looked at E. Effectively, we’re looking at things one away from the initial state, then two away from the initial state, and only then, things that are three away from the initial state, unlike depth first search, which just went as deep as possible into the search tree until it hit a dead end and then ultimately had to back up. So these now are two different search algorithms that we could apply in order to try and solve a problem. And let’s take a look at how these would actually work in practice with something like maze solving, for example. So here’s an example of a maze. These empty cells represent places where our agent can move. These darkened gray cells represent walls that the agent can’t pass through. And ultimately, our agent, our AI, is going to try to find a way to get from position A to position B via some sequence of actions, where those actions are left, right, up, and down. What will depth first search do in this case? Well, depth first search will just follow one path. If it reaches a fork in the road where it has multiple different options, depth first search is just, in this case, going to choose one. That doesn’t a real preference. But it’s going to keep following one until it hits a dead end. And when it hits a dead end, depth first search effectively goes back to the last decision point and tries the other path, fully exhausting this entire path. And when it realizes that, OK, the goal is not here, then it turns its attention to this path. It goes as deep as possible. When it hits a dead end, it backs up and then tries this other path, keeps going as deep as possible down one particular path. And when it realizes that that’s a dead end, then it’ll back up, and then ultimately find its way to the goal. And maybe you got lucky, and maybe you made a different choice earlier on. But ultimately, this is how depth first search is going to work. It’s going to keep following until it hits a dead end. And when it hits a dead end, it backs up and looks for a different solution. And so one thing you might reasonably ask is, is this algorithm always going to work? Will it always actually find a way to get from the initial state? To the goal. And it turns out that as long as our maze is finite, as long as there are only finitely many spaces where we can travel, then, yes, depth first search is going to find a solution. Because eventually, it’ll just explore everything. If the maze happens to be infinite and there’s an infinite state space, which does exist in certain types of problems, then it’s a slightly different story. But as long as our maze has finitely many squares, we’re going to find a solution. The next question, though, that we want to ask is, is it going to be a good solution? Is it the optimal solution that we can find? And the answer there is not necessarily. And let’s take a look at an example of that. In this maze, for example, we’re again trying to find our way from A to B. And you notice here there are multiple possible solutions. We could go this way or we could go up in order to make our way from A to B. Now, if we’re lucky, depth first search will choose this way and get to B. But there’s no reason necessarily why depth first search would choose between going up or going to the right. It’s sort of an arbitrary decision point because both are going to be added to the frontier. And ultimately, if we get unlucky, depth first search might choose to explore this path first because it’s just a random choice at this point. It’ll explore, explore, explore. And it’ll eventually find the goal, this particular path, when in actuality there was a better path. There was a more optimal solution that used fewer steps, assuming we’re measuring the cost of a solution based on the number of steps that we need to take. So depth first search, if we’re unlucky, might end up not finding the best solution when a better solution is available. So that’s DFS, depth first search. How does BFS, or breadth first search, compare? How would it work in this particular situation? Well, the algorithm is going to look very different visually in terms of how BFS explores. Because BFS looks at shallower nodes first, the idea is going to be, BFS will first look at all of the nodes that are one away from the initial state. Look here and look here, for example, just at the two nodes that are immediately next to this initial state. Then it’ll explore nodes that are two away, looking at this state and that state, for example. Then it’ll explore nodes that are three away, this state and that state. Whereas depth first search just picked one path and kept following it, breadth first search, on the other hand, is taking the option of exploring all of the possible paths as kind of at the same time bouncing back between them, looking deeper and deeper at each one, but making sure to explore the shallower ones or the ones that are closer to the initial state earlier. So we’ll keep following this pattern, looking at things that are four away, looking at things that are five away, looking at things that are six away, until eventually we make our way to the goal. And in this case, it’s true we had to explore some states that ultimately didn’t lead us anywhere, but the path that we found to the goal was the optimal path. This is the shortest way that we could get to the goal. And so what might happen then in a larger maze? Well, let’s take a look at something like this and how breadth first search is going to behave. Well, breadth first search, again, we’ll just keep following the states until it receives a decision point. It could go either left or right. And while DFS just picked one and kept following that until it hit a dead end, BFS, on the other hand, will explore both. It’ll say look at this node, then this node, and it’ll look at this node, then that node. So on and so forth. And when it hits a decision point here, rather than pick one left or two right and explore that path, it’ll again explore both, alternating between them, going deeper and deeper. We’ll explore here, and then maybe here and here, and then keep going. Explore here and slowly make our way, you can visually see, further and further out. Once we get to this decision point, we’ll explore both up and down until ultimately we make our way to the goal. And what you’ll notice is, yes, breadth first search did find our way from A to B by following this particular path, but it needed to explore a lot of states in order to do so. And so we see some trade offs here between DFS and BFS, that in DFS, there may be some cases where there is some memory savings as compared to a breadth first approach, where breadth first search in this case had to explore a lot of states. But maybe that won’t always be the case. So now let’s actually turn our attention to some code and look at the code that we could actually write in order to implement something like depth first search or breadth first search in the context of solving a maze, for example. So I’ll go ahead and go into my terminal. And what I have here inside of maze.py is an implementation of this same idea of maze solving. I’ve defined a class called node that in this case is keeping track of the state, the parent, in other words, the state before the state, and the action. In this case, we’re not keeping track of the path cost because we can calculate the cost of the path at the end after we found our way from the initial state to the goal. In addition to this, I’ve defined a class called a stack frontier. And if unfamiliar with a class, a class is a way for me to define a way to generate objects in Python. It refers to an idea of object oriented programming, where the idea here is that I would like to create an object that is able to store all of my frontier data. And I would like to have functions, otherwise known as methods, on that object that I can use to manipulate the object. And so what’s going on here, if unfamiliar with the syntax, is I have a function that initially creates a frontier that I’m going to represent using a list. And initially, my frontier is represented by the empty list. There’s nothing in my frontier to begin with. I have an add function that adds something to the frontier as by appending it to the end of the list. I have a function that checks if the frontier contains a particular state. I have an empty function that checks if the frontier is empty. If the frontier is empty, that just means the length of the frontier is 0. And then I have a function for removing something from the frontier. I can’t remove something from the frontier if the frontier is empty, so I check for that first. But otherwise, if the frontier isn’t empty, recall that I’m implementing this frontier as a stack, a last in first out data structure, which means the last thing I add to the frontier, in other words, the last thing in the list, is the item that I should remove from this frontier. So what you’ll see here is I have removed the last item of a list. And if you index into a Python list with negative 1, that gets you the last item in the list. Since 0 is the first item, negative 1 kind of wraps around and gets you to the last item in the list. So we give that the node. We call that node. We update the frontier here on line 28 to say, go ahead and remove that node that you just removed from the frontier. And then we return the node as a result. So this class here effectively implements the idea of a frontier. It gives me a way to add something to a frontier and a way to remove something from the frontier as a stack. I’ve also, just for good measure, implemented an alternative version of the same thing called a queue frontier, which in parentheses you’ll see here, it inherits from a stack frontier, meaning it’s going to do all the same things that the stack frontier did, except the way we remove a node from the frontier is going to be slightly different. Instead of removing from the end of the list the way we would in a stack, we’re instead going to remove from the beginning of the list. Self.frontier 0 will get me the first node in the frontier, the first one that was added, and that is going to be the one that we return in the case of a queue. Then under here, I have a definition of a class called maze. This is going to handle the process of taking a sequence, a maze-like text file, and figuring out how to solve it. So it will take as input a text file that looks something like this, for example, where we see hash marks that are here representing walls, and I have the character A representing the starting position and the character B representing the ending position. And you can take a look at the code for parsing this text file right now. That’s the less interesting part. The more interesting part is this solve function here, the solve function is going to figure out how to actually get from point A to point B. And here we see an implementation of the exact same idea we saw from a moment ago. We’re going to keep track of how many states we’ve explored, just so we can report that data later. But I start with a node that represents just the start state. And I start with a frontier that, in this case, is a stack frontier. And given that I’m treating my frontier as a stack, you might imagine that the algorithm I’m using here is now depth-first search, because depth-first search, or DFS, uses a stack as its data structure. And initially, this frontier is just going to contain the start state. We initialize an explored set that initially is empty. There’s nothing we’ve explored so far. And now here’s our loop, that notion of repeating something again and again. First, we check if the frontier is empty by calling that empty function that we saw the implementation of a moment ago. And if the frontier is indeed empty, we’ll go ahead and raise an exception, or a Python error, to say, sorry, there is no solution to this problem. Otherwise, we’ll go ahead and remove a node from the frontier as by calling frontier.remove and update the number of states we’ve explored, because now we’ve explored one additional state. So we say self.numexplored plus equals 1, adding 1 to the number of states we’ve explored. Once we remove a node from the frontier, recall that the next step is to see whether or not it’s the goal, the goal test. And in the case of the maze, the goal is pretty easy. I check to see whether the state of the node is equal to the goal. Initially, when I set up the maze, I set up this value called goal, which is a property of the maze, so I can just check to see if the node is actually the goal. And if it is the goal, then what I want to do is backtrack my way towards figuring out what actions I took in order to get to this goal. And how do I do that? We’ll recall that every node stores its parent, the node that came before it that we used to get to this node, and also the action used in order to get there. So I can create this loop where I’m constantly just looking at the parent of every node and keeping track for all of the parents what action I took to get from the parent to this current node. So this loop is going to keep repeating this process of looking through all of the parent nodes until we get back to the initial state, which has no parent, where node.parent is going to be equal to none. As I do so, I’m going to be building up the list of all of the actions that I’m following and the list of all the cells that are part of the solution. But I’ll reverse them because when I build it up, going from the goal back to the initial state and building the sequence of actions from the goal to the initial state, but I want to reverse them in order to get the sequence of actions from the initial state to the goal. And that is ultimately going to be the solution. So all of that happens if the current state is equal to the goal. And otherwise, if it’s not the goal, well, then I’ll go ahead and add this state to the explored set to say, I’ve explored this state now. No need to go back to it if I come across it in the future. And then this logic here implements the idea of adding neighbors to the frontier. I’m saying, look at all of my neighbors, and I implemented a function called neighbors that you can take a look at. And for each of those neighbors, I’m going to check, is the state already in the frontier? Is the state already in the explored set? And if it’s not in either of those, then I’ll go ahead and add this new child node, this new node, to the frontier. So there’s a fair amount of syntax here, but the key here is not to understand all the nuances of the syntax. So feel free to take a closer look at this file on your own to get a sense for how it is working. But the key is to see how this is an implementation of the same pseudocode, the same idea that we were describing a moment ago on the screen when we were looking at the steps that we might follow in order to solve this kind of search problem. So now let’s actually see this in action. I’ll go ahead and run maze.py on maze1.txt, for example. And what we’ll see is here, we have a printout of what the maze initially looked like. And then here down below is after we’ve solved it. We had to explore 11 states in order to do it, and we found a path from A to B. And in this program, I just happened to generate a graphical representation of this as well. So I can open up maze.png, which is generated by this program, that shows you where in the darker color here are the walls, red is the initial state, green is the goal, and yellow is the path that was followed. We found a path from the initial state to the goal. But now let’s take a look at a more sophisticated maze to see what might happen instead. Let’s look now at maze2.txt. We’re now here. We have a much larger maze. Again, we’re trying to find our way from point A to point B. But now you’ll imagine that depth-first search might not be so lucky. It might not get the goal on the first try. It might have to follow one path, then backtrack and explore something else a little bit later. So let’s try this. We’ll run python maze.py of maze2.txt, this time trying on this other maze. And now, depth-first search is able to find a solution. Here, as indicated by the stars, is a way to get from A to B. And we can represent this visually by opening up this maze. Here’s what that maze looks like, and highlighted in yellow is the path that was found from the initial state to the goal. But how many states did we have to explore before we found that path? Well, recall that in my program, I was keeping track of the number of states that we’ve explored so far. And so I can go back to the terminal and see that, all right, in order to solve this problem, we had to explore 399 different states. And in fact, if I make one small modification of the program and tell the program at the end when we output this image, I added an argument called show explored. And if I set show explored equal to true and rerun this program, python maze.py, running it on maze2, and then I open the maze, what you’ll see here is highlighted in red are all of the states that had to be explored to get from the initial state to the goal. Depth-first search, or DFS, didn’t find its way to the goal right away. It made a choice to first explore this direction. And when it explored this direction, it had to follow every conceivable path all the way to the very end, even this long and winding one, in order to realize that, you know what? That’s a dead end. And instead, the program needed to backtrack. After going this direction, it must have gone this direction. It got lucky here by just not choosing this path, but it got unlucky here, exploring this direction, exploring a bunch of states it didn’t need to, and then likewise exploring all of this top part of the graph when it probably didn’t need to do that either. So all in all, depth-first search here really not performing optimally, or probably exploring more states than it needs to. It finds an optimal solution, the best path to the goal, but the number of states needed to explore in order to do so, the number of steps I had to take, that was much higher. So let’s compare. How would breadth-first search, or BFS, do on this exact same maze instead? And in order to do so, it’s a very easy change. The algorithm for DFS and BFS is identical with the exception of what data structure we use to represent the frontier, that in DFS, I used a stack frontier, last in, first out, whereas in BFS, I’m going to use a queue frontier, first in, first out, where the first thing I add to the frontier is the first thing that I remove. So I’ll go back to the terminal, rerun this program on the same maze, and now you’ll see that the number of states we had to explore was only 77 as compared to almost 400 when we used depth-first search. And we can see exactly why. We can see what happened if we open up maze.png now and take a look. Again, yellow highlight is the solution that breadth-first search found, which incidentally is the same solution that depth-first search found. They’re both finding the best solution. But notice all the white unexplored cells. There was much fewer states that needed to be explored in order to make our way to the goal because breadth-first search operates a little more shallowly. It’s exploring things that are close to the initial state without exploring things that are further away. So if the goal is not too far away, then breadth-first search can actually behave quite effectively on a maze that looks a little something like this. Now, in this case, both BFS and DFS ended up finding the same solution, but that won’t always be the case. And in fact, let’s take a look at one more example. For instance, maze3.txt. In maze3.txt, notice that here there are multiple ways that you could get from A to B. It’s a relatively small maze, but let’s look at what happens. If I use, and I’ll go ahead and turn off show explored so we just see the solution. If I use BFS, breadth-first search, to solve maze3.txt, well, then we find a solution, and if I open up the maze, here is the solution that we found. It is the optimal one. With just four steps, we can get from the initial state to what the goal happens to be. But what happens if we tried to use depth-first search or DFS instead? Well, again, I’ll go back up to my Q frontier, where Q frontier means that we’re using breadth-first search, and I’ll change it to a stack frontier, which means that now we’ll be using depth-first search. I’ll rerun pythonmaze.py, and now you’ll see that we find the solution, but it is not the optimal solution. This instead is what our algorithm finds, and maybe depth-first search would have found the solution. It’s possible, but it’s not guaranteed that if we just happen to be unlucky, if we choose this state instead of that state, then depth-first search might find a longer route to get from the initial state to the goal. So we do see some trade-offs here, where depth-first search might not find the optimal solution. So at that point, it seems like breadth-first search is pretty good. Is that the best we can do, where it’s going to find us the optimal solution, and we don’t have to worry about situations where we might end up finding a longer path to the solution than what actually exists? Where the goal is far away from the initial state, and we might have to take lots of steps in order to get from the initial state to the goal, what ended up happening is that this algorithm, BFS, ended up exploring basically the entire graph, having to go through the entire maze in order to find its way from the initial state to the goal state. What we’d ultimately like is for our algorithm to be a little bit more intelligent. And now what would it mean for our algorithm to be a little bit more intelligent in this case? Well, let’s look back to where breadth-first search might have been able to make a different decision and consider human intuition in this process as well. What might a human do when solving this maze that is different than what BFS ultimately chose to do? Well, the very first decision point that BFS made was right here, when it made five steps and ended up in a position where it had a fork in the row. It could either go left or it could go right. In these initial couple steps, there was no choice. There was only one action that could be taken from each of those states. And so the search algorithm did the only thing that any search algorithm could do, which is keep following that state after the next state. But this decision point is where things get a little bit interesting. Depth-first search, that very first search algorithm we looked at, chose to say, let’s pick one path and exhaust that path. See if anything that way has the goal. And if not, then let’s try the other way. Depth-first search took the alternative approach of saying, you know what, let’s explore things that are shallow, close to us first. Look left and right, then back left and back right, so on and so forth, alternating between our options in the hopes of finding something nearby. But ultimately, what might a human do if confronted with a situation like this of go left or go right? Well, a human might visually see that, all right, I’m trying to get to state b, which is way up there, and going right just feels like it’s closer to the goal. It feels like going right should be better than going left because I’m making progress towards getting to that goal. Now, of course, there are a couple of assumptions that I’m making here. I’m making the assumption that we can represent this grid as like a two-dimensional grid where I know the coordinates of everything. I know that a is in coordinate 0, 0, and b is in some other coordinate pair, and I know what coordinate I’m at now. So I can calculate that, yeah, going this way, that is closer to the goal. And that might be a reasonable assumption for some types of search problems, but maybe not in others. But for now, we’ll go ahead and assume that, that I know what my current coordinate pair is, and I know the coordinate, x, y, of the goal that I’m trying to get to. And in this situation, I’d like an algorithm that is a little bit more intelligent, that somehow knows that I should be making progress towards the goal, and this is probably the way to do that because in a maze, moving in the coordinate direction of the goal is usually, though not always, a good thing. And so here we draw a distinction between two different types of search algorithms, uninformed search and informed search. Uninformed search algorithms are algorithms like DFS and BFS, the two algorithms that we just looked at, which are search strategies that don’t use any problem-specific knowledge to be able to solve the problem. DFS and BFS didn’t really care about the structure of the maze or anything about the way that a maze is in order to solve the problem. They just look at the actions available and choose from those actions, and it doesn’t matter whether it’s a maze or some other problem, the solution or the way that it tries to solve the problem is really fundamentally going to be the same. What we’re going to take a look at now is an improvement upon uninformed search. We’re going to take a look at informed search. Informed search are going to be search strategies that use knowledge specific to the problem to be able to better find a solution. And in the case of a maze, this problem-specific knowledge is something like if I’m in a square that is geographically closer to the goal, that is better than being in a square that is geographically further away. And this is something we can only know by thinking about this problem and reasoning about what knowledge might be helpful for our AI agent to know a little something about. There are a number of different types of informed search. Specifically, first, we’re going to look at a particular type of search algorithm called greedy best-first search. Greedy best-first search, often abbreviated G-BFS, is a search algorithm that instead of expanding the deepest node like DFS or the shallowest node like BFS, this algorithm is always going to expand the node that it thinks is closest to the goal. Now, the search algorithm isn’t going to know for sure whether it is the closest thing to the goal. Because if we knew what was closest to the goal all the time, then we would already have a solution. The knowledge of what is close to the goal, we could just follow those steps in order to get from the initial position to the solution. But if we don’t know the solution, meaning we don’t know exactly what’s closest to the goal, instead we can use an estimate of what’s closest to the goal, otherwise known as a heuristic, just some way of estimating whether or not we’re close to the goal. And we’ll do so using a heuristic function conventionally called h of n that takes a status input and returns our estimate of how close we are to the goal. So what might this heuristic function actually look like in the case of a maze solving algorithm? Where we’re trying to solve a maze, what does the heuristic look like? Well, the heuristic needs to answer a question between these two cells, C and D, which one is better? Which one would I rather be in if I’m trying to find my way to the goal? Well, any human could probably look at this and tell you, you know what, D looks like it’s better. Even if the maze is convoluted and you haven’t thought about all the walls, D is probably better. And why is D better? Well, because if you ignore the wall, so let’s just pretend the walls don’t exist for a moment and relax the problem, so to speak, D, just in terms of coordinate pairs, is closer to this goal. It’s fewer steps that I wouldn’t take to get to the goal as compared to C, even if you ignore the walls. If you just know the xy-coordinate of C and the xy-coordinate of the goal, and likewise you know the xy-coordinate of D, you can calculate the D just geographically. Ignoring the walls looks like it’s better. And so this is the heuristic function that we’re going to use. And it’s something called the Manhattan distance, one specific type of heuristic, where the heuristic is how many squares vertically and horizontally and then left to right, so not allowing myself to go diagonally, just either up or right or left or down. How many steps do I need to take to get from each of these cells to the goal? Well, as it turns out, D is much closer. There are fewer steps. It only needs to take six steps in order to get to that goal. Again, here, ignoring the walls. We’ve relaxed the problem a little bit. We’re just concerned with if you do the math to subtract the x values from each other and the y values from each other, what is our estimate of how far we are away? We can estimate the D is closer to the goal than C is. And so now we have an approach. We have a way of picking which node to remove from the frontier. And at each stage in our algorithm, we’re going to remove a node from the frontier. We’re going to explore the node if it has the smallest value for this heuristic function, if it has the smallest Manhattan distance to the goal. And so what would this actually look like? Well, let me first label this graph, label this maze, with a number representing the value of this heuristic function, the value of the Manhattan distance from any of these cells. So from this cell, for example, we’re one away from the goal. From this cell, we’re two away from the goal, three away, four away. Here, we’re five away because we have to go one to the right and then four up. From somewhere like here, the Manhattan distance is two. We’re only two squares away from the goal geographically, even though in practice, we’re going to have to take a longer path. But we don’t know that yet. The heuristic is just some easy way to estimate how far we are away from the goal. And maybe our heuristic is overly optimistic. It thinks that, yeah, we’re only two steps away. When in practice, when you consider the walls, it might be more steps. So the important thing here is that the heuristic isn’t a guarantee of how many steps it’s going to take. It is estimating. It’s an attempt at trying to approximate. And it does seem generally the case that the squares that look closer to the goal have smaller values for the heuristic function than squares that are further away. So now, using greedy best-first search, what might this algorithm actually do? Well, again, for these first five steps, there’s not much of a choice. We start at this initial state a, and we say, all right, we have to explore these five states. But now we have a decision point. Now we have a choice between going left and going right. And before, when DFS and BFS would just pick arbitrarily, because it just depends on the order you throw these two nodes into the frontier, and we didn’t specify what order you put them into the frontier, only the order you take them out, here we can look at 13 and 11 and say that, all right, this square is a distance of 11 away from the goal according to our heuristic, according to our estimate. And this one, we estimate to be 13 away from the goal. So between those two options, between these two choices, I’d rather have the 11. I’d rather be 11 steps away from the goal, so I’ll go to the right. We’re able to make an informed decision, because we know a little something more about this problem. So then we keep following, 10, 9, 8. Between the two 7s, we don’t really have much of a way to know between those. So then we do just have to make an arbitrary choice. And you know what, maybe we choose wrong. But that’s OK, because now we can still say, all right, let’s try this 7. We say 7, 6, we have to make this choice, even though it increases the value of the heuristic function. But now we have another decision point, between 6 and 8, and between those two. And really, we’re also considering this 13, but that’s much higher. Between 6, 8, and 13, well, the 6 is the smallest value, so we’d rather take the 6. We’re able to make an informed decision that going this way to the right is probably better than going down. So we turn this way, we go to 5. And now we find a decision point where we’ll actually make a decision that we might not want to make, but there’s unfortunately not too much of a way around this. We see 4 and 6. 4 looks closer to the goal, right? It’s going up, and the goal is further up. So we end up taking that route, which ultimately leads us to a dead end. But that’s OK, because we can still say, all right, now let’s try the 6. And now follow this route that will ultimately lead us to the goal. And so this now is how greedy best-for-search might try to approach this problem by saying, whenever we have a decision between multiple nodes that we could explore, let’s explore the node that has the smallest value of h of n, this heuristic function that is estimating how far I have to go. And it just so happens that in this case, we end up doing better in terms of the number of states we needed to explore than BFS needed to. BFS explored all of this section and all of that section, but we were able to eliminate that by taking advantage of this heuristic, this knowledge about how close we are to the goal or some estimate of that idea. So this seems much better. So wouldn’t we always prefer an algorithm like this over an algorithm like breadth-first search? Well, maybe one thing to take into consideration is that we need to come up with a good heuristic, how good the heuristic is, is going to affect how good this algorithm is. And coming up with a good heuristic can oftentimes be challenging. But the other thing to consider is to ask the question, just as we did with the prior two algorithms, is this algorithm optimal? Will it always find the shortest path from the initial state to the goal? And to answer that question, let’s take a look at this example for a moment. Take a look at this example. Again, we’re trying to get from A to B. And again, I’ve labeled each of the cells with their Manhattan distance from the goal. The number of squares up and to the right, you would need to travel in order to get from that square to the goal. And let’s think about, would greedy best-first search that always picks the smallest number end up finding the optimal solution? What is the shortest solution? And would this algorithm find it? And the important thing to realize is that right here is the decision point. We’re estimated to be 12 away from the goal. And we have two choices. We can go to the left, which we estimate to be 13 away from the goal. Or we can go up, where we estimate it to be 11 away from the goal. And between those two, greedy best-first search is going to say the 11 looks better than the 13. And in doing so, greedy best-first search will end up finding this path to the goal. But it turns out this path is not optimal. There is a way to get to the goal using fewer steps. And it’s actually this way, this way that ultimately involved fewer steps, even though it meant at this moment choosing the worst option between the two or what we estimated to be the worst option based on the heuristics. And so this is what we mean by this is a greedy algorithm. It’s making the best decision locally. At this decision point, it looks like it’s better to go here than it is to go to the 13. But in the big picture, it’s not necessarily optimal. That it might find a solution when in actuality, there was a better solution available. So we would like some way to solve this problem. We like the idea of this heuristic, of being able to estimate the path, the distance between us and the goal. And that helps us to be able to make better decisions and to eliminate having to search through entire parts of this state space. But we would like to modify the algorithm so that we can achieve optimality, so that it can be optimal. And what is the way to do this? What is the intuition here? Well, let’s take a look at this problem. In this initial problem, greedy best research found us this solution here, this long path. And the reason why it wasn’t great is because, yes, the heuristic numbers went down pretty low. But later on, they started to build back up. They built back 8, 9, 10, 11, all the way up to 12 in this case. And so how might we go about trying to improve this algorithm? Well, one thing that we might realize is that if we go all the way through this algorithm, through this path, and we end up going to the 12, and we’ve had to take this many steps, who knows how many steps that is, just to get to this 12, we could have also, as an alternative, taken much fewer steps, just six steps, and ended up at this 13 here. And yes, 13 is more than 12, so it looks like it’s not as good. But it required far fewer steps. It only took six steps to get to this 13 versus many more steps to get to this 12. And while greedy best research says, oh, well, 12 is better than 13, so pick the 12, we might more intelligently say, I’d rather be somewhere that heuristically looks like it takes slightly longer if I can get there much more quickly. And we’re going to encode that idea, this general idea, into a more formal algorithm known as A star search. A star search is going to solve this problem by instead of just considering the heuristic, also considering how long it took us to get to any particular state. So the distinction is greedy best for search. If I am in a state right now, the only thing I care about is, what is the estimated distance, the heuristic value, between me and the goal? Whereas A star search will take into consideration two pieces of information. It’ll take into consideration, how far do I estimate I am from the goal? But also, how far did I have to travel in order to get here? Because that is relevant, too. So we’ll search algorithms by expanding the node with the lowest value of g of n plus h of n. h of n is that same heuristic that we were talking about a moment ago that’s going to vary based on the problem. But g of n is going to be the cost to reach the node, how many steps I had to take, in this case, to get to my current position. So what does that search algorithm look like in practice? Well, let’s take a look. Again, we’ve got the same maze. And again, I’ve labeled them with their Manhattan distance. This value is the h of n value, the heuristic estimate of how far each of these squares is away from the goal. But now, as we begin to explore states, we care not just about this heuristic value, but also about g of n, the number of steps I had to take in order to get there. And I care about summing those two numbers together. So what does that look like? On this very first step, I have taken one step. And now I am estimated to be 16 steps away from the goal. So the total value here is 17. Then I take one more step. I’ve now taken two steps. And I estimate myself to be 15 away from the goal, again, a total value of 17. Now I’ve taken three steps. And I’m estimated to be 14 away from the goal, so on and so forth. Four steps, an estimate of 13. Five steps, estimate of 12. And now here’s a decision point. I could either be six steps away from the goal with a heuristic of 13 for a total of 19, or I could be six steps away from the goal with a heuristic of 11 with an estimate of 17 for the total. So between 19 and 17, I’d rather take the 17, the 6 plus 11. So so far, no different than what we saw before. We’re still taking this option because it appears to be better. And I keep taking this option because it appears to be better. But it’s right about here that things get a little bit different. Now I could be 15 steps away from the goal with an estimated distance of 6. So 15 plus 6, total value of 21. Alternatively, I could be six steps away from the goal, because this is five steps away, so this is six steps away, with a total value of 13 as my estimate. So 6 plus 13, that’s 19. So here, we would evaluate g of n plus h of n to be 19, 6 plus 13. Whereas here, we would be 15 plus 6, or 21. And so the intuition is 19 less than 21, pick here. But the idea is ultimately I’d rather be having taken fewer steps, get to a 13, than having taken 15 steps and be at a 6, because it means I’ve had to take more steps in order to get there. Maybe there’s a better path this way. So instead, we’ll explore this route. Now if we go one more, this is seven steps plus 14 is 21. So between those two, it’s sort of a toss-up. We might end up exploring that one anyways. But after that, as these numbers start to get bigger in the heuristic values, and these heuristic values start to get smaller, you’ll find that we’ll actually keep exploring down this path. And you can do the math to see that at every decision point, A star search is going to make a choice based on the sum of how many steps it took me to get to my current position, and then how far I estimate I am from the goal. So while we did have to explore some of these states, the ultimate solution we found was, in fact, an optimal solution. It did find us the quickest possible way to get from the initial state to the goal. And it turns out that A star is an optimal search algorithm under certain conditions. So the conditions are H of n, my heuristic, needs to be admissible. What does it mean for a heuristic to be admissible? Well, a heuristic is admissible if it never overestimates the true cost. H of n always needs to either get it exactly right in terms of how far away I am, or it needs to underestimate. So we saw an example from before where the heuristic value was much smaller than the actual cost it would take. That’s totally fine, but the heuristic value should never overestimate. It should never think that I’m further away from the goal than I actually am. And meanwhile, to make a stronger statement, H of n also needs to be consistent. And what does it mean for it to be consistent? Mathematically, it means that for every node, which we’ll call n, and successor, the node after me, that I’ll call n prime, where it takes a cost of C to make that step, the heuristic value of n needs to be less than or equal to the heuristic value of n prime plus the cost. So it’s a lot of math, but in words what that ultimately means is that if I am here at this state right now, the heuristic value from me to the goal shouldn’t be more than the heuristic value of my successor, the next place I could go to, plus however much it would cost me to just make that step from one step to the next step. And so this is just making sure that my heuristic is consistent between all of these steps that I might take. So as long as this is true, then A star search is going to find me an optimal solution. And this is where much of the challenge of solving these search problems can sometimes come in, that A star search is an algorithm that is known and you could write the code fairly easily, but it’s choosing the heuristic. It can be the interesting challenge. The better the heuristic is, the better I’ll be able to solve the problem in the fewer states that I’ll have to explore. And I need to make sure that the heuristic satisfies these particular constraints. So all in all, these are some of the examples of search algorithms that might work, and certainly there are many more than just this. A star, for example, does have a tendency to use quite a bit of memory. So there are alternative approaches to A star that ultimately use less memory than this version of A star happens to use, and there are other search algorithms that are optimized for other cases as well. But now so far, we’ve only been looking at search algorithms where there is one agent. I am trying to find a solution to a problem. I am trying to navigate my way through a maze. I am trying to solve a 15 puzzle. I am trying to find driving directions from point A to point B. Sometimes in search situations, though, we’ll enter an adversarial situation, where I am an agent trying to make intelligent decisions. And there’s someone else who is fighting against me, so to speak, that has opposite objectives, someone where I am trying to succeed, someone else that wants me to fail. And this is most popular in something like a game, a game like Tic Tac Toe, where we’ve got this 3 by 3 grid, and x and o take turns, either writing an x or an o in any one of these squares. And the goal is to get three x’s in a row if you’re the x player, or three o’s in a row if you’re the o player. And computers have gotten quite good at playing games, Tic Tac Toe very easily, but even more complex games. And so you might imagine, what does an intelligent decision in a game look like? So maybe x makes an initial move in the middle, and o plays up here. What does an intelligent move for x now become? Where should you move if you were x? And it turns out there are a couple of possibilities. But if an AI is playing this game optimally, then the AI might play somewhere like the upper right, where in this situation, o has the opposite objective of x. x is trying to win the game to get three in a row diagonally here. And o is trying to stop that objective, opposite of the objective. And so o is going to place here to try to block. But now, x has a pretty clever move. x can make a move like this, where now x has two possible ways that x can win the game. x could win the game by getting three in a row across here. Or x could win the game by getting three in a row vertically this way. So it doesn’t matter where o makes their next move. o could play here, for example, blocking the three in a row horizontally. But then x is going to win the game by getting a three in a row vertically. And so there’s a fair amount of reasoning that’s going on here in order for the computer to be able to solve a problem. And it’s similar in spirit to the problems we’ve looked at so far. There are actions. There’s some sort of state of the board and some transition from one action to the next. But it’s different in the sense that this is now not just a classical search problem, but an adversarial search problem. That I am at the x player trying to find the best moves to make, but I know that there is some adversary that is trying to stop me. So we need some sort of algorithm to deal with these adversarial type of search situations. And the algorithm we’re going to take a look at is an algorithm called Minimax, which works very well for these deterministic games where there are two players. It can work for other types of games as well. But we’ll look right now at games where I make a move, then my opponent makes a move. And I am trying to win, and my opponent is trying to win also. Or in other words, my opponent is trying to get me to lose. And so what do we need in order to make this algorithm work? Well, any time we try and translate this human concept of playing a game, winning and losing to a computer, we want to translate it in terms that the computer can understand. And ultimately, the computer really just understands the numbers. And so we want some way of translating a game of x’s and o’s on a grid to something numerical, something the computer can understand. The computer doesn’t normally understand notions of win or lose. But it does understand the concept of bigger and smaller. And so what we might do is we might take each of the possible ways that a tic-tac-toe game can unfold and assign a value or a utility to each one of those possible ways. And in a tic-tac-toe game, and in many types of games, there are three possible outcomes. The outcomes are o wins, x wins, or nobody wins. So player one wins, player two wins, or nobody wins. And for now, let’s go ahead and assign each of these possible outcomes a different value. We’ll say o winning, that’ll have a value of negative 1. Nobody winning, that’ll have a value of 0. And x winning, that will have a value of 1. So we’ve just assigned numbers to each of these three possible outcomes. And now we have two players, we have the x player and the o player. And we’re going to go ahead and call the x player the max player. And we’ll call the o player the min player. And the reason why is because in the min and max algorithm, the max player, which in this case is x, is aiming to maximize the score. These are the possible options for the score, negative 1, 0, and 1. x wants to maximize the score, meaning if at all possible, x would like this situation, where x wins the game, and we give it a score of 1. But if this isn’t possible, if x needs to choose between these two options, negative 1, meaning o winning, or 0, meaning nobody winning, x would rather that nobody wins, score of 0, than a score of negative 1, o winning. So this notion of winning and losing and tying has been reduced mathematically to just this idea of try and maximize the score. The x player always wants the score to be bigger. And on the flip side, the min player, in this case o, is aiming to minimize the score. The o player wants the score to be as small as possible. So now we’ve taken this game of x’s and o’s and winning and losing and turned it into something mathematical, something where x is trying to maximize the score, o is trying to minimize the score. Let’s now look at all of the parts of the game that we need in order to encode it in an AI so that an AI can play a game like tic-tac-toe. So the game is going to need a couple of things. We’ll need some sort of initial state that will, in this case, call s0, which is how the game begins, like an empty tic-tac-toe board, for example. We’ll also need a function called player, where the player function is going to take as input a state here represented by s. And the output of the player function is going to be which player’s turn is it. We need to be able to give a tic-tac-toe board to the computer, run it through a function, and that function tells us whose turn it is. We’ll need some notion of actions that we can take. We’ll see examples of that in just a moment. We need some notion of a transition model, same as before. If I have a state and I take an action, I need to know what results as a consequence of it. I need some way of knowing when the game is over. So this is equivalent to kind of like a goal test, but I need some terminal test, some way to check to see if a state is a terminal state, where a terminal state means the game is over. In a classic game of tic-tac-toe, a terminal state means either someone has gotten three in a row or all of the squares of the tic-tac-toe board are filled. Either of those conditions make it a terminal state. In a game of chess, it might be something like when there is checkmate or if checkmate is no longer possible, that that becomes a terminal state. And then finally, we’ll need a utility function, a function that takes a state and gives us a numerical value for that terminal state, some way of saying if x wins the game, that has a value of 1. If o is won the game, that has a value of negative 1. If nobody has won the game, that has a value of 0. So let’s take a look at each of these in turn. The initial state, we can just represent in tic-tac-toe as the empty game board. This is where we begin. It’s the place from which we begin this search. And again, I’ll be representing these things visually, but you can imagine this really just being like an array or a two-dimensional array of all of these possible squares. Then we need the player function that, again, takes a state and tells us whose turn it is. Assuming x makes the first move, if I have an empty game board, then my player function is going to return x. And if I have a game board where x has made a move, then my player function is going to return o. The player function takes a tic-tac-toe game board and tells us whose turn it is. Next up, we’ll consider the actions function. The actions function, much like it did in classical search, takes a state and gives us the set of all of the possible actions we can take in that state. So let’s imagine it’s o is turned to move in a game board that looks like this. What happens when we pass it into the actions function? So the actions function takes this state of the game as input, and the output is a set of possible actions. It’s a set of I could move in the upper left or I could move in the bottom middle. So those are the two possible action choices that I have when I begin in this particular state. Now, just as before, when we had states and actions, we need some sort of transition model to tell us when we take this action in the state, what is the new state that we get. And here, we define that using the result function that takes a state as input as well as an action. And when we apply the result function to this state, saying that let’s let o move in this upper left corner, the new state we get is this resulting state where o is in the upper left corner. And now, this seems obvious to someone who knows how to play tic-tac-toe. Of course, you play in the upper left corner. That’s the board you get. But all of this information needs to be encoded into the AI. The AI doesn’t know how to play tic-tac-toe until you tell the AI how the rules of tic-tac-toe work. And this function, defining this function here, allows us to tell the AI how this game actually works and how actions actually affect the outcome of the game. So the AI needs to know how the game works. The AI also needs to know when the game is over, as by defining a function called terminal that takes as input a state s, such that if we take a game that is not yet over, pass it into the terminal function, the output is false. The game is not over. But if we take a game that is over because x has gotten three in a row along that diagonal, pass that into the terminal function, then the output is going to be true because the game now is, in fact, over. And finally, we’ve told the AI how the game works in terms of what moves can be made and what happens when you make those moves. We’ve told the AI when the game is over. Now we need to tell the AI what the value of each of those states is. And we do that by defining this utility function that takes a state s and tells us the score or the utility of that state. So again, we said that if x wins the game, that utility is a value of 1, whereas if o wins the game, then the utility of that is negative 1. And the AI needs to know, for each of these terminal states where the game is over, what is the utility of that state? So if I give you a game board like this where the game is, in fact, over, and I ask the AI to tell me what the value of that state is, it could do so. The value of the state is 1. Where things get interesting, though, is if the game is not yet over. Let’s imagine a game board like this, where in the middle of the game, it’s o’s turn to make a move. So how do we know it’s o’s turn to make a move? We can calculate that using the player function. We can say player of s, pass in the state, o is the answer. So we know it’s o’s turn to move. And now, what is the value of this board and what action should o take? Well, that’s going to depend. We have to do some calculation here. And this is where the minimax algorithm really comes in. Recall that x is trying to maximize the score, which means that o is trying to minimize the score. So o would like to minimize the total value that we get at the end of the game. And because this game isn’t over yet, we don’t really know just yet what the value of this game board is. We have to do some calculation in order to figure that out. And so how do we do that kind of calculation? Well, in order to do so, we’re going to consider, just as we might in a classical search situation, what actions could happen next and what states will that take us to. And it turns out that in this position, there are only two open squares, which means there are only two open places where o can make a move. o could either make a move in the upper left or o can make a move in the bottom middle. And minimax doesn’t know right out of the box which of those moves is going to be better. So it’s going to consider both. But now, we sort of run into the same situation. Now, I have two more game boards, neither of which is over. What happens next? And now, it’s in this sense that minimax is what we’ll call a recursive algorithm. It’s going to now repeat the exact same process, although now considering it from the opposite perspective. It’s as if I am now going to put myself, if I am the o player, I’m going to put myself in my opponent’s shoes, my opponent as the x player, and consider what would my opponent do if they were in this position? What would my opponent do, the x player, if they were in that position? And what would then happen? Well, the other player, my opponent, the x player, is trying to maximize the score, whereas I am trying to minimize the score as the o player. So x is trying to find the maximum possible value that they can get. And so what’s going to happen? Well, from this board position, x only has one choice. x is going to play here, and they’re going to get three in a row. And we know that that board, x winning, that has a value of 1. If x wins the game, the value of that game board is 1. And so from this position, if this state can only ever lead to this state, it’s the only possible option, and this state has a value of 1, then the maximum possible value that the x player can get from this game board is also 1. From here, the only place we can get is to a game with a value of 1, so this game board also has a value of 1. Now we consider this one over here. What’s going to happen now? Well, x needs to make a move. The only move x can make is in the upper left, so x will go there. And in this game, no one wins the game. Nobody has three in a row. And so the value of that game board is 0. Nobody is 1. And so again, by the same logic, if from this board position the only place we can get to is a board where the value is 0, then this state must also have a value of 0. And now here comes the choice part, the idea of trying to minimize. I, as the o player, now know that if I make this choice moving in the upper left, that is going to result in a game with a value of 1, assuming everyone plays optimally. And if I instead play in the lower middle, choose this fork in the road, that is going to result in a game board with a value of 0. I have two options. I have a 1 and a 0 to choose from, and I need to pick. And as the min player, I would rather choose the option with the minimum value. So whenever a player has multiple choices, the min player will choose the option with the smallest value. The max player will choose the option with the largest value. Between the 1 and the 0, the 0 is smaller, meaning I’d rather tie the game than lose the game. And so this game board will say also has a value of 0, because if I am playing optimally, I will pick this fork in the road. I’ll place my o here to block x’s 3 in a row, x will move in the upper left, and the game will be over, and no one will have won the game. So this is now the logic of minimax, to consider all of the possible options that I can take, all of the actions that I can take, and then to put myself in my opponent’s shoes. I decide what move I’m going to make now by considering what move my opponent will make on the next turn. And to do that, I consider what move I would make on the turn after that, so on and so forth, until I get all the way down to the end of the game, to one of these so-called terminal states. In fact, this very decision point, where I am trying to decide as the o player what to make a decision about, might have just been a part of the logic that the x player, my opponent, was using, the move before me. This might be part of some larger tree, where x is trying to make a move in this situation, and needs to pick between three different options in order to make a decision about what to happen. And the further and further away we are from the end of the game, the deeper this tree has to go. Because every level in this tree is going to correspond to one move, one move or action that I take, one move or action that my opponent takes, in order to decide what happens. And in fact, it turns out that if I am the x player in this position, and I recursively do the logic, and see I have a choice, three choices, in fact, one of which leads to a value of 0. If I play here, and if everyone plays optimally, the game will be a tie. If I play here, then o is going to win, and I’ll lose playing optimally. Or here, where I, the x player, can win, well between a score of 0, and negative 1, and 1, I’d rather pick the board with a value of 1, because that’s the maximum value I can get. And so this board would also have a maximum value of 1. And so this tree can get very, very deep, especially as the game starts to have more and more moves. And this logic works not just for tic-tac-toe, but any of these sorts of games, where I make a move, my opponent makes a move, and ultimately, we have these adversarial objectives. And we can simplify the diagram into a diagram that looks like this. This is a more abstract version of the minimax tree, where these are each states, but I’m no longer representing them as exactly like tic-tac-toe boards. This is just representing some generic game that might be tic-tac-toe, might be some other game altogether. Any of these green arrows that are pointing up, that represents a maximizing state. I would like the score to be as big as possible. And any of these red arrows pointing down, those are minimizing states, where the player is the min player, and they are trying to make the score as small as possible. So if you imagine in this situation, I am the maximizing player, this player here, and I have three choices. One choice gives me a score of 5, one choice gives me a score of 3, and one choice gives me a score of 9. Well, then between those three choices, my best option is to choose this 9 over here, the score that maximizes my options out of all the three options. And so I can give this state a value of 9, because among my three options, that is the best choice that I have available to me. So that’s my decision now. You imagine it’s like one move away from the end of the game. But then you could also ask a reasonable question, what might my opponent do two moves away from the end of the game? My opponent is the minimizing player. They are trying to make the score as small as possible. Imagine what would have happened if they had to pick which choice to make. One choice leads us to this state, where I, the maximizing player, am going to opt for 9, the biggest score that I can get. And 1 leads to this state, where I, the maximizing player, would choose 8, which is then the largest score that I can get. Now the minimizing player, forced to choose between a 9 or an 8, is going to choose the smallest possible score, which in this case is an 8. And that is then how this process would unfold, that the minimizing player in this case considers both of their options, and then all of the options that would happen as a result of that. So this now is a general picture of what the minimax algorithm looks like. Let’s now try to formalize it using a little bit of pseudocode. So what exactly is happening in the minimax algorithm? Well, given a state s, we need to decide what to happen. The max player, if it’s max’s player’s turn, then max is going to pick an action a in actions of s. Recall that actions is a function that takes a state and gives me back all of the possible actions that I can take. It tells me all of the moves that are possible. The max player is going to specifically pick an action a in this set of actions that gives me the highest value of min value of result of s and a. So what does that mean? Well, it means that I want to make the option that gives me the highest score of all of the actions a. But what score is that going to have? To calculate that, I need to know what my opponent, the min player, is going to do if they try to minimize the value of the state that results. So we say, what state results after I take this action? And what happens when the min player tries to minimize the value of that state? I consider that for all of my possible options. And after I’ve considered that for all of my possible options, I pick the action a that has the highest value. Likewise, the min player is going to do the same thing but backwards. They’re also going to consider what are all of the possible actions they can take if it’s their turn. And they’re going to pick the action a that has the smallest possible value of all the options. And the way they know what the smallest possible value of all the options is is by considering what the max player is going to do by saying, what’s the result of applying this action to the current state? And then what would the max player try to do? What value would the max player calculate for that particular state? So everyone makes their decision based on trying to estimate what the other person would do. And now we need to turn our attention to these two functions, max value and min value. How do you actually calculate the value of a state if you’re trying to maximize its value? And how do you calculate the value of a state if you’re trying to minimize the value? If you can do that, then we have an entire implementation of this min and max algorithm. So let’s try it. Let’s try and implement this max value function that takes a state and returns as output the value of that state if I’m trying to maximize the value of the state. Well, the first thing I can check for is to see if the game is over. Because if the game is over, in other words, if the state is a terminal state, then this is easy. I already have this utility function that tells me what the value of the board is. If the game is over, I just check, did x win, did o win, is it a tie? And this utility function just knows what the value of the state is. What’s trickier is if the game isn’t over. Because then I need to do this recursive reasoning about thinking, what is my opponent going to do on the next move? And I want to calculate the value of this state. And I want the value of the state to be as high as possible. And I’ll keep track of that value in a variable called v. And if I want the value to be as high as possible, I need to give v an initial value. And initially, I’ll just go ahead and set it to be as low as possible. Because I don’t know what options are available to me yet. So initially, I’ll set v equal to negative infinity, which seems a little bit strange. But the idea here is I want the value initially to be as low as possible. Because as I consider my actions, I’m always going to try and do better than v. And if I set v to negative infinity, I know I can always do better than that. So now I consider my actions. And this is going to be some kind of loop where for every action in actions of state, recall actions as a function that takes my state and gives me all the possible actions that I can use in that state. So for each one of those actions, I want to compare it to v and say, all right, v is going to be equal to the maximum of v and this expression. So what is this expression? Well, first it is get the result of taking the action in the state and then get the min value of that. In other words, let’s say I want to find out from that state what is the best that the min player can do because they’re going to try and minimize the score. So whatever the resulting score is of the min value of that state, compare it to my current best value and just pick the maximum of those two because I am trying to maximize the value. In short, what these three lines of code are doing are going through all of my possible actions and asking the question, how do I maximize the score given what my opponent is going to try to do? After this entire loop, I can just return v and that is now the value of that particular state. And for the min player, it’s the exact opposite of this, the same logic just backwards. To calculate the minimum value of a state, first we check if it’s a terminal state. If it is, we return its utility. Otherwise, we’re going to now try to minimize the value of the state given all of my possible actions. So I need an initial value for v, the value of the state. And initially, I’ll set it to infinity because I know I can always get something less than infinity. So by starting with v equals infinity, I make sure that the very first action I find, that will be less than this value of v. And then I do the same thing, loop over all of my possible actions. And for each of the results that we could get when the max player makes their decision, let’s take the minimum of that and the current value of v. So after all is said and done, I get the smallest possible value of v that I then return back to the user. So that, in effect, is the pseudocode for Minimax. That is how we take a gain and figure out what the best move to make is by recursively using these max value and min value functions, where max value calls min value, min value calls max value back and forth, all the way until we reach a terminal state, at which point our algorithm can simply return the utility of that particular state. So what you might imagine is that this is going to start to be a long process, especially as games start to get more complex, as we start to add more moves and more possible options and games that might last quite a bit longer. So the next question to ask is, what sort of optimizations can we make here? How can we do better in order to use less space or take less time to be able to solve this kind of problem? And we’ll take a look at a couple of possible optimizations. But for one, we’ll take a look at this example. Again, returning to these up arrows and down arrows, let’s imagine that I now am the max player, this green arrow. I am trying to make this score as high as possible. And this is an easy game where there are just two moves. I make a move, one of these three options. And then my opponent makes a move, one of these three options, based on what move I make. And as a result, we get some value. Let’s look at the order in which I do these calculations and figure out if there are any optimizations I might be able to make to this calculation process. I’m going to have to look at these states one at a time. So let’s say I start here on the left and say, all right, now I’m going to consider, what will the min player, my opponent, try to do here? Well, the min player is going to look at all three of their possible actions and look at their value, because these are terminal states. They’re the end of the game. And so they’ll see, all right, this node is a value of four, value of eight, value of five. And the min player is going to say, well, all right, between these three options, four, eight, and five, I’ll take the smallest one. I’ll take the four. So this state now has a value of four. Then I, as the max player, say, all right, if I take this action, it will have a value of four. That’s the best that I can do, because min player is going to try and minimize my score. So now what if I take this option? We’ll explore this next. And now explore what the min player would do if I choose this action. And the min player is going to say, all right, what are the three options? The min player has options between nine, three, and seven. And so three is the smallest among nine, three, and seven. So we’ll go ahead and say this state has a value of three. So now I, as the max player, I have now explored two of my three options. I know that one of my options will guarantee me a score of four, at least. And one of my options will guarantee me a score of three. And now I consider my third option and say, all right, what happens here? Same exact logic. The min player is going to look at these three states, two, four, and six. I’ll say the minimum possible option is two. So the min player wants the two. Now I, as the max player, have calculated all of the information by looking two layers deep, by looking at all of these nodes. And I can now say, between the four, the three, and the two, you know what? I’d rather take the four. Because if I choose this option, if my opponent plays optimally, they will try and get me to the four. But that’s the best I can do. I can’t guarantee a higher score. Because if I pick either of these two options, I might get a three or I might get a two. And it’s true that down here is a nine. And that’s the highest score out of any of the scores. So I might be tempted to say, you know what? Maybe I should take this option because I might get the nine. But if the min player is playing intelligently, if they’re making the best moves at each possible option they have when they get to make a choice, I’ll be left with a three. Whereas I could better, playing optimally, have guaranteed that I would get the four. So that is, in effect, the logic that I would use as a min and max player trying to maximize my score from that node there. But it turns out they took quite a bit of computation for me to figure that out. I had to reason through all of these nodes in order to draw this conclusion. And this is for a pretty simple game where I have three choices, my opponent has three choices, and then the game’s over. So what I’d like to do is come up with some way to optimize this. Maybe I don’t need to do all of this calculation to still reach the conclusion that, you know what, this action to the left, that’s the best that I could do. Let’s go ahead and try again and try to be a little more intelligent about how I go about doing this. So first, I start the exact same way. I don’t know what to do initially, so I just have to consider one of the options and consider what the min player might do. Min has three options, four, eight, and five. And between those three options, min says four is the best they can do because they want to try to minimize the score. Now I, the max player, will consider my second option, making this move here, and considering what my opponent would do in response. What will the min player do? Well, the min player is going to, from that state, look at their options. And I would say, all right, nine is an option, three is an option. And if I am doing the math from this initial state, doing all this calculation, when I see a three, that should immediately be a red flag for me. Because when I see a three down here at this state, I know that the value of this state is going to be at most three. It’s going to be three or something less than three, even though I haven’t yet looked at this last action or even further actions if there were more actions that could be taken here. How do I know that? Well, I know that the min player is going to try to minimize my score. And if they see a three, the only way this could be something other than a three is if this remaining thing that I haven’t yet looked at is less than three, which means there is no way for this value to be anything more than three because the min player can already guarantee a three and they are trying to minimize my score. So what does that tell me? Well, it tells me that if I choose this action, my score is going to be three or maybe even less than three if I’m unlucky. But I already know that this action will guarantee me a four. And so given that I know that this action guarantees me a score of four and this action means I can’t do better than three, if I’m trying to maximize my options, there is no need for me to consider this triangle here. There is no value, no number that could go here that would change my mind between these two options. I’m always going to opt for this path that gets me a four as opposed to this path where the best I can do is a three if my opponent plays optimally. And this is going to be true for all the future states that I look at too. That if I look over here at what min player might do over here, if I see that this state is a two, I know that this state is at most a two because the only way this value could be something other than two is if one of these remaining states is less than a two and so the min player would opt for that instead. So even without looking at these remaining states, I as the maximizing player can know that choosing this path to the left is going to be better than choosing either of those two paths to the right because this one can’t be better than three. This one can’t be better than two. And so four in this case is the best that I can do. So in order to do this cut, and I can say now that this state has a value of four. So in order to do this type of calculation, I was doing a little bit more bookkeeping, keeping track of things, keeping track all the time of what is the best that I can do, what is the worst that I can do, and for each of these states saying, all right, well, if I already know that I can get a four, then if the best I can do at this state is a three, no reason for me to consider it, I can effectively prune this leaf and anything below it from the tree. And it’s for that reason this approach, this optimization to minimax, is called alpha, beta pruning. Alpha and beta stand for these two values that you’ll have to keep track of of the best you can do so far and the worst you can do so far. And pruning is the idea of if I have a big, long, deep search tree, I might be able to search it more efficiently if I don’t need to search through everything, if I can remove some of the nodes to try and optimize the way that I look through this entire search space. So alpha, beta pruning can definitely save us a lot of time as we go about the search process by making our searches more efficient. But even then, it’s still not great as games get more complex. Tic-tac-toe, fortunately, is a relatively simple game. And we might reasonably ask a question like, how many total possible tic-tac-toe games are there? You can think about it. You can try and estimate how many moves are there at any given point, how many moves long can the game last. It turns out there are about 255,000 possible tic-tac-toe games that can be played. But compare that to a more complex game, something like a game of chess, for example. Far more pieces, far more moves, games that last much longer. How many total possible chess games could there be? It turns out that after just four moves each, four moves by the white player, four moves by the black player, that there are 288 billion possible chess games that can result from that situation, after just four moves each. And going even further, if you look at entire chess games and how many possible chess games there could be as a result there, there are more than 10 to the 29,000 possible chess games, far more chess games than could ever be considered. And this is a pretty big problem for the Minimax algorithm, because the Minimax algorithm starts with an initial state, considers all the possible actions, and all the possible actions after that, all the way until we get to the end of the game. And that’s going to be a problem if the computer is going to need to look through this many states, which is far more than any computer could ever do in any reasonable amount of time. So what do we do in order to solve this problem? Instead of looking through all these states which is totally intractable for a computer, we need some better approach. And it turns out that better approach generally takes the form of something called depth-limited Minimax, where normally Minimax is depth-unlimited. We just keep going layer after layer, move after move, until we get to the end of the game. Depth-limited Minimax is instead going to say, you know what, after a certain number of moves, maybe I’ll look 10 moves ahead, maybe I’ll look 12 moves ahead, but after that point, I’m going to stop and not consider additional moves that might come after that, just because it would be computationally intractable to consider all of those possible options. But what do we do after we get 10 or 12 moves deep when we arrive at a situation where the game’s not over? Minimax still needs a way to assign a score to that game board or game state to figure out what its current value is, which is easy to do if the game is over, but not so easy to do if the game is not yet over. So in order to do that, we need to add one additional feature to depth-limited Minimax called an evaluation function, which is just some function that is going to estimate the expected utility of a game from a given state. So in a game like chess, if you imagine that a game value of 1 means white wins, negative 1 means black wins, 0 means it’s a draw, then you might imagine that a score of 0.8 means white is very likely to win, though certainly not guaranteed. And you would have an evaluation function that estimates how good the game state happens to be. And depending on how good that evaluation function is, that is ultimately what’s going to constrain how good the AI is. The better the AI is at estimating how good or how bad any particular game state is, the better the AI is going to be able to play that game. If the evaluation function is worse and not as good as it estimating what the expected utility is, then it’s going to be a whole lot harder. And you can imagine trying to come up with these evaluation functions. In chess, for example, you might write an evaluation function based on how many pieces you have as compared to how many pieces your opponent has, because each one has a value. And your evaluation function probably needs to be a little bit more complicated than that to consider other possible situations that might arise as well. And there are many other variants on Minimax that add additional features in order to help it perform better under these larger, more computationally untractable situations where we couldn’t possibly explore all of the possible moves. So we need to figure out how to use evaluation functions and other techniques to be able to play these games ultimately better. But this now was a look at this kind of adversarial search, these search problems where we have situations where I am trying to play against some sort of opponent. And these search problems show up all over the place throughout artificial intelligence. We’ve been talking a lot today about more classical search problems, like trying to find directions from one location to another. But any time an AI is faced with trying to make a decision, like what do I do now in order to do something that is rational, or do something that is intelligent, or trying to play a game, like figuring out what move to make, these sort of algorithms can really come in handy. It turns out that for tic-tac-toe, the solution is pretty simple because it’s a small game. XKCD has famously put together a web comic where he will tell you exactly what move to make as the optimal move to make no matter what your opponent happens to do. This type of thing is not quite as possible for a much larger game like Checkers or Chess, for example, where chess is totally computationally untractable for most computers to be able to explore all the possible states. So we really need our AI to be far more intelligent about how they go about trying to deal with these problems and how they go about taking this environment that they find themselves in and ultimately searching for one of these solutions. So this, then, was a look at search in artificial intelligence. Next time, we’ll take a look at knowledge, thinking about how it is that our AIs are able to know information, reason about that information, and draw conclusions, all in our look at AI and the principles behind it. We’ll see you next time. [“AIMS INTRO MUSIC”] All right, welcome back, everyone, to an introduction to artificial intelligence with Python. Last time, we took a look at search problems, in particular, where we have AI agents that are trying to solve some sort of problem by taking actions in some sort of environment, whether that environment is trying to take actions by playing moves in a game or whether those actions are something like trying to figure out where to make turns in order to get driving directions from point A to point B. This time, we’re going to turn our attention more generally to just this idea of knowledge, the idea that a lot of intelligence is based on knowledge, especially if we think about human intelligence. People know information. We know facts about the world. And using that information that we know, we’re able to draw conclusions, reason about the information that we know in order to figure out how to do something or figure out some other piece of information that we conclude based on the information we already have available to us. What we’d like to focus on now is the ability to take this idea of knowledge and being able to reason based on knowledge and apply those ideas to artificial intelligence. In particular, we’re going to be building what are known as knowledge-based agents, agents that are able to reason and act by representing knowledge internally. Somehow inside of our AI, they have some understanding of what it means to know something. And ideally, they have some algorithms or some techniques they can use based on that knowledge that they know in order to figure out the solution to a problem or figure out some additional piece of information that can be helpful in some sense. So what do we mean by reasoning based on knowledge to be able to draw conclusions? Well, let’s look at a simple example drawn from the world of Harry Potter. We take one sentence that we know to be true. Imagine if it didn’t rain, then Harry visited Hagrid today. So one fact that we might know about the world. And then we take another fact. Harry visited Hagrid or Dumbledore today, but not both. So it tells us something about the world, that Harry either visited Hagrid but not Dumbledore, or Harry visited Dumbledore but not Hagrid. And now we have a third piece of information about the world that Harry visited Dumbledore today. So we now have three pieces of information now, three facts. Inside of a knowledge base, so to speak, information that we know. And now we, as humans, can try and reason about this and figure out, based on this information, what additional information can we begin to conclude? And well, looking at these last two statements, Harry either visited Hagrid or Dumbledore but not both, and we know that Harry visited Dumbledore today, well, then it’s pretty reasonable that we could draw the conclusion that, you know what, Harry must not have visited Hagrid today. Because based on a combination of these two statements, we can draw this inference, so to speak, a conclusion that Harry did not visit Hagrid today. But it turns out we can even do a little bit better than that, get some more information by taking a look at this first statement and reasoning about that. This first statement says, if it didn’t rain, then Harry visited Hagrid today. So what does that mean? In all cases where it didn’t rain, then we know that Harry visited Hagrid. But if we also know now that Harry did not visit Hagrid, then that tells us something about our initial premise that we were thinking about. In particular, it tells us that it did rain today, because we can reason, if it didn’t rain, that Harry would have visited Hagrid. But we know for a fact that Harry did not visit Hagrid today. So it’s this kind of reason, this sort of logical reasoning, where we use logic based on the information that we know in order to take information and reach conclusions that is going to be the focus of what we’re going to be talking about today. How can we make our artificial intelligence logical so that they can perform the same kinds of deduction, the same kinds of reasoning that we’ve been doing so far? Of course, humans reason about logic generally in terms of human language. That I just now was speaking in English, talking in English about these sentences and trying to reason through how it is that they relate to one another. We’re going to need to be a little bit more formal when we turn our attention to computers and being able to encode this notion of logic and truthhood and falsehood inside of a machine. So we’re going to need to introduce a few more terms and a few symbols that will help us reason through this idea of logic inside of an artificial intelligence. And we’ll begin with the idea of a sentence. Now, a sentence in a natural language like English is just something that I’m saying, like what I’m saying right now. In the context of AI, though, a sentence is just an assertion about the world in what we’re going to call a knowledge representation language, some way of representing knowledge inside of our computers. And the way that we’re going to spend most of today reasoning about knowledge is through a type of logic known as propositional logic. There are a number of different types of logic, some of which we’ll touch on. But propositional logic is based on a logic of propositions, or just statements about the world. And so we begin in propositional logic with a notion of propositional symbols. We will have certain symbols that are oftentimes just letters, something like P or Q or R, where each of those symbols is going to represent some fact or sentence about the world. So P, for example, might represent the fact that it is raining. And so P is going to be a symbol that represents that idea. And Q, for example, might represent Harry visited Hagrid today. Each of these propositional symbols represents some sentence or some fact about the world. But in addition to just having individual facts about the world, we want some way to connect these propositional symbols together in order to reason more complexly about other facts that might exist inside of the world in which we’re reasoning. So in order to do that, we’ll need to introduce some additional symbols that are known as logical connectives. Now, there are a number of these logical connectives. But five of the most important, and the ones we’re going to focus on today, are these five up here, each represented by a logical symbol. Not is represented by this symbol here, and is represented as sort of an upside down V, or is represented by a V shape. Implication, and we’ll talk about what that means in just a moment, is represented by an arrow. And biconditional, again, we’ll talk about what that means in a moment, is represented by these double arrows. But these five logical connectives are the main ones we’re going to be focusing on in terms of thinking about how it is that a computer can reason about facts and draw conclusions based on the facts that it knows. But in order to get there, we need to take a look at each of these logical connectives and build up an understanding for what it is that they actually mean. So let’s go ahead and begin with the not symbol, so this not symbol here. And what we’re going to show for each of these logical connectives is what we’re going to call a truth table, a table that demonstrates what this word not means when we attach it to a propositional symbol or any sentence inside of our logical language. And so the truth table for not is shown right here. If P, some propositional symbol, or some other sentence even, is false, then not P is true. And if P is true, then not P is false. So you can imagine that placing this not symbol in front of some sentence of propositional logic just says the opposite of that. So if, for example, P represented it is raining, then not P would represent the idea that it is not raining. And as you might expect, if P is false, meaning if the sentence, it is raining, is false, well then the sentence not P must be true. The sentence that it is not raining is therefore true. So not, you can imagine, just takes whatever is in P and it inverts it. It turns false into true and true into false, much analogously to what the English word not means, just taking whatever comes after it and inverting it to mean the opposite. Next up, and also very English-like, is this idea of and represented by this upside-down V shape or this point shape. And as opposed to just taking a single argument the way not does, we have P and we have not P. And is going to combine two different sentences in propositional logic together. So I might have one sentence P and another sentence Q, and I want to combine them together to say P and Q. And the general logic for what P and Q means is it means that both of its operands are true. P is true and also Q is true. And so here’s what that truth table looks like. This time we have two variables, P and Q. And when we have two variables, each of which can be in two possible states, true or false, that leads to two squared or four possible combinations of truth and falsehood. So we have P is false and Q is false. We have P is false and Q is true. P is true and Q is false. And then P and Q both are true. And those are the only four possibilities for what P and Q could mean. And in each of those situations, this third column here, P and Q, is telling us a little bit about what it actually means for P and Q to be true. And we see that the only case where P and Q is true is in this fourth row here, where P happens to be true, Q also happens to be true. And in all other situations, P and Q is going to evaluate to false. So this, again, is much in line with what our intuition of and might mean. If I say P and Q, I probably mean that I expect both P and Q to be true. Next up, also potentially consistent with what we mean, is this word or, represented by this V shape, sort of an upside down and symbol. And or, as the name might suggest, is true if either of its arguments are true, as long as P is true or Q is true, then P or Q is going to be true. Which means the only time that P or Q is false is if both of its operands are false. If P is false and Q is false, then P or Q is going to be false. But in all other cases, at least one of the operands is true. Maybe they’re both true, in which case P or Q is going to evaluate to true. Now, this is mostly consistent with the way that most people might use the word or, in the sense of speaking the word or in normal English, though there is sometimes when we might say or, where we mean P or Q, but not both, where we mean, sort of, it can only be one or the other. It’s important to note that this symbol here, this or, means P or Q or both, that those are totally OK. As long as either or both of them are true, then the or is going to evaluate to be true, as well. It’s only in the case where all of the operands are false that P or Q ultimately evaluates to false, as well. In logic, there’s another symbol known as the exclusive or, which encodes this idea of exclusivity of one or the other, but not both. But we’re not going to be focusing on that today. Whenever we talk about or, we’re always talking about either or both, in this case, as represented by this truth table here. So that now is not an and an or. And next up is what we might call implication, as denoted by this arrow symbol. So we have P and Q. And this sentence here will generally read as P implies Q. And what P implies Q means is that if P is true, then Q is also true. So I might say something like, if it is raining, then I will be indoors. Meaning, it is raining implies I will be indoors, as the logical sentence that I’m saying there. And the truth table for this can sometimes be a little bit tricky. So obviously, if P is true and Q is true, then P implies Q. That’s true. That definitely makes sense. And it should also stand to reason that when P is true and Q is false, then P implies Q is false. Because if I said to you, if it is raining, then I will be out indoors. And it is raining, but I’m not indoors? Well, then it would seem to be that my original statement was not true. P implies Q means that if P is true, then Q also needs to be true. And if it’s not, well, then the statement is false. What’s also worth noting, though, is what happens when P is false. When P is false, the implication makes no claim at all. If I say something like, if it is raining, then I will be indoors. And it turns out it’s not raining. Then in that case, I am not making any statement as to whether or not I will be indoors or not. P implies Q just means that if P is true, Q must be true. But if P is not true, then we make no claim about whether or not Q is true at all. So in either case, if P is false, it doesn’t matter what Q is. Whether it’s false or true, we’re not making any claim about Q whatsoever. We can still evaluate the implication to true. The only way that the implication is ever false is if our premise, P, is true, but the conclusion that we’re drawing Q happens to be false. So in that case, we would say P does not imply Q in that case. Finally, the last connective that we’ll discuss is this bi-conditional. You can think of a bi-conditional as a condition that goes in both directions. So originally, when I said something like, if it is raining, then I will be indoors. I didn’t say what would happen if it wasn’t raining. Maybe I’ll be indoors, maybe I’ll be outdoors. This bi-conditional, you can read as an if and only if. So I can say, I will be indoors if and only if it is raining, meaning if it is raining, then I will be indoors. And if I am indoors, it’s reasonable to conclude that it is also raining. So this bi-conditional is only true when P and Q are the same. So if P is true and Q is true, then this bi-conditional is also true. P implies Q, but also the reverse is true. Q also implies P. So if P and Q both happen to be false, we would still say it’s true. But in any of these other two situations, this P if and only if Q is going to ultimately evaluate to false. So a lot of trues and falses going on there, but these five basic logical connectives are going to form the core of the language of propositional logic, the language that we’re going to use in order to describe ideas, and the language that we’re going to use in order to reason about those ideas in order to draw conclusions. So let’s now take a look at some of the additional terms that we’ll need to know about in order to go about trying to form this language of propositional logic and writing AI that’s actually able to understand this sort of logic. The next thing we’re going to need is the notion of what is actually true about the world. We have a whole bunch of propositional symbols, P and Q and R and maybe others, but we need some way of knowing what actually is true in the world. Is P true or false? Is Q true or false? So on and so forth. And to do that, we’ll introduce the notion of a model. A model just assigns a truth value, where a truth value is either true or false, to every propositional symbol. In other words, it’s creating what we might call a possible world. So let me give an example. If, for example, I have two propositional symbols, P is it is raining and Q is it is a Tuesday, a model just takes each of these two symbols and assigns a truth value to them, either true or false. So here’s a sample model. In this model, in other words, in this possible world, it is possible that P is true, meaning it is raining, and Q is false, meaning it is not a Tuesday. But there are other possible worlds or other models as well. There is some model where both of these variables are true, some model where both of these variables are false. In fact, if there are n variables that are propositional symbols like this that are either true or false, then the number of possible models is 2 to the n, because each of these possible models, possible variables within my model, could be set to either true or false if I don’t know any information about it. So now that I have the symbols and the connectives that I’m going to need in order to construct these parts of knowledge, we need some way to represent that knowledge. And to do so, we’re going to allow our AI access to what we’ll call a knowledge base. And a knowledge base is really just a set of sentences that our AI knows to be true. Some set of sentences in propositional logic that are things that our AI knows about the world. And so we might tell our AI some information, information about a situation that it finds itself in, or a situation about a problem that it happens to be trying to solve. And we would give that information to the AI that the AI would store inside of its knowledge base. And what happens next is the AI would like to use that information in the knowledge base to be able to draw conclusions about the rest of the world. And what do those conclusions look like? Well, to understand those conclusions, we’ll need to introduce one more idea, one more symbol. And that is the notion of entailment. So this sentence here, with this double turnstile in these Greek letters, this is the Greek letter alpha and the Greek letter beta. And we read this as alpha entails beta. And alpha and beta here are just sentences in propositional logic. And what this means is that alpha entails beta means that in every model, in other words, in every possible world in which sentence alpha is true, then sentence beta is also true. So if something entails something else, if alpha entails beta, it means that if I know alpha to be true, then beta must therefore also be true. So if my alpha is something like I know that it is a Tuesday in January, then a reasonable beta might be something like I know that it is January. Because in all worlds where it is a Tuesday in January, I know for sure that it must be January, just by definition. This first statement or sentence about the world entails the second statement. And we can reasonably use deduction based on that first sentence to figure out that the second sentence is, in fact, true as well. And ultimately, it’s this idea of entailment that we’re going to try and encode into our computer. We want our AI agent to be able to figure out what the possible entailments are. We want our AI to be able to take these three sentences, sentences like, if it didn’t rain, Harry visited Hagrid. That Harry visited Hagrid or Dumbledore, but not both. And that Harry visited Dumbledore. And just using that information, we’d like our AI to be able to infer or figure out that using these three sentences inside of a knowledge base, we can draw some conclusions. In particular, we can draw the conclusions here that, one, Harry did not visit Hagrid today. And we can draw the entailment, too, that it did, in fact, rain today. And this process is known as inference. And that’s what we’re going to be focusing on today, this process of deriving new sentences from old ones, that I give you these three sentences, you put them in the knowledge base in, say, the AI. And the AI is able to use some sort of inference algorithm to figure out that these two sentences must also be true. And that is how we define inference. So let’s take a look at an inference example to see how we might actually go about inferring things in a human sense before we take a more algorithmic approach to see how we could encode this idea of inference in AI. And we’ll see there are a number of ways that we can actually achieve this. So again, we’ll deal with a couple of propositional symbols. We’ll deal with P, Q, and R. P is it is a Tuesday. Q is it is raining. And R is Harry will go for a run, three propositional symbols that we are just defining to mean this. We’re not saying anything yet about whether they’re true or false. We’re just defining what they are. Now, we’ll give ourselves or an AI access to a knowledge base, abbreviated to KB, the knowledge that we know about the world. We know this statement. All right. So let’s try to parse it. The parentheses here are just used for precedent, so we can see what associates with what. But you would read this as P and not Q implies R. All right. So what does that mean? Let’s put it piece by piece. P is it is a Tuesday. Q is it is raining, so not Q is it is not raining, and implies R is Harry will go for a run. So the way to read this entire sentence in human natural language at least is if it is a Tuesday and it is not raining, then Harry will go for a run. So if it is a Tuesday and it is not raining, then Harry will go for a run. And that is now inside of our knowledge base. And let’s now imagine that our knowledge base has two other pieces of information as well. It has information that P is true, that it is a Tuesday. And we also have the information not Q, that it is not raining, that this sentence Q, it is raining, happens to be false. And those are the three sentences that we have access to. P and not Q implies R, P and not Q. Using that information, we should be able to draw some inferences. P and not Q is only true if both P and not Q are true. All right, we know that P is true and we know that not Q is true. So we know that this whole expression is true. And the definition of implication is if this whole thing on the left is true, then this thing on the right must also be true. So if we know that P and not Q is true, then R must be true as well. So the inference we should be able to draw from all of this is that R is true and we know that Harry will go for a run by taking this knowledge inside of our knowledge base and being able to reason based on that idea. And so this ultimately is the beginning of what we might consider to be some sort of inference algorithm, some process that we can use to try and figure out whether or not we can draw some conclusion. And ultimately, what these inference algorithms are going to answer is the central question about entailment. Given some query about the world, something we’re wondering about the world, and we’ll call that query alpha, the question we want to ask using these inference algorithms is does KB, our knowledge base, entail alpha? In other words, using only the information we know inside of our knowledge base, the knowledge that we have access to, can we conclude that this sentence alpha is true? And that’s ultimately what we would like to do. So how can we do that? How can we go about writing an algorithm that can look at this knowledge base and figure out whether or not this query alpha is actually true? Well, it turns out there are a couple of different algorithms for doing so. And one of the simplest, perhaps, is known as model checking. Now, remember that a model is just some assignment of all of the propositional symbols inside of our language to a truth value, true or false. And you can think of a model as a possible world, that there are many possible worlds where different things might be true or false, and we can enumerate all of them. And the model checking algorithm does exactly that. So what does our model checking algorithm do? Well, if we wanted to determine if our knowledge base entails some query alpha, then we are going to enumerate all possible models. In other words, consider all possible values of true and false for our variables, all possible states in which our world can be in. And if in every model where our knowledge base is true, alpha is also true, then we know that the knowledge base entails alpha. So let’s take a closer look at that sentence and try and figure out what it actually means. If we know that in every model, in other words, in every possible world, no matter what assignment of true and false to variables you give, if we know that whenever our knowledge is true, what we know to be true is true, that this query alpha is also true, well, then it stands to reason that as long as our knowledge base is true, then alpha must also be true. And so this is going to form the foundation of our model checking algorithm. We’re going to enumerate all of the possible worlds and ask ourselves whenever the knowledge base is true, is alpha true? And if that’s the case, then we know alpha to be true. And otherwise, there is no entailment. Our knowledge base does not entail alpha. All right. So this is a little bit abstract, but let’s take a look at an example to try and put real propositional symbols to this idea. So again, we’ll work with the same example. P is it is a Tuesday, Q is it is raining, R as Harry will go for a run. Our knowledge base contains these pieces of information. P and not Q implies R. We also know P. It is a Tuesday and not Q. It is not raining. And our query, our alpha in this case, the thing we want to ask is R. We want to know, is it guaranteed? Is it entailed that Harry will go for a run? So the first step is to enumerate all of the possible models. We have three propositional symbols here, P, Q, and R, which means we have 2 to the third power, or eight possible models. All false, false, false true, false true, false, false true, true, et cetera. Eight possible ways you could assign true and false to all of these models. And we might ask in each one of them, is the knowledge base true? Here are the set of things that we know. In which of these worlds could this knowledge base possibly apply to? In which world is this knowledge base true? Well, in the knowledge base, for example, we know P. We know it is a Tuesday, which means we know that these four first four rows where P is false, none of those are going to be true or are going to work for this particular knowledge base. Our knowledge base is not true in those worlds. Likewise, we also know not Q. We know that it is not raining. So any of these models where Q is true, like these two and these two here, those aren’t going to work either because we know that Q is not true. And finally, we also know that P and not Q implies R, which means that when P is true or P is true here and Q is false, Q is false in these two, then R must be true. And if ever P is true, Q is false, but R is also false, well, that doesn’t satisfy this implication here. That implication does not hold true under those situations. So we could say that for our knowledge base, we can conclude under which of these possible worlds is our knowledge base true and under which of the possible worlds is our knowledge base false. And it turns out there is only one possible world where our knowledge base is actually true. In some cases, there might be multiple possible worlds where the knowledge base is true. But in this case, it just so happens that there’s only one, one possible world where we can definitively say something about our knowledge base. And in this case, we would look at the query. The query of R is R true, R is true, and so as a result, we can draw that conclusion. And so this is this idea of model check-in. Enumerate all the possible models and look in those possible models to see whether or not, if our knowledge base is true, is the query in question true as well. So let’s now take a look at how we might actually go about writing this in a programming language like Python. Take a look at some actual code that would encode this notion of propositional symbols and logic and these connectives like and and or and not and implication and so forth and see what that code might actually look like. So I’ve written in advance a logic library that’s more detailed than we need to worry about entirely today. But the important thing is that we have one class for every type of logical symbol or connective that we might have. So we just have one class for logical symbols, for example, where every symbol is going to represent and store some name for that particular symbol. And we also have a class for not that takes an operand. So we might say not one symbol to say something is not true or some other sentence is not true. We have one for and, one for or, so on and so forth. And I’ll just demonstrate how this works. And you can take a look at the actual logic.py later on. But I’ll go ahead and call this file harry.py. We’re going to store information about this world of Harry Potter, for example. So I’ll go ahead and import from my logic module. I’ll import everything. And in this library, in order to create a symbol, you use capital S symbol. And I’ll create a symbol for rain, to mean it is raining, for example. And I’ll create a symbol for Hagrid, to mean Harry visited Hagrid, is what this symbol is going to mean. So this symbol means it is raining. This symbol means Harry visited Hagrid. And I’ll add another symbol called Dumbledore for Harry visited Dumbledore. Now, I’d like to save these symbols so that I can use them later as I do some logical analysis. So I’ll go ahead and save each one of them inside of a variable. So like rain, Hagrid, and Dumbledore, so you could call the variables anything. And now that I have these logical symbols, I can use logical connectives to combine them together. So for example, if I have a sentence like and rain and Hagrid, for example, which is not necessarily true, but just for demonstration, I can now try and print out sentence.formula, which is a function I wrote that takes a sentence in propositional logic and just prints it out so that we, the programmers, can now see this in order to get an understanding for how it actually works. So if I run python harry.py, what we’ll see is this sentence in propositional logic, rain and Hagrid. This is the logical representation of what we have here in our Python program of saying and whose arguments are rain and Hagrid. So we’re saying rain and Hagrid by encoding that idea. And this is quite common in Python object-oriented programming, where you have a number of different classes, and you pass arguments into them in order to create a new and object, for example, in order to represent this idea. But now what I’d like to do is somehow encode the knowledge that I have about the world in order to solve that problem from the beginning of class, where we talked about trying to figure out who Harry visited and trying to figure out if it’s raining or if it’s not raining. And so what knowledge do I have? I’ll go ahead and create a new variable called knowledge. And what do I know? Well, I know the very first sentence that we talked about was the idea that if it is not raining, then Harry will visit Hagrid. So all right, how do I encode the idea that it is not raining? Well, I can use not and then the rain symbol. So here’s me saying that it is not raining. And now the implication is that if it is not raining, then Harry visited Hagrid. So I’ll wrap this inside of an implication to say, if it is not raining, this first argument to the implication will then Harry visited Hagrid. So I’m saying implication, the premise is that it’s not raining. And if it is not raining, then Harry visited Hagrid. And I can print out knowledge.formula to see the logical formula equivalent of that same idea. So I run Python of harry.py. And this is the logical formula that we see as a result, which is a text-based version of what we were looking at before, that if it is not raining, then that implies that Harry visited Hagrid. But there was additional information that we had access to as well. In this case, we had access to the fact that Harry visited either Hagrid or Dumbledore. So how do I encode that? Well, this means that in my knowledge, I’ve really got multiple pieces of knowledge going on. I know one thing and another thing and another thing. So I’ll go ahead and wrap all of my knowledge inside of an and. And I’ll move things on to new lines just for good measure. But I know multiple things. So I’m saying knowledge is an and of multiple different sentences. I know multiple different sentences to be true. One such sentence that I know to be true is this implication, that if it is not raining, then Harry visited Hagrid. Another such sentence that I know to be true is or Hagrid Dumbledore. In other words, Hagrid or Dumbledore is true, because I know that Harry visited Hagrid or Dumbledore. But I know more than that, actually. That initial sentence from before said that Harry visited Hagrid or Dumbledore, but not both. So now I want a sentence that will encode the idea that Harry didn’t visit both Hagrid and Dumbledore. Well, the notion of Harry visiting Hagrid and Dumbledore would be represented like this, and of Hagrid and Dumbledore. And if that is not true, if I want to say not that, then I’ll just wrap this whole thing inside of a not. So now these three lines, line 8 says that if it is not raining, then Harry visited Hagrid. Line 9 says Harry visited Hagrid or Dumbledore. And line 10 says Harry didn’t visit both Hagrid and Dumbledore, that it is not true that both the Hagrid symbol and the Dumbledore symbol are true. Only one of them can be true. And finally, the last piece of information that I knew was the fact that Harry visited Dumbledore. So these now are the pieces of knowledge that I know, one sentence and another sentence and another and another. And I can print out what I know just to see it a little bit more visually. And here now is a logical representation of the information that my computer is now internally representing using these various different Python objects. And again, take a look at logic.py if you want to take a look at how exactly it’s implementing this, but no need to worry too much about all of the details there. We’re here saying that if it is not raining, then Harry visited Hagrid. We’re saying that Hagrid or Dumbledore is true. And we’re saying it is not the case that Hagrid and Dumbledore is true, that they’re not both true. And we also know that Dumbledore is true. So this long logical sentence represents our knowledge base. It is the thing that we know. And now what we’d like to do is we’d like to use model checking to ask a query, to ask a question like, based on this information, do I know whether or not it’s raining? And we as humans were able to logic our way through it and figure out that, all right, based on these sentences, we can conclude this and that to figure out that, yes, it must have been raining. But now we’d like for the computer to do that as well. So let’s take a look at the model checking algorithm that is going to follow that same pattern that we drew out in pseudocode a moment ago. So I’ve defined a function here in logic.py that you can take a look at called model check. Model check takes two arguments, the knowledge that I already know, and the query. And the idea is, in order to do model checking, I need to enumerate all of the possible models. And for each of the possible models, I need to ask myself, is the knowledge base true? And is the query true? So the first thing I need to do is somehow enumerate all of the possible models, meaning for all possible symbols that exist, I need to assign true and false to each one of them and see whether or not it’s still true. And so here is the way we’re going to do that. We’re going to start. So I’ve defined another helper function internally that we’ll get to in just a moment. But this function starts by getting all of the symbols in both the knowledge and the query, by figuring out what symbols am I dealing with. In this case, the symbols I’m dealing with are rain and Hagrid and Dumbledore, but there might be other symbols depending on the problem. And we’ll take a look soon at some examples of situations where ultimately we’re going to need some additional symbols in order to represent the problem. And then we’re going to run this check all function, which is a helper function that’s basically going to recursively call itself checking every possible configuration of propositional symbols. So we start out by looking at this check all function. And what do we do? So if not symbols means if we finish assigning all of the symbols. We’ve assigned every symbol a value. So far we haven’t done that, but if we ever do, then we check. In this model, is the knowledge true? That’s what this line is saying. If we evaluate the knowledge propositional logic formula using the model’s assignment of truth values, is the knowledge true? If the knowledge is true, then we should return true only if the query is true. Because if the knowledge is true, we want the query to be true as well in order for there to be entailment. Otherwise, we don’t know that there otherwise there won’t be an entailment if there’s ever a situation where what we know in our knowledge is true, but the query, the thing we’re asking, happens to be false. So this line here is checking that same idea that in all worlds where the knowledge is true, the query must also be true. Otherwise, we can just return true because if the knowledge isn’t true, then we don’t care. This is equivalent to when we were enumerating this table from a moment ago. In all situations where the knowledge base wasn’t true, all of these seven rows here, we didn’t care whether or not our query was true or not. We only care to check whether the query is true when the knowledge base is actually true, which was just this green highlighted row right there. So that logic is encoded using that statement there. And otherwise, if we haven’t assigned symbols yet, which we haven’t seen anything yet, then the first thing we do is pop one of the symbols. I make a copy of the symbols first just to save an existing copy. But I pop one symbol off of the remaining symbols so that I just pick one symbol at random. And I create one copy of the model where that symbol is true. And I create a second copy of the model where that symbol is false. So I now have two copies of the model, one where the symbol is true and one where the symbol is false. And I need to make sure that this entailment holds in both of those models. So I recursively check all on the model where the statement is true and check all on the model where the statement is false. So again, you can take a look at that function to try to get a sense for how exactly this logic is working. But in effect, what it’s doing is recursively calling this check all function again and again and again. And on every level of the recursion, we’re saying let’s pick a new symbol that we haven’t yet assigned, assign it to true and assign it to false, and then check to make sure that the entailment holds in both cases. Because ultimately, I need to check every possible world. I need to take every combination of symbols and try every combination of true and false in order to figure out whether the entailment relation actually holds. So that function we’ve written for you. But in order to use that function inside of harry.py, what I’ll write is something like this. I would like to model check based on the knowledge. And then I provide as a second argument what the query is, what the thing I want to ask is. And what I want to ask in this case is, is it raining? So model check again takes two arguments. The first argument is the information that I know, this knowledge, which in this case is this information that was given to me at the beginning. And the second argument, rain, is encoding the idea of the query. What am I asking? I would like to ask, based on this knowledge, do I know for sure that it is raining? And I can try and print out the result of that. And when I run this program, I see that the answer is true. That based on this information, I can conclusively say that it is raining, because using this model checking algorithm, we were able to check that in every world where this knowledge is true, it is raining. In other words, there is no world where this knowledge is true, and it is not raining. So you can conclude that it is, in fact, raining. And this sort of logic can be applied to a number of different types of problems, that if confronted with a problem where some sort of logical deduction can be used in order to try to solve it, you might try thinking about what propositional symbols you might need in order to represent that information, and what statements and propositional logic you might use in order to encode that information which you know. And this process of trying to take a problem and figure out what propositional symbols to use in order to encode that idea, or how to represent it logically, is known as knowledge engineering. That software engineers and AI engineers will take a problem and try and figure out how to distill it down into knowledge that is representable by a computer. And if we can take any general purpose problem, some problem that we find in the human world, and turn it into a problem that computers know how to solve as by using any number of different variables, well, then we can take a computer that is able to do something like model checking or some other inference algorithm and actually figure out how to solve that problem. So now we’ll take a look at two or three examples of knowledge engineering and practice, of taking some problem and figuring out how we can apply logical symbols and use logical formulas to be able to encode that idea. And we’ll start with a very popular board game in the US and the UK known as Clue. Now, in the game of Clue, there’s a number of different factors that are going on. But the basic premise of the game, if you’ve never played it before, is that there are a number of different people. For now, we’ll just use three, Colonel Mustard, Professor Plumb, and Miss Scarlet. There are a number of different rooms, like a ballroom, a kitchen, and a library. And there are a number of different weapons, a knife, a revolver, and a wrench. And three of these, one person, one room, and one weapon, is the solution to the mystery, the murderer and what room they were in and what weapon they happened to use. And what happens at the beginning of the game is that all these cards are randomly shuffled together. And three of them, one person, one room, and one weapon, are placed into a sealed envelope that we don’t know. And we would like to figure out, using some sort of logical process, what’s inside the envelope, which person, which room, and which weapon. And we do so by looking at some, but not all, of these cards here, by looking at these cards to try and figure out what might be going on. And so this is a very popular game. But let’s now try and formalize it and see if we could train a computer to be able to play this game by reasoning through it logically. So in order to do this, we’ll begin by thinking about what propositional symbols we’re ultimately going to need. Remember, again, that propositional symbols are just some symbol, some variable, that can be either true or false in the world. And so in this case, the propositional symbols are really just going to correspond to each of the possible things that could be inside the envelope. Mustard is a propositional symbol that, in this case, will just be true if Colonel Mustard is inside the envelope, if he is the murderer, and false otherwise. And likewise for Plum, for Professor Plum, and Scarlet, for Miss Scarlet. And likewise for each of the rooms and for each of the weapons. We have one propositional symbol for each of these ideas. Then using those propositional symbols, we can begin to create logical sentences, create knowledge that we know about the world. So for example, we know that someone is the murderer, that one of the three people is, in fact, the murderer. And how would we encode that? Well, we don’t know for sure who the murderer is. But we know it is one person or the second person or the third person. So I could say something like this. Mustard or Plum or Scarlet. And this piece of knowledge encodes that one of these three people is the murderer. We don’t know which, but one of these three things must be true. What other information do we know? Well, we know that, for example, one of the rooms must have been the room in the envelope. The crime was committed either in the ballroom or the kitchen or the library. Again, right now, we don’t know which. But this is knowledge we know at the outset, knowledge that one of these three must be inside the envelope. And likewise, we can say the same thing about the weapon, that it was either the knife or the revolver or the wrench, that one of those weapons must have been the weapon of choice and therefore the weapon in the envelope. And then as the game progresses, the gameplay works by people get various different cards. And using those cards, you can deduce information. That if someone gives you a card, for example, I have the Professor Plum card in my hand, then I know the Professor Plum card can’t be inside the envelope. I know that Professor Plum is not the criminal, so I know a piece of information like not Plum, for example. I know that Professor Plum has to be false. This propositional symbol is not true. And sometimes I might not know for sure that a particular card is not in the middle, but sometimes someone will make a guess and I’ll know that one of three possibilities is not true. Someone will guess Colonel Mustard in the library with the revolver or something to that effect. And in that case, a card might be revealed that I don’t see. But if it is a card and it is either Colonel Mustard or the revolver or the library, then I know that at least one of them can’t be in the middle. So I know something like it is either not Mustard or it is not the library or it is not the revolver. Now maybe multiple of these are not true, but I know that at least one of Mustard, Library, and Revolver must, in fact, be false. And so this now is a propositional logic representation of this game of Clue, a way of encoding the knowledge that we know inside this game using propositional logic that a computer algorithm, something like model checking that we saw a moment ago, can actually look at and understand. So let’s now take a look at some code to see how this algorithm might actually work in practice. All right, so I’m now going to open up a file called Clue.py, which I’ve started already. And what we’ll see here is I’ve defined a couple of things. To find some symbols initially, notice I have a symbol for Colonel Mustard, a symbol for Professor Plum, a symbol for Miss Scarlett, all of which I’ve put inside of this list of characters. I have a symbol for Ballroom and Kitchen and Library inside of a list of rooms. And then I have symbols for Knife and Revolver and Wrench. These are my weapons. And so all of these characters and rooms and weapons altogether, those are my symbols. And now I also have this check knowledge function. And what the check knowledge function does is it takes my knowledge and it’s going to try and draw conclusions about what I know. So for example, we’ll loop over all of the possible symbols and we’ll check, do I know that that symbol is true? And a symbol is going to be something like Professor Plum or the Knife or the Library. And if I know that it is true, in other words, I know that it must be the card in the envelope, then I’m going to print out using a function called cprint, which prints things in color. I’m going to print out the word yes, and I’m going to print that in green, just to make it very clear to us. If we’re not sure that the symbol is true, maybe I can check to see if I’m sure that the symbol is not true. Like if I know for sure that it is not Professor Plum, for example. And I do that by running model check again, this time checking if my knowledge is not the symbol, if I know for sure that the symbol is not true. And if I don’t know for sure that the symbol is not true, because I say if not model check, meaning I’m not sure that the symbol is false, well, then I’ll go ahead and print out maybe next to the symbol. Because maybe the symbol is true, maybe it’s not, I don’t actually know. So what knowledge do I actually have? Well, let’s try and represent my knowledge now. So my knowledge is, I know a couple of things, so I’ll put them in an and. And I know that one of the three people must be the criminal. So I know or mustard, plum, scarlet. This is my way of encoding that it is either Colonel Mustard or Professor Plum or Miss Scarlet. I know that it must have happened in one of the rooms. So I know or ballroom, kitchen, library, for example. And I know that one of the weapons must have been used as well. So I know or knife, revolver, wrench. So that might be my initial knowledge, that I know that it must have been one of the people, I know it must have been in one of the rooms, and I know that it must have been one of the weapons. And I can see what that knowledge looks like as a formula by printing out knowledge.formula. So I’ll run python clue.py. And here now is the information that I know in logical format. I know that it is Colonel Mustard or Professor Plum or Miss Scarlet. And I know that it is the ballroom, the kitchen, or the library. And I know that it is the knife, the revolver, or the wrench. But I don’t know much more than that. I can’t really draw any firm conclusions. And in fact, we can see that if I try and do, let me go ahead and run my knowledge check function on my knowledge. Knowledge check is this function that I, or check knowledge rather, is this function that I just wrote that looks over all of the symbols and tries to see what conclusions I can actually draw about any of the symbols. So I’ll go ahead and run clue.py and see what it is that I know. And it seems that I don’t really know anything for sure. I have all three people are maybes, all three of the rooms are maybes, all three of the weapons are maybes. I don’t really know anything for certain just yet. But now let me try and add some additional information and see if additional information, additional knowledge, can help us to logically reason our way through this process. And we are just going to provide the information. Our AI is going to take care of doing the inference and figuring out what conclusions it’s able to draw. So I start with some cards. And those cards tell me something. So if I have the kernel mustard card, for example, I know that the mustard symbol must be false. In other words, mustard is not the one in the envelope, is not the criminal. So I can say, knowledge supports something called, every and in this library supports dot add, which is a way of adding knowledge or adding an additional logical sentence to an and clause. So I can say, knowledge dot add, not mustard. I happen to know, because I have the mustard card, that kernel mustard is not the suspect. And maybe I have a couple of other cards too. Maybe I also have a card for the kitchen. So I know it’s not the kitchen. And maybe I have another card that says that it is not the revolver. So I have three cards, kernel mustard, the kitchen, and the revolver. And I encode that into my AI this way by saying, it’s not kernel mustard, it’s not the kitchen, and it’s not the revolver. And I know those to be true. So now, when I rerun clue.py, we’ll see that I’ve been able to eliminate some possibilities. Before, I wasn’t sure if it was the knife or the revolver or the wrench. If a knife was maybe, a revolver was maybe, wrench is maybe. Now I’m down to just the knife and the wrench. Between those two, I don’t know which one it is. They’re both maybes. But I’ve been able to eliminate the revolver, which is one that I know to be false, because I have the revolver card. And so additional information might be acquired over the course of this game. And we would represent that just by adding knowledge to our knowledge set or knowledge base that we’ve been building here. So if, for example, we additionally got the information that someone made a guess, someone guessed like Miss Scarlet in the library with the wrench. And we know that a card was revealed, which means that one of those three cards, either Miss Scarlet or the library or the wrench, one of those at minimum must not be inside of the envelope. So I could add some knowledge, say knowledge.add. And I’m going to add an or clause, because I don’t know for sure which one it’s not, but I know one of them is not in the envelope. So it’s either not Scarlet, or it’s not the library, and or supports multiple arguments. I can say it’s also or not the wrench. So at least one of those needs a Scarlet library and wrench. At least one of those needs to be false. I don’t know which, though. Maybe it’s multiple. Maybe it’s just one, but at least one I know needs to hold. And so now if I rerun clue.py, I don’t actually have any additional information just yet. Nothing I can say conclusively. I still know that maybe it’s Professor Plum, maybe it’s Miss Scarlet. I haven’t eliminated any options. But let’s imagine that I get some more information, that someone shows me the Professor Plum card, for example. So I say, all right, let’s go back here, knowledge.add, not Plum. So I have the Professor Plum card. I know the Professor Plum is not in the middle. I rerun clue.py. And right now, I’m able to draw some conclusions. Now I’ve been able to eliminate Professor Plum, and the only person it could left remaining be is Miss Scarlet. So I know, yes, Miss Scarlet, this variable must be true. And I’ve been able to infer that based on the information I already had. Now between the ballroom and the library and the knife and the wrench, for those two, I’m still not sure. So let’s add one more piece of information. Let’s say that I know that it’s not the ballroom. Someone has shown me the ballroom card, so I know it’s not the ballroom. Which means at this point, I should be able to conclude that it’s the library. Let’s see. I’ll say knowledge.add, not the ballroom. And we’ll go ahead and run that. And it turns out that after all of this, not only can I conclude that I know that it’s the library, but I also know that the weapon was the knife. And that might have been an inference that was a little bit trickier, something I wouldn’t have realized immediately, but the AI, via this model checking algorithm, is able to draw that conclusion, that we know for sure that it must be Miss Scarlet in the library with the knife. And how did we know that? Well, we know it from this or clause up here, that we know that it’s either not Scarlet, or it’s not the library, or it’s not the wrench. And given that we know that it is Miss Scarlet, and we know that it is the library, then the only remaining option for the weapon is that it is not the wrench, which means that it must be the knife. So we as humans now can go back and reason through that, even though it might not have been immediately clear. And that’s one of the advantages of using an AI or some sort of algorithm in order to do this, is that the computer can exhaust all of these possibilities and try and figure out what the solution actually should be. And so for that reason, it’s often helpful to be able to represent knowledge in this way. Knowledge engineering, some situation where we can use a computer to be able to represent knowledge and draw conclusions based on that knowledge. And any time we can translate something into propositional logic symbols like this, this type of approach can be useful. So you might be familiar with logic puzzles, where you have to puzzle your way through trying to figure something out. This is what a classic logic puzzle might look like. Something like Gilderoy, Minerva, Pomona, and Horace each belong to a different one of the four houses, Gryffindor, Hufflepuff, Ravenclaw, and Slytherin. And then we have some information. The Gilderoy belongs to Gryffindor or Ravenclaw, Pomona does not belong in Slytherin, and Minerva does belong to Gryffindor. So we have a couple pieces of information. And using that information, we need to be able to draw some conclusions about which person should be assigned to which house. And again, we can use the exact same idea to try and implement this notion. So we need some propositional symbols. And in this case, the propositional symbols are going to get a little more complex, although we’ll see ways to make this a little bit cleaner later on. But we’ll need 16 propositional symbols, one for each person and house. So we need to say, remember, every propositional symbol is either true or false. So Gilderoy Gryffindor is either true or false. Either he’s in Gryffindor or he is not. Likewise, Gilderoy Hufflepuff also true or false. Either it is true or it’s false. And that’s true for every combination of person and house that we could come up with. We have some sort of propositional symbol for each one of those. Using this type of knowledge, we can then begin to think about what types of logical sentences we can say about the puzzle. That if we know what will before even think about the information we were given, we can think about the premise of the problem, that every person is assigned to a different house. So what does that tell us? Well, it tells us sentences like this. It tells us like Pomona Slytherin implies not Pomona Hufflepuff. Something like if Pomona is in Slytherin, then we know that Pomona is not in Hufflepuff. And we know this for all four people and for all combinations of houses, that no matter what person you pick, if they’re in one house, then they’re not in some other house. So I’ll probably have a whole bunch of knowledge statements that are of this form, that if we know Pomona is in Slytherin, then we know Pomona is not in Hufflepuff. We were also given the information that each person is in a different house. So I also have pieces of knowledge that look something like this. Minerva Ravenclaw implies not Gilderoy Ravenclaw. If they’re all in different houses, then if Minerva is in Ravenclaw, then we know the Gilderoy is not in Ravenclaw as well. And I have a whole bunch of similar sentences like this that are expressing that idea for other people and other houses as well. And so in addition to sentences of these form, I also have the knowledge that was given to me. Information like Gilderoy was in Gryffindor or in Ravenclaw that would be represented like this, Gilderoy Gryffindor or Gilderoy Ravenclaw. And then using these sorts of sentences, I can begin to draw some conclusions about the world. So let’s see an example of this. We’ll go ahead and actually try and implement this logic puzzle to see if we can figure out what the answer is. I’ll go ahead and open up puzzle.py, where I’ve already started to implement this sort of idea. I’ve defined a list of people and a list of houses. And I’ve so far created one symbol for every person and for every house. That’s what this double four loop is doing, looping over all people, looping over all houses, creating a new symbol for each of them. And then I’ve added some information. I know that every person belongs to a house, so I’ve added the information for every person that person Gryffindor or person Hufflepuff or person Ravenclaw or person Slytherin, that one of those four things must be true. Every person belongs to a house. What other information do I know? I also know that only one house per person, so no person belongs to multiple houses. So how does this work? Well, this is going to be true for all people. So I’ll loop over every person. And then I need to loop over all different pairs of houses. The idea is I want to encode the idea that if Minerva is in Gryffindor, then Minerva can’t be in Ravenclaw. So I’ll loop over all houses, each one. And I’ll loop over all houses again, h2. And as long as they’re different, h1 not equal to h2, then I’ll add to my knowledge base this piece of information. That implication, in other words, an if then, if the person is in h1, then I know that they are not in house h2. So these lines here are encoding the notion that for every person, if they belong to house one, then they are not in house two. And the other piece of logic we need to encode is the idea that every house can only have one person. In other words, if Pomona is in Hufflepuff, then nobody else is allowed to be in Hufflepuff either. And that’s the same logic, but sort of backwards. I loop over all of the houses and loop over all different pairs of people. So I loop over people once, loop over people again, and only do this when the people are different, p1 not equal to p2. And I add the knowledge that if, as given by the implication, if person one belongs to the house, then it is not the case that person two belongs to the same house. So here I’m just encoding the knowledge that represents the problem’s constraints. I know that everyone’s in a different house. I know that any person can only belong to one house. And I can now take my knowledge and try and print out the information that I happen to know. So I’ll go ahead and print out knowledge.formula, just to see this in action, and I’ll go ahead and skip this for now. But we’ll come back to this in a second. Let’s print out the knowledge that I know by running Python puzzle.py. It’s a lot of information, a lot that I have to scroll through, because there are 16 different variables all going on. But the basic idea, if we scroll up to the very top, is I see my initial information. Gilderoy is either in Gryffindor, or Gilderoy is in Hufflepuff, or Gilderoy is in Ravenclaw, or Gilderoy is in Slytherin, and then way more information as well. So this is quite messy, more than we really want to be looking at. And soon, too, we’ll see ways of representing this a little bit more nicely using logic. But for now, we can just say these are the variables that we’re dealing with. And now we’d like to add some information. So the information we’re going to add is Gilderoy is in Gryffindor, or he is in Ravenclaw. So that knowledge was given to us. So I’ll go ahead and say knowledge.add. And I know that either or Gilderoy Gryffindor or Gilderoy Ravenclaw. One of those two things must be true. I also know that Pomona was not in Slytherin, so I can say knowledge.add not this symbol, not the Pomona-Slytherin symbol. And then I can add the knowledge that Minerva is in Gryffindor by adding the symbol Minerva Gryffindor. So those are the pieces of knowledge that I know. And this loop here at the bottom just loops over all of my symbols, checks to see if the knowledge entails that symbol by calling this model check function again. And if it does, if we know the symbol is true, we print out the symbol. So now I can run Python, puzzle.py, and Python is going to solve this puzzle for me. We’re able to conclude that Gilderoy belongs to Ravenclaw, Pomona belongs to Hufflepuff, Minerva to Gryffindor, and Horace to Slytherin just by encoding this knowledge inside the computer, although it was quite tedious to do in this case. And as a result, we were able to get the conclusion from that as well. And you can imagine this being applied to many sorts of different deductive situations. So not only these situations where we’re trying to deal with Harry Potter characters in this puzzle, but if you’ve ever played games like Mastermind, where you’re trying to figure out which order different colors go in and trying to make predictions about it, I could tell you, for example, let’s play a simplified version of Mastermind where there are four colors, red, blue, green, and yellow, and they’re in some order, but I’m not telling you what order. You just have to make a guess, and I’ll tell you of red, blue, green, and yellow how many of the four you got in the right position. So a simplified version of this game, you might make a guess like red, blue, green, yellow, and I would tell you something like two of those four are in the correct position, but the other two are not. And then you could reasonably make a guess and say, all right, look at this, blue, red, green, yellow. Try switching two of them around, and this time maybe I tell you, you know what, none of those are in the correct position. And the question then is, all right, what is the correct order of these four colors? And we as humans could begin to reason this through. All right, well, if none of these were correct, but two of these were correct, well, it must have been because I switched the red and the blue, which means red and blue here must be correct, which means green and yellow are probably not correct. You can begin to do this sort of deductive reasoning. And we can also equivalently try and take this and encode it inside of our computer as well. And it’s going to be very similar to the logic puzzle that we just did a moment ago. So I won’t spend too much time on this code because it is fairly similar. But again, we have a whole bunch of colors and four different positions in which those colors can be. And then we have some additional knowledge. And I encode all of that knowledge. And you can take a look at this code on your own time. But I just want to demonstrate that when we run this code, run python mastermind.py and run and see what we get, we ultimately are able to compute red 0 in the 0 position, blue in the 1 position, yellow in the 2 position, and green in the 3 position as the ordering of those symbols. Now, ultimately, what you might have noticed is this process was taking quite a long time. And in fact, model checking is not a particularly efficient algorithm, right? What I need to do in order to model check is take all of my possible different variables and enumerate all of the possibilities that they could be in. If I have n variables, I have 2 to the n possible worlds that I need to be looking through in order to perform this model checking algorithm. And this is probably not tractable, especially as we start to get to much larger and larger sets of data where you have many, many more variables that are at play. Right here, we only have a relatively small number of variables. So this sort of approach can actually work. But as the number of variables increases, model checking becomes less and less good of a way of trying to solve these sorts of problems. So while it might have been OK for something like Mastermind to conclude that this is indeed the correct sequence where all four are in the correct position, what we’d like to do is come up with some better ways to be able to make inferences rather than just enumerate all of the possibilities. And to do so, what we’ll transition to next is the idea of inference rules, some sort of rules that we can apply to take knowledge that already exists and translate it into new forms of knowledge. And the general way we’ll structure an inference rule is by having a horizontal line here. Anything above the line is going to represent a premise, something that we know to be true. And then anything below the line will be the conclusion that we can arrive at after we apply the logic from the inference rule that we’re going to demonstrate. So we’ll do some of these inference rules by demonstrating them in English first, but then translating them into the world of propositional logic so you can see what those inference rules actually look like. So for example, let’s imagine that I have access to two pieces of information. I know, for example, that if it is raining, then Harry is inside, for example. And let’s say I also know it is raining. Then most of us could reasonably then look at this information and conclude that, all right, Harry must be inside. This inference rule is known as modus ponens, and it’s phrased more formally in logic as this. If we know that alpha implies beta, in other words, if alpha, then beta, and we also know that alpha is true, then we should be able to conclude that beta is also true. We can apply this inference rule to take these two pieces of information and generate this new piece of information. Notice that this is a totally different approach from the model checking approach, where the approach was look at all of the possible worlds and see what’s true in each of these worlds. Here, we’re not dealing with any specific world. We’re just dealing with the knowledge that we know and what conclusions we can arrive at based on that knowledge. That I know that A implies B, and I know A, and the conclusion is B. And this should seem like a relatively obvious rule. But of course, if alpha, then beta, and we know alpha, then we should be able to conclude that beta is also true. And that’s going to be true for many, but maybe even all of the inference rules that we’ll take a look at. You should be able to look at them and say, yeah, of course that’s going to be true. But it’s putting these all together, figuring out the right combination of inference rules that can be applied that ultimately is going to allow us to generate interesting knowledge inside of our AI. So that’s modus ponensis application of implication, that if we know alpha and we know that alpha implies beta, then we can conclude beta. Let’s take a look at another example. Fairly straightforward, something like Harry is friends with Ron and Hermione. Based on that information, we can reasonably conclude Harry is friends with Hermione. That must also be true. And this inference rule is known as and elimination. And what and elimination says is that if we have a situation where alpha and beta are both true, I have information alpha and beta, well then, just alpha is true. Or likewise, just beta is true. That if I know that both parts are true, then one of those parts must also be true. Again, something obvious from the point of view of human intuition, but a computer needs to be told this kind of information. To be able to apply the inference rule, we need to tell the computer that this is an inference rule that you can apply, so the computer has access to it and is able to use it in order to translate information from one form to another. In addition to that, let’s take a look at another example of an inference rule, something like it is not true that Harry did not pass the test. Bit of a tricky sentence to parse. I’ll read it again. It is not true, or it is false, that Harry did not pass the test. Well, if it is false that Harry did not pass the test, then the only reasonable conclusion is that Harry did pass the test. And so this, instead of being and elimination, is what we call double negation elimination. That if we have two negatives inside of our premise, then we can just remove them altogether. They cancel each other out. One turns true to false, and the other one turns false back into true. Phrased a little bit more formally, we say that if the premise is not alpha, then the conclusion we can draw is just alpha. We can say that alpha is true. We’ll take a look at a couple more of these. If I have it is raining, then Harry is inside. How do I reframe this? Well, this one is a little bit trickier. But if I know if it is raining, then Harry is inside, then I conclude one of two things must be true. Either it is not raining, or Harry is inside. Now, this one’s trickier. So let’s think about it a little bit. This first premise here, if it is raining, then Harry is inside, is saying that if I know that it is raining, then Harry must be inside. So what is the other possible case? Well, if Harry is not inside, then I know that it must not be raining. So one of those two situations must be true. Either it’s not raining, or it is raining, in which case Harry is inside. So the conclusion I can draw is either it is not raining, or it is raining, so therefore, Harry is inside. And so this is a way to translate if-then statements into or statements. And this is known as implication elimination. And this is similar to what we actually did in the beginning when we were first looking at those very first sentences about Harry and Hagrid and Dumbledore. And phrased a little bit more formally, this says that if I have the implication, alpha implies beta, that I can draw the conclusion that either not alpha or beta, because there are only two possibilities. Either alpha is true or alpha is not true. So one of those possibilities is alpha is not true. But if alpha is true, well, then we can draw the conclusion that beta must be true. So either alpha is not true or alpha is true, in which case beta is also true. So this is one way to turn an implication into just a statement about or. In addition to eliminating implications, we can also eliminate biconditionals as well. So let’s take an English example, something like, it is raining if and only if Harry is inside. And this if and only if really sounds like that biconditional, that double arrow sign that we saw in propositional logic not too long ago. And what does this actually mean if we were to translate this? Well, this means that if it is raining, then Harry is inside. And if Harry is inside, then it is raining, that this implication goes both ways. And this is what we would call biconditional elimination, that I can take a biconditional, a if and only if b, and translate that into something like this, a implies b, and b implies a. So many of these inference rules are taking logic that uses certain symbols and turning them into different symbols, taking an implication and turning it into an or, or taking a biconditional and turning it into implication. And another example of it would be something like this. It is not true that both Harry and Ron passed the test. Well, all right, how do we translate that? What does that mean? Well, if it is not true that both of them passed the test, well, then the reasonable conclusion we might draw is that at least one of them didn’t pass the test. So the conclusion is either Harry did not pass the test or Ron did not pass the test, or both. This is not an exclusive or. But if it is true that it is not true that both Harry and Ron passed the test, well, then either Harry didn’t pass the test or Ron didn’t pass the test. And this type of law is one of De Morgan’s laws. Quite famous in logic where the idea is that we can turn an and into an or. We can say we can take this and that both Harry and Ron passed the test and turn it into an or by moving the nots around. So if it is not true that Harry and Ron passed the test, well, then either Harry did not pass the test or Ron did not pass the test either. And the way we frame that more formally using logic is to say this. If it is not true that alpha and beta, well, then either not alpha or not beta. The way I like to think about this is that if you have a negation in front of an and expression, you move the negation inwards, so to speak, moving the negation into each of these individual sentences and then flip the and into an or. So the negation moves inwards and the and flips into an or. So I go from not a and b to not a or not b. And there’s actually a reverse of De Morgan’s law that goes in the other direction for something like this. If I say it is not true that Harry or Ron passed the test, meaning neither of them passed the test, well, then the conclusion I can draw is that Harry did not pass the test and Ron did not pass the test. So in this case, instead of turning an and into an or, we’re turning an or into an and. But the idea is the same. And this, again, is another example of De Morgan’s laws. And the way that works is that if I have not a or b this time, the same logic is going to apply. I’m going to move the negation inwards. And I’m going to flip this time, flip the or into an and. So if not a or b, meaning it is not true that a or b or alpha or beta, then I can say not alpha and not beta, moving the negation inwards in order to make that conclusion. So those are De Morgan’s laws and a couple other inference rules that are worth just taking a look at. One is the distributive law that works this way. So if I have alpha and beta or gamma, well, then much in the same way that you can use in math, use distributive laws to distribute operands like addition and multiplication, I can do a similar thing here, where I can say if alpha and beta or gamma, then I can say something like alpha and beta or alpha and gamma, that I’ve been able to distribute this and sign throughout this expression. So this is an example of the distributive property or the distributive law as applied to logic in much the same way that you would distribute a multiplication over the addition of something, for example. This works the other way too. So if, for example, I have alpha or beta and gamma, I can distribute the or throughout the expression. I can say alpha or beta and alpha or gamma. So the distributive law works in that way too. And it’s helpful if I want to take an or and move it into the expression. And we’ll see an example soon of why it is that we might actually care to do something like that. All right, so now we’ve seen a lot of different inference rules. And the question now is, how can we use those inference rules to actually try and draw some conclusions, to actually try and prove something about entailment, proving that given some initial knowledge base, we would like to find some way to prove that a query is true? Well, one way to think about it is actually to think back to what we talked about last time when we talked about search problems. Recall again that search problems have some sort of initial state. They have actions that you can take from one state to another as defined by a transition model that tells you how to get from one state to another. We talked about testing to see if you were at a goal. And then some path cost function to see how many steps did you have to take or how costly was the solution that you found. Now that we have these inference rules that take some set of sentences in propositional logic and get us some new set of sentences in propositional logic, we can actually treat those sentences or those sets of sentences as states inside of a search problem. So if we want to prove that some query is true, prove that some logical theorem is true, we can treat theorem proving as a form of a search problem. I can say that we begin in some initial state, where that initial state is the knowledge base that I begin with, the set of all of the sentences that I know to be true. What actions are available to me? Well, the actions are any of the inference rules that I can apply at any given time. The transition model just tells me after I apply the inference rule, here is the new set of all of the knowledge that I have, which will be the old set of knowledge, plus some additional inference that I’ve been able to draw, much as in the same way we saw what we got when we applied those inference rules and got some sort of conclusion. That conclusion gets added to our knowledge base, and our transition model will encode that. What is the goal test? Well, our goal test is checking to see if we have proved the statement we’re trying to prove, if the thing we’re trying to prove is inside of our knowledge base. And the path cost function, the thing we’re trying to minimize, is maybe the number of inference rules that we needed to use, the number of steps, so to speak, inside of our proof. And so here we’ve been able to apply the same types of ideas that we saw last time with search problems to something like trying to prove something about knowledge by taking our knowledge and framing it in terms that we can understand as a search problem with an initial state, with actions, with a transition model. So this shows a couple of things, one being how versatile search problems are, that they can be the same types of algorithms that we use to solve a maze or figure out how to get from point A to point B inside of driving directions, for example, can also be used as a theorem proving method of taking some sort of starting knowledge base and trying to prove something about that knowledge. So this, yet again, is a second way, in addition to model checking, to try and prove that certain statements are true. But it turns out there’s yet another way that we can try and apply inference. And we’ll talk about this now, which is not the only way, but certainly one of the most common, which is known as resolution. And resolution is based on another inference rule that we’ll take a look at now, quite a powerful inference rule that will let us prove anything that can be proven about a knowledge base. And it’s based on this basic idea. Let’s say I know that either Ron is in the Great Hall or Hermione is in the library. And let’s say I also know that Ron is not in the Great Hall. Based on those two pieces of information, what can I conclude? Well, I could pretty reasonably conclude that Hermione must be in the library. How do I know that? Well, it’s because these two statements, these two what we’ll call complementary literals, literals that complement each other, they’re opposites of each other, seem to conflict with each other. This sentence tells us that either Ron is in the Great Hall or Hermione is in the library. So if we know that Ron is not in the Great Hall, that conflicts with this one, which means Hermione must be in the library. And this we can frame as a more general rule known as the unit resolution rule, a rule that says that if we have p or q and we also know not p, well then from that we can reasonably conclude q. That if p or q are true and we know that p is not true, the only possibility is for q to then be true. And this, it turns out, is quite a powerful inference rule in terms of what it can do, in part because we can quickly start to generalize this rule. This q right here doesn’t need to just be a single propositional symbol. It could be multiple, all chained together in a single clause, as we’ll call it. So if I had something like p or q1 or q2 or q3, so on and so forth, up until qn, so I had n different other variables, and I have not p, well then what happens when these two complement each other is that these two clauses resolve, so to speak, to produce a new clause that is just q1 or q2 all the way up to qn. And in an or, the order of the arguments in the or doesn’t actually matter. The p doesn’t need to be the first thing. It could have been in the middle. But the idea here is that if I have p in one clause and not p in the other clause, well then I know that one of these remaining things must be true. I’ve resolved them in order to produce a new clause. But it turns out we can generalize this idea even further, in fact, and display even more power that we can have with this resolution rule. So let’s take another example. Let’s say, for instance, that I know the same piece of information that either Ron is in the Great Hall or Hermione is in the library. And the second piece of information I know is that Ron is not in the Great Hall or Harry is sleeping. So it’s not just a single piece of information. I have two different clauses. And we’ll define clauses more precisely in just a moment. What do I know here? Well again, for any propositional symbol like Ron is in the Great Hall, there are only two possibilities. Either Ron is in the Great Hall, in which case, based on resolution, we know that Harry must be sleeping, or Ron is not in the Great Hall, in which case we know based on the same rule that Hermione must be in the library. Based on those two things in combination, I can say based on these two premises that I can conclude that either Hermione is in the library or Harry is sleeping. So again, because these two conflict with each other, I know that one of these two must be true. And you can take a closer look and try and reason through that logic. Make sure you convince yourself that you believe this conclusion. Stated more generally, we can name this resolution rule by saying that if we know p or q is true, and we also know that not p or r is true, we resolve these two clauses together to get a new clause, q or r, that either q or r must be true. And again, much as in the last case, q and r don’t need to just be single propositional symbols. It could be multiple symbols. So if I had a rule that had p or q1 or q2 or q3, so on and so forth, up until qn, where n is just some number. And likewise, I had not p or r1 or r2, so on and so forth, up until rm, where m, again, is just some other number. I can resolve these two clauses together to get one of these must be true, q1 or q2 up until qn or r1 or r2 up until rm. And this is just a generalization of that same rule we saw before. Each of these things here are what we’re going to call a clause, where a clause is formally defined as a disjunction of literals, where a disjunction means it’s a bunch of things that are connected with or. Disjunction means things connected with or. Conjunction, meanwhile, is things connected with and. And a literal is either a propositional symbol or the opposite of a propositional symbol. So it’s something like p or q or not p or not q. Those are all propositional symbols or not of the propositional symbols. And we call those literals. And so a clause is just something like this, p or q or r, for example. Meanwhile, what this gives us an ability to do is it gives us an ability to turn logic, any logical sentence, into something called conjunctive normal form. A conjunctive normal form sentence is a logical sentence that is a conjunction of clauses. Recall, again, conjunction means things are connected to one another using and. And so a conjunction of clauses means it is an and of individual clauses, each of which has ors in it. So something like this, a or b or c, and d or not e, and f or g. Everything in parentheses is one clause. All of the clauses are connected to each other using an and. And everything in the clause is separated using an or. And this is just a standard form that we can translate a logical sentence into that just makes it easy to work with and easy to manipulate. And it turns out that we can take any sentence in logic and turn it into conjunctive normal form just by applying some inference rules and transformations to it. So we’ll take a look at how we can actually do that. So what is the process for taking a logical formula and converting it into conjunctive normal form, otherwise known as c and f? Well, the process looks a little something like this. We need to take all of the symbols that are not part of conjunctive normal form. The bi-conditionals and the implications and so forth, and turn them into something that is more closely like conjunctive normal form. So the first step will be to eliminate bi-conditionals, those if and only if double arrows. And we know how to eliminate bi-conditionals because we saw there was an inference rule to do just that. Any time I have an expression like alpha if and only if beta, I can turn that into alpha implies beta and beta implies alpha based on that inference rule we saw before. Likewise, in addition to eliminating bi-conditionals, I can eliminate implications as well, the if then arrows. And I can do that using the same inference rule we saw before too, taking alpha implies beta and turning that into not alpha or beta because that is logically equivalent to this first thing here. Then we can move knots inwards because we don’t want knots on the outsides of our expressions. Conjunctive normal form requires that it’s just claws and claws and claws and claws. Any knots need to be immediately next to propositional symbols. But we can move those knots around using De Morgan’s laws by taking something like not A and B and turn it into not A or not B, for example, using De Morgan’s laws to manipulate that. And after that, all we’ll be left with are ands and ors. And those are easy to deal with. We can use the distributive law to distribute the ors so that the ors end up on the inside of the expression, so to speak, and the ands end up on the outside. So this is the general pattern for how we’ll take a formula and convert it into conjunctive normal form. And let’s now take a look at an example of how we would do this and explore then why it is that we would want to do something like this. Here’s how we can do it. Let’s take this formula, for example. P or Q implies R. And I’d like to convert this into conjunctive normal form, where it’s all ands of clauses, and every clause is a disjunctive clause. It’s ors together. So what’s the first thing I need to do? Well, this is an implication. So let me go ahead and remove that implication. Using the implication inference rule, I can turn P or Q into P or Q implies R into not P or Q or R. So that’s the first step. I’ve gotten rid of the implication. And next, I can get rid of the not on the outside of this expression, too. I can move the nots inwards so they’re closer to the literals themselves by using De Morgan’s laws. And De Morgan’s law says that not P or Q is equivalent to not P and not Q. Again, here, just applying the inference rules that we’ve already seen in order to translate these statements. And now, I have two things that are separated by an or, where this thing on the inside is an and. What I’d really like to move the ors so the ors are on the inside, because conjunctive normal form means I need clause and clause and clause and clause. And so to do that, I can use the distributive law. If I have not P and not Q or R, I can distribute the or R to both of these to get not P or R and not Q or R using the distributive law. And this now here at the bottom is in conjunctive normal form. It is a conjunction and and of disjunctions of clauses that just are separated by ors. So this process can be used by any formula to take a logical sentence and turn it into this conjunctive normal form, where I have clause and clause and clause and clause and clause and so on. So why is this helpful? Why do we even care about taking all these sentences and converting them into this form? It’s because once they’re in this form where we have these clauses, these clauses are the inputs to the resolution inference rule that we saw a moment ago, that if I have two clauses where there’s something that conflicts or something complementary between those two clauses, I can resolve them to get a new clause, to draw a new conclusion. And we call this process inference by resolution, using the resolution rule to draw some sort of inference. And it’s based on the same idea, that if I have P or Q, this clause, and I have not P or R, that I can resolve these two clauses together to get Q or R as the resulting clause, a new piece of information that I didn’t have before. Now, a couple of key points that are worth noting about this before we talk about the actual algorithm. One thing is that, let’s imagine we have P or Q or S, and I also have not P or R or S. The resolution rule says that because this P conflicts with this not P, we would resolve to put everything else together to get Q or S or R or S. But it turns out that this double S is redundant, or S here and or S there. It doesn’t change the meaning of the sentence. So in resolution, when we do this resolution process, we’ll usually also do a process known as factoring, where we take any duplicate variables that show up and just eliminate them. So Q or S or R or S just becomes Q or R or S. The S only needs to appear once, no need to include it multiple times. Now, one final question worth considering is what happens if I try to resolve P and not P together? If I know that P is true and I know that not P is true, well, resolution says I can merge these clauses together and look at everything else. Well, in this case, there is nothing else, so I’m left with what we might call the empty clause. I’m left with nothing. And the empty clause is always false. The empty clause is equivalent to just being false. And that’s pretty reasonable because it’s impossible for both P and not P to both hold at the same time. P is either true or it’s not true, which means that if P is true, then this must be false. And if this is true, then this must be false. There is no way for both of these to hold at the same time. So if ever I try and resolve these two, it’s a contradiction, and I’ll end up getting this empty clause where the empty clause I can call equivalent to false. And this idea that if I resolve these two contradictory terms, I get the empty clause, this is the basis for our inference by resolution algorithm. Here’s how we’re going to perform inference by resolution at a very high level. We want to prove that our knowledge base entails some query alpha, that based on the knowledge we have, we can prove conclusively that alpha is going to be true. How are we going to do that? Well, in order to do that, we’re going to try to prove that if we know the knowledge and not alpha, that that would be a contradiction. And this is a common technique in computer science more generally, this idea of proving something by contradiction. If I want to prove that something is true, I can do so by first assuming that it is false and showing that it would be contradictory, showing that it leads to some contradiction. And if the thing I’m trying to prove, if when I assume it’s false, leads to a contradiction, then it must be true. And that’s the logical approach or the idea behind a proof by contradiction. And that’s what we’re going to do here. We want to prove that this query alpha is true. So we’re going to assume that it’s not true. We’re going to assume not alpha. And we’re going to try and prove that it’s a contradiction. If we do get a contradiction, well, then we know that our knowledge entails the query alpha. If we don’t get a contradiction, there is no entailment. This is this idea of a proof by contradiction of assuming the opposite of what you’re trying to prove. And if you can demonstrate that that’s a contradiction, then what you’re proving must be true. But more formally, how do we actually do this? How do we check that knowledge base and not alpha is going to lead to a contradiction? Well, here is where resolution comes into play. To determine if our knowledge base entails some query alpha, we’re going to convert knowledge base and not alpha to conjunctive normal form, that form where we have a whole bunch of clauses that are all anded together. And when we have these individual clauses, now we can keep checking to see if we can use resolution to produce a new clause. We can take any pair of clauses and check, is there some literal that is the opposite of each other or complementary to each other in both of them? For example, I have a p in one clause and a not p in another clause. Or an r in one clause and a not r in another clause. If ever I have that situation where once I convert to conjunctive normal form and I have a whole bunch of clauses, I see two clauses that I can resolve to produce a new clause, then I’ll do so. This process occurs in a loop. I’m going to keep checking to see if I can use resolution to produce a new clause and keep using those new clauses to try to generate more new clauses after that. Now, it just so may happen that eventually we may produce the empty clause, the clause we were talking about before. If I resolve p and not p together, that produces the empty clause and the empty clause we know to be false. Because we know that there’s no way for both p and not p to both simultaneously be true. So if ever we produce the empty clause, then we have a contradiction. And if we have a contradiction, that’s exactly what we were trying to do in a fruit by contradiction. If we have a contradiction, then we know that our knowledge base must entail this query alpha. And we know that alpha must be true. And it turns out, and we won’t go into the proof here, but you can show that otherwise, if you don’t produce the empty clause, then there is no entailment. If we run into a situation where there are no more new clauses to add, we’ve done all the resolution that we can do, and yet we still haven’t produced the empty clause, then there is no entailment in this case. And this now is the resolution algorithm. And it’s very abstract looking, especially this idea of like, what does it even mean to have the empty clause? So let’s take a look at an example, actually try and prove some entailment by using this inference by resolution process. So here’s our question. We have this knowledge base. Here is the knowledge that we know, A or B, and not B or C, and not C. And we want to know if all of this entails A. So this is our knowledge base here, this whole log thing. And our query alpha is just this propositional symbol, A. So what do we do? Well, first, we want to prove by contradiction. So we want to first assume that A is false, and see if that leads to some sort of contradiction. So here is what we’re going to start with, A or B, and not B or C, and not C. This is our knowledge base. And we’re going to assume not A. We’re going to assume that the thing we’re trying to prove is, in fact, false. And so this is now in conjunctive normal form, and I have four different clauses. I have A or B. I have not B or C. I have not C, and I have not A. And now, I can begin to just pick two clauses that I can resolve, and apply the resolution rule to them. And so looking at these four clauses, I see, all right, these two clauses are ones I can resolve. I can resolve them because there are complementary literals that show up in them. There’s a C here, and a not C here. So just looking at these two clauses, if I know that not B or C is true, and I know that C is not true, well, then I can resolve these two clauses to say, all right, not B, that must be true. I can generate this new clause as a new piece of information that I now know to be true. And all right, now I can repeat this process, do the process again. Can I use resolution again to get some new conclusion? Well, it turns out I can. I can use that new clause I just generated, along with this one here. There are complementary literals. This B is complementary to, or conflicts with, this not B over here. And so if I know that A or B is true, and I know that B is not true, well, then the only remaining possibility is that A must be true. So now we have A. That is a new clause that I’ve been able to generate. And now, I can do this one more time. I’m looking for two clauses that can be resolved, and you might programmatically do this by just looping over all possible pairs of clauses and checking for complementary literals in each. And here, I can say, all right, I found two clauses, not A and A, that conflict with each other. And when I resolve these two together, well, this is the same as when we were resolving P and not P from before. When I resolve these two clauses together, I get rid of the As, and I’m left with the empty clause. And the empty clause we know to be false, which means we have a contradiction, which means we can safely say that this whole knowledge base does entail A. That if this sentence is true, that we know that A for sure is also true. So this now, using inference by resolution, is an entirely different way to take some statement and try and prove that it is, in fact, true. Instead of enumerating all of the possible worlds that we might be in in order to try to figure out in which cases is the knowledge base true and in which cases are query true, instead we use this resolution algorithm to say, let’s keep trying to figure out what conclusions we can draw and see if we reach a contradiction. And if we reach a contradiction, then that tells us something about whether our knowledge actually entails the query or not. And it turns out there are many different algorithms that can be used for inference. What we’ve just looked at here are just a couple of them. And in fact, all of this is just based on one particular type of logic. It’s based on propositional logic, where we have these individual symbols and we connect them using and and or and not and implies and by conditionals. But propositional logic is not the only kind of logic that exists. And in fact, we see that there are limitations that exist in propositional logic, especially as we saw in examples like with the mastermind example or with the example with the logic puzzle where we had different Hogwarts house people that belong to different houses and we were trying to figure out who belonged to which houses. There were a lot of different propositional symbols that we needed in order to represent some fairly basic ideas. So now is the final topic that we’ll take a look at just before we end class today is one final type of logic different from propositional logic known as first order logic, which is a little bit more powerful than propositional logic and is going to make it easier for us to express certain types of ideas. In propositional logic, if we think back to that puzzle with the people in the Hogwarts houses, we had a whole bunch of symbols. And every symbol could only be true or false. We had a symbol for Minerva Gryffindor, which was either true of Minerva within Gryffindor and false otherwise, and likewise for Minerva Hufflepuff and Minerva Ravenclaw and Minerva Slytherin and so forth. But this was starting to get quite redundant. We wanted some way to be able to express that there is a relationship between these propositional symbols, that Minerva shows up in all of them. And also, I would have liked to have not have had so many different symbols to represent what really was a fairly straightforward problem. So first order logic will give us a different way of trying to deal with this idea by giving us two different types of symbols. We’re going to have constant symbols that are going to represent objects like people or houses. And then predicate symbols, which you can think of as relations or functions that take an input and evaluate them to true or false, for example, that tell us whether or not some property of some constant or some pair of constants or multiple constants actually holds. So we’ll see an example of that in just a moment. For now, in this same problem, our constant symbols might be objects, things like people or houses. So Minerva, Pomona, Horace, Gilderoy, those are all constant symbols, as are my four houses, Gryffindor, Hufflepuff, Ravenclaw, and Slytherin. Predicates, meanwhile, these predicate symbols are going to be properties that might hold true or false of these individual constants. So person might hold true of Minerva, but it would be false for Gryffindor because Gryffindor is not a person. And house is going to hold true for Ravenclaw, but it’s not going to hold true for Horace, for example, because Horace is a person. And belongs to, meanwhile, is going to be some relation that is going to relate people to their houses. And it’s going to only tell me when someone belongs to a house or does not. So let’s take a look at some examples of what a sentence in first order logic might actually look like. A sentence might look like something like this. Person Minerva, with Minerva in parentheses, and person being a predicate symbol, Minerva being a constant symbol. This sentence in first order logic effectively means Minerva is a person, or the person property applies to the Minerva object. So if I want to say something like Minerva is a person, here is how I express that idea using first order logic. Meanwhile, I can say something like, house Gryffindor, to likewise express the idea that Gryffindor is a house. I can do that this way. And all of the same logical connectives that we saw in propositional logic, those are going to work here too. And or implication by conditional not. In fact, I can use not to say something like, not house Minerva. And this sentence in first order logic means something like, Minerva is not a house. It is not true that the house property applies to Minerva. Meanwhile, in addition to some of these predicate symbols that just take a single argument, some of our predicate symbols are going to express binary relations, relations between two of its arguments. So I could say something like, belongs to, and then two inputs, Minerva and Gryffindor, to express the idea that Minerva belongs to Gryffindor. And so now here’s the key difference, or one of the key differences, between this and propositional logic. In propositional logic, I needed one symbol for Minerva Gryffindor, and one symbol for Minerva Hufflepuff, and one symbol for all the other people’s Gryffindor and Hufflepuff variables. In this case, I just need one symbol for each of my people, and one symbol for each of my houses. And then I can express as a predicate something like, belongs to, and say, belongs to Minerva Gryffindor, to express the idea that Minerva belongs to Gryffindor House. So already we can see that first order logic is quite expressive in being able to express these sorts of sentences using the existing constant symbols and predicates that already exist, while minimizing the number of new symbols that I need to create. I can just use eight symbols for people for houses, instead of 16 symbols for every possible combination of each. But first order logic gives us a couple of additional features that we can use to express even more complex ideas. And these more additional features are generally known as quantifiers. And there are two main quantifiers in first order logic, the first of which is universal quantification. Universal quantification lets me express an idea like something is going to be true for all values of a variable. Like for all values of x, some statement is going to hold true. So what might a sentence in universal quantification look like? Well, we’re going to use this upside down a to mean for all. So upside down ax means for all values of x, where x is any object, this is going to hold true. Belongs to x Gryffindor implies not belongs to x Hufflepuff. So let’s try and parse this out. This means that for all values of x, if this holds true, if x belongs to Gryffindor, then this does not hold true. x does not belong to Hufflepuff. So translated into English, this sentence is saying something like for all objects x, if x belongs to Gryffindor, then x does not belong to Hufflepuff, for example. Or a phrase even more simply, anyone in Gryffindor is not in Hufflepuff, simplified way of saying the same thing. So this universal quantification lets us express an idea like something is going to hold true for all values of a particular variable. In addition to universal quantification though, we also have existential quantification. Whereas universal quantification said that something is going to be true for all values of a variable, existential quantification says that some expression is going to be true for some value of a variable, at least one value of the variable. So let’s take a look at a sample sentence using existential quantification. One such sentence looks like this. There exists an x. This backwards e stands for exists. And here we’re saying there exists an x such that house x and belongs to Minerva x. In other words, there exists some object x where x is a house and Minerva belongs to x. Or phrased a little more succinctly in English, I’m here just saying Minerva belongs to a house. There’s some object that is a house and Minerva belongs to a house. And combining this universal and existential quantification, we can create far more sophisticated logical statements than we were able to just using propositional logic. I could combine these to say something like this. For all x, person x implies there exists a y such that house y and belongs to xy. All right. So a lot of stuff going on there, a lot of symbols. Let’s try and parse it out and just understand what it’s saying. Here we’re saying that for all values of x, if x is a person, then this is true. So in other words, I’m saying for all people, and we call that person x, this statement is going to be true. What statement is true of all people? Well, there exists a y that is a house, so there exists some house, and x belongs to y. In other words, I’m saying that for all people out there, there exists some house such that x, the person, belongs to y, the house. This is phrased more succinctly. I’m saying that every person belongs to a house, that for all x, if x is a person, then there exists a house that x belongs to. And so we can now express a lot more powerful ideas using this idea now of first order logic. And it turns out there are many other kinds of logic out there. There’s second order logic and other higher order logic, each of which allows us to express more and more complex ideas. But all of it, in this case, is really in pursuit of the same goal, which is the representation of knowledge. We want our AI agents to be able to know information, to represent that information, whether that’s using propositional logic or first order logic or some other logic, and then be able to reason based on that, to be able to draw conclusions, make inferences, figure out whether there’s some sort of entailment relationship, as by using some sort of inference algorithm, something like inference by resolution or model checking or any number of these other algorithms that we can use in order to take information that we know and translate it to additional conclusions. So all of this has helped us to create AI that is able to represent information about what it knows and what it doesn’t know. Next time, though, we’ll take a look at how we can make our AI even more powerful by not just encoding information that we know for sure to be true and not to be true, but also to take a look at uncertainty, to look at what happens if AI thinks that something might be probable or maybe not very probable or somewhere in between those two extremes, all in the pursuit of trying to build our intelligent systems to be even more intelligent. We’ll see you next time. Thank you. All right, welcome back, everyone, to an introduction to artificial intelligence with Python. And last time, we took a look at how it is that AI inside of our computers can represent knowledge. We represented that knowledge in the form of logical sentences in a variety of different logical languages. And the idea was we wanted our AI to be able to represent knowledge or information and somehow use those pieces of information to be able to derive new pieces of information by inference, to be able to take some information and deduce some additional conclusions based on the information that it already knew for sure. But in reality, when we think about computers and we think about AI, very rarely are our machines going to be able to know things for sure. Oftentimes, there’s going to be some amount of uncertainty in the information that our AIs or our computers are dealing with, where it might believe something with some probability, as we’ll soon discuss what probability is all about and what it means, but not entirely for certain. And we want to use the information that it has some knowledge about, even if it doesn’t have perfect knowledge, to still be able to make inferences, still be able to draw conclusions. So you might imagine, for example, in the context of a robot that has some sensors and is exploring some environment, it might not know exactly where it is or exactly what’s around it, but it does have access to some data that can allow it to draw inferences with some probability. There’s some likelihood that one thing is true or another. Or you can imagine in context where there is a little bit more randomness and uncertainty, something like predicting the weather, where you might not be able to know for sure what tomorrow’s weather is with 100% certainty, but you can probably infer with some probability what tomorrow’s weather is going to be based on maybe today’s weather and yesterday’s weather and other data that you might have access to as well. And so oftentimes, we can distill this in terms of just possible events that might happen and what the likelihood of those events are. This comes a lot in games, for example, where there is an element of chance inside of those games. So you imagine rolling a dice. You’re not sure exactly what the die roll is going to be, but you know it’s going to be one of these possibilities from 1 to 6, for example. And so here now, we introduce the idea of probability theory. And what we’ll take a look at today is beginning by looking at the mathematical foundations of probability theory, getting an understanding for some of the key concepts within probability, and then diving into how we can use probability and the ideas that we look at mathematically to represent some ideas in terms of models that we can put into our computers in order to program an AI that is able to use information about probability to draw inferences, to make some judgments about the world with some probability or likelihood of being true. So probability ultimately boils down to this idea that there are possible worlds that we’re here representing using this little Greek letter omega. And the idea of a possible world is that when I roll a die, there are six possible worlds that could result from it. I could roll a 1, or a 2, or a 3, or a 4, or a 5, or a 6. And each of those are a possible world. And each of those possible worlds has some probability of being true, the probability that I do roll a 1, or a 2, or a 3, or something else. And we represent that probability like this, using the capital letter P. And then in parentheses, what it is that we want the probability of. So this right here would be the probability of some possible world as represented by the little letter omega. Now, there are a couple of basic axioms of probability that become relevant as we consider how we deal with probability and how we think about it. First and foremost, every probability value must range between 0 and 1 inclusive. So the smallest value any probability can have is the number 0, which is an impossible event. Something like I roll a die, and the die is a 7 is the roll that I get. If the die only has numbers 1 through 6, the event that I roll a 7 is impossible, so it would have probability 0. And on the other end of the spectrum, probability can range all the way up to the positive number 1, meaning an event is certain to happen, that I roll a die and the number is less than 10, for example. That is an event that is guaranteed to happen if the only sides on my die are 1 through 6, for instance. And then they can range through any real number in between these two values. Where, generally speaking, a higher value for the probability means an event is more likely to take place, and a lower value for the probability means the event is less likely to take place. And the other key rule for probability looks a little bit like this. This sigma notation, if you haven’t seen it before, refers to summation, the idea that we’re going to be adding up a whole sequence of values. And this sigma notation is going to come up a couple of times today, because as we deal with probability, oftentimes we’re adding up a whole bunch of individual values or individual probabilities to get some other value. So we’ll see this come up a couple of times. But what this notation means is that if I sum up all of the possible worlds omega that are in big omega, which represents the set of all the possible worlds, meaning I take for all of the worlds in the set of possible worlds and add up all of their probabilities, what I ultimately get is the number 1. So if I take all the possible worlds, add up what each of their probabilities is, I should get the number 1 at the end, meaning all probabilities just need to sum to 1. So for example, if I take dice, for example, and if you imagine I have a fair die with numbers 1 through 6 and I roll the die, each one of these rolls has an equal probability of taking place. And the probability is 1 over 6, for example. So each of these probabilities is between 0 and 1, 0 meaning impossible and 1 meaning for certain. And if you add up all of these probabilities for all of the possible worlds, you get the number 1. And we can represent any one of those probabilities like this. The probability that we roll the number 2, for example, is just 1 over 6. Every six times we roll the die, we’d expect that one time, for instance, the die might come up as a 2. Its probability is not certain, but it’s a little more than nothing, for instance. And so this is all fairly straightforward for just a single die. But things get more interesting as our models of the world get a little bit more complex. Let’s imagine now that we’re not just dealing with a single die, but we have two dice, for example. I have a red die here and a blue die there, and I care not just about what the individual roll is, but I care about the sum of the two rolls. In this case, the sum of the two rolls is the number 3. How do I begin to now reason about what does the probability look like if instead of having one die, I now have two dice? Well, what we might imagine is that we could first consider what are all of the possible worlds. And in this case, all of the possible worlds are just every combination of the red and blue die that I could come up with. For the red die, it could be a 1 or a 2 or a 3 or a 4 or a 5 or a 6. And for each of those possibilities, the blue die, likewise, could also be either 1 or 2 or 3 or 4 or 5 or 6. And it just so happens that in this particular case, each of these possible combinations is equally likely. Equally likely are all of these various different possible worlds. That’s not always going to be the case. If you imagine more complex models that we could try to build and things that we could try to represent in the real world, it’s probably not going to be the case that every single possible world is always equally likely. But in the case of fair dice, where in any given die roll, any one number has just as good a chance of coming up as any other number, we can consider all of these possible worlds to be equally likely. But even though all of the possible worlds are equally likely, that doesn’t necessarily mean that their sums are equally likely. So if we consider what the sum is of all of these two, so 1 plus 1, that’s a 2. 2 plus 1 is a 3. And consider for each of these possible pairs of numbers what their sum ultimately is, we can notice that there are some patterns here, where it’s not entirely the case that every number comes up equally likely. If you consider 7, for example, what’s the probability that when I roll two dice, their sum is 7? There are several ways this can happen. There are six possible worlds where the sum is 7. It could be a 1 and a 6, or a 2 and a 5, or a 3 and a 4, a 4 and a 3, and so forth. But if you instead consider what’s the probability that I roll two dice, and the sum of those two die rolls is 12, for example, we’re looking at this diagram, there’s only one possible world in which that can happen. And that’s the possible world where both the red die and the blue die both come up as sixes to give us a sum total of 12. So based on just taking a look at this diagram, we see that some of these probabilities are likely different. The probability that the sum is a 7 must be greater than the probability that the sum is a 12. And we can represent that even more formally by saying, OK, the probability that we sum to 12 is 1 out of 36. Out of the 36 equally likely possible worlds, 6 squared because we have six options for the red die and six options for the blue die, out of those 36 options, only one of them sums to 12. Whereas on the other hand, the probability that if we take two dice rolls and they sum up to the number 7, well, out of those 36 possible worlds, there were six worlds where the sum was 7. And so we get 6 over 36, which we can simplify as a fraction to just 1 over 6. So here now, we’re able to represent these different ideas of probability, representing some events that might be more likely and then other events that are less likely as well. And these sorts of judgments, where we’re figuring out just in the abstract what is the probability that this thing takes place, are generally known as unconditional probabilities. Some degree of belief we have in some proposition, some fact about the world, in the absence of any other evidence. Without knowing any additional information, if I roll a die, what’s the chance it comes up as a 2? Or if I roll two dice, what’s the chance that the sum of those two die rolls is a 7? But usually when we’re thinking about probability, especially when we’re thinking about training in AI to intelligently be able to know something about the world and make predictions based on that information, it’s not unconditional probability that our AI is dealing with, but rather conditional probability, probability where rather than having no original knowledge, we have some initial knowledge about the world and how the world actually works. So conditional probability is the degree of belief in a proposition given some evidence that has already been revealed to us. So what does this look like? Well, it looks like this in terms of notation. We’re going to represent conditional probability as probability of A and then this vertical bar and then B. And the way to read this is the thing on the left-hand side of the vertical bar is what we want the probability of. Here now, I want the probability that A is true, that it is the real world, that it is the event that actually does take place. And then on the right side of the vertical bar is our evidence, the information that we already know for certain about the world. For example, that B is true. So the way to read this entire expression is what is the probability of A given B, the probability that A is true, given that we already know that B is true. And this type of judgment, conditional probability, the probability of one thing given some other fact, comes up quite a lot when we think about the types of calculations we might want our AI to be able to do. For example, we might care about the probability of rain today given that we know that it rained yesterday. We could think about the probability of rain today just in the abstract. What is the chance that today it rains? But usually, we have some additional evidence. I know for certain that it rained yesterday. And so I would like to calculate the probability that it rains today given that I know that it rained yesterday. Or you might imagine that I want to know the probability that my optimal route to my destination changes given the current traffic condition. So whether or not traffic conditions change, that might change the probability that this route is actually the optimal route. Or you might imagine in a medical context, I want to know the probability that a patient has a particular disease given some results of some tests that have been performed on that patient. And I have some evidence, the results of that test, and I would like to know the probability that a patient has a particular disease. So this notion of conditional probability comes up everywhere. So we begin to think about what we would like to reason about, but being able to reason a little more intelligently by taking into account evidence that we already have. We’re more able to get an accurate result for what is the likelihood that someone has this disease if we know this evidence, the results of the test, as opposed to if we were just calculating the unconditional probability of saying, what is the probability they have the disease without any evidence to try and back up our result one way or the other. So now that we’ve got this idea of what conditional probability is, the next question we have to ask is, all right, how do we calculate conditional probability? How do we figure out mathematically, if I have an expression like this, how do I get a number from that? What does conditional probability actually mean? Well, the formula for conditional probability looks a little something like this. The probability of a given b, the probability that a is true, given that we know that b is true, is equal to this fraction, the probability that a and b are true, divided by just the probability that b is true. And the way to intuitively try to think about this is that if I want to know the probability that a is true, given that b is true, well, I want to consider all the ways they could both be true out of the only worlds that I care about are the worlds where b is already true. I can sort of ignore all the cases where b isn’t true, because those aren’t relevant to my ultimate computation. They’re not relevant to what it is that I want to get information about. So let’s take a look at an example. Let’s go back to that example of rolling two dice and the idea that those two dice might sum up to the number 12. We discussed earlier that the unconditional probability that if I roll two dice and they sum to 12 is 1 out of 36, because out of the 36 possible worlds that I might care about, in only one of them is the sum of those two dice 12. It’s only when red is 6 and blue is also 6. But let’s say now that I have some additional information. I now want to know what is the probability that the two dice sum to 12, given that I know that the red die was a 6. So I already have some evidence. I already know the red die is a 6. I don’t know what the blue die is. That information isn’t given to me in this expression. But given the fact that I know that the red die rolled a 6, what is the probability that we sum to 12? And so we can begin to do the math using that expression from before. Here, again, are all of the possibilities, all of the possible combinations of red die being 1 through 6 and blue die being 1 through 6. And I might consider first, all right, what is the probability of my evidence, my B variable, where I want to know, what is the probability that the red die is a 6? Well, the probability that the red die is a 6 is just 1 out of 6. So these 1 out of 6 options are really the only worlds that I care about here now. All the rest of them are irrelevant to my calculation, because I already have this evidence that the red die was a 6, so I don’t need to care about all of the other possibilities that could result. So now, in addition to the fact that the red die rolled as a 6 and the probability of that, the other piece of information I need to know in order to calculate this conditional probability is the probability that both of my variables, A and B, are true. The probability that both the red die is a 6, and they all sum to 12. So what is the probability that both of these things happen? Well, it only happens in one possible case in 1 out of these 36 cases, and it’s the case where both the red and the blue die are equal to 6. This is a piece of information that we already knew. And so this probability is equal to 1 over 36. And so to get the conditional probability that the sum is 12, given that I know that the red dice is equal to 6, well, I just divide these two values together, and 1 over 36 divided by 1 over 6 gives us this probability of 1 over 6. Given that I know that the red die rolled a value of 6, the probability that the sum of the two dice is 12 is also 1 over 6. And that probably makes intuitive sense to you, too, because if the red die is a 6, the only way for me to get to a 12 is if the blue die also rolls a 6, and we know that the probability of the blue die rolling a 6 is 1 over 6. So in this case, the conditional probability seems fairly straightforward. But this idea of calculating a conditional probability by looking at the probability that both of these events take place is an idea that’s going to come up again and again. This is the definition now of conditional probability. And we’re going to use that definition as we think about probability more generally to be able to draw conclusions about the world. This, again, is that formula. The probability of A given B is equal to the probability that A and B take place divided by the probability of B. And you’ll see this formula sometimes written in a couple of different ways. You could imagine algebraically multiplying both sides of this equation by probability of B to get rid of the fraction, and you’ll get an expression like this. The probability of A and B, which is this expression over here, is just the probability of B times the probability of A given B. Or you could represent this equivalently since A and B in this expression are interchangeable. A and B is the same thing as B and A. You could imagine also representing the probability of A and B as the probability of A times the probability of B given A, just switching all of the A’s and B’s. These three are all equivalent ways of trying to represent what joint probability means. And so you’ll sometimes see all of these equations, and they might be useful to you as you begin to reason about probability and to think about what values might be taking place in the real world. Now, sometimes when we deal with probability, we don’t just care about a Boolean event like did this happen or did this not happen. Sometimes we might want the ability to represent variable values in a probability space where some variable might take on multiple different possible values. And in probability, we call a variable in probability theory a random variable. A random variable in probability is just some variable in probability theory that has some domain of values that it can take on. So what do I mean by this? Well, what I mean is I might have a random variable that is just called roll, for example, that has six possible values. Roll is my variable, and the possible values, the domain of values that it can take on are 1, 2, 3, 4, 5, and 6. And I might like to know the probability of each. In this case, they happen to all be the same. But in other random variables, that might not be the case. For example, I might have a random variable to represent the weather, for example, where the domain of values it could take on are things like sun or cloudy or rainy or windy or snowy. And each of those might have a different probability. And I care about knowing what is the probability that the weather equals sun or that the weather equals clouds, for instance. And I might like to do some mathematical calculations based on that information. Other random variables might be something like traffic. What are the odds that there is no traffic or light traffic or heavy traffic? Traffic, in this case, is my random variable. And the values that that random variable can take on are here. It’s either none or light or heavy. And I, the person doing these calculations, I, the person encoding these random variables into my computer, need to make the decision as to what these possible values actually are. You might imagine, for example, for a flight. If I care about whether or not I make it or do a flight on time, my flight has a couple of possible values that it could take on. My flight could be on time. My flight could be delayed. My flight could be canceled. So flight, in this case, is my random variable. And these are the values that it can take on. And often, I want to know something about the probability that my random variable takes on each of those possible values. And this is what we then call a probability distribution. A probability distribution takes a random variable and gives me the probability for each of the possible values in its domain. So in the case of this flight, for example, my probability distribution might look something like this. My probability distribution says the probability that the random variable flight is equal to the value on time is 0.6. Or otherwise, put into more English human-friendly terms, the likelihood that my flight is on time is 60%, for example. And in this case, the probability that my flight is delayed is 30%. The probability that my flight is canceled is 10% or 0.1. And if you sum up all of these possible values, the sum is going to be 1, right? If you take all of the possible worlds, here are my three possible worlds for the value of the random variable flight, add them all up together, the result needs to be the number 1 per that axiom of probability theory that we’ve discussed before. So this now is one way of representing this probability distribution for the random variable flight. Sometimes you’ll see it represented a little bit more concisely that this is pretty verbose for really just trying to express three possible values. And so often, you’ll instead see the same notation representing using a vector. And all a vector is is a sequence of values. As opposed to just a single value, I might have multiple values. And so I could extend instead, represent this idea this way. Bold p, so a larger p, generally meaning the probability distribution of this variable flight is equal to this vector represented in angle brackets. The probability distribution is 0.6, 0.3, and 0.1. And I would just have to know that this probability distribution is in order of on time or delayed and canceled to know how to interpret this vector. To mean the first value in the vector is the probability that my flight is on time. The second value in the vector is the probability that my flight is delayed. And the third value in the vector is the probability that my flight is canceled. And so this is just an alternate way of representing this idea, a little more verbosely. But oftentimes, you’ll see us just talk about a probability distribution over a random variable. And whenever we talk about that, what we’re really doing is trying to figure out the probabilities of each of the possible values that that random variable can take on. But this notation is just a little bit more succinct, even though it can sometimes be a little confusing, depending on the context in which you see it. So we’ll start to look at examples where we use this sort of notation to describe probability and to describe events that might take place. A couple of other important ideas to know with regards to probability theory. One is this idea of independence. And independence refers to the idea that the knowledge of one event doesn’t influence the probability of another event. So for example, in the context of my two dice rolls, where I had the red die and the blue die, the probability that I roll the red die and the blue die, those two events, red die and blue die, are independent. Knowing the result of the red die doesn’t change the probabilities for the blue die. It doesn’t give me any additional information about what the value of the blue die is ultimately going to be. But that’s not always going to be the case. You might imagine that in the case of weather, something like clouds and rain, those are probably not independent. But if it is cloudy, that might increase the probability that later in the day it’s going to rain. So some information informs some other event or some other random variable. So independence refers to the idea that one event doesn’t influence the other. And if they’re not independent, then there might be some relationship. So mathematically, formally, what does independence actually mean? Well, recall this formula from before, that the probability of A and B is the probability of A times the probability of B given A. And the more intuitive way to think about this is that to know how likely it is that A and B happen, well, let’s first figure out the likelihood that A happens. And then given that we know that A happens, let’s figure out the likelihood that B happens and multiply those two things together. But if A and B were independent, meaning knowing A doesn’t change anything about the likelihood that B is true, well, then the probability of B given A, meaning the probability that B is true, given that I know A is true, well, that I know A is true shouldn’t really make a difference if these two things are independent, that A shouldn’t influence B at all. So the probability of B given A is really just the probability of B. If it is true that A and B are independent. And so this right here is one example of a definition for what it means for A and B to be independent. The probability of A and B is just the probability of A times the probability of B. Anytime you find two events A and B where this relationship holds, then you can say that A and B are independent. So an example of that might be the dice that we were taking a look at before. Here, if I wanted the probability of red being a 6 and blue being a 6, well, that’s just the probability that red is a 6 multiplied by the probability that blue is a 6. It’s both equal to 1 over 36. So I can say that these two events are independent. What wouldn’t be independent, for example, would be an example. So this, for example, has a probability of 1 over 36, as we talked about before. But what wouldn’t be independent would be a case like this, the probability that the red die rolls a 6 and the red die rolls a 4. If you just naively took, OK, red die 6, red die 4, well, if I’m only rolling the die once, you might imagine the naive approach is to say, well, each of these has a probability of 1 over 6. So multiply them together, and the probability is 1 over 36. But of course, if you’re only rolling the red die once, there’s no way you could get two different values for the red die. It couldn’t both be a 6 and a 4. So the probability should be 0. But if you were to multiply probability of red 6 times probability of red 4, well, that would equal 1 over 36. But of course, that’s not true. Because we know that there is no way, probability 0, that when we roll the red die once, we get both a 6 and a 4, because only one of those possibilities can actually be the result. And so we can say that the event that red roll is 6 and the event that red roll is 4, those two events are not independent. If I know that the red roll is a 6, I know that the red roll cannot possibly be a 4, so these things are not independent. And instead, if I wanted to calculate the probability, I would need to use this conditional probability as the regular definition of the probability of two events taking place. And the probability of this now, well, the probability of the red roll being a 6, that’s 1 over 6. But what’s the probability that the roll is a 4 given that the roll is a 6? Well, this is just 0, because there’s no way for the red roll to be a 4, given that we already know the red roll is a 6. And so the value, if we do add all that multiplication, is we get the number 0. So this idea of conditional probability is going to come up again and again, especially as we begin to reason about multiple different random variables that might be interacting with each other in some way. And this gets us to one of the most important rules in probability theory, which is known as Bayes rule. And it turns out that just using the information we’ve already learned about probability and just applying a little bit of algebra, we can actually derive Bayes rule for ourselves. But it’s a very important rule when it comes to inference and thinking about probability in the context of what it is that a computer can do or what a mathematician could do by having access to information about probability. So let’s go back to these equations to be able to derive Bayes rule ourselves. We know the probability of A and B, the likelihood that A and B take place, is the likelihood of B, and then the likelihood of A, given that we know that B is already true. And likewise, the probability of A given A and B is the probability of A times the probability of B, given that we know that A is already true. This is sort of a symmetric relationship where it doesn’t matter the order of A and B and B and A mean the same thing. And so in these equations, we can just swap out A and B to be able to represent the exact same idea. So we know that these two equations are already true. We’ve seen that already. And now let’s just do a little bit of algebraic manipulation of this stuff. Both of these expressions on the right-hand side are equal to the probability of A and B. So what I can do is take these two expressions on the right-hand side and just set them equal to each other. If they’re both equal to the probability of A and B, then they both must be equal to each other. So probability of A times probability of B given A is equal to the probability of B times the probability of A given B. And now all we’re going to do is do a little bit of division. I’m going to divide both sides by P of A. And now I get what is Bayes’ rule. The probability of B given A is equal to the probability of B times the probability of A given B divided by the probability of A. And sometimes in Bayes’ rule, you’ll see the order of these two arguments switched. So instead of B times A given B, it’ll be A given B times B. That ultimately doesn’t matter because in multiplication, you can switch the order of the two things you’re multiplying, and it doesn’t change the result. But this here right now is the most common formulation of Bayes’ rule. The probability of B given A is equal to the probability of A given B times the probability of B divided by the probability of A. And this rule, it turns out, is really important when it comes to trying to infer things about the world, because it means you can express one conditional probability, the conditional probability of B given A, using knowledge about the probability of A given B, using the reverse of that conditional probability. So let’s first do a little bit of an example with this, just to see how we might use it, and then explore what this means a little bit more generally. So we’re going to construct a situation where I have some information. There are two events that I care about, the idea that it’s cloudy in the morning and the idea that it is rainy in the afternoon. Those are two different possible events that could take place, cloudy in the morning, or the AM, rainy in the PM. And what I care about is, given clouds in the morning, what is the probability of rain in the afternoon? A reasonable question I might ask, in the morning, I look outside, or an AI’s camera looks outside and sees that there are clouds in the morning. And we want to conclude, we want to figure out what is the probability that in the afternoon, there is going to be rain. Of course, in the abstract, we don’t have access to this kind of information, but we can use data to begin to try and figure this out. So let’s imagine now that I have access to some pieces of information. I have access to the idea that 80% of rainy afternoons start out with a cloudy morning. And you might imagine that I could have gathered this data just by looking at data over a sequence of time, that I know that 80% of the time when it’s raining in the afternoon, it was cloudy that morning. I also know that 40% of days have cloudy mornings. And I also know that 10% of days have rainy afternoons. And now using this information, I would like to figure out, given clouds in the morning, what is the probability that it rains in the afternoon? I want to know the probability of afternoon rain given morning clouds. And I can do that, in particular, using this fact, the probability of, so if I know that 80% of rainy afternoons start with cloudy mornings, then I know the probability of cloudy mornings given rainy afternoons. So using sort of the reverse conditional probability, I can figure that out. Expressed in terms of Bayes rule, this is what that would look like. Probability of rain given clouds is the probability of clouds given rain times the probability of rain divided by the probability of clouds. Here I’m just substituting in for the values of a and b from that equation of Bayes rule from before. And then I can just do the math. I have this information. I know that 80% of the time, if it was raining, then there were clouds in the morning. So 0.8 here. Probability of rain is 0.1, because 10% of days were rainy, and 40% of days were cloudy. I do the math, and I can figure out the answer is 0.2. So the probability that it rains in the afternoon, given that it was cloudy in the morning, is 0.2 in this case. And this now is an application of Bayes rule, the idea that using one conditional probability, we can get the reverse conditional probability. And this is often useful when one of the conditional probabilities might be easier for us to know about or easier for us to have data about. And using that information, we can calculate the other conditional probability. So what does this look like? Well, it means that knowing the probability of cloudy mornings given rainy afternoons, we can calculate the probability of rainy afternoons given cloudy mornings. Or, for example, more generally, if we know the probability of some visible effect, some effect that we can see and observe, given some unknown cause that we’re not sure about, well, then we can calculate the probability of that unknown cause given the visible effect. So what might that look like? Well, in the context of medicine, for example, I might know the probability of some medical test result given a disease. Like, I know that if someone has a disease, then x% of the time the medical test result will show up as this, for instance. And using that information, then I can calculate, all right, what is the probability that given I know the medical test result, what is the likelihood that someone has the disease? This is the piece of information that is usually easier to know, easier to immediately have access to data for. And this is the information that I actually want to calculate. Or I might want to know, for example, if I know that some probability of counterfeit bills have blurry text around the edges, because counterfeit printers aren’t nearly as good at printing text precisely. So I have some information about, given that something is a counterfeit bill, like x% of counterfeit bills have blurry text, for example. And using that information, then I can calculate some piece of information that I might want to know, like, given that I know there’s blurry text on a bill, what is the probability that that bill is counterfeit? So given one conditional probability, I can calculate the other conditional probability as well. And so now we’ve taken a look at a couple of different types of probability. And we’ve looked at unconditional probability, where I just look at what is the probability of this event occurring, given no additional evidence that I might have access to. And we’ve also looked at conditional probability, where I have some sort of evidence, and I would like to, using that evidence, be able to calculate some other probability as well. And the other kind of probability that will be important for us to think about is joint probability. And this is when we’re considering the likelihood of multiple different events simultaneously. And so what do we mean by this? For example, I might have probability distributions that look a little something like this. Like, oh, I want to know the probability distribution of clouds in the morning. And that distribution looks like this. 40% of the time, C, which is my random variable here, is equal to it’s cloudy. And 60% of the time, it’s not cloudy. So here is just a simple probability distribution that is effectively telling me that 40% of the time, it’s cloudy. I might also have a probability distribution for rain in the afternoon, where 10% of the time, or with probability 0.1, it is raining in the afternoon. And with probability 0.9, it is not raining in the afternoon. And using just these two pieces of information, I don’t actually have a whole lot of information about how these two variables relate to each other. But I could if I had access to their joint probability, meaning for every combination of these two things, meaning morning cloudy and afternoon rain, morning cloudy and afternoon not rain, morning not cloudy and afternoon rain, and morning not cloudy and afternoon not raining, if I had access to values for each of those four, I’d have more information. So information that’d be organized in a table like this, and this, rather than just a probability distribution, is a joint probability distribution. It tells me the probability distribution of each of the possible combinations of values that these random variables can take on. So if I want to know what is the probability that on any given day it is both cloudy and rainy, well, I would say, all right, we’re looking at cases where it is cloudy and cases where it is raining. And the intersection of those two, that row in that column, is 0.08. So that is the probability that it is both cloudy and rainy using that information. And using this conditional probability table, using this joint probability table, I can begin to draw other pieces of information about things like conditional probability. So I might ask a question like, what is the probability distribution of clouds given that I know that it is raining? Meaning I know for sure that it’s raining. Tell me the probability distribution over whether it’s cloudy or not, given that I know already that it is, in fact, raining. And here I’m using C to stand for that random variable. I’m looking for a distribution, meaning the answer to this is not going to be a single value. It’s going to be two values, a vector of two values, where the first value is probability of clouds, the second value is probability that it is not cloudy, but the sum of those two values is going to be 1. Because when you add up the probabilities of all of the possible worlds, the result that you get must be the number 1. And well, what do we know about how to calculate a conditional probability? Well, we know that the probability of A given B is the probability of A and B divided by the probability of B. So what does this mean? Well, it means that I can calculate the probability of clouds given that it’s raining as the probability of clouds and raining divided by the probability of rain. And this comma here for the probability distribution of clouds and rain, this comma sort of stands in for the word and. You’ll sort of see in the logical operator and and the comma used interchangeably. This means the probability distribution over the clouds and knowing the fact that it is raining divided by the probability of rain. And the interesting thing to note here and what we’ll often do in order to simplify our mathematics is that dividing by the probability of rain, the probability of rain here is just some numerical constant. It is some number. Dividing by probability of rain is just dividing by some constant, or in other words, multiplying by the inverse of that constant. And it turns out that oftentimes we can just not worry about what the exact value of this is and just know that it is, in fact, a constant value. And we’ll see why in a moment. So instead of expressing this as this joint probability divided by the probability of rain, sometimes we’ll just represent it as alpha times the numerator here, the probability distribution of C, this variable, and that we know that it is raining, for instance. So all we’ve done here is said this value of 1 over the probability of rain, that’s really just a constant we’re going to divide by or equivalently multiply by the inverse of at the end. We’ll just call it alpha for now and deal with it a little bit later. But the key idea here now, and this is an idea that’s going to come up again, is that the conditional distribution of C given rain is proportional to, meaning just some factor multiplied by the joint probability of C and rain being true. And so how do we figure this out? Well, this is going to be the probability that it is cloudy given that it’s raining, which is 0.08, and the probability that it’s not cloudy given that it’s raining, which is 0.02. And so we get alpha times here now is that probability distribution. 0.08 is clouds and rain. 0.02 is not cloudy and rain. But of course, 0.08 and 0.02 don’t sum up to the number 1. And we know that in a probability distribution, if you consider all of the possible values, they must sum up to a probability of 1. And so we know that we just need to figure out some constant to normalize, so to speak, these values, something we can multiply or divide by to get it so that all these probabilities sum up to 1, and it turns out that if we multiply both numbers by 10, then we can get that result of 0.8 and 0.2. The proportions are still equivalent, but now 0.8 plus 0.2, those sum up to the number 1. So take a look at this and see if you can understand step by step how it is we’re getting from one point to another. The key idea here is that by using the joint probabilities, these probabilities that it is both cloudy and rainy and that it is not cloudy and rainy, I can take that information and figure out the conditional probability given that it’s raining. What is the chance that it’s cloudy versus not cloudy? Just by multiplying by some normalization constant, so to speak. And this is what a computer can begin to use to be able to interact with these various different types of probabilities. And it turns out there are a number of other probability rules that are going to be useful to us as we begin to explore how we can actually use this information to encode into our computers some more complex analysis that we might want to do about probability and distributions and random variables that we might be interacting with. So here are a couple of those important probability rules. One of the simplest rules is just this negation rule. What is the probability of not event A? So A is an event that has some probability, and I would like to know what is the probability that A does not occur. And it turns out it’s just 1 minus P of A, which makes sense. Because if those are the two possible cases, either A happens or A doesn’t happen, then when you add up those two cases, you must get 1, which means that P of not A must just be 1 minus P of A. Because P of A and P of not A must sum up to the number 1. They must include all of the possible cases. We’ve seen an expression for calculating the probability of A and B. We might also reasonably want to calculate the probability of A or B. What is the probability that one thing happens or another thing happens? So for example, I might want to calculate what is the probability that if I roll two dice, a red die and a blue die, what is the likelihood that A is a 6 or B is a 6, like one or the other? And what you might imagine you could do, and the wrong way to approach it, would be just to say, all right, well, A comes up as a 6 with the red die comes up as a 6 with probability 1 over 6. The same for the blue die, it’s also 1 over 6. Add them together, and you get 2 over 6, otherwise known as 1 third. But this suffers from a problem of over counting, that we’ve double counted the case, where both A and B, both the red die and the blue die, both come up as a 6-roll. And I’ve counted that instance twice. So to resolve this, the actual expression for calculating the probability of A or B uses what we call the inclusion-exclusion formula. So I take the probability of A, add it to the probability of B. That’s all same as before. But then I need to exclude the cases that I’ve double counted. So I subtract from that the probability of A and B. And that gets me the result for A or B. I consider all the cases where A is true and all the cases where B is true. And if you imagine this is like a Venn diagram of cases where A is true, cases where B is true, I just need to subtract out the middle to get rid of the cases that I have overcounted by double counting them inside of both of these individual expressions. One other rule that’s going to be quite helpful is a rule called marginalization. So marginalization is answering the question of how do I figure out the probability of A using some other variable that I might have access to, like B? Even if I don’t know additional information about it, I know that B, some event, can have two possible states, either B happens or B doesn’t happen, assuming it’s a Boolean, true or false. And well, what that means is that for me to be able to calculate the probability of A, there are only two cases. Either A happens and B happens, or A happens and B doesn’t happen. And those are two disjoint, meaning they can’t both happen together. Either B happens or B doesn’t happen. They’re disjoint or separate cases. And so I can figure out the probability of A just by adding up those two cases. The probability that A is true is the probability that A and B is true, plus the probability that A is true and B isn’t true. So by marginalizing, I’ve looked at the two possible cases that might take place, either B happens or B doesn’t happen. And in either of those cases, I look at what’s the probability that A happens. And if I add those together, well, then I get the probability that A happens as a whole. So take a look at that rule. It doesn’t matter what B is or how it’s related to A. So long as I know these joint distributions, I can figure out the overall probability of A. And this can be a useful way if I have a joint distribution, like the joint distribution of A and B, to just figure out some unconditional probability, like the probability of A. And we’ll see examples of this soon as well. Now, sometimes these might not just be random, might not just be variables that are events that are like they happened or they didn’t happen, like B is here. They might be some broader probability distribution where there are multiple possible values. And so here, in order to use this marginalization rule, I need to sum up not just over B and not B, but for all of the possible values that the other random variable could take on. And so here, we’ll see a version of this rule for random variables. And it’s going to include that summation notation to indicate that I’m summing up, adding up a whole bunch of individual values. So here’s the rule. Looks a lot more complicated, but it’s actually the equivalent exactly the same rule. What I’m saying here is that if I have two random variables, one called x and one called y, well, the probability that x is equal to some value x sub i, this is just some value that this variable takes on. How do I figure it out? Well, I’m going to sum up over j, where j is going to range over all of the possible values that y can take on. Well, let’s look at the probability that x equals xi and y equals yj. So the exact same rule, the only difference here is now I’m summing up over all of the possible values that y can take on, saying let’s add up all of those possible cases and look at this joint distribution, this joint probability, that x takes on the value I care about, given all of the possible values for y. And if I add all those up, then I can get this unconditional probability of what x is equal to, whether or not x is equal to some value x sub i. So let’s take a look at this rule, because it does look a little bit complicated. Let’s try and put a concrete example to it. Here again is that same joint distribution from before. I have cloud, not cloudy, rainy, not rainy. And maybe I want to access some variable. I want to know what is the probability that it is cloudy. Well, marginalization says that if I have this joint distribution and I want to know what is the probability that it is cloudy, well, I need to consider the other variable, the variable that’s not here, the idea that it’s rainy. And I consider the two cases, either it’s raining or it’s not raining. And I just sum up the values for each of those possibilities. In other words, the probability that it is cloudy is equal to the sum of the probability that it’s cloudy and it’s rainy and the probability that it’s cloudy and it is not raining. And so these now are values that I have access to. These are values that are just inside of this joint probability table. What is the probability that it is both cloudy and rainy? Well, it’s just the intersection of these two here, which is 0.08. And the probability that it’s cloudy and not raining is, all right, here’s cloudy, here’s not raining. It’s 0.32. So it’s 0.08 plus 0.32, which just gives us equal to 0.4. That is the unconditional probability that it is, in fact, cloudy. And so marginalization gives us a way to go from these joint distributions to just some individual probability that I might care about. And you’ll see a little bit later why it is that we care about that and why that’s actually useful to us as we begin doing some of these calculations. Last rule we’ll take a look at before transitioning to something a little bit different is this rule of conditioning, very similar to the marginalization rule. But it says that, again, if I have two events, a and b, but instead of having access to their joint probabilities, I have access to their conditional probabilities, how they relate to each other. Well, again, if I want to know the probability that a happens, and I know that there’s some other variable b, either b happens or b doesn’t happen, and so I can say that the probability of a is the probability of a given b times the probability of b, meaning b happened. And given that I know b happened, what’s the likelihood that a happened? And then I consider the other case, that b didn’t happen. So here’s the probability that b didn’t happen. And here’s the probability that a happens, given that I know that b didn’t happen. And this is really the equivalent rule just using conditional probability instead of joint probability, where I’m saying let’s look at both of these two cases and condition on b. Look at the case where b happens, and look at the case where b doesn’t happen, and look at what probabilities I get as a result. And just as in the case of marginalization, where there was an equivalent rule for random variables that could take on multiple possible values in a domain of possible values, here, too, conditioning has the same equivalent rule. Again, there’s a summation to mean I’m summing over all of the possible values that some random variable y could take on. But if I want to know what is the probability that x takes on this value, then I’m going to sum up over all the values j that y could take on, and say, all right, what’s the chance that y takes on that value yj? And multiply it by the conditional probability that x takes on this value, given that y took on that value yj. So equivalent rule just using conditional probabilities instead of joint probabilities. And using the equation we know about joint probabilities, we can translate between these two. So all right, we’ve seen a whole lot of mathematics, and we’ve just laid the foundation for mathematics. And no need to worry if you haven’t seen probability in too much detail up until this point. These are the foundations of the ideas that are going to come up as we begin to explore how we can now take these ideas from probability and begin to apply them to represent something inside of our computer, something inside of the AI agent we’re trying to design that is able to represent information and probabilities and the likelihoods between various different events. So there are a number of different probabilistic models that we can generate, but the first of the models we’re going to talk about are what are known as Bayesian networks. And a Bayesian network is just going to be some network of random variables, connected random variables that are going to represent the dependence between these random variables. The odds are most random variables in this world are not independent from each other, but there’s some relationship between things that are happening that we care about. If it is rainy today, that might increase the likelihood that my flight or my train gets delayed, for example. There are some dependence between these random variables, and a Bayesian network is going to be able to capture those dependencies. So what is a Bayesian network? What is its actual structure, and how does it work? Well, a Bayesian network is going to be a directed graph. And again, we’ve seen directed graphs before. They are individual nodes with arrows or edges that connect one node to another node pointing in a particular direction. And so this directed graph is going to have nodes as well, where each node in this directed graph is going to represent a random variable, something like the weather, or something like whether my train was on time or delayed. And we’re going to have an arrow from a node x to a node y to mean that x is a parent of y. So that’ll be our notation. If there’s an arrow from x to y, x is going to be considered a parent of y. And the reason that’s important is because each of these nodes is going to have a probability distribution that we’re going to store along with it, which is the distribution of x given some evidence, given the parents of x. So the way to more intuitively think about this is the parents seem to be thought of as sort of causes for some effect that we’re going to observe. And so let’s take a look at an actual example of a Bayesian network and think about the types of logic that might be involved in reasoning about that network. Let’s imagine for a moment that I have an appointment out of town, and I need to take a train in order to get to that appointment. So what are the things I might care about? Well, I care about getting to my appointment on time. Whether I make it to my appointment and I’m able to attend it or I miss the appointment. And you might imagine that that’s influenced by the train, that the train is either on time or it’s delayed, for example. But that train itself is also influenced. Whether the train is on time or not depends maybe on the rain. Is there no rain? Is it light rain? Is there heavy rain? And it might also be influenced by other variables too. It might be influenced as well by whether or not there’s maintenance on the train track, for example. If there is maintenance on the train track, that probably increases the likelihood that my train is delayed. And so we can represent all of these ideas using a Bayesian network that looks a little something like this. Here I have four nodes representing four random variables that I would like to keep track of. I have one random variable called rain that can take on three possible values in its domain, either none or light or heavy, for no rain, light rain, or heavy rain. I have a variable called maintenance for whether or not there is maintenance on the train track, which it has two possible values, just either yes or no. Either there is maintenance or there’s no maintenance happening on the track. Then I have a random variable for the train indicating whether or not the train was on time or not. That random variable has two possible values in its domain. The train is either on time or the train is delayed. And then finally, I have a random variable for whether I make it to my appointment. For my appointment down here, I have a random variable called appointment that itself has two possible values, attend and miss. And so here are the possible values. Here are my four nodes, each of which represents a random variable, each of which has a domain of possible values that it can take on. And the arrows, the edges pointing from one node to another, encode some notion of dependence inside of this graph, that whether I make it to my appointment or not is dependent upon whether the train is on time or delayed. And whether the train is on time or delayed is dependent on two things given by the two arrows pointing at this node. It is dependent on whether or not there was maintenance on the train track. And it is also dependent upon whether or not it was raining or whether it is raining. And just to make things a little complicated, let’s say as well that whether or not there is maintenance on the track, this too might be influenced by the rain. That if there’s heavier rain, well, maybe it’s less likely that it’s going to be maintenance on the train track that day because they’re more likely to want to do maintenance on the track on days when it’s not raining, for example. And so these nodes might have different relationships between them. But the idea is that we can come up with a probability distribution for any of these nodes based only upon its parents. And so let’s look node by node at what this probability distribution might actually look like. And we’ll go ahead and begin with this root node, this rain node here, which is at the top, and has no arrows pointing into it, which means its probability distribution is not going to be a conditional distribution. It’s not based on anything. I just have some probability distribution over the possible values for the rain random variable. And that distribution might look a little something like this. None, light and heavy, each have a possible value. Here I’m saying the likelihood of no rain is 0.7, of light rain is 0.2, of heavy rain is 0.1, for example. So here is a probability distribution for this root node in this Bayesian network. And let’s now consider the next node in the network, maintenance. Track maintenance is yes or no. And the general idea of what this distribution is going to encode, at least in this story, is the idea that the heavier the rain is, the less likely it is that there’s going to be maintenance on the track. Because the people that are doing maintenance on the track probably want to wait until a day when it’s not as rainy in order to do the track maintenance, for example. And so what might that probability distribution look like? Well, this now is going to be a conditional probability distribution, that here are the three possible values for the rain random variable, which I’m here just going to abbreviate to R, either no rain, light rain, or heavy rain. And for each of those possible values, either there is yes track maintenance or no track maintenance. And those have probabilities associated with them. That I see here that if it is not raining, then there is a probability of 0.4 that there’s track maintenance and a probability of 0.6 that there isn’t. But if there’s heavy rain, then here the chance that there is track maintenance is 0.1 and the chance that there is not track maintenance is 0.9. Each of these rows is going to sum up to 1. Because each of these represent different values of whether or not it’s raining, the three possible values that that random variable can take on. And each is associated with its own probability distribution that is ultimately all going to add up to the number 1. So that there is our distribution for this random variable called maintenance, about whether or not there is maintenance on the train track. And now let’s consider the next variable. Here we have a node inside of our Bayesian network called train that has two possible values, on time and delayed. And this node is going to be dependent upon the two nodes that are pointing towards it, that whether or not the train is on time or delayed depends on whether or not there is track maintenance. And it depends on whether or not there is rain, that heavier rain probably means more likely that my train is delayed. And if there is track maintenance, that also probably means it’s more likely that my train is delayed as well. And so you could construct a larger probability distribution, a conditional probability distribution, that instead of conditioning on just one variable, as was the case here, is now conditioning on two variables, conditioning both on rain represented by r and on maintenance represented by yes. Again, each of these rows has two values that sum up to the number 1, one for whether the train is on time, one for whether the train is delayed. And here I can say something like, all right, if I know there was light rain and track maintenance, well, OK, that would be r is light and m is yes. Well, then there is a probability of 0.6 that my train is on time, and a probability of 0.4 the train is delayed. And you can imagine gathering this data just by looking at real world data, looking at data about, all right, if I knew that it was light rain and there was track maintenance, how often was a train delayed or not delayed? And you could begin to construct this thing. The interesting thing is intelligently, being able to try to figure out how might you go about ordering these things, what things might influence other nodes inside of this Bayesian network. And the last thing I care about is whether or not I make it to my appointment. So did I attend or miss the appointment? And ultimately, whether I attend or miss the appointment, it is influenced by track maintenance, because it’s indirectly this idea that, all right, if there is track maintenance, well, then my train might more likely be delayed. And if my train is more likely to be delayed, then I’m more likely to miss my appointment. But what we encode in this Bayesian network are just what we might consider to be more direct relationships. So the train has a direct influence on the appointment. And given that I know whether the train is on time or delayed, knowing whether there’s track maintenance isn’t going to give me any additional information that I didn’t already have. That if I know train, these other nodes that are up above isn’t really going to influence the result. And so here we might represent it using another conditional probability distribution that looks a little something like this. The train can take on two possible values. Either my train is on time or my train is delayed. And for each of those two possible values, I have a distribution for what are the odds that I’m able to attend the meeting and what are the odds that I missed the meeting. And obviously, if my train is on time, I’m much more likely to be able to attend the meeting than if my train is delayed, in which case I’m more likely to miss that meeting. So all of these nodes put all together here represent this Bayesian network, this network of random variables whose values I ultimately care about, and that have some sort of relationship between them, some sort of dependence where these arrows from one node to another indicate some dependence, that I can calculate the probability of some node given the parents that happen to exist there. So now that we’ve been able to describe the structure of this Bayesian network and the relationships between each of these nodes by associating each of the nodes in the network with a probability distribution, whether that’s an unconditional probability distribution in the case of this root node here, like rain, and a conditional probability distribution in the case of all of the other nodes whose probabilities are dependent upon the values of their parents, we can begin to do some computation and calculation using the information inside of that table. So let’s imagine, for example, that I just wanted to compute something simple like the probability of light rain. How would I get the probability of light rain? Well, light rain, rain here is a root node. And so if I wanted to calculate that probability, I could just look at the probability distribution for rain and extract from it the probability of light rains, just a single value that I already have access to. But we could also imagine wanting to compute more complex joint probabilities, like the probability that there is light rain and also no track maintenance. This is a joint probability of two values, light rain and no track maintenance. And the way I might do that is first by starting by saying, all right, well, let me get the probability of light rain. But now I also want the probability of no track maintenance. But of course, this node is dependent upon the value of rain. So what I really want is the probability of no track maintenance, given that I know that there was light rain. And so the expression for calculating this idea that the probability of light rain and no track maintenance is really just the probability of light rain and the probability that there is no track maintenance, given that I know that there already is light rain. So I take the unconditional probability of light rain, multiply it by the conditional probability of no track maintenance, given that I know there is light rain. And you can continue to do this again and again for every variable that you want to add into this joint probability that I might want to calculate. If I wanted to know the probability of light rain and no track maintenance and a delayed train, well, that’s going to be the probability of light rain, multiplied by the probability of no track maintenance, given light rain, multiplied by the probability of a delayed train, given light rain and no track maintenance. Because whether the train is on time or delayed is dependent upon both of these other two variables. And so I have two pieces of evidence that go into the calculation of that conditional probability. And each of these three values is just a value that I can look up by looking at one of these individual probability distributions that is encoded into my Bayesian network. And if I wanted a joint probability over all four of the variables, something like the probability of light rain and no track maintenance and a delayed train and I miss my appointment, well, that’s going to be multiplying four different values, one from each of these individual nodes. It’s going to be the probability of light rain, then of no track maintenance given light rain, then of a delayed train, given light rain and no track maintenance. And then finally, for this node here, for whether I make it to my appointment or not, it’s not dependent upon these two variables, given that I know whether or not the train is on time. I only need to care about the conditional probability that I miss my train, or that I miss my appointment, given that the train happens to be delayed. And so that’s represented here by four probabilities, each of which is located inside of one of these probability distributions for each of the nodes, all multiplied together. And so I can take a variable like that and figure out what the joint probability is by multiplying a whole bunch of these individual probabilities from the Bayesian network. But of course, just as with last time, where what I really wanted to do was to be able to get new pieces of information, here, too, this is what we’re going to want to do with our Bayesian network. In the context of knowledge, we talked about the problem of inference. Given things that I know to be true, can I draw conclusions, make deductions about other facts about the world that I also know to be true? And what we’re going to do now is apply the same sort of idea to probability. Using information about which I have some knowledge, whether some evidence or some probabilities, can I figure out not other variables for certain, but can I figure out the probabilities of other variables taking on particular values? And so here, we introduce the problem of inference in a probabilistic setting, in a case where variables might not necessarily be true for sure, but they might be random variables that take on different values with some probability. So how do we formally define what exactly this inference problem actually is? Well, the inference problem has a couple of parts to it. We have some query, some variable x that we want to compute the distribution for. Maybe I want the probability that I miss my train, or I want the probability that there is track maintenance, something that I want information about. And then I have some evidence variables. Maybe it’s just one piece of evidence. Maybe it’s multiple pieces of evidence. But I’ve observed certain variables for some sort of event. So for example, I might have observed that it is raining. This is evidence that I have. I know that there is light rain, or I know that there is heavy rain. And that is evidence I have. And using that evidence, I want to know what is the probability that my train is delayed, for example. And that is a query that I might want to ask based on this evidence. So I have a query, some variable. Evidence, which are some other variables that I have observed inside of my Bayesian network. And of course, that does leave some hidden variables. Why? These are variables that are not evidence variables and not query variables. So you might imagine in the case where I know whether or not it’s raining, and I want to know whether my train is going to be delayed or not, the hidden variable, the thing I don’t have access to, is something like, is there maintenance on the track? Or am I going to make or not make my appointment, for example? These are variables that I don’t have access to. They’re hidden because they’re not things I observed, and they’re also not the query, the thing that I’m asking. And so ultimately, what we want to calculate is I want to know the probability distribution of x given e, the event that I observed. So given that I observed some event, I observed that it is raining, I would like to know what is the distribution over the possible values of the train random variable. Is it on time? Is it delayed? What’s the likelihood it’s going to be there? And it turns out we can do this calculation just using a lot of the probability rules that we’ve already seen in action. And ultimately, we’re going to take a look at the math at a little bit of a high level, at an abstract level. But ultimately, we can allow computers and programming libraries that already exist to begin to do some of this math for us. But it’s good to get a general sense for what’s actually happening when this inference process takes place. Let’s imagine, for example, that I want to compute the probability distribution of the appointment random variable given some evidence, given that I know that there was light rain and no track maintenance. So there’s my evidence, these two variables that I observe the values of. I observe the value of rain. I know there’s light rain. And I know that there is no track maintenance going on today. And what I care about knowing, my query, is this random variable appointment. I want to know the distribution of this random variable appointment, like what is the chance that I’m able to attend my appointment? What is the chance that I miss my appointment given this evidence? And the hidden variable, the information that I don’t have access to, is this variable train. This is information that is not part of the evidence that I see, not something that I observe. But it is also not the query that I’m asking for. And so what might this inference procedure look like? Well, if you recall back from when we were defining conditional probability and doing math with conditional probabilities, we know that a conditional probability is proportional to the joint probability. And we remembered this by recalling that the probability of A given B is just some constant factor alpha multiplied by the probability of A and B. That constant factor alpha turns out to be like dividing over the probability of B. But the important thing is that it’s just some constant multiplied by the joint distribution, the probability that all of these individual things happen. So in this case, I can take the probability of the appointment random variable given light rain and no track maintenance and say that is just going to be proportional, some constant alpha, multiplied by the joint probability, the probability of a particular value for the appointment random variable and light rain and no track maintenance. Well, all right, how do I calculate this, probability of appointment and light rain and no track maintenance, when what I really care about is knowing I need all four of these values to be able to calculate a joint distribution across everything because in a particular appointment depends upon the value of train? Well, in order to do that, here I can begin to use that marginalization trick, that there are only two ways I can get any configuration of an appointment, light rain, and no track maintenance. Either this particular setting of variables happens and the train is on time, or this particular setting of variables happens and the train is delayed. Those are two possible cases that I would want to consider. And if I add those two cases up, well, then I get the result just by adding up all of the possibilities for the hidden variable or variables that there are multiple. But since there’s only one hidden variable here, train, all I need to do is iterate over all the possible values for that hidden variable train and add up their probabilities. So this probability expression here becomes probability distribution over appointment, light, no rain, and train is on time, and the probability distribution over the appointment, light rain, no track maintenance, and that the train is delayed, for example. So I take both of the possible values for train, go ahead and add them up. These are just joint probabilities that we saw earlier, how to calculate just by going parent, parent, parent, parent, and calculating those probabilities and multiplying them together. And then you’ll need to normalize them at the end, speaking at a high level, to make sure that everything adds up to the number 1. So the formula for how you do this in a process known as inference by enumeration looks a little bit complicated, but ultimately it looks like this. And let’s now try to distill what it is that all of these symbols actually mean. Let’s start here. What I care about knowing is the probability of x, my query variable, given some sort of evidence. What do I know about conditional probabilities? Well, a conditional probability is proportional to the joint probability. So it is some alpha, some normalizing constant, multiplied by this joint probability of x and evidence. And how do I calculate that? Well, to do that, I’m going to marginalize over all of the hidden variables, all the variables that I don’t directly observe the values for. I’m basically going to iterate over all of the possibilities that it could happen and just sum them all up. And so I can translate this into a sum over all y, which ranges over all the possible hidden variables and the values that they could take on, and adds up all of those possible individual probabilities. And that is going to allow me to do this process of inference by enumeration. Now, ultimately, it’s pretty annoying if we as humans have to do all this math for ourselves. But turns out this is where computers and AI can be particularly helpful, that we can program a computer to understand a Bayesian network, to be able to understand these inference procedures, and to be able to do these calculations. And using the information you’ve seen here, you could implement a Bayesian network from scratch yourself. But turns out there are a lot of libraries, especially written in Python, that allow us to make it easier to do this sort of probabilistic inference, to be able to take a Bayesian network and do these sorts of calculations, so that you don’t need to know and understand all of the underlying math, though it’s helpful to have a general sense for how it works. But you just need to be able to describe the structure of the network and make queries in order to be able to produce the result. And so let’s take a look at an example of that right now. It turns out that there are a lot of possible libraries that exist in Python for doing this sort of inference. It doesn’t matter too much which specific library you use. They all behave in fairly similar ways. But the library I’m going to use here is one known as pomegranate. And here inside of model.py, I have defined a Bayesian network, just using the structure and the syntax that the pomegranate library expects. And what I’m effectively doing is just, in Python, creating nodes to represent each of the nodes of the Bayesian network that you saw me describe a moment ago. So here on line four, after I’ve imported pomegranate, I’m defining a variable called rain that is going to represent a node inside of my Bayesian network. It’s going to be a node that follows this distribution, where there are three possible values, none for no rain, light for light rain, heavy for heavy rain. And these are the probabilities of each of those taking place. 0.7 is the likelihood of no rain, 0.2 for light rain, 0.1 for heavy rain. Then after that, we go to the next variable, the variable for track maintenance, for example, which is dependent upon that rain variable. And this, instead of being an unconditional distribution, is a conditional distribution, as indicated by a conditional probability table here. And the idea is that I’m following this is conditional on the distribution of rain. So if there is no rain, then the chance that there is, yes, track maintenance is 0.4. If there’s no rain, the chance that there is no track maintenance is 0.6. Likewise, for light rain, I have a distribution. For heavy rain, I have a distribution as well. But I’m effectively encoding the same information you saw represented graphically a moment ago. But I’m telling this Python program that the maintenance node obeys this particular conditional probability distribution. And we do the same thing for the other random variables as well. Train was a node inside my distribution that was a conditional probability table with two parents. It was dependent not only on rain, but also on track maintenance. And so here I’m saying something like, given that there is no rain and, yes, track maintenance, the probability that my train is on time is 0.8. And the probability that it’s delayed is 0.2. And likewise, I can do the same thing for all of the other possible values of the parents of the train node inside of my Bayesian network by saying, for all of those possible values, here is the distribution that the train node should follow. Then I do the same thing for an appointment based on the distribution of the variable train. Then at the end, what I do is actually construct this network by describing what the states of the network are and by adding edges between the dependent nodes. So I create a new Bayesian network, add states to it, one for rain, one for maintenance, one for the train, one for the appointment. And then I add edges connecting the related pieces. Rain has an arrow to maintenance because rain influences track maintenance. Rain also influences the train. Maintenance also influences the train. And train influences whether I make it to my appointment and bake just finalizes the model and does some additional computation. So the specific syntax of this is not really the important part. Pomegranate just happens to be one of several different libraries that can all be used for similar purposes. And you could describe and define a library for yourself that implemented similar things. But the key idea here is that someone can design a library for a general Bayesian network that has nodes that are based upon its parents. And then all a programmer needs to do using one of those libraries is to define what those nodes and what those probability distributions are. And we can begin to do some interesting logic based on it. So let’s try doing that conditional or joint probability calculation that we saw us do by hand before by going into likelihood.py, where here I’m importing the model that I just defined a moment ago. And here I’d just like to calculate model.probability, which calculates the probability for a given observation. And I’d like to calculate the probability of no rain, no track maintenance, my train is on time, and I’m able to attend the meeting. So sort of the optimal scenario that there is no rain and no maintenance on the track, my train is on time, and I’m able to attend the meeting. What is the probability that all of that actually happens? And I can calculate that using the library and just print out its probability. And so I’ll go ahead and run python of likelihood.py. And I see that, OK, the probability is about 0.34. So about a third of the time, everything goes right for me in this case. No rain, no track maintenance, train is on time, and I’m able to attend the meeting. But I could experiment with this, try and calculate other probabilities as well. What’s the probability that everything goes right up until the train, but I still miss my meeting? So no rain, no track maintenance, train is on time, but I miss the appointment. Let’s calculate that probability. And all right, that has a probability of about 0.04. So about 4% of the time, the train will be on time, there won’t be any rain, no track maintenance, and yet I’ll still miss the meeting. And so this is really just an implementation of the calculation of the joint probabilities that we did before. What this library is likely doing is first figuring out the probability of no rain, then figuring out the probability of no track maintenance given no rain, then the probability that my train is on time given both of these values, and then the probability that I miss my appointment given that I know that the train was on time. So this, again, is the calculation of that joint probability. And turns out we can also begin to have our computer solve inference problems as well, to begin to infer, based on information, evidence that we see, what is the likelihood of other variables also being true. So let’s go into inference.py, for example. We’re here, I’m again importing that exact same model from before, importing all the nodes and all the edges and the probability distribution that is encoded there as well. And now there’s a function for doing some sort of prediction. And here, into this model, I pass in the evidence that I observe. So here, I’ve encoded into this Python program the evidence that I have observed. I have observed the fact that the train is delayed. And that is the value for one of the four random variables inside of this Bayesian network. And using that information, I would like to be able to draw inspiration and figure out inferences about the values of the other random variables that are inside of my Bayesian network. I would like to make predictions about everything else. So all of the actual computational logic is happening in just these three lines, where I’m making this call to this prediction. Down below, I’m just iterating over all of the states and all the predictions and just printing them out so that we can visually see what the results are. But let’s find out, given the train is delayed, what can I predict about the values of the other random variables? Let’s go ahead and run python inference.py. I run that, and all right, here is the result that I get. Given the fact that I know that the train is delayed, this is evidence that I have observed. Well, given that there is a 45% chance or a 46% chance that there was no rain, a 31% chance there was light rain, a 23% chance there was heavy rain, I can see a probability distribution of a track maintenance and a probability distribution over whether I’m able to attend or miss my appointment. Now, we know that whether I attend or miss the appointment, that is only dependent upon the train being delayed or not delayed. It shouldn’t depend on anything else. So let’s imagine, for example, that I knew that there was heavy rain. That shouldn’t affect the distribution for making the appointment. And indeed, if I go up here and add some evidence, say that I know that the value of rain is heavy. That is evidence that I now have access to. I now have two pieces of evidence. I know that the rain is heavy, and I know that my train is delayed. I can calculate the probability by running this inference procedure again and seeing the result. I know that the rain is heavy. I know my train is delayed. The probability distribution for track maintenance changed. Given that I know that there’s heavy rain, now it’s more likely that there is no track maintenance, 88%, as opposed to 64% from here before. And now, what is the probability that I make the appointment? Well, that’s the same as before. It’s still going to be attend the appointment with probability 0.6, missed the appointment with probability 0.4, because it was only dependent upon whether or not my train was on time or delayed. And so this here is implementing that idea of that inference algorithm to be able to figure out, based on the evidence that I have, what can we infer about the values of the other variables that exist as well. So inference by enumeration is one way of doing this inference procedure, just looping over all of the values the hidden variables could take on and figuring out what the probability is. Now, it turns out this is not particularly efficient. And there are definitely optimizations you can make by avoiding repeated work. If you’re calculating the same sort of probability multiple times, there are ways of optimizing the program to avoid having to recalculate the same probabilities again and again. But even then, as the number of variables get large, as the number of possible values of variables could take on, get large, we’re going to start to have to do a lot of computation, a lot of calculation, to be able to do this inference. And at that point, it might start to get unreasonable, in terms of the amount of time that it would take to be able to do this sort of exact inference. And it’s for that reason that oftentimes, when it comes towards probability and things we’re not entirely sure about, we don’t always care about doing exact inference and knowing exactly what the probability is. But if we can approximate the inference procedure, do some sort of approximate inference, that that can be pretty good as well. That if I don’t know the exact probability, but I have a general sense for the probability that I can get increasingly accurate with more time, that that’s probably pretty good, especially if I can get that to happen even faster. So how could I do approximate inference inside of a Bayesian network? Well, one method is through a procedure known as sampling. In the process of sampling, I’m going to take a sample of all of the variables inside of this Bayesian network here. And how am I going to sample? Well, I’m going to sample one of the values from each of these nodes according to their probability distribution. So how might I take a sample of all these nodes? Well, I’ll start at the root. I’ll start with rain. Here’s the distribution for rain. And I’ll go ahead and, using a random number generator or something like it, randomly pick one of these three values. I’ll pick none with probability 0.7, light with probability 0.2, and heavy with probability 0.1. So I’ll randomly just pick one of them according to that distribution. And maybe in this case, I pick none, for example. Then I do the same thing for the other variable. Maintenance also has a probability distribution. And I’m going to sample. Now, there are three probability distributions here. But I’m only going to sample from this first row here, because I’ve observed already in my sample that the value of rain is none. So given that rain is none, I’m going to sample from this distribution to say, all right, what should the value of maintenance be? And in this case, maintenance is going to be, let’s just say yes, which happens 40% of the time in the event that there is no rain, for example. And we’ll sample all of the rest of the nodes in this way as well, that I want to sample from the train distribution. And I’ll sample from this first row here, where there is no rain, but there is track maintenance. And I’ll sample 80% of the time. I’ll say the train is on time. 20% of the time, I’ll say the train is delayed. And finally, we’ll do the same thing for whether I make it to my appointment or not. Did I attend or miss the appointment? We’ll sample based on this distribution and maybe say that in this case, I attend the appointment, which happens 90% of the time when the train is actually on time. So by going through these nodes, I can very quickly just do some sampling and get a sample of the possible values that could come up from going through this entire Bayesian network according to those probability distributions. And where this becomes powerful is if I do this not once, but I do this thousands or tens of thousands of times and generate a whole bunch of samples all using this distribution. I get different samples. Maybe some of them are the same. But I get a value for each of the possible variables that could come up. And so then if I’m ever faced with a question, a question like, what is the probability that the train is on time, you could do an exact inference procedure. This is no different than the inference problem we had before where I could just marginalize, look at all the possible other values of the variables, and do the computation of inference by enumeration to find out this probability exactly. But I could also, if I don’t care about the exact probability, just sample it, approximate it to get close. And this is a powerful tool in AI where we don’t need to be right 100% of the time or we don’t need to be exactly right. If we just need to be right with some probability, we can often do so more effectively, more efficiently. And so if here now are all of those possible samples, I’ll highlight the ones where the train is on time. I’m ignoring the ones where the train is delayed. And in this case, there’s like six out of eight of the samples have the train is arriving on time. And so maybe in this case, I can say that in six out of eight cases, that’s the likelihood that the train is on time. And with eight samples, that might not be a great prediction. But if I had thousands upon thousands of samples, then this could be a much better inference procedure to be able to do these sorts of calculations. So this is a direct sampling method to just do a bunch of samples and then figure out what the probability of some event is. Now, this from before was an unconditional probability. What is the probability that the train is on time? And I did that by looking at all the samples and figuring out, right, here are the ones where the train is on time. But sometimes what I want to calculate is not an unconditional probability, but rather a conditional probability, something like what is the probability that there is light rain, given that the train is on time, something to that effect. And to do that kind of calculation, well, what I might do is here are all the samples that I have. And I want to calculate a probability distribution, given that I know that the train is on time. So to be able to do that, I can kind of look at the two cases where the train was delayed and ignore or reject them, sort of exclude them from the possible samples that I’m considering. And now I want to look at these remaining cases where the train is on time. Here are the cases where there is light rain. And I say, OK, these are two out of the six possible cases. That can give me an approximation for the probability of light rain, given the fact that I know the train was on time. And I did that in almost exactly the same way, just by adding an additional step, by saying that, all right, when I take each sample, let me reject all of the samples that don’t match my evidence and only consider the samples that do match what it is that I have in my evidence that I want to make some sort of calculation about. And it turns out, using the libraries that we’ve had for Bayesian networks, we can begin to implement this same sort of idea, like implement rejection sampling, which is what this method is called, to be able to figure out some probability, not via direct inference, but instead by sampling. So what I have here is a program called sample.py. Imports the exact same model. And what I define first is a program to generate a sample. And the way I generate a sample is just by looping over all of the states. The states need to be in some sort of order to make sure I’m looping in the correct order. But effectively, if it is a conditional distribution, I’m going to sample based on the parents. And otherwise, I’m just going to directly sample the variable, like rain, which has no parents. It’s just an unconditional distribution and keep track of all those parent samples and return the final sample. The exact syntax of this, again, not particularly important. It just happens to be part of the implementation details of this particular library. The interesting logic is down below. Now that I have the ability to generate a sample, if I want to know the distribution of the appointment random variable, given that the train is delayed, well, then I can begin to do calculations like this. Let me take 10,000 samples and assemble all my results in this list called data. I’ll go ahead and loop n times, in this case, 10,000 times. I’ll generate a sample. And I want to know the distribution of appointment, given that the train is delayed. So according to rejection sampling, I’m only going to consider samples where the train is delayed. If the train is not delayed, I’m not going to consider those values at all. So I’m going to say, all right, if I take the sample, look at the value of the train random variable, if the train is delayed, well, let me go ahead and add to my data that I’m collecting the value of the appointment random variable that it took on in this particular sample. So I’m only considering the samples where the train is delayed. And for each of those samples, considering what the value of appointment is, and then at the end, I’m using a Python class called counter, which quickly counts up all the values inside of a data set. So I can take this list of data and figure out how many times was my appointment made and how many times was my appointment missed. And so this here, with just a couple lines of code, is an implementation of rejection sampling. And I can run it by going ahead and running Python sample.py. And when I do that, here is the result I get. This is the result of the counter. 1,251 times, I was able to attend the meeting. And 856 times, I was able to miss the meeting. And you can imagine, by doing more and more samples, I’ll be able to get a better and better, more accurate result. And this is a randomized process. It’s going to be an approximation of the probability. If I run it a different time, you’ll notice the numbers are similar, 12, 72, and 905. But they’re not identical because there’s some randomization, some likelihood that things might be higher or lower. And so this is why we generally want to try and use more samples so that we can have a greater amount of confidence in our result, be more sure about the result that we’re getting of whether or not it accurately reflects or represents the actual underlying probabilities that are inherent inside of this distribution. And so this, then, was an instance of rejection sampling. And it turns out there are a number of other sampling methods that you could use to begin to try to sample. One problem that rejection sampling has is that if the evidence you’re looking for is a fairly unlikely event, well, you’re going to be rejecting a lot of samples. Like if I’m looking for the probability of x given some evidence e, if e is very unlikely to occur, like occurs maybe one every 1,000 times, then I’m only going to be considering 1 out of every 1,000 samples that I do, which is a pretty inefficient method for trying to do this sort of calculation. I’m throwing away a lot of samples. And it takes computational effort to be able to generate those samples. So I’d like to not have to do something like that. So there are other sampling methods that can try and address this. One such sampling method is called likelihood weighting. In likelihood weighting, we follow a slightly different procedure. And the goal is to avoid needing to throw out samples that didn’t match the evidence. And so what we’ll do is we’ll start by fixing the values for the evidence variables. Rather than sample everything, we’re going to fix the values of the evidence variables and not sample those. Then we’re going to sample all the other non-evidence variables in the same way, just using the Bayesian network looking at the probability distributions, sampling all the non-evidence variables. But then what we need to do is weight each sample by its likelihood. If our evidence is really unlikely, we want to make sure that we’ve taken into account how likely was the evidence to actually show up in the sample. If I have a sample where the evidence was much more likely to show up than another sample, then I want to weight the more likely one higher. So we’re going to weight each sample by its likelihood, where likelihood is just defined as the probability of all the evidence. Given all the evidence we have, what is the probability that it would happen in that particular sample? So before, all of our samples were weighted equally. They all had a weight of 1 when we were calculating the overall average. In this case, we’re going to weight each sample, multiply each sample by its likelihood in order to get the more accurate distribution. So what would this look like? Well, if I ask the same question, what is the probability of light rain, given that the train is on time, when I do the sampling procedure and start by trying to sample, I’m going to start by fixing the evidence variable. I’m already going to have in my sample the train is on time. That way, I don’t have to throw out anything. I’m only sampling things where I know the value of the variables that are my evidence are what I expect them to be. So I’ll go ahead and sample from rain. And maybe this time, I sample light rain instead of no rain. Then I’ll sample from track maintenance and say, maybe, yes, there’s track maintenance. Then for train, well, I’ve already fixed it in place. Train was an evidence variable. So I’m not going to bother sampling again. I’ll just go ahead and move on. I’ll move on to appointment and go ahead and sample from appointment as well. So now I’ve generated a sample. I’ve generated a sample by fixing this evidence variable and sampling the other three. And the last step is now weighting the sample. How much weight should it have? And the weight is based on how probable is it that the train was actually on time, this evidence actually happened, given the values of these other variables, light rain and the fact that, yes, there was track maintenance. Well, to do that, I can just go back to the train variable and say, all right, if there was light rain and track maintenance, the likelihood of my evidence, the likelihood that my train was on time, is 0.6. And so this particular sample would have a weight of 0.6. And I could repeat the sampling procedure again and again. Each time every sample would be given a weight according to the probability of the evidence that I see associated with it. And there are other sampling methods that exist as well, but all of them are designed to try and get it the same idea, to approximate the inference procedure of figuring out the value of a variable. So we’ve now dealt with probability as it pertains to particular variables that have these discrete values. But what we haven’t really considered is how values might change over time. That we’ve considered something like a variable for rain, where rain can take on values of none or light rain or heavy rain. But in practice, usually when we consider values for variables like rain, we like to consider it for over time, how do the values of these variables change? What do we do with when we’re dealing with uncertainty over a period of time, which can come up in the context of weather, for example, if I have sunny days and I have rainy days. And I’d like to know not just what is the probability that it’s raining now, but what is the probability that it rains tomorrow, or the day after that, or the day after that. And so to do this, we’re going to introduce a slightly different kind of model. But here, we’re going to have a random variable, not just one for the weather, but for every possible time step. And you can define time step however you like. A simple way is just to use days as your time step. And so we can define a variable called x sub t, which is going to be the weather at time t. So x sub 0 might be the weather on day 0. x sub 1 might be the weather on day 1, so on and so forth. x sub 2 is the weather on day 2. But as you can imagine, if we start to do this over longer and longer periods of time, there’s an incredible amount of data that might go into this. If you’re keeping track of data about the weather for a year, now suddenly you might be trying to predict the weather tomorrow, given 365 days of previous pieces of evidence. And that’s a lot of evidence to have to deal with and manipulate and calculate. Probably nobody knows what the exact conditional probability distribution is for all of those combinations of variables. And so when we’re trying to do this inference inside of a computer, when we’re trying to reasonably do this sort of analysis, it’s helpful to make some simplifying assumptions, some assumptions about the problem that we can just assume are true, to make our lives a little bit easier. Even if they’re not totally accurate assumptions, if they’re close to accurate or approximate, they’re usually pretty good. And the assumption we’re going to make is called the Markov assumption, which is the assumption that the current state depends only on a finite fixed number of previous states. So the current day’s weather depends not on all the previous day’s weather for the rest of all of history, but the current day’s weather I can predict just based on yesterday’s weather, or just based on the last two days weather, or the last three days weather. But oftentimes, we’re going to deal with just the one previous state that helps to predict this current state. And by putting a whole bunch of these random variables together, using this Markov assumption, we can create what’s called a Markov chain, where a Markov chain is just some sequence of random variables where each of the variables distribution follows that Markov assumption. And so we’ll do an example of this where the Markov assumption is, I can predict the weather. Is it sunny or rainy? And we’ll just consider those two possibilities for now, even though there are other types of weather. But I can predict each day’s weather just on the prior day’s weather, using today’s weather, I can come up with a probability distribution for tomorrow’s weather. And here’s what this weather might look like. It’s formatted in terms of a matrix, as you might describe it, as rows and columns of values, where on the left-hand side, I have today’s weather, represented by the variable x sub t. And over here in the columns, I have tomorrow’s weather, represented by the variable x sub t plus 1, t plus 1 day’s weather instead. And what this matrix is saying is, if today is sunny, well, then it’s more likely than not that tomorrow is also sunny. Oftentimes, the weather stays consistent for multiple days in a row. And for example, let’s say that if today is sunny, our model says that tomorrow, with probability 0.8, it will also be sunny. And with probability 0.2, it will be raining. And likewise, if today is raining, then it’s more likely than not that tomorrow is also raining. With probability 0.7, it’ll be raining. With probability 0.3, it will be sunny. So this matrix, this description of how it is we transition from one state to the next state is what we’re going to call the transition model. And using the transition model, you can begin to construct this Markov chain by just predicting, given today’s weather, what’s the likelihood of tomorrow’s weather happening. And you can imagine doing a similar sampling procedure, where you take this information, you sample what tomorrow’s weather is going to be. Using that, you sample the next day’s weather. And the result of that is you can form this Markov chain of like x0, time and time, day zero is sunny, the next day is sunny, maybe the next day it changes to raining, then raining, then raining. And the pattern that this Markov chain follows, given the distribution that we had access to, this transition model here, is that when it’s sunny, it tends to stay sunny for a little while. The next couple of days tend to be sunny too. And when it’s raining, it tends to be raining as well. And so you get a Markov chain that looks like this, and you can do analysis on this. You can say, given that today is raining, what is the probability that tomorrow is raining? Or you can begin to ask probability questions like, what is the probability of this sequence of five values, sun, sun, rain, rain, rain, and answer those sorts of questions too. And it turns out there are, again, many Python libraries for interacting with models like this of probabilities that have distributions and random variables that are based on previous variables according to this Markov assumption. And pomegranate2 has ways of dealing with these sorts of variables. So I’ll go ahead and go into the chain directory, where I have some information about Markov chains. And here, I’ve defined a file called model.py, where I’ve defined in a very similar syntax. And again, the exact syntax doesn’t matter so much as the idea that I’m encoding this information into a Python program so that the program has access to these distributions. I’ve here defined some starting distribution. So every Markov model begins at some point in time, and I need to give it some starting distribution. And so we’ll just say, you know at the start, you can pick 50-50 between sunny and rainy. We’ll say it’s sunny 50% of the time, rainy 50% of the time. And then down below, I’ve here defined the transition model, how it is that I transition from one day to the next. And here, I’ve encoded that exact same matrix from before, that if it was sunny today, then with probability 0.8, it will be sunny tomorrow. And it’ll be rainy tomorrow with probability 0.2. And I likewise have another distribution for if it was raining today instead. And so that alone defines the Markov model. You can begin to answer questions using that model. But one thing I’ll just do is sample from the Markov chain. It turns out there is a method built into this Markov chain library that allows me to sample 50 states from the chain, basically just simulating like 50 instances of weather. And so let me go ahead and run this. Python model.py. And when I run it, what I get is that it’s going to sample from this Markov chain 50 states, 50 days worth of weather that it’s just going to randomly sample. And you can imagine sampling many times to be able to get more data, to be able to do more analysis. But here, for example, it’s sunny two days in a row, rainy a whole bunch of days in a row before it changes back to sun. And so you get this model that follows the distribution that we originally described, that follows the distribution of sunny days tend to lead to more sunny days. Rainy days tend to lead to more rainy days. And that then is a Markov model. And Markov models rely on us knowing the values of these individual states. I know that today is sunny or that today is raining. And using that information, I can draw some sort of inference about what tomorrow is going to be like. But in practice, this often isn’t the case. It often isn’t the case that I know for certain what the exact state of the world is. Oftentimes, the state of the world is exactly unknown. But I’m able to somehow sense some information about that state, that a robot or an AI doesn’t have exact knowledge about the world around it. But it has some sort of sensor, whether that sensor is a camera or sensors that detect distance or just a microphone that is sensing audio, for example. It is sensing data. And using that data, that data is somehow related to the state of the world, even if it doesn’t actually know, our AI doesn’t know, what the underlying true state of the world actually is. And for that, we need to get into the world of sensor models, the way of describing how it is that we translate what the hidden state, the underlying true state of the world, is with what the observation, what it is that the AI knows or the AI has access to, actually is. And so for example, a hidden state might be a robot’s position. If a robot is exploring new uncharted territory, the robot likely doesn’t know exactly where it is. But it does have an observation. It has robot sensor data, where it can sense how far away are possible obstacles around it. And using that information, using the observed information that it has, it can infer something about the hidden state. Because what the true hidden state is influences those observations. Whatever the robot’s true position is affects or has some effect upon what the sensor data of the robot is able to collect is, even if the robot doesn’t actually know for certain what its true position is. Likewise, if you think about a voice recognition or a speech recognition program that listens to you and is able to respond to you, something like Alexa or what Apple and Google are doing with their voice recognition as well, that you might imagine that the hidden state, the underlying state, is what words are actually spoken. The true nature of the world contains you saying a particular sequence of words, but your phone or your smart home device doesn’t know for sure exactly what words you said. The only observation that the AI has access to is some audio waveforms. And those audio waveforms are, of course, dependent upon this hidden state. And you can infer, based on those audio waveforms, what the words spoken likely were. But you might not know with 100% certainty what that hidden state actually is. And it might be a task to try and predict, given this observation, given these audio waveforms, can you figure out what the actual words spoken are. And likewise, you might imagine on a website, true user engagement. Might be information you don’t directly have access to. But you can observe data, like website or app analytics, about how often was this button clicked or how often are people interacting with a page in a particular way. And you can use that to infer things about your users as well. So this type of problem comes up all the time when we’re dealing with AI and trying to infer things about the world. That often AI doesn’t really know the hidden true state of the world. All the AI has access to is some observation that is related to the hidden true state. But it’s not direct. There might be some noise there. The audio waveform might have some additional noise that might be difficult to parse. The sensor data might not be exactly correct. There’s some noise that might not allow you to conclude with certainty what the hidden state is, but can allow you to infer what it might be. And so the simple example we’ll take a look at here is imagining the hidden state as the weather, whether it’s sunny or rainy or not. And imagine you are programming an AI inside of a building that maybe has access to just a camera to inside the building. And all you have access to is an observation as to whether or not employees are bringing an umbrella into the building or not. You can detect whether it’s an umbrella or not. And so you might have an observation as to whether or not an umbrella is brought into the building or not. And using that information, you want to predict whether it’s sunny or rainy, even if you don’t know what the underlying weather is. So the underlying weather might be sunny or rainy. And if it’s raining, obviously people are more likely to bring an umbrella. And so whether or not people bring an umbrella, your observation, tells you something about the hidden state. And of course, this is a bit of a contrived example, but the idea here is to think about this more broadly in terms of more generally, any time you observe something, it having to do with some underlying hidden state. And so to try and model this type of idea where we have these hidden states and observations, rather than just use a Markov model, which has state, state, state, state, each of which is connected by that transition matrix that we described before, we’re going to use what we call a hidden Markov model. Very similar to a Markov model, but this is going to allow us to model a system that has hidden states that we don’t directly observe, along with some observed event that we do actually see. And so in addition to that transition model that we still need of saying, given the underlying state of the world, if it’s sunny or rainy, what’s the probability of tomorrow’s weather? We also need another model that, given some state, is going to give us an observation of green, yes, someone brings an umbrella into the office, or red, no, nobody brings umbrellas into the office. And so the observation might be that if it’s sunny, then odds are nobody is going to bring an umbrella to the office. But maybe some people are just being cautious, and they do bring an umbrella to the office anyways. And if it’s raining, then with much higher probability, then people are going to bring umbrellas into the office. But maybe if the rain was unexpected, people didn’t bring an umbrella. And so it might have some other probability as well. And so using the observations, you can begin to predict with reasonable likelihood what the underlying state is, even if you don’t actually get to observe the underlying state, if you don’t get to see what the hidden state is actually equal to. This here we’ll often call the sensor model. It’s also often called the emission probabilities, because the state, the underlying state, emits some sort of emission that you then observe. And so that can be another way of describing that same idea. And the sensor Markov assumption that we’re going to use is this assumption that the evidence variable, the thing we observe, the emission that gets produced, depends only on the corresponding state, meaning it can predict whether or not people will bring umbrellas or not entirely dependent just on whether it is sunny or rainy today. Of course, again, this assumption might not hold in practice, that in practice, it might depend whether or not people bring umbrellas, might depend not just on today’s weather, but also on yesterday’s weather and the day before. But for simplification purposes, it can be helpful to apply this sort of assumption just to allow us to be able to reason about these probabilities a little more easily. And if we’re able to approximate it, we can still often get a very good answer. And so what these hidden Markov models end up looking like is a little something like this, where now, rather than just have one chain of states, like sun, sun, rain, rain, rain, we instead have this upper level, which is the underlying state of the world. Is it sunny or is it rainy? And those are connected by that transition matrix we described before. But each of these states produces an emission, produces an observation that I see, that on this day, it was sunny and people didn’t bring umbrellas. And on this day, it was sunny, but people did bring umbrellas. And on this day, it was raining and people did bring umbrellas, and so on and so forth. And so each of these underlying states represented by x sub t for x sub 1, 0, 1, 2, so on and so forth, produces some sort of observation or emission, which is what the e stands for, e sub 0, e sub 1, e sub 2, so on and so forth. And so this, too, is a way of trying to represent this idea. And what you want to think about is that these underlying states are the true nature of the world, the robot’s position as it moves over time, and that produces some sort of sensor data that might be observed, or what people are actually saying and using the emission data of what audio waveforms do you detect in order to process that data and try and figure it out. And there are a number of possible tasks that you might want to do given this kind of information. And one of the simplest is trying to infer something about the future or the past or about these sort of hidden states that might exist. And so the tasks that you’ll often see, and we’re not going to go into the mathematics of these tasks, but they’re all based on the same idea of conditional probabilities and using the probability distributions we have to draw these sorts of conclusions. One task is called filtering, which is given observations from the start until now, calculate the distribution for the current state, meaning given information about from the beginning of time until now, on which days do people bring an umbrella or not bring an umbrella, can I calculate the probability of the current state that today, is it sunny or is it raining? Another task that might be possible is prediction, which is looking towards the future. Given observations about people bringing umbrellas from the beginning of when we started counting time until now, can I figure out the distribution that tomorrow is it sunny or is it raining? And you can also go backwards as well by a smoothing, where I can say given observations from start until now, calculate the distributions for some past state. Like I know that today people brought umbrellas and tomorrow people brought umbrellas. And so given two days worth of data of people bringing umbrellas, what’s the probability that yesterday it was raining? And that I know that people brought umbrellas today, that might inform that decision as well. It might influence those probabilities. And there’s also a most likely explanation task, in addition to other tasks that might exist as well, which is combining some of these given observations from the start up until now, figuring out the most likely sequence of states. And this is what we’re going to take a look at now, this idea that if I have all these observations, umbrella, no umbrella, umbrella, no umbrella, can I calculate the most likely states of sun, rain, sun, rain, and whatnot that actually represented the true weather that would produce these observations? And this is quite common when you’re trying to do something like voice recognition, for example, that you have these emissions of the audio waveforms, and you would like to calculate based on all of the observations that you have, what is the most likely sequence of actual words, or syllables, or sounds that the user actually made when they were speaking to this particular device, or other tasks that might come up in that context as well. And so we can try this out by going ahead and going into the HMM directory, HMM for Hidden Markov Model. And here, what I’ve done is I’ve defined a model where this model first defines my possible state, sun, and rain, along with their emission probabilities, the observation model, or the emission model, where here, given that I know that it’s sunny, the probability that I see people bring an umbrella is 0.2, the probability of no umbrella is 0.8. And likewise, if it’s raining, then people are more likely to bring an umbrella. Umbrella has probability 0.9, no umbrella has probability 0.1. So the actual underlying hidden states, those states are sun and rain, but the things that I observe, the observations that I can see, are either umbrella or no umbrella as the things that I observe as a result. So this then, I also need to add to it a transition matrix, same as before, saying that if today is sunny, then tomorrow is more likely to be sunny. And if today is rainy, then tomorrow is more likely to be raining. As of before, I give it some starting probabilities, saying at first, 50-50 chance for whether it’s sunny or rainy. And then I can create the model based on that information. Again, the exact syntax of this is not so important, so much as it is the data that I am now encoding into a program, such that now I can begin to do some inference. So I can give my program, for example, a list of observations, umbrella, umbrella, no umbrella, umbrella, umbrella, so on and so forth, no umbrella, no umbrella. And I would like to calculate, I would like to figure out the most likely explanation for these observations. What is likely is whether rain, rain, is this rain, or is it more likely that this was actually sunny, and then it switched back to it being rainy? And that’s an interesting question. We might not be sure, because it might just be that it just so happened on this rainy day, people decided not to bring an umbrella. Or it could be that it switched from rainy to sunny back to rainy, which doesn’t seem too likely, but it certainly could happen. And using the data we give to the hidden Markov model, our model can begin to predict these answers, can begin to figure it out. So we’re going to go ahead and just predict these observations. And then for each of those predictions, go ahead and print out what the prediction is. And this library just so happens to have a function called predict that does this prediction process for me. So I’ll run python sequence.py. And the result I get is this. This is the prediction based on the observations of what all of those states are likely to be. And it’s likely to be rain and rain. In this case, it thinks that what most likely happened is that it was sunny for a day and then went back to being rainy. But in different situations, if it was rainy for longer maybe, or if the probabilities were slightly different, you might imagine that it’s more likely that it was rainy all the way through. And it just so happened on one rainy day, people decided not to bring umbrellas. And so here, too, Python libraries can begin to allow for the sort of inference procedure. And by taking what we know and by putting it in terms of these tasks that already exist, these general tasks that work with hidden Markov models, then any time we can take an idea and formulate it as a hidden Markov model, formulate it as something that has hidden states and observed emissions that result from those states, then we can take advantage of these algorithms that are known to exist for trying to do this sort of inference. So now we’ve seen a couple of ways that AI can begin to deal with uncertainty. We’ve taken a look at probability and how we can use probability to describe numerically things that are likely or more likely or less likely to happen than other events or other variables. And using that information, we can begin to construct these standard types of models, things like Bayesian networks and Markov chains and hidden Markov models that all allow us to be able to describe how particular events relate to other events or how the values of particular variables relate to other variables, not for certain, but with some sort of probability distribution. And by formulating things in terms of these models that already exist, we can take advantage of Python libraries that implement these sort of models already and allow us just to be able to use them to produce some sort of resulting effect. So all of this then allows our AI to begin to deal with these sort of uncertain problems so that our AI doesn’t need to know things for certain but can infer based on information it doesn’t know. Next time, we’ll take a look at additional types of problems that we can solve by taking advantage of AI-related algorithms, even beyond the world of the types of problems we’ve already explored. We’ll see you next time. OK. Welcome back, everyone, to an introduction to artificial intelligence with Python. And now, so far, we’ve taken a look at a couple of different types of problems. We’ve seen classical search problems where we’re trying to get from an initial state to a goal by figuring out some optimal path. We’ve taken a look at adversarial search where we have a game-playing agent that is trying to make the best move. We’ve seen knowledge-based problems where we’re trying to use logic and inference to be able to figure out and draw some additional conclusions. And we’ve seen some probabilistic models as well where we might not have certain information about the world, but we want to use the knowledge about probabilities that we do have to be able to draw some conclusions. Today, we’re going to turn our attention to another category of problems generally known as optimization problems, where optimization is really all about choosing the best option from a set of possible options. And we’ve already seen optimization in some contexts, like game-playing, where we’re trying to create an AI that chooses the best move out of a set of possible moves. But what we’ll take a look at today is a category of types of problems and algorithms to solve them that can be used in order to deal with a broader range of potential optimization problems. And the first of the algorithms that we’ll take a look at is known as a local search. And local search differs from search algorithms we’ve seen before in the sense that the search algorithms we’ve looked at so far, which are things like breadth-first search or A-star search, for example, generally maintain a whole bunch of different paths that we’re simultaneously exploring, and we’re looking at a bunch of different paths at once trying to find our way to the solution. On the other hand, in local search, this is going to be a search algorithm that’s really just going to maintain a single node, looking at a single state. And we’ll generally run this algorithm by maintaining that single node and then moving ourselves to one of the neighboring nodes throughout this search process. And this is generally useful in context not like these problems, which we’ve seen before, like a maze-solving situation where we’re trying to find our way from the initial state to the goal by following some path. But local search is most applicable when we really don’t care about the path at all, and all we care about is what the solution is. And in the case of solving a maze, the solution was always obvious. You could point to the solution. You know exactly what the goal is, and the real question is, what is the path to get there? But local search is going to come up in cases where figuring out exactly what the solution is, exactly what the goal looks like, is actually the heart of the challenge. And to give an example of one of these kinds of problems, we’ll consider a scenario where we have two types of buildings, for example. We have houses and hospitals. And our goal might be in a world that’s formatted as this grid, where we have a whole bunch of houses, a house here, house here, two houses over there, maybe we want to try and find a way to place two hospitals on this map. So maybe a hospital here and a hospital there. And the problem now is we want to place two hospitals on the map, but we want to do so with some sort of objective. And our objective in this case is to try and minimize the distance of any of the houses from a hospital. So you might imagine, all right, what’s the distance from each of the houses to their nearest hospital? There are a number of ways we could calculate that distance. But one way is using a heuristic we’ve looked at before, which is the Manhattan distance, this idea of how many rows and columns would you have to move inside of this grid layout in order to get to a hospital, for example. And it turns out, if you take each of these four houses and figure out, all right, how close are they to their nearest hospital, you get something like this, where this house is three away from a hospital, this house is six away, and these two houses are each four away. And if you add all those numbers up together, you get a total cost of 17, for example. So for this particular configuration of hospitals, a hospital here and a hospital there, that state, we might say, has a cost of 17. And the goal of this problem now that we would like to apply a search algorithm to figure out is, can you solve this problem to find a way to minimize that cost? Minimize the total amount if you sum up all of the distances from all the houses to the nearest hospital. How can we minimize that final value? And if we think about this problem a little bit more abstractly, abstracting away from this specific problem and thinking more generally about problems like it, you can often formulate these problems by thinking about them as a state-space landscape, as we’ll soon call it. Here in this diagram of a state-space landscape, each of these vertical bars represents a particular state that our world could be in. So for example, each of these vertical bars represents a particular configuration of two hospitals. And the height of this vertical bar is generally going to represent some function of that state, some value of that state. So maybe in this case, the height of the vertical bar represents what is the cost of this particular configuration of hospitals in terms of what is the sum total of all the distances from all of the houses to their nearest hospital. And generally speaking, when we have a state-space landscape, we want to do one of two things. We might be trying to maximize the value of this function, trying to find a global maximum, so to speak, of this state-space landscape, a single state whose value is higher than all of the other states that we could possibly choose from. And generally in this case, when we’re trying to find a global maximum, we’ll call the function that we’re trying to optimize some objective function, some function that measures for any given state how good is that state, such that we can take any state, pass it into the objective function, and get a value for how good that state is. And ultimately, what our goal is is to find one of these states that has the highest possible value for that objective function. An equivalent but reversed problem is the problem of finding a global minimum, some state that has a value after you pass it into this function that is lower than all of the other possible values that we might choose from. And generally speaking, when we’re trying to find a global minimum, we call the function that we’re calculating a cost function. Generally, each state has some sort of cost, whether that cost is a monetary cost, or a time cost, or in the case of the houses and hospitals, we’ve been looking at just now, a distance cost in terms of how far away each of the houses is from a hospital. And we’re trying to minimize the cost, find the state that has the lowest possible value of that cost. So these are the general types of ideas we might be trying to go for within a state space landscape, trying to find a global maximum, or trying to find a global minimum. And how exactly do we do that? We’ll recall that in local search, we generally operate this algorithm by maintaining just a single state, just some current state represented inside of some node, maybe inside of a data structure, where we’re keeping track of where we are currently. And then ultimately, what we’re going to do is from that state, move to one of its neighbor states. So in this case, represented in this one-dimensional space by just the state immediately to the left or to the right of it. But for any different problem, you might define what it means for there to be a neighbor of a particular state. In the case of a hospital, for example, that we were just looking at, a neighbor might be moving one hospital one space to the left or to the right or up or down. Some state that is close to our current state, but slightly different, and as a result, might have a slightly different value in terms of its objective function or in terms of its cost function. So this is going to be our general strategy in local search, to be able to take a state, maintaining some current node, and move where we’re looking at in the state space landscape in order to try to find a global maximum or a global minimum somehow. And perhaps the simplest of algorithms that we could use to implement this idea of local search is an algorithm known as hill climbing. And the basic idea of hill climbing is, let’s say I’m trying to maximize the value of my state. I’m trying to figure out where the global maximum is. I’m going to start at a state. And generally, what hill climbing is going to do is it’s going to consider the neighbors of that state, that from this state, all right, I could go left or I could go right, and this neighbor happens to be higher and this neighbor happens to be lower. And in hill climbing, if I’m trying to maximize the value, I’ll generally pick the highest one I can between the state to the left and right of me. This one is higher. So I’ll go ahead and move myself to consider that state instead. And then I’ll repeat this process, continually looking at all of my neighbors and picking the highest neighbor, doing the same thing, looking at my neighbors, picking the highest of my neighbors, until I get to a point like right here, where I consider both of my neighbors and both of my neighbors have a lower value than I do. This current state has a value that is higher than any of its neighbors. And at that point, the algorithm terminates. And I can say, all right, here I have now found the solution. And the same thing works in exactly the opposite way for trying to find a global minimum. But the algorithm is fundamentally the same. If I’m trying to find a global minimum and say my current state starts here, I’ll continually look at my neighbors, pick the lowest value that I possibly can, until I eventually, hopefully, find that global minimum, a point at which when I look at both of my neighbors, they each have a higher value. And I’m trying to minimize the total score or cost or value that I get as a result of calculating some sort of cost function. So we can formulate this graphical idea in terms of pseudocode. And the pseudocode for hill climbing might look like this. We define some function called hill climb that takes as input the problem that we’re trying to solve. And generally, we’re going to start in some sort of initial state. So I’ll start with a variable called current that is keeping track of my initial state, like an initial configuration of hospitals. And maybe some problems lend themselves to an initial state, some place where you begin. In other cases, maybe not, in which case we might just randomly generate some initial state, just by choosing two locations for hospitals at random, for example, and figuring out from there how we might be able to improve. But that initial state, we’re going to store inside of current. And now, here comes our loop, some repetitive process we’re going to do again and again until the algorithm terminates. And what we’re going to do is first say, let’s figure out all of the neighbors of the current state. From my state, what are all of the neighboring states for some definition of what it means to be a neighbor? And I’ll go ahead and choose the highest value of all of those neighbors and save it inside of this variable called neighbor. So keep track of the highest-valued neighbor. This is in the case where I’m trying to maximize the value. In the case where I’m trying to minimize the value, you might imagine here, you’ll pick the neighbor with the lowest possible value. But these ideas are really fundamentally interchangeable. And it’s possible, in some cases, there might be multiple neighbors that each have an equally high value or an equally low value in the minimizing case. And in that case, we can just choose randomly from among them. Choose one of them and save it inside of this variable neighbor. And then the key question to ask is, is this neighbor better than my current state? And if the neighbor, the best neighbor that I was able to find, is not better than my current state, well, then the algorithm is over. And I’ll just go ahead and return the current state. If none of my neighbors are better, then I may as well stay where I am, is the general logic of the hill climbing algorithm. But otherwise, if the neighbor is better, then I may as well move to that neighbor. So you might imagine setting current equal to neighbor, where the general idea is if I’m at a current state and I see a neighbor that is better than me, then I’ll go ahead and move there. And then I’ll repeat the process, continually moving to a better neighbor until I reach a point at which none of my neighbors are better than I am. And at that point, we’d say the algorithm can just terminate there. So let’s take a look at a real example of this with these houses and hospitals. So we’ve seen now that if we put the hospitals in these two locations, that has a total cost of 17. And now we need to define, if we’re going to implement this hill climbing algorithm, what it means to take this particular configuration of hospitals, this particular state, and get a neighbor of that state. And a simple definition of neighbor might be just, let’s pick one of the hospitals and move it by one square, the left or right or up or down, for example. And that would mean we have six possible neighbors from this particular configuration. We could take this hospital and move it to any of these three possible squares, or we take this hospital and move it to any of those three possible squares. And each of those would generate a neighbor. And what I might do is say, all right, here’s the locations and the distances between each of the houses and their nearest hospital. Let me consider all of the neighbors and see if any of them can do better than a cost of 17. And it turns out there are a couple of ways that we could do that. And it doesn’t matter if we randomly choose among all the ways that are the best. But one such possible way is by taking a look at this hospital here and considering the directions in which it might move. If we hold this hospital constant, if we take this hospital and move it one square up, for example, that doesn’t really help us. It gets closer to the house up here, but it gets further away from the house down here. And it doesn’t really change anything for the two houses along the left-hand side. But if we take this hospital on the right and move it one square down, it’s the opposite problem. It gets further away from the house up above, and it gets closer to the house down below. The real idea, the goal should be to be able to take this hospital and move it one square to the left. By moving it one square to the left, we move it closer to both of these houses on the right without changing anything about the houses on the left. For them, this hospital is still the closer one, so they aren’t affected. So we’re able to improve the situation by picking a neighbor that results in a decrease in our total cost. And so we might do that. Move ourselves from this current state to a neighbor by just taking that hospital and moving it. And at this point, there’s not a whole lot that can be done with this hospital. But there’s still other optimizations we can make, other neighbors we can move to that are going to have a better value. If we consider this hospital, for example, we might imagine that right now it’s a bit far up, that both of these houses are a little bit lower. So we might be able to do better by taking this hospital and moving it one square down, moving it down so that now instead of a cost of 15, we’re down to a cost of 13 for this particular configuration. And we can do even better by taking the hospital and moving it one square to the left. Now instead of a cost of 13, we have a cost of 11, because this house is one away from the hospital. This one is four away. This one is three away. And this one is also three away. So we’ve been able to do much better than that initial cost that we had using the initial configuration. Just by taking every state and asking ourselves the question, can we do better by just making small incremental changes, moving to a neighbor, moving to a neighbor, and moving to a neighbor after that? And now at this point, we can potentially see that at this point, the algorithm is going to terminate. There’s actually no neighbor we can move to that is going to improve the situation, get us a cost that is less than 11. Because if we take this hospital and move it upper to the right, well, that’s going to make it further away. If we take it and move it down, that doesn’t really change the situation. It gets further away from this house but closer to that house. And likewise, the same story was true for this hospital. Any neighbor we move it to, up, left, down, or right, is either going to make it further away from the houses and increase the cost, or it’s going to have no effect on the cost whatsoever. And so the question we might now ask is, is this the best we could do? Is this the best placement of the hospitals we could possibly have? And it turns out the answer is no, because there’s a better way that we could place these hospitals. And in particular, there are a number of ways you could do this. But one of the ways is by taking this hospital here and moving it to this square, for example, moving it diagonally by one square, which was not part of our definition of neighbor. We could only move left, right, up, or down. But this is, in fact, better. It has a total cost of 9. It is now closer to both of these houses. And as a result, the total cost is less. But we weren’t able to find it, because in order to get there, we had to go through a state that actually wasn’t any better than the current state that we had been on previously. And so this appears to be a limitation, or a concern you might have as you go about trying to implement a hill climbing algorithm, is that it might not always give you the optimal solution. If we’re trying to maximize the value of any particular state, we’re trying to find the global maximum, a concern might be that we could get stuck at one of the local maxima, highlighted here in blue, where a local maxima is any state whose value is higher than any of its neighbors. If we ever find ourselves at one of these two states when we’re trying to maximize the value of the state, we’re not going to make any changes. We’re not going to move left or right. We’re not going to move left here, because those states are worse. But yet, we haven’t found the global optimum. We haven’t done as best as we could do. And likewise, in the case of the hospitals, what we’re ultimately trying to do is find a global minimum, find a value that is lower than all of the others. But we have the potential to get stuck at one of the local minima, any of these states whose value is lower than all of its neighbors, but still not as low as the local minima. And so the takeaway here is that it’s not always going to be the case that when we run this naive hill climbing algorithm, that we’re always going to find the optimal solution. There are things that could go wrong. If we started here, for example, and tried to maximize our value as much as possible, we might move to the highest possible neighbor, move to the highest possible neighbor, move to the highest possible neighbor, and stop, and never realize that there’s actually a better state way over there that we could have gone to instead. And other problems you might imagine just by taking a look at this state space landscape are these various different types of plateaus, something like this flat local maximum here, where all six of these states each have the exact same value. And so in the case of the algorithm we showed before, none of the neighbors are better, so we might just get stuck at this flat local maximum. And even if you allowed yourself to move to one of the neighbors, it wouldn’t be clear which neighbor you would ultimately move to, and you could get stuck here as well. And there’s another one over here. This one is called a shoulder. It’s not really a local maximum, because there’s still places where we can go higher, not a local minimum, because we can go lower. So we can still make progress, but it’s still this flat area, where if you have a local search algorithm, there’s potential to get lost here, unable to make some upward or downward progress, depending on whether we’re trying to maximize or minimize it, and therefore another potential for us to be able to find a solution that might not actually be the optimal solution. And so because of this potential, the potential that hill climbing has to not always find us the optimal result, it turns out there are a number of different varieties and variations on the hill climbing algorithm that help to solve the problem better depending on the context, and depending on the specific type of problem, some of these variants might be more applicable than others. What we’ve taken a look at so far is a version of hill climbing generally called steepest ascent hill climbing, where the idea of steepest ascent hill climbing is we are going to choose the highest valued neighbor, in the case where we’re trying to maximize or the lowest valued neighbor in cases where we’re trying to minimize. But generally speaking, if I have five neighbors and they’re all better than my current state, I will pick the best one of those five. Now, sometimes that might work pretty well. It’s sort of a greedy approach of trying to take the best operation at any particular time step, but it might not always work. There might be cases where actually I want to choose an option that is slightly better than me, but maybe not the best one because that later on might lead to a better outcome ultimately. So there are other variants that we might consider of this basic hill climbing algorithm. One is known as stochastic hill climbing. And in this case, we choose randomly from all of our higher value neighbors. So if I’m at my current state and there are five neighbors that are all better than I am, rather than choosing the best one, as steep as the set would do, stochastic will just choose randomly from one of them, thinking that if it’s better, then it’s better. And maybe there’s a potential to make forward progress, even if it is not locally the best option I could possibly choose. First choice hill climbing ends up just choosing the very first highest valued neighbor that it follows, behaving on a similar idea, rather than consider all of the neighbors. As soon as we find a neighbor that is better than our current state, we’ll go ahead and move there. There may be some efficiency improvements there and maybe has the potential to find a solution that the other strategies weren’t able to find. And with all of these variants, we still suffer from the same potential risk, this risk that we might end up at a local minimum or a local maximum. And we can reduce that risk by repeating the process multiple times. So one variant of hill climbing is random restart hill climbing, where the general idea is we’ll conduct hill climbing multiple times. If we apply steepest descent hill climbing, for example, we’ll start at some random state, try and figure out how to solve the problem and figure out what is the local maximum or local minimum we get to. And then we’ll just randomly restart and try again, choose a new starting configuration, try and figure out what the local maximum or minimum is, and do this some number of times. And then after we’ve done it some number of times, we can pick the best one out of all of the ones that we’ve taken a look at. So there’s another option we have access to as well. And then, although I said that generally local search will usually just keep track of a single node and then move to one of its neighbors, there are variants of hill climbing that are known as local beam searches, where rather than keep track of just one current best state, we’re keeping track of k highest valued neighbors, such that rather than starting at one random initial configuration, I might start with 3 or 4 or 5, randomly generate all the neighbors, and then pick the 3 or 4 or 5 best of all of the neighbors that I find, and continually repeat this process, with the idea being that now I have more options that I’m considering, more ways that I could potentially navigate myself to the optimal solution that might exist for a particular problem. So let’s now take a look at some actual code that can implement some of these kinds of ideas, something like steepest ascent hill climbing, for example, for trying to solve this hospital problem. So I’m going to go ahead and go into my hospitals directory, where I’ve actually set up the basic framework for solving this type of problem. I’ll go ahead and go into hospitals.py, and we’ll take a look at the code we’ve created here. I’ve defined a class that is going to represent the state space. So the space has a height, and a width, and also some number of hospitals. So you can configure how big is your map, how many hospitals should go here. We have a function for adding a new house to the state space, and then some functions that are going to get me all of the available spaces for if I want to randomly place hospitals in particular locations. And here now is the hill climbing algorithm. So what are we going to do in the hill climbing algorithm? Well, we’re going to start by randomly initializing where the hospitals are going to go. We don’t know where the hospitals should actually be, so let’s just randomly place them. So here I’m running a loop for each of the hospitals that I have. I’m going to go ahead and add a new hospital at some random location. So I basically get all of the available spaces, and I randomly choose one of them as where I would like to add this particular hospital. I have some logging output and generating some images, which we’ll take a look at a little bit later. But here is the key idea. So I’m going to just keep repeating this algorithm. I could specify a maximum of how many times I want it to run, or I could just run it up until it hits a local maximum or local minimum. And now we’ll basically consider all of the hospitals that could potentially move. So consider each of the two hospitals or more hospitals if they’re more than that. And consider all of the places where that hospital could move to, some neighbor of that hospital that we can move the neighbor to. And then see, is this going to be better than where we were currently? So if it is going to be better, then we’ll go ahead and update our best neighbor and keep track of this new best neighbor that we found. And then afterwards, we can ask ourselves the question, if best neighbor cost is greater than or equal to the cost of the current set of hospitals, meaning if the cost of our best neighbor is greater than the current cost, meaning our best neighbor is worse than our current state, well, then we shouldn’t make any changes at all. And we should just go ahead and return the current set of hospitals. But otherwise, we can update our hospitals in order to change them to one of the best neighbors. And if there are multiple that are all equivalent, I’m here using random.choice to say go ahead and choose one randomly. So this is really just a Python implementation of that same idea that we were just talking about, this idea of taking a current state, some current set of hospitals, generating all of the neighbors, looking at all of the ways we could take one hospital and move it one square to the left or right or up or down, and then figuring out, based on all of that information, which is the best neighbor or the set of all the best neighbors, and then choosing from one of those. And each time, we go ahead and generate an image in order to do that. And so now what we’re doing is if we look down at the bottom, I’m going to randomly generate a space with height 10 and width 20. And I’ll say go ahead and put three hospitals somewhere in the space. I’ll randomly generate 15 houses that I just go ahead and add in random locations. And now I’m going to run this hill climbing algorithm in order to try and figure out where we should place those hospitals. So we’ll go ahead and run this program by running Python hospitals. And we see that we started. Our initial state had a cost of 72, but we were able to continually find neighbors that were able to decrease that cost, decrease to 69, 66, 63, so on and so forth, all the way down to 53, as the best neighbor we were able to ultimately find. And we can take a look at what that looked like by just opening up these files. So here, for example, was the initial configuration. We randomly selected a location for each of these 15 different houses and then randomly selected locations for one, two, three hospitals that were just located somewhere inside of the state space. And if you add up all the distances from each of the houses to their nearest hospital, you get a total cost of about 72. And so now the question is, what neighbors can we move to that improve the situation? And it looks like the first one the algorithm found was by taking this house that was over there on the right and just moving it to the left. And that probably makes sense because if you look at the houses in that general area, really these five houses look like they’re probably the ones that are going to be closest to this hospital over here. Moving it to the left decreases the total distance, at least to most of these houses, though it does increase that distance for one of them. And so we’re able to make these improvements to the situation by continually finding ways that we can move these hospitals around until we eventually settle at this particular state that has a cost of 53, where we figured out a position for each of the hospitals. And now none of the neighbors that we could move to are actually going to improve the situation. We can take this hospital and this hospital and that hospital and look at each of the neighbors. And none of those are going to be better than this particular configuration. And again, that’s not to say that this is the best we could do. There might be some other configuration of hospitals that is a global minimum. And this might just be a local minimum that is the best of all of its neighbors, but maybe not the best in the entire possible state space. And you could search through the entire state space by considering all of the possible configurations for hospitals. But ultimately, that’s going to be very time intensive, especially as our state space gets bigger and there might be more and more possible states. It’s going to take quite a long time to look through all of them. And so being able to use these sort of local search algorithms can often be quite good for trying to find the best solution we can do. And especially if we don’t care about doing the best possible and we just care about doing pretty good and finding a pretty good placement of those hospitals, then these methods can be particularly powerful. But of course, we can try and mitigate some of this concern by instead of using hill climbing to use random restart, this idea of rather than just hill climb one time, we can hill climb multiple times and say, try hill climbing a whole bunch of times on the exact same map and figure out what is the best one that we’ve been able to find. And so I’ve here implemented a function for random restart that restarts some maximum number of times. And what we’re going to do is repeat that number of times this process of just go ahead and run the hill climbing algorithm, figure out what the cost is of getting from all the houses to the hospitals, and then figure out is this better than we’ve done so far. So I can try this exact same idea where instead of running hill climbing, I’ll go ahead and run random restart. And I’ll randomly restart maybe 20 times, for example. And we’ll go ahead and now I’ll remove all the images and then rerun the program. And now we started by finding a original state. When we initially ran hill climbing, the best cost we were able to find was 56. Each of these iterations is a different iteration of the hill climbing algorithm. We’re running hill climbing not one time, but 20 times here, each time going until we find a local minimum in this case. And we look and see each time did we do better than we did the best time we’ve done so far. So we went from 56 to 46. This one was greater, so we ignored it. This one was 41, which was less, so we went ahead and kept that one. And for all of the remaining 16 times that we tried to implement hill climbing and we tried to run the hill climbing algorithm, we couldn’t do any better than that 41. Again, maybe there is a way to do better that we just didn’t find, but it looks like that way ended up being a pretty good solution to the problem. That was attempt number three, starting from counting at zero. So we can take a look at that, open up number three. And this was the state that happened to have a cost of 41, that after running the hill climbing algorithm on some particular random initial configuration of hospitals, this is what we found was the local minimum in terms of trying to minimize the cost. And it looks like we did pretty well. This hospital is pretty close to this region. This one is pretty close to these houses here. This hospital looks about as good as we can do for trying to capture those houses over on that side. And so these sorts of algorithms can be quite useful for trying to solve these problems. But the real problem with many of these different types of hill climbing, steepest of sense, stochastic, first choice, and so forth, is that they never make a move that makes our situation worse. They’re always going to take ourselves in our current state, look at the neighbors, and consider can we do better than our current state and move to one of those neighbors. Which of those neighbors we choose might vary among these various different types of algorithms, but we never go from a current position to a position that is worse than our current position. And ultimately, that’s what we’re going to need to do if we want to be able to find a global maximum or a global minimum. Because sometimes if we get stuck, we want to find some way of dislodging ourselves from our local maximum or local minimum in order to find the global maximum or the global minimum or increase the probability that we do find it. And so the most popular technique for trying to approach the problem from that angle is a technique known as simulated annealing, simulated because it’s modeling after a real physical process of annealing, where you can think about this in terms of physics, a physical situation where you have some system of particles. And you might imagine that when you heat up a particular physical system, there’s a lot of energy there. Things are moving around quite randomly. But over time, as the system cools down, it eventually settles into some final position. And that’s going to be the general idea of simulated annealing. We’re going to simulate that process of some high temperature system where things are moving around randomly quite frequently, but over time decreasing that temperature until we eventually settle at our ultimate solution. And the idea is going to be if we have some state space landscape that looks like this and we begin at its initial state here, if we’re looking for a global maximum and we’re trying to maximize the value of the state, our traditional hill climbing algorithms would just take the state and look at the two neighbor ones and always pick the one that is going to increase the value of the state. But if we want some chance of being able to find the global maximum, we can’t always make good moves. We have to sometimes make bad moves and allow ourselves to make a move in a direction that actually seems for now to make our situation worse such that later we can find our way up to that global maximum in terms of trying to solve that problem. Of course, once we get up to this global maximum, once we’ve done a whole lot of the searching, then we probably don’t want to be moving to states that are worse than our current state. And so this is where this metaphor for annealing starts to come in, where we want to start making more random moves and over time start to make fewer of those random moves based on a particular temperature schedule. So the basic outline looks something like this. Early on in simulated annealing, we have a higher temperature state. And what we mean by a higher temperature state is that we are more likely to accept neighbors that are worse than our current state. We might look at our neighbors. And if one of our neighbors is worse than the current state, especially if it’s not all that much worse, if it’s pretty close but just slightly worse, then we might be more likely to accept that and go ahead and move to that neighbor anyways. But later on as we run simulated annealing, we’re going to decrease that temperature. And at a lower temperature, we’re going to be less likely to accept neighbors that are worse than our current state. Now to formalize this and put a little bit of pseudocode to it, here is what that algorithm might look like. We have a function called simulated annealing that takes as input the problem we’re trying to solve and also potentially some maximum number of times we might want to run the simulated annealing process, how many different neighbors we’re going to try and look for. And that value is going to vary based on the problem you’re trying to solve. We’ll, again, start with some current state that will be equal to the initial state of the problem. But now we need to repeat this process over and over for max number of times. Repeat some process some number of times where we’re first going to calculate a temperature. And this temperature function takes the current time t starting at 1 going all the way up to max and then gives us some temperature that we can use in our computation, where the idea is that this temperature is going to be higher early on and it’s going to be lower later on. So there are a number of ways this temperature function could often work. One of the simplest ways is just to say it is like the proportion of time that we still have remaining. Out of max units of time, how much time do we have remaining? You start off with a lot of that time remaining. And as time goes on, the temperature is going to decrease because you have less and less of that remaining time still available to you. So we calculate a temperature for the current time. And then we pick a random neighbor of the current state. No longer are we going to be picking the best neighbor that we possibly can or just one of the better neighbors that we can. We’re going to pick a random neighbor. It might be better. It might be worse. But we’re going to calculate that. We’re going to calculate delta E, E for energy in this case, which is just how much better is the neighbor than the current state. So if delta E is positive, that means the neighbor is better than our current state. If delta E is negative, that means the neighbor is worse than our current state. And so we can then have a condition that looks like this. If delta E is greater than 0, that means the neighbor state is better than our current state. And if ever that situation arises, we’ll just go ahead and update current to be that neighbor. Same as before, move where we are currently to be the neighbor because the neighbor is better than our current state. We’ll go ahead and accept that. But now the difference is that whereas before, we never, ever wanted to take a move that made our situation worse, now we sometimes want to make a move that is actually going to make our situation worse because sometimes we’re going to need to dislodge ourselves from a local minimum or local maximum to increase the probability that we’re able to find the global minimum or the global maximum a little bit later. And so how do we do that? How do we decide to sometimes accept some state that might actually be worse? Well, we’re going to accept a worse state with some probability. And that probability needs to be based on a couple of factors. It needs to be based in part on the temperature, where if the temperature is higher, we’re more likely to move to a worse neighbor. And if the temperature is lower, we’re less likely to move to a worse neighbor. But it also, to some degree, should be based on delta E. If the neighbor is much worse than the current state, we probably want to be less likely to choose that than if the neighbor is just a little bit worse than the current state. So again, there are a couple of ways you could calculate this. But it turns out one of the most popular is just to calculate E to the power of delta E over T, where E is just a constant. Delta E over T are based on delta E and T here. We calculate that value. And that’ll be some value between 0 and 1. And that is the probability with which we should just say, all right, let’s go ahead and move to that neighbor. And it turns out that if you do the math for this value, when delta E is such that the neighbor is not that much worse than the current state, that’s going to be more likely that we’re going to go ahead and move to that state. And likewise, when the temperature is lower, we’re going to be less likely to move to that neighboring state as well. So now this is the big picture for simulated annealing, this process of taking the problem and going ahead and generating random neighbors will always move to a neighbor if it’s better than our current state. But even if the neighbor is worse than our current state, we’ll sometimes move there depending on how much worse it is and also based on the temperature. And as a result, the hope, the goal of this whole process is that as we begin to try and find our way to the global maximum or the global minimum, we can dislodge ourselves if we ever get stuck at a local maximum or local minimum in order to eventually make our way to exploring the part of the state space that is going to be the best. And then as the temperature decreases, eventually we settle there without moving around too much from what we’ve found to be the globally best thing that we can do thus far. So at the very end, we just return whatever the current state happens to be. And that is the conclusion of this algorithm. We’ve been able to figure out what the solution is. And these types of algorithms have a lot of different applications. Any time you can take a problem and formulate it as something where you can explore a particular configuration and then ask, are any of the neighbors better than this current configuration and have some way of measuring that, then there is an applicable case for these hill climbing, simulated annealing types of algorithms. So sometimes it can be for facility location type problems, like for when you’re trying to plan a city and figure out where the hospitals should be. But there are definitely other applications as well. And one of the most famous problems in computer science is the traveling salesman problem. Traveling salesman problem generally is formulated like this. I have a whole bunch of cities here indicated by these dots. And what I’d like to do is find some route that takes me through all of the cities and ends up back where I started. So some route that starts here, goes through all these cities, and ends up back where I originally started. And what I might like to do is minimize the total distance that I have to travel or the total cost of taking this entire path. And you can imagine this is a problem that’s very applicable in situations like when delivery companies are trying to deliver things to a whole bunch of different houses, they want to figure out, how do I get from the warehouse to all these various different houses and get back again, all using as minimal time and distance and energy as possible. So you might want to try to solve these sorts of problems. But it turns out that solving this particular kind of problem is very computationally difficult. It is a very computationally expensive task to be able to figure it out. This falls under the category of what are known as NP-complete problems, problems that there is no known efficient way to try and solve these sorts of problems. And so what we ultimately have to do is come up with some approximation, some ways of trying to find a good solution, even if we’re not going to find the globally best solution that we possibly can, at least not in a feasible or tractable amount of time. And so what we could do is take the traveling salesman problem and try to formulate it using local search and ask a question like, all right, I can pick some state, some configuration, some route between all of these nodes. And I can measure the cost of that state, figure out what the distance is. And I might now want to try to minimize that cost as much as possible. And then the only question now is, what does it mean to have a neighbor of this state? What does it mean to take this particular route and have some neighboring route that is close to it but slightly different and such that it might have a different total distance? And there are a number of different definitions for what a neighbor of a traveling salesman configuration might look like. But one way is just to say, a neighbor is what happens if we pick two of these edges between nodes and switch them effectively. So for example, I might pick these two edges here, these two that just happened across this node goes here, this node goes there, and go ahead and switch them. And what that process will generally look like is removing both of these edges from the graph, taking this node, and connecting it to the node it wasn’t connected to. So connecting it up here instead. We’ll need to take these arrows that were originally going this way and reverse them, so move them going the other way, and then just fill in that last remaining blank, add an arrow that goes in that direction instead. So by taking two edges and just switching them, I have been able to consider one possible neighbor of this particular configuration. And it looks like this neighbor is actually better. It looks like this probably travels a shorter distance in order to get through all the cities through this route than the current state did. And so you could imagine implementing this idea inside of a hill climbing or simulated annealing algorithm, where we repeat this process to try and take a state of this traveling salesman problem, look at all the neighbors, and then move to the neighbors if they’re better, or maybe even move to the neighbors if they’re worse, until we eventually settle upon some best solution that we’ve been able to find. And it turns out that these types of approximation algorithms, even if they don’t always find the very best solution, can often do pretty well at trying to find solutions that are helpful too. So that then was a look at local search, a particular category of algorithms that can be used for solving a particular type of problem, where we don’t really care about the path to the solution. I didn’t care about the steps I took to decide where the hospitals should go. I just cared about the solution itself. I just care about where the hospitals should be, or what the route through the traveling salesman journey really ought to be. Another type of algorithm that might come up are known as these categories of linear programming types of problems. And linear programming often comes up in the context where we’re trying to optimize for some mathematical function. But oftentimes, linear programming will come up when we might have real numbered values. So it’s not just discrete fixed values that we might have, but any decimal values that we might want to be able to calculate. And so linear programming is a family of types of problems where we might have a situation that looks like this, where the goal of linear programming is to minimize a cost function. And you can invert the numbers and say try and maximize it, but often we’ll frame it as trying to minimize a cost function that has some number of variables, x1, x2, x3, all the way up to xn, just some number of variables that are involved, things that I want to know the values to. And this cost function might have coefficients in front of those variables. And this is what we would call a linear equation, where we just have all of these variables that might be multiplied by a coefficient and then add it together. We’re not going to square anything or cube anything, because that’ll give us different types of equations. With linear programming, we’re just dealing with linear equations in addition to linear constraints, where a constraint is going to look something like if we sum up this particular equation that is just some linear combination of all of these variables, it is less than or equal to some bound b. And we might have a whole number of these various different constraints that we might place onto our linear programming exercise. And likewise, just as we can have constraints that are saying this linear equation is less than or equal to some bound b, it might also be equal to something. That if you want some sum of some combination of variables to be equal to a value, you can specify that. And we can also maybe specify that each variable has lower and upper bounds, that it needs to be a positive number, for example, or it needs to be a number that is less than 50, for example. And there are a number of other choices that we can make there for defining what the bounds of a variable are. But it turns out that if you can take a problem and formulate it in these terms, formulate the problem as your goal is to minimize a cost function, and you’re minimizing that cost function subject to particular constraints, subjects to equations that are of the form like this of some sequence of variables is less than a bound or is equal to some particular value, then there are a number of algorithms that already exist for solving these sorts of problems. So let’s go ahead and take a look at an example. Here’s an example of a problem that might come up in the world of linear programming. Often, this is going to come up when we’re trying to optimize for something. And we want to be able to do some calculations, and we have constraints on what we’re trying to optimize. And so it might be something like this. In the context of a factory, we have two machines, x1 and x2. x1 costs $50 an hour to run. x2 costs $80 an hour to run. And our goal, what we’re trying to do, our objective, is to minimize the total cost. So that’s what we’d like to do. But we need to do so subject to certain constraints. So there might be a labor constraint that x1 requires five units of labor per hour, x2 requires two units of labor per hour, and we have a total of 20 units of labor that we have to spend. So this is a constraint. We have no more than 20 units of labor that we can spend, and we have to spend it across x1 and x2, each of which requires a different amount of labor. And we might also have a constraint like this that tells us x1 is going to produce 10 units of output per hour, x2 is going to produce 12 units of output per hour, and the company needs 90 units of output. So we have some goal, something we need to achieve. We need to achieve 90 units of output, but there are some constraints that x1 can only produce 10 units of output per hour, x2 produces 12 units of output per hour. These types of problems come up quite frequently, and you can start to notice patterns in these types of problems, problems where I am trying to optimize for some goal, minimizing cost, maximizing output, maximizing profits, or something like that. And there are constraints that are placed on that process. And so now we just need to formulate this problem in terms of linear equations. So let’s start with this first point. Two machines, x1 and x2, x costs $50 an hour, x2 costs $80 an hour. Here we can come up with an objective function that might look like this. This is our cost function, rather. 50 times x1 plus 80 times x2, where x1 is going to be a variable representing how many hours do we run machine x1 for, x2 is going to be a variable representing how many hours are we running machine x2 for. And what we’re trying to minimize is this cost function, which is just how much it costs to run each of these machines per hour summed up. This is an example of a linear equation, just some combination of these variables plus coefficients that are placed in front of them. And I would like to minimize that total value. But I need to do so subject to these constraints. x1 requires 50 units of labor per hour, x2 requires 2, and we have a total of 20 units of labor to spend. And so that gives us a constraint of this form. 5 times x1 plus 2 times x2 is less than or equal to 20. 20 is the total number of units of labor we have to spend. And that’s spent across x1 and x2, each of which requires a different number of units of labor per hour, for example. And finally, we have this constraint here. x1 produces 10 units of output per hour, x2 produces 12, and we need 90 units of output. And so this might look something like this. That 10×1 plus 12×2, this is amount of output per hour, it needs to be at least 90. We can do better or great, but it needs to be at least 90. And if you recall from my formulation before, I said that generally speaking in linear programming, we deal with equals constraints or less than or equal to constraints. So we have a greater than or equal to sign here. That’s not a problem. Whenever we have a greater than or equal to sign, we can just multiply the equation by negative 1, and that’ll flip it around to a less than or equals negative 90, for example, instead of a greater than or equal to 90. And that’s going to be an equivalent expression that we can use to represent this problem. So now that we have this cost function and these constraints that it’s subject to, it turns out there are a number of algorithms that can be used in order to solve these types of problems. And these problems go a little bit more into geometry and linear algebra than we’re really going to get into. But the most popular of these types of algorithms are simplex, which was one of the first algorithms discovered for trying to solve linear programs. And later on, a class of interior point algorithms can be used to solve this type of problem as well. The key is not to understand exactly how these algorithms work, but to realize that these algorithms exist for efficiently finding solutions any time we have a problem of this particular form. And so we can take a look, for example, at the production directory here, where here I have a file called production.py, where here I’m using scipy, which was the library for a lot of science-related functions within Python. And I can go ahead and just run this optimization function in order to run a linear program. .linprog here is going to try and solve this linear program for me, where I provide to this expression, to this function call, all of the data about my linear program. So it needs to be in a particular format, which might be a little confusing at first. But this first argument to scipy.optimize.linprogramming is the cost function, which is in this case just an array or a list that has 50 and 80, because my original cost function was 50 times x1 plus 80 times x2. So I just tell Python, 50 and 80, those are the coefficients that I am now trying to optimize for. And then I provide all of the constraints. So the constraints, and I wrote them up above in comments, is the constraint 1 is 5×1 plus 2×2 is less than or equal to 20. And constraint 2 is negative 10×1 plus negative 12×2 is less than or equal to negative 90. And so scipy expects these constraints to be in a particular format. It first expects me to provide all of the coefficients for the upper bound equations, ub just for upper bound, where the coefficients of the first equation are 5 and 2, because we have 5×1 and 2×2. And the coefficients for the second equation are negative 10 and negative 12, because I have negative 10×1 plus negative 12×2. And then here, we provide it as a separate argument, just to keep things separate, what the actual bound is. What is the upper bound for each of these constraints? Well, for the first constraint, the upper bound is 20. That was constraint number 1. And then for constraint number 2, the upper bound is 90. So a bit of a cryptic way of representing it. It’s not quite as simple as just writing the mathematical equations. What really is being expected here are all of the coefficients and all of the numbers that are in these equations by first providing the coefficients for the cost function, then providing all the coefficients for the inequality constraints, and then providing all of the upper bounds for those inequality constraints. And once all of that information is there, then we can run any of these interior point algorithms or the simplex algorithm. Even if you don’t understand how it works, you can just run the function and figure out what the result should be. And here, I said if the result is a success, we were able to solve this problem. Go ahead and print out what the value of x1 and x2 should be. Otherwise, go ahead and print out no solution. And so if I run this program by running python production.py, it takes a second to calculate. But then we see here is what the optimal solution should be. x1 should run for 1.5 hours. x2 should run for 6.25 hours. And we were able to do this by just formulating the problem as a linear equation that we were trying to optimize, some cost that we were trying to minimize, and then some constraints that were placed on that. And many, many problems fall into this category of problems that you can solve if you can just figure out how to use equations and use these constraints to represent that general idea. And that’s a theme that’s going to come up a couple of times today, where we want to be able to take some problem and reduce it down to some problem we know how to solve in order to begin to find a solution and to use existing methods that we can use in order to find a solution more effectively or more efficiently. And it turns out that these types of problems, where we have constraints, show up in other ways too. And there’s an entire class of problems that’s more generally just known as constraint satisfaction problems. And we’re going to now take a look at how you might formulate a constraint satisfaction problem and how you might go about solving a constraint satisfaction problem. But the basic idea of a constraint satisfaction problem is we have some number of variables that need to take on some values. And we need to figure out what values each of those variables should take on. But those variables are subject to particular constraints that are going to limit what values those variables can actually take on. So let’s take a look at a real world example, for example. Let’s look at exam scheduling, that I have four students here, students 1, 2, 3, and 4. Each of them is taking some number of different classes. Classes here are going to be represented by letters. So student 1 is enrolled in courses A, B, and C. Student 2 is enrolled in courses B, D, and E, so on and so forth. And now, say university, for example, is trying to schedule exams for all of these courses. But there are only three exam slots on Monday, Tuesday, and Wednesday. And we have to schedule an exam for each of these courses. But the constraint now, the constraint we have to deal with with the scheduling, is that we don’t want anyone to have to take two exams on the same day. We would like to try and minimize that or eliminate it if at all possible. So how do we begin to represent this idea? How do we structure this in a way that a computer with an AI algorithm can begin to try and solve the problem? Well, let’s in particular just look at these classes that we might take and represent each of the courses as some node inside of a graph. And what we’ll do is we’ll create an edge between two nodes in this graph if there is a constraint between those two nodes. So what does this mean? Well, we can start with student 1, who’s enrolled in courses A, B, and C. What that means is that A and B can’t have an exam at the same time. A and C can’t have an exam at the same time. And B and C also can’t have an exam at the same time. And I can represent that in this graph by just drawing edges. One edge between A and B, one between B and C, and then one between C and A. And that encodes now the idea that between those nodes, there is a constraint. And in particular, the constraint happens to be that these two can’t be equal to each other, though there are other types of constraints that are possible, depending on the type of problem that you’re trying to solve. And then we can do the same thing for each of the other students. So for student 2, who’s enrolled in courses B, D, and E, well, that means B, D, and E, those all need to have edges that connect each other as well. Student 3 is enrolled in courses C, E, and F. So we’ll go ahead and take C, E, and F and connect those by drawing edges between them too. And then finally, student 4 is enrolled in courses E, F, and G. And we can represent that by drawing edges between E, F, and G, although E and F already had an edge between them. We don’t need another one, because this constraint is just encoding the idea that course E and course F cannot have an exam on the same day. So this then is what we might call the constraint graph. There’s some graphical representation of all of my variables, so to speak, and the constraints between those possible variables. Where in this particular case, each of the constraints represents an inequality constraint, that an edge between B and D means whatever value the variable B takes on cannot be the value that the variable D takes on as well. So what then actually is a constraint satisfaction problem? Well, a constraint satisfaction problem is just some set of variables, x1 all the way through xn, some set of domains for each of those variables. So every variable needs to take on some values. Maybe every variable has the same domain, but maybe each variable has a slightly different domain. And then there’s a set of constraints, and we’ll just call a set C, that is some constraints that are placed upon these variables, like x1 is not equal to x2. But there could be other forms too, like maybe x1 equals x2 plus 1 if these variables are taking on numerical values in their domain, for example. The types of constraints are going to vary based on the types of problems. And constraint satisfaction shows up all over the place as well, in any situation where we have variables that are subject to particular constraints. So one popular game is Sudoku, for example, this 9 by 9 grid where you need to fill in numbers in each of these cells, but you want to make sure there’s never a duplicate number in any row, or in any column, or in any grid of 3 by 3 cells, for example. So what might this look like as a constraint satisfaction problem? Well, my variables are all of the empty squares in the puzzle. So represented here is just like an x comma y coordinate, for example, as all of the squares where I need to plug in a value, where I don’t know what value it should take on. The domain is just going to be all of the numbers from 1 through 9, any value that I could fill in to one of these cells. So that is going to be the domain for each of these variables. And then the constraints are going to be of the form, like this cell can’t be equal to this cell, can’t be equal to this cell, can’t be, and all of these need to be different, for example, and same for all of the rows, and the columns, and the 3 by 3 squares as well. So those constraints are going to enforce what values are actually allowed. And we can formulate the same idea in the case of this exam scheduling problem, where the variables we have are the different courses, a up through g. The domain for each of these variables is going to be Monday, Tuesday, and Wednesday. Those are the possible values each of the variables can take on, that in this case just represent when is the exam for that class. And then the constraints are of this form, a is not equal to b, a is not equal to c, meaning a and b can’t have an exam on the same day, a and c can’t have an exam on the same day. Or more formally, these two variables cannot take on the same value within their domain. So that then is this formulation of a constraint satisfaction problem that we can begin to use to try and solve this problem. And constraints can come in a number of different forms. There are hard constraints, which are constraints that must be satisfied for a correct solution. So something like in the Sudoku puzzle, you cannot have this cell and this cell that are in the same row take on the same value. That is a hard constraint. But problems can also have soft constraints, where these are constraints that express some notion of preference, that maybe a and b can’t have an exam on the same day, but maybe someone has a preference that a’s exam is earlier than b’s exam. It doesn’t need to be the case with some expression that some solution is better than another solution. And in that case, you might formulate the problem as trying to optimize for maximizing people’s preferences. You want people’s preferences to be satisfied as much as possible. In this case, though, we’ll mostly just deal with hard constraints, constraints that must be met in order to have a correct solution to the problem. So we want to figure out some assignment of these variables to their particular values that is ultimately going to give us a solution to the problem by allowing us to assign some day to each of the classes such that we don’t have any conflicts between classes. So it turns out that we can classify the constraints in a constraint satisfaction problem into a number of different categories. The first of those categories are perhaps the simplest of the types of constraints, which are known as unary constraints, where unary constraint is a constraint that just involves a single variable. For example, a unary constraint might be something like, a does not equal Monday, meaning Course A cannot have its exam on Monday. If for some reason the instructor for the course isn’t available on Monday, you might have a constraint in your problem that looks like this, something that just has a single variable a in it, and maybe says a is not equal to Monday, or a is equal to something, or in the case of numbers greater than or less than something, a constraint that just has one variable, we consider to be a unary constraint. And this is in contrast to something like a binary constraint, which is a constraint that involves two variables, for example. So this would be a constraint like the ones we were looking at before. Something like a does not equal b is an example of a binary constraint, because it is a constraint that has two variables involved in it, a and b. And we represented that using some arc or some edge that connects variable a to variable b. And using this knowledge of, OK, what is a unary constraint? What is a binary constraint? There are different types of things we can say about a particular constraint satisfaction problem. And one thing we can say is we can try and make the problem node consistent. So what does node consistency mean? Node consistency means that we have all of the values in a variable’s domain satisfying that variable’s unary constraints. So for each of the variables inside of our constraint satisfaction problem, if all of the values satisfy the unary constraints for that particular variable, we can say that the entire problem is node consistent, or we can even say that a particular variable is node consistent if we just want to make one node consistent within itself. So what does that actually look like? Let’s look at now a simplified example, where instead of having a whole bunch of different classes, we just have two classes, a and b, each of which has an exam on either Monday or Tuesday or Wednesday. So this is the domain for the variable a, and this is the domain for the variable b. And now let’s imagine we have these constraints, a not equal to Monday, b not equal to Tuesday, b not equal to Monday, a not equal to b. So those are the constraints that we have on this particular problem. And what we can now try to do is enforce node consistency. And node consistency just means we make sure that all of the values for any variable’s domain satisfy its unary constraints. And so we could start by trying to make node a node consistent. Is it consistent? Does every value inside of a’s domain satisfy its unary constraints? Well, initially, we’ll see that Monday does not satisfy a’s unary constraints, because we have a constraint, a unary constraint here, that a is not equal to Monday. But Monday is still in a’s domain. And so this is something that is not node consistent, because we have Monday in the domain. But this is not a valid value for this particular node. And so how do we make this node consistent? Well, to make the node consistent, what we’ll do is we’ll just go ahead and remove Monday from a’s domain. Now a can only be on Tuesday or Wednesday, because we had this constraint that said a is not equal to Monday. And at this point now, a is node consistent. For each of the values that a can take on, Tuesday and Wednesday, there is no constraint that is a unary constraint that conflicts with that idea. There is no constraint that says that a can’t be Tuesday. There is no unary constraint that says that a cannot be on Wednesday. And so now we can turn our attention to b. b also has a domain, Monday, Tuesday, and Wednesday. And we can begin to see whether those variables satisfy the unary constraints as well. Well, here is a unary constraint, b is not equal to Tuesday. And that does not appear to be satisfied by this domain of Monday, Tuesday, and Wednesday, because Tuesday, this possible value that the variable b could take on is not consistent with this unary constraint, that b is not equal to Tuesday. So to solve that problem, we’ll go ahead and remove Tuesday from b’s domain. Now b’s domain only contains Monday and Wednesday. But as it turns out, there’s yet another unary constraint that we placed on the variable b, which is here. b is not equal to Monday. And that means that this value, Monday, inside of b’s domain, is not consistent with b’s unary constraints, because we have a constraint that says the b cannot be Monday. And so we can remove Monday from b’s domain. And now we’ve made it through all of the unary constraints. We’ve not yet considered this constraint, which is a binary constraint. But we’ve considered all of the unary constraints, all of the constraints that involve just a single variable. And we’ve made sure that every node is consistent with those unary constraints. So we can say that now we have enforced node consistency, that for each of these possible nodes, we can pick any of these values in the domain. And there won’t be a unary constraint that is violated as a result of it. So node consistency is fairly easy to enforce. We just take each node, make sure the values in the domain satisfy the unary constraints. Where things get a little bit more interesting is when we consider different types of consistency, something like arc consistency, for example. And arc consistency refers to when all of the values in a variable’s domain satisfy the variable’s binary constraints. So when we’re looking at trying to make a arc consistent, we’re no longer just considering the unary constraints that involve a. We’re trying to consider all of the binary constraints that involve a as well. So any edge that connects a to another variable inside of that constraint graph that we were taking a look at before. Put a little bit more formally, arc consistency. And arc really is just another word for an edge that connects two of these nodes inside of our constraint graph. We can define arc consistency a little more precisely like this. In order to make some variable x arc consistent with respect to some other variable y, we need to remove any element from x’s domain to make sure that every choice for x, every choice in x’s domain, has a possible choice for y. So put another way, if I have a variable x and I want to make x an arc consistent, then I’m going to look at all of the possible values that x can take on and make sure that for all of those possible values, there is still some choice that I can make for y, if there’s some arc between x and y, to make sure that y has a possible option that I can choose as well. So let’s look at an example of that going back to this example from before. We enforced node consistency already by saying that a can only be on Tuesday or Wednesday because we knew that a could not be on Monday. And we also said that b’s only domain only consists of Wednesday because we know that b does not equal Tuesday and also b does not equal Monday. So now let’s begin to consider arc consistency. Let’s try and make a arc consistent with b. And what that means is to make a arc consistent with respect to b means that for any choice we make in a’s domain, there is some choice we can make in b’s domain that is going to be consistent. And we can try that. For a, we can choose Tuesday as a possible value for a. If I choose Tuesday for a, is there a value for b that satisfies the binary constraint? Well, yes, b Wednesday would satisfy this constraint that a does not equal b because Tuesday does not equal Wednesday. However, if we chose Wednesday for a, well, then there is no choice in b’s domain that satisfies this binary constraint. There is no way I can choose something for b that satisfies a does not equal b because I know b must be Wednesday. And so if ever I run into a situation like this where I see that here is a possible value for a such that there is no choice of value for b that satisfies the binary constraint, well, then this is not arc consistent. And to make it arc consistent, I would need to take Wednesday and remove it from a’s domain. Because Wednesday was not going to be a possible choice I can make for a because it wasn’t consistent with this binary constraint for b. There was no way I could choose Wednesday for a and still have an available solution by choosing something for b as well. So here now, I’ve been able to enforce arc consistency. And in doing so, I’ve actually solved this entire problem, that given these constraints where a and b can have exams on either Monday or Tuesday or Wednesday, the only solution, as it would appear, is that a’s exam must be on Tuesday and b’s exam must be on Wednesday. And that is the only option available to me. So if we want to apply our consistency to a larger graph, not just looking at one particular pair of our consistency, there are ways we can do that too. And we can begin to formalize what the pseudocode would look like for trying to write an algorithm that enforces arc consistency. And we’ll start by defining a function called revise. Revise is going to take as input a CSP, otherwise known as a constraint satisfaction problem, and also two variables, x and y. And what revise is going to do is it is going to make x arc consistent with respect to y, meaning remove anything from x’s domain that doesn’t allow for a possible option for y. How does this work? Well, we’ll go ahead and first keep track of whether or not we’ve made a revision. Revise is ultimately going to return true or false. It’ll return true in the event that we did make a revision to x’s domain. It’ll return false if we didn’t make any change to x’s domain. And we’ll see in a moment why that’s going to be helpful. But we start by saying revised equals false. We haven’t made any changes. Then we’ll say, all right, let’s go ahead and loop over all of the possible values in x’s domain. So loop over x’s domain for each little x in x’s domain. I want to make sure that for each of those choices, I have some available choice in y that satisfies the binary constraints that are defined inside of my CSP, inside of my constraint satisfaction problem. So if ever it’s the case that there is no value y in y’s domain that satisfies the constraint for x and y, well, if that’s the case, that means that this value x shouldn’t be in x’s domain. So we’ll go ahead and delete x from x’s domain. And I’ll set revised equal to true because I did change x’s domain. I changed x’s domain by removing little x. And I removed little x because it wasn’t art consistent. There was no way I could choose a value for y that would satisfy this xy constraint. So in this case, we’ll go ahead and set revised equal true. And we’ll do this again and again for every value in x’s domain. Sometimes it might be fine. In other cases, it might not allow for a possible choice for y, in which case we need to remove this value from x’s domain. And at the end, we just return revised to indicate whether or not we actually made a change. So this function, then, this revised function is effectively an implementation of what you saw me do graphically a moment ago. And it makes one variable, x, arc consistent with another variable, in this case, y. But generally speaking, when we want to enforce our consistency, we’ll often want to enforce our consistency not just for a single arc, but for the entire constraint satisfaction problem. And it turns out there’s an algorithm to do that as well. And that algorithm is known as AC3. AC3 takes a constraint satisfaction problem. And it enforces our consistency across the entire problem. How does it do that? Well, it’s going to basically maintain a queue or basically just a line of all of the arcs that it needs to make consistent. And over time, we might remove things from that queue as we begin dealing with our consistency. And we might need to add things to that queue as well if there are more things we need to make arc consistent. So we’ll go ahead and start with a queue that contains all of the arcs in the constraint satisfaction problem, all of the edges that connect two nodes that have some sort of binary constraint between them. And now, as long as the queue is non-empty, there is work to be done. The queue is all of the things that we need to make arc consistent. So as long as the queue is non-empty, there’s still things we have to do. What do we have to do? Well, we’ll start by de-queuing from the queue, remove something from the queue. And strictly speaking, it doesn’t need to be a queue, but a queue is a traditional way of doing this. We’ll de-queue from the queue, and that’ll give us an arc, x and y, these two variables where I would like to make x arc consistent with y. So how do we make x arc consistent with y? Well, we can go ahead and just use that revise function that we talked about a moment ago. We called the revise function, passing as input the constraint satisfaction problem, and also these variables x and y, because I want to make x arc consistent with y. In other words, remove any values from x’s domain that don’t leave an available option for y. And recall, what does revised return? Well, it returns true if we actually made a change, if we removed something from x’s domain, because there wasn’t an available option for y, for example. And it returns false if we didn’t make any change to x’s domain at all. And it turns out if revised returns false, if we didn’t make any changes, well, then there’s not a whole lot more work to be done here for this arc. We can just move ahead to the next arc that’s in the queue. But if we did make a change, if we did reduce x’s domain by removing values from x’s domain, well, then what we might realize is that this creates potential problems later on, that it might mean that some arc that was arc consistent with x, that node might no longer be arc consistent with x, because while there used to be an option that we could choose for x, now there might not be, because now we might have removed something from x that was necessary for some other arc to be arc consistent. And so if ever we did revise x’s domain, we’re going to need to add some things to the queue, some additional arcs that we might want to check. How do we do that? Well, first thing we want to check is to make sure that x’s domain is not 0. If x’s domain is 0, that means there are no available options for x at all. And that means that there’s no way you can solve the constraint satisfaction problem. If we’ve removed everything from x’s domain, we’ll go ahead and just return false here to indicate there’s no way to solve the problem, because there’s nothing left in x’s domain. But otherwise, if there are things left in x’s domain, but fewer things than before, well, then what we’ll do is we’ll loop over each variable z that is in all of x’s neighbors, except for y, y we already handled. But we’ll consider all of x’s other’s neighbors and ask ourselves, all right, will that arc from each of those z’s to x, that arc might no longer be arc consistent, because while for each z, there might have been a possible option we could choose for x to correspond with each of z’s possible values, now there might not be, because we removed some elements from x’s domain. And so what we’ll do here is we’ll go ahead and enqueue, adding something to the queue, this arc zx for all of those neighbors z. So we need to add back some arcs to the queue in order to continue to enforce arc consistency. At the very end, if we make it through all this process, then we can return true. But this now is AC3, this algorithm for enforcing arc consistency on a constraint satisfaction problem. And the big idea is really just keep track of all of the arcs that we might need to make arc consistent, make it arc consistent by calling the revise function. And if we did revise it, then there are some new arcs that might need to be added to the queue in order to make sure that everything is still arc consistent, even after we’ve removed some of the elements from a particular variable’s domain. So what then would happen if we tried to enforce arc consistency on a graph like this, on a graph where each of these variables has a domain of Monday, Tuesday, and Wednesday? Well, it turns out that by enforcing arc consistency on this graph, well, it can solve some types of problems. Nothing actually changes here. For any particular arc, just considering two variables, there’s always a way for me to just, for any of the choices I make for one of them, make a choice for the other one, because there are three options, and I just need the two to be different from each other. So this is actually quite easy to just take an arc and just declare that it is arc consistent, because if I pick Monday for D, then I just pick something that isn’t Monday for B. In arc consistency, we only consider consistency between a binary constraint between two nodes, and we’re not really considering all of the rest of the nodes yet. So just using AC3, the enforcement of arc consistency, that can sometimes have the effect of reducing domains to make it easier to find solutions, but it will not always actually solve the problem. We might still need to somehow search to try and find a solution. And we can use classical traditional search algorithms to try to do so. You’ll recall that a search problem generally consists of these parts. We have some initial state, some actions, a transition model that takes me from one state to another state, a goal test to tell me have I satisfied my objective correctly, and then some path cost function, because in the case of like maze solving, I was trying to get to my goal as quickly as possible. So you could formulate a CSP, or a constraint satisfaction problem, as one of these types of search problems. The initial state will just be an empty assignment, where an assignment is just a way for me to assign any particular variable to any particular value. So if an empty assignment is no variables that are assigned to any values yet, then the action I can take is adding some new variable equals value pair to that assignment, saying for this assignment, let me add a new value for this variable. And the transition model just defines what happens when you take that action. You get a new assignment that has that variable equal to that value inside of it. The goal test is just checking to make sure all the variables have been assigned and making sure all the constraints have been satisfied. And the path cost function is sort of irrelevant. I don’t really care about what the path really is. I just care about finding some assignment that actually satisfies all of the constraints. So really, all the paths have the same cost. I don’t really care about the path to the goal. I just care about the solution itself, much as we’ve talked about now before. The problem here, though, is that if we just implement this naive search algorithm just by implementing like breadth-first search or depth-first search, this is going to be very, very inefficient. And there are ways we can take advantage of efficiencies in the structure of a constraint satisfaction problem itself. And one of the key ideas is that we can really just order these variables. And it doesn’t matter what order we assign variables in. The assignment a equals 2 and then b equals 8 is identical to the assignment of b equals 8 and then a equals 2. Switching the order doesn’t really change anything about the fundamental nature of that assignment. And so there are some ways that we can try and revise this idea of a search algorithm to apply it specifically for a problem like a constraint satisfaction problem. And it turns out the search algorithm we’ll generally use when talking about constraint satisfaction problems is something known as backtracking search. And the big idea of backtracking search is we’ll go ahead and make assignments from variables to values. And if ever we get stuck, we arrive at a place where there is no way we can make any forward progress while still preserving the constraints that we need to enforce, we’ll go ahead and backtrack and try something else instead. So the very basic sketch of what backtracking search looks like is it looks like this. Function called backtrack that takes as input an assignment and a constraint satisfaction problem. So initially, we don’t have any assigned variables. So when we begin backtracking search, this assignment is just going to be the empty assignment with no variables inside of it. But we’ll see later this is going to be a recursive function. So backtrack takes as input the assignment and the problem. If the assignment is complete, meaning all of the variables have been assigned, we just return that assignment. That, of course, won’t be true initially, because we start with an empty assignment. But over time, we might add things to that assignment. So if ever the assignment actually is complete, then we’re done. Then just go ahead and return that assignment. But otherwise, there is some work to be done. So what we’ll need to do is select an unassigned variable for this particular problem. So we need to take the problem, look at the variables that have already been assigned, and pick a variable that has not yet been assigned. And I’ll go ahead and take that variable. And then I need to consider all of the values in that variable’s domain. So we’ll go ahead and call this domain values function. We’ll talk a little more about that later, that takes a variable and just gives me back an ordered list of all of the values in its domain. So I’ve taken a random unselected variable. I’m going to loop over all of the possible values. And the idea is, let me just try all of these values as possible values for the variable. So if the value is consistent with the assignment so far, it doesn’t violate any of the constraints, well then let’s go ahead and add variable equals value to the assignment because it’s so far consistent. And now let’s recursively call backtrack to try and make the rest of the assignments also consistent. So I’ll go ahead and call backtrack on this new assignment that I’ve added the variable equals value to. And now I recursively call backtrack and see what the result is. And if the result isn’t a failure, well then let me just return that result. And otherwise, what else could happen? Well, if it turns out the result was a failure, well then that means this value was probably a bad choice for this particular variable because when I assigned this variable equal to that value, eventually down the road I ran into a situation where I violated constraints. There was nothing more I could do. So now I’ll remove variable equals value from the assignment, effectively backtracking to say, all right, that value didn’t work. Let’s try another value instead. And then at the very end, if we were never able to return a complete assignment, we’ll just go ahead and return failure because that means that none of the values worked for this particular variable. This now is the idea for backtracking search, to take each of the variables, try values for them, and recursively try backtracking search, see if we can make progress. And if ever we run into a dead end, we run into a situation where there is no possible value we can choose that satisfies the constraints, we return failure. And that propagates up, and eventually we make a different choice by going back and trying something else instead. So let’s put this algorithm into practice. Let’s actually try and use backtracking search to solve this problem now, where I need to figure out how to assign each of these courses to an exam slot on Monday or Tuesday or Wednesday in such a way that it satisfies these constraints, that each of these edges mean those two classes cannot have an exam on the same day. So I can start by just starting at a node. It doesn’t really matter which I start with, but in this case, I’ll just start with A. And I’ll ask the question, all right, let me loop over the values in the domain. And maybe in this case, I’ll just start with Monday and say, all right, let’s go ahead and assign A to Monday. We’ll just go and order Monday, Tuesday, Wednesday. And now let’s consider node B. So I’ve made an assignment to A, so I recursively call backtrack with this new part of the assignment. And now I’m looking to pick another unassigned variable like B. And I’ll say, all right, maybe I’ll start with Monday, because that’s the very first value in B’s domain. And I ask, all right, does Monday violate any constraints? And it turns out, yes, it does. It violates this constraint here between A and B, because A and B are now both on Monday, and that doesn’t work, because B can’t be on the same day as A. So that doesn’t work. So we might instead try Tuesday, try the next value in B’s domain. And is that consistent with the assignment so far? Well, yeah, B, Tuesday, A, Monday, that is consistent so far, because they’re not on the same day. So that’s good. Now we can recursively call backtrack. Try again. Pick another unassigned variable, something like D, and say, all right, let’s go through its possible values. Is Monday consistent with this assignment? Well, yes, it is. B and D are on different days, Monday versus Tuesday. And A and B are also on different days, Monday versus Tuesday. So that’s fine so far, too. We’ll go ahead and try again. Maybe we’ll go to this variable here, E. Say, can we make that consistent? Let’s go through the possible values. We’ve recursively called backtrack. We might start with Monday and say, all right, that’s not consistent, because D and E now have exams on the same day. So we might try Tuesday instead, going to the next one. Ask, is that consistent? Well, no, it’s not, because B and E, those have exams on the same day. And so we try, all right, is Wednesday consistent? And in turn, it’s like, all right, yes, it is. Wednesday is consistent, because D and E now have exams on different days. B and E now have exams on different days. All seems to be well so far. I recursively call backtrack, select another unassigned variable, we’ll say maybe choose C this time, and say, all right, let’s try the values that C could take on. Let’s start with Monday. And it turns out that’s not consistent, because now A and C both have exams on the same day. So I try Tuesday and say, that’s not consistent either, because B and C now have exams on the same day. And then I say, all right, let’s go ahead and try Wednesday. But that’s not consistent either, because C and E each have exams on the same day too. So now we’ve gone through all the possible values for C, Monday, Tuesday, and Wednesday. And none of them are consistent. There is no way we can have a consistent assignment. Backtrack, in this case, will return a failure. And so then we’d say, all right, we have to backtrack back to here. Well, now for E, we’ve tried all of Monday, Tuesday, and Wednesday. And none of those work, because Wednesday, which seemed to work, turned out to be a failure. So that means there’s no possible way we can assign E. So that’s a failure too. We have to go back up to D, which means that Monday assignment to D, that must be wrong. We must try something else. So we can try, all right, what if instead of Monday, we try Tuesday? Tuesday, it turns out, is not consistent, because B and D now have an exam on the same day. But Wednesday, as it turns out, works. And now we can begin to mix and forward progress again. We go back to E and say, all right, which of these values works? Monday turns out to work by not violating any constraints. Then we go up to C now. Monday doesn’t work, because it violates a constraint. Violates two, actually. Tuesday doesn’t work, because it violates a constraint as well. But Wednesday does work. Then we can go to the next variable, F, and say, all right, does Monday work? We’ll know. It violates a constraint. But Tuesday does work. And then finally, we can look at the last variable, G, recursively calling backtrack one more time. Monday is inconsistent. That violates a constraint. Tuesday also violates a constraint. But Wednesday, that doesn’t violate a constraint. And so now at this point, we recursively call backtrack one last time. We now have a satisfactory assignment of all of the variables. And at this point, we can say that we are now done. We have now been able to successfully assign a variable or a value to each one of these variables in such a way that we’re not violating any constraints. We’re going to go ahead and have classes A and E have their exams on Monday. Classes B and F can have their exams on Tuesday. And classes C, D, and G can have their exams on Wednesday. And there’s no violated constraints that might come up there. So that then was a graphical look at how this might work. Let’s now take a look at some code we could use to actually try and solve this problem as well. So here I’ll go ahead and go into the scheduling directory. We’re here now. We’ll start by looking at schedule0.py. We’re here. I define a list of variables, A, B, C, D, E, F, G. Those are all different classes. Then underneath that, I define my list of constraints. So constraint A and B. That is a constraint because they can’t be on the same day. Likewise, A and C, B and C, so on and so forth, enforcing those exact same constraints. And here then is what the backtracking function might look like. First, if the assignment is complete, if I’ve made an assignment of every variable to a value, go ahead and just return that assignment. Then we’ll select an unassigned variable from that assignment. Then for each of the possible values in the domain, Monday, Tuesday, Wednesday, let’s go ahead and create a new assignment that assigns the variable to that value. I’ll call this consistent function, which I’ll show you in a moment, that just checks to make sure this new assignment is consistent. But if it is consistent, we’ll go ahead and call backtrack to go ahead and continue trying to run backtracking search. And as long as the result is not none, meaning it wasn’t a failure, we can go ahead and return that result. But if we make it through all the values and nothing works, then it is a failure. There’s no solution. We go ahead and return none here. What do these functions do? Select unassigned variable is just going to choose a variable not yet assigned. So it’s going to loop over all the variables. And if it’s not already assigned, we’ll go ahead and just return that variable. And what does the consistent function do? Well, the consistent function goes through all the constraints. And if we have a situation where we’ve assigned both of those values to variables, but they are the same, well, then that is a violation of the constraint, in which case we’ll return false. But if nothing is inconsistent, then the assignment is consistent and will return true. And then all the program does is it calls backtrack on an empty assignment, an empty dictionary that has no variable assigned and no values yet, save that inside a solution, and then print out that solution. So by running this now, I can run Python schedule0.py. And what I get as a result of that is an assignment of all these variables to values. And it turns out we assign a to Monday as we would expect, b to Tuesday, c to Wednesday, exactly the same type of thing we were talking about before, an assignment of each of these variables to values that doesn’t violate any constraints. And I had to do a fair amount of work in order to implement this idea myself. I had to write the backtrack function that went ahead and went through this process of recursively trying to do this backtracking search. But it turns out the constraint satisfaction problems are so popular that there exist many libraries that already implement this type of idea. Again, as with before, the specific library is not as important as the fact that libraries do exist. This is just one example of a Python constraint library, where now, rather than having to do all the work from scratch inside of schedule1.py, I’m just taking advantage of a library that implements a lot of these ideas already. So here, I create a new problem, add variables to it with particular domains. I add a whole bunch of these individual constraints, where I call addConstraint and pass in a function describing what the constraint is. And the constraint basically says the function that takes two variables, x and y, and makes sure that x is not equal to y, enforcing the idea that these two classes cannot have exams on the same day. And then, for any constraint satisfaction problem, I can call getSolutions to get all the solutions to that problem. And then, for each of those solutions, print out what that solution happens to be. And if I run python schedule1.py, and now see, there are actually a number of different solutions that can be used to solve the problem. There are, in fact, six different solutions, assignments of variables to values that will give me a satisfactory answer to this constraint satisfaction problem. So this then was an implementation of a very basic backtracking search method, where really we just went through each of the variables, picked one that wasn’t assigned, tried the possible values the variable could take on. And then, if it worked, if it didn’t violate any constraints, then we kept trying other variables. And if ever we hit a dead end, we had to backtrack. But ultimately, we might be able to be a little bit more intelligent about how we do this in order to improve the efficiency of how we solve these sorts of problems. And one thing we might imagine trying to do is going back to this idea of inference, using the knowledge we know to be able to draw conclusions in order to make the rest of the problem solving process a little bit easier. And let’s now go back to where we got stuck in this problem the first time. When we were solving this constraint satisfaction problem, we dealt with B. And then we went on to D. And we went ahead and just assigned D to Monday, because that seemed to work with the assignment so far. It didn’t violate any constraints. But it turned out that later on that choice turned out to be a bad one, that that choice wasn’t consistent with the rest of the values that we could take on here. And the question is, is there anything we could do to avoid getting into a situation like this, avoid trying to go down a path that’s ultimately not going to lead anywhere by taking advantage of knowledge that we have initially? And it turns out we do have that kind of knowledge. We can look at just the structure of this graph so far. And we can say that right now C’s domain, for example, contains values Monday, Tuesday, and Wednesday. And based on those values, we can say that this graph is not arc consistent. Recall that arc consistency is all about making sure that for every possible value for a particular node, that there is some other value that we are able to choose. And as we can see here, Monday and Tuesday are not going to be possible values that we can choose for C. They’re not going to be consistent with a node like B, for example, because B is equal to Tuesday, which means that C cannot be Tuesday. And because A is equal to Monday, C also cannot be Monday. So using that information, by making C arc consistent with A and B, we could remove Monday and Tuesday from C’s domain and just leave C with Wednesday, for example. And if we continued to try and enforce arc consistency, we’d see there are some other conclusions we can draw as well. We see that B’s only option is Tuesday and C’s only option is Wednesday. And so if we want to make E arc consistent, well, E can’t be Tuesday, because that wouldn’t be arc consistent with B. And E can’t be Wednesday, because that wouldn’t be arc consistent with C. So we can go ahead and say E and just set that equal to Monday, for example. And then we can begin to do this process again and again, that in order to make D arc consistent with B and E, then D would have to be Wednesday. That’s the only possible option. And likewise, we can make the same judgments for F and G as well. And it turns out that without having to do any additional search, just by enforcing arc consistency, we were able to actually figure out what the assignment of all the variables should be without needing to backtrack at all. And the way we did that is by interleaving this search process and the inference step, by this step of trying to enforce arc consistency. And the algorithm to do this is often called just the maintaining arc consistency algorithm, which just enforces arc consistency every time we make a new assignment of a value to an existing variable. So sometimes we can enforce our consistency using that AC3 algorithm at the very beginning of the problem before we even begin searching in order to limit the domain of the variables in order to make it easier to search. But we can also take advantage of the interleaving of enforcing our consistency with search such that every time in the search process we make a new assignment, we go ahead and enforce arc consistency as well to make sure that we’re just eliminating possible values from domains whenever possible. And how do we do this? Well, this is really equivalent to just every time we make a new assignment to a variable x. We’ll go ahead and call our AC3 algorithm, this algorithm that enforces arc consistency on a constraint satisfaction problem. And we go ahead and call that, starting it with a Q, not of all of the arcs, which we did originally, but just of all of the arcs that we want to make arc consistent with x, this thing that we have just made an assignment to. So all arcs yx, where y is a neighbor of x, something that shares a constraint with x, for example. And by maintaining arc consistency in the backtracking search process, we can ultimately make our search process a little bit more efficient. And so this is the revised version of this backtrack function. Same as before, the changes here are highlighted in yellow. Every time we add a new variable equals value to our assignment, we’ll go ahead and run this inference procedure, which might do a number of different things. But one thing it could do is call the maintaining arc consistency algorithm to make sure we’re able to enforce arc consistency on the problem. And we might be able to draw new inferences as a result of that process. Get new guarantees of this variable needs to be equal to that value, for example. That might happen one time. It might happen many times. And so long as those inferences are not a failure, as long as they don’t lead to a situation where there is no possible way to make forward progress, well, then we can go ahead and add those inferences, those new knowledge, that new pieces of knowledge I know about what variables should be assigned to what values, I can add those to the assignment in order to more quickly make forward progress by taking advantage of information that I can just deduce, information I know based on the rest of the structure of the constraint satisfaction problem. And the only other change I’ll need to make now is if it turns out this value doesn’t work, well, then down here, I’ll go ahead and need to remove not only variable equals value, but also any of those inferences that I made, remove that from the assignment as well. So here, then, we’re often able to solve the problem by backtracking less than we might originally have needed to, just by taking advantage of the fact that every time we make a new assignment of one variable to one value, that might reduce the domains of other variables as well. And we can use that information to begin to more quickly draw conclusions in order to try and solve the problem more efficiently as well. And it turns out there are other heuristics we can use to try and improve the efficiency of our search process as well. And it really boils down to a couple of these functions that I’ve talked about, but we haven’t really talked about how they’re working. And one of them is this function here, select unassigned variable, where we’re selecting some variable in the constraint satisfaction problem that has not yet been assigned. So far, I’ve sort of just been selecting variables randomly, just like picking one variable and one unassigned variable in order to decide, all right, this is the variable that we’re going to assign next, and then going from there. But it turns out that by being a little bit intelligent, by following certain heuristics, we might be able to make the search process much more efficient just by choosing very carefully which variable we should explore next. So some of those heuristics include the minimum remaining values, or MRV heuristic, which generally says that if I have a choice between which variable I should select, I should select the variable with the smallest domain, the variable that has the fewest number of remaining values left. With the idea being, if there are only two remaining values left, well, I may as well prune one of them very quickly in order to get to the other, because one of those two has got to be the solution, if a solution does exist. Sometimes minimum remaining values might not give a conclusive result if all the nodes have the same number of remaining values, for example. And in that case, another heuristic that can be helpful to look at is the degree heuristic. The degree of a node is the number of nodes that are attached to that node, the number of nodes that are constrained by that particular node. And if you imagine which variable should I choose, should I choose a variable that has a high degree that is connected to a lot of different things, or a variable with a low degree that is not connected to a lot of different things, well, it can often make sense to choose the variable that has the highest degree that is connected to the most other nodes as the thing you would search first. Why is that the case? Well, it’s because by choosing a variable with a high degree, that is immediately going to constrain the rest of the variables more, and it’s more likely to be able to eliminate large sections of the state space that you don’t need to search through at all. So what could this actually look like? Let’s go back to this search problem here. In this particular case, I’ve made an assignment here. I’ve made an assignment here. And the question is, what should I look at next? And according to the minimum remaining values heuristic, what I should choose is the variable that has the fewest remaining possible values. And in this case, that’s this node here, node C, that only has one variable left in this domain, which in this case is Wednesday, which is a very reasonable choice of a next assignment to make, because I know it’s the only option, for example. I know that the only possible option for C is Wednesday, so I may as well make that assignment and then potentially explore the rest of the space after that. But meanwhile, at the very start of the problem, when I didn’t have any knowledge of what nodes should have what values yet, I still had to pick what node should be the first one that I try and assign a value to. And I arbitrarily just chose the one at the top, node A originally. But we can be more intelligent about that. We can look at this particular graph. All of them have domains of the same size, domain of size 3. So minimum remaining values doesn’t really help us there. But we might notice that node E has the highest degree. It is connected to the most things. And so perhaps it makes sense to begin our search, rather than starting at node A at the very top, start with the node with the highest degree. Start by searching from node E, because from there, that’s going to much more easily allow us to enforce the constraints that are nearby, eliminating large portions of the search space that I might not need to search through. And in fact, by starting with E, we can immediately then assign other variables. And following that, we can actually assign the rest of the variables without needing to do any backtracking at all, even if I’m not using this inference procedure. Just by starting with a node that has a high degree, that is going to very quickly restrict the possible values that other nodes can take on. So that then is how we can go about selecting an unassigned variable in a particular order. Rather than randomly picking a variable, if we’re a little bit intelligent about how we choose it, we can make our search process much, much more efficient by making sure we don’t have to search through portions of the search space that ultimately aren’t going to matter. The other variable we haven’t really talked about, the other function here, is this domain values function. This domain values function that takes a variable and gives me back a sequence of all of the values inside of that variable’s domain. The naive way to approach it is what we did before, which is just go in order, go Monday, then Tuesday, then Wednesday. But the problem is that going in that order might not be the most efficient order to search in, that sometimes it might be more efficient to choose values that are likely to be solutions first and then go to other values. Now, how do you assess whether a value is likelier to lead to a solution or less likely to lead to a solution? Well, one thing you can take a look at is how many constraints get added, how many things get removed from domains as you make this new assignment of a variable to this particular value. And the heuristic we can use here is the least constraining value heuristic, which is the idea that we should return variables in order based on the number of choices that are ruled out for neighboring values. And I want to start with the least constraining value, the value that rules out the fewest possible options. And the idea there is that if all I care about doing is finding a solution, if I start with a value that rules out a lot of other choices, I’m ruling out a lot of possibilities that maybe is going to make it less likely that this particular choice leads to a solution. Whereas on the other hand, if I have a variable and I start by choosing a value that doesn’t rule out very much, well, then I still have a lot of space where there might be a solution that I could ultimately find. And this might seem a little bit counterintuitive and a little bit at odds with what we were talking about before, where I said, when you’re picking a variable, you should pick the variable that is going to have the fewest possible values remaining. But here, I want to pick the value for the variable that is the least constraining. But the general idea is that when I am picking a variable, I would like to prune large portions of the search space by just choosing a variable that is going to allow me to quickly eliminate possible options. Whereas here, within a particular variable, as I’m considering values that that variable could take on, I would like to just find a solution. And so what I want to do is ultimately choose a value that still leaves open the possibility of me finding a solution to be as likely as possible. By not ruling out many options, I leave open the possibility that I can still find a solution without needing to go back later and backtrack. So an example of that might be in this particular situation here, if I’m trying to choose a variable for a value for node C here, that C is equal to either Tuesday or Wednesday. We know it can’t be Monday because it conflicts with this domain here, where we already know that A is Monday, so C must be Tuesday or Wednesday. And the question is, should I try Tuesday first, or should I try Wednesday first? And if I try Tuesday, what gets ruled out? Well, one option gets ruled out here, a second option gets ruled out here, and a third option gets ruled out here. So choosing Tuesday would rule out three possible options. And what about choosing Wednesday? Well, choosing Wednesday would rule out one option here, and it would rule out one option there. And so I have two choices. I can choose Tuesday that rules out three options, or Wednesday that rules out two options. And according to the least constraining value heuristic, what I should probably do is go ahead and choose Wednesday, the one that rules out the fewest number of possible options, leaving open as many chances as possible for me to eventually find the solution inside of the state space. And ultimately, if you continue this process, we will find the solution, an assignment of variables, two values, that allows us to give each of these exams, each of these classes, an exam date that doesn’t conflict with anyone that happens to be enrolled in two classes at the same time. So the big takeaway now with all of this is that there are a number of different ways we can formulate a problem. The ways we’ve looked at today are we can formulate a problem as a local search problem, a problem where we’re looking at a current node and moving to a neighbor based on whether that neighbor is better or worse than the current node that we are looking at. We looked at formulating problems as linear programs, where just by putting things in terms of equations and constraints, we’re able to solve problems a little bit more efficiently. And we saw formulating a problem as a constraint satisfaction problem, creating this graph of all of the constraints that connect two variables that have some constraint between them, and using that information to be able to figure out what the solution should be. And so the takeaway of all of this now is that if we have some problem in artificial intelligence that we would like to use AI to be able to solve them, whether that’s trying to figure out where hospitals should be or trying to solve the traveling salesman problem, trying to optimize productions and costs and whatnot, or trying to figure out how to satisfy certain constraints, whether that’s in a Sudoku puzzle, or whether that’s in trying to figure out how to schedule exams for a university, or any number of a wide variety of types of problems, if we can formulate that problem as one of these sorts of problems, then we can use these known algorithms, these algorithms for enforcing art consistency and backtracking search, these hill climbing and simulated annealing algorithms, these simplex algorithms and interior point algorithms that can be used to solve linear programs, that we can use those techniques to begin to solve a whole wide variety of problems all in this world of optimization inside of artificial intelligence. This was an introduction to artificial intelligence with Python for today. We will see you next time. [” All right. Welcome back, everyone, to an introduction to artificial intelligence with Python. Now, so far in this class, we’ve used AI to solve a number of different problems, giving AI instructions for how to search for a solution, or how to satisfy certain constraints in order to find its way from some input point to some output point in order to solve some sort of problem. Today, we’re going to turn to the world of learning, in particular the idea of machine learning, which generally refers to the idea where we are not going to give the computer explicit instructions for how to perform a task, but rather we are going to give the computer access to information in the form of data, or patterns that it can learn from, and let the computer try and figure out what those patterns are, try and understand that data to be able to perform a task on its own. Now, machine learning comes in a number of different forms, and it’s a very wide field. So today, we’ll explore some of the foundational algorithms and ideas that are behind a lot of the different areas within machine learning. And one of the most popular is the idea of supervised machine learning, or just supervised learning. And supervised learning is a particular type of task. It refers to the task where we give the computer access to a data set, where that data set consists of input-output pairs. And what we would like the computer to do is we would like our AI to be able to figure out some function that maps inputs to outputs. So we have a whole bunch of data that generally consists of some kind of input, some evidence, some information that the computer will have access to. And we would like the computer, based on that input information, to predict what some output is going to be. And we’ll give it some data so that the computer can train its model on and begin to understand how it is that this information works and how it is that the inputs and outputs relate to each other. But ultimately, we hope that our computer will be able to figure out some function that, given those inputs, is able to get those outputs. There are a couple of different tasks within supervised learning. The one we’ll focus on and start with is known as classification. And classification is the problem where, if I give you a whole bunch of inputs, you need to figure out some way to map those inputs into discrete categories, where you can decide what those categories are, and it’s the job of the computer to predict what those categories are going to be. So that might be, for example, I give you information about a bank note, like a US dollar, and I’m asking you to predict for me, does it belong to the category of authentic bank notes, or does it belong to the category of counterfeit bank notes? You need to categorize the input, and we want to train the computer to figure out some function to be able to do that calculation. Another example might be the case of weather, someone we’ve talked about a little bit so far in this class, where we would like to predict on a given day, is it going to rain on that day? Is it going to be cloudy on that day? And before we’ve seen how we could do this, if we really give the computer all the exact probabilities for if these are the conditions, what’s the probability of rain? Oftentimes, we don’t have access to that information, though. But what we do have access to is a whole bunch of data. So if we wanted to be able to predict something like, is it going to rain or is it not going to rain, we would give the computer historical information about days when it was raining and days when it was not raining and ask the computer to look for patterns in that data. So what might that data look like? Well, we could structure that data in a table like this. This might be what our table looks like, where for any particular day, going back, we have information about that day’s humidity, that day’s air pressure, and then importantly, we have a label, something where the human has said that on this particular day, it was raining or it was not raining. So you could fill in this table with a whole bunch of data. And what makes this what we would call a supervised learning exercise is that a human has gone in and labeled each of these data points, said that on this day, when these were the values for the humidity and pressure, that day was a rainy day and this day was a not rainy day. And what we would like the computer to be able to do then is to be able to figure out, given these inputs, given the humidity and the pressure, can the computer predict what label should be associated with that day? Does that day look more like it’s going to be a day that rains or does it look more like a day when it’s not going to rain? Put a little bit more mathematically, you can think of this as a function that takes two inputs, the inputs being the data points that our computer will have access to, things like humidity and pressure. So we could write a function f that takes as input both humidity and pressure. And then the output is going to be what category we would ascribe to these particular input points, what label we would associate with that input. So we’ve seen a couple of example data points here, where given this value for humidity and this value for pressure, we predict, is it going to rain or is it not going to rain? And that’s information that we just gathered from the world. We measured on various different days what the humidity and pressure were. We observed whether or not we saw rain or no rain on that particular day. And this function f is what we would like to approximate. Now, the computer and we humans don’t really know exactly how this function f works. It’s probably quite a complex function. So what we’re going to do instead is attempt to estimate it. We would like to come up with a hypothesis function. h, which is going to try to approximate what f does. We want to come up with some function h that will also take the same inputs and will also produce an output, rain or no rain. And ideally, we’d like these two functions to agree as much as possible. So the goal then of the supervised learning classification tasks is going to be to figure out, what does that function h look like? How can we begin to estimate, given all of this information, all of this data, what category or what label should be assigned to a particular data point? So where could you begin doing this? Well, a reasonable thing to do, especially in this situation, I have two numerical values, is I could try to plot this on a graph that has two axes, an x-axis and a y-axis. And in this case, we’re just going to be using two numerical values as input. But these same types of ideas scale as you add more and more inputs as well. We’ll be plotting things in two dimensions. But as we soon see, you could add more inputs and just imagine things in multiple dimensions. And while we humans have trouble conceptualizing anything really beyond three dimensions, at least visually, a computer has no problem with trying to imagine things in many, many more dimensions, that for a computer, each dimension is just some separate number that it is keeping track of. So it wouldn’t be unreasonable for a computer to think in 10 dimensions or 100 dimensions to be able to try to solve a problem. But for now, we’ve got two inputs. So we’ll graph things along two axes, an x-axis, which will here represent humidity, and a y-axis, which here represents pressure. And what we might do is say, let’s take all of the days that were raining and just try to plot them on this graph and see where they fall on this graph. And here might be all of the rainy days, where each rainy day is one of these blue dots here that corresponds to a particular value for humidity and a particular value for pressure. And then I might do the same thing with the days that were not rainy. So take all the not rainy days, figure out what their values were for each of these two inputs, and go ahead and plot them on this graph as well. And I’ve here plotted them in red. So blue here stands for a rainy day. Red here stands for a not rainy day. And this then is the input that my computer has access to all of this input. And what I would like the computer to be able to do is to train a model such that if I’m ever presented with a new input that doesn’t have a label associated with it, something like this white dot here, I would like to predict, given those values for each of the two inputs, should we classify it as a blue dot, a rainy day, or should we classify it as a red dot, a not rainy day? And if you’re just looking at this picture graphically, trying to say, all right, this white dot, does it look like it belongs to the blue category, or does it look like it belongs to the red category, I think most people would agree that it probably belongs to the blue category. And why is that? Well, it looks like it’s close to other blue dots. And that’s not a very formal notion, but it’s a notion that we’ll formalize in just a moment. That because it seems to be close to this blue dot here, nothing else is closer to it, then we might say that it should be categorized as blue. It should fall into that category of, I think that day is going to be a rainy day based on that input. Might not be totally accurate, but it’s a pretty good guess. And this type of algorithm is actually a very popular and common machine learning algorithm known as nearest neighbor classification. It’s an algorithm for solving these classification-type problems. And in nearest neighbor classification, it’s going to perform this algorithm. What it will do is, given an input, it will choose the class of the nearest data point to that input. By class, we just here mean category, like rain or no rain, counterfeit or not counterfeit. And we choose the category or the class based on the nearest data point. So given all that data, we just looked at, is the nearest data point a blue point or is it a red point? And depending on the answer to that question, we were able to make some sort of judgment. We were able to say something like, we think it’s going to be blue or we think it’s going to be red. So likewise, we could apply this to other data points that we encounter as well. If suddenly this data point comes about, well, its nearest data is red. So we would go ahead and classify this as a red point, not raining. Things get a little bit trickier, though, when you look at a point like this white point over here and you ask the same sort of question. Should it belong to the category of blue points, the rainy days? Or should it belong to the category of red points, the not rainy days? Now, nearest neighbor classification would say the way you solve this problem is look at which point is nearest to that point. You look at this nearest point and say it’s red. It’s a not rainy day. And therefore, according to nearest neighbor classification, I would say that this unlabeled point, well, that should also be red. It should also be classified as a not rainy day. But your intuition might think that that’s a reasonable judgment to make, that it’s the closest thing is a not rainy day. So may as well guess that it’s a not rainy day. But it’s probably also reasonable to look at the bigger picture of things to say, yes, it is true that the nearest point to it was a red point. But it’s surrounded by a whole bunch of other blue points. So looking at the bigger picture, there’s potentially an argument to be made that this point should actually be blue. And with only this data, we actually don’t know for sure. We are given some input, something we’re trying to predict. And we don’t necessarily know what the output is going to be. So in this case, which one is correct is difficult to say. But oftentimes, considering more than just a single neighbor, considering multiple neighbors can sometimes give us a better result. And so there’s a variant on the nearest neighbor classification algorithm that is known as the K nearest neighbor classification algorithm, where K is some parameter, some number that we choose, for how many neighbors are we going to look at. So one nearest neighbor classification is what we saw before. Just pick the one nearest neighbor and use that category. But with K nearest neighbor classification, where K might be 3, or 5, or 7, to say look at the 3, or 5, or 7 closest neighbors, closest data points to that point, works a little bit differently. This algorithm, we’ll give it an input. Choose the most common class out of the K nearest data points to that input. So if we look at the five nearest points, and three of them say it’s raining, and two of them say it’s not raining, we’ll go with the three instead of the two, because each one effectively gets one vote towards what they believe the category ought to be. And ultimately, you choose the category that has the most votes as a consequence of that. So K nearest neighbor classification, fairly straightforward one to understand intuitively. You just look at the neighbors and figure out what the answer might be. And it turns out this can work very, very well for solving a whole variety of different types of classification problems. But not every model is going to work under every situation. And so one of the things we’ll take a look at today, especially in the context of supervised machine learning, is that there are a number of different approaches to machine learning, a number of different algorithms that we can apply, all solving the same type of problem, all solving some kind of classification problem where we want to take inputs and organize it into different categories. And no one algorithm is necessarily always going to be better than some other algorithm. They each have their trade-offs. And maybe depending on the data, one type of algorithm is going to be better suited to trying to model that information than some other algorithm. And so this is what a lot of machine learning research ends up being about, that when you’re trying to apply machine learning techniques, you’re often looking not just at one particular algorithm, but trying multiple different algorithms, trying to see what is going to give you the best results for trying to predict some function that maps inputs to outputs. So what then are the drawbacks of K nearest neighbor classification? Well, there are a couple. One might be that in a naive approach, at least, it could be fairly slow to have to go through and measure the distance between a point and every single one of these points that exist here. Now, there are ways of trying to get around that. There are data structures that can help to make it more quickly to be able to find these neighbors. There are also techniques you can use to try and prune some of this data, remove some of the data points so that you’re only left with the relevant data points just to make it a little bit easier. But ultimately, what we might like to do is come up with another way of trying to do this classification. And one way of trying to do the classification was looking at what are the neighboring points. But another way might be to try to look at all of the data and see if we can come up with some decision boundary, some boundary that will separate the rainy days from the not rainy days. And in the case of two dimensions, we can do that by drawing a line, for example. So what we might want to try to do is just find some line, find some separator that divides the rainy days, the blue points over here, from the not rainy days, the red points over there. We’re now trying a different approach in contrast with the nearest neighbor approach, which just looked at local data around the input data point that we cared about. Now what we’re doing is trying to use a technique known as linear regression to find some sort of line that will separate the two halves from each other. Now sometimes it’ll actually be possible to come up with some line that perfectly separates all the rainy days from the not rainy days. Realistically, though, this is probably cleaner than many data sets will actually be. Oftentimes, data is messier. There are outliers. There’s random noise that happens inside of a particular system. And what we’d like to do is still be able to figure out what a line might look like. So in practice, the data will not always be linearly separable. Or linearly separable refers to some data set where I could draw a line just to separate the two halves of it perfectly. Instead, you might have a situation like this, where there are some rainy points that are on this side of the line and some not rainy points that are on that side of the line. And there may not be a line that perfectly separates what path of the inputs from the other half, that perfectly separates all the rainy days from the not rainy days. But we can still say that this line does a pretty good job. And we’ll try to formalize a little bit later what we mean when we say something like this line does a pretty good job of trying to make that prediction. But for now, let’s just say we’re looking for a line that does as good of a job as we can at trying to separate one category of things from another category of things. So let’s now try to formalize this a little bit more mathematically. We want to come up with some sort of function, some way we can define this line. And our inputs are things like humidity and pressure in this case. So our inputs we might call x1 is going to represent humidity, and x2 is going to represent pressure. These are inputs that we are going to provide to our machine learning algorithm. And given those inputs, we would like for our model to be able to predict some sort of output. And we are going to predict that using our hypothesis function, which we called h. Our hypothesis function is going to take as input x1 and x2, humidity and pressure in this case. And you can imagine if we didn’t just have two inputs, we had three or four or five inputs or more, we could have this hypothesis function take all of those as input. And we’ll see examples of that a little bit later as well. And now the question is, what does this hypothesis function do? Well, it really just needs to measure, is this data point on one side of the boundary, or is it on the other side of the boundary? And how do we formalize that boundary? Well, the boundary is generally going to be a linear combination of these input variables, at least in this particular case. So what we’re trying to do when we say linear combination is take each of these inputs and multiply them by some number that we’re going to have to figure out. We’ll generally call that number a weight for how important should these variables be in trying to determine the answer. So we’ll weight each of these variables with some weight, and we might add a constant to it just to try and make the function a little bit different. And the result, we just need to compare. Is it greater than 0, or is it less than 0 to say, does it belong on one side of the line or the other side of the line? So what that mathematical expression might look like is this. We would take each of my variables, x1 and x2, multiply them by some weight. I don’t yet know what that weight is, but it’s going to be some number, weight 1 and weight 2. And maybe we just want to add some other weight 0 to it, because the function might require us to shift the entire value up or down by a certain amount. And then we just compare. If we do all this math, is it greater than or equal to 0? If so, we might categorize that data point as a rainy day. And otherwise, we might say, no rain. So the key here, then, is that this expression is how we are going to calculate whether it’s a rainy day or not. We’re going to do a bunch of math where we take each of the variables, multiply them by a weight, maybe add an extra weight to it, see if the result is greater than or equal to 0. And using that result of that expression, we’re able to determine whether it’s raining or not raining. This expression here is in this case going to refer to just some line. If you were to plot that graphically, it would just be some line. And what the line actually looks like depends upon these weights. x1 and x2 are the inputs, but these weights are really what determine the shape of that line, the slope of that line, and what that line actually looks like. So we then would like to figure out what these weights should be. We can choose whatever weights we want, but we want to choose weights in such a way that if you pass in a rainy day’s humidity and pressure, then you end up with a result that is greater than or equal to 0. And we would like it such that if we passed into our hypothesis function a not rainy day’s inputs, then the output that we get should be not raining. So before we get there, let’s try and formalize this a little bit more mathematically just to get a sense for how it is that you’ll often see this if you ever go further into supervised machine learning and explore this idea. One thing is that generally for these categories, we’ll sometimes just use the names of the categories like rain and not rain. Often mathematically, if we’re trying to do comparisons between these things, it’s easier just to deal in the world of numbers. So we could just say 1 and 0, 1 for raining, 0 for not raining. So we do all this math. And if the result is greater than or equal to 0, we’ll go ahead and say our hypothesis function outputs 1, meaning raining. And otherwise, it outputs 0, meaning not raining. And oftentimes, this type of expression will instead express using vector mathematics. And all a vector is, if you’re not familiar with the term, is it refers to a sequence of numerical values. You could represent that in Python using a list of numerical values or a tuple with numerical values. And here, we have a couple of sequences of numerical values. One of our vectors, one of our sequences of numerical values, are all of these individual weights, w0, w1, and w2. So we could construct what we’ll call a weight vector, and we’ll see why this is useful in a moment, called w, generally represented using a boldface w, that is just a sequence of these three weights, weight 0, weight 1, and weight 2. And to be able to calculate, based on those weights, whether we think a day is raining or not raining, we’re going to multiply each of those weights by one of our input variables. That w2, this weight, is going to be multiplied by input variable x2. w1 is going to be multiplied by input variable x1. And w0, well, it’s not being multiplied by anything. But to make sure the vectors are the same length, and we’ll see why that’s useful in just a second, we’ll just go ahead and say w0 is being multiplied by 1. Because you can multiply by something by 1, and you end up getting the exact same number. So in addition to the weight vector w, we’ll also have an input vector that we’ll call x that has three values, 1, again, because we’re just multiplying w0 by 1 eventually, and then x1 and x2. So here, then, we’ve represented two distinct vectors, a vector of weights that we need to somehow learn. The goal of our machine learning algorithm is to learn what this weight vector is supposed to be. We could choose any arbitrary set of numbers, and it would produce a function that tries to predict rain or not rain, but it probably wouldn’t be very good. What we want to do is come up with a good choice of these weights so that we’re able to do the accurate predictions. And then this input vector represents a particular input to the function, a data point for which we would like to estimate, is that day a rainy day, or is that day a not rainy day? And so that’s going to vary just depending on what input is provided to our function, what it is that we are trying to estimate. And then to do the calculation, we want to calculate this expression here, and it turns out that expression is what we would call the dot product of these two vectors. The dot product of two vectors just means taking each of the terms in the vectors and multiplying them together, w0 multiply it by 1, w1 multiply it by x1, w2 multiply it by x2, and that’s why these vectors need to be the same length. And then we just add all of the results together. So the dot product of w and x, our weight vector and our input vector, that’s just going to be w0 times 1, or just w0, plus w1 times x1, multiplying these two terms together, plus w2 times x2, multiplying those terms together. So we have our weight vector, which we need to figure out. We need our machine learning algorithm to figure out what the weights should be. We have the input vector representing the data point that we’re trying to predict a category for, predict a label for. And we’re able to do that calculation by taking this dot product, which you’ll often see represented in vector form. But if you haven’t seen vectors before, you can think of it as identical to just this mathematical expression, just doing the multiplication, adding the results together, and then seeing whether the result is greater than or equal to 0 or not. This expression here is identical to the expression that we’re calculating to see whether or not that answer is greater than or equal to 0 in this case. And so for that reason, you’ll often see the hypothesis function written as something like this, a simpler representation where the hypothesis takes as input some input vector x, some humidity and pressure for some day. And we want to predict an output like rain or no rain or 1 or 0 if we choose to represent things numerically. And the way we do that is by taking the dot product of the weights and our input. If it’s greater than or equal to 0, we’ll go ahead and say the output is 1. Otherwise, the output is going to be 0. And this hypothesis, we say, is parameterized by the weights. Depending on what weights we choose, we’ll end up getting a different hypothesis. If we choose the weights randomly, we’re probably not going to get a very good hypothesis function. We’ll get a 1 or a 0. But it’s probably not accurately going to reflect whether we think a day is going to be rainy or not rainy. But if we choose the weights right, we can often do a pretty good job of trying to estimate whether we think the output of the function should be a 1 or a 0. And so the question, then, is how to figure out what these weights should be, how to be able to tune those parameters. And there are a number of ways you can do that. One of the most common is known as the perceptron learning rule. And we’ll see more of this later. But the idea of the perceptron learning rule, and we’re not going to get too deep into the mathematics, we’ll mostly just introduce it more conceptually, is to say that given some data point that we would like to learn from, some data point that has an input x and an output y, where y is like 1 for rain or 0 for not rain, then we’re going to update the weights. And we’ll look at the formula in just a moment. But the big picture idea is that we can start with random weights, but then learn from the data. Take the data points one at a time. And for each one of the data points, figure out, all right, what parameters do we need to change inside of the weights in order to better match that input point. And so that is the value of having access to a lot of data in the supervised machine learning algorithm, is that you take each of the data points and maybe look at them multiple times and constantly try and figure out whether you need to shift your weights in order to better create some weight vector that is able to correctly or more accurately try to estimate what the output should be, whether we think it’s going to be raining or whether we think it’s not going to be raining. So what does that weight update look like? Without going into too much of the mathematics, we’re going to update each of the weights to be the result of the original weight plus some additional expression. And to understand this expression, y, well, y is what the actual output is. And hypothesis of x, the input, that’s going to be what we thought the input was. And so I can replace this by saying what the actual value was minus what our estimate was. And based on the difference between the actual value and what our estimate was, we might want to change our hypothesis, change the way that we do that estimation. If the actual value and the estimate were the same thing, meaning we were correctly able to predict what category this data point belonged to, well, then actual value minus estimate, that’s just going to be 0, which means this whole term on the right-hand side goes to be 0, and the weight doesn’t change. Weight i, where i is like weight 1 or weight 2 or weight 0, weight i just stays at weight i. And none of the weights change if we were able to correctly predict what category the input belonged to. But if our hypothesis didn’t correctly predict what category the input belonged to, well, then maybe then we need to make some changes, adjust the weights so that we’re better able to predict this kind of data point in the future. And what is the way we might do that? Well, if the actual value was bigger than the estimate, then, and for now we’ll go ahead and assume that these x’s are positive values, then if the actual value was bigger than the estimate, well, that means we need to increase the weight in order to make it such that the output is bigger, and therefore we’re more likely to get to the right actual value. And so if the actual value is bigger than the estimate, then actual value minus estimate, that’ll be a positive number. And so you imagine we’re just adding some positive number to the weight just to increase it ever so slightly. And likewise, the inverse case is true, that if the actual value was less than the estimate, the actual value was 0, but we estimated 1, meaning it actually was not raining, but we predicted it was going to be raining. Well, then we want to decrease the value of the weight, because then in that case, we want to try and lower the total value of computing that dot product in order to make it less likely that we would predict that it would actually be raining. So no need to get too deep into the mathematics of that, but the general idea is that every time we encounter some data point, we can adjust these weights accordingly to try and make the weights better line up with the actual data that we have access to. And you can repeat this process with data point after data point until eventually, hopefully, your algorithm converges to some set of weights that do a pretty good job of trying to figure out whether a day is going to be rainy or not raining. And just as a final point about this particular equation, this value alpha here is generally what we’ll call the learning rate. It’s just some parameter, some number we choose for how quickly we’re actually going to be updating these weight values. So that if alpha is bigger, then we’re going to update these weight values by a lot. And if alpha is smaller, then we’ll update the weight values by less. And you can choose a value of alpha. Depending on the problem, different values might suit the situation better or worse than others. So after all of that, after we’ve done this training process of take all this data and using this learning rule, look at all the pieces of data and use each piece of data as an indication to us of do the weights stay the same, do we increase the weights, do we decrease the weights, and if so, by how much? What you end up with is effectively a threshold function. And we can look at what the threshold function looks like like this. On the x-axis here, we have the output of that function, taking the weights, taking the dot product of it with the input. And on the y-axis, we have what the output is going to be, 0, which in this case represented not raining, and 1, which in this case represented raining. And the way that our hypothesis function works is it calculates this value. And if it’s greater than 0 or greater than some threshold value, then we declare that it’s a rainy day. And otherwise, we declare that it’s a not rainy day. And this then graphically is what that function looks like, that initially when the value of this dot product is small, it’s not raining, it’s not raining, it’s not raining. But as soon as it crosses that threshold, we suddenly say, OK, now it’s raining, now it’s raining, now it’s raining. And the way to interpret this kind of representation is that anything on this side of the line, that would be the category of data points where we say, yes, it’s raining. Anything that falls on this side of the line are the data points where we would say, it’s not raining. And again, we want to choose some value for the weights that results in a function that does a pretty good job of trying to do this estimation. But one tricky thing with this type of hard threshold is that it only leaves two possible outcomes. We plug in some data as input. And the output we get is raining or not raining. And there’s no room for anywhere in between. And maybe that’s what you want. Maybe all you want is given some data point, you would like to be able to classify it into one or two or more of these various different categories. But it might also be the case that you care about knowing how strong that prediction is, for example. So if we go back to this instance here, where we have rainy days on this side of the line, not rainy days on that side of the line, you might imagine that let’s look now at these two white data points. This data point here that we would like to predict a label or a category for. And this data point over here that we would also like to predict a label or a category for. It seems likely that you could pretty confidently say that this data point, that should be a rainy day. Seems close to the other rainy days if we’re going by the nearest neighbor strategy. It’s on this side of the line if we’re going by the strategy of just saying, which side of the line does it fall on by figuring out what those weights should be. And if we’re using the line strategy of just which side of the line does it fall on, which side of this decision boundary, well, we’d also say that this point here is also a rainy day because it falls on the side of the line that corresponds to rainy days. But it’s likely that even in this case, we would know that we don’t feel nearly as confident about this data point on the left as compared to this data point on the right. That for this one on the right, we can feel very confident that yes, it’s a rainy day. This one, it’s pretty close to the line if we’re judging just by distance. And so you might be less sure. But our threshold function doesn’t allow for a notion of less sure or more sure about something. It’s what we would call a hard threshold. It’s once you’ve crossed this line, then immediately we say, yes, this is going to be a rainy day. Anywhere before it, we’re going to say it’s not a rainy day. And that may not be helpful in a number of cases. One, this is not a particularly easy function to deal with. As you get deeper into the world of machine learning and are trying to do things like taking derivatives of these curves with this type of function makes things challenging. But the other challenge is that we don’t really have any notion of gradation between things. We don’t have a notion of yes, this is a very strong belief that it’s going to be raining as opposed to it’s probably more likely than not that it’s going to be raining, but maybe not totally sure about that either. So what we can do by taking advantage of a technique known as logistic regression is instead of using this hard threshold type of function, we can use instead a logistic function, something we might call a soft threshold. And that’s going to transform this into looking something a little more like this, something that more nicely curves. And as a result, the possible output values are no longer just 0 and 1, 0 for not raining, 1 for raining. But you can actually get any real numbered value between 0 and 1. But if you’re way over on this side, then you get a value of 0. OK, it’s not going to be raining, and we’re pretty sure about that. And if you’re over on this side, you get a value of 1. And yes, we’re very sure that it’s going to be raining. But in between, you could get some real numbered value, where a value like 0.7 might mean we think it’s going to rain. It’s more probable that it’s going to rain than not based on the data. But we’re not as confident as some of the other data points might be. So one of the advantages of the soft threshold is that it allows us to have an output that could be some real number that potentially reflects some sort of probability, the likelihood that we think that this particular data point belongs to that particular category. And there are some other nice mathematical properties of that as well. So that then is two different approaches to trying to solve this type of classification problem. One is this nearest neighbor type of approach, where you just take a data point and look at the data points that are nearby to try and estimate what category we think it belongs to. And the other approach is the approach of saying, all right, let’s just try and use linear regression, figure out what these weights should be, adjust the weights in order to figure out what line or what decision boundary is going to best separate these two categories. It turns out that another popular approach, a very popular approach if you just have a data set and you want to start trying to do some learning on it, is what we call the support vector machine. And we’re not going to go too much into the mathematics of the support vector machine, but we’ll at least explore it graphically to see what it is that it looks like. And the idea or the motivation behind the support vector machine is the idea that there are actually a lot of different lines that we could draw, a lot of different decision boundaries that we could draw to separate two groups. So for example, I had the red data points over here and the blue data points over here. One possible line I could draw is a line like this, that this line here would separate the red points from the blue points. And it does so perfectly. All the red points are on one side of the line. All the blue points are on the other side of the line. But this should probably make you a little bit nervous. If you come up with a model and the model comes up with a line that looks like this. And the reason why is that you worry about how well it’s going to generalize to other data points that are not necessarily in the data set that we have access to. For example, if there was a point that fell like right here, for example, on the right side of the line, well, then based on that, we might want to guess that it is, in fact, a red point, but it falls on the side of the line where instead we would estimate that it’s a blue point instead. And so based on that, this line is probably not a great choice just because it is so close to these various data points. We might instead prefer like a diagonal line that just goes diagonally through the data set like we’ve seen before. But there too, there’s a lot of diagonal lines that we could draw as well. For example, I could draw this diagonal line here, which also successfully separates all the red points from all of the blue points. From the perspective of something like just trying to figure out some setting of weights that allows us to predict the correct output, this line will predict the correct output for this particular set of data every single time because the red points are on one side, the blue points are on the other. But yet again, you should probably be a little nervous because this line is so close to these red points, even though we’re able to correctly predict on the input data, if there was a point that fell somewhere in this general area, our algorithm, this model, would say that, yeah, we think it’s a blue point, when in actuality, it might belong to the red category instead just because it looks like it’s close to the other red points. What we really want to be able to say, given this data, how can you generalize this as best as possible, is to come up with a line like this that seems like the intuitive line to draw. And the reason why it’s intuitive is because it seems to be as far apart as possible from the red data and the blue data. So that if we generalize a little bit and assume that maybe we have some points that are different from the input but still slightly further away, we can still say that something on this side probably red, something on that side probably blue, and we can make those judgments that way. And that is what support vector machines are designed to do. They’re designed to try and find what we call the maximum margin separator, where the maximum margin separator is just some boundary that maximizes the distance between the groups of points rather than come up with some boundary that’s very close to one set or the other, where in the case before, we wouldn’t have cared. As long as we’re categorizing the input well, that seems all we need to do. The support vector machine will try and find this maximum margin separator, some way of trying to maximize that particular distance. And it does so by finding what we call the support vectors, which are the vectors that are closest to the line, and trying to maximize the distance between the line and those particular points. And it works that way in two dimensions. It also works in higher dimensions, where we’re not looking for some line that separates the two data points, but instead looking for what we generally call a hyperplane, some decision boundary, effectively, that separates one set of data from the other set of data. And this ability of support vector machines to work in higher dimensions actually has a number of other applications as well. But one is that it helpfully deals with cases where data may not be linearly separable. So we talked about linear separability before, this idea that you can take data and just draw a line or some linear combination of the inputs that allows us to perfectly separate the two sets from each other. There are some data sets that are not linearly separable. And some were even two. You would not be able to find a good line at all that would try to do that kind of separation. Something like this, for example. Or if you imagine here are the red points and the blue points around it. If you try to find a line that divides the red points from the blue points, it’s actually going to be difficult, if not impossible, to do that any line you choose, well, if you draw a line here, then you ignore all of these blue points that should actually be blue and not red. Anywhere else you draw a line, there’s going to be a lot of error, a lot of mistakes, a lot of what we’ll soon call loss to that line that you draw, a lot of points that you’re going to categorize incorrectly. What we really want is to be able to find a better decision boundary that may not be just a straight line through this two dimensional space. And what support vector machines can do is they can begin to operate in higher dimensions and be able to find some other decision boundary, like the circle in this case, that actually is able to separate one of these sets of data from the other set of data a lot better. So oftentimes in data sets where the data is not linearly separable, support vector machines by working in higher dimensions can actually figure out a way to solve that kind of problem effectively. So that then, three different approaches to trying to solve these sorts of problems. We’ve seen support vector machines. We’ve seen trying to use linear regression and the perceptron learning rule to be able to figure out how to categorize inputs and outputs. We’ve seen the nearest neighbor approach. No one necessarily better than any other again. It’s going to depend on the data set, the information you have access to. It’s going to depend on what the function looks like that you’re ultimately trying to predict. And this is where a lot of research and experimentation can be involved in trying to figure out how it is to best perform that kind of estimation. But classification is only one of the tasks that you might encounter in supervised machine learning. Because in classification, what we’re trying to predict is some discrete category. We’re trying to predict red or blue, rain or not rain, authentic or counterfeit. But sometimes what we want to predict is a real numbered value. And for that, we have a related problem, not classification, but instead known as regression. And regression is the supervised learning problem where we try and learn a function mapping inputs to outputs same as before. But instead of the outputs being discrete categories, things like rain or not rain, in a regression problem, the output values are generally continuous values, some real number that we would like to predict. This happens all the time as well. You might imagine that a company might take this approach if it’s trying to figure out, for instance, what the effect of its advertising is. How do advertising dollars spent translate into sales for the company’s product, for example? And so they might like to try to predict some function that takes as input the amount of money spent on advertising. And here, we’re just going to use one input. But again, you could scale this up to many more inputs as well if you have a lot of different kinds of data you have access to. And the goal is to learn a function that given this amount of spending on advertising, we’re going to get this amount in sales. And you might judge, based on having access to a whole bunch of data, like for every past month, here is how much we spent on advertising, and here is what sales were. And we would like to predict some sort of hypothesis function that, again, given the amount spent on advertising, we can predict, in this case, some real number, some number estimate of how much sales we expect that company to do in this month or in this quarter or whatever unit of time we’re choosing to measure things in. And so again, the approach to solving this type of problem, we could try using a linear regression type approach where we take this data and we just plot it. On the x-axis, we have advertising dollars spent. On the y-axis, we have sales. And we might just want to try and draw a line that does a pretty good job of trying to estimate this relationship between advertising and sales. And in this case, unlike before, we’re not trying to separate the data points into discrete categories. But instead, in this case, we’re just trying to find a line that approximates this relationship between advertising and sales so that if we want to figure out what the estimated sales are for a particular advertising budget, you just look it up in this line, figure out for this amount of advertising, we would have this amount of sales and just try and make the estimate that way. And so you can try and come up with a line, again, figuring out how to modify the weights using various different techniques to try and make it so that this line fits as well as possible. So with all of these approaches, then, to trying to solve machine learning style problems, the question becomes, how do we evaluate these approaches? How do we evaluate the various different hypotheses that we could come up with? Because each of these algorithms will give us some sort of hypothesis, some function that maps inputs to outputs, and we want to know, how well does that function work? And you can think of evaluating these hypotheses and trying to get a better hypothesis as kind of like an optimization problem. In an optimization problem, as you recall from before, we were either trying to maximize some objective function by trying to find a global maximum, or we were trying to minimize some cost function by trying to find some global minimum. And in the case of evaluating these hypotheses, one thing we might say is that this cost function, the thing we’re trying to minimize, we might be trying to minimize what we would call a loss function. And what a loss function is, is it is a function that is going to estimate for us how poorly our function performs. More formally, it’s like a loss of utility by whenever we predict something that is wrong, that is a loss of utility. That’s going to add to the output of our loss function. And you could come up with any loss function that you want, just some mathematical way of estimating, given each of these data points, given what the actual output is, and given what our projected output is, our estimate, you could calculate some sort of numerical loss for it. But there are a couple of popular loss functions that are worth discussing, just so that you’ve seen them before. When it comes to discrete categories, things like rain or not rain, counterfeit or not counterfeit, one approaches the 0, 1 loss function. And the way that works is for each of the data points, our loss function takes as input what the actual output is, like whether it was actually raining or not raining, and takes our prediction into account. Did we predict, given this data point, that it was raining or not raining? And if the actual value equals the prediction, well, then the 0, 1 loss function will just say the loss is 0. There was no loss of utility, because we were able to predict correctly. And otherwise, if the actual value was not the same thing as what we predicted, well, then in that case, our loss is 1. We lost something, lost some utility, because what we predicted was the output of the function, was not what it actually was. And the goal, then, in a situation like this would be to come up with some hypothesis that minimizes the total empirical loss, the total amount that we’ve lost, if you add up for all these data points what the actual output is and what your hypothesis would have predicted. So in this case, for example, if we go back to classifying days as raining or not raining, and we came up with this decision boundary, how would we evaluate this decision boundary? How much better is it than drawing the line here or drawing the line there? Well, we could take each of the input data points, and each input data point has a label, whether it was raining or whether it was not raining. And we could compare it to the prediction, whether we predicted it would be raining or not raining, and assign it a numerical value as a result. So for example, these points over here, they were all rainy days, and we predicted they would be raining, because they fall on the bottom side of the line. So they have a loss of 0, nothing lost from those situations. And likewise, same is true for some of these points over here, where it was not raining and we predicted it would not be raining either. Where we do have loss are points like this point here and that point there, where we predicted that it would not be raining, but in actuality, it’s a blue point. It was raining. Or likewise here, we predicted that it would be raining, but in actuality, it’s a red point. It was not raining. And so as a result, we miscategorized these data points that we were trying to train on. And as a result, there is some loss here. One loss here, there, here, and there, for a total loss of 4, for example, in this case. And that might be how we would estimate or how we would say that this line is better than a line that goes somewhere else or a line that’s further down, because this line might minimize the loss. So there is no way to do better than just these four points of loss if you’re just drawing a straight line through our space. So the 0, 1 loss function checks. Did we get it right? Did we get it wrong? If we got it right, the loss is 0, nothing lost. If we got it wrong, then our loss function for that data point says 1. And we add up all of those losses across all of our data points to get some sort of empirical loss, how much we have lost across all of these original data points that our algorithm had access to. There are other forms of loss as well that work especially well when we deal with more real valued cases, cases like the mapping between advertising budget and amount that we do in sales, for example. Because in that case, you care not just that you get the number exactly right, but you care how close you were to the actual value. If the actual value is you did like $2,800 in sales and you predicted that you would do $2,900 in sales, maybe that’s pretty good. That’s much better than if you had predicted you’d do $1,000 in sales, for example. And so we would like our loss function to be able to take that into account as well, take into account not just whether the actual value and the expected value are exactly the same, but also take into account how far apart they were. And so for that one approach is what we call L1 loss. L1 loss doesn’t just look at whether actual and predicted are equal to each other, but we take the absolute value of the actual value minus the predicted value. In other words, we just ask how far apart were the actual and predicted values, and we sum that up across all of the data points to be able to get what our answer ultimately is. So what might this actually look like for our data set? Well, if we go back to this representation where we had advertising along the x-axis, sales along the y-axis, our line was our prediction, our estimate for any given amount of advertising, what we predicted sales was going to be. And our L1 loss is just how far apart vertically along the sales axis our prediction was from each of the data points. So we could figure out exactly how far apart our prediction was from each of the data points and figure out as a result of that what our loss is overall for this particular hypothesis just by adding up all of these various different individual losses for each of these data points. And our goal then is to try and minimize that loss, to try and come up with some line that minimizes what the utility loss is by judging how far away our estimate amount of sales is from the actual amount of sales. And turns out there are other loss functions as well. One that’s quite popular is the L2 loss. The L2 loss, instead of just using the absolute value, like how far away the actual value is from the predicted value, it uses the square of actual minus predicted. So how far apart are the actual and predicted value? And it squares that value, effectively penalizing much more harshly anything that is a worse prediction. So you imagine if you have two data points that you predict as being one value away from their actual value, as opposed to one data point that you predict as being two away from its actual value, the L2 loss function will more harshly penalize that one that is two away, because it’s going to square, however, much the differences between the actual value and the predicted value. And depending on the situation, you might want to choose a loss function depending on what you care about minimizing. If you really care about minimizing the error on more outlier cases, then you might want to consider something like this. But if you’ve got a lot of outliers, and you don’t necessarily care about modeling them, then maybe an L1 loss function is preferable. But there are trade-offs here that you need to decide, based on a particular set of data. But what you do run the risk of with any of these loss functions, with anything that we’re trying to do, is a problem known as overfitting. And overfitting is a big problem that you can encounter in machine learning, which happens anytime a model fits too closely with a data set, and as a result, fails to generalize. We would like our model to be able to accurately predict data and inputs and output pairs for the data that we have access to. But the reason we wanted to do so is because we want our model to generalize well to data that we haven’t seen before. I would like to take data from the past year of whether it was raining or not raining, and use that data to generalize it towards the future. Say, in the future, is it going to be raining or not raining? Or if I have a whole bunch of data on what counterfeit and not counterfeit US dollar bills look like in the past when people have encountered them, I’d like to train a computer to be able to, in the future, generalize to other dollar bills that I might see as well. And the problem with overfitting is that if you try and tie yourself too closely to the data set that you’re training your model on, you can end up not generalizing very well. So what does this look like? Well, we might imagine the rainy day and not rainy day example again from here, where the blue points indicate rainy days and the red points indicate not rainy days. And we decided that we felt pretty comfortable with drawing a line like this as the decision boundary between rainy days and not rainy days. So we can pretty comfortably say that points on this side more likely to be rainy days, points on that side more likely to be not rainy days. But the loss, the empirical loss, isn’t zero in this particular case because we didn’t categorize everything perfectly. There was this one outlier, this one day that it wasn’t raining, but yet our model still predicts that it is raining. But that doesn’t necessarily mean our model is bad. It just means the model isn’t 100% accurate. If you really wanted to try and find a hypothesis that resulted in minimizing the loss, you could come up with a different decision boundary. It wouldn’t be a line, but it would look something like this. This decision boundary does separate all of the red points from all of the blue points because the red points fall on this side of this decision boundary, the blue points fall on the other side of the decision boundary. But this, we would probably argue, is not as good of a prediction. Even though it seems to be more accurate based on all of the available training data that we have for training this machine learning model, we might say that it’s probably not going to generalize well. That if there were other data points like here and there, we might still want to consider those to be rainy days because we think this was probably just an outlier. So if the only thing you care about is minimizing the loss on the data you have available to you, you run the risk of overfitting. And this can happen in the classification case. It can also happen in the regression case, that here we predicted what we thought was a pretty good line relating advertising to sales, trying to predict what sales were going to be for a given amount of advertising. But I could come up with a line that does a better job of predicting the training data, and it would be something that looks like this, just connecting all of the various different data points. And now there is no loss at all. Now I’ve perfectly predicted, given any advertising, what sales are. And for all the data available to me, it’s going to be accurate. But it’s probably not going to generalize very well. I have overfit my model on the training data that is available to me. And so in general, we want to avoid overfitting. We’d like strategies to make sure that we haven’t overfit our model to a particular data set. And there are a number of ways that you could try to do this. One way is by examining what it is that we’re optimizing for. In an optimization problem, all we do is we say, there is some cost, and I want to minimize that cost. And so far, we’ve defined that cost function, the cost of a hypothesis, just as being equal to the empirical loss of that hypothesis, like how far away are the actual data points, the outputs, away from what I predicted them to be based on that particular hypothesis. And if all you’re trying to do is minimize cost, meaning minimizing the loss in this case, then the result is going to be that you might overfit, that to minimize cost, you’re going to try and find a way to perfectly match all the input data. And that might happen as a result of overfitting on that particular input data. So in order to address this, you could add something to the cost function. What counts as cost will not just loss, but also some measure of the complexity of the hypothesis. The word the complexity of the hypothesis is something that you would need to define for how complicated does our line look. This is sort of an Occam’s razor-style approach where we want to give preference to a simpler decision boundary, like a straight line, for example, some simpler curve, as opposed to something far more complex that might represent the training data better but might not generalize as well. We’ll generally say that a simpler solution is probably the better solution and probably the one that is more likely to generalize well to other inputs. So we measure what the loss is, but we also measure the complexity. And now that all gets taken into account when we consider the overall cost, that yes, something might have less loss if it better predicts the training data, but if it’s much more complex, it still might not be the best option that we have. And we need to come up with some balance between loss and complexity. And for that reason, you’ll often see this represented as multiplying the complexity by some parameter that we have to choose, parameter lambda in this case, where we’re saying if lambda is a greater value, then we really want to penalize more complex hypotheses. Whereas if lambda is smaller, we’re going to penalize more complex hypotheses a little bit, and it’s up to the machine learning programmer to decide where they want to set that value of lambda for how much do I want to penalize a more complex hypothesis that might fit the data a little better. And again, there’s no one right answer to a lot of these things, but depending on the data set, depending on the data you have available to you and the problem you’re trying to solve, your choice of these parameters may vary, and you may need to experiment a little bit to figure out what the right choice of that is ultimately going to be. This process, then, of considering not only loss, but also some measure of the complexity is known as regularization. Regularization is the process of penalizing a hypothesis that is more complex in order to favor a simpler hypothesis that is more likely to generalize well, more likely to be able to apply to other situations that are dealing with other input points unlike the ones that we’ve necessarily seen before. So oftentimes, you’ll see us add some regularizing term to what we’re trying to minimize in order to avoid this problem of overfitting. Now, another way of making sure we don’t overfit is to run some experiments and to see whether or not we are able to generalize our model that we’ve created to other data sets as well. And it’s for that reason that oftentimes when you’re doing a machine learning experiment, when you’ve got some data and you want to try and come up with some function that predicts, given some input, what the output is going to be, you don’t necessarily want to do your training on all of the data you have available to you that you could employ a method known as holdout cross-validation, where in holdout cross-validation, we split up our data. We split up our data into a training set and a testing set. The training set is the set of data that we’re going to use to train our machine learning model. And the testing set is the set of data that we’re going to use in order to test to see how well our machine learning model actually performed. So the learning happens on the training set. We figure out what the parameters should be. We figure out what the right model is. And then we see, all right, now that we’ve trained the model, we’ll see how well it does at predicting things inside of the testing set, some set of data that we haven’t seen before. And the hope then is that we’re going to be able to predict the testing set pretty well if we’re able to generalize based on the training data that’s available to us. If we’ve overfit the training data, though, and we’re not able to generalize, well, then when we look at the testing set, it’s likely going to be the case that we’re not going to predict things in the testing set nearly as effectively. So this is one method of cross-validation, validating to make sure that the work we have done is actually going to generalize to other data sets as well. And there are other statistical techniques we can use as well. One of the downsides of this just hold out cross-validation is if you say I just split it 50-50, I train using 50% of the data and test using the other 50%, or you could choose other percentages as well, is that there is a fair amount of data that I am now not using to train, that I might be able to get a better model as a result, for example. So one approach is known as k-fold cross-validation. In k-fold cross-validation, rather than just divide things into two sets and run one experiment, we divide things into k different sets. So maybe I divide things up into 10 different sets and then run 10 different experiments. So if I split up my data into 10 different sets of data, then what I’ll do is each time for each of my 10 experiments, I will hold out one of those sets of data, where I’ll say, let me train my model on these nine sets, and then test to see how well it predicts on set number 10. And then pick another set of nine sets to train on, and then test it on the other one that I held out, where each time I train the model on everything minus the one set that I’m holding out, and then test to see how well our model performs on the test that I did hold out. And what you end up getting is 10 different results, 10 different answers for how accurately our model worked. And oftentimes, you could just take the average of those 10 to get an approximation for how well we think our model performs overall. But the key idea is separating the training data from the testing data, because you want to test your model on data that is different from what you trained the model on. Because the training, you want to avoid overfitting. You want to be able to generalize. And the way you test whether you’re able to generalize is by looking at some data that you haven’t seen before and seeing how well we’re actually able to perform. And so if we want to actually implement any of these techniques inside of a programming language like Python, number of ways we could do that. We could write this from scratch on our own, but there are libraries out there that allow us to take advantage of existing implementations of these algorithms, that we can use the same types of algorithms in a lot of different situations. And so there’s a library, very popular one, known as Scikit-learn, which allows us in Python to be able to very quickly get set up with a lot of these different machine learning models. This library has already written an algorithm for nearest neighbor classification, for doing perceptron learning, for doing a bunch of other types of inference and supervised learning that we haven’t yet talked about. But using it, we can begin to try actually testing how these methods work and how accurately they perform. So let’s go ahead and take a look at one approach to trying to solve this type of problem. All right, so I’m first going to pull up banknotes.csv, which is a whole bunch of data provided by UC Irvine, which is information about various different banknotes that people took pictures of various different banknotes and measured various different properties of those banknotes. And in particular, some human categorized each of those banknotes as either a counterfeit banknote or as not counterfeit. And so what you’re looking at here is each row represents one banknote. This is formatted as a CSV spreadsheet, where just comma separated values separating each of these various different fields. We have four different input values for each of these data points, just information, some measurement that was made on the banknote. And what those measurements exactly are aren’t as important as the fact that we do have access to this data. But more importantly, we have access for each of these data points to a label, where 0 indicates something like this was not a counterfeit bill, meaning it was an authentic bill. And a data point labeled 1 means that it is a counterfeit bill, at least according to the human researcher who labeled this particular data. So we have a whole bunch of data representing a whole bunch of different data points, each of which has these various different measurements that were made on that particular bill, and each of which has an output value, 0 or 1, 0 meaning it was a genuine bill, 1 meaning it was a counterfeit bill. And what we would like to do is use supervised learning to begin to predict or model some sort of function that can take these four values as input and predict what the output would be. We want our learning algorithm to find some sort of pattern that is able to predict based on these measurements, something that you could measure just by taking a photo of a bill, predict whether that bill is authentic or whether that bill is counterfeit. And so how can we do that? Well, I’m first going to open up banknote0.py and see how it is that we do this. I’m first importing a lot of things from Scikit-learn, but importantly, I’m going to set my model equal to the perceptron model, which is one of those models that we talked about before. We’re just going to try and figure out some setting of weights that is able to divide our data into two different groups. Then I’m going to go ahead and read data in for my file from banknotes.csv. And basically, for every row, I’m going to separate that row into the first four values of that row, which is the evidence for that row. And then the label, where if the final column in that row is a 0, the label is authentic. And otherwise, it’s going to be counterfeit. So I’m effectively reading data in from the CSV file, dividing into a whole bunch of rows where each row has some evidence, those four input values that are going to be inputs to my hypothesis function. And then the label, the output, whether it is authentic or counterfeit, that is the thing that I am then trying to predict. So the next step is that I would like to split up my data set into a training set and a testing set, some set of data that I would like to train my machine learning model on, and some set of data that I would like to use to test that model, see how well it performed. So what I’ll do is I’ll go ahead and figure out length of the data, how many data points do I have. I’ll go ahead and take half of them, save that number as a number called holdout. That is how many items I’m going to hold out for my data set to save for the testing phase. I’ll randomly shuffle the data so it’s in some random order. And then I’ll say my testing set will be all of the data up to the holdout. So I’ll take holdout many data items, and that will be my testing set. My training data will be everything else, the information that I’m going to train my model on. And then I’ll say I need to divide my training data into two different sets. I need to divide it into my x values, where x here represents the inputs. So the x values, the x values that I’m going to train on, are basically for every row in my training set, I’m going to get the evidence for that row, those four values, where it’s basically a vector of four numbers, where that is going to be all of the input. And then I need the y values. What are the outputs that I want to learn from, the labels that belong to each of these various different input points? Well, that’s going to be the same thing for each row in the training data. But this time, I take that row and get what its label is, whether it is authentic or counterfeit. So I end up with one list of all of these vectors of my input data, and one list, which follows the same order, but is all of the labels that correspond with each of those vectors. And then to train my model, which in this case is just this perceptron model, I just call model.fit, pass in the training data, and what the labels for those training data are. And scikit-learn will take care of fitting the model, will do the entire algorithm for me. And then when it’s done, I can then test to see how well that model performed. So I can say, let me get all of these input vectors for what I want to test on. So for each row in my testing data set, go ahead and get the evidence. And the y values, those are what the actual values were for each of the rows in the testing data set, what the actual label is. But then I’m going to generate some predictions. I’m going to use this model and try and predict, based on the testing vectors, I want to predict what the output is. And my goal then is to now compare y testing with predictions. I want to see how well my predictions, based on the model, actually reflect what the y values were, what the output is, that were actually labeled. Because I now have this label data, I can assess how well the algorithm worked. And so now I can just compute how well we did. I’m going to, this zip function basically just lets me look through two different lists, one by one at the same time. So for each actual value and for each predicted value, if the actual is the same thing as what I predicted, I’ll go ahead and increment the counter by one. Otherwise, I’ll increment my incorrect counter by one. And so at the end, I can print out, here are the results, here’s how many I got right, here’s how many I got wrong, and here was my overall accuracy, for example. So I can go ahead and run this. I can run python banknote0.py. And it’s going to train on half the data set and then test on half the data set. And here are the results for my perceptron model. In this case, it correctly was able to classify 679 bills as correctly either authentic or counterfeit and incorrectly classified seven of them for an overall accuracy of close to 99% accurate. So on this particular data set, using this perceptron model, we were able to predict very well what the output was going to be. And we can try different models, too, that scikit-learn makes it very easy just to swap out one model for another model. So instead of the perceptron model, I can use the support vector machine using the SVC, otherwise known as a support vector classifier, using a support vector machine to classify things into two different groups. And now see, all right, how well does this perform? And all right, this time, we were able to correctly predict 682 and incorrectly predicted four for accuracy of 99.4%. And we could even try the k-neighbors classifier as the model instead. And this takes a parameter, n neighbors, for how many neighbors do you want to look at? Let’s just look at one neighbor, the one nearest neighbor, and use that to predict. Go ahead and run this as well. And it looks like, based on the k-neighbors classifier, looking at just one neighbor, we were able to correctly classify 685 data points, incorrectly classified one. Maybe let’s try three neighbors instead, instead of just using one neighbor. Do more of a k-nearest neighbors approach, where I look at the three nearest neighbors and see how that performs. And that one, in this case, seems to have gotten 100% of all of the predictions correctly described as either authentic banknotes or as counterfeit banknotes. And we could run these experiments multiple times, because I’m randomly reorganizing the data every time. We’re technically training these on slightly different data sets. And so you might want to run multiple experiments to really see how well they’re actually going to perform. But in short, they all perform very well. And while some of them perform slightly better than others here, that might not always be the case for every data set. But you can begin to test now by very quickly putting together these machine learning models using Scikit-learn to be able to train on some training set and then test on some testing set as well. And this splitting up into training groups and testing groups and testing happens so often that Scikit-learn has functions built in for trying to do it. I did it all by hand just now. But if we take a look at banknotes one, we take advantage of some other features that exist in Scikit-learn, where we can really simplify a lot of our logic, that there is a function built into Scikit-learn called train test split, which will automatically split data into a training group and a testing group. I just have to say what proportion should be in the testing group, something like 0.5, half the data inside the testing group. Then I can fit the model on the training data, make the predictions on the testing data, and then just count up. And Scikit-learn has some nice methods for just counting up how many times our testing data match the predictions, how many times our testing data didn’t match the predictions. So very quickly, you can write programs with not all that many lines of code. It’s maybe like 40 lines of code to get through all of these predictions. And then as a result, see how well we’re able to do. So these types of libraries can allow us, without really knowing the implementation details of these algorithms, to be able to use the algorithms in a very practical way to be able to solve these types of problems. So that then was supervised learning, this task of given a whole set of data, some input output pairs, we would like to learn some function that maps those inputs to those outputs. But turns out there are other forms of learning as well. And another popular type of machine learning, especially nowadays, is known as reinforcement learning. And the idea of reinforcement learning is rather than just being given a whole data set at the beginning of input output pairs, reinforcement learning is all about learning from experience. In reinforcement learning, our agent, whether it’s like a physical robot that’s trying to make actions in the world or just some virtual agent that is a program running somewhere, our agent is going to be given a set of rewards or punishments in the form of numerical values. But you can think of them as reward or punishment. And based on that, it learns what actions to take in the future, that our agent, our AI, will be put in some sort of environment. It will make some actions. And based on the actions that it makes, it learns something. It either gets a reward when it does something well, it gets a punishment when it does something poorly, and it learns what to do or what not to do in the future based on those individual experiences. And so what this will often look like is it will often start with some agent, some AI, which might, again, be a physical robot, if you’re imagining a physical robot moving around, but it can also just be a program. And our agent is situated in their environment, where the environment is where they’re going to make their actions, and it’s what’s going to give them rewards or punishments for various actions that they’re in. So for example, the environment is going to start off by putting our agent inside of a state. Our agent has some state that, in a game, might be the state of the game that the agent is playing. In a world that the agent is exploring might be some position inside of a grid representing the world that they’re exploring. But the agent is in some sort of state. And in that state, the agent needs to choose to take an action. The agent likely has multiple actions they can choose from, but they pick an action. So they take an action in a particular state. And as a result of that, the agent will generally get two things in response as we model them. The agent gets a new state that they find themselves in. After being in this state, taking one action, they end up in some other state. And they’re also given some sort of numerical reward, positive meaning reward, meaning it was a good thing, negative generally meaning they did something bad, they received some sort of punishment. And that is all the information the agent has. It’s told what state it’s in. It makes some sort of action. And based on that, it ends up in another state. And it ends up getting some particular reward. And it needs to learn, based on that information, what actions to begin to take in the future. And so you could imagine generalizing this to a lot of different situations. This is oftentimes how you train if you’ve ever seen those robots that are now able to walk around the way humans do. It would be quite difficult to program the robot in exactly the right way to get it to walk the way humans do. You could instead train it through reinforcement learning, give it some sort of numerical reward every time it does something good, like take steps forward, and punish it every time it does something bad, like fall over, and then let the AI just learn based on that sequence of rewards, based on trying to take various different actions. You can begin to have the agent learn what to do in the future and what not to do. So in order to begin to formalize this, the first thing we need to do is formalize this notion of what we mean about states and actions and rewards, like what does this world look like? And oftentimes, we’ll formulate this world as what’s known as a Markov decision process, similar in spirit to Markov chains, which you might recall from before. But a Markov decision process is a model that we can use for decision making, for an agent trying to make decisions in its environment. And it’s a model that allows us to represent the various different states that an agent can be in, the various different actions that they can take, and also what the reward is for taking one action as opposed to another action. So what then does it actually look like? Well, if you recall a Markov chain from before, a Markov chain looked a little something like this, where we had a whole bunch of these individual states, and each state immediately transitioned to another state based on some probability distribution. We saw this in the context of the weather before, where if it was sunny, we said with some probability, it’ll be sunny the next day. With some other probability, it’ll be rainy, for example. But we could also imagine generalizing this. It’s not just sun and rain anymore. We just have these states, where one state leads to another state according to some probability distribution. But in this original model, there was no agent that had any control over this process. It was just entirely probability based, where with some probability, we moved to this next state. But maybe it’s going to be some other state with some other probability. What we’ll now have is the ability for the agent in this state to choose from a set of actions, where maybe instead of just one path forward, they have three different choices of actions that each lead up down different paths. And even this is a bit of an oversimplification, because in each of these states, you might imagine more branching points where there are more decisions that can be taken as well. So we’ve extended the Markov chain to say that from a state, you now have available action choices. And each of those actions might be associated with its own probability distribution of going to various different states. Then in addition, we’ll add another extension, where any time you move from a state, taking an action, going into this other state, we can associate a reward with that outcome, saying either r is positive, meaning some positive reward, or r is negative, meaning there was some sort of punishment. And this then is what we’ll consider to be a Markov decision process. That a Markov decision process has some initial set of states, of states in the world that we can be in. We have some set of actions that, given a state, I can say, what are the actions that are available to me in that state, an action that I can choose from? Then we have some transition model. The transition model before just said that, given my current state, what is the probability that I end up in that next state or this other state? The transition model now has effectively two things we’re conditioning on. We’re saying, given that I’m in this state and that I take this action, what’s the probability that I end up in this next state? Now maybe we live in a very deterministic world in this Markov decision process. We’re given a state and given an action. We know for sure what next state we’ll end up in. But maybe there’s some randomness in the world that when you take in a state and you take an action, you might not always end up in the exact same state. There might be some probabilities involved there as well. The Markov decision process can handle both of those possible cases. And then finally, we have a reward function, generally called r, that in this case says, what is the reward for being in this state, taking this action, and then getting to s prime this next state? So I’m in this original state. I take this action. I get to this next state. What is the reward for doing that process? And you can add up these rewards every time you take an action to get the total amount of rewards that an agent might get from interacting in a particular environment modeled using this Markov decision process. So what might this actually look like in practice? Well, let’s just create a little simulated world here where I have this agent that is just trying to navigate its way. This agent is this yellow dot here, like a robot in the world, trying to navigate its way through this grid. And ultimately, it’s trying to find its way to the goal. And if it gets to the green goal, then it’s going to get some sort of reward. But then we might also have some red squares that are places where you get some sort of punishment, some bad place where we don’t want the agent to go. And if it ends up in the red square, then our agent is going to get some sort of punishment as a result of that. But the agent originally doesn’t know all of these details. It doesn’t know that these states are associated with punishments. But maybe it does know that this state is associated with a reward. Maybe it doesn’t. But it just needs to sort of interact with the environment to try and figure out what to do and what not to do. So the first thing the agent might do is, given no additional information, if it doesn’t know what the punishments are, it doesn’t know where the rewards are, it just might try and take an action. And it takes an action and ends up realizing that it got some sort of punishment. And so what does it learn from that experience? Well, it might learn that when you’re in this state in the future, don’t take the action move to the right, that that is a bad action to take. That in the future, if you ever find yourself back in the state, don’t take this action of going to the right when you’re in this particular state, because that leads to punishment. That might be the intuition at least. And so you could try doing other actions. You move up, all right, that didn’t lead to any immediate rewards. Maybe try something else. Then maybe try something else. And all right, now you found that you got another punishment. And so you learn something from that experience. So the next time you do this whole process, you know that if you ever end up in this square, you shouldn’t take the down action, because being in this state and taking that action ultimately leads to some sort of punishment, a negative reward, in other words. And this process repeats. You might imagine just letting our agent explore the world, learning over time what states tend to correspond with poor actions, learning over time what states correspond with poor actions, until eventually, if it tries enough things randomly, it might find that eventually when you get to this state, if you take the up action in this state, it might find that you actually get a reward from that. And what it can learn from that is that if you’re in this state, you should take the up action, because that leads to a reward. And over time, you can also learn that if you’re in this state, you should take the left action, because that leads to this state that also lets you eventually get to the reward. So you begin to learn over time not only which actions are good in particular states, but also which actions are bad, such that once you know some sequence of good actions that leads you to some sort of reward, our agent can just follow those instructions, follow the experience that it has learned. We didn’t tell the agent what the goal was. We didn’t tell the agent where the punishments were. But the agent can begin to learn from this experience and learn to begin to perform these sorts of tasks better in the future. And so let’s now try to formalize this idea, formalize the idea that we would like to be able to learn in this state taking this action, is that a good thing or a bad thing? There are lots of different models for reinforcement learning. We’re just going to look at one of them today. And the one that we’re going to look at is a method known as Q-learning. And what Q-learning is all about is about learning a function, a function Q, that takes inputs S and A, where S is a state and A is an action that you take in that state. And what this Q function is going to do is it is going to estimate the value. How much reward will I get from taking this action in this state? Originally, we don’t know what this Q function should be. But over time, based on experience, based on trying things out and seeing what the result is, I would like to try and learn what Q of SA is for any particular state and any particular action that I might take in that state. So what is the approach? Well, the approach originally is we’ll start with Q SA equal to 0 for all states S and for all actions A. That initially, before I’ve ever started anything, before I’ve had any experiences, I don’t know the value of taking any action in any given state. So I’m going to assume that the value is just 0 all across the board. But then as I interact with the world, as I experience rewards or punishments, or maybe I go to a cell where I don’t get either reward or a punishment, I want to somehow update my estimate of Q SA. I want to continually update my estimate of Q SA based on the experiences and rewards and punishments that I’ve received, such that in the future, my knowledge of what actions are good and what states will be better. So when we take an action and receive some sort of reward, I want to estimate the new value of Q SA. And I estimate that based on a couple of different things. I estimate it based on the reward that I’m getting from taking this action and getting into the next state. But assuming the situation isn’t over, assuming there are still future actions that I might take as well, I also need to take into account the expected future rewards. That if you imagine an agent interacting with the environment, then sometimes you’ll take an action and get a reward, but then you can keep taking more actions and get more rewards, that these both are relevant, both the current reward I’m getting from this current step and also my future reward. And it might be the case that I’ll want to take a step that doesn’t immediately lead to a reward, because later on down the line, I know it will lead to more rewards as well. So there’s a balancing act between current rewards that the agent experiences and future rewards that the agent experiences as well. And then we need to update QSA. So we estimate the value of QSA based on the current reward and the expected future rewards. And then we need to update this Q function to take into account this new estimate. Now, we already, as we go through this process, we’ll already have an estimate for what we think the value is. Now we have a new estimate, and then somehow we need to combine these two estimates together, and we’ll look at more formal ways that we can actually begin to do that. So to actually show you what this formula looks like, here is the approach we’ll take with Q learning. We’re going to, again, start with Q of S and A being equal to 0 for all states. And then every time we take an action A in state S and observer reward R, we’re going to update our value, our estimate, for Q of SA. And the idea is that we’re going to figure out what the new value estimate is minus what our existing value estimate is. And so we have some preconceived notion for what the value is for taking this action in this state. Maybe our expectation is we currently think the value is 10. But then we’re going to estimate what we now think it’s going to be. Maybe the new value estimate is something like 20. So there’s a delta of 10 that our new value estimate is 10 points higher than what our current value estimate happens to be. And so we have a couple of options here. We need to decide how much we want to adjust our current expectation of what the value is of taking this action in this particular state. And what that difference is, how much we add or subtract from our existing notion of how much do we expect the value to be, is dependent on this parameter alpha, also called a learning rate. And alpha represents, in effect, how much we value new information compared to how much we value old information. An alpha value of 1 means we really value new information. But if we have a new estimate, then it doesn’t matter what our old estimate is. We’re only going to consider our new estimate because we always just want to take into consideration our new information. So the way that works is that if you imagine alpha being 1, well, then we’re taking the old value of QSA and then adding 1 times the new value minus the old value. And that just leaves us with the new value. So when alpha is 1, all we take into consideration is what our new estimate happens to be. But over time, as we go through a lot of experiences, we already have some existing information. We might have tried taking this action nine times already. And now we just tried it a 10th time. And we don’t only want to consider this 10th experience. I also want to consider the fact that my prior nine experiences, those were meaningful, too. And that’s data I don’t necessarily want to lose. And so this alpha controls that decision, controls how important is the new information. 0 would mean ignore all the new information. Just keep this Q value the same. 1 means replace the old information entirely with the new information. And somewhere in between, keep some sort of balance between these two values. We can put this equation a little bit more formally as well. The old value estimate is our old estimate for what the value is of taking this action in a particular state. That’s just Q of SNA. So we have it once here, and we’re going to add something to it. We’re going to add alpha times the new value estimate minus the old value estimate. But the old value estimate, we just look up by calling this Q function. And what then is the new value estimate? Based on this experience we have just taken, what is our new estimate for the value of taking this action in this particular state? Well, it’s going to be composed of two parts. It’s going to be composed of what reward did I just get from taking this action in this state. And then it’s going to be, what can I expect my future rewards to be from this point forward? So it’s going to be R, some reward I’m getting right now, plus whatever I estimate I’m going to get in the future. And how do I estimate what I’m going to get in the future? Well, it’s a bit of another call to this Q function. It’s going to be take the maximum across all possible actions I could take next and say, all right, of all of these possible actions I could take, which one is going to have the highest reward? And so this then looks a little bit complicated. This is going to be our notion for how we’re going to perform this kind of update. I have some estimate, some old estimate, for what the value is of taking this action in this state. And I’m going to update it based on new information that I experience some reward. I predict what my future reward is going to be. And using that I update what I estimate the reward will be for taking this action in this particular state. And there are other additions you might make to this algorithm as well. Sometimes it might not be the case that future rewards you want to wait equally to current rewards. Maybe you want an agent that values reward now over reward later. And so sometimes you can even add another term in here, some other parameter, where you discount future rewards and say future rewards are not as valuable as rewards immediately. That getting reward in the current time step is better than waiting a year and getting rewards later. But that’s something up to the programmer to decide what that parameter ought to be. But the big picture idea of this entire formula is to say that every time we experience some new reward, we take that into account. We update our estimate of how good is this action. And then in the future, we can make decisions based on that algorithm. Once we have some good estimate for every state and for every action, what the value is of taking that action, then we can do something like implement a greedy decision making policy. That if I am in a state and I want to know what action should I take in that state, well, then I consider for all of my possible actions, what is the value of QSA? What is my estimated value of taking that action in that state? And I will just pick the action that has the highest value after I evaluate that expression. So I pick the action that has the highest value. And based on that, that tells me what action I should take. At any given state that I’m in, I can just greedily say across all my actions, this action gives me the highest expected value. And so I’ll go ahead and choose that action as the action that I take as well. But there is a downside to this kind of approach. And then downside comes up in a situation like this, where we know that there is some solution that gets me to the reward. And our agent has been able to figure that out. But it might not necessarily be the best way or the fastest way. If the agent is allowed to explore a little bit more, it might find that it can get the reward faster by taking some other route instead, by going through this particular path that is a faster way to get to that ultimate goal. And maybe we would like for the agent to be able to figure that out as well. But if the agent always takes the actions that it knows to be best, well, when it gets to this particular square, it doesn’t know that this is a good action because it’s never really tried it. But it knows that going down eventually leads its way to this reward. So it might learn in the future that it should just always take this route and it’s never going to explore and go along that route instead. So in reinforcement learning, there is this tension between exploration and exploitation. And exploitation generally refers to using knowledge that the AI already has. The AI already knows that this is a move that leads to reward. So we’ll go ahead and use that move. And exploration is all about exploring other actions that we may not have explored as thoroughly before because maybe one of these actions, even if I don’t know anything about it, might lead to better rewards faster or to more rewards in the future. And so an agent that only ever exploits information and never explores might be able to get reward, but it might not maximize its rewards because it doesn’t know what other possibilities are out there, possibilities that we only know about by taking advantage of exploration. And so how can we try and address this? Well, one possible solution is known as the Epsilon greedy algorithm, where we set Epsilon equal to how often we want to just make a random move, where occasionally we will just make a random move in order to say, let’s try to explore and see what happens. And then the logic of the algorithm will be with probability 1 minus Epsilon, choose the estimated best move. In a greedy case, we’d always choose the best move. But in Epsilon greedy, we’re most of the time going to choose the best move or sometimes going to choose the best move. But sometimes with probability Epsilon, we’re going to choose a random move instead. So every time we’re faced with the ability to take an action, sometimes we’re going to choose the best move. Sometimes we’re just going to choose a random move. So this type of algorithm can be quite powerful in a reinforcement learning context by not always just choosing the best possible move right now, but sometimes, especially early on, allowing yourself to make random moves that allow you to explore various different possible states and actions more, and maybe over time, you might decrease your value of Epsilon. More and more often, choosing the best move after you’re more confident that you’ve explored what all of the possibilities actually are. So we can put this into practice. And one very common application of reinforcement learning is in game playing, that if you want to teach an agent how to play a game, you just let the agent play the game a whole bunch. And then the reward signal happens at the end of the game. When the game is over, if our AI won the game, it gets a reward of like 1, for example. And if it lost the game, it gets a reward of negative 1. And from that, it begins to learn what actions are good and what actions are bad. You don’t have to tell the AI what’s good and what’s bad, but the AI figures it out based on that reward. Winning the game is some signal, losing the game is some signal, and based on all of that, it begins to figure out what decisions it should actually make. So one very simple game, which you may have played before, is a game called Nim. And in the game of Nim, you’ve got a whole bunch of objects in a whole bunch of different piles, where here I’ve represented each pile as an individual row. So you’ve got one object in the first pile, three in the second pile, five in the third pile, seven in the fourth pile. And the game of Nim is a two player game where players take turns removing objects from piles. And the rule is that on any given turn, you were allowed to remove as many objects as you want from any one of these piles, any one of these rows. You have to remove at least one object, but you remove as many as you want from exactly one of the piles. And whoever takes the last object loses. So player one might remove four from this pile here. Player two might remove four from this pile here. So now we’ve got four piles left, one, three, one, and three. Player one might remove the entirety of the second pile. Player two, if they’re being strategic, might remove two from the third pile. Now we’ve got three piles left, each with one object left. Player one might remove one from one pile. Player two removes one from the other pile. And now player one is left with choosing this one object from the last pile, at which point player one loses the game. So fairly simple game. Piles of objects, any turn you choose how many objects to remove from a pile, whoever removes the last object loses. And this is the type of game you could encode into an AI fairly easily, because the states are really just four numbers. Every state is just how many objects in each of the four piles. And the actions are things like, how many am I going to remove from each one of these individual piles? And the reward happens at the end, that if you were the player that had to remove the last object, then you get some sort of punishment. But if you were not, and the other player had to remove the last object, well, then you get some sort of reward. So we could actually try and show a demonstration of this, that I’ve implemented an AI to play the game of Nim. All right, so here, what we’re going to do is create an AI as a result of training the AI on some number of games, that the AI is going to play against itself, where the idea is the AI will play games against itself, learn from each of those experiences, and learn what to do in the future. And then I, the human, will play against the AI. So initially, we’ll say train zero times, meaning we’re not going to let the AI play any practice games against itself in order to learn from its experiences. We’re just going to see how well it plays. And it looks like there are four piles. I can choose how many I remove from any one of the piles. So maybe from pile three, I will remove five objects, for example. So now, AI chose to take one item from pile zero. So I’m left with these piles now, for example. And so here, I could choose maybe to say, I would like to remove from pile two, I’ll remove all five of them, for example. And so AI chose to take two away from pile one. Now I’m left with one pile that has one object, one pile that has two objects. So from pile three, I will remove two objects. And now I’ve left the AI with no choice but to take that last one. And so the game is over, and I was able to win. But I did so because the AI was really just playing randomly. It didn’t have any prior experience that it was using in order to make these sorts of judgments. Now let me let the AI train itself on 10,000 games. I’m going to let the AI play 10,000 games of nim against itself. Every time it wins or loses, it’s going to learn from that experience and learn in the future what to do and what not to do. So here then, I’ll go ahead and run this again. And now you see the AI running through a whole bunch of training games, 10,000 training games against itself. And now it’s going to let me make these sorts of decisions. So now I’m going to play against the AI. Maybe I’ll remove one from pile three. And the AI took everything from pile three, so I’m left with three piles. I’ll go ahead and from pile two maybe remove three items. And the AI removes one item from pile zero. I’m left with two piles, each of which has two items in it. I’ll remove one from pile one, I guess. And the AI took two from pile two, leaving me with no choice but to take one away from pile one. So it seems like after playing 10,000 games of nim against itself, the AI has learned something about what states and what actions tend to be good and has begun to learn some sort of pattern for how to predict what actions are going to be good and what actions are going to be bad in any given state. So reinforcement learning can be a very powerful technique for achieving these sorts of game-playing agents, agents that are able to play a game well just by learning from experience, whether that’s playing against other people or by playing against itself and learning from those experiences as well. Now, nim is a bit of an easy game to use reinforcement learning for because there are so few states. There are only states that are as many as how many different objects are in each of these various different piles. You might imagine that it’s going to be harder if you think of a game like chess or games where there are many, many more states and many, many more actions that you can imagine taking, where it’s not going to be as easy to learn for every state and for every action what the value is going to be. So oftentimes in that case, we can’t necessarily learn exactly what the value is for every state and for every action, but we can approximate it. So much as we saw with minimax, so we could use a depth-limiting approach to stop calculating at a certain point in time, we can do a similar type of approximation known as function approximation in a reinforcement learning context where instead of learning a value of q for every state and every action, we just have some function that estimates what the value is for taking this action in this particular state that might be based on various different features of the state that the agent happens to be in, where you might have to choose what those features actually are. But you can begin to learn some patterns that generalize beyond one specific state and one specific action that you can begin to learn if certain features tend to be good things or bad things. Reinforcement learning can allow you, using a very similar mechanism, to generalize beyond one particular state and say, if this other state looks kind of like this state, then maybe the similar types of actions that worked in one state will also work in another state as well. And so this type of approach can be quite helpful as you begin to deal with reinforcement learning that exist in larger and larger state spaces where it’s just not feasible to explore all of the possible states that could actually exist. So there, then, are two of the main categories of reinforcement learning. Supervised learning, where you have labeled input and output pairs, and reinforcement learning, where an agent learns from rewards or punishments that it receives. The third major category of machine learning that we’ll just touch on briefly is known as unsupervised learning. And unsupervised learning happens when we have data without any additional feedback, without labels, that in the supervised learning case, all of our data had labels. We labeled the data point with whether that was a rainy day or not rainy day. And using those labels, we were able to infer what the pattern was. Or we labeled data as a counterfeit banknote or not a counterfeit. And using those labels, we were able to draw inferences and patterns to figure out what does a banknote look like versus not. In unsupervised learning, we don’t have any access to any of those labels. But we still would like to learn some of those patterns. And one of the tasks that you might want to perform in unsupervised learning is something like clustering, where clustering is just the task of, given some set of objects, organize it into distinct clusters, groups of objects that are similar to one another. And there’s lots of applications for clustering. It comes up in genetic research, where you might have a whole bunch of different genes and you want to cluster them into similar genes if you’re trying to analyze them across a population or across species. It comes up in an image if you want to take all the pixels of an image, cluster them into different parts of the image. Comes a lot up in market research if you want to divide your consumers into different groups so you know which groups to target with certain types of product advertisements, for example, and a number of other contexts as well in which clustering can be very applicable. One technique for clustering is an algorithm known as k-means clustering. And what k-means clustering is going to do is it is going to divide all of our data points into k different clusters. And it’s going to do so by repeating this process of assigning points to clusters and then moving around those clusters at centers. We’re going to define a cluster by its center, the middle of the cluster, and then assign points to that cluster based on which center is closest to that point. And I’ll show you an example of that now. Here, for example, I have a whole bunch of unlabeled data, just various data points that are in some sort of graphical space. And I would like to group them into various different clusters. But I don’t know how to do that originally. And let’s say I want to assign like three clusters to this group. And you have to choose how many clusters you want in k-means clustering that you could try multiple and see how well those values perform. But I’ll start just by randomly picking some places to put the centers of those clusters. Maybe I have a blue cluster, a red cluster, and a green cluster. And I’m going to start with the centers of those clusters just being in these three locations here. And what k-means clustering tells us to do is once I have the centers of the clusters, assign every point to a cluster based on which cluster center it is closest to. So we end up with something like this, where all of these points are closer to the blue cluster center than any other cluster center. All of these points here are closer to the green cluster center than any other cluster center. And then these two points plus these points over here, those are all closest to the red cluster center instead. So here then is one possible assignment of all these points to three different clusters. But it’s not great that it seems like in this red cluster, these points are kind of far apart. In this green cluster, these points are kind of far apart. It might not be my ideal choice of how I would cluster these various different data points. But k-means clustering is an iterative process that after I do this, there is a next step, which is that after I’ve assigned all of the points to the cluster center that it is nearest to, we are going to re-center the clusters, meaning take the cluster centers, these diamond shapes here, and move them to the middle, or the average, effectively, of all of the points that are in that cluster. So we’ll take this blue point, this blue center, and go ahead and move it to the middle or to the center of all of the points that were assigned to the blue cluster, moving it slightly to the right in this case. And we’ll do the same thing for red. We’ll move the cluster center to the middle of all of these points, weighted by how many points there are. There are more points over here, so the red center ends up moving a little bit further that way. And likewise, for the green center, there are many more points on this side of the green center. So the green center ends up being pulled a little bit further in this direction. So we re-center all of the clusters, and then we repeat the process. We go ahead and now reassign all of the points to the cluster center that they are now closest to. And now that we’ve moved around the cluster centers, these cluster assignments might change. That this point originally was closer to the red cluster center, but now it’s actually closer to the blue cluster center. Same goes for this point as well. And these three points that were originally closer to the green cluster center are now closer to the red cluster center instead. So we can reassign what colors or which clusters each of these data points belongs to, and then repeat the process again, moving each of these cluster means and the middles of the clusterism to the mean, the average, of all of the other points that happen to be there, and repeat the process again. Go ahead and assign each of the points to the cluster that they are closest to. So once we reach a point where we’ve assigned all the points to clusters to the cluster that they are nearest to, and nothing changed, we’ve reached a sort of equilibrium in this situation, where no points are changing their allegiance. And as a result, we can declare this algorithm is now over. And we now have some assignment of each of these points into three different clusters. And it looks like we did a pretty good job of trying to identify which points are more similar to one another than they are to points in other groups. So we have the green cluster down here, this blue cluster here, and then this red cluster over there as well. And we did so without any access to some labels to tell us what these various different clusters were. We just used an algorithm in an unsupervised sense without any of those labels to figure out which points belonged to which categories. And again, lots of applications for this type of clustering technique. And there are many more algorithms in each of these various different fields within machine learning, supervised and reinforcement and unsupervised. But those are many of the big picture foundational ideas that underlie a lot of these techniques, where these are the problems that we’re trying to solve. And we try and solve those problems using a number of different methods of trying to take data and learn patterns in that data, whether that’s trying to find neighboring data points that are similar or trying to minimize some sort of loss function or any number of other techniques that allow us to begin to try to solve these sorts of problems. That then was a look at some of the principles that are at the foundation of modern machine learning, this ability to take data and learn from that data so that the computer can perform a task even if they haven’t explicitly been given instructions in order to do so. Next time, we’ll continue this conversation about machine learning, looking at other techniques we can use for solving these sorts of problems. We’ll see you then. All right, welcome back, everyone, to an introduction to artificial intelligence with Python. Now, last time, we took a look at machine learning, a set of techniques that computers can use in order to take a set of data and learn some patterns inside of that data, learn how to perform a task even if we the programmers didn’t give the computer explicit instructions for how to perform that task. Today, we transition to one of the most popular techniques and tools within machine learning, that of neural networks. And neural networks were inspired as early as the 1940s by researchers who were thinking about how it is that humans learn, studying neuroscience in the human brain and trying to see whether or not we could apply those same ideas to computers as well and model computer learning off of human learning. So how is the brain structured? Well, very simply put, the brain consists of a whole bunch of neurons. And those neurons are connected to one another and communicate with one another in some way. In particular, if you think about the structure of a biological neural network, something like this, there are a couple of key properties that scientists observed. One was that these neurons are connected to each other and receive electrical signals from one another, that one neuron can propagate electrical signals to another neuron. And another point is that neurons process those input signals and then can be activated, that a neuron becomes activated at a certain point and then can propagate further signals onto neurons in the future. And so the question then became, could we take this biological idea of how it is that humans learn with brains and with neurons and apply that to a machine as well, in effect designing an artificial neural network, or an ANN, which will be a mathematical model for learning that is inspired by these biological neural networks? And what artificial neural networks will allow us to do is they will first be able to model some sort of mathematical function. Every time you look at a neural network, which we’ll see more of later today, each one of them is really just some mathematical function that is mapping certain inputs to particular outputs based on the structure of the network, that depending on where we place particular units inside of this neural network, that’s going to determine how it is that the network is going to function. And in particular, artificial neural networks are going to lend themselves to a way that we can learn what the network’s parameters should be. We’ll see more on that in just a moment. But in effect, we want a model such that it is easy for us to be able to write some code that allows for the network to be able to figure out how to model the right mathematical function given a particular set of input data. So in order to create our artificial neural network, instead of using biological neurons, we’re just going to use what we’re going to call units, units inside of a neural network, which we can represent kind of like a node in a graph, which will here be represented just by a blue circle like this. And these artificial units, these artificial neurons, can be connected to one another. So here, for instance, we have two units that are connected by this edge inside of this graph, effectively. And so what we’re going to do now is think of this idea as some sort of mapping from inputs to outputs. So we have one unit that is connected to another unit that we might think of this side of the input and that side of the output. And what we’re trying to do then is to figure out how to solve a problem, how to model some sort of mathematical function. And this might take the form of something we saw last time, which was something like we have certain inputs, like variables x1 and x2. And given those inputs, we want to perform some sort of task, a task like predicting whether or not it’s going to rain. And ideally, we’d like some way, given these inputs, x1 and x2, which stand for some sort of variables to do with the weather, we would like to be able to predict, in this case, a Boolean classification. Is it going to rain, or is it not going to rain? And we did this last time by way of a mathematical function. We defined some function, h, for our hypothesis function, that took as input x1 and x2, the two inputs that we cared about processing, in order to determine whether we thought it was going to rain or whether we thought it was not going to rain. The question then becomes, what does this hypothesis function do in order to make that determination? And we decided last time to use a linear combination of these input variables to determine what the output should be. So our hypothesis function was equal to something like this. Weight 0 plus weight 1 times x1 plus weight 2 times x2. So what’s going on here is that x1 and x2, those are input variables, the inputs to this hypothesis function. And each of those input variables is being multiplied by some weight, which is just some number. So x1 is being multiplied by weight 1, x2 is being multiplied by weight 2. And we have this additional weight, weight 0, that doesn’t get multiplied by an input variable at all, that just serves to either move the function up or move the function’s value down. You can think of this as either a weight that’s just multiplied by some dummy value, like the number 1. It’s multiplied by 1, and so it’s not multiplied by anything. Or sometimes, you’ll see in the literature, people call this variable weight 0 a bias, so that you can think of these variables as slightly different. We have weights that are multiplied by the input, and we separately add some bias to the result as well. You’ll hear both of those terminologies used when people talk about neural networks and machine learning. So in effect, what we’ve done here is that in order to define a hypothesis function, we just need to decide and figure out what these weights should be to determine what values to multiply by our inputs to get some sort of result. Of course, at the end of this, what we need to do is make some sort of classification, like rainy or not rainy. And to do that, we use some sort of function that defines some sort of threshold. And so we saw, for instance, the step function, which is defined as 1 if the result of multiplying the weights by the inputs is at least 0, otherwise it’s 0. And you can think of this line down the middle as kind of like a dotted line. Effectively, it stays at 0 all the way up to one point, and then the function steps or jumps up to 1. So it’s 0 before it reaches some threshold, and then it’s 1 after it reaches a particular threshold. And so this was one way we could define what will come to call an activation function, a function that determines when it is that this output becomes active, changes to 1 instead of being a 0. But we also saw that if we didn’t just want a purely binary classification, we didn’t want purely 1 or 0, but we wanted to allow for some in-between real numbered values, we could use a different function. And there are a number of choices, but the one that we looked at was the logistic sigmoid function that has sort of an s-shaped curve, where we could represent this as a probability that may be somewhere in between the probability of rain or something like 0.5. Maybe a little bit later, the probability of rain is 0.8. And so rather than just have a binary classification of 0 or 1, we could allow for numbers that are in between as well. And it turns out there are many other different types of activation functions, where an activation function just takes the output of multiplying the weights together and adding that bias, and then figuring out what the actual output should be. Another popular one is the rectified linear unit, otherwise known as ReLU. And the way that works is that it just takes its input and takes the maximum of that input and 0. So if it’s positive, it remains unchanged. But if it’s 0, if it’s negative, it goes ahead and levels out at 0. And there are other activation functions that we could choose as well. But in short, each of these activation functions, you can just think of as a function that gets applied to the result of all of this computation. We take some function g and apply it to the result of all of that calculation. And this then is what we saw last time, the way of defining some hypothesis function that takes in inputs, calculate some linear combination of those inputs, and then passes it through some sort of activation function to get our output. And this actually turns out to be the model for the simplest of neural networks, that we’re going to instead represent this mathematical idea graphically by using a structure like this. Here then is a neural network that has two inputs. We can think of this as x1 and this as x2. And then one output, which you can think of as classifying whether or not we think it’s going to rain or not rain, for example, in this particular instance. And so how exactly does this model work? Well, each of these two inputs represents one of our input variables, x1 and x2. And notice that these inputs are connected to this output via these edges, which are going to be defined by their weights. So these edges each have a weight associated with them, weight 1 and weight 2. And then this output unit, what it’s going to do is it is going to calculate an output based on those inputs and based on those weights. This output unit is going to multiply all the inputs by their weights, add in this bias term, which you can think of as an extra w0 term that gets added into it, and then we pass it through an activation function. So this then is just a graphical way of representing the same idea we saw last time just mathematically. And we’re going to call this a very simple neural network. And we’d like for this neural network to be able to learn how to calculate some function, that we want some function for the neural network to learn. And the neural network is going to learn what should the values of w0, w1, and w2 be? What should the activation function be in order to get the result that we would expect? So we can actually take a look at an example of this. What then is a very simple function that we might calculate? Well, if we recall back from when we were looking at propositional logic, one of the simplest functions we looked at was something like the or function that takes two inputs, x and y, and outputs 1, otherwise known as true, if either one of the inputs or both of them are 1, and outputs of 0 if both of the inputs are 0 or false. So this then is the or function. And this was the truth table for the or function, that as long as either of the inputs are 1, the output of the function is 1, and the only case where the output is 0 is where both of the inputs are 0. So the question is, how could we take this and train a neural network to be able to learn this particular function? What would those weights look like? Well, we could do something like this. Here’s our neural network. And I’ll propose that in order to calculate the or function, we’re going to use a value of 1 for each of the weights. And we’ll use a bias of negative 1. And then we’ll just use this step function as our activation function. How then does this work? Well, if I wanted to calculate something like 0 or 0, which we know to be 0 because false or false is false, then what are we going to do? Well, our output unit is going to calculate this input multiplied by the weight, 0 times 1, that’s 0. Same thing here, 0 times 1, that’s 0. And we’ll add to that the bias minus 1. So that’ll give us a result of negative 1. If we plot that on our activation function, negative 1 is here. It’s before the threshold, which means either 0 or 1. It’s only 1 after the threshold. Since negative 1 is before the threshold, the output that this unit provides is going to be 0. And that’s what we would expect it to be, that 0 or 0 should be 0. What if instead we had had 1 or 0, where this is the number 1? Well, in this case, in order to calculate what the output is going to be, we again have to do this weighted sum, 1 times 1, that’s 1. 0 times 1, that’s 0. Sum of that so far is 1. Add negative 1 to that. Well, then the output is 0. And if we plot 0 on the step function, 0 ends up being here. It’s just at the threshold. And so the output here is going to be 1, because the output of 1 or 0, that’s 1. So that’s what we would expect as well. And just for one more example, if I had 1 or 1, what would the result be? Well, 1 times 1 is 1. 1 times 1 is 1. The sum of those is 2. I add the bias term to that. I get the number 1. 1 plotted on this graph is way over there. That’s well beyond the threshold. And so this output is going to be 1 as well. The output is always 0 or 1, depending on whether or not we’re past the threshold. And this neural network then models the OR function, a very simple function, definitely. But it still is able to model it correctly. If I give it the inputs, it will tell me what x1 or x2 happens to be. And you could imagine trying to do this for other functions as well. A function like the AND function, for instance, that takes two inputs and calculates whether both x and y are true. So if x is 1 and y is 1, then the output of x and y is 1. But in all the other cases, the output is 0. How could we model that inside of a neural network as well? Well, it turns out we could do it in the same way, except instead of negative 1 as the bias, we can use negative 2 as the bias instead. What does that end up looking like? Well, if I had 1 and 1, that should be 1, because 1 true and true is equal to true. Well, I take 1 times 1, that’s 1. 1 times 1 is 1. I get a total sum of 2 so far. Now I add the bias of negative 2, and I get the value 0. And 0, when I plot it on the activation function, is just past that threshold, and so the output is going to be 1. But if I had any other input, for example, like 1 and 0, well, the weighted sum of these is 1 plus 0 is going to be 1. Minus 2 is going to give us negative 1, and negative 1 is not past that threshold, and so the output is going to be 0. So those then are some very simple functions that we can model using a neural network that has two inputs and one output, where our goal is to be able to figure out what those weights should be in order to determine what the output should be. And you could imagine generalizing this to calculate more complex functions as well, that maybe, given the humidity and the pressure, we want to calculate what’s the probability that it’s going to rain, for example. Or we might want to do a regression-style problem. We’re given some amount of advertising, and given what month it is maybe, we want to predict what our expected sales are going to be for that particular month. So you could imagine these inputs and outputs being different as well. And it turns out that in some problems, we’re not just going to have two inputs, and the nice thing about these neural networks is that we can compose multiple units together, make our networks more complex just by adding more units into this particular neural network. So the network we’ve been looking at has two inputs and one output. But we could just as easily say, let’s go ahead and have three inputs in there, or have even more inputs, where we could arbitrarily decide however many inputs there are to our problem, all going to be calculating some sort of output that we care about figuring out the value of. How then does the math work for figuring out that output? Well, it’s going to work in a very similar way. In the case of two inputs, we had two weights indicated by these edges, and we multiplied the weights by the numbers, adding this bias term. And we’ll do the same thing in the other cases as well. If I have three inputs, you’ll imagine multiplying each of these three inputs by each of these weights. If I had five inputs instead, we’re going to do the same thing. Here I’m saying sum up from 1 to 5, xi multiplied by weight i. So take each of the five input variables, multiply them by their corresponding weight, and then add the bias to that. So this would be a case where there are five inputs into this neural network, for example. But there could be more, arbitrarily many nodes that we want inside of this neural network, where each time we’re just going to sum up all of those input variables multiplied by their weight and then add the bias term at the very end. And so this allows us to be able to represent problems that have even more inputs just by growing the size of our neural network. Now, the next question we might ask is a question about how it is that we train these neural networks. In the case of the or function and the and function, they were simple enough functions that I could just tell you, like here, what the weights should be. And you could probably reason through it yourself what the weights should be in order to calculate the output that you want. But in general, with functions like predicting sales or predicting whether or not it’s going to rain, these are much trickier functions to be able to figure out. We would like the computer to have some mechanism of calculating what it is that the weights should be, how it is to set the weights so that our neural network is able to accurately model the function that we care about trying to estimate. And it turns out that the strategy for doing this, inspired by the domain of calculus, is a technique called gradient descent. And what gradient descent is, it is an algorithm for minimizing loss when you’re training a neural network. And recall that loss refers to how bad our hypothesis function happens to be, that we can define certain loss functions. And we saw some examples of loss functions last time that just give us a number for any particular hypothesis, saying, how poorly does it model the data? How many examples does it get wrong? How are they worse or less bad as compared to other hypothesis functions that we might define? And this loss function is just a mathematical function. And when you have a mathematical function, in calculus what you could do is calculate something known as the gradient, which you can think of as like a slope. It’s the direction the loss function is moving at any particular point. And what it’s going to tell us is, in which direction should we be moving these weights in order to minimize the amount of loss? And so generally speaking, we won’t get into the calculus of it. But the high level idea for gradient descent is going to look something like this. If we want to train a neural network, we’ll go ahead and start just by choosing the weights randomly. Just pick random weights for all of the weights in the neural network. And then we’ll use the input data that we have access to in order to train the network, in order to figure out what the weights should actually be. So we’ll repeat this process again and again. The first step is we’re going to calculate the gradient based on all of the data points. So we’ll look at all the data and figure out what the gradient is at the place where we currently are for the current setting of the weights, which means in which direction should we move the weights in order to minimize the total amount of loss, in order to make our solution better. And once we’ve calculated that gradient, which direction we should move in the loss function, well, then we can just update those weights according to the gradient. Take a small step in the direction of those weights in order to try to make our solution a little bit better. And the size of the step that we take, that’s going to vary. And you can choose that when you’re training a particular neural network. But in short, the idea is going to be take all the data points, figure out based on those data points in what direction the weights should move, and then move the weights one small step in that direction. And if you repeat that process over and over again, adjusting the weights a little bit at a time based on all the data points, eventually you should end up with a pretty good solution to trying to solve this sort of problem. At least that’s what we would hope to happen. Now, if you look at this algorithm, a good question to ask anytime you’re analyzing an algorithm is what is going to be the expensive part of doing the calculation? What’s going to take a lot of work to try to figure out? What is going to be expensive to calculate? And in particular, in the case of gradient descent, the really expensive part is this all data points part right here, having to take all of the data points and using all of those data points figure out what the gradient is at this particular setting of all of the weights. Because odds are in a big machine learning problem where you’re trying to solve a big problem with a lot of data, you have a lot of data points in order to calculate. And figuring out the gradient based on all of those data points is going to be expensive. And you’ll have to do it many times. You’ll likely repeat this process again and again and again, going through all the data points, taking one small step over and over as you try and figure out what the optimal setting of those weights happens to be. It turns out that we would ideally like to be able to train our neural networks faster, to be able to more quickly converge to some sort of solution that is going to be a good solution to the problem. So in that case, there are alternatives to just standard gradient descent, which looks at all of the data points at once. We can employ a method like stochastic gradient descent, which will randomly just choose one data point at a time to calculate the gradient based on, instead of calculating it based on all of the data points. So the idea there is that we have some setting of the weights. We pick a data point. And based on that one data point, we figure out in which direction should we move all of the weights and move the weights in that small direction, then take another data point and do that again and repeat this process again and again, maybe looking at each of the data points multiple times, but each time only using one data point to calculate the gradient, to calculate which direction we should move in. Now, just using one data point instead of all of the data points probably gives us a less accurate estimate of what the gradient actually is. But on the plus side, it’s going to be much faster to be able to calculate, that we can much more quickly calculate what the gradient is based on one data point, instead of calculating based on all of the data points and having to do all of that computational work again and again. So there are trade-offs here between looking at all of the data points and just looking at one data point. And it turns out that a middle ground that is also quite popular is a technique called mini-batch gradient descent, where the idea there is instead of looking at all of the data versus just a single point, we instead divide our data set up into small batches, groups of data points, where you can decide how big a particular batch is. But in short, you’re just going to look at a small number of points at any given time, hopefully getting a more accurate estimate of the gradient, but also not requiring all of the computational effort needed to look at every single one of these data points. So gradient descent, then, is this technique that we can use in order to train these neural networks, in order to figure out what the setting of all of these weights should be if we want some way to try and get an accurate notion of how it is that this function should work, some way of modeling how to transform the inputs into particular outputs. Now, so far, the networks that we’ve taken a look at have all been structured similar to this. We have some number of inputs, maybe two or three or five or more. And then we have one output that is just predicting like rain or no rain or just predicting one particular value. But often in machine learning problems, we don’t just care about one output. We might care about an output that has multiple different values associated with it. So in the same way that we could take a neural network and add units to the input layer, we can likewise add inputs or add outputs to the output layer as well. Instead of just one output, you could imagine we have two outputs, or we could have four outputs, for example, where in each case, as we add more inputs or add more outputs, if we want to keep this network fully connected between these two layers, we just need to add more weights, that now each of these input nodes has four weights associated with each of the four outputs. And that’s true for each of these various different input nodes. So as we add nodes, we add more weights in order to make sure that each of the inputs can somehow be connected to each of the outputs so that each output value can be calculated based on what the value of the input happens to be. So what might a case be where we want multiple different output values? Well, you might consider that in the case of weather predicting, for example, we might not just care whether it’s raining or not raining. There might be multiple different categories of weather that we would like to categorize the weather into. With just a single output variable, we can do a binary classification, like rain or no rain, for instance, 1 or 0. But it doesn’t allow us to do much more than that. With multiple output variables, I might be able to use each one to predict something a little different. Maybe I want to categorize the weather into one of four different categories, something like is it going to be raining or sunny or cloudy or snowy. And I now have four output variables that can be used to represent maybe the probability that it is rainy as opposed to sunny as opposed to cloudy or as opposed to snowy. How then would this neural network work? Well, we have some input variables that represent some data that we have collected about the weather. Each of those inputs gets multiplied by each of these various different weights. We have more multiplications to do, but these are fairly quick mathematical operations to perform. And then what we get is after passing them through some sort of activation function in the outputs, we end up getting some sort of number, where that number, you might imagine, you could interpret as a probability, like a probability that it is one category as opposed to another category. So here we’re saying that based on the inputs, we think there is a 10% chance that it’s raining, a 60% chance that it’s sunny, a 20% chance of cloudy, a 10% chance that it’s snowy. And given that output, if these represent a probability distribution, well, then you could just pick whichever one has the highest value, in this case, sunny, and say that, well, most likely, we think that this categorization of inputs means that the output should be snowy or should be sunny. And that is what we would expect the weather to be in this particular instance. And so this allows us to do these sort of multi-class classifications, where instead of just having a binary classification, 1 or 0, we can have as many different categories as we want. And we can have our neural network output these probabilities over which categories are more likely than other categories. And using that data, we’re able to draw some sort of inference on what it is that we should do. So this was sort of the idea of supervised machine learning. I can give this neural network a whole bunch of data, a whole bunch of input data corresponding to some label, some output data, like we know that it was raining on this day, we know that it was sunny on that day. And using all of that data, the algorithm can use gradient descent to figure out what all of the weights should be in order to create some sort of model that hopefully allows us a way to predict what we think the weather is going to be. But neural networks have a lot of other applications as well. You could imagine applying the same sort of idea to a reinforcement learning sort of example as well, where you remember that in reinforcement learning, what we wanted to do is train some sort of agent to learn what action to take, depending on what state they currently happen to be in. So depending on the current state of the world, we wanted the agent to pick from one of the available actions that is available to them. And you might model that by having each of these input variables represent some information about the state, some data about what state our agent is currently in. And then the output, for example, could be each of the various different actions that our agent could take, action 1, 2, 3, and 4. And you might imagine that this network would work in the same way, but based on these particular inputs, we go ahead and calculate values for each of these outputs. And those outputs could model which action is better than other actions. And we could just choose, based on looking at those outputs, which action we should take. And so these neural networks are very broadly applicable, that all they’re really doing is modeling some mathematical function. So anything that we can frame as a mathematical function, something like classifying inputs into various different categories or figuring out based on some input state what action we should take, these are all mathematical functions that we could attempt to model by taking advantage of this neural network structure, and in particular, taking advantage of this technique, gradient descent, that we can use in order to figure out what the weights should be in order to do this sort of calculation. Now, how is it that you would go about training a neural network that has multiple outputs instead of just one? Well, with just a single output, we could see what the output for that value should be, and then you update all of the weights that corresponded to it. And when we have multiple outputs, at least in this particular case, we can really think of this as four separate neural networks, that really we just have one network here that has these three inputs corresponding with these three weights corresponding to this one output value. And the same thing is true for this output value. This output value effectively defines yet another neural network that has these same three inputs, but a different set of weights that correspond to this output. And likewise, this output has its own set of weights as well, and same thing for the fourth output too. And so if you wanted to train a neural network that had four outputs instead of just one, in this case where the inputs are directly connected to the outputs, you could really think of this as just training four independent neural networks. We know what the outputs for each of these four should be based on our input data, and using that data, we can begin to figure out what all of these individual weights should be. And maybe there’s an additional step at the end to make sure that we turn these values into a probability distribution such that we can interpret which one is better than another or more likely than another as a category or something like that. So this then seems like it does a pretty good job of taking inputs and trying to predict what outputs should be. And we’ll see some real examples of this in just a moment as well. But it’s important then to think about what the limitations of this sort of approach is, of just taking some linear combination of inputs and passing it into some sort of activation function. And it turns out that when we do this in the case of binary classification, trying to predict does it belong to one category or another, we can only predict things that are linearly separable. Because we’re taking a linear combination of inputs and using that to define some decision boundary or threshold, then what we get is a situation where if we have this set of data, we can predict a line that separates linearly the red points from the blue points, but a single unit that is making a binary classification, otherwise known as a perceptron, can’t deal with a situation like this, where we’ve seen this type of situation before, where there is no straight line that just goes straight through the data that will divide the red points away from the blue points. It’s a more complex decision boundary. The decision boundary somehow needs to capture the things inside of this circle. And there isn’t really a line that will allow us to deal with that. So this is the limitation of the perceptron, these units that just make these binary decisions based on their inputs, that a single perceptron is only capable of learning a linearly separable decision boundary. All it can do is define a line. And sure, it can give us probabilities based on how close to that decision boundary we are, but it can only really decide based on a linear decision boundary. And so this doesn’t seem like it’s going to generalize well to situations where real world data is involved, because real world data often isn’t linearly separable. It often isn’t the case that we can just draw a line through the data and be able to divide it up into multiple groups. So what then is the solution to this? Well, what was proposed was the idea of a multilayer neural network, that so far all of the neural networks we’ve seen have had a set of inputs and a set of outputs, and the inputs are connected to those outputs. But in a multilayer neural network, this is going to be an artificial neural network that has an input layer still. It has an output layer, but also has one or more hidden layers in between. Other layers of artificial neurons or units that are going to calculate their own values as well. So instead of a neural network that looks like this with three inputs and one output, you might imagine in the middle here injecting a hidden layer, something like this. This is a hidden layer that has four nodes. You could choose how many nodes or units end up going into the hidden layer. You can have multiple hidden layers as well. And so now each of these inputs isn’t directly connected to the output. Each of the inputs is connected to this hidden layer. And then all of the nodes in the hidden layer, those are connected to the one output. And so this is just another step that we can take towards calculating more complex functions. Each of these hidden units will calculate its output value, otherwise known as its activation, based on a linear combination of all the inputs. And once we have values for all of these nodes, as opposed to this just being the output, we do the same thing again. Calculate the output for this node based on multiplying each of the values for these units by their weights as well. So in effect, the way this works is that we start with inputs. They get multiplied by weights in order to calculate values for the hidden nodes. Those get multiplied by weights in order to figure out what the ultimate output is going to be. And the advantage of layering things like this is it gives us an ability to model more complex functions, that instead of just having a single decision boundary, a single line dividing the red points from the blue points, each of these hidden nodes can learn a different decision boundary. And we can combine those decision boundaries to figure out what the ultimate output is going to be. And as we begin to imagine more complex situations, you could imagine each of these nodes learning some useful property or learning some useful feature of all of the inputs and us somehow learning how to combine those features together in order to get the output that we actually want. Now, the natural question when we begin to look at this now is to ask the question of, how do we train a neural network that has hidden layers inside of it? And this turns out to initially be a bit of a tricky question, because the input data that we are given is we are given values for all of the inputs, and we’re given what the value of the output should be, what the category is, for example. But the input data doesn’t tell us what the values for all of these nodes should be. So we don’t know how far off each of these nodes actually is because we’re only given data for the inputs and the outputs. The reason this is called the hidden layer is because the data that is made available to us doesn’t tell us what the values for all of these intermediate nodes should actually be. And so the strategy people came up with was to say that if you know what the error or the losses on the output node, well, then based on what these weights are, if one of these weights is higher than another, you can calculate an estimate for how much the error from this node was due to this part of the hidden node, or this part of the hidden layer, or this part of the hidden layer, based on the values of these weights, in effect saying that based on the error from the output, I can back propagate the error and figure out an estimate for what the error is for each of these nodes in the hidden layer as well. And there’s some more calculus here that we won’t get into the details of, but the idea of this algorithm is known as back propagation. It’s an algorithm for training a neural network with multiple different hidden layers. And the idea for this, the pseudocode for it, will again be if we want to run gradient descent with back propagation. We’ll start with a random choice of weights, as we did before. And now we’ll go ahead and repeat the training process again and again. But what we’re going to do each time is now we’re going to calculate the error for the output layer first. We know the output and what it should be, and we know what we calculated so we can figure out what the error there is. But then we’re going to repeat for every layer, starting with the output layer, moving back into the hidden layer, then the hidden layer before that if there are multiple hidden layers, going back all the way to the very first hidden layer, assuming there are multiple, we’re going to propagate the error back one layer. Whatever the error was from the output, figure out what the error should be a layer before that based on what the values of those weights are. And then we can update those weights. So graphically, the way you might think about this is that we first start with the output. We know what the output should be. We know what output we calculated. And based on that, we can figure out, all right, how do we need to update those weights? Backpropagating the error to these nodes. And using that, we can figure out how we should update these weights. And you might imagine if there are multiple layers, we could repeat this process again and again to begin to figure out how all of these weights should be updated. And this backpropagation algorithm is really the key algorithm that makes neural networks possible. It makes it possible to take these multi-level structures and be able to train those structures depending on what the values of these weights are in order to figure out how it is that we should go about updating those weights in order to create some function that is able to minimize the total amount of loss, to figure out some good setting of the weights that will take the inputs and translate it into the output that we expect. And this works, as we said, not just for a single hidden layer. But you can imagine multiple hidden layers, where each hidden layer we just define however many nodes we want, where each of the nodes in one layer, we can connect to the nodes in the next layer, defining more and more complex networks that are able to model more and more complex types of functions. And so this type of network is what we might call a deep neural network, part of a larger family of deep learning algorithms, if you’ve ever heard that term. And all deep learning is about is it’s using multiple layers to be able to predict and be able to model higher level features inside of the input, to be able to figure out what the output should be. And so a deep neural network is just a neural network that has multiple of these hidden layers, where we start at the input, calculate values for this layer, then this layer, then this layer, and then ultimately get an output. And this allows us to be able to model more and more sophisticated types of functions, that each of these layers can calculate something a little bit different, and we can combine that information to figure out what the output should be. Of course, as with any situation of machine learning, as we begin to make our models more and more complex, to model more and more complex functions, the risk we run is something like overfitting. And we talked about overfitting last time in the context of overfitting based on when we were training our models to be able to learn some sort of decision boundary, where overfitting happens when we fit too closely to the training data. And as a result, we don’t generalize well to other situations as well. And one of the risks we run with a far more complex neural network that has many, many different nodes is that we might overfit based on the input data. We might grow over reliant on certain nodes to calculate things just purely based on the input data that doesn’t allow us to generalize very well to the output. And there are a number of strategies for dealing with overfitting. But one of the most popular in the context of neural networks is a technique known as dropout. And what dropout does is it, when we’re training the neural network, what we’ll do in dropout is temporarily remove units, temporarily remove these artificial neurons from our network chosen at random. And the goal here is to prevent over-reliance on certain units. What generally happens in overfitting is that we begin to over-rely on certain units inside the neural network to be able to tell us how to interpret the input data. What dropout will do is randomly remove some of these units in order to reduce the chance that we over-rely on certain units to make our neural network more robust, to be able to handle the situations even when we just drop out particular neurons entirely. So the way that might work is we have a network like this. And as we’re training it, when we go about trying to update the weights the first time, we’ll just randomly pick some percentage of the nodes to drop out of the network. It’s as if those nodes aren’t there at all. It’s as if the weights associated with those nodes aren’t there at all. And we’ll train it this way. Then the next time we update the weights, we’ll pick a different set and just go ahead and train that way. And then again, randomly choose and train with other nodes that have been dropped out as well. And the goal of that is that after the training process, if you train by dropping out random nodes inside of this neural network, you hopefully end up with a network that’s a little bit more robust, that doesn’t rely too heavily on any one particular node, but more generally learns how to approximate a function in general. So that then is a look at some of these techniques that we can use in order to implement a neural network, to get at the idea of taking this input, passing it through these various different layers in order to produce some sort of output. And what we’d like to do now is take those ideas and put them into code. And to do that, there are a number of different machine learning libraries, neural network libraries that we can use that allow us to get access to someone’s implementation of back propagation and all of these hidden layers. And one of the most popular, developed by Google, is known as TensorFlow, a library that we can use for quickly creating neural networks and modeling them and running them on some sample data to see what the output is going to be. And before we actually start writing code, we’ll go ahead and take a look at TensorFlow’s playground, which will be an opportunity for us just to play around with this idea of neural networks in different layers, just to get a sense for what it is that we can do by taking advantage of neural networks. So let’s go ahead and go into TensorFlow’s playground, which you can go to by visiting that URL from before. And what we’re going to do now is we’re going to try and learn the decision boundary for this particular output. I want to learn to separate the orange points from the blue points. And I’d like to learn some sort of setting of weights inside of a neural network that will be able to separate those from each other. The features we have access to, our input data, are the x value and the y value, so the two values along each of the two axes. And what I’ll do now is I can set particular parameters, like what activation function I would like to use. And I’ll just go ahead and press play and see what happens. And what happens here is that you’ll see that just by using these two input features, the x value and the y value, with no hidden layers, just take the input, x and y values, and figure out what the decision boundary is. Our neural network learns pretty quickly that in order to divide these two points, we should just use this line. This line acts as a decision boundary that separates this group of points from that group of points, and it does it very well. You can see up here what the loss is. The training loss is 0, meaning we were able to perfectly model separating these two points from each other inside of our training data. So this was a fairly simple case of trying to apply a neural network because the data is very clean. It’s very nicely linearly separable. We could just draw a line that separates all of those points from each other. Let’s now consider a more complex case. So I’ll go ahead and pause the simulation, and we’ll go ahead and look at this data set here. This data set is a little bit more complex now. In this data set, we still have blue and orange points that we’d like to separate from each other. But there’s no single line that we can draw that is going to be able to figure out how to separate the blue from the orange, because the blue is located in these two quadrants, and the orange is located here and here. It’s a more complex function to be able to learn. So let’s see what happens. If we just try and predict based on those inputs, the x and y coordinates, what the output should be, I’ll press Play. And what you’ll notice is that we’re not really able to draw much of a conclusion, that we’re not able to very cleanly see how we should divide the orange points from the blue points, and you don’t see a very clean separation there. So it seems like we don’t have enough sophistication inside of our network to be able to model something that is that complex. We need a better model for this neural network. And I’ll do that by adding a hidden layer. So now I have a hidden layer that has two neurons inside of it. So I have two inputs that then go to two neurons inside of a hidden layer that then go to our output. And now I’ll press Play. And what you’ll notice here is that we’re able to do slightly better. We’re able to now say, all right, these points are definitely blue. These points are definitely orange. We’re still struggling a little bit with these points up here, though. And what we can do is we can see for each of these hidden neurons, what is it exactly that these hidden neurons are doing? Each hidden neuron is learning its own decision boundary. And we can see what that boundary is. This first neuron is learning, all right, this line that seems to separate some of the blue points from the rest of the points. This other hidden neuron is learning another line that seems to be separating the orange points in the lower right from the rest of the points. So that’s why we’re able to figure out these two areas in the bottom region. But we’re still not able to perfectly classify all of the points. So let’s go ahead and add another neuron. Now we’ve got three neurons inside of our hidden layer and see what we’re able to learn now. All right, well, now we seem to be doing a better job. By learning three different decision boundaries, which each of the three neurons inside of our hidden layer, we’re able to much better figure out how to separate these blue points from the orange points. And we can see what each of these hidden neurons is learning. Each one is learning a slightly different decision boundary. And then we’re combining those decision boundaries together to figure out what the overall output should be. And then we can try it one more time by adding a fourth neuron there and try learning that. And it seems like now we can do even better at trying to separate the blue points from the orange points. But we were only able to do this by adding a hidden layer, by adding some layer that is learning some other boundaries and combining those boundaries to determine the output. And the strength, the size and thickness of these lines indicate how high these weights are, how important each of these inputs is for making this sort of calculation. And we can do maybe one more simulation. Let’s go ahead and try this on a data set that looks like this. Go ahead and get rid of the hidden layer. Here now we’re trying to separate the blue points from the orange points where all the blue points are located, again, inside of a circle effectively. So we’re not going to be able to learn a line. Notice I press Play. And we’re really not able to draw any sort of classification at all because there is no line that cleanly separates the blue points from the orange points. So let’s try to solve this by introducing a hidden layer. I’ll go ahead and press Play. And all right, with two neurons in a hidden layer, we’re able to do a little better because we effectively learned two different decision boundaries. We learned this line here. And we learned this line on the right-hand side. And right now we’re just saying, all right, well, if it’s in between, we’ll call it blue. And if it’s outside, we’ll call it orange. So not great, but certainly better than before, that we’re learning one decision boundary and another. And based on those, we can figure out what the output should be. But let’s now go ahead and add a third neuron and see what happens now. I go ahead and train it. And now, using three different decision boundaries that are learned by each of these hidden neurons, we’re able to much more accurately model this distinction between blue points and orange points. We’re able to figure out maybe with these three decision boundaries, combining them together, you can imagine figuring out what the output should be and how to make that sort of classification. And so the goal here is just to get a sense for having more neurons in these hidden layers allows us to learn more structure in the data, allows us to figure out what the relevant and important decision boundaries are. And then using this backpropagation algorithm, we’re able to figure out what the values of these weights should be in order to train this network to be able to classify one category of points away from another category of points instead. And this is ultimately what we’re going to be trying to do whenever we’re training a neural network. So let’s go ahead and actually see an example of this. You’ll recall from last time that we had this banknotes file that included information about counterfeit banknotes as opposed to authentic banknotes, where I had four different values for each banknote and then a categorization of whether that banknote is considered to be authentic or a counterfeit note. And what I wanted to do was, based on that input information, figure out some function that could calculate based on the input information what category it belonged to. And what I’ve written here in banknotes.py is a neural network that will learn just that, a network that learns based on all of the input whether or not we should categorize a banknote as authentic or as counterfeit. The first step is the same as what we saw from last time. I’m really just reading the data in and getting it into an appropriate format. And so this is where more of the writing Python code on your own comes in, in terms of manipulating this data, massaging the data into a format that will be understood by a machine learning library like scikit-learn or like TensorFlow. And so here I separate it into a training and a testing set. And now what I’m doing down below is I’m creating a neural network. Here I’m using TF, which stands for TensorFlow. Up above, I said import TensorFlow as TF, TF just an abbreviation that we’ll often use so we don’t need to write out TensorFlow every time we want to use anything inside of the library. I’m using TF.keras. Keras is an API, a set of functions that we can use in order to manipulate neural networks inside of TensorFlow. And it turns out there are other machine learning libraries that also use the Keras API. But here I’m saying, all right, go ahead and give me a model that is a sequential model, a sequential neural network, meaning one layer after another. And now I’m going to add to that model what layers I want inside of my neural network. So here I’m saying model.add. Go ahead and add a dense layer. And when we say a dense layer, we mean a layer that is just each of the nodes inside of the layer is going to be connected to each of the nodes from the previous layer. So we have a densely connected layer. This layer is going to have eight units inside of it. So it’s going to be a hidden layer inside of a neural network with eight different units, eight artificial neurons, each of which might learn something different. And I just sort of chose eight arbitrarily. You could choose a different number of hidden nodes inside of the layer. And as we saw before, depending on the number of units there are inside of your hidden layer, more units means you can learn more complex functions. So maybe you can more accurately model the training data. But it comes at the cost. More units means more weights that you need to figure out how to update. So it might be more expensive to do that calculation. And you also run the risk of overfitting on the data. If you have too many units and you learn to just overfit on the training data, that’s not good either. So there is a balance. And there’s often a testing process where you’ll train on some data and maybe validate how well you’re doing on a separate set of data, often called a validation set, to see, all right, which setting of parameters. How many layers should I have? How many units should be in each layer? Which one of those performs the best on the validation set? So you can do some testing to figure out what these hyper parameters, so called, should be equal to. Next, I specify what the input shape is. Meaning, all right, what does my input look like? My input has four values. And so the input shape is just four, because we have four inputs. And then I specify what the activation function is. And the activation function, again, we can choose. There are a number of different activation functions. Here I’m using relu, which you might recall from earlier. And then I’ll add an output layer. So I have my hidden layer. Now I’m adding one more layer that will just have one unit, because all I want to do is predict something like counterfeit build or authentic build. So I just need a single unit. And the activation function I’m going to use here is that sigmoid activation function, which, again, was that S-shaped curve that just gave us a probability of what is the probability that this is a counterfeit build, as opposed to an authentic build. So that, then, is the structure of my neural network, a sequential neural network that has one hidden layer with eight units inside of it, and then one output layer that just has a single unit inside of it. And I can choose how many units there are. I can choose the activation function. Then I’m going to compile this model. TensorFlow gives you a choice of how you would like to optimize the weights. There are various different algorithms for doing that. What type of loss function you want to use. Again, many different options for doing that. And then how I want to evaluate my model, well, I care about accuracy. I care about how many of my points am I able to classify correctly versus not correctly as counterfeit or not counterfeit. And I would like it to report to me how accurate my model is performing. Then, now that I’ve defined that model, I call model.fit to say go ahead and train the model. Train it on all the training data plus all of the training labels. So labels for each of those pieces of training data. And I’m saying run it for 20 epics, meaning go ahead and go through each of these training points 20 times, effectively. Go through the data 20 times and keep trying to update the weights. If I did it for more, I could train for even longer and maybe get a more accurate result. But then after I fit it on all the data, I’ll go ahead and just test it. I’ll evaluate my model using model.evaluate built into TensorFlow that is just going to tell me how well do I perform on the testing data. So ultimately, this is just going to give me some numbers that tell me how well we did in this particular case. So now what I’m going to do is go into banknotes and go ahead and run banknotes.py. And what’s going to happen now is it’s going to read in all of that training data. It’s going to generate a neural network with all my inputs, my eight hidden units inside my layer, and then an output unit. And now what it’s doing is it’s training. It’s training 20 times. And each time you can see how my accuracy is increasing on my training data. It starts off the very first time not very accurate, though better than random, something like 79% of the time. It’s able to accurately classify one bill from another. But as I keep training, notice this accuracy value improves and improves and improves until after I’ve trained through all the data points 20 times, it looks like my accuracy is above 99% on the training data. And here’s where I tested it on a whole bunch of testing data. And it looks like in this case, I was also like 99.8% accurate. So just using that, I was able to generate a neural network that can detect counterfeit bills from authentic bills based on this input data 99.8% of the time, at least based on this particular testing data. And I might want to test it with more data as well, just to be confident about that. But this is really the value of using a machine learning library like TensorFlow. And there are others available for Python and other languages as well. But all I have to do is define the structure of the network and define the data that I’m going to pass into the network. And then TensorFlow runs the backpropagation algorithm for learning what all of those weights should be, for figuring out how to train this neural network to be able to accurately, as accurately as possible, figure out what the output values should be there as well. And so this then was a look at what it is that neural networks can do just using these sequences of layer after layer after layer. And you can begin to imagine applying these to much more general problems. And one big problem in computing and artificial intelligence more generally is the problem of computer vision. Computer vision is all about computational methods for analyzing and understanding images. You might have pictures that you want the computer to figure out how to deal with, how to process those images and figure out how to produce some sort of useful result out of this. You’ve seen this in the context of social media websites that are able to look at a photo that contains a whole bunch of faces. And it’s able to figure out what’s a picture of whom and label those and tag them with appropriate people. This is becoming increasingly relevant as we begin to discuss self-driving cars, that these cars now have cameras. And we would like for the computer to have some sort of algorithm that looks at the image and figures out what color is the light, what cars are around us and in what direction, for example. And so computer vision is all about taking an image and figuring out what sort of computation, what sort of calculation we can do with that image. It’s also relevant in the context of something like handwriting recognition. This, what you’re looking at, is an example of the MNIST data set. It’s a big data set just of handwritten digits that we could use to ideally try and figure out how to predict, given someone’s handwriting, given a photo of a digit that they have drawn, can you predict whether it’s a 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9, for example. So this sort of handwriting recognition is yet another task that we might want to use computer vision tasks and tools to be able to apply it towards. This might be a task that we might care about. So how, then, can we use neural networks to be able to solve a problem like this? Well, neural networks rely upon some sort of input where that input is just numerical data. We have a whole bunch of units where each one of them just represents some sort of number. And so in the context of something like handwriting recognition or in the context of just an image, you might imagine that an image is really just a grid of pixels, grid of dots where each dot has some sort of color. And in the context of something like handwriting recognition, you might imagine that if you just fill in each of these dots in a particular way, you can generate a 2 or an 8, for example, based on which dots happen to be shaded in and which dots are not. And we can represent each of these pixel values just using numbers. So for a particular pixel, for example, 0 might represent entirely black. Depending on how you’re representing color, it’s often common to represent color values on a 0 to 255 range so that you can represent a color using 8 bits for a particular value, like how much white is in the image. So 0 might represent all black. 255 might represent entirely white as a pixel. And somewhere in between might represent some shade of gray, for example. But you might imagine not just having a single slider that determines how much white is in the image, but if you had a color image, you might imagine three different numerical values, a red, green, and blue value, where the red value controls how much red is in the image. We have one value for controlling how much green is in the pixel and one value for how much blue is in the pixel as well. And depending on how it is that you set these values of red, green, and blue, you can get a different color. And so any pixel can really be represented, in this case, by three numerical values, a red value, a green value, and a blue value. And if you take a whole bunch of these pixels, assemble them together inside of a grid of pixels, then you really just have a whole bunch of numerical values that you can use in order to perform some sort of prediction task. And so what you might imagine doing is using the same techniques we talked about before, just design a neural network with a lot of inputs, that for each of the pixels, we might have one or three different inputs in the case of a color image, a different input that is just connected to a deep neural network, for example. And this deep neural network might take all of the pixels inside of the image of what digit a person drew. And the output might be like 10 neurons that classify it as a 0, or a 1, or a 2, or a 3, or just tells us in some way what that digit happens to be. Now, there are a couple of drawbacks to this approach. The first drawback to the approach is just the size of this input array, that we have a whole bunch of inputs. If we have a big image that has a lot of different channels, we’re looking at a lot of inputs, and therefore a lot of weights that we have to calculate. And a second problem is the fact that by flattening everything into just this structure of all the pixels, we’ve lost access to a lot of the information about the structure of the image that’s relevant, that really, when a person looks at an image, they’re looking at particular features of the image. They’re looking at curves. They’re looking at shapes. They’re looking at what things can you identify in different regions of the image, and maybe put those things together in order to get a better picture of what the overall image is about. And by just turning it into pixel values for each of the pixels, sure, you might be able to learn that structure, but it might be challenging in order to do so. It might be helpful to take advantage of the fact that you can use properties of the image itself, the fact that it’s structured in a particular way, to be able to improve the way that we learn based on that image too. So in order to figure out how we can train our neural networks to better be able to deal with images, we’ll introduce a couple of ideas, a couple of algorithms that we can apply that allow us to take the image and extract some useful information out of that image. And the first idea we’ll introduce is the notion of image convolution. And what image convolution is all about is it’s about filtering an image, sort of extracting useful or relevant features out of the image. And the way we do that is by applying a particular filter that basically adds the value for every pixel with the values for all of the neighboring pixels to it, according to some sort of kernel matrix, which we’ll see in a moment, is going to allow us to weight these pixels in various different ways. And the goal of image convolution, then, is to extract some sort of interesting or useful features out of an image, to be able to take a pixel and, based on its neighboring pixels, maybe predict some sort of valuable information. Something like taking a pixel and looking at its neighboring pixels, you might be able to predict whether or not there’s some sort of curve inside the image, or whether it’s forming the outline of a particular line or a shape, for example. And that might be useful if you’re trying to use all of these various different features to combine them to say something meaningful about an image as a whole. So how, then, does image convolution work? Well, we start with a kernel matrix. And the kernel matrix looks something like this. And the idea of this is that, given a pixel that will be the middle pixel, we’re going to multiply each of the neighboring pixels by these values in order to get some sort of result by summing up all the numbers together. So if I take this kernel, which you can think of as a filter that I’m going to apply to the image, and let’s say that I take this image. This is a 4 by 4 image. We’ll think of it as just a black and white image, where each one is just a single pixel value. So somewhere between 0 and 255, for example. So we have a whole bunch of individual pixel values like this. And what I’d like to do is apply this kernel, this filter, so to speak, to this image. And the way I’ll do that is, all right, the kernel is 3 by 3. You can imagine a 5 by 5 kernel or a larger kernel, too. And I’ll take it and just first apply it to the first 3 by 3 section of the image. And what I’ll do is I’ll take each of these pixel values, multiply it by its corresponding value in the filter matrix, and add all of the results together. So here, for example, I’ll say 10 times 0, plus 20 times negative 1, plus 30 times 0, so on and so forth, doing all of this calculation. And at the end, if I take all these values, multiply them by their corresponding value in the kernel, add the results together, for this particular set of 9 pixels, I get the value of 10, for example. And then what I’ll do is I’ll slide this 3 by 3 grid, effectively, over. I’ll slide the kernel by 1 to look at the next 3 by 3 section. Here, I’m just sliding it over by 1 pixel. But you might imagine a different stride length, or maybe I jump by multiple pixels at a time if you really wanted to. You have different options here. But here, I’m just sliding over, looking at the next 3 by 3 section. And I’ll do the same math, 20 times 0, plus 30 times negative 1, plus 40 times 0, plus 20 times negative 1, so on and so forth, plus 30 times 5. And what I end up getting is the number 20. Then you can imagine shifting over to this one, doing the same thing, calculating the number 40, for example, and then doing the same thing here, and calculating a value there as well. And so what we have now is what we’ll call a feature map. We have taken this kernel, applied it to each of these various different regions, and what we get is some representation of a filtered version of that image. And so to give a more concrete example of why it is that this kind of thing could be useful, let’s take this kernel matrix, for example, which is quite a famous one, that has an 8 in the middle, and then all of the neighboring pixels get a negative 1. And let’s imagine we wanted to apply that to a 3 by 3 part of an image that looks like this, where all the values are the same. They’re all 20, for instance. Well, in this case, if you do 20 times 8, and then subtract 20, subtract 20, subtract 20 for each of the eight neighbors, well, the result of that is you just get that expression, which comes out to be 0. You multiplied 20 by 8, but then you subtracted 20 eight times, according to that particular kernel. The result of all that is just 0. So the takeaway here is that when a lot of the pixels are the same value, we end up getting a value close to 0. If, though, we had something like this, 20 is along this first row, then 50 is in the second row, and 50 is in the third row, well, then when you do this, because it’s the same kind of math, 20 times negative 1, 20 times negative 1, so on and so forth, then I get a higher value, a value like 90 in this particular case. And so the more general idea here is that by applying this kernel, negative 1s, 8 in the middle, and then negative 1s, what I get is when this middle value is very different from the neighboring values, like 50 is greater than these 20s, then you’ll end up with a value higher than 0. If this number is higher than its neighbors, you end up getting a bigger output. But if this value is the same as all of its neighbors, then you get a lower output, something like 0. And it turns out that this sort of filter can therefore be used in something like detecting edges in an image. Or I want to detect the boundaries between various different objects inside of an image. I might use a filter like this, which is able to tell whether the value of this pixel is different from the values of the neighboring pixel, if it’s greater than the values of the pixels that happen to surround it. And so we can use this in terms of image filtering. And so I’ll show you an example of that. I have here in filter.py a file that uses Python’s image library, or PIL, to do some image filtering. I go ahead and open an image. And then all I’m going to do is apply a kernel to that image. It’s going to be a 3 by 3 kernel, same kind of kernel we saw before. And here is the kernel. This is just a list representation of the same matrix that I showed you a moment ago. It’s negative 1, negative 1, negative 1. The second row is negative 1, 8, negative 1. And the third row is all negative 1s. And then at the end, I’m going to go ahead and show the filtered image. So if, for example, I go into convolution directory and I open up an image, like bridge.png, this is what an input image might look like, just an image of a bridge over a river. Now I’m going to go ahead and run this filter program on the bridge. And what I get is this image here. Just by taking the original image and applying that filter to each 3 by 3 grid, I’ve extracted all of the boundaries, all of the edges inside the image that separate one part of the image from another. So here I’ve got a representation of boundaries between particular parts of the image. And you might imagine that if a machine learning algorithm is trying to learn what an image is of, a filter like this could be pretty useful. Maybe the machine learning algorithm doesn’t care about all of the details of the image. It just cares about certain useful features. It cares about particular shapes that are able to help it determine that based on the image, this is going to be a bridge, for example. And so this type of idea of image convolution can allow us to apply filters to images that allow us to extract useful results out of those images, taking an image and extracting its edges, for example. And you might imagine many other filters that could be applied to an image that are able to extract particular values as well. And a filter might have separate kernels for the red values, the green values, and the blue values that are all summed together at the end, such that you could have particular filters looking for, is there red in this part of the image? Are there green in other parts of the image? You can begin to assemble these relevant and useful filters that are able to do these calculations as well. So that then was the idea of image convolution, applying some sort of filter to an image to be able to extract some useful features out of that image. But all the while, these images are still pretty big. There’s a lot of pixels involved in the image. And realistically speaking, if you’ve got a really big image, that poses a couple of problems. One, it means a lot of input going into the neural network. But two, it also means that we really have to care about what’s in each particular pixel. Whereas realistically, we often, if you’re looking at an image, you don’t care whether something is in one particular pixel versus the pixel immediately to the right of it. They’re pretty close together. You really just care about whether there’s a particular feature in some region of the image. And maybe you don’t care about exactly which pixel it happens to be in. And so there’s a technique we can use known as pooling. And what pooling is, is it means reducing the size of an input by sampling from regions inside of the input. So we’re going to take a big image and turn it into a smaller image by using pooling. And in particular, one of the most popular types of pooling is called max pooling. And what max pooling does is it pools just by choosing the maximum value in a particular region. So for example, let’s imagine I had this 4 by 4 image. But I wanted to reduce its dimensions. I wanted to make it a smaller image so that I have fewer inputs to work with. Well, what I could do is I could apply a 2 by 2 max pool, where the idea would be that I’m going to first look at this 2 by 2 region and say, what is the maximum value in that region? Well, it’s the number 50. So we’ll go ahead and just use the number 50. And then we’ll look at this 2 by 2 region. What is the maximum value here? It’s 110, so that’s going to be my value. Likewise here, the maximum value looks like 20. Go ahead and put that there. Then for this last region, the maximum value was 40. So we’ll go ahead and use that. And what I have now is a smaller representation of this same original image that I obtained just by picking the maximum value from each of these regions. So again, the advantages here are now I only have to deal with a 2 by 2 input instead of a 4 by 4. And you can imagine shrinking the size of an image even more. But in addition to that, I’m now able to make my analysis independent of whether a particular value was in this pixel or this pixel. I don’t care if the 50 was here or here. As long as it was generally in this region, I’ll still get access to that value. So it makes our algorithms a little bit more robust as well. So that then is pooling, taking the size of the image, reducing it a little bit by just sampling from particular regions inside of the image. And now we can put all of these ideas together, pooling, image convolution, and neural networks all together into another type of neural network called a convolutional neural network, or a CNN, which is a neural network that uses this convolution step usually in the context of analyzing an image, for example. And so the way that a convolutional neural network works is that we start with some sort of input image, some grid of pixels. But rather than immediately put that into the neural network layers that we’ve seen before, we’ll start by applying a convolution step, where the convolution step involves applying some number of different image filters to our original image in order to get what we call a feature map, the result of applying some filter to an image. And we could do this once, but in general, we’ll do this multiple times, getting a whole bunch of different feature maps, each of which might extract some different relevant feature out of the image, some different important characteristic of the image that we might care about using in order to calculate what the result should be. And in the same way that when we train neural networks, we can train neural networks to learn the weights between particular units inside of the neural networks, we can also train neural networks to learn what those filters should be, what the values of the filters should be in order to get the most useful, most relevant information out of the original image just by figuring out what setting of those filter values, the values inside of that kernel, results in minimizing the loss function, minimizing how poorly our hypothesis actually performs in figuring out the classification of a particular image, for example. So we first apply this convolution step, get a whole bunch of these various different feature maps. But these feature maps are quite large. There’s a lot of pixel values that happen to be here. And so a logical next step to take is a pooling step, where we reduce the size of these images by using max pooling, for example, extracting the maximum value from any particular region. There are other pooling methods that exist as well, depending on the situation. You could use something like average pooling, where instead of taking the maximum value from a region, you take the average value from a region, which has its uses as well. But in effect, what pooling will do is it will take these feature maps and reduce their dimensions so that we end up with smaller grids with fewer pixels. And this then is going to be easier for us to deal with. It’s going to mean fewer inputs that we have to worry about. And it’s also going to mean we’re more resilient, more robust against potential movements of particular values, just by one pixel, when ultimately we really don’t care about those one-pixel differences that might arise in the original image. And now, after we’ve done this pooling step, now we have a whole bunch of values that we can then flatten out and just put into a more traditional neural network. So we go ahead and flatten it, and then we end up with a traditional neural network that has one input for each of these values in each of these resulting feature maps after we do the convolution and after we do the pooling step. And so this then is the general structure of a convolutional network. We begin with the image, apply convolution, apply pooling, flatten the results, and then put that into a more traditional neural network that might itself have hidden layers. You can have deep convolutional networks that have hidden layers in between this flattened layer and the eventual output to be able to calculate various different features of those values. But this then can help us to be able to use convolution and pooling to use our knowledge about the structure of an image to be able to get better results, to be able to train our networks faster in order to better capture particular parts of the image. And there’s no reason necessarily why you can only use these steps once. In fact, in practice, you’ll often use convolution and pooling multiple times in multiple different steps. See, what you might imagine doing is starting with an image, first applying convolution to get a whole bunch of maps, then applying pooling, then applying convolution again, because these maps are still pretty big. You can apply convolution to try and extract relevant features out of this result. Then take those results, apply pooling in order to reduce their dimensions, and then take that and feed it into a neural network that maybe has fewer inputs. So here I have two different convolution and pooling steps. I do convolution and pooling once, and then I do convolution and pooling a second time, each time extracting useful features from the layer before it, each time using pooling to reduce the dimensions of what you’re ultimately looking at. And the goal now of this sort of model is that in each of these steps, you can begin to learn different types of features of the original image. That maybe in the first step, you learn very low level features. Just learn and look for features like edges and curves and shapes, because based on pixels and their neighboring values, you can figure out, all right, what are the edges? What are the curves? What are the various different shapes that might be present there? But then once you have a mapping that just represents where the edges and curves and shapes happen to be, you can imagine applying the same sort of process again to begin to look for higher level features, look for objects, maybe look for people’s eyes and facial recognition, for example. Maybe look for more complex shapes like the curves on a particular number if you’re trying to recognize a digit in a handwriting recognition sort of scenario. And then after all of that, now that you have these results that represent these higher level features, you can pass them into a neural network, which is really just a deep neural network that looks like this, where you might imagine making a binary classification or classifying into multiple categories or performing various different tasks on this sort of model. So convolutional neural networks can be quite powerful and quite popular when it comes towards trying to analyze images. We don’t strictly need them. We could have just used a vanilla neural network that just operates with layer after layer, as we’ve seen before. But these convolutional neural networks can be quite helpful, in particular, because of the way they model the way a human might look at an image, that instead of a human looking at every single pixel simultaneously and trying to convolve all of them by multiplying them together, you might imagine that what convolution is really doing is looking at various different regions of the image and extracting relevant information and features out of those parts of the image, the same way that a human might have visual receptors that are looking at particular parts of what they see and using those combining them to figure out what meaning they can draw from all of those various different inputs. And so you might imagine applying this to a situation like handwriting recognition. So we’ll go ahead and see an example of that now, where I’ll go ahead and open up handwriting.py. Again, what we do here is we first import TensorFlow. And then TensorFlow, it turns out, has a few data sets that are built into the library that you can just immediately access. And one of the most famous data sets in machine learning is the MNIST data set, which is just a data set of a whole bunch of samples of people’s handwritten digits. I showed you a slide of that a little while ago. And what we can do is just immediately access that data set which is built into the library so that if I want to do something like train on a whole bunch of handwritten digits, I can just use the data set that is provided to me. Of course, if I had my own data set of handwritten images, I can apply the same idea. I’d first just need to take those images and turn them into an array of pixels, because that’s the way that these are going to be formatted. They’re going to be formatted as, effectively, an array of individual pixels. Now there’s a bit of reshaping I need to do, just turning the data into a format that I can put into my convolutional neural network. So this is doing things like taking all the values and dividing them by 255. If you remember, these color values tend to range from 0 to 255. So I can divide them by 255 just to put them into 0 to 1 range, which might be a little bit easier to train on. And then doing various other modifications to the data just to get it into a nice usable format. But here’s the interesting and important part. Here is where I create the convolutional neural network, the CNN, where here I’m saying, go ahead and use a sequential model. And before I could use model.add to say add a layer, add a layer, add a layer, another way I could define it is just by passing as input to this sequential neural network a list of all of the layers that I want. And so here, the very first layer in my model is a convolution layer, where I’m first going to apply convolution to my image. I’m going to use 13 different filters. So my model is going to learn 32, rather, 32 different filters that I would like to learn on the input image, where each filter is going to be a 3 by 3 kernel. So we saw those 3 by 3 kernels before, where we could multiply each value in a 3 by 3 grid by a value, multiply it, and add all the results together. So here, I’m going to learn 32 different of these 3 by 3 filters. I can, again, specify my activation function. And I specify what my input shape is. My input shape in the banknotes case was just 4. I had 4 inputs. My input shape here is going to be 28, 28, 1, because for each of these handwritten digits, it turns out that the MNIST data set organizes their data. Each image is a 28 by 28 pixel grid. So we’re going to have a 28 by 28 pixel grid. And each one of those images only has one channel value. These handwritten digits are just black and white. So there’s just a single color value representing how much black or how much white. You might imagine that in a color image, if you were doing this sort of thing, you might have three different channels, a red, a green, and a blue channel, for example. But in the case of just handwriting recognition, recognizing a digit, we’re just going to use a single value for, like, shaded in or not shaded in. And it might range, but it’s just a single color value. And that, then, is the very first layer of our neural network, a convolutional layer that will take the input and learn a whole bunch of different filters that we can apply to the input to extract meaningful features. Next step is going to be a max pooling layer, also built right into TensorFlow, where this is going to be a layer that is going to use a pool size of 2 by 2, meaning we’re going to look at 2 by 2 regions inside of the image and just extract the maximum value. Again, we’ve seen why this can be helpful. It’ll help to reduce the size of our input. And once we’ve done that, we’ll go ahead and flatten all of the units just into a single layer that we can then pass into the rest of the neural network. And now, here’s the rest of the neural network. Here, I’m saying, let’s add a hidden layer to my neural network with 128 units, so a whole bunch of hidden units inside of the hidden layer. And just to prevent overfitting, I can add a dropout to that. Say, you know what, when you’re training, randomly dropout half of the nodes from this hidden layer just to make sure we don’t become over-reliant on any particular node, we begin to really generalize and stop ourselves from overfitting. So TensorFlow allows us, just by adding a single line, to add dropout into our model as well, such that when it’s training, it will perform this dropout step in order to help make sure that we don’t overfit on this particular data. And then finally, I add an output layer. The output layer is going to have 10 units, one for each category that I would like to classify digits into, so 0 through 9, 10 different categories. And the activation function I’m going to use here is called the softmax activation function. And in short, what the softmax activation function is going to do is it’s going to take the output and turn it into a probability distribution. So ultimately, it’s going to tell me, what did we estimate the probability is that this is a 2 versus a 3 versus a 4. And so it will turn it into that probability distribution for me. Next up, I’ll go ahead and compile my model and fit it on all of my training data. And then I can evaluate how well the neural network performs. And then I’ve added to my Python program, if I’ve provided a command line argument like the name of a file, I’m going to go ahead and save the model to a file. And so this can be quite useful too. Once you’ve done the training step, which could take some time in terms of taking all the time, going through the data, running back propagation with gradient descent to be able to say, all right, how should we adjust the weight to this particular model? You end up calculating values for these weights, calculating values for these filters. You’d like to remember that information so you can use it later. And so TensorFlow allows us to just save a model to a file, such that later, if we want to use the model we’ve learned, use the weights that we’ve learned to make some sort of new prediction, we can just use the model that already exists. So what we’re doing here is after we’ve done all the calculation, we go ahead and save the model to a file, such that we can use it a little bit later. So for example, if I go into digits, I’m going to run handwriting.py. I won’t save it this time. We’ll just run it and go ahead and see what happens. What will happen is we need to go through the model in order to train on all of these samples of handwritten digits. The MNIST data set gives us thousands and thousands of sample handwritten digits in the same format that we can use in order to train. And so now what you’re seeing is this training process. And unlike the banknotes case, where there was much fewer data points, the data was very, very simple, here this data is more complex and this training process takes time. And so this is another one of those cases where when training neural networks, this is why computational power is so important that oftentimes you see people wanting to use sophisticated GPUs in order to more efficiently be able to do this sort of neural network training. It also speaks to the reason why more data can be helpful. The more sample data points you have, the better you can begin to do this training. So here we’re going through 60,000 different samples of handwritten digits. And I said we’re going to go through them 10 times. We’re going to go through the data set 10 times, training each time, hopefully improving upon our weights with every time we run through this data set. And we can see over here on the right what the accuracy is each time we go ahead and run this model, that the first time it looks like we got an accuracy of about 92% of the digits correct based on this training set. We increased that to 96% or 97%. And every time we run this, we’re going to see hopefully the accuracy improve as we continue to try and use that gradient descent, that process of trying to run the algorithm, to minimize the loss that we get in order to more accurately predict what the output should be. And what this process is doing is it’s learning not only the weights, but it’s learning the features to use, the kernel matrix to use when performing that convolution step. Because this is a convolutional neural network, where I’m first performing those convolutions and then doing the more traditional neural network structure, this is going to learn all of those individual steps as well. And so here we see the TensorFlow provides me with some very nice output, telling me about how many seconds are left with each of these training runs that allows me to see just how well we’re doing. So we’ll go ahead and see how this network performs. It looks like we’ve gone through the data set seven times. We’re going through it an eighth time now. And at this point, the accuracy is pretty high. We saw we went from 92% up to 97%. Now it looks like 98%. And at this point, it seems like things are starting to level out. It’s probably a limit to how accurate we can ultimately be without running the risk of overfitting. Of course, with enough nodes, you would just memorize the input and overfit upon them. But we’d like to avoid doing that. And Dropout will help us with this. But now we see we’re almost done finishing our training step. We’re at 55,000. All right, we finished training. And now it’s going to go ahead and test for us on 10,000 samples. And it looks like on the testing set, we were at 98.8% accurate. So we ended up doing pretty well, it seems, on this testing set to see how accurately can we predict these handwritten digits. And so what we could do then is actually test it out. I’ve written a program called Recognition.py using PyGame. If you pass it a model that’s been trained, and I pre-trained an example model using this input data, what we can do is see whether or not we’ve been able to train this convolutional neural network to be able to predict handwriting, for example. So I can try, just like drawing a handwritten digit. I’ll go ahead and draw the number 2, for example. So there’s my number 2. Again, this is messy. If you tried to imagine, how would you write a program with just ifs and thens to be able to do this sort of calculation, it would be tricky to do so. But here I’ll press Classify, and all right, it seems I was able to correctly classify that what I drew was the number 2. I’ll go ahead and reset it, try it again. We’ll draw an 8, for example. So here is an 8. Press Classify. And all right, it predicts that the digit that I drew was an 8. And the key here is this really begins to show the power of what the neural network is doing, somehow looking at various different features of these different pixels, figuring out what the relevant features are, and figuring out how to combine them to get a classification. And this would be a difficult task to provide explicit instructions to the computer on how to do, to use a whole bunch of ifs ands to process all these pixel values to figure out what the handwritten digit is. Everyone’s going to draw their 8s a little bit differently. If I drew the 8 again, it would look a little bit different. And yet, ideally, we want to train a network to be robust enough so that it begins to learn these patterns on its own. All I said was, here is the structure of the network, and here is the data on which to train the network. And the network learning algorithm just tries to figure out what is the optimal set of weights, what is the optimal set of filters to use them in order to be able to accurately classify a digit into one category or another. Just going to show the power of these sorts of convolutional neural networks. And so that then was a look at how we can use convolutional neural networks to begin to solve problems with regards to computer vision, the ability to take an image and begin to analyze it. So this is the type of analysis you might imagine that’s happening in self-driving cars that are able to figure out what filters to apply to an image to understand what it is that the computer is looking at, or the same type of idea that might be applied to facial recognition and social media to be able to determine how to recognize faces in an image as well. You can imagine a neural network that instead of classifying into one of 10 different digits could instead classify like, is this person A or is this person B, trying to tell those people apart just based on convolution. And so now what we’ll take a look at is yet another type of neural network that can be quite popular for certain types of tasks. But to do so, we’ll try to generalize and think about our neural network a little bit more abstractly. That here we have a sample deep neural network where we have this input layer, a whole bunch of different hidden layers that are performing certain types of calculations, and then an output layer here that just generates some sort of output that we care about calculating. But we could imagine representing this a little more simply like this. Here is just a more abstract representation of our neural network. We have some input that might be like a vector of a whole bunch of different values as our input. That gets passed into a network that performs some sort of calculation or computation, and that network produces some sort of output. That output might be a single value. It might be a whole bunch of different values. But this is the general structure of the neural network that we’ve seen. There is some sort of input that gets fed into the network. And using that input, the network calculates what the output should be. And this sort of model for a neural network is what we might call a feed-forward neural network. Feed-forward neural networks have connections only in one direction. They move from one layer to the next layer to the layer after that, such that the inputs pass through various different hidden layers and then ultimately produce some sort of output. So feed-forward neural networks were very helpful for solving these types of classification problems that we saw before. We have a whole bunch of input. We want to learn what setting of weights will allow us to calculate the output effectively. But there are some limitations on feed-forward neural networks that we’ll see in a moment. In particular, the input needs to be of a fixed shape, like a fixed number of neurons are in the input layer. And there’s a fixed shape for the output, like a fixed number of neurons in the output layer. And that has some limitations of its own. And a possible solution to this, and we’ll see examples of the types of problems we can solve for this in just a second, is instead of just a feed-forward neural network, where there are only connections in one direction from left to right effectively across the network, we could also imagine a recurrent neural network, where a recurrent neural network generates output that gets fed back into itself as input for future runs of that network. So whereas in a traditional neural network, we have inputs that get fed into the network, that get fed into the output. And the only thing that determines the output is based on the original input and based on the calculation we do inside of the network itself. This goes in contrast with a recurrent neural network, where in a recurrent neural network, you can imagine output from the network feeding back to itself into the network again as input for the next time you do the calculations inside of the network. What this allows is it allows the network to maintain some sort of state, to store some sort of information that can be used on future runs of the network. Previously, the network just defined some weights, and we passed inputs through the network, and it generated outputs. But the network wasn’t saving any information based on those inputs to be able to remember for future iterations or for future runs. What a recurrent neural network will let us do is let the network store information that gets passed back in as input to the network again the next time we try and perform some sort of action. And this is particularly helpful when dealing with sequences of data. So we’ll see a real world example of this right now, actually. Microsoft has developed an AI known as the caption bot. And what the caption bot does is it says, I can understand the content of any photograph, and I’ll try to describe it as well as any human. I’ll analyze your photo, but I won’t store it or share it. And so what Microsoft’s caption bot seems to be claiming to do is it can take an image and figure out what’s in the image and just give us a caption to describe it. So let’s try it out. Here, for example, is an image of Harvard Square. It’s some people walking in front of one of the buildings at Harvard Square. I’ll go ahead and take the URL for that image, and I’ll paste it into caption bot and just press Go. So caption bot is analyzing the image, and then it says, I think it’s a group of people walking in front of a building, which seems amazing. The AI is able to look at this image and figure out what’s in the image. And the important thing to recognize here is that this is no longer just a classification task. We saw being able to classify images with a convolutional neural network where the job was take the image and then figure out, is it a 0 or a 1 or a 2, or is it this person’s face or that person’s face? What seems to be happening here is the input is an image, and we know how to get networks to take input of images, but the output is text. It’s a sentence. It’s a phrase, like a group of people walking in front of a building. And this would seem to pose a challenge for our more traditional feed-forward neural networks, for the reason being that in traditional neural networks, we just have a fixed-size input and a fixed-size output. There are a certain number of neurons in the input to our neural network and a certain number of outputs for our neural network, and then some calculation that goes on in between. But the size of the inputs and the number of values in the input and the number of values in the output, those are always going to be fixed based on the structure of the neural network. And that makes it difficult to imagine how a neural network could take an image like this and say it’s a group of people walking in front of the building because the output is text, like it’s a sequence of words. Now, it might be possible for a neural network to output one word, one word you could represent as a vector of values, and you can imagine ways of doing that. Next time, we’ll talk a little bit more about AI as it relates to language and language processing. But a sequence of words is much more challenging because depending on the image, you might imagine the output is a different number of words. We could have sequences of different lengths, and somehow we still want to be able to generate the appropriate output. And so the strategy here is to use a recurrent neural network, a neural network that can feed its own output back into itself as input for the next time. And this allows us to do what we call a one-to-many relationship for inputs to outputs, that in vanilla, more traditional neural networks, these are what we might consider to be one-to-one neural networks. You pass in one set of values as input. You get one vector of values as the output. But in this case, we want to pass in one value as input, the image, and we want to get a sequence, many values as output, where each value is like one of these words that gets produced by this particular algorithm. And so the way we might do this is we might imagine starting by providing input, the image, into our neural network. And the neural network is going to generate output, but the output is not going to be the whole sequence of words, because we can’t represent the whole sequence of words using just a fixed set of neurons. Instead, the output is just going to be the first word. We’re going to train the network to output what the first word of the caption should be. And you could imagine that Microsoft has trained this by running a whole bunch of training samples through the AI, giving it a whole bunch of pictures and what the appropriate caption was, and having the AI begin to learn from that. But now, because the network generates output that can be fed back into itself, you could imagine the output of the network being fed back into the same network. This here looks like a separate network, but it’s really the same network that’s just getting different input, that this network’s output gets fed back into itself, but it’s going to generate another output. And that other output is going to be the second word in the caption. And this recurrent neural network then, this network is going to generate other output that can be fed back into itself to generate yet another word, fed back into itself to generate another word. And so recurrent neural networks allow us to represent this one-to-many structure. You provide one image as input, and the neural network can pass data into the next run of the network, and then again and again, such that you could run the network multiple times, each time generating a different output still based on that original input. And this is where recurrent neural networks become particularly useful when dealing with sequences of inputs or outputs. And my output is a sequence of words, and since I can’t very easily represent outputting an entire sequence of words, I’ll instead output that sequence one word at a time by allowing my network to pass information about what still needs to be said about the photo into the next stage of running the network. So you could run the network multiple times, the same network with the same weights, just getting different input each time. First, getting input from the image, and then getting input from the network itself as additional information about what additionally needs to be given in a particular caption, for example. So this then is a one-to-many relationship inside of a recurrent neural network, but it turns out there are other models that we can use, other ways we can try and use recurrent neural networks to be able to represent data that might be stored in other forms as well. We saw how we could use neural networks in order to analyze images in the context of convolutional neural networks that take an image, figure out various different properties of the image, and are able to draw some sort of conclusion based on that. But you might imagine that something like YouTube, they need to be able to do a lot of learning based on video. They need to look through videos to detect if they’re like copyright violations, or they need to be able to look through videos to maybe identify what particular items are inside of the video, for example. And video, you might imagine, is much more difficult to put in as input to a neural network, because whereas an image, you could just treat each pixel as a different value, videos are sequences. They’re sequences of images, and each sequence might be of different length. And so it might be challenging to represent that entire video as a single vector of values that you could pass in to a neural network. And so here, too, recurrent neural networks can be a valuable solution for trying to solve this type of problem. Then instead of just passing in a single input into our neural network, we could pass in the input one frame at a time, you might imagine. First, taking the first frame of the video, passing it into the network, and then maybe not having the network output anything at all yet. Let it take in another input, and this time, pass it into the network. But the network gets information from the last time we provided an input into the network. Then we pass in a third input, and then a fourth input, where each time, what the network gets is it gets the most recent input, like each frame of the video. But it also gets information the network processed from all of the previous iterations. So on frame number four, you end up getting the input for frame number four plus information the network has calculated from the first three frames. And using all of that data combined, this recurrent neural network can begin to learn how to extract patterns from a sequence of data as well. And so you might imagine, if you want to classify a video into a number of different genres, like an educational video, or a music video, or different types of videos, that’s a classification task, where you want to take as input each of the frames of the video, and you want to output something like what it is, what category that it happens to belong to. And you can imagine doing this sort of thing, this sort of many-to-one learning, any time your input is a sequence. And so input is a sequence in the context of video. It could be in the context of, like, if someone has typed a message and you want to be able to categorize that message, like if you’re trying to take a movie review and trying to classify it as, is it a positive review or a negative review? That input is a sequence of words, and the output is a classification, positive or negative. There, too, a recurrent neural network might be helpful for analyzing sequences of words. And they’re quite popular when it comes to dealing with language. Could even be used for spoken language as well, that spoken language is an audio waveform that can be segmented into distinct chunks. And each of those could be passed in as an input into a recurrent neural network to be able to classify someone’s voice, for instance. If you want to do voice recognition to say, is this one person or is this another, here are also cases where you might want this many-to-one architecture for a recurrent neural network. And then as one final problem, just to take a look at in terms of what we can do with these sorts of networks, imagine what Google Translate is doing. So what Google Translate is doing is it’s taking some text written in one language and converting it into text written in some other language, for example, where now this input is a sequence of data. It’s a sequence of words. And the output is a sequence of words as well. It’s also a sequence. So here we want effectively a many-to-many relationship. Our input is a sequence and our output is a sequence as well. And it’s not quite going to work to just say, take each word in the input and translate it into a word in the output. Because ultimately, different languages put their words in different orders. And maybe one language uses two words for something, whereas another language only uses one. So we really want some way to take this information, this input, encode it somehow, and use that encoding to generate what the output ultimately should be. And this has been one of the big advancements in automated translation technology, is the ability to use the neural networks to do this instead of older, more traditional methods. And this has improved accuracy dramatically. And the way you might imagine doing this is, again, using a recurrent neural network with multiple inputs and multiple outputs. We start by passing in all the input. Input goes into the network. Another input, like another word, goes into the network. And we do this multiple times, like once for each word in the input that I’m trying to translate. And only after all of that is done does the network now start to generate output, like the first word of the translated sentence, and the next word of the translated sentence, so on and so forth, where each time the network passes information to itself by allowing for this model of giving some sort of state from one run in the network to the next run, assembling information about all the inputs, and then passing in information about which part of the output in order to generate next. And there are a number of different types of these sorts of recurrent neural networks. One of the most popular is known as the long short-term memory neural network, otherwise known as LSTM. But in general, these types of networks can be very, very powerful whenever we’re dealing with sequences, whether those are sequences of images or especially sequences of words when it comes towards dealing with natural language. And so that then were just some of the different types of neural networks that can be used to do all sorts of different computations. And these are incredibly versatile tools that can be applied to a number of different domains. We only looked at a couple of the most popular types of neural networks from more traditional feed-forward neural networks, convolutional neural networks, and recurrent neural networks. But there are other types as well. There are adversarial networks where networks compete with each other to try and be able to generate new types of data, as well as other networks that can solve other tasks based on what they happen to be structured and adapted for. And these are very powerful tools in machine learning from being able to very easily learn based on some set of input data and to be able to, therefore, figure out how to calculate some function from inputs to outputs, whether it’s input to some sort of classification like analyzing an image and getting a digit or machine translation where the input is in one language and the output is in another. These tools have a lot of applications for machine learning more generally. Next time, we’ll look at machine learning and AI in particular in the context of natural language. We talked a little bit about this today, but looking at how it is that our AI can begin to understand natural language and can begin to be able to analyze and do useful tasks with regards to human language, which turns out to be a challenging and interesting task. So we’ll see you next time. And welcome back, everybody, to our final class in an introduction to artificial intelligence with Python. Now, so far in this class, we’ve been taking problems that we want to solve intelligently and framing them in ways that computers are going to be able to make sense of. We’ve been taking problems and framing them as search problems or constraint satisfaction problems or optimization problems, for example. In essence, we have been trying to communicate about problems in ways that our computer is going to be able to understand. Today, the goal is going to be to get computers to understand the way you and I communicate naturally via our own natural languages, languages like English. But natural language contains a lot of nuance and complexity that’s going to make it challenging for computers to be able to understand. So we’ll need to explore some new tools and some new techniques to allow computers to make sense of natural language. So what is it exactly that we’re trying to get computers to do? Well, they all fall under this general heading of natural language processing, getting computers to work with natural language. And these tasks include tasks like automatic summarization. Given a long text, can we train the computer to be able to come up with a shorter representation of it? Information extraction, getting the computer to pull out relevant facts or details out of some text. Machine translation, like Google Translate, translating some text from one language into another language. Question answering, if you’ve ever asked a question to your phone or had a conversation with an AI chatbot where you provide some text to the computer, the computer is able to understand that text and then generate some text in response. Text classification, where we provide some text to the computer and the computer assigns it a label, positive or negative, inbox or spam, for example. And there are several other kinds of tasks that all fall under this heading of natural language processing. But before we take a look at how the computer might try to solve these kinds of tasks, it might be useful for us to think about language in general. What are the kinds of challenges that we might need to deal with as we start to think about language and getting a computer to be able to understand it? So one part of language that we’ll need to consider is the syntax of language. Syntax is all about the structure of language. Language is composed of individual words. And those words are composed together in some kind of structured whole. And if our computer is going to be able to understand language, it’s going to need to understand something about that structure. So let’s take a couple of examples. Here, for instance, is a sentence. Just before 9 o’clock, Sherlock Holmes stepped briskly into the room. That sentence is made up of words. And those words together form a structured whole. This is syntactically valid as a sentence. But we could take some of those same words, rearrange them, and come up with a sentence that is not syntactically valid. Here, for example, just before Sherlock Holmes 9 o’clock stepped briskly the room is still composed of valid words. But they’re not in any kind of logical whole. This is not a syntactically well-formed sentence. Another interesting challenge is that some sentences will have multiple possible valid structures. Here’s a sentence, for example. I saw the man on the mountain with a telescope. And here, this is a valid sentence. But it actually has two different possible structures that lend themselves to two different interpretations and two different meanings. Maybe I, the one doing the seeing, am the one with the telescope. Or maybe the man on the mountain is the one with the telescope. And so natural language is ambiguous. Sometimes the same sentence can be interpreted in multiple ways. And that’s something that we’ll need to think about as well. And this lends itself to another problem within language that we’ll need to think about, which is semantics. While syntax is all about the structure of language, semantics is about the meaning of language. It’s not enough for a computer just to know that a sentence is well-structured if it doesn’t know what that sentence means. And so semantics is going to concern itself with the meaning of words and the meaning of sentences. So if we go back to that same sentence as before, just before 9 o’clock, Sherlock Holmes stepped briskly into the room, I could come up with another sentence, say the sentence, a few minutes before 9, Sherlock Holmes walked quickly into the room. And those are two different sentences with some of the words the same and some of the words different. But the two sentences have essentially the same meaning. And so ideally, whatever model we build, we’ll be able to understand that these two sentences, while different, mean something very similar. Some syntactically well-formed sentences don’t mean anything at all. A famous example from linguist Noam Chomsky is the sentence, colorless green ideas sleep furiously. This is a syntactically, structurally well-formed sentence. We’ve got adjectives modifying a noun, ideas. We’ve got a verb and an adverb in the correct positions. But when taken as a whole, the sentence doesn’t really mean anything. And so if our computers are going to be able to work with natural language and perform tasks in natural language processing, these are some concerns we’ll need to think about. We’ll need to be thinking about syntax. And we’ll need to be thinking about semantics. So how could we go about trying to teach a computer how to understand the structure of natural language? Well, one approach we might take is by starting by thinking about the rules of natural language. Our natural languages have rules. In English, for example, nouns tend to come before verbs. Nouns can be modified by adjectives, for example. And so if only we could formalize those rules, then we could give those rules to a computer, and the computer would be able to make sense of them and understand them. And so let’s try to do exactly that. We’re going to try to define a formal grammar. Where a formal grammar is some system of rules for generating sentences in a language. This is going to be a rule-based approach to natural language processing. We’re going to give the computer some rules that we know about language and have the computer use those rules to make sense of the structure of language. And there are a number of different types of formal grammars. Each one of them has slightly different use cases. But today, we’re going to focus specifically on one kind of grammar known as a context-free grammar. So how does the context-free grammar work? Well, here is a sentence that we might want a computer to generate. She saw the city. And we’re going to call each of these words a terminal symbol. A terminal symbol, because once our computer has generated the word, there’s nothing else for it to generate. Once it’s generated the sentence, the computer is done. We’re going to associate each of these terminal symbols with a non-terminal symbol that generates it. So here we’ve got n, which stands for noun, like she or city. We’ve got v as a non-terminal symbol, which stands for a verb. And then we have d, which stands for determiner. A determiner is a word like the or a or an in English, for example. So each of these non-terminal symbols can generate the terminal symbols that we ultimately care about generating. But how do we know, or how does the computer know which non-terminal symbols are associated with which terminal symbols? Well, to do that, we need some kind of rule. Here are some what we call rewriting rules that have a non-terminal symbol on the left-hand side of an arrow. And on the right side is what that non-terminal symbol can be replaced with. So here we’re saying the non-terminal symbol n, again, which stands for noun, could be replaced by any of these options separated by vertical bars. n could be replaced by she or city or car or hairy. d for determiner could be replaced by the a or an and so forth. Each of these non-terminal symbols could be replaced by any of these words. We can also have non-terminal symbols that are replaced by other non-terminal symbols. Here is an interesting rule, np arrow n bar dn. So what does that mean? Well, np stands for a noun phrase. Sometimes when we have a noun phrase in a sentence, it’s not just a single word, it could be multiple words. And so here we’re saying a noun phrase could be just a noun, or it could be a determiner followed by a noun. So we might have a noun phrase that’s just a noun, like she, that’s a noun phrase. Or we could have a noun phrase that’s multiple words, something like the city also acts as a noun phrase. But in this case, it’s composed of two words, a determiner, the, and a noun city. We could do the same for verb phrases. A verb phrase, or VP, might be just a verb, or it might be a verb followed by a noun phrase. So we could have a verb phrase that’s just a single word, like the word walked, or we could have a verb phrase that is an entire phrase, something like saw the city, as an entire verb phrase. A sentence, meanwhile, we might then define as a noun phrase followed by a verb phrase. And so this would allow us to generate a sentence like she saw the city, an entire sentence made up of a noun phrase, which is just the word she, and then a verb phrase, which is saw the city, saw which is a verb, and then the city, which itself is also a noun phrase. And so if we could give these rules to a computer explaining to it what non-terminal symbols could be replaced by what other symbols, then a computer could take a sentence and begin to understand the structure of that sentence. And so let’s take a look at an example of how we might do that. And to do that, we’re going to use a Python library called NLTK, or the Natural Language Toolkit, which we’ll see a couple of times today. It contains a lot of helpful features and functions that we can use for trying to deal with and process natural language. So here we’ll take a look at how we can use NLTK in order to parse a context-free grammar. So let’s go ahead and open up cfg0.py, cfg standing for context-free grammar. And what you’ll see in this file is that I first import NLTK, the Natural Language Toolkit. And the first thing I do is define a context-free grammar, saying that a sentence is a noun phrase followed by a verb phrase. I’m defining what a noun phrase is, defining what a verb phrase is, and then giving some examples of what I can do with these non-terminal symbols, D for determiner, N for noun, and V for verb. We’re going to use NLTK to parse that grammar. Then we’ll ask the user for some input in the form of a sentence and split it into words. And then we’ll use this context-free grammar parser to try to parse that sentence and print out the resulting syntax tree. So let’s take a look at an example. We’ll go ahead and go into my cfg directory, and we’ll run cfg0.py. And here I’m asked to type in a sentence. Let’s say I type in she walked. And when I do that, I see that she walked is a valid sentence, where she is a noun phrase, and walked is the corresponding verb phrase. I could try to do this with a more complex sentence too. I could do something like she saw the city. And here we see that she is the noun phrase, and then saw the city is the entire verb phrase that makes up this sentence. So that was a very simple grammar. Let’s take a look at a slightly more complex grammar. Here is cfg1.py, where a sentence is still a noun phrase followed by a verb phrase, but I’ve added some other possible non-terminal symbols too. I have AP for adjective phrase and PP for prepositional phrase. And we specified that we could have an adjective phrase before a noun phrase or a prepositional phrase after a noun, for example. So lots of additional ways that we might try to structure a sentence and interpret and parse one of those resulting sentences. So let’s see that one in action. We’ll go ahead and run cfg1.py with this new grammar. And we’ll try a sentence like she saw the wide street. Here, Python’s NLTK is able to parse that sentence and identify that she saw the wide street has this particular structure, a sentence with a noun phrase and a verb phrase, where that verb phrase has a noun phrase that within it contains an adjective. And so it’s able to get some sense for what the structure of this language actually is. Let’s try another example. Let’s say she saw the dog with the binoculars. And we’ll try that sentence. And here, we get one possible syntax tree, she saw the dog with the binoculars. But notice that this sentence is actually a little bit ambiguous in our own natural language. Who has the binoculars? Is it she who has the binoculars or the dog who has the binoculars? And NLTK is able to identify both possible structures for the sentence. In this case, the dog with the binoculars is an entire noun phrase. It’s all underneath this NP here. So it’s the dog that has the binoculars. But we also got an alternative parse tree, where the dog is just the noun phrase. And with the binoculars is a prepositional phrase modifying saw. So she saw the dog and she used the binoculars in order to see the dog as well. So this allows us to get a sense for the structure of natural language. But it relies on us writing all of these rules. And it would take a lot of effort to write all of the rules for any possible sentence that someone might write or say in the English language. Language is complicated. And as a result, there are going to be some very complex rules. So what else might we try? We might try to take a statistical lens towards approaching this problem of natural language processing. If we were able to give the computer a lot of existing data of sentences written in the English language, what could we try to learn from that data? Well, it might be difficult to try and interpret long pieces of text all at once. So instead, what we might want to do is break up that longer text into smaller pieces of information instead. In particular, we might try to create n-grams out of a longer sequence of text. An n-gram is just some contiguous sequence of n items from a sample of text. It might be n characters in a row or n words in a row, for example. So let’s take a passage from Sherlock Holmes. And let’s look for all of the trigrams. A trigram is an n-gram where n is equal to 3. So in this case, we’re looking for sequences of three words in a row. So the trigrams here would be phrases like how often have. That’s three words in a row. Often have I is another trigram. Have I said, I said to, said to you, to you that. These are all trigrams, sequences of three words that appear in sequence. And if we could give the computer a large corpus of text and have it pull out all of the trigrams in this case, it could get a sense for what sequences of three words tend to appear next to each other in our own natural language and, as a result, get some sense for what the structure of the language actually is. So let’s take a look at an example of that. How can we use NLTK to try to get access to information about n-grams? So here, we’re going to open up ngrams.py. And this is a Python program that’s going to load a corpus of data, just some text files, into our computer’s memory. And then we’re going to use NLTK’s ngrams function, which is going to go through the corpus of text, pulling out all of the ngrams for a particular value of n. And then, by using Python’s counter class, we’re going to figure out what are the most common ngrams inside of this entire corpus of text. And we’re going to need a data set in order to do this. And I’ve prepared a data set of some of the stories of Sherlock Holmes. So it’s just a bunch of text files. A lot of words for it to analyze. And as a result, we’ll get a sense for what sequences of two words or three words that tend to be most common in natural language. So let’s give this a try. We’ll go into my ngrams directory. And we’ll run ngrams.py. We’ll try an n value of 2. So we’re looking for sequences of two words in a row. And we’ll use our corpus of stories from Sherlock Holmes. And when we run this program, we get a list of the most common ngrams where n is equal to 2, otherwise known as a bigram. So the most common one is of the. That’s a sequence of two words that appears quite frequently in natural language. Then in the. And it was. These are all common sequences of two words that appear in a row. Let’s instead now try running ngrams with n equal to 3. Let’s get all of the trigrams and see what we get. And now we see the most common trigrams are it was a. One of the. I think that. These are all sequences of three words that appear quite frequently. And we were able to do this essentially via a process known as tokenization. Tokenization is the process of splitting a sequence of characters into pieces. In this case, we’re splitting a long sequence of text into individual words and then looking at sequences of those words to get a sense for the structure of natural language. So once we’ve done this, once we’ve done the tokenization, once we’ve built up our corpus of ngrams, what can we do with that information? So the one thing that we might try is we could build a Markov chain, which you might recall from when we talked about probability. Recall that a Markov chain is some sequence of values where we can predict one value based on the values that came before it. And as a result, if we know all of the common ngrams in the English language, what words tend to be associated with what other words in sequence, we can use that to predict what word might come next in a sequence of words. And so we could build a Markov chain for language in order to try to generate natural language that follows the same statistical patterns as some input data. So let’s take a look at that and build a Markov chain for natural language. And as input, I’m going to use the works of William Shakespeare. So here I have a file Shakespeare.txt, which is just a bunch of the works of William Shakespeare. It’s a long text file, so plenty of data to analyze. And here in generator.py, I’m using a third party Python library in order to do this analysis. We’re going to read in the sample of text, and then we’re going to train a Markov model based on that text. And then we’re going to have the Markov chain generate some sentences. We’re going to generate a sentence that doesn’t appear in the original text, but that follows the same statistical patterns that’s generating it based on the ngrams trying to predict what word is likely to come next that we would expect based on those statistical patterns. So we’ll go ahead and go into our Markov directory, run this generator with the works of William Shakespeare’s input. And what we’re going to get are five new sentences, where these sentences are not necessarily sentences from the original input text itself, but just that follow the same statistical patterns. It’s predicting what word is likely to come next based on the input data that we’ve seen and the types of words that tend to appear in sequence there too. And so we’re able to generate these sentences. Of course, so far, there’s no guarantee that any of the sentences that are generated actually mean anything or make any sense. They just happen to follow the statistical patterns that our computer is already aware of. So we’ll return to this issue of how to generate text in perhaps a more accurate or more meaningful way a little bit later. So let’s now turn our attention to a slightly different problem, and that’s the problem of text classification. Text classification is the problem where we have some text and we want to put that text into some kind of category. We want to apply some sort of label to that text. And this kind of problem shows up in a wide variety of places. A commonplace might be your email inbox, for example. You get an email and you want your computer to be able to identify whether the email belongs in your inbox or whether it should be filtered out into spam. So we need to classify the text. Is it a good email or is it spam? Another common use case is sentiment analysis. We might want to know whether the sentiment of some text is positive or negative. And so how might we do that? This comes up in situations like product reviews, where we might have a bunch of reviews for a product on some website. My grandson loved it so much fun. Product broke after a few days. One of the best games I’ve played in a long time and kind of cheap and flimsy, not worth it. Here’s some example sentences that you might see on a product review website. And you and I could pretty easily look at this list of product reviews and decide which ones are positive and which ones are negative. We might say the first one and the third one, those seem like positive sentiment messages. But the second one and the fourth one seem like negative sentiment messages. But how did we know that? And how could we train a computer to be able to figure that out as well? Well, you might have clued your eye in on particular key words, where those particular words tend to mean something positive or negative. So you might have identified words like loved and fun and best tend to be associated with positive messages. And words like broke and cheap and flimsy tend to be associated with negative messages. So if only we could train a computer to be able to learn what words tend to be associated with positive versus negative messages, then maybe we could train a computer to do this kind of sentiment analysis as well. So we’re going to try to do just that. We’re going to use a model known as the bag of words model, which is a model that represents text as just an unordered collection of words. For the purpose of this model, we’re not going to worry about the sequence and the ordering of the words, which word came first, second, or third. We’re just going to treat the text as a collection of words in no particular order. And we’re losing information there, right? The order of words is important. And we’ll come back to that a little bit later. But for now, to simplify our model, it’ll help us tremendously just to think about text as some unordered collection of words. And in particular, we’re going to use the bag of words model to build something known as a naive Bayes classifier. So what is a naive Bayes classifier? Well, it’s a tool that’s going to allow us to classify text based on Bayes rule, again, which you might remember from when we talked about probability. Bayes rule says that the probability of B given A is equal to the probability of A given B multiplied by the probability of B divided by the probability of A. So how are we going to use this rule to be able to analyze text? Well, what are we interested in? We’re interested in the probability that a message has a positive sentiment and the probability that a message has a negative sentiment, which I’m here for simplicity going to represent just with these emoji, happy face and frown face, as positive and negative sentiment. And so if I had a review, something like my grandson loved it, then what I’m interested in is not just the probability that a message has positive sentiment, but the conditional probability that a message has positive sentiment given that this is the message my grandson loved it. But how do I go about calculating this value, the probability that the message is positive given that the review is this sequence of words? Well, here’s where the bag of words model comes in. Rather than treat this review as a string of a sequence of words in order, we’re just going to treat it as an unordered collection of words. We’re going to try to calculate the probability that the review is positive given that all of these words, my grandson loved it, are in the review in no particular order, just this unordered collection of words. And this is a conditional probability, which we can then apply Bayes rule to try to make sense of. And so according to Bayes rule, this conditional probability is equal to what? It’s equal to the probability that all of these four words are in the review given that the review is positive multiplied by the probability that the review is positive divided by the probability that all of these words happen to be in the review. So this is the value now that we’re going to try to calculate. Now, one thing you might notice is that the denominator here, the probability that all of these words appear in the review, doesn’t actually depend on whether or not we’re looking at the positive sentiment or negative sentiment case. So we can actually get rid of this denominator. We don’t need to calculate it. We can just say that this probability is proportional to the numerator. And then at the end, we’re going to need to normalize the probability distribution to make sure that all of the values sum up to the value 1. So now, how do we calculate this value? Well, this is the probability of all of these words given positive times probability of positive. And that, by the definition of joint probability, is just one big joint probability, the probability that all of these things are the case, that it’s a positive review, and that all four of these words are in the review. But still, it’s not entirely obvious how we calculate that value. And here is where we need to make one more assumption. And this is where the naive part of naive Bayes comes in. We’re going to make the assumption that all of the words are independent of each other. And by that, I mean that if the word grandson is in the review, that doesn’t change the probability that the word loved is in the review or that the word it is in the review, for example. And in practice, this assumption might not be true. It’s almost certainly the case that the probability of words do depend on each other. But it’s going to simplify our analysis and still give us reasonably good results just to assume that the words are independent of each other and they only depend on whether it’s positive or negative. You might, for example, expect the word loved to appear more often in a positive review than in a negative review. So what does that mean? Well, if we make this assumption, then we can say that this value, the probability we’re interested in, is not directly proportional to, but it’s naively proportional to this value. The probability that the review is positive times the probability that my is in the review, given that it’s positive, times the probability that grandson is in the review, given that it’s positive, and so on for the other two words that happen to be in this review. And now this value, which looks a little more complex, is actually a value that we can calculate pretty easily. So how are we going to estimate the probability that the review is positive? Well, if we have some training data, some example data of example reviews where each one has already been labeled as positive or negative, then we can estimate the probability that a review is positive just by counting the number of positive samples and dividing by the total number of samples that we have in our training data. And for the conditional probabilities, the probability of loved, given that it’s positive, well, that’s going to be the number of positive samples with loved in it divided by the total number of positive samples. So let’s take a look at an actual example to see how we could try to calculate these values. Here I’ve put together some sample data. The way to interpret the sample data is that based on the training data, 49% of the reviews are positive, 51% are negative. And then over here in this table, we have some conditional probabilities. And then we have if the review is positive, then there is a 30% chance that my appears in it. And if the review is negative, there is a 20% chance that my appears in it. And based on our training data among the positive reviews, 1% of them contain the word grandson. And among the negative reviews, 2% contain the word grandson. So using this data, let’s try to calculate this value, the value we’re interested in. And to do that, we’ll need to multiply all of these values together. The probability of positive, and then all of these positive conditional probabilities. And when we do that, we get some value. And then we can do the same thing for the negative case. We’re going to do the same thing, take the probability that it’s negative, multiply it by all of these conditional probabilities, and we’re going to get some other value. And now these values don’t sum to one. They’re not a probability distribution yet. But I can normalize them and get some values. And that tells me that we’re going to predict that my grandson loved it. We think there’s a 68% chance, probability 0.68, that that is a positive sentiment review, and 0.32 probability that it’s a negative review. So what problems might we run into here? What could potentially go wrong when doing this kind of analysis in order to analyze whether text has a positive or negative sentiment? Well, a couple of problems might arise. One problem might be, what if the word grandson never appears for any of the positive reviews? If that were the case, then when we try to calculate the value, the probability that we think the review is positive, we’re going to multiply all these values together, and we’re just going to get 0 for the positive case, because we’re all going to ultimately multiply by that 0 value. And so we’re going to say that we think there is no chance that the review is positive because it contains the word grandson. And in our training data, we’ve never seen the word grandson appear in a positive sentiment message before. And that’s probably not the right analysis, because in cases of rare words, it might be the case that in nowhere in our training data did we ever see the word grandson appear in a message that has positive sentiment. So what can we do to solve this problem? Well, one thing we’ll often do is some kind of additive smoothing, where we add some value alpha to each value in our distribution just to smooth out the data a little bit. And a common form of this is Laplace smoothing, where we add 1 to each value in our distribution. In essence, we pretend we’ve seen each value one more time than we actually have. So if we’ve never seen the word grandson for a positive review, we pretend we’ve seen it once. If we’ve seen it once, we pretend we’ve seen it twice, just to avoid the possibility that we might multiply by 0 and as a result, get some results we don’t want in our analysis. So let’s see what this looks like in practice. Let’s try to do some naive Bayes classification in order to classify text as either positive or negative. We’ll take a look at sentiment.py. And what this is going to do is load some sample data into memory, some examples of positive reviews and negative reviews. And then we’re going to train a naive Bayes classifier on all of this training data, training data that includes all of the words we see in positive reviews and all of the words we see in negative reviews. And then we’re going to try to classify some input. And so we’re going to do this based on a corpus of data. I have some example positive reviews. Here are some positive reviews. It was great, so much fun, for example. And then some negative reviews, not worth it, kind of cheap. These are some examples of negative reviews. So now let’s try to run this classifier and see how it would classify particular text as either positive or negative. We’ll go ahead and run our sentiment analysis on this corpus. And we need to provide it with a review. So I’ll say something like, I enjoyed it. And we see that the classifier says there is about a 0.92 probability that we think that this particular review is positive. Let’s try something negative. We’ll try kind of overpriced. And we see that there is a 0.96 probability now that we think that this particular review is negative. And so our naive Bayes classifier has learned what kinds of words tend to appear in positive reviews and what kinds of words tend to appear in negative reviews. And as a result of that, we’ve been able to design a classifier that can predict whether a particular review is positive or negative. And so this definitely is a useful tool that we can use to try and make some predictions. But we had to make some assumptions in order to get there. So what if we want to now try to build some more sophisticated models, use some tools from machine learning to try and take better advantage of language data to be able to draw more accurate conclusions and solve new kinds of tasks and new kinds of problems? Well, we’ve seen a couple of times now that when we want to take some data and take some input, put it in a way that the computer is going to be able to make sense of, it can be helpful to take that data and turn it into numbers, ultimately. And so what we might want to try to do is come up with some word representation, some way to take a word and translate its meaning into numbers. Because, for example, if we wanted to use a neural network to be able to process language, give our language to a neural network and have it make some predictions or perform some analysis there, a neural network takes its input and produces its output a vector of values, a vector of numbers. And so what we might want to do is take our data and somehow take words and convert them into some kind of numeric representation. So how might we do that? How might we take words and turn them into numbers? Let’s take a look at an example. Here’s a sentence, he wrote a book. And let’s say I wanted to take each of those words and turn it into a vector of values. Here’s one way I might do that. We’ll say he is going to be a vector that has a 1 in the first position and the rest of the values are 0. Wrote will have a 1 in the second position and the rest of the values are 0. A has a 1 in the third position with the rest of the value 0. And book has a 1 in the fourth position with the rest of the value 0. So each of these words now has a distinct vector representation. And this is what we often call a one-hot representation, a representation of the meaning of a word as a vector with a single 1 and all of the rest of the values are 0. And so when doing this, we now have a numeric representation for every word and we could pass in those vector representations into a neural network or other models that require some kind of numeric data as input. But this one-hot representation actually has a couple of problems and it’s not ideal for a few reasons. One reason is, here we’re just looking at four words. But if you imagine a vocabulary of thousands of words or more, these vectors are going to get quite long in order to have a distinct vector for every possible word in a vocabulary. And as a result of that, these longer vectors are going to be more difficult to deal with, more difficult to train, and so forth. And so that might be a problem. Another problem is a little bit more subtle. If we want to represent a word as a vector, and in particular the meaning of a word as a vector, then ideally it should be the case that words that have similar meanings should also have similar vector representations, so that they’re close to each other together inside a vector space. But that’s not really going to be the case with these one-hot representations, because if we take some similar words, say the word wrote and the word authored, which means similar things, they have entirely different vector representations. Likewise, book and novel, those two words mean somewhat similar things, but they have entirely different vector representations because they each have a one in some different position. And so that’s not ideal either. So what we might be interested in instead is some kind of distributed representation. A distributed representation is the representation of the meaning of a word distributed across multiple values, instead of just being one-hot with a one in one position. Here is what a distributed representation of words might be. Each word is associated with some vector of values, with the meaning distributed across multiple values, ideally in such a way that similar words have a similar vector representation. But how are we going to come up with those values? Where do those values come from? How can we define the meaning of a word in this distributed sequence of numbers? Well, to do that, we’re going to draw inspiration from a quote from British linguist J.R. Firth, who said, you shall know a word by the company it keeps. In other words, we’re going to define the meaning of a word based on the words that appear around it, the context words around it. Take, for example, this context, for blank he ate. You might wonder, what words could reasonably fill in that blank? Well, it might be words like breakfast or lunch or dinner. All of those could reasonably fill in that blank. And so what we’re going to say is because the words breakfast and lunch and dinner appear in a similar context, that they must have a similar meaning. And that’s something our computer could understand and try to learn. A computer could look at a big corpus of text, look at what words tend to appear in similar context to each other, and use that to identify which words have a similar meaning and should therefore appear close to each other inside a vector space. And so one common model for doing this is known as the word to vec model. It’s a model for generating word vectors, a vector representation for every word by looking at data and looking at the context in which a word appears. The idea is going to be this. If you start out with all of the words just in some random position in space and train it on some training data, what the word to vec model will do is start to learn what words appear in similar contexts. And it will move these vectors around in such a way that hopefully words with similar meanings, breakfast, lunch, and dinner, book, memoir, novel, will hopefully appear to be near to each other as vectors as well. So let’s now take a look at what word to vec might look like in practice when implemented in code. What I have here inside of words.txt is a pre-trained model where each of these words has some vector representation trained by word to vec. Each of these words has some sequence of values representing its meaning, hopefully in such a way that similar words are represented by similar vectors. I also have this file vectors.py, which is going to open up the words and form them into a dictionary. And we also define some useful functions like distance to get the distance between two word vectors and closest words to find which words are nearby in terms of having close vectors to each other. And so let’s give this a try. We’ll go ahead and open a Python interpreter. And I’m going to import these vectors. And we might say, all right, what is the vector representation of the word book? And we get this big long vector that represents the word book as a sequence of values. And this sequence of values by itself is not all that meaningful. But it is meaningful in the context of comparing it to other vectors for other words. So we could use this distance function, which is going to get us the distance between two word vectors. And we might say, what is the distance between the vector representation for the word book and the vector representation for the word novel? And we see that it’s 0.34. You can kind of interpret 0 as being really close together and 1 being very far apart. And so now, what is the distance between book and, let’s say, breakfast? Well, book and breakfast are more different from each other than book and novel are. So I would hopefully expect the distance to be larger. And in fact, it is 0.64 approximately. These two words are further away from each other. And what about now the distance between, let’s say, lunch and breakfast? Well, that’s about 0.2. Those are even closer together. They have a meaning that is closer to each other. Another interesting thing we might do is calculate the closest words. We might say, what are the closest words, according to Word2Vec, to the word book? And let’s say, let’s get the 10 closest words. What are the 10 closest vectors to the vector representation for the word book? And when we perform that analysis, we get this list of words. The closest one is book itself, but we also have books plural, and then essay, memoir, essays, novella, anthology, and so on. All of these words mean something similar to the word book, according to Word2Vec, at least, because they have a similar vector representation. So it seems like we’ve done a pretty good job of trying to capture this kind of vector representation of word meaning. One other interesting side effect of Word2Vec is that it’s also able to capture something about the relationships between words as well. Let’s take a look at an example. Here, for instance, are two words, man and king. And these are each represented by Word2Vec as vectors. So what might happen if I subtracted one from the other, calculated the value king minus man? Well, that will be the vector that will take us from man to king, somehow represent this relationship between the vector representation of the word man and the vector representation of the word king. And that’s what this value, king minus man, represents. So what would happen if I took the vector representation of the word woman and added that same value, king minus man, to it? What would we get as the closest word to that, for example? Well, we could try it. Let’s go ahead and go back to our Python interpreter and give this a try. I could say, what is the closest word to the vector representation of the word king minus the representation of the word man plus the representation of the word woman? And we see that the closest word is the word queen. We’ve somehow been able to capture the relationship between king and man. And then when we apply it to the word woman, we get, as the result, the word queen. So Word2Vec has been able to capture not just the words and how they’re similar to each other, but also something about the relationships between words and how those words are connected to each other. So now that we have this vector representation of words, what can we now do with it? Now we can represent words as numbers. And so we might try to pass those words as input to, say, a neural network. Neural networks we’ve seen are very powerful tools for identifying patterns and making predictions. Recall that a neural network you can think of as all of these units. But really what the neural network is doing is taking some input, passing it into the network, and then producing some output. And by providing the neural network with training data, we’re able to update the weights inside of the network so that the neural network can do a more accurate job of translating those inputs into those outputs. And now that we can represent words as numbers that could be the input or output, you could imagine passing a word in as input to a neural network and getting a word as output. And so when might that be useful? One common use for neural networks is in machine translation, when we want to translate text from one language into another, say translate English into French by passing English into the neural network and getting some French output. You might imagine, for instance, that we could take the English word for lamp, pass it into the neural network, get the French word for lamp as output. But in practice, when we’re translating text from one language to another, we’re usually not just interested in translating a single word from one language to another, but a sequence, say a sentence or a paragraph of words. Here, for example, is another paragraph, again taken from Sherlock Holmes, written in English. And what I might want to do is take that entire sentence, pass it into the neural network, and get as output a French translation of the same sentence. But recall that a neural network’s input and output needs to be of some fixed size. And a sentence is not a fixed size. It’s variable. You might have shorter sentences, and you might have longer sentences. So somehow, we need to solve the problem of translating a sequence into another sequence by means of a neural network. And that’s going to be true not only for machine translation, but also for other problems, problems like question answering. If I want to pass as input a question, something like what is the capital of Massachusetts, feed that as input into the neural network, I would hope that what I would get as output is a sentence like the capital is Boston, again, translating some sequence into some other sequence. And if you’ve ever had a conversation with an AI chatbot, or have ever asked your phone a question, it needs to do something like this. It needs to understand the sequence of words that you, the human, provided as input. And then the computer needs to generate some sequence of words as output. So how can we do this? Well, one tool that we can use is the recurrent neural network, which we took a look at last time, which is a way for us to provide a sequence of values to a neural network by running the neural network multiple times. And each time we run the neural network, what we’re going to do is we’re going to keep track of some hidden state. And that hidden state is going to be passed from one run of the neural network to the next run of the neural network, keeping track of all of the relevant information. And so let’s take a look at how we can apply that to something like this. And in particular, we’re going to look at an architecture known as an encoder-decoder architecture, where we’re going to encode this question into some kind of hidden state, and then use a decoder to decode that hidden state into the output that we’re interested in. So what’s that going to look like? We’ll start with the first word, the word what. That goes into our neural network, and it’s going to produce some hidden state. This is some information about the word what that our neural network is going to need to keep track of. Then when the second word comes along, we’re going to feed it into that same encoder neural network, but it’s going to get as input that hidden state as well. So we pass in the second word. We also get the information about the hidden state, and that’s going to continue for the other words in the input. This is going to produce a new hidden state. And so then when we get to the third word, the, that goes into the encoder. It also gets access to the hidden state, and then it produces a new hidden state that gets passed into the next run when we use the word capital. And the same thing is going to repeat for the other words that appear in the input. So of Massachusetts, that produces one final piece of hidden state. Now somehow, we need to signal the fact that we’re done. There’s nothing left in the input. And we typically do this by passing some kind of special token, say an end token, into the neural network. And now the decoding process is going to start. We’re going to generate the word the. But in addition to generating the word the, this decoder network is also going to generate some kind of hidden state. And so what happens the next time? Well, to generate the next word, it might be helpful to know what the first word was. So we might pass the first word the back into the decoder network. It’s going to get as input this hidden state, and it’s going to generate the next word capital. And that’s also going to generate some hidden state. And we’ll repeat that, passing capital into the network to generate the third word is, and then one more time in order to get the fourth word Boston. And at that point, we’re done. But how do we know we’re done? Usually, we’ll do this one more time, pass Boston into the decoder network, and get an output some end token to indicate that that is the end of our input. And so this then is how we could use a recurrent neural network to take some input, encode it into some hidden state, and then use that hidden state to decode it into the output we’re interested in. To visualize it in a slightly different way, we have some input sequence. This is just some sequence of words. That input sequence goes into the encoder, which in this case is a recurrent neural network generating these hidden states along the way until we generate some final hidden state, at which point we start the decoding process. Again, using a recurrent neural network, that’s going to generate the output sequence as well. So we’ve got the encoder, which is encoding the information about the input sequence into this hidden state, and then the decoder, which takes that hidden state and uses it in order to generate the output sequence. But there are some problems. And for many years, this was the state of the art. The recurrent neural network and variance on this approach were some of the best ways we knew in order to perform tasks in natural language processing. But there are some problems that we might want to try to deal with and that have been dealt with over the years to try and improve upon this kind of model. And one problem you might notice happens in this encoder stage. We’ve taken this input sequence, the sequence of words, and encoded it all into this final piece of hidden state. And that final piece of hidden state needs to contain all of the information from the input sequence that we need in order to generate the output sequence. And while that’s possible, it becomes increasingly difficult as the sequence gets larger and larger. For larger and larger input sequences, it’s going to become more and more difficult to store all of the information we need about the input inside this single hidden state piece of context. That’s a lot of information to pack into just a single value. It might be useful for us, when generating output, to not just refer to this one value, but to all of the previous hidden values that have been generated by the encoder. And so that might be useful, but how could we do that? We’ve got a lot of different values. We need to combine them somehow. So you could imagine adding them together, taking the average of them, for example. But doing that would assume that all of these pieces of hidden state are equally important. But that’s not necessarily true either. Some of these pieces of hidden state are going to be more important than others, depending on what word they most closely correspond to. This piece of hidden state very closely corresponds to the first word of the input sequence. This one very closely corresponds to the second word of the input sequence, for example. And some of those are going to be more important than others. To make matters more complicated, depending on which word of the output sequence we’re generating, different input words might be more or less important. And so what we really want is some way to decide for ourselves which of the input values are worth paying attention to, at what point in time. And this is the key idea behind a mechanism known as attention. Attention is all about letting us decide which values are important to pay attention to, when generating, in this case, the next word in our sequence. So let’s take a look at an example of that. Here’s a sentence. What is the capital of Massachusetts? Same sentence as before. And let’s imagine that we were trying to answer that question by generating tokens of output. So what would the output look like? Well, it’s going to look like something like the capital is. And let’s say we’re now trying to generate this last word here. What is that last word? How is the computer going to figure it out? Well, what it’s going to need to do is decide which values it’s going to pay attention to. And so the attention mechanism will allow us to calculate some attention scores for each word, some value corresponding to each word, determining how relevant is it for us to pay attention to that word right now? And in this case, when generating the fourth word of the output sequence, the most important words to pay attention to might be capital and Massachusetts, for example. That those words are going to be particularly relevant. And there are a number of different mechanisms that have been used in order to calculate these attention scores. It could be something as simple as a dot product to see how similar two vectors are, or we could train an entire neural network to calculate these attention scores. But the key idea is that during the training process for our neural network, we’re going to learn how to calculate these attention scores. Our model is going to learn what is important to pay attention to in order to decide what the next word should be. So the result of all of this, calculating these attention scores, is that we can calculate some value, some value for each input word, determining how important is it for us to pay attention to that particular value. And recall that each of these input words is also associated with one of these hidden state context vectors, capturing information about the sentence up to that point, but primarily focused on that word in particular. And so what we can now do is if we have all of these vectors and we have values representing how important is it for us to pay attention to those particular vectors, is we can take a weighted average. We can take all of these vectors, multiply them by their attention scores, and add them up to get some new vector value, which is going to represent the context from the input, but specifically paying attention to the words that we think are most important. And once we’ve done that, that context vector can be fed into our decoder in order to say that the word should be, in this case, Boston. So attention is this very powerful tool that allows any word when we’re trying to decode it to decide which words from the input should we pay attention to in order to determine what’s important for generating the next word of the output. And one of the first places this was really used was in the field of machine translation. Here’s an example of a diagram from the paper that introduced this idea, which was focused on trying to translate English sentences into French sentences. So we have an input English sentence up along the top, and then along the left side, the output French equivalent of that same sentence. And what you see in all of these squares are the attention scores visualized, where a lighter square indicates a higher attention score. And what you’ll notice is that there’s a strong correspondence between the French word and the equivalent English word, that the French word for agreement is really paying attention to the English word for agreement in order to decide what French word should be generated at that point in time. And sometimes you might pay attention to multiple words if you look at the French word for economic. That’s primarily paying attention to the English word for economic, but also paying attention to the English word for European in this case too. And so attention scores are very easy to visualize to get a sense for what is our machine learning model really paying attention to, what information is it using in order to determine what’s important and what’s not in order to determine what the ultimate output token should be. And so when we combine the attention mechanism with a recurrent neural network, we can get very powerful and useful results where we’re able to generate an output sequence by paying attention to the input sequence too. But there are other problems with this approach of using a recurrent neural network as well. In particular, notice that every run of the neural network depends on the output of the previous step. And that was important for getting a sense for the sequence of words and the ordering of those particular words. But we can’t run this unit of the neural network until after we’ve calculated the hidden state from the run before it from the previous input token. And what that means is that it’s very difficult to parallelize this process. That as the input sequence get longer and longer, we might want to use parallelism to try and speed up this process of training the neural network and making sense of all of this language data. But it’s difficult to do that. And it’s slow to do that with a recurrent neural network because all of it needs to be performed in sequence. And that’s become an increasing challenge as we’ve started to get larger and larger language models. The more language data that we have available to us to use to train our machine learning models, the more accurate it can be, the better representation of language it can have, the better understanding it can have, and the better results that we can see. And so we’ve seen this growth of large language models that are using larger and larger data sets. But as a result, they take longer and longer to train. And so this problem that recurrent neural networks are not easy to parallelize has become an increasing problem. And as a result of that, that was one of the main motivations for a different architecture, for thinking about how to deal with natural language. And that’s known as the transformer architecture. And this has been a significant milestone in the world of natural language processing for really increasing how well we can perform these kinds of natural language processing tasks, as well as how quickly we can train a machine learning model to be able to produce effective results. There are a number of different types of transformers in terms of how they work. But what we’re going to take a look at here is the basic architecture for how one might work with a transformer to get a sense for what’s involved and what we’re doing. So let’s start with the model we were looking at before, specifically at this encoder part of our encoder-decoder architecture, where we used a recurrent neural network to take this input sequence and capture all of this information about the hidden state and the information we need to know about that input sequence. Right now, it all needs to happen in this linear progression. But what the transformer is going to allow us to do is process each of the words independently in a way that’s easy to parallelize, rather than have each word wait for some other word. Each word is going to go through this same neural network and produce some kind of encoded representation of that particular input word. And all of this is going to happen in parallel. Now, it’s happening for all of the words at once, but we’re really just going to focus on what’s happening for one word to make it clear. But know that whatever you’re seeing happen for this one word is going to happen for all of the other input words, too. So what’s going on here? Well, we start with some input word. That input word goes into the neural network. And the output is hopefully some encoded representation of the input word, the information we need to know about the input word that’s going to be relevant to us as we’re generating the output. And because we’re doing this each word independently, it’s easy to parallelize. We don’t have to wait for the previous word before we run this word through the neural network. But what did we lose in this process by trying to parallelize this whole thing? Well, we’ve lost all notion of word ordering. The order of words is important. The sentence, Sherlock Holmes gave the book to Watson, has a different meaning than Watson gave the book to Sherlock Holmes. And so we want to keep track of that information about word position. In the recurrent neural network, that happened for us automatically because we could run each word one at a time through the neural network, get the hidden state, pass it on to the next run of the neural network. But that’s not the case here with the transformer, where each word is being processed independent of all of the other ones. So what are we going to do to try to solve that problem? One thing we can do is add some kind of positional encoding to the input word. The positional encoding is some vector that represents the position of the word in the sentence. This is the first word, the second word, the third word, and so forth. We’re going to add that to the input word. And the result of that is going to be a vector that captures multiple pieces of information. It captures the input word itself as well as where in the sentence it appears. The result of that is we can pass the output of that addition, the addition of the input word and the positional encoding into the neural network. That way, the neural network knows the word and where it appears in the sentence and can use both of those pieces of information to determine how best to represent the meaning of that word in the encoded representation at the end of it. In addition to what we have here, in addition to the positional encoding and this feed forward neural network, we’re also going to add one additional component, which is going to be a self-attention step. This is going to be attention where we’re paying attention to the other input words. Because the meaning or interpretation of an input word might vary depending on the other words in the input as well. And so we’re going to allow each word in the input to decide what other words in the input it should pay attention to in order to decide on its encoded representation. And that’s going to allow us to get a better encoded representation for each word because words are defined by their context, by the words around them and how they’re used in that particular context. This kind of self-attention is so valuable, in fact, that oftentimes the transformer will use multiple different self-attention layers at the same time to allow for this model to be able to pay attention to multiple facets of the input at the same time. And we call this multi-headed attention, where each attention head can pay attention to something different. And as a result, this network can learn to pay attention to many different parts of the input for this input word all at the same time. And in the spirit of deep learning, these two steps, this multi-headed self-attention layer and this neural network layer, that itself can be repeated multiple times, too, in order to get a deeper representation, in order to learn deeper patterns within the input text and ultimately get a better representation of language in order to get useful encoded representations of all of the input words. And so this is the process that a transformer might use in order to take an input word and get it its encoded representation. And the key idea is to really rely on this attention step in order to get information that’s useful in order to determine how to encode that word. And that process is going to repeat for all of the input words that are in the input sequence. We’re going to take all of the input words, encode them with some kind of positional encoding, feed those into these self-attention and feed-forward neural networks in order to ultimately get these encoded representations of the words. That’s the result of the encoder. We get all of these encoded representations that will be useful to us when it comes time then to try to decode all of this information into the output sequence we’re interested in. And again, this might take place in the context of machine translation, where the output is going to be the same sentence in a different language, or it might be an answer to a question in the case of an AI chatbot, for example. And so now let’s take a look at how that decoder is going to work. Ultimately, it’s going to have a very similar structure. Any time we’re trying to generate the next output word, we need to know what the previous output word is, as well as its positional encoding. Where in the output sequence are we? And we’re going to have these same steps, self-attention, because we might want an output word to be able to pay attention to other words in that same output, as well as a neural network. And that might itself repeat multiple times. But in this decoder, we’re going to add one additional step. We’re going to add an additional attention step, where instead of self-attention, where the output word is going to pay attention to other output words, in this step, we’re going to allow the output word to pay attention to the encoded representations. So recall that the encoder is taking all of the input words and transforming them into these encoded representations of all of the input words. But it’s going to be important for us to be able to decide which of those encoded representations we want to pay attention to when generating any particular token in the output sequence. And that’s what this additional attention step is going to allow us to do. It’s saying that every time we’re generating a word of the output, we can pay attention to the other words in the output, because we might want to know, what are the words we’ve generated previously? And we want to pay attention to some of them to decide what word is going to be next in the sequence. But we also care about paying attention to the input words, too. And we want the ability to decide which of these encoded representations of the input words are going to be relevant in order for us to generate the next step. And so these two pieces combine together. We have this encoder that takes all of the input words and produces this encoded representation. And we have this decoder that is able to take the previous output word, pay attention to that encoded input, and then generate the next output word. And this is one of the possible architectures we could use for a transformer, with the key idea being these attention steps that allow words to pay attention to each other. During the training process here, we can now much more easily parallelize this, because we don’t have to wait for all of the words to happen in sequence. And we can learn how we should perform these attention steps. The model is able to learn what is important to pay attention to, what things do I need to pay attention to, in order to be more accurate at predicting what the output word is. And this has proved to be a tremendously effective model for conversational AI agents, for building machine translation systems. And there have been many variants proposed on this model, too. Some transformers only use an encoder. Some only use a decoder. Some use some other combination of these different particular features. But the key ideas ultimately remain the same, this real focus on trying to pay attention to what is most important. And the world of natural language processing is fast growing and fast evolving. Year after year, we keep coming up with new models that allow us to do an even better job of performing these natural language related tasks, all on the surface of solving the tricky problem, which is our own natural language. We’ve seen how the syntax and semantics of our language is ambiguous, and it introduces all of these new challenges that we need to think about, if we’re going to be able to design AI agents that are able to work with language effectively. So as we think about where we’ve been in this class, all of the different types of artificial intelligence we’ve considered, we’ve looked at artificial intelligence in a wide variety of different forms now. We started by taking a look at search problems, where we looked at how AI can search for solutions, play games, and find the optimal decision to make. We talked about knowledge, how AI can represent information that it knows and use that information to generate new knowledge as well. Then we looked at what AI can do when it’s less certain, when it doesn’t know things for sure, and we have to represent things in terms of probability. We then took a look at optimization problems. We saw how a lot of problems in AI can be boiled down to trying to maximize or minimize some function. And we looked at strategies that AI can use in order to do that kind of maximizing and minimizing. We then looked at the world of machine learning, learning from data in order to figure out some patterns and identify how to perform a task by looking at the training data that we have available to it. And one of the most powerful tools there was the neural network, the sequence of units whose weights can be trained in order to allow us to really effectively go from input to output and predict how to get there by learning these underlying patterns. And then today, we took a look at language itself, trying to understand how can we train the computer to be able to understand our natural language, to be able to understand syntax and semantics, make sense of and generate natural language, which introduces a number of interesting problems too. And we’ve really just scratched the surface of artificial intelligence. There is so much interesting research and interesting new techniques and algorithms and ideas being introduced to try to solve these types of problems. So I hope you enjoyed this exploration into the world of artificial intelligence. A huge thanks to all of the course’s teaching staff and production team for making the class possible. This was an introduction to artificial intelligence with Python.

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Python Lists, Dictionaries, and Object-Oriented Programming

    Python Lists, Dictionaries, and Object-Oriented Programming

    The provided text serves as a comprehensive set of lecture notes on Python programming. It begins with fundamental concepts like reserved words, assignment statements, and basic program structure. The notes progress to cover conditional execution, functions, file handling, and data structures, including lists, dictionaries, and tuples. Later sections discuss regular expressions and object-oriented programming concepts, with a focus on terminology rather than in-depth coding. The material also contains practical code examples for data analysis, such as word counting and visualization, demonstrating the application of these concepts in real-world scenarios. Finally, it walks through the construction of visualizations including word clouds and line charts.

    Python Programming Study Guide

    Quiz

    1. What is an “if” statement used for in programming? An “if” statement is used for conditional execution of code. It allows a program to make a decision based on a true/false condition, executing a specific block of code only if the condition is true.
    2. Explain the purpose of the “while” keyword in Python. The “while” keyword in Python creates a loop that executes a block of code repeatedly as long as a specified condition remains true. Once the condition becomes false, the loop terminates, and the program continues with the next line of code after the loop.
    3. What are constants in Python? Constants are values that do not change during the execution of a program. They can be numbers, strings, or other data types, and they are used to represent fixed values in calculations or comparisons.
    4. What are variables in Python? Variables are named storage locations in memory used to hold values that can change during program execution. They allow you to store and manipulate data within your program, and their values can be updated as needed.
    5. Explain the difference between integer division in Python 2 and Python 3. In Python 2, integer division between two integers would truncate the decimal portion, resulting in an integer result, while in Python 3, integer division between two integers always produces a floating-point result. This change makes division more predictable in Python 3.
    6. What does the “else” statement do in conjunction with an “if” statement? The “else” statement provides an alternative block of code to be executed if the condition in the “if” statement is false. It ensures that one of two blocks of code will always be executed, depending on the truthiness of the “if” condition.
    7. What is the purpose of the “elif” keyword in Python? The “elif” keyword allows for multiple conditional checks in a single “if” statement. It provides a way to check additional conditions if the previous “if” or “elif” conditions were false, creating a multi-way branch.
    8. Explain the concept of a function call and return value. A function call is the act of executing a defined function, passing arguments to it if necessary. The return value is the result that a function sends back to the calling code after it has finished executing, which can be used in expressions or assigned to variables.
    9. What are definite loops? Definite loops iterate through the members of a set.
    10. Explain how try and accept blocks work. Try and except blocks are a method of handling dangerous code. Place dangerous code in the try block and if the program would trace back (quit) that code jumps down into the accept block, executing that code instead.

    Quiz Answer Key

    1. An “if” statement is used for conditional execution of code. It allows a program to make a decision based on a true/false condition, executing a specific block of code only if the condition is true.
    2. The “while” keyword in Python creates a loop that executes a block of code repeatedly as long as a specified condition remains true. Once the condition becomes false, the loop terminates, and the program continues with the next line of code after the loop.
    3. Constants are values that do not change during the execution of a program. They can be numbers, strings, or other data types, and they are used to represent fixed values in calculations or comparisons.
    4. Variables are named storage locations in memory used to hold values that can change during program execution. They allow you to store and manipulate data within your program, and their values can be updated as needed.
    5. In Python 2, integer division between two integers would truncate the decimal portion, resulting in an integer result, while in Python 3, integer division between two integers always produces a floating-point result. This change makes division more predictable in Python 3.
    6. The “else” statement provides an alternative block of code to be executed if the condition in the “if” statement is false. It ensures that one of two blocks of code will always be executed, depending on the truthiness of the “if” condition.
    7. The “elif” keyword allows for multiple conditional checks in a single “if” statement. It provides a way to check additional conditions if the previous “if” or “elif” conditions were false, creating a multi-way branch.
    8. A function call is the act of executing a defined function, passing arguments to it if necessary. The return value is the result that a function sends back to the calling code after it has finished executing, which can be used in expressions or assigned to variables.
    9. Definite loops iterate through the members of a set.
    10. Try and except blocks are a method of handling dangerous code. Place dangerous code in the try block and if the program would trace back (quit) that code jumps down into the accept block, executing that code instead.

    Essay Questions

    1. Explain the significance of indentation in Python and how it affects the structure and execution of control flow statements like “if,” “else,” “elif,” “while,” and “for” loops. Provide examples to illustrate how incorrect indentation can lead to errors and unexpected program behavior.
    2. Compare and contrast definite and indefinite loops in Python, providing examples of when each type of loop is most appropriate. Discuss the use of “break” and “continue” statements within loops and how they can alter the flow of execution.
    3. Describe the concept of loop idioms and their importance in solving common programming problems. Provide detailed examples of how loop idioms can be used for tasks such as finding the maximum or minimum value in a list, summing elements, and searching for specific patterns.
    4. Discuss the different ways to open and read files in Python, including the use of file handles, “for” loops to iterate through lines, and the “read” method to read the entire file content. Explain how to handle newlines and white space when processing file data, and describe the use of “try” and “except” blocks for error handling.
    5. Explain how dictionaries are used in Python, including how to create, access, and modify key-value pairs. Discuss the purpose and usage of the .get() method and how it simplifies conditional logic when working with dictionaries. Illustrate how dictionaries can be used to count word frequencies in a text file and find the most common word.

    Glossary of Key Terms

    • Conditional Execution: Executing a block of code only if a specific condition is met (true).
    • Indentation: The spacing at the beginning of a code line. In Python, it is used to define the block of statements, and it is vitally important.
    • Loop: A programming construct that repeats a sequence of instructions until a specific condition is met.
    • Iteration Variable: A variable that changes with each iteration of a loop, often used to control the loop’s execution.
    • Constants: Values that do not change during program execution.
    • Reserved Words: Special words in a programming language with predefined meanings (e.g., if, else, while, for, def).
    • Variables: Named storage locations that can hold values that change during program execution.
    • Concatenation: Joining two or more strings or lists together.
    • String Slicing: Extracting a portion of a string using indices.
    • Function Call: Executing a defined function with optional arguments.
    • Return Value: The value returned by a function after it has finished executing.
    • Argument/Parameter: A value passed into a function.
    • Indefinite Loop: A loop that continues executing until a specific condition is no longer met. Also known as while loops.
    • Definite Loop: A loop that executes a predetermined number of times. Also known as for loops.
    • Break Statement: A statement that terminates the current loop and transfers control to the next statement after the loop.
    • Continue Statement: A statement that skips the rest of the current iteration of a loop and proceeds to the next iteration.
    • Loop Idiom: A common pattern for constructing loops to accomplish specific tasks, such as finding the maximum or minimum value, summing elements, or searching.
    • File Handle: A variable that represents an open file object, used for reading or writing data to the file.
    • Try/Except Block: A construct used for error handling. The code in the try block is executed, and if an exception occurs, the code in the except block is executed.
    • Dictionaries: Key-value pairs.
    • Keys: Value that is associated with an element.
    • Values: Value of an element.
    • Mutable: Changeable.
    • Concatenate: Adding sets together.
    • Tuple: Like a list but with parentheses.
    • Junction Table: A table that facilitates many-to-many relationships.

    Python Programming Fundamentals: An Overview

    Here’s a briefing document summarizing the main themes and ideas from the provided text excerpts.

    Briefing Document: Python Programming Fundamentals

    Overview:

    The provided excerpts offer an introduction to fundamental concepts in Python programming. The material covers program structure, data types, control flow (conditional execution and loops), functions, strings, lists, dictionaries, tuples, file handling, and an introduction to SQLite database interaction. The overall theme is building a foundation for understanding and writing basic Python programs.

    Key Themes and Ideas:

    1. Program Structure and Control Flow:
    • Sequential Execution: Code executes line by line, from top to bottom.
    • Conditional Execution (if/else): The if statement allows code to be executed selectively based on a true/false condition. elif extends this to multi-way branching, executing only one branch within a block.
    • “So, inside the if statement, right here, there is a question, saying, is x less than 10? That’s a, that resolves to a true or false… If it’s false, skip that code.”
    • “And the way it works is it’s probably best to look at this here, where it checks the first one, and if it’s a true, then it runs that, and then it’s done. It doesn’t check them all.”
    • Loops (while/for): Loops allow code to be repeated. while loops continue as long as a condition is true. for loops iterate over a sequence of items (e.g., numbers, strings, lines in a file).
    • “This is repeated over and over and over and over again, and this is the essence of how we make computers do things that are seemingly difficult, while they’re more naturally difficult for people”
    • “As long as n remains greater than zero, keep doing this indented block”
    • Indentation: Python uses indentation to define code blocks, crucial for if statements, loops, and function definitions. Incorrect indentation leads to errors.
    • Loop Control (break/continue): break exits a loop prematurely. continue skips the rest of the current iteration and goes to the next.
    • “And if you hit the break, it exits the innermost loop out to the place beyond the end of the loop.”
    • “Continue effectively says stop this iteration. We’re done with this iteration.”
    1. Data Types and Variables:
    • Constants: Values that don’t change (e.g., numbers, strings).
    • “These are just things we call constants because they don’t change, they’re numbers, strings, et cetera.”
    • Variables: Named memory locations that store values.
    • “Variables are the third building block, and that is a way that you can ask Python to allocate a piece of memory and then give it a name”
    • Data Types: Common types include integers (int), floating-point numbers (float), strings (str), and Booleans (bool). Python is sensitive to data types.
    • “Type error can’t convert int object to str implicitly. So, that’s an integer right there, and that’s a string, and that’s what it’s complaining about, that little bit right there.”
    • Type Conversion: Functions like int(), float(), and str() are used to convert values between data types.
    • “You can also use a set of built-in functions, like float and int, to convert from one to another.”
    • None: A special type with a single value (None) often used to indicate emptiness or the absence of a value.
    • “But none type has one value, none. None is a constant. Capital none is a constant… None is often used to indicate emptiness.”
    1. Operators:
    • Arithmetic Operators: +, -, *, / perform mathematical operations. Division in Python 3 always produces a floating-point result.
    • “Integer division produces a floating result in Python 3.0, not in Python 2.0. That is an improvement in Python 3.0.”
    • String Operators: + concatenates strings.
    • “We can also use this plus to concatenate two strings.”
    • Comparison Operators: ==, !=, <, >, <=, >= compare values.
    • Logical Operators: and, or, not combine Boolean expressions.
    • Membership Operator: in checks if a value exists within a sequence (string, list, etc.).
    • “We can use in differently as a logical operator, so we’re using it as an iteration structure in for loops, but we can also use it as a logical operator in if statements.”
    • Identity Operator: is and is not check if two variables refer to the same object in memory. They are stronger than ==. Recommended for use with Booleans and None.
    • “It is stronger. Double equal says are these things equal in type and value? …So is is stronger than equals, meaning that it demands equality in both the type of the variable and the value of the variable.”
    1. Functions:
    • Built-in Functions: Python provides many built-in functions (e.g., print(), len(), type(), max(), min(), sum()).
    • User-Defined Functions: The def keyword defines a function. Functions can accept arguments (parameters) and return values. return exits a function and specifies the return value.
    • “We use the def keyword to define a function and then later we’re gonna invoke this and there’s a bit to it…It starts with a def keyword”
    • Function Invocation: Calling a function executes its code.
    1. Strings:
    • Indexing: Individual characters in a string can be accessed using square brackets (e.g., fruit[0] for the first character). Strings are indexed starting from zero.
    • “So fruit is a variable that contains the string banana, and then fruit sub one is the character that’s in position one.”
    • Slicing: Extracting substrings using [start:end]. The end index is exclusive.
    • “S sub zero through four says, start at position zero, and then go up through, but not including four, right? So we don’t include four.”
    • Immutability: Strings cannot be changed after they are created. Operations like lower() create new strings.
    • “Strings are not mutable. So if I take a look at assigning banana into fruit, well, fruit sub-zero is a capital letter B…it turns out that strings are not mutable, meaning they’re not changeable once you create them.”
    • String Methods: Functions associated with string objects (e.g., strip(), startswith(), find(), lower()).
    1. Lists:
    • Definition: Lists are ordered collections of items enclosed in square brackets ([]). Lists can contain items of different data types.
    • “Lists are really a lot like arrays, but they’re surrounded with these square braces, like that. So the square braces are what defines a list.”
    • Mutability: Lists can be modified after creation (elements can be changed, added, or removed).
    • “Lists are mutable. Mutable is another word for changeable. They can be changed”
    • Indexing and Slicing: Similar to strings.
    • List Methods: Functions associated with list objects (e.g., append(), sort(), remove(), insert(), pop()).
    • List Operations: + concatenates lists.
    • Built-in Functions: len(), max(), min(), sum() can be used with lists.
    1. Dictionaries:
    • Definition: Dictionaries are collections of key-value pairs enclosed in curly braces ({}). Keys must be unique and immutable (e.g., strings, numbers).
    • “Dictionaries are not sequential, they are just sort of a bag of stuff, and when you want to get something out of it, you look it up with a label”
    • Accessing Values: Values are accessed using their corresponding keys (e.g., dictionary[‘key’]).
    • Adding/Updating Pairs: New key-value pairs are added, and existing values can be updated.
    • get() Method: Provides a way to retrieve a value associated with a key, with an option to specify a default value if the key doesn’t exist.
    • “The get says in its first parameters, the key to lookup…and 99 is the default value that we get if the key doesn’t exist.”
    • Looping through Dictionaries: Using .items() provides access to key-value pairs for iteration.
    1. Tuples:
    • Definition: Tuples are ordered, immutable collections of items enclosed in parentheses ().
    • Immutability: Once created, tuples cannot be changed.
    • Use cases: Tuples are often used for representing fixed collections of data.
    1. File Handling:
    • open() Function: Opens a file and returns a file handle object.
    • “So then you have to use the open command… And the name of the file, which we put it in quotes. It’s really kind of a string and then this little r means read.”
    • Reading a File: Files can be read line by line (using a for loop) or as a single string (using read()).
    • “But the most common way that we’re gonna read through the file is to treat it as a sequence of lines… we use the for loop and we have an iteration variable… Python doesn’t know anything special by naming that variable line.”
    • strip() Method: Removes whitespace (including newlines) from the beginning and end of a string.
    • “Thankfully, as we saw in the previous chapter, there is a nice little function in Python for strings called strip that allows you to throw away white space.”
    • Error Handling (try/except): Handles potential exceptions (e.g., FileNotFoundError) that might occur when opening a file.
    • “We took out insurance on it, and we know that it might blow up, and so we have it in a try and accept block.”
    1. Loop Idioms:
    • Largest/Smallest Value: Finding the largest or smallest element in a sequence using a loop and a “largest_so_far” or “smallest_so_far” variable.
    • “So I have this variable called largest so far, I set it to negative one, before the loop…If nine is greater than largest so far, well largest so far is negative one, so that’s true, so this code’s gonna run.”
    • Counting: Counting occurrences of items or lines that meet a certain criteria using a loop and a counter variable.
    • Summing: Adding up values in a sequence using a loop and a running total variable.
    • SQLite Database InteractionUse import sqlite3 to interact with SQLite databases.
    • Establish database connections.
    • Create tables using SQL.
    • Insert, update, delete, and select data.
    • Utilize parameterized SQL to prevent SQL injection.
    • Implement error handling and transaction management.
    • Foreign key relationships: connect tables by referencing primary keys in other tables, enforcing data integrity.
    • Junction tables: implement many-to-many relationships by having a table with foreign keys to the tables related.
    • Foreign keys: “So I have the title artist ID. This is the foreign key to the artist table. And I got this value that I just moments ago retrieved.”

    Important Considerations:

    • Python’s Readability: The excerpts highlight Python’s emphasis on readability and ease of use, particularly in file handling and loop construction.
    • Debugging: Tracebacks provide information about errors, helping to identify and fix problems.
    • Code Style: The use of mnemonic variable names and consistent indentation improves code clarity.
    • Iteration Variables: The use of singular and plural is common to better understand what the code is working with (e.g., friends (list of friends) and friend (an element in that list)).

    This briefing document summarizes the core concepts. Further study and practice are essential for mastering Python programming.

    Python Programming Fundamentals

    Programming Concepts and Constructs

    1. What is conditional execution in Python, and how is it implemented? Conditional execution allows a program to execute different code blocks based on whether a condition is true or false. In Python, it’s implemented using the if statement, optionally with elif (else if) and else clauses. The if statement evaluates a boolean expression, and if it’s true, the indented block of code following the if is executed. If it’s false, the code block is skipped. elif allows you to check multiple conditions in sequence, and else provides a default block to execute if none of the previous conditions are true. Only one of the branches (the if, an elif, or the else) in a conditional block will ever run in a single execution.

    2. What is a loop, and what are the two main types of loops in Python? A loop is a control flow statement that allows code to be executed repeatedly. The two main types of loops in Python are:

    • While Loop: Continues to execute a block of code as long as a condition is true. It is considered an indefinite loop because the number of iterations isn’t known beforehand. The loop continues until the condition becomes false.
    • For Loop: Iterates over a sequence (like a list, tuple, string, or range) and executes a block of code for each item in the sequence. It is considered a definite loop because the number of iterations is determined by the length of the sequence.

    3. What is the purpose of the break and continue statements within loops?

    • break: Immediately terminates the loop and transfers control to the statement immediately following the loop.
    • continue: Skips the rest of the current iteration and resumes execution at the next iteration of the loop.

    4. What are constants and variables in Python, and how do they differ?

    • Constants: Values that do not change during the program’s execution (e.g., numbers like 123, 98.6, or strings like “Hello World”).
    • Variables: Named storage locations that can hold values. These values can be changed during the program’s execution using assignment statements. Variables are created when a value is first assigned to them.

    5. How does Python handle data types, and what are some common data types? Python is dynamically typed, meaning the data type of a variable is determined at runtime and can change. Common data types include:

    • Integer (int): Whole numbers (e.g., 1, 100, -5).
    • Floating-point number (float): Numbers with decimal points (e.g., 3.14, 2.5, -0.01).
    • String (str): Sequences of characters enclosed in quotes (e.g., “hello”, ‘world’).
    • Boolean (bool): Represents truth values, either True or False.
    • NoneType (None): Represents the absence of a value. The only value of this type is None. Python provides built-in functions like type() to check the data type of a variable or value, and functions like int(), float(), and str() to convert between data types.

    6. What are functions in Python, and how are they defined and called? Functions are reusable blocks of code that perform a specific task. They promote modularity and code reuse. Functions are defined using the def keyword, followed by the function name, parentheses (optionally containing parameters), and a colon. The function body is an indented block of code. Functions are called by using their name followed by parentheses, passing arguments if necessary. A function can return a value using the return statement.

    7. What is a list in Python, and how does it differ from a string? A list is an ordered, mutable (changeable) sequence of items. Lists are created using square brackets [] and can contain elements of different data types. Strings are sequences of characters, but are immutable. Lists can be modified after creation (e.g., by adding, removing, or changing elements), while strings cannot be changed directly.

    8. What is a dictionary in Python, and how is it used? A dictionary is an unordered collection of key-value pairs. Each key must be unique, and the value can be of any data type. Dictionaries are created using curly braces {}. Dictionaries are useful for storing and retrieving data based on a key. You can add, remove, or modify key-value pairs in a dictionary. They’re particularly useful for tasks like counting the frequency of items in a sequence or storing configuration settings. Dictionaries are particularly useful for accumulating data when processing files.

    Python Vocabulary and Reserved Words

    The sources talk about vocabulary in the context of learning Python. Here’s a summary:

    • When learning Python, it’s helpful to start with a basic vocabulary.
    • Reserved words are a key part of the Python vocabulary. These words have specific meanings to Python and cannot be used for other purposes. Examples of reserved words include and, or, del, and if.
    • Python looks at reserved words the same way a dog responds to the word “walk”. When Python sees a reserved word, it knows exactly what it means.
    • When choosing variable names, it can be helpful to make them descriptive so that others can easily understand the code. Using descriptive names is also called using mnemonic variable names. Python is happy with any variable name.
    • It’s important to choose variable names carefully to help readers understand the code.
    • Python doesn’t understand singular and plural forms of variable names.
    • Comments are also important for humans to read to understand what the code does. Comments in Python are added with a #.

    Python Syntax Overview

    Here’s a discussion of Python syntax, drawing from the sources:

    • Translation: Python code is translated into machine language (zeros and ones) for the microprocessor to execute. Python acts as a translator.
    • Installation: To get started, you need to install both Python 3 and a programmer’s text editor. A programmer’s editor is needed instead of a word processor.
    • Running Code: Python code can be run in the command line, and the command cd tells you where you are in the folder. Code can be saved in a file with a .py suffix. In Windows, typing first.py will run the code.
    • Errors: Syntax errors indicate that Python doesn’t understand what you’re trying to say.
    • Statements: A Python program consists of a series of statements or lines that the computer executes in order, from beginning to end.
    • Vocabulary: Python has reserved words that it recognizes, and these words cannot be used for other purposes.
    • Variables: An assignment statement uses an equal sign, but this should be thought of as an arrow. For example, x = 2 means that the value 2 is assigned to the variable x. Python’s job is to remember the variable. Variable names can start with a letter or underscore, but it is bad practice to only differentiate variable names by case.
    • Operators: Operators, such as + and =, perform actions. The + operator can add integers or concatenate strings.
    • Types: Python is able to determine the type of data. The type() function can be used to ask Python about the type of a variable or constant. There are different types of numbers, such as integers and floating point numbers. You can convert between types using built-in functions such as float() and int().
    • Comments: Comments are added to Python code using a pound sign (#). Python ignores everything after the pound sign.
    • Conditionals: Conditional steps can be performed using if statements. Comparison operators include == (equal to), >=, <=, >, <, and != (not equal). Indentation is very important in Python to indicate blocks of code. Four spaces are normally used for each indent. After an if, you must indent.
    • else: if statements may be followed by else.
    • elif: Multiple conditions can be checked using elif.
    • Try/Except: try and except are used for error detection.
    • Functions: Functions are defined using the def keyword.
    • Strings: Strings can be concatenated using the plus sign. Individual characters in strings can be accessed using indexing, starting at zero. There are many string methods available, such as lower(), upper(), find(), replace(), and strip().
    • Files: Text files are a sequence of lines. To read a file, you must call the open() function, which returns a file handle. The newline character (n) indicates the end of a line.
    • Lists: Lists are a collection that can hold more than one item, which can be a mix of types. List values are in square brackets and are mutable.
    • Dictionaries: Dictionaries are Python’s most powerful collection. Dictionaries use key value pairs.
    • Tuples: Tuples are similar to lists, but use parentheses instead of square brackets, and are not mutable.

    Python Reserved Words: Vocabulary and Usage

    Reserved words are a key part of the Python vocabulary. These words have specific meanings to Python and cannot be used for other purposes.

    • Examples of reserved words include and, or, del, and if.
    • When Python sees a reserved word, it knows exactly what it means. It looks at reserved words the same way a dog responds to the word “walk”.
    • There are only a few of them. A lot of them, you won’t end up using. They are just these are reserved for Python and part of the Python vocabulary.
    • You can’t use them for any other purpose than the purpose that Python wants. It’s sort of part of the contract.

    Python Conditional Steps: If, Else, and Try-Except

    Here’s what the sources say about conditional steps in Python:

    • Conditional steps in Python allow the program to make decisions based on whether a condition is true or false. This is where code starts to get intelligent.
    • The keyword that is used is the if statement. The if is like a little fork in the road. You can go one way, or you can go another way, and you’re asking a question.
    • The if statement has a question as part of it. The question resolves to a true or false.
    • The syntax is the reserved word if, a question, and a colon. An indented block of code begins after the colon. If the question is true, the indented block of code is run. If the question is false, the indented block of code is skipped.
    • When finished with an if statement, you deindent back.
    • Comparison operators are used to ask true-false questions. These include <, <=, ==, >=, >, and !=.
    • If there is only one line in the indented block, it can be pulled up to the same line after the equals.
    • The indented block can include more than one line. As long as the code stays indented, it is part of the if block. If the question is false, all of the lines are skipped.
    • An else statement can be added, with a colon, followed by a reindented block. The else is part of the block. The first indented block is what runs if the condition is true, and the second indented block, after the else, is what runs if the condition is false. With an if then else, one of the two branches is going to run.
    • A multi-way branch can be implemented to pick one of many options using the reserved word elif. The questions are checked in order. If the first one is true, it runs and is done, and the other questions are not checked. Only one of the options is going to run. It is possible to have no else. With no else, it is not guaranteed that one of the blocks will run. There can also be many elif statements. The order in which the questions are asked matters.
    • The try and except structure is used to handle code that might cause a traceback. Code that might blow up is put in a try block. If it fails, the code in the except block is run. The except is ignored if the code in the try block works.

    Python Data Structures: Lists, Dictionaries, and More

    Here’s what the sources say about data structures in Python:

    • Algorithms involve using a programming language to express the steps to solve a problem, while data structures involve clever ways to lay out data to achieve a desired result.
    • Lists are the first real data structure that is covered in the sources.
    • When you put more than one thing in a data structure, you need to get them out.

    Key characteristics and concepts related to data structures:

    • Variables A variable is a piece of memory with a label on it, capable of holding a single value at a time.
    • Collections Collections are like suitcases that can hold multiple items. Lists, dictionaries, and tuples are different ways to organize collections.
    • Lists Lists are a simple data structure that can hold a variety of data types. Every time square bracket syntax is used, it’s working with lists. Lists maintain order, and the first thing in the list is the sub-zero position.
    • Lists can contain strings, integers, and floating point numbers.
    • Lists can contain other lists.
    • Lists can be empty.
    • The append method can be used to add items to a list.
    • The sort method sorts the list in place.
    • Strings Strings are like data structures.
    • Dictionaries Dictionaries do not have a sense of order.
    • Everything in a dictionary has a key.
    • Dictionaries are like a purse or a bag where things are labeled, thrown in, and can be retrieved by shouting out the label.
    • Dictionaries are referred to as associative arrays, property maps, or hash maps.
    • Keys can be strings.
    • Tuples Will be covered in more detail.
    • Relationship between strings and lists The split function is used to split a string into a list of words.
    Python for Everybody – Full University Python Course

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Python Project Collection: From Text Games to Automation Scripts

    Python Project Collection: From Text Games to Automation Scripts

    This text presents a series of Python coding projects aimed at beginner to intermediate programmers. It begins with a quiz game, then guides the reader through developing a number guesser, rock paper scissors game, mad libs generator, a timed math question app, turtle racing, a typing speed test, a Pathfinder game, and an alarm clock. Each project introduces new programming concepts, including functions, loops, conditional statements, user input, random number generation, file handling, and graphical elements using the curses and turtle modules. The projects increase in complexity, and offer opportunities for the programmer to build their skills. Emphasis is put on proper program structure and naming conventions. The text details how to implement various features, troubleshoot errors, and refine code for optimal functionality.

    Python Programming Study Guide

    Quiz

    Instructions: Answer the following questions in 2-3 sentences each.

    1. What is a Boolean in Python, and how does it relate to if statements?
    2. Explain the difference between the == and != operators in Python. Give an example using an if statement.
    3. Describe the purpose of the else statement and how it relates to an if statement.
    4. What is the significance of indentation in Python, particularly within if and else blocks?
    5. Explain what .lower() does and why it’s useful when getting input from users.
    6. What does it mean to “increment” a variable? Give an example of incrementing a score variable.
    7. What is the purpose of the random.randint() function, and how is it different from random.randrange()?
    8. What does the break statement do within a while loop?
    9. Explain the purpose of a continue statement within a while loop.
    10. What are the main differences between opening a file in “w,” “r,” and “a” modes?

    Quiz Answer Key

    1. A Boolean in Python is a data type that can have one of two values: True or False. Booleans are fundamental to if statements, as the condition within an if statement must evaluate to a Boolean value to determine whether the code block under the if statement will be executed.
    2. The == operator checks if two values are equal, while the != operator checks if two values are not equal. For example, if user_input == “yes”: executes the code block only if the variable user_input is equal to the string “yes,” while if user_input != “yes”: does the opposite.
    3. The else statement provides an alternative block of code to execute if the condition in the preceding if statement evaluates to False. It allows the program to take different paths based on whether the if condition is met or not, ensuring that some code is always executed.
    4. Indentation in Python is crucial for defining code blocks, especially within if and else statements. The lines of code that are indented after an if or else statement are considered part of that block and will only be executed if the corresponding condition is met.
    5. The .lower() method converts a string to lowercase. It’s useful when getting input from users because it allows the program to handle different casings (e.g., “Yes,” “YES,” “yes”) as the same value, ensuring consistent behavior regardless of user input.
    6. To “increment” a variable means to increase its value, usually by 1. For example, score += 1 adds 1 to the current value of the score variable.
    7. The random.randint(a, b) function generates a random integer between a and b (inclusive). random.randrange(a, b) generates a random integer between a and b (exclusive of b).
    8. The break statement immediately terminates the loop and transfers execution to the statement immediately following the loop.
    9. The continue statement skips the rest of the current iteration of the loop and proceeds to the next iteration.
    10. “w” opens a file for writing, creating a new file or overwriting an existing one. “r” opens a file for reading, requiring the file to already exist. “a” opens a file for appending, creating a new file if one doesn’t exist or adding to the end of an existing file.

    Essay Questions

    1. Discuss the importance of user input validation in Python programs, providing examples of how to validate different types of input (e.g., numbers, strings). Explain why input validation is crucial for program stability and security.
    2. Explain how to structure and implement a menu-driven program in Python using while loops, if/else statements, and functions. Detail the steps required to display options to the user, accept input, validate the input, and execute the corresponding actions.
    3. Discuss the different ways to generate random numbers in Python using the random module. Explain the differences between random.randint(), random.random(), and random.choice(), and provide examples of when to use each function.
    4. Explain the concept of file handling in Python. Describe how to open, read, write, and close files, and discuss the different modes for opening files (e.g., “r,” “w,” “a”).
    5. Discuss how to apply ANSI escape codes and characters within Python programming and explain its purpose. Provide examples to illustrate how these codes can be used to control cursor movements, modify text attributes, and manage the visual output in terminal-based applications.

    Glossary of Key Terms

    • Boolean: A data type that can have one of two values: True or False.
    • Condition: An expression that evaluates to a Boolean value (True or False), used in control flow statements like if.
    • Indentation: The spaces at the beginning of a code line used to define code blocks in Python.
    • .lower(): A string method that converts a string to lowercase.
    • Increment: To increase the value of a variable, usually by 1.
    • random.randint(): A function that generates a random integer between two specified values (inclusive).
    • random.randrange(): A function that generates a random integer within a specified range.
    • While Loop: A control flow statement that repeatedly executes a block of code as long as a specified condition is true.
    • Break Statement: A statement that terminates the current loop and transfers execution to the statement immediately following the loop.
    • Continue Statement: A statement that skips the rest of the current iteration of a loop and proceeds to the next iteration.
    • File Modes (r, w, a): Modes used when opening a file, where “r” is for reading, “w” is for writing (overwriting existing files), and “a” is for appending.
    • ANSI Escape Codes: special sequences of characters in programming that control text formatting (color, style) and cursor movement in terminal output.
    • List Comprehension: A concise way to create lists in Python using a single line of code.
    • Set: A data structure that stores unique, unordered elements.
    • Dictionary: A data structure that stores key-value pairs.
    • Slice: A subsection of a string or list, accessed using the slice operator [:].
    • String Concatenation: The process of combining two or more strings into a single string.
    • Modules: Reusable pieces of code or program units.
    • Parameters: Variables that get passed into a function.
    • Exception: An unusual condition or error that arises during code execution that changes the normal flow of a program’s execution.
    • String Literals: Refers to the values we assign to strings.
    • Argument: Information that is passed into a function.
    • Local Variable: A variable defined inside a function.
    • Global Constant: A constant that is defined outside of the scope of a function, in all uppercase lettering.
    • Data Structures: A data organization management and storage format that enables efficient access and modification.

    Python Concepts, Projects, and Techniques

    Okay, here’s a briefing document summarizing the main themes and ideas from the provided source material, with quotes. This document covers a range of Python programming concepts, projects, and problem-solving approaches.

    Briefing Document: Python Programming Concepts & Project Walkthroughs

    I. Core Programming Concepts (If/Else Statements, Loops, Functions, Variables, Data Structures)

    • Conditional Logic (If/Else): The material emphasizes the use of if and else statements for decision-making within programs. It explains the syntax (colon, indentation) and the concept of Boolean evaluation. “If this condition here does not evaluate to true you can put this lse statement here which means if this is not true whatever is in the else statement will run.”
    • Loops (While Loops): The use of while loops for repetitive tasks is also highlighted. The concept of break to exit a loop prematurely is introduced. “what this will do is just Reas them to type in rock paper scissors or Q so we could give some error message saying that’s not a valid option or something like that but what I’m going to do is just have it so it keeps asking them to type something again until eventually they give us something valid so when I hit continue that means anything after here is not going to happen”
    • Functions: Functions are presented as reusable blocks of code, improving organization and readability. “what a function is is an executable reusable block of code”
    • Variables: The use of variables to store data and the importance of data types (strings, integers, Booleans) are discussed. The use of underscores for multi-word variable names is encouraged. “you can make a variable using underscores when you want to have multiple words it makes a lot of sense to make the name with underscores so like top underscore of underscore range”
    • Data Structures (Lists, Sets, Dictionaries):Lists: Lists are introduced as ordered collections of elements. The syntax and use cases are demonstrated, especially in project examples. “a list is anything encapsulated in these square brackets…that is like separated by commas”
    • Sets: Sets are highlighted as collections that only contain unique elements, eliminating duplicates.
    • Dictionaries: Dictionaries are used for storing key-value pairs, facilitating data lookup and organization.

    II. String Manipulation & Input/Output

    • Input: The input() function is used to get user input, which is always returned as a string. “bind default when the user types something in it’s going to return it to us with double quotation mark so it’s going to be a string”
    • String Methods:.lower(): Converts a string to lowercase. Crucial for case-insensitive comparisons. “what lower does is it takes whatever text we type in and it just makes it completely lowercase”
    • .isdigit(): Checks if a string contains only digits.
    • .replace(): Replaces all occurrences of a substring within a string.
    • .endswith(): Check if a string ends with a certain value
    • String Formatting: The use of f-strings (formatted string literals) for embedding variables within strings.

    III. Random Number Generation

    • random.randint() and random.randrange(): These functions from the random module are used to generate random integers within specified ranges. The differences in inclusivity of the upper bound are explained. “Rand in works the exact same way as Rand range except now it will include 11 so the upper bound range now includes the number that you typed doesn’t go up to but not include it”

    IV. File Handling

    • Opening Files: The open() function is used to open files in different modes (“w” for write, “r” for read, “a” for append).
    • Context Manager (with open(…) as f:): The with statement is emphasized for file handling as it ensures automatic file closing. “as soon as you are done doing all of the operations with the file since we Ed this width it will automatically close the file for us”
    • Reading and Writing Files: The .read() and .write() methods are used for reading from and writing to files, respectively.
    • File Modes: w, r, and a are described.
    • Line Breaks: The use of n for creating new lines in text files.

    V. Modules & Packages

    • Import Statements: The use of import to bring in external modules and packages. Specific imports (e.g., from playsound import playsound) are also shown.
    • Key Modules:random: For random number generation.
    • time: For timing and delays.
    • os: For interacting with the operating system (file system operations, etc.).
    • json: For working with JSON data.
    • shutil: For file operations (copying, moving).
    • subprocess: For running external commands.
    • sys: For accessing system-specific parameters and functions, including command-line arguments.
    • curses: For controlling terminal output (especially on Unix-like systems).
    • pygame: for making games.
    • playsound: for playing sound

    VI. Project Overviews

    The source material outlines the structure and logic for several Python projects:

    • Computer Quiz: A quiz program that asks the user questions and provides feedback on their answers, implementing if/else statements, input, and scoring. “I’m going to say score is equal to zero this is going to allow us to keep track of how many correct answers they have now all we have to do here since we Define score equal to zero is every time the user gets a question correct we just need to increment score”
    • Number Guessing Game: A game where the user guesses a randomly generated number, with hints (“higher” or “lower”). Incorporates input validation and loops.
    • Rock Paper Scissors: A simple game against the computer, demonstrating random number generation, conditional logic, and loop control.
    • Choose Your Own Adventure: A text-based adventure game with multiple choices and outcomes, heavily using if/elif/else structures.
    • Password Manager: A project to store passwords (with a disclaimer about its security limitations), demonstrating file handling (reading, writing, appending), and encryption.
    • Slot Machine: A simulation of a slot machine, with random symbol generation, betting logic, and win calculation. “what we’ll do for our player scores is we’ll just have a list which contains all of the individual scores now we don’t know if we’re going to have two players three players or four players so we’re going to have this list kind of change size based on the number of players that we have”
    • Mad Libs Generator: A program that generates a Mad Libs story by taking user input for different types of words. “the idea here is that whenever we have these angle brackets we’re g to kind of treat that as a word that needs to be replaced so what we’ll do is we’ll take the story from chat gbt so I’m just going to copy this on my clipboard we’ll put it in a text file we’ll load that story in for all of those individual words we’ll then kind of grab them figure out what they are ask the user to give us a word for them and then and replace them in the story all right”
    • Typing Speed Test (WPM): This projects uses time to test the typing speed of the user
    • Alarm Clock: countdown clock made in Python
    • Password Generator: A program for generating random passwords based on user-specified criteria (length, inclusion of numbers/special characters).
    • Pathfinder: A maze navigator made in Python
    • Color Guessing Game: a Mastermind clone game
    • Aim Trainer: a game made in Pygame
    • Go game manager: Script to manage code for a program made in Go.

    VII. Advanced Techniques & Best Practices

    • Command-Line Arguments: The sys.argv list is used to access command-line arguments passed to a Python script, increasing flexibility. “you must pass a source and Target directory only”
    • List Comprehensions: used for writing code in one line
    • Try/Except Blocks: Used for error handling
    • Using chatGPT: To make programs

    Let me know if you would like me to elaborate on any of these points.

    Interactive Python Projects: Scripting Fundamentals

    ### **1. What is the purpose of the Python script shown in the sources?**

    The Python script demonstrates how to create simple interactive programs using basic Python syntax. It shows examples for building a computer quiz, a number guessing game, a rock-paper-scissors game, a choose-your-own-adventure game, a password manager, a Mad Libs generator, an aim trainer, a basic slot machine, a color guessing game, a countdown alarm, a typing speed test, and a data extraction tool for games written in Go. The script primarily teaches fundamental programming concepts such as variables, conditional statements (`if`, `else`), loops (`while`), functions, and user input.

    ### **2. How can the script take user input and respond accordingly?**

    The script uses the `input()` function to prompt the user for information. The user’s response is stored as a string in a variable. `if` and `else` statements are used to check the value of this string and execute different code blocks based on the user’s input. The `.lower()` method is used to convert the input to lowercase, allowing the script to handle variations in capitalization. For example, if a program is expecting “yes”, it will still work even if the user enters “YES” or “Yes”.

    ### **3. What is the role of ‘if’, ‘else’, and ‘elif’ statements in the script?**

    These are conditional statements. The `if` statement checks a condition. If the condition is true, the code block indented under the `if` statement is executed. If the condition is false, the code block is skipped. The `else` statement provides an alternative code block to execute if the `if` condition is false. The `elif` statement (short for “else if”) allows you to check multiple conditions in sequence. Only the code block corresponding to the first true condition is executed.

    ### **4. How are loops implemented and used in these projects?**

    The script uses `while` loops to repeat a block of code as long as a certain condition is true. For example, a `while` loop can be used to keep asking the user for input until they provide a valid response. The `break` statement is used to exit a loop prematurely, and the `continue` statement skips the rest of the current iteration and moves to the next one. `for` loops are used to iterate over elements of a sequence, such as the list of events in pgame.

    ### **5. How are functions defined and called in the script?**

    Functions are defined using the `def` keyword, followed by the function name, parentheses `()`, and a colon `:`. The code block that belongs to the function is indented below the `def` line. Functions can take arguments (inputs) within the parentheses. Functions are called by using the function name followed by parentheses, passing in any required arguments. Functions are often used to break programs into logical chunks, improving organization and reusability.

    ### **6. How does the script generate random numbers and use them to its advantage?**

    The `random` module is used to generate random numbers. `random.randint(a, b)` generates a random integer between `a` and `b` (inclusive). `random.randrange(start, stop, step)` generates a random number within a given range. This allows for creating unpredictable game elements, like dice rolls, computer choices in Rock Paper Scissors, random target placement in the Aim Trainer and randomized colors in the color guessing game.

    ### **7. How can external modules enhance the functionality of this code?**

    The script utilizes several external modules to extend its capabilities. The `pygame` module is used for graphics and game development, allowing for drawing shapes, handling user input, and managing game logic. The `playsound` module is used to play sound effects. The `curses` module is used for creating text-based user interfaces in the terminal, allowing for more interactive and visually appealing command-line applications. The `time` module facilitates time-related tasks such as pausing the program execution (`time.sleep`) or measuring elapsed time. The `os`, `json`, `shutil`, `subprocess`, and `sys` modules provide access to operating system features, JSON data handling, file manipulation, command execution, and system arguments.

    ### **8. How can the script work with external files for stories or data?**

    The script uses the `open()` function to open and read data from external text files. The `with open()` construct ensures that the file is properly closed after it is used. The `.read()` method reads the entire contents of a file into a string. This string can then be parsed to extract information, such as in the Mad Libs generator, where words enclosed in angle brackets are identified and replaced with user input.

    Python Computer Quiz Game: A Development Guide

    The source provides details on how to build a computer quiz game in Python. Here’s a breakdown:

    • Functionality The quiz game asks users a series of questions, evaluates their answers, and provides a score at the end.
    • Welcoming the User The game starts by welcoming the user.
    • User Input It prompts the user to indicate whether they want to play. The input() function is used to get user input. A prompt, such as “Do you want to play?”, is displayed to the user. The user’s response is stored in a variable.
    • Conditional Statements An if statement checks if the user wants to play. If the user does not type “yes”, the program quits using the quit() function.
    • Asking Questions The game asks the user questions using the input() function and stores the responses in a variable called answer.
    • Checking Answers An if statement compares the user’s answer to the correct answer. If the answer is correct, a message is printed.
    • Lowercasing The .lower() method converts the user’s input to lowercase to accommodate different capitalizations of “yes”.
    • Else Statement An else statement is used to print a message if the user’s answer is incorrect.
    • Scoring A score variable, initialized to zero, keeps track of the number of correct answers. The score increments by one each time the user answers correctly. The final score is displayed to the user, along with the percentage of correct answers.
    • Concatenation The + operator is used to concatenate strings. When combining a string with a number, the number must be converted to a string using str().

    The source code includes an example quiz with questions about computer acronyms:

    • What does CPU stand for?
    • What does GPU stand for?
    • What does RAM stand for?
    • What does PSU stand for?

    GPU Acronym Quiz: Assessment and Feedback

    The source mentions GPU as part of a quiz question about computer acronyms.

    In the quiz game described in the source, the user is asked “What does GPU stand for?”. This suggests that understanding what GPU stands for is part of the quiz’s assessment of basic computer knowledge.

    The quiz evaluates a user’s answer to this question and provides feedback. If the user answers correctly, the game acknowledges the correct answer. If the user answers incorrectly, the game provides a message indicating the answer was wrong. The game uses conditional statements (if and else) to check the user’s answer and provide appropriate feedback.

    RAM Acronym Quiz: Computer Knowledge Assessment

    The source mentions RAM as part of a quiz question about computer acronyms.

    In the quiz game described in the source, the user is asked “What does RAM stand for?”. This suggests that understanding what RAM stands for is part of the quiz’s assessment of basic computer knowledge.

    The quiz evaluates a user’s answer to this question and provides feedback. If the user answers correctly, the game acknowledges the correct answer. If the user answers incorrectly, the game provides a message indicating the answer was wrong. The game uses conditional statements (if and else) to check the user’s answer and provide appropriate feedback.

    Python’s Lowercase Method: Usage, Syntax, and Alternatives

    The source uses the .lower() method in Python to convert text to lowercase. Here’s how it’s applied:

    • Quiz Game The .lower() method is used to standardize user input in the quiz game. This ensures that the program evaluates the user’s answer correctly, regardless of whether they use capital letters or not. For example, if the correct answer is “yes”, the program will accept “Yes”, “YES”, or “yEs” as correct because the input is converted to lowercase before comparison.
    • Syntax The syntax for using the .lower() method is string.lower(). It is applied to a string variable. It returns a new string with all characters converted to lowercase.
    • Alternatives The source mentions .upper() as an alternative to .lower(). The .upper() method converts a string to uppercase. If using .upper(), the program would need to compare the user’s input to an uppercase version of the correct answer.

    Computer Quiz Game: Score Keeping

    The source explains score keeping as it relates to building a computer quiz game.

    Here’s a summary:

    • score Variable A variable named score is used to keep track of the number of correct answers.
    • Initialization The score variable is initialized to zero at the beginning of the game. This ensures that the score starts at zero before any questions are answered.
    • Incrementing the Score The score is incremented by one (score += 1) each time the user answers a question correctly. This reflects that the user has earned a point for that question.
    • Displaying the Final Score At the end of the quiz, the final score is displayed to the user. This provides feedback on their overall performance in the quiz.
    • Percentage Calculation Along with the raw score, the percentage of correct answers is also calculated and displayed. This provides a relative measure of the user’s performance, making it easier to understand their score in the context of the entire quiz.
    • Concatenation The + operator is used to concatenate strings when displaying the score. Because the score is a number, it must be converted to a string using str() before it can be concatenated with other strings in the output message.
    9 HOURS of Python Projects – From Beginner to Advanced

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Smart Contract Development with Viper and Python

    Smart Contract Development with Viper and Python

    The provided text is a series of excerpts from a course on building smart contracts using Viper, a Python-like language for Ethereum. The course progressively teaches smart contract development, starting with basic concepts and gradually introducing more advanced topics like testing and deployment using tools like Remix, Anvil, Titanoboa, and Moccasin. The instruction includes detailed code examples for various smart contract projects, such as a “Buy Me a Coffee” contract and an ERC-20 token. The lessons emphasize best practices, including secure key management and thorough testing methodologies, such as unit and fuzz testing. The final section introduces the concept of building a decentralized stablecoin smart contract.

    Smart Contract & Development Study Guide

    Quiz

    1. What does a revert in a smart contract do, and what happens to the gas spent? A revert undoes any state changes that occurred before the revert, effectively rolling back the transaction. The remaining gas that was allocated to the function call is sent back to the caller, though the gas used to reach the revert will still be paid for.
    2. Why do failed transactions on the blockchain still cost gas? Even if a transaction fails due to a revert, the Ethereum nodes still had to do work to process the transaction up to the point of the revert, including any state changes. This work requires computation, and so gas is still spent.
    3. How are oracles used in smart contracts? Oracles provide external data, such as price feeds, to smart contracts. Smart contracts cannot directly access external information, so oracles are used to bring real-world data onto the blockchain.
    4. Explain the difference between hiding and deleting the terminal in VS Code. Hiding the terminal with the ‘X’ or a keyboard shortcut maintains the current state and history of the terminal. Deleting the terminal with the trash can icon clears the history, removes all the previous lines, and kills the active terminal session.
    5. What are Linux commands and what are some common examples? Linux commands are instructions used to interact with the operating system from a command-line interface. Common examples include pwd (print working directory), cd (change directory), mkd (make directory), and ls (list directory contents).
    6. What is the purpose of the pyproject.toml file? The pyproject.toml file is used in Python projects to declare dependencies and other settings required for the project. It tells tools like moccasin and pip how to install and interact with the python project.
    7. How does the UV tool help manage different Python versions? UV allows you to easily switch between Python versions by pinning a version to your project via the python version file. This helps avoid compatibility issues between various scripts and packages that require specific Python versions.
    8. What are mock contracts and why are they used? Mock contracts are simulated versions of real smart contracts used for local testing, where complex logic or real-world dependencies can be replaced with simplified versions. They allow testing of smart contract logic in isolation.
    9. What is the difference between unit tests and integration tests? Unit tests are designed to test individual functions or small parts of a code in isolation, whereas integration tests check how different systems or contracts interact with one another.
    10. What is the basic idea behind fuzz testing? Fuzz testing involves throwing random data at your contract or system multiple times to discover bugs, vulnerabilities, and edge cases that might not be caught by traditional unit testing.

    Essay Questions

    1. Discuss the importance of using a development environment like VS Code for smart contract development. Explain how VS Code and its plugins can improve developer efficiency.
    2. Explain the “DRY” (Don’t Repeat Yourself) principle in the context of smart contract development. Provide specific examples from the source material of how the principle was applied and why it is important.
    3. Compare and contrast stateful and stateless fuzz testing, and explain how each type of fuzzing is used to discover different categories of vulnerabilities in smart contracts.
    4. Describe the concept of decentralized storage and the role that IPFS plays in it. Compare and contrast IPFS with traditional data storage methods and provide examples of where it is used in smart contract applications.
    5. Explain the fundamental concept of a stablecoin and some of the different design methodologies including the trade-offs of each. How is this achieved and what challenges are inherent to its design?

    Glossary of Key Terms

    • Revert: An operation in a smart contract that cancels any state changes within a transaction, sending gas back and rolling back updates.
    • Gas: A unit of computation cost in Ethereum, used to pay for executing smart contract code.
    • Oracle: A service or entity that provides external data, like price feeds, to smart contracts.
    • Linux commands: instructions used to interact with the operating system from a command-line interface.
    • pyproject.toml: used in Python projects to declare dependencies and settings.
    • UV: A Python tool for managing different Python versions.
    • Mock Contracts: Simplified versions of smart contracts used for local testing and development.
    • Unit Test: A type of test designed to verify small, individual pieces of code.
    • Integration Test: A test that verifies how different parts of a system or contract interact with each other.
    • Fuzz Testing: The process of testing a system or program with random data to discover potential errors and vulnerabilities.
    • Stateless Fuzzing: A type of fuzz test where each run is independent and does not depend on previous runs’ outcomes.
    • Stateful Fuzzing: A type of fuzz test where the tests can depend on the state or results of prior tests, allowing for more complex interactions to be tested.
    • IPFS (InterPlanetary File System): A decentralized storage system that allows files to be accessed through a content-addressing scheme rather than a centralized server.
    • CID: (Content Identifier) A unique identifier of data on the IPFS network, obtained by hashing the data.
    • SVG (Scalable Vector Graphics): A format for vector-based graphics that can be displayed within web browsers and directly encoded in URLs.
    • Base64: A binary-to-text encoding scheme used to encode data for transport over channels that only support text.
    • Merkle Root: A single hash representing a collection of data, used in Merkle trees to verify data integrity efficiently.
    • Defi (Decentralized Finance): A financial system that leverages blockchain and smart contract technology to disintermediate traditional financial structures.
    • Stablecoin: A cryptocurrency that attempts to maintain a stable value, often pegged to a fiat currency or another asset.
    • Airdrop: The distribution of a cryptocurrency or token to multiple wallet addresses.
    • Code Coverage: A measure of the amount of code that has been executed or tested by test suites.
    • Health Factor: A metric used to measure the collateralization of a position within a decentralized lending protocol.

    Smart Contract Development: A Comprehensive Guide

    Okay, here’s a detailed briefing document summarizing the provided sources, including key themes, important ideas, and relevant quotes:

    Briefing Document: Smart Contract Reverts, Development Environment Setup, Testing, and Advanced Concepts

    I. Source Overview

    The provided documents consist of a collection of excerpts from a course, likely aimed at training smart contract developers. The content covers several important areas including how reverts work in smart contracts, setting up a local development environment, how to write different types of tests, and more advanced concepts such as oracles, dependency management, fuzzing, NFTs and DeFi.

    II. Key Themes and Ideas

    • Reverts and Transaction Costs:Reverts undo any actions in a transaction before the revert was triggered. “anytime you see a revert anytime you see an assert like this that gets reverted it means it undoes any actions that happened before.”
    • Even if a transaction fails (reverts), gas is still spent because the Ethereum nodes have to do the work of executing the transaction and then undoing the state. “in the blockchain world if you send a transaction and it reverts essentially you’ve updated nothing…but you’ve spent money.”
    • Blockchain applications often include checks to prevent transactions that are likely to revert.
    • Smart Contract Funding and Assertions:Contracts can be funded by sending Ether (or other tokens), and logic can ensure a minimum amount is sent.
    • Assertions can be used to require that a condition is met otherwise a revert is triggered. The example shows using assert to ensure the msg.value is greater than a minimum amount.
    • The sources move from strict equality (==) asserts to greater than or equal (>=) asserts which increases flexibility.
    • Oracles and Chainlink:Oracles are essential for smart contracts to interact with real-world data, like USD prices of other assets. “this is the part where oracles and chain link come into play and oracles are an incredible important part of your smart contract developer Journey”
    • Chainlink is mentioned as a solution for getting external price information.
    • Development Environment Setup (VS Code & Terminal):The importance of a well-organized folder structure to keep projects separated. A new folder mo-cu (or similar) is created to hold files for this course. “for all the cyphon updraft course I recommend you making a brand new folder specifically to hold all of your files and folders for this curriculum”
    • Instructions for using the terminal within VS Code, including shortcuts to hide/show (Ctrl + ~ or Cmd + ~) and create a new terminal (Ctrl+Shift+~)
    • Use of Linux commands (e.g. pwd, cd, mkdir, ls) within the terminal to navigate the file system.
    • The use of code . to open the current folder in VS Code from the terminal is also mentioned as a shortcut.
    • The importance of saving files (Cmd + S on macOS) to avoid losing changes. A small dot next to the filename indicates an unsaved file.
    • Python Version Management and uv:uv is introduced as a tool for managing Python environments and dependencies.
    • uv can pin the project’s python version in a file named python-version, ensuring that it will run with the correct version. This helps avoid version conflicts. “UV is a great tool for actually automatically very easily switching between Python versions all you got to do is update this python version”
    • uv allows direct installation of python versions (uv python install 3.12)
    • Virtual environments can be created and activated using uv venv and then activating the shell.
    • Dependency Management
    • Moccasin can install packages from GitHub (MOX install <org>/<repo>) or PyPi (MOX install <package-name>).
    • pyproject.toml keeps track of project dependencies.
    • The lib directory is where all dependencies are installed.
    • You must activate a virtual environment before installing Pypi dependencies.
    • Moccasin Configuration and Manifest Filesmox.toml contains configurations for different networks. The networks.contracts section allows specification of deploy scripts for specific networks.
    • Top-level network contracts can be set up so that a default mock contract is deployed if an address is not specified.
    • The manifest_named function will check for an address in a network config, database, or finally, a deploy script.
    • Moccasin can track contract deployments in a database deployments.db.
    • You can access the most recently deployed contract with get latest contract unchecked or get latest contract checked.
    • Testing Methodologies:Unit tests test individual functions or code components.
    • Integration tests test different systems or contracts working together.
    • Fuzz tests use random inputs to attempt to break code. It is a way of checking invariants. “The basic idea behind fuzzing is just throwing random data at your contract in order to find a bug.”
    • “Stateless” fuzzing involves throwing random data at single function calls.
    • “Stateful” fuzzing involves running through complex sequences of transactions.
    • Hypothesis for Fuzzing:Hypothesis is a Python library used for writing fuzz tests.
    • The @given decorator specifies a range of random values for a variable.
    • Strategy is a type used to specify more complex inputs to tests such as a uint256.
    • The @settings decorator allows setting additional options on your test, including suppressing function-scoped fixture warnings.
    • Max examples can increase how many random test cases are run.
    • Hypothesis reports a “falsifying example” upon test failure, which can be used to recreate the bug.
    • NFTs:The source material goes over a basic NFT using a token URI stored on IPFS.
    • A dynamic NFT is created where the metadata is dynamically changed between a happy or sad SVG based on a variable on-chain.
    • SVGs can be encoded into a data URI, allowing them to be displayed directly in the browser.
    • IPFS (InterPlanetary File System)IPFS is a decentralized data storage network. “it’s this distributed decentralized data structure that’s not exactly a blockchain but it’s it’s similar to a blockchain”
    • Data is hashed on IPFS and then pinned by nodes.
    • Nodes choose which data to pin, unlike blockchains that replicate everything.
    • IPFS nodes communicate with each other to locate data based on the hash.
    • IPFS can be run through your local machine.
    • Merkle Trees and Airdrops:
    • A Merkle root is a compact way of encoding a large list of users.
    • The Merkle root can be used to authorize claims in an airdrop.
    • This reduces gas costs compared to using a large on-chain mapping.
    • Decentralized Stablecoins:A decentralized stablecoin is created.
    • Collateral can be deposited to mint the stablecoin.
    • The source goes over the key concepts such as:
    • Collateral types.
    • Exogenous vs endogenous.
    • The minting and burning process.
    • Health factors.
    • Liquidations.
    • The importance of using price feeds from chainlink is reemphasized.
    • The stablecoin relies on a health factor to determine if a user can mint or must be liquidated.
    • Liquidations occur if the price of collateral drops below a threshold.
    • Scripting:Scripts are used to interact with contracts, similar to devops.
    • A deploy.py file is used to deploy the contracts and interact with the blockchain.
    • Formatting:VS code extensions and command-line formatters, such as Ruff, help to format your code.
    • Section headers can make code more readable. This is implemented using the vhe-header tool.
    • Advanced ToolsJust is a command-line tool that allows developers to create compound commands.
    • MocksMock contracts are used in tests to simulate other contracts and services, such as price feeds.

    III. Important Quotes

    • On reverts: “anytime you see a revert anytime you see an assert like this that gets reverted it means it undoes any actions that happened before.”
    • On failed transactions: “in the blockchain world if you send a transaction and it reverts essentially you’ve updated nothing…but you’ve spent money.”
    • On the importance of oracles: “this is the part where oracles and chain link come into play and oracles are an incredible important part of your smart contract developer Journey”
    • On folder organization: “for all the cyphon updraft course I recommend you making a brand new folder specifically to hold all of your files and folders for this curriculum”
    • On uv: “UV is a great tool for actually automatically very easily switching between Python versions all you got to do is update this python version”
    • On fuzzing: “The basic idea behind fuzzing is just throwing random data at your contract in order to find a bug.”
    • On IPFS: “it’s this distributed decentralized data structure that’s not exactly a blockchain but it’s it’s similar to a blockchain”

    IV. Conclusion

    The sources provide a comprehensive introduction to smart contract development concepts and practices, covering everything from basic transaction handling to more complex topics such as testing strategies, dynamic NFTs, and building a decentralized stablecoin. The emphasis on testing, modular design, and practical use cases provides a good foundation for becoming a proficient smart contract developer.

    Smart Contracts, Testing, and Oracles

    1. What is a revert in the context of smart contracts, and what happens when it occurs?

    A revert in a smart contract is like an undo button. It cancels all actions that happened within the current function call and sends back any unused gas. For example, if a function updates a variable and then encounters a revert due to a failed assertion, the variable will revert to its original value as if the update never happened. All gas that wasn’t used by the function is returned to the sender.

    2. If a transaction fails due to a revert, does it still cost gas?

    Yes, even if a transaction fails due to a revert, you still pay gas. The Ethereum nodes have to perform work to execute the transaction up to the point of the revert, which includes updating the state of the contract before reverting it. Therefore, it is good practice to test and validate transactions before sending them to the blockchain.

    3. How can you prevent transactions that are likely to revert?

    Many applications in the blockchain space have built-in checks to see if a transaction is likely to revert before sending it. Remix and Metamask will often give you a warning and popup, asking you if you’re sure you want to send the transaction. You should do this before sending to avoid wasting gas.

    4. What are oracles and why are they important for smart contracts?

    Oracles are external data feeds that connect smart contracts to real-world information. This is important because smart contracts themselves cannot directly access information outside of the blockchain. Oracles allow for smart contracts to incorporate off-chain information such as prices, weather data, and other real-world data into their logic. In the example, chainlink is mentioned as a popular source for oracles providing price information for a smart contract.

    5. What is the purpose of the UV tool in the Python development environment?

    UV is a tool used to manage Python environments and dependencies. It allows developers to easily switch between different Python versions, making sure that scripts run with their intended versions. It handles the installation and management of Python packages within a specific project or environment. This ensures that the project runs consistently regardless of the global python installed, and removes any ambiguity when multiple versions are in place.

    6. What is the Manifest Named system and why is it useful?

    The Manifest Named system is a way to create a contract and define how it gets used, by letting you specify contracts by their name. If a specific network has a given contract at a specific address the contract will use that, otherwise, the contract can use a mock or deploy a new contract. This is helpful when using smart contracts that may exist on different networks or when you’re working in a test environment using a mock. This removes manual config and ensures you can switch between any network and the correct dependencies will be loaded.

    7. What is the difference between unit tests and integration tests, and which is better?

    Unit tests test individual functions or components of code. Integration tests are used to verify how multiple components work together. Both are necessary and have their own function. They are two different tests, with unit tests testing more fine-grained logic while integration tests test overall interactions and workflows.

    8. What is fuzz testing, and why is it a useful testing strategy?

    Fuzz testing involves supplying random, or “fuzz”, data to a program in an attempt to break it. This is especially important in smart contract development as it allows you to find edge cases and vulnerabilities that you might not have accounted for during standard testing. Fuzzing can help discover bugs that are caused by unexpected inputs or interactions in complex systems. In smart contracts, fuzzing is especially helpful because it can help catch security vulnerabilities.

    Smart Contracts: A Comprehensive Guide

    Smart contracts are a set of instructions executed in a decentralized, autonomous way without the need for a third party or centralized body to run them [1]. They are written in code and embodied on decentralized blockchain platforms [1].

    Smart contracts have several advantages over traditional contracts:

    • Decentralization: They have no centralized intermediary. Thousands of node operators running the same software and algorithms make the network decentralized [2, 3].
    • Transparency and Flexibility: Since all node operators run the software, everyone can see what is happening on the chain [2, 3].
    • Speed and Efficiency: Transactions happen instantly on the blockchain, without the need for clearing houses and settlement days [2, 3].
    • Security and Immutability: Once a smart contract is deployed, it cannot be altered or tampered with [2, 3]. Hacking a blockchain is also more difficult than hacking a centralized server [3].
    • Reduced Counterparty Risk: Smart contracts remove the risk of a party altering the terms of a deal because the code cannot be changed [3, 4].

    Smart contracts are used for a variety of applications, including:

    • Decentralized Finance (DeFi): DeFi gives users the ability to engage with finance and markets without a centralized intermediary [4].
    • Decentralized Autonomous Organizations (DAOs): DAOs are groups that are governed in a decentralized way by smart contracts [4].
    • Non-Fungible Tokens (NFTs): NFTs are unique digital assets [4].

    Hybrid smart contracts combine on-chain decentralized logic with off-chain decentralized data and computation [1, 2]. To accomplish this, they use decentralized oracle networks [1, 2].

    Layer 1 (L1) refers to any base-layer blockchain implementation, such as Bitcoin or Ethereum [5]. Layer 2 (L2) is any application built on top of a layer 1 [5]. Rollups are a type of L2 scaling solution that increases the number of transactions on Ethereum without increasing gas costs [5].

    Solidity is a popular programming language for writing smart contracts [6]. Viper is another smart contract programming language that is designed to be pythonic [6, 7].

    Other important concepts in smart contract development include:

    • Function visibility: external functions can be called by anyone outside the contract, whereas internal functions can only be called by other functions within the contract [8].
    • view functions are read-only but can read state and global variables, whereas pure functions are read-only and cannot read any state or global variables [9].
    • payable functions can receive ether [10, 11].
    • static call is a type of call that ensures that the called function cannot modify the state of the blockchain [11].
    • Interfaces define how a contract interacts with other contracts [11].
    • Constructors are functions that automatically run when a contract is deployed [12].
    • Fallback functions are triggered when no function is called in the contract [12].
    • Dynamic arrays can change in size, whereas fixed-size arrays cannot [12].
    • Mappings use keys to look up values, whereas arrays and lists are ordered [12].
    • Merkle trees use hashing to compress data [13].
    • Signatures are used to verify the authenticity of a message or transaction [13].
    • Proxies allow for upgradeable smart contracts via a delegatecall function [13].

    Smart contracts, blockchains, and cryptocurrencies can be used to create trust-minimized agreements or unbreakable promises [2].

    Viper Smart Contract Programming

    Viper is a smart contract programming language that is designed to be easy to learn, read, and write [1]. It is also intended to be easily understood by AI and security researchers, which can help reduce bugs [1]. Viper is designed to be pythonic, meaning it shares similar syntax with the Python programming language [1].

    Key features of Viper smart contracts include:

    • Trust-minimized agreements: Viper smart contracts allow for the creation of “trust-minimized agreements” or “unbreakable promises” [2]. Once created, smart contracts cannot be altered, thereby removing counterparty risk [2].
    • Transparency: The code of smart contracts can be viewed on the blockchain [2]. This provides transparency about how the contract will execute [2].
    • Decentralized Finance (DeFi): Viper smart contracts enable users to interact with finance and markets without a centralized intermediary, allowing them to engage with money markets and sophisticated financial products securely and efficiently [2].
    • Decentralized Autonomous Organizations (DAOs): DAOs, which are groups governed in a decentralized way by smart contracts, use Viper to define rules and make governance transparent [2].
    • Non-Fungible Tokens (NFTs): Viper smart contracts can be used to create NFTs, or unique digital assets, which can be used for art, collectibles, and more [2].
    • Interactions: Interactions with smart contracts are designed to be user-friendly, allowing users to interact without fear of being exploited [2].
    • EVM Compatibility: Viper smart contracts can be deployed on any EVM (Ethereum Virtual Machine) compatible blockchain or layer 2 (L2) solution [2, 3]. Some examples of EVM compatible chains are Ethereum, Arbitrum, Optimism, Polygon, and ZK sync [3].
    • Compiler: The Viper compiler is used to compile Viper code down to machine-readable code that can be executed by the EVM [3].
    • Interfaces: Viper uses interfaces to define how contracts interact with other contracts [4, 5]. An interface contains the names of functions and their parameters, but not the implementation of those functions [4].
    • Visibility: Functions can be declared as external, meaning they can be called by anyone outside of the contract, or internal, meaning they can only be called by other functions within the contract [4, 6].
    • Read-only functions: Functions can be declared as view or pure. Both are read-only, meaning that they cannot modify the state of the blockchain. However, a view function can read state and global variables, while a pure function cannot read any state or global variables [3].
    • Payable functions: Functions can be marked as payable, which allows them to receive ether [6].
    • Static Calls: A static call is a type of call that ensures that the called function cannot modify the state of the blockchain [5].
    • Constants and Immutables: Constants and immutables can save gas, and they are different than storage variables [5].
    • Constructors: Constructors, or init functions, are automatically called when a smart contract is deployed [5].
    • Fallback functions: Fallback functions are triggered when a contract receives ether and no function is called [5].
    • Arrays: Viper has both fixed-size and dynamic arrays. Fixed-size arrays have a defined size and cannot be changed, whereas dynamic arrays can change in size up to a maximum [5].
    • Mappings: Mappings use keys to look up values. Mappings are hard to reset, while dynamic arrays are easy to reset [5].

    Viper smart contracts can be written using a text editor and then compiled using the Viper compiler. Remix is a browser-based IDE that can be used for writing, compiling, and deploying Viper smart contracts [6, 7]. Smart contracts can also be deployed using command line tools such as Viper or Moccasin [8].

    Additional concepts in Viper include:

    • Modules: Viper smart contracts can use modules to organize and reuse code [9].
    • Libraries: Viper smart contracts can use libraries, such as snackmate, to import useful functions and contracts [10].
    • Events: Smart contracts can emit events that can be used to track activity on the blockchain [5].
    • Merkle Trees: Merkle trees use hashing to compress data [11]. They can be used to verify if an address is part of a list without having to store all the addresses on-chain [12].
    • Signatures: Signatures can be used to verify that a transaction was authorized by a specific address [13]. Viper uses the EIP-712 standard for structured data hashing and signing, which prevents replay attacks [12, 14].
    • Proxies: Proxies enable smart contracts to be upgraded by using a delegatecall [11].

    Ethereum Development

    Ethereum development involves creating and deploying applications on the Ethereum blockchain. These applications can range from simple transactions to complex decentralized applications (dApps) [1]. Ethereum is a popular platform for developing smart contracts and other decentralized applications due to its versatility and large community [1].

    Key aspects of Ethereum development include:

    • Smart Contracts: Ethereum enables the creation of smart contracts, which are self-executing contracts with the terms of the agreement written directly into code [1].
    • EVM: Smart contracts on Ethereum are compiled down to machine readable code for the Ethereum Virtual Machine (EVM) [2]. The EVM defines a set of rules or standards for how smart contract code should look [2].
    • EVM Compatibility: Many other blockchains and L2 solutions are also EVM-compatible, meaning that smart contracts written for Ethereum can be deployed on these other networks with little or no modification [2]. Some popular EVM compatible chains include Arbitrum, Optimism, Polygon, and ZK sync [2].
    • Transactions: All interactions with the Ethereum blockchain, whether deploying a contract, calling a function that updates the state of the blockchain, or transferring value, are done via transactions [3]. A transaction is a signed data package that contains information such as the sender’s address, the recipient’s address, a signature, the amount of ether to transfer, input data, and gas limits [4]. Each transaction has a unique identifier called a nonce [4].
    • Wallets: In order to interact with the Ethereum blockchain, users need a wallet such as Metamask [5]. Wallets store the user’s private keys and allow them to sign transactions.
    • Gas: Every transaction on the Ethereum network requires a certain amount of gas to be paid to the network for computation [6].
    • Testnets: Developers use test networks to test their smart contracts before deploying them to the main Ethereum network [5]. Test networks include Sepolia [7].
    • Virtual testnets: Developers can use virtual testnets to test smart contracts without using testnet tokens [7].

    Development tools for Ethereum include:

    • Remix: A browser-based IDE that can be used for writing, compiling, and deploying smart contracts [8].
    • Viper: A pythonic smart contract programming language that is designed to be easy to learn, read, and write, and it can be compiled with the Viper compiler [9, 10].
    • Moccasin: A Python-based framework for building and deploying smart contracts, as well as for testing and interacting with them [3, 10].
    • Tenderly: A platform for testing and monitoring smart contracts that can be used to create virtual testnets [7].
    • Web3.py: A Python library for interacting with the Ethereum blockchain [3].

    Smart contract development is critical for creating dApps, DeFi applications, DAOs, and NFTs [1, 9].

    Security Considerations

    It is important for developers to be aware of security considerations when developing on Ethereum, as there are risks of private key leaks [11]. Developers should:

    • Never store private keys or secret phrases in a .env file [11].
    • Use different wallets for testing and development than for real funds [11].
    • Encrypt private keys before storing them [11].

    Smart Contracts

    Smart contracts have many benefits over traditional contracts [1]:

    • Decentralization: Smart contracts have no centralized intermediary, and the network is decentralized due to thousands of node operators running the same software [9].
    • Transparency: Since all node operators run the same software, everyone can see what’s happening on the blockchain [9].
    • Speed and efficiency: Transactions occur instantly on the blockchain, eliminating the need for clearing houses and settlement days [9].
    • Security and immutability: Once a smart contract is deployed, it cannot be changed, and hacking a blockchain is more difficult than hacking a centralized server [9].
    • Reduced counterparty risk: Because the code cannot be altered, smart contracts remove the risk of a party altering the terms of a deal [9].

    Decentralized Applications (dApps)

    Ethereum can be used to create decentralized applications (dApps). These dApps are programs that run on a decentralized network, and they can be used for a wide variety of purposes [1, 9].

    • Decentralized Finance (DeFi): DeFi applications use smart contracts to enable users to interact with financial markets without intermediaries, offering services like lending, borrowing, and trading [1, 9].
    • Decentralized Autonomous Organizations (DAOs): DAOs are groups that are governed in a decentralized way by smart contracts [1, 9].
    • Non-Fungible Tokens (NFTs): NFTs are unique digital assets that can be used to represent a variety of items [1, 9].

    Hybrid smart contracts combine on-chain decentralized logic with off-chain decentralized data and computation by using decentralized oracle networks [1].

    Blockchain Technology Fundamentals

    Blockchain technology is a revolutionary system that enables secure, transparent, and decentralized transactions and agreements [1-3]. It is the foundation for cryptocurrencies and smart contracts and has the potential to transform many industries [3].

    Key concepts of blockchain technology include:

    • Decentralization: Blockchains operate on a network of independent nodes, rather than a centralized authority [4]. This makes the system more resistant to censorship and single points of failure [2, 5].
    • Immutability: Once data is added to the blockchain, it cannot be changed or tampered with [3-5]. This is achieved through the use of cryptographic hashing and consensus mechanisms [3].
    • Transparency: All transactions on the blockchain are publicly visible to anyone on the network [4, 6]. This promotes accountability and trust [3].
    • Cryptography: Blockchain technology uses cryptographic hashing to secure transactions and data [2, 3, 7-9]. This ensures that transactions are valid and that data cannot be altered without detection [2, 3].
    • Consensus Mechanisms: Blockchains use consensus mechanisms to ensure that all nodes agree on the state of the blockchain [5]. Proof of work and proof of stake are common consensus mechanisms that are used by different blockchains [5].

    Here are some additional aspects of blockchain technology:

    • Blocks: Data is organized into blocks, which are chained together to create a chronological record of all transactions [7]. Each block contains a hash of the previous block, which ensures the integrity of the chain [7, 8].
    • Hashing: A hash is a unique, fixed-length string that identifies a specific piece of data [7, 9]. It’s created by putting data through a hash function or algorithm [7-9]. Even a small change in the input data will result in a drastically different hash [7]. This process is used in blockchains to ensure that data is not tampered with [7-9].
    • Nodes: A blockchain network consists of many independent nodes [4, 5]. Each node maintains a copy of the blockchain and participates in verifying new transactions [4, 5].
    • Mining: In proof-of-work systems, mining is the process of finding the solution to a difficult problem, often requiring significant computational power [7, 9]. Miners are rewarded for verifying and adding new blocks to the blockchain [5, 9].
    • Layer 1 (L1): A layer 1 blockchain is the base layer of the blockchain ecosystem [10]. Examples of L1 chains include Bitcoin and Ethereum [10].
    • Layer 2 (L2): A layer 2 blockchain is built on top of a layer 1 to provide additional features and scalability [10, 11]. Rollups are a type of layer 2 solution that increases the number of transactions on a layer 1 without increasing gas costs [10].
    • Blobs: Blobs are a new transaction type that allows for storing data on-chain for a short period of time [12]. Blobs are used by L2s such as ZK Sync to reduce costs by making transaction data available without storing it on the L1 [12].

    Smart Contracts

    Blockchains can be used to execute smart contracts, which are self-executing agreements with the terms of the agreement written into code [1-3]. Smart contracts have many advantages over traditional contracts:

    • Trust-minimized agreements: Smart contracts create agreements that do not require trust between parties [1, 3].
    • Immutability: Once deployed, smart contracts cannot be altered or tampered with [3-5].
    • Transparency: Smart contract code is publicly visible on the blockchain [4, 6].
    • Speed and Efficiency: Transactions are executed instantly [3, 4].

    Applications of Blockchain

    Blockchain technology is used in a variety of applications:

    • Cryptocurrencies: Bitcoin and Ethereum are examples of cryptocurrencies that use blockchain technology to enable decentralized transactions [2, 3].
    • Decentralized Finance (DeFi): DeFi applications use smart contracts to enable users to interact with financial markets without intermediaries [13, 14].
    • Decentralized Autonomous Organizations (DAOs): DAOs are groups that are governed in a decentralized way by smart contracts [13].
    • Non-Fungible Tokens (NFTs): NFTs are unique digital assets that can be used to represent a variety of items [13].
    • Algorithmic trading: Smart contracts and blockchain technology can be used for algorithmic trading, enabling automated portfolio rebalancing and trades [14].

    Challenges Despite the many benefits of blockchain, there are also some challenges. One challenge is the scalability of blockchains. Layer 2 solutions such as rollups are one approach to address this scalability problem [3, 10-12]. Also, blockchain technology has a learning curve, so training developers is necessary to continue advancing the technology [1, 11].

    Smart Contract Testing Frameworks and Best Practices

    Testing frameworks are essential tools for smart contract developers to ensure their code functions correctly and securely [1, 2]. Testing is a critical part of smart contract development because bugs can lead to significant financial losses [2]. Several frameworks are available, each with different features and approaches to testing.

    Here are some key aspects of testing frameworks:

    • Unit Tests: These tests focus on individual functions or modules within a smart contract [3]. They verify that each part of the contract works as expected in isolation [3].
    • Integration Tests: These tests check how different parts of the system work together [3]. This involves testing the interactions between multiple smart contracts, or between a smart contract and other components of a system.
    • Testnets: These are simulated blockchain environments that mimic the real main network but use fake currency [1, 4]. Developers can use testnets to deploy and interact with their smart contracts in a realistic setting without risking real funds [1, 4]. Popular testnets include Sepolia [4].
    • Virtual or Local Networks: These are local or virtual blockchain networks that can be used for fast and efficient testing without using testnet tokens [1]. These can be set up to simulate the behavior of the main network [5].
    • Forked Networks: These are virtual networks that are forked from the main network, allowing developers to test smart contracts with real-world data and contract interactions, but without spending real money [3, 6]. They make API calls to the real blockchain for contract and data information that is not present on the local or virtual network [6].
    • Staging Tests: These tests involve deploying contracts to a production-like environment, such as an actual testnet, and calling the functions of those contracts on the network [3, 6].
    • Fuzzing: This is a type of automated testing where a large amount of random data is input into a program to find edge cases or security vulnerabilities [3, 7].
    • Invariant Testing: This involves defining properties of a smart contract that should always hold true, and then writing tests that check whether these properties are violated [7].
    • Code Coverage: Code coverage is a metric that shows how much of the codebase is being tested by the test suite [8, 9]. A high percentage of code coverage is an indication that the code has been thoroughly tested.

    Popular Testing Frameworks

    • Moccasin: This Python-based framework is used for building, deploying, testing, and interacting with smart contracts [2]. It includes features such as fixtures for setting up test environments, and it uses py test for organizing and running tests [2, 10]. Moccasin can be used to simulate various network conditions and interactions to achieve high-quality code and more effective testing [5]. Moccasin allows for tests to be written using Python, and it includes built-in cheat codes to easily test smart contract functionality [11]. It also supports forked tests, staging tests, and test coverage reports [6, 8].
    • Foundry: This is a smart contract development framework that includes a tool called Anvil which can be used to run a local or virtual blockchain [5]. It also has built-in fuzzing and invariant testing features [7].
    • Brownie: This is a Python-based framework for deploying and interacting with smart contracts, which includes testing tools [12].
    • Pytest: This is a general-purpose testing framework for Python that is used by Moccasin [2, 10]. It looks for the test keyword on different functions in a test folder [2].
    • Tenderly: This is a platform for testing and monitoring smart contracts [1]. Tenderly can be used to create virtual testnets, and it allows developers to simulate transactions and debug issues [1].

    Test Organization

    • Tests are often organized into folders, such as unit, integration, fuzz, and staging [3].
    • Fixtures: Fixtures are functions that set up a test environment, such as by deploying contracts or setting balances [10]. Fixtures can be scoped to run before each test function, or before an entire test session [10].
    • Configuration files: Configuration files, such as conf test.py in Moccasin, are used to share fixtures and other configurations across test files [3, 10].

    Key Testing Concepts

    • Assertions: Assertions are used to check that a test passes if a condition is met, and fails if it is not. [2].
    • Reverts: Smart contracts are expected to revert if a function is called with invalid parameters or under invalid conditions [11]. Tests should verify that functions revert correctly when they are expected to [11].
    • Pranking: This is a feature that enables tests to simulate different users or conditions [6, 11].
    • Mocking: Mocking is a way to simulate a dependency, so a smart contract can be tested even when that dependency is not available [6]. Mocking involves replacing real dependencies with simulated ones to test contract logic in isolation.
    • Gas Profiling: Some frameworks such as Moccasin allow developers to analyze how much gas a contract is using [8].
    • Logging: Smart contracts can write events or logs to a special data structure in the EVM that cannot be accessed by other smart contracts [12, 13]. These events are important for indexers and off-chain applications that need to track changes to smart contracts, and they can be used in tests to verify contract behavior [12, 13].

    Best Practices

    • Write unit tests first to test individual functions [3].
    • Use fixtures to set up common test environments and share test configurations [10].
    • Use forked networks to test with real world data [3, 6].
    • Write fuzz tests to identify unexpected inputs or edge cases [3, 7].
    • Always test that functions revert when they are expected to [11].
    • Aim for high code coverage [8].
    • Always run tests before deploying to a live network [6].
    • Consider multiple audits for your smart contracts by different auditors [14].

    By using these testing frameworks and following these best practices, developers can significantly improve the quality and security of their smart contracts [2].

    Vyper and Python Smart Contracts on Blockchain – Full Course for Beginners

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Algorithmic Trading: Machine Learning & Quant Strategies with Python

    Algorithmic Trading: Machine Learning & Quant Strategies with Python

    This comprehensive course focuses on algorithmic trading, machine learning, and quantitative strategies using Python. It introduces participants to three distinct trading strategies: an unsupervised learning strategy using S&P 500 data and K-means clustering, a Twitter sentiment-based strategy for NASDAQ 100 stocks, and an intraday strategy employing a GARCH model for volatility prediction on simulated data. The course covers data preparation, feature engineering, backtesting strategies, and the role of machine learning in trading, while emphasizing that the content is for educational purposes only and not financial advice. Practical steps for implementing these strategies in Python are demonstrated, including data download, indicator calculation, and portfolio construction and analysis.

    Podcast

    Listen or Download Podcast – Algorithmic Trading: Machine Learning

    Algorithmic Trading Fundamentals and Opportunities

    Based on the sources, here is a discussion of algorithmic trading basics:

    Algorithmic trading is defined as trading on a predefined set of rules. These rules are combined into a strategy or a system. The strategy or system is developed using a programming language and is run by a computer.

    Algorithmic trading can be used for both manual and automated trading. In manual algorithmic trading, you might use a screener developed algorithmically to identify stocks to trade, or an alert system that notifies you when conditions are triggered, but you would manually execute the trade. In automated trading, a complex system performs calculations, determines positions and sizing, and executes trades automatically.

    Python is highlighted as the most popular language used in algorithmic trading, quantitative finance, and data science. This is primarily due to the vast amount of libraries available in Python and its ease of use. Python is mainly used for data pipelines, research, backtesting strategies, and automating low complexity systems. However, Python is noted as a slow language, so for high-end, complicated systems requiring very fast trade execution, languages like Java or C++ might be used instead.

    The sources also present algorithmic trading as a great career opportunity within a huge industry, with potential jobs at hedge funds, banks, and prop shops. Key skills needed for those interested in this field include Python, backtesting strategies, replicating papers, and machine learning in trading.

    Machine Learning Strategies in Algorithmic Trading

    Drawing on the provided sources, machine learning plays a significant role within algorithmic trading and quantitative finance. Algorithmic trading itself involves trading based on a predefined set of rules, which are combined into a strategy or system developed using a programming language and run by a computer. Machine learning can be integrated into these strategies.

    Here’s a discussion of machine learning strategies as presented in the sources:

    Role and Types of Machine Learning in Trading

    Machine learning is discussed as a key component in quantitative strategies. The course overview explicitly includes “machine learning in trading” as a topic. Two main types of machine learning are mentioned in the context of their applications in trading:

    1. Supervised Learning: This can be used for signal generation by making predictions, such as generating buy or sell signals for an asset based on predicting its return or the sign of its return. It can also be applied in risk management to determine position sizing, the weight of a stock in a portfolio, or to predict stop-loss levels.
    2. Unsupervised Learning: The primary use case highlighted is to extract insights from data. This involves analyzing financial data to discover patterns, relationships, or structures, like clusters, without predefined labels. These insights can then be used to aid decision-making. Specific unsupervised learning techniques mentioned include clustering, dimensionality reduction, anomaly detection, market regime detection, and portfolio optimization.

    Specific Strategies Covered in the Course

    The course develops three large quantitative projects that incorporate or relate to machine learning concepts:

    1. Unsupervised Learning Trading Strategy (Project 1): This strategy uses unsupervised learning (specifically K-means clustering) on S&P 500 stocks. The process involves collecting daily price data, calculating various technical indicators (like Garmon-Class Volatility, RSI, Bollinger Bands, ATR, MACD, Dollar Volume) and features (including monthly returns for different time horizons and rolling Fama-French factor betas). This data is aggregated monthly and filtered to the top 150 most liquid stocks. K-means clustering is then applied to group stocks into similar clusters based on these features. A specific cluster (cluster 3, hypothesized to contain stocks with good upward momentum based on RSI) is selected each month, and a portfolio is formed using efficient frontier optimization to maximize the Sharpe ratio for stocks within that cluster. This portfolio is held for one month and rebalanced. A notable limitation mentioned is that the project uses a stock list that likely has survivorship bias.
    2. Twitter Sentiment Investing Strategy (Project 2): This project uses Twitter sentiment data on NASDAQ 100 stocks. While it is described as not having “machine learning modeling”, the core idea is to demonstrate how alternative data can be used to create a quantitative feature for a strategy. An “engagement ratio” is calculated (Twitter comments divided by Twitter likes). Stocks are ranked monthly based on this ratio, and the top five stocks are selected for an equally weighted portfolio. The performance is then compared to the NASDAQ benchmark (QQQ ETF). The concept here is feature engineering from alternative data sources. Survivorship bias in the stock list is again noted as a limitation that might skew results.
    3. Intraday Strategy using GARCH Model (Project 3): This strategy focuses on a single asset using simulated daily and 5-minute intraday data. It combines signals from two time frames: a daily signal derived from predicting volatility using a GARCH model in a rolling window, and an intraday signal based on technical indicators (like RSI and Bollinger Bands) and price action patterns on 5-minute data. A position (long or short) is taken intraday only when both the daily GARCH signal and the intraday technical signal align, and the position is held until the end of the day. While GARCH is a statistical model, not a typical supervised/unsupervised ML algorithm, it’s presented within this course framework as a quantitative prediction method.

    Challenges in Applying Machine Learning

    Applying machine learning in trading faces significant challenges:

    • Theoretical Challenges: The reflexivity/feedback loop makes predictions difficult. If a profitable pattern predicted by a model is exploited by many traders, their actions can change the market dynamics, making the initial prediction invalid (the strategy is “arbitraged away”). Predicting returns and prices is considered particularly hard, followed by predicting the sign/direction of returns, while predicting volatility is considered “not that hard” or “quite straightforward”.
    • Technical Challenges: These include overfitting (where the model performs well on training data but fails on test data) and generalization issues (the model doesn’t perform the same in real-world trading). Nonstationarity in training data and regime shifts can also ruin model performance. The black box nature of complex models like neural networks can make them difficult to interpret.

    Skills for Algorithmic Trading with ML

    Key skills needed for a career in algorithmic trading and quantitative finance include knowing Python, how to backtest strategies, how to replicate research papers, and understanding machine learning in trading. Python is the most popular language due to its libraries and ease of use, suitable for research, backtesting, and automating low-complexity systems, though slower than languages like Java or C++ needed for high-end, speed-critical systems.

    In summary, machine learning in algorithmic trading involves using models, primarily supervised and unsupervised techniques, for tasks like signal generation, risk management, and identifying patterns. The course examples illustrate building strategies based on clustering (unsupervised learning), engineering features from alternative data, and utilizing quantitative prediction models like GARCH, while also highlighting the considerable theoretical and technical challenges inherent in this field.

    Algorithmic Trading Technical Indicators and Features

    Technical indicators are discussed in the sources as calculations derived from financial data, such as price and volume, used as features and signals within algorithmic and quantitative trading strategies. They form part of the predefined set of rules that define an algorithmic trading system.

    The sources mention and utilize several specific technical indicators and related features:

    • Garmon-Class Volatility: An approximation to measure the intraday volatility of an asset, used in the first project.
    • RSI (Relative Strength Index): Calculated using the pandas_ta package, it’s used in the first project. In the third project, it’s combined with Bollinger Bands to generate an intraday momentum signal. In the first project, it was intentionally not normalized to aid in visualizing clustering results.
    • Bollinger Bands: Includes the lower, middle, and upper bands, calculated using pandas_ta. In the third project, they are used alongside RSI to define intraday trading signals based on price action patterns.
    • ATR (Average True Range): Calculated using pandas_ta, it requires multiple data series as input, necessitating a group by apply methodology for calculation per stock. Used as a feature in the first project.
    • MACD (Moving Average Convergence Divergence): Calculated using pandas_ta, also requiring a custom function and group by apply methodology. Used as a feature in the first project.
    • Dollar Volume: Calculated as adjusted close price multiplied by volume, often divided by 1 million. In the first project, it’s used to filter for the top 150 most liquid stocks each month, rather than as a direct feature for the machine learning model.
    • Monthly Returns: Calculated for different time horizons (1, 2, 3, 6, 9, 12 months) using the percent_change method and outliers are handled by clipping. These are added as features to capture momentum patterns.
    • Rolling Factor Betas: Derived from Fama-French factors using rolling regression. While not traditional technical indicators, they are quantitative features calculated from market data to estimate asset exposure to risk factors.

    In the algorithmic trading strategies presented, technical indicators serve multiple purposes:

    • Features for Machine Learning Models: In the first project, indicators like Garmon-Class Volatility, RSI, Bollinger Bands, ATR, and MACD, along with monthly returns and factor betas, form an 18-feature dataset used as input for a K-means clustering algorithm. These features help the model group stocks into clusters based on their characteristics.
    • Signal Generation: In the third project, RSI and Bollinger Bands are used directly to generate intraday trading signals based on price action patterns. Specifically, a long signal occurs when RSI is above 70 and the close price is above the upper Bollinger band, and a short signal occurs when RSI is below 30 and the close is below the lower band. This intraday signal is then combined with a daily signal from a GARCH volatility model to determine position entry.

    The process of incorporating technical indicators often involves:

    • Calculating the indicator for each asset, frequently by grouping the data by ticker symbol. Libraries like pandas_ta simplify this process.
    • Aggregating the calculated indicator values to a relevant time frequency, such as taking the last value for the month.
    • Normalizing or scaling the indicator values, particularly when they are used as features for machine learning models. This helps ensure features are on a similar scale.
    • Combining technical indicators with other data types, such as alternative data (like sentiment in Project 2, though not a technical indicator based strategy) or volatility predictions (like the GARCH model in Project 3), to create more complex strategies.

    In summary, technical indicators are fundamental building blocks in the algorithmic trading strategies discussed, serving as crucial data inputs for analysis, feature engineering for machine learning models, and direct triggers for trading signals. Their calculation, processing, and integration are key steps in developing quantitative trading systems.

    Algorithmic Portfolio Optimization and Strategy

    Based on the sources, portfolio optimization is a significant component of the quantitative trading strategies discussed, particularly within the context of machine learning applications.

    Here’s a breakdown of how portfolio optimization is presented:

    • Role in Algorithmic Trading Portfolio optimization is explicitly listed as a topic covered in the course, specifically within the first module focusing on unsupervised learning strategies. It’s also identified as a use case for unsupervised learning in trading, alongside clustering, dimensionality reduction, and anomaly detection. The general idea is that after selecting a universe of stocks, optimization is used to determine the weights or magnitude of the position in each stock within the portfolio.
    • Method: Efficient Frontier and Maximizing Sharpe Ratio In the first project, the strategy involves using efficient frontier optimization to maximize the Sharpe ratio for the stocks selected from a particular cluster. This falls under the umbrella of “mean variance optimization”. The goal is to find the weights that yield the highest Sharpe ratio based on historical data.
    • Process and Inputs To perform this optimization, a function is defined that takes the prices of the selected stocks as input. The optimization process involves several steps:
    • Calculating expected returns for the stocks, using methods like mean_historical_return.
    • Calculating the covariance matrix of the stock returns, using methods like sample_covariance.
    • Initializing the EfficientFrontier object with the calculated expected returns and covariance matrix.
    • Applying constraints, such as weight bounds for individual stocks. The sources mention potentially setting a maximum weight (e.g., 10% or 0.1) for diversification and a dynamic lower bound (e.g., half the weight of an equally weighted portfolio).
    • Using a method like max_sharpe on the efficient frontier object to compute the optimized weights.
    • The optimization requires at least one year of historical daily price data prior to the optimization date for the selected stocks.
    • Rebalancing Frequency In the first project, the portfolio is formed using the optimized weights and held for one month, after which it is rebalanced by re-optimizing the weights for the next month’s selected stocks.
    • Challenges and Workarounds A practical challenge encountered during the implementation is that the optimization solver can sometimes fail, resulting in an “infeasible” status. When the Max Sharpe optimization fails, the implemented workaround is to default to using equal weights for the portfolio in that specific month.
    • Contrast with Other Strategies Notably, the second project, the Twitter sentiment investing strategy, is explicitly described as not having “machine learning modeling”, and it does not implement efficient frontier optimization. Instead, it forms an equally weighted portfolio of the top selected stocks each month. This highlights that while portfolio optimization, particularly using sophisticated methods like Efficient Frontier, is a key strategy, simpler approaches like equal weighting are also used depending on the strategy’s complexity and goals.

    Twitter Sentiment Trading Strategy Using Engagement Ratio

    Based on the sources, Sentiment analysis is discussed in the context of a specific quantitative trading strategy referred to as the Twitter sentiment investing strategy. This strategy forms the basis of the second project covered in the course.

    Here’s what the sources say about sentiment analysis and its use in this strategy:

    • Concept: Sentiment investing focuses on analyzing how people feel about certain stocks, industries, or the overall market. The underlying assumption is that public sentiment can impact stock prices. For example, if many people express positive sentiment about a company on Twitter, it might indicate that the company’s stock has the potential to perform well.
    • Data Source: The strategy utilizes Twitter sentiment data specifically for NASDAQ 100 stocks. The data includes information like date, symbol, Twitter posts, comments, likes, impressions, and a calculated “Twitter sentiment” value provided by a data provider.
    • Feature Engineering: Rather than using the raw sentiment or impressions directly, the strategy focuses on creating a derivative quantitative feature called the “engagement ratio”. This is done to potentially create more value from the data.
    • The engagement ratio is calculated as Twitter comments divided by Twitter likes.
    • The reason for using the engagement ratio is to gauge the actual engagement people have with posts about a company. This is seen as more informative than raw likes or comments, partly because there can be many bots on Twitter that skew raw metrics. A high ratio (comments as much as or more than likes) suggests genuine engagement, whereas many likes and few comments might indicate bot activity.
    • Strategy Implementation:
    • The strategy involves calculating the average engagement ratio for each stock every month.
    • Stocks are then ranked cross-sectionally each month based on their average monthly engagement ratio.
    • For portfolio formation, the strategy selects the top stocks based on this rank. Specifically, the implementation discussed selects the top five stocks for each month.
    • A key characteristic of this particular sentiment strategy, in contrast to the first project, is that it does not use machine learning modeling.
    • Instead of portfolio optimization methods like Efficient Frontier, the strategy forms an equally weighted portfolio of the selected top stocks each month.
    • The portfolio is rebalanced monthly.
    • Purpose: The second project serves to demonstrate how alternative or different data, such as sentiment data, can be used to create a quantitative feature and a potential trading strategy.
    • Performance: Using the calculated engagement ratio in the strategy showed that it created “a little bit of value above the NASDAQ itself” when compared to the NASDAQ index as a benchmark. Using raw metrics like average likes or comments for ranking resulted in similar or underperformance compared to the benchmark.
    Algorithmic Trading – Machine Learning & Quant Strategies Course with Python

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Python Foundations and Generative AI Applications

    Python Foundations and Generative AI Applications

    This text provides a comprehensive guide to Python programming, starting with fundamental concepts such as installing Python, understanding variables, data types, operators, and flow control statements like decision making and loops. It progresses to core data structures like tuples, lists, dictionaries, and sets, explaining their uses and manipulations. More advanced topics covered include object-oriented programming (classes, objects, inheritance) and file handling, with practical demonstrations using the PyCharm IDE. The document also explores data structures and algorithms, detailing arrays, stacks, queues, linked lists, and essential sorting (insertion, quick, merge) and searching (linear, binary) techniques. Finally, it touches upon machine learning libraries like NumPy, Pandas, Matplotlib, and Seaborn for data analysis and visualization, as well as an introduction to generative AI and using libraries like Flask and OpenAI for applications, alongside web automation with Selenium and GUI development with Tkinter.

    Python Fundamentals: Core Concepts and Structures

    Based on the sources provided, Python fundamentals cover the essential building blocks you need to start coding in Python. This module aims to introduce you to Python’s syntax and core concepts.

    Key topics discussed under Python fundamentals include:

    • Installing Python and Setting up the Environment: The journey begins with installing Python onto your system. Python is platform-independent, meaning you can download it for Windows, Linux, or Mac operating systems from the python.org downloads site. After installing Python, you need an Integrated Development Environment (IDE) to make coding easier. The sources mention PyCharm and Anaconda as IDE options for Python. Anaconda is a complete toolkit often used for machine learning and data science tasks, providing libraries like NumPy, pandas, matplotlib, and seaborn, along with an IDE called Jupyter notebook. Jupyter is a browser-based interpreter that allows interactive work with Python. You can open Jupyter notebook via the Anaconda prompt by typing jupyter notebook.
    • Variables: Variables are used to store data when working with any programming language. In Python, you can assign values to variables, and these values can be changed later.
    • Data Types: Every variable in Python has an associated data type. The sources highlight four main built-in data types: Integer, Float, Boolean, and String. It also mentions a Complex type, represented with ‘j’ instead of ‘i’. Examples are given for creating integer and complex variables and checking their type using the type() method.
    • Operators: Operators help perform operations on data. The sources discuss arithmetic operators, relational operators, and logical operators. Arithmetic operations are demonstrated, such as division. Relational operators help find the relationship between two operands, like checking if one is less than or greater than another.
    • Python Tokens: A Python token is the smallest meaningful component in a program. Combining tokens forms your Python code. The basic Python tokens are keywords, identifiers, literals, and operators.
    • Keywords: Special reserved words that cannot be used for other purposes, such as variable, function, or class names. Examples include if, def, del, True, False, while, not, or, return. The Python interpreter recognizes these keywords, often highlighting them (e.g., turning green). Trying to assign a value to a keyword like def results in an error.
    • Identifiers: Names used for variables, functions, or objects. There are basic rules for identifiers: they cannot contain special characters (except underscore), are case-sensitive, and the first letter cannot be a digit. The case sensitivity means N1 and n1 are treated as different variables.
    • Literals: These are constants, meaning values that do not change. Whatever values you store inside a variable are called literals. For example, in N1 = 10, the value 10 is a literal.
    • Flow Control Statements: These statements determine the order in which program code is executed. An example of an if-else statement is shown, demonstrating how to check a condition (e.g., if variable B is greater than variable A) and execute a block of code if the condition is true. Loops are also mentioned in the context of applying operations to elements in a list.
    • Core Data Structures: Python fundamentals introduce several basic data structures beyond single variables, enabling you to store multiple values.
    • Tuples: Described as continuous sequences of elements. Indexing starts from zero. When slicing, the starting index is inclusive, and the ending index is exclusive. Functions like max() can be used on tuples.
    • Lists: A new list is created using square braces. Lists can store different types of data values. Like tuples and other data structures in Python, indexing for lists starts from zero. You can extract individual elements or a series of elements using indexing and slicing, keeping in mind the exclusive nature of the ending index in slicing. Lists are mutable, meaning you can change elements after creation, unlike tuples (though tuples aren’t explicitly stated as immutable in the source excerpts, lists are explicitly called mutable in comparison to dictionaries which are also mutable). You can modify elements by assigning a new value to a specific index. List methods include append() to add an element to the end, pop() to remove an element (which follows LIFO – Last In, First Out – order, useful for implementing stacks), sort() for alphabetical sorting, and reverse(). You can also repeat list elements using multiplication. List implementation can be used to create a stack.
    • Dictionaries: An unordered collection of key-value pairs enclosed within curly braces. Dictionaries are mutable. Elements can be removed using the pop() method by providing the key.
    • Sets: An unordered and unindexed collection of elements enclosed within square braces (though the source excerpt says square braces, the example uses curly braces which is standard for sets in Python). Sets allow finding common elements using the intersection() method.

    By the end of this section, you should be comfortable writing simple Python programs and ready for more complex challenges.

    Python Data Structures Overview

    Based on the sources, discussing data structures involves exploring how data can be organized and stored to be used efficiently in programming. Understanding data structures is key to managing your data effectively. In Python, various data structures are available, ranging from built-in core types to more complex structures used in algorithms and specific libraries.

    Here’s a breakdown of the data structures discussed in the sources:

    Core Python Data Structures Python’s fundamentals introduce several basic data structures that allow you to store multiple values, unlike single variables which store only one value. You can store elements of different types within these data structures.

    • Tuples:
    • Tuples are described as a collection of elements enclosed within round braces.
    • They are an ordered collection.
    • Tuples are immutable, meaning that once the elements inside a tuple are created, you cannot change them later on.
    • Indexing for tuples starts from zero. You can find the length (number of elements) of a tuple using the len() method.
    • You can also concatenate (attach elements of) two tuples.
    • Lists:
    • Lists are an ordered collection of elements enclosed within square braces.
    • Unlike tuples, lists are mutable, which means you can actually change the values present in a list after it’s created.
    • Lists can store different types of data values.
    • Indexing for lists starts from zero, similar to tuples and other Python data structures. You can extract individual elements or a series of elements using indexing and slicing.
    • Common list methods mentioned include append() to add an element to the end, pop() to remove an element (following a Last In, First Out – LIFO – order), sort() for alphabetical sorting, and reverse().
    • You can also repeat list elements using multiplication.
    • Lists can be used to implement a stack.
    • Dictionaries:
    • Dictionaries are an unordered collection of key-value pairs.
    • They are enclosed within curly braces.
    • Dictionaries are mutable [implied by operations like pop, and noted in prior conversation].
    • In a dictionary, keys are separated from their values by a colon, and key-value pairs are separated by commas. For example, a dictionary could store fruit names as keys and their quantities as values.
    • You can extract the individual keys and values present in a dictionary. The keys are on the left side of the colon. You can use the .keys() method to get all the keys.
    • Elements (key-value pairs) can be removed from a dictionary using the pop() method by providing the key.
    • Sets:
    • Sets are an unordered and unindexed collection of elements.
    • Although one source mentions square braces, the example provided uses curly braces, which is the standard Python syntax for sets.
    • Sets allow finding common elements between two sets using the intersection() method. You can also combine elements from two sets using the Union() method.

    Data Structures for Algorithms Beyond the basic Python data structures, the sources delve into other common data structures, particularly in the context of data structures and algorithms, often described as advanced concepts. These are typically linear data structures where elements are stored in a linear fashion.

    • Arrays:
    • An array is a linear data structure where elements are stored in a linear fashion and at continuous memory locations.
    • Each memory location has an address.
    • The data type of elements stored in an array must be homogeneous, meaning you can only store similar elements.
    • You can access elements randomly using indexing. The name of the array represents its Base address.
    • Arrays are useful for scenarios where you want to store data linearly in continuous memory for efficient memory utilization and are suitable for frequent searching.
    • Arrays can be one-dimensional or two-dimensional (used for matrices). A 1D array is declared with a name, data type, one subscript/index, and size.
    • Arrays can serve as a replacement for multiple individual variables when dealing with a large number of similar data points.
    • A drawback of arrays is that insertion and deletion can be difficult because it requires swapping elements and ensuring continuous memory is available.
    • Note: While the sources discuss arrays conceptually and show examples using list-like syntax, Python’s built-in list type is more flexible than traditional C-style arrays (e.g., can store heterogeneous data, doesn’t require fixed size). NumPy arrays are closer to the homogeneous, fixed-size array concept.
    • Stacks:
    • A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This means the element inserted last is the first one to be removed.
    • Insertion and removal of elements are done at one end, often called the “top” of the stack.
    • Standard stack operations include push (to insert an element at the end/top) and pop (to remove an element from the end/top).
    • In Python, stacks can be implemented using a list (using append for push and pop for pop), the collections.deque class, or the queue.LifoQueue class.
    • The collections.deque implementation is preferred over lists for stack operations because append and pop are faster (Big O(1) time complexity) compared to lists, which can become slow due to potential memory reallocations (Big O(N)).
    • When using queue.LifoQueue, the insertion operation is called put, and the removal operation is called get.
    • Queues:
    • A queue is a linear data structure where elements are stored in a linear fashion and follow the First In, First Out (FIFO) principle. The first item inserted is the first item to be removed.
    • You can imagine a queue like people waiting in line; the person who arrives first gets served first.
    • Major queue operations include Enqueue (inserting an element), Dequeue (deleting an element), Peek first (looking at the first element without removing it), and Peek last (looking at the last element).
    • A significant advantage of queues is that these four major operations are performed in a constant amount of time (Big O(1)).
    • Queues are commonly used in competitive programming because of their efficient operations.
    • Applications include scheduling algorithms in operating systems (like FIFO and round robin) and maintaining playlists.
    • A circular queue is a type of queue where the front and rear are connected, forming a circle.
    • In Python, a basic queue can be implemented using a class with append for NQ and pop for DQ. Other implementations exist within modules like collections (deque) or queue.
    • A disadvantage of queues is that they are not very flexible due to the restriction on insertion and deletion points (only at the rear and front, respectively).
    • Linked Lists:
    • A linked list is a linear data structure that is a collection of nodes.
    • Each node contains two parts: the data itself and a reference (or pointer) to the next node. This reference stores the memory address of the subsequent node.
    • Unlike arrays or lists, linked list elements are stored randomly in memory, not necessarily at continuous locations.
    • The beginning of the linked list is typically marked by a head pointer, which stores the address of the first node. The reference of the last node points to null (or None in Python).
    • A singly linked list is one where traversal is done only in one direction (from the head to the end).
    • Linked lists can offer more efficiency for operations like insertion and deletion compared to lists in certain scenarios.
    • Operations include insertion, deletion, and traversal. Insertion and deletion can be performed at the beginning, end, or a specified node.
    • Traversal means going through each node of the linked list.
    • Accessing elements in a linked list is slower compared to a list because you have to traverse from the head to the desired node; you cannot directly jump to an element using an index.
    • Memory utilization might be more in linked lists compared to lists.
    • Creating a node in Python involves defining a class (e.g., class node) and initializing its data and next pointer (often None initially) using a constructor (__init__ method).

    Data Structures in Libraries for Data Science and Machine Learning Specific Python libraries designed for data handling introduce their own data structures:

    • NumPy Arrays: NumPy (Numerical Python) is the core library for numeric and scientific computing. It consists of multi-dimensional array objects. You can create single-dimensional or multi-dimensional arrays (arrays of arrays or lists of lists) to perform various numerical operations like sum or dot product. NumPy arrays are generally used for homogeneous numerical data.
    • Pandas Data Structures: Pandas is a core library for data manipulation and analysis. It provides single and multi-dimensional data structures to handle tabular data seamlessly.
    • Series: A single-dimensional data structure, described as a one-dimensional labeled array. Created using pd.Series(), typically from a list or dictionary.
    • DataFrame: A multi-dimensional data structure, specifically a two-dimensional labeled data structure. DataFrames are used to work with tabular data, consisting of rows and columns. They are commonly created from dictionaries where dictionary keys become column names and the list values become the records in those columns. DataFrames have useful methods like head, tail, shape, and describe (providing summary statistics) and allow extracting data by index or label using .iloc and .loc.

    In summary, Python offers a range of data structures, from fundamental built-in types like lists, tuples, dictionaries, and sets to more complex structures like arrays, stacks, queues, and linked lists used in algorithms, and specialized structures like NumPy arrays and Pandas Series/DataFrames vital for data science tasks. They are crucial tools for efficiently organizing and manipulating data in various programming contexts.

    Python Object-Oriented Programming Explained

    Based on the sources and our conversation history, object-oriented programming (OOP) is discussed as one of the advanced concepts in Python. It is considered one of the most important concepts.

    Here’s a breakdown of Object-oriented programming based on the sources:

    • Purpose of OOP: Object-oriented programming allows you to represent real-world objects in the programming paradigm. Python is an object-oriented programming language.
    • Core Components: To understand OOP, you need to understand its two main components: classes and objects.
    • Class: A class can be thought of as a template or a blueprint for real-world entities. It is described as a user-defined data type, similar to predefined data types like integer, float, Boolean, and string. A class will have attributes (which are its properties) and methods (which represent its behavior). Using the example of a phone, properties could include color, cost, and battery life, while behaviors could include making calls, watching videos, and playing games.
    • Object: An object is a specific instance of a class. Following the phone example, specific phones like Apple, Motorola, and Samsung would be objects of the phone or mobile class. Just as a variable a = 10 means a is an integer variable with the value 10, objects are specific instances of the class’s data type.
    • Creating Classes and Objects:
    • To create a class in Python, you use the class keyword followed by the name of the class. By convention, the first letter of the class name should be capitalized.
    • Methods within a class are defined using the def keyword. These methods represent the class’s behavior. Methods take self as a parameter, which helps invoke the attributes present in the class. Examples include methods like make_call and play_game in a Phone class.
    • Attributes are associated with the object using self.attribute_name within the methods or the constructor. Methods can be created to set or show the values of these attributes.
    • An object of a class is created by calling the class name followed by parentheses and assigning it to a variable (e.g., P1 = Phone()).
    • Once an object is created, you can invoke the methods associated with the class using the dot operator (e.g., p1.make_call(), p1.play_game()).
    • Constructor (__init__):
    • A Constructor is a special concept in OOP. In Python, the constructor method is named __init__ (with double underscores before and after).
    • The purpose of the constructor is to initialize the values of the attributes when an object is being created.
    • The __init__ method takes self and typically additional parameters corresponding to the attribute values you want to set upon object creation. Inside the constructor, these parameter values are assigned to the object’s attributes using self.attribute = parameter.
    • When you create an object of a class that has a constructor, you pass the initial values for the attributes as arguments to the class call (e.g., E1 = Employee(“Sam”, 28, 75000, “male”)). The constructor then runs automatically to initialize the object’s attributes.
    • Inheritance:
    • Inheritance is another important concept in OOP. It means that a child class can inherit some or all of the features (attributes and methods) from a parent class.
    • To create a child class that inherits from a parent class, you include the name of the parent class in parentheses after the child class name during its definition (e.g., class Car(Vehicle):).
    • If a child class does not define its own constructor (__init__), it will inherit the constructor from its parent class. This means you pass values for the parent’s attributes when creating an object of the child class. The child object can then call methods defined in the parent class.
    • A child class can also have its own specific methods in addition to the inherited ones.
    • It is possible to override the __init__ method (or other methods) in the child class to provide specific initialization or behavior for the child class.
    • Types of Inheritance: The sources mention different types of inheritance:
    • Single Inheritance: A child class inherits from a single parent class. The Car inheriting from Vehicle is an example of this.
    • Multiple Inheritance: A child class inherits from more than one parent class. The child class inherits features from both parents.
    • Multi-level Inheritance: There are multiple levels of inheritance, where a class inherits from a child class which itself inherited from another class. An example is a Grandchild class inheriting from a Child class, which inherited from a Parent class. An object of the grandchild class can access methods from all classes in the hierarchy.

    In summary, OOP in Python provides a structured way to design programs by creating classes as blueprints for objects, defining their properties and behaviors, and using concepts like constructors for initialization and inheritance to create hierarchies of classes that share features.

    Python File Handling Essentials

    Based on the sources, here’s a discussion of File Handling in Python:

    File Handling in Python involves dealing with text files. It allows you to use Python programming to write, read, and perform various operations on these text files, which typically have a .txt extension. File handling is considered one of the advanced topics in Python. Another name used for file handling is IO functions, referring to input/output functions.

    With file handling, you can perform operations using built-in functions, including:

    • Opening the file
    • Reading text from the file
    • Writing text into the file
    • Appending text (adding onto existing text)
    • Altering text
    • Deleting text

    Core Concepts and Operations:

    1. Opening a File:
    • The very first step in file handling is always opening the file.
    • This is done using the open() function.
    • You typically store the result of the open() function call in a variable, which is sometimes referred to as a file pointer. This variable is used to perform subsequent operations on the file.
    • The open() function requires the name of the file as an argument.
    1. Modes:
    • When opening a file, you specify a mode which determines the type of operations you intend to perform.
    • Read Mode (‘r’): Used when you want to read existing text from the file. You must open the file in ‘r’ mode to use reading functions.
    • Write Mode (‘w’): Used when you want to write or add text to the file. Opening in write mode allows using the write() function.
    • Append Mode (‘a’): Used specifically for adding new text to the end of the text file’s existing content. You open the file with mode ‘a’.
    1. Reading Text:
    • To read the entire content of a file opened in read mode, you use the read() function on the file pointer (e.g., f.read()). The content is typically stored in a variable for use.
    • To read the text line by line, you use the readline() function. Each call to readline() reads the very next single line from the file. If there are no more lines to read, readline() returns an empty string. The readline() function differs from read() which displays all text at once.
    1. Writing and Appending Text:
    • To write text to a file opened in write mode (‘w’) or append mode (‘a’), you use the write() function on the file pointer (e.g., f.write(“some text here”)).
    • Using the \n operator (backslash n) at the beginning of the string you are writing will ensure that the text is added starting on a new line in the file.
    • Text written in append mode (‘a’) is added after the existing content in the file. Text written in write mode (‘w’) can overwrite previous content (implied by the read example that follows a write example).
    1. Closing the File:
    • After you have finished performing operations on a file, it is a good practice to close it.
    • This is done using the close() function on the file pointer (e.g., f.close()). This is compared to closing a book after reading or writing in it.
    1. Counting Characters:
    • You can count the total number of characters in a text file by opening the file in read mode, reading its content into a variable (e.g., using read()), and then using the len() function on that variable. The len() function calculates the total number of characters.

    Development Environment: To perform file handling in Python, you need an Integrated Development Environment (IDE) that supports both Python files (.py) and text files (.txt) simultaneously. Offline IDEs such as PyCharm, VS Code, or Jupiter notebooks are suitable. Practical examples in the source were shown using PyCharm. You would create your Python script file and the text file you wish to interact with within the IDE’s project environment. When running code that writes to a file, the output is typically seen directly in the text file itself, not necessarily in the console. You cannot perform reading and writing operations (like write() and readline()) on the same file object at the same time if opened in conflicting modes.

    An Overview of Generative AI and Python Applications

    Based on the sources and our conversation history, Generative AI (Gen AI) is presented as an advanced concept within the realm of Artificial Intelligence, incorporating human-like intelligence and creativity. It is a rapidly evolving AI system that is gaining prominence.

    Here’s a detailed discussion of Generative AI as described in the sources:

    What is Generative AI?

    • Generative AI is an artificial intelligence system that focuses on creativity.
    • It is described as evolving beyond simply understanding programming languages to mimicking human-like intelligence and creativity.
    • Generative AI is a subset of artificial intelligence.
    • Unlike traditional AI, which might classify or discriminate between data, Generative AI acts like an artist, capable of creating, generating, or transforming new content. This content can include text, video, audio, images, and more.

    How Generative AI Works

    • Technically, Generative AI or Gen AI functions by employing a neural network.
    • This neural network mimics or replicates biological neurons.
    • Based on this mimicry, it analyzes data patterns and generates new content based on those patterns.
    • Generative AI models receive an input (which can be text, audio, video, or any format).
    • These models are then pre-trained on data and fine-tuned to perform specific tasks. This fine-tuning allows them to cater to specific requirements and generate personalized content based on prompts.

    Discriminative vs. Generative AI

    • The sources contrast Generative AI with Discriminative AI.
    • Discriminative AI acts like a judge; given a dataset (e.g., images of dogs and cats), it classifies them into predefined categories (cats and dogs).
    • Generative AI, on the other hand, acts like an artist; given a similar dataset, it can create a new species or generate new content.

    Why Generative AI is Trending

    • Generative AI is trending because it does not depend on giving input and getting the same form of output, unlike traditional AI. It works based on your inputs and instructions.
    • It has impacted various fields, including text, audio, and video domains, and sectors like data management, tech, healthcare, and entertainment.
    • It has creative applications such as DALL-E and ChatGPT. For example, you can give a text prompt as input, and it can create an image as output.
    • It is enabling professionals (like business professionals and researchers) to generate code using tools like ChatGPT and develop new large language models and tasks.

    Types of Generative AI Models Mentioned

    The sources list different types of generative AI models:

    • Generative Adversarial Networks (GANs): Two models work together – one generating content, and one judging it – to produce realistic new data.
    • Variational Autoencoders (VAEs): This AI learns to recreate and generate new, similar data.
    • Transformers: An AI that learns to produce sequences using context. Transformer-based models include examples like ChatGPT.
    • Diffusion Models: Generates data by refining noisy starting points until they look realistic.

    Applications of Generative AI

    Generative AI has numerous applications:

    • Content Generation: Creates textual or other code-based content. It boosts creativity by providing content ideas and new ways to approach problems.
    • Customer Support and Engagement: Helps firms interact with customers.
    • Data Analysis and Data Science: Aids with visualization and analyzing data.
    • Code Generation and Software Development: Helps generate code.
    • Research and Information Retrieval: Helps researchers and professionals extract information from various data sources.
    • Machine Translation: Translates text, audio, or other content into required languages.
    • Sentiment Analysis: Analyzes text feedback to determine positive, negative, or neutral sentiment.
    • Other domains include Healthcare and Transport.
    • It automates content creation, saving time, and provides personalization based on user requirements and prompts.

    Python and Generative AI

    • Python is a high-level programming language preferred in Generative AI development.
    • One reason is that Python already has a well-supported set of libraries used for years in related domains like data science, machine learning, natural language processing (NLP), and deep learning. Artificial intelligence and Generative AI are “grabbing” these existing Python libraries.
    • Python is described as a versatile programming language that makes life easier for people working in this technological domain.

    Practical Applications in Python (from sources)

    The sources demonstrate building applications that utilize Generative AI:

    1. Flask ChatGPT App: Integrating the OpenAI API with a Flask web application.
    • This involves setting up a Python virtual environment, installing Flask and OpenAI libraries, and obtaining an OpenAI API key.
    • The application consists of a backend Python file (app.py) using Flask to handle API calls and a frontend HTML file (index.html) for the user interface.
    • The Python backend defines routes to handle requests, interacts with the OpenAI GPT model (e.g., GPT 3.5 turbo) by sending user input (prompts) via the API, receives responses, and handles potential errors like exceeding the usage quota.
    • The frontend uses HTML for structure and JavaScript to manage the interaction between the user interface and the backend, sending user prompts and displaying GPT’s responses.
    • Note: Using the OpenAI API is not entirely free; there is a limit ($5 worth of conversation mentioned) before payment is required.
    1. Text to Image Application: Creating images from textual descriptions using AI models via a web application.
    • Similar to the ChatGPT app, this uses Flask for the web framework, the OpenAI API for image generation, and HTML/CSS/JavaScript for the front end.
    • Prerequisites include Python, Flask, OpenAI library, and an OpenAI API key.
    • The Python backend (app.py) receives text prompts from the user via the frontend, uses the openai library to call the API’s image generation function (specifying prompt, size, and number of images), and gets an image response back.
    • The HTML frontend provides a text box for input, a button to trigger the generation, and displays the resulting image.
    • More precise descriptions from the user lead to more precise image outputs.
    1. Personalized Story Generator using LangChain: Developing an application to generate unique stories based on user inputs like character names, settings, and themes.
    • This project utilizes the LangChain library in Python, described as streamlining development processes and utilizing LLMs. LangChain is a Python library, similar to libraries like NumPy. It supports use cases like creating assistants and chatbots.
    • The application also requires the OpenAI library and an API key.
    • The structure involves two Python files: user_input.py to collect character name, setting, and theme from the user, and story_generator.py as the main script that uses LangChain and OpenAI to generate the story based on the inputs received from user_input.py.
    • The story_generator.py file imports necessary components from langchain, imports the user input function, defines the story generation logic using an OpenAI text model (like GPT 3.5 turbo), includes the API key, and prints the generated story.
    • This demonstration was executed in the command prompt rather than a web browser interface.

    In essence, Generative AI represents a shift in AI towards creative content generation, leveraging powerful models and neural networks. Python, with its rich ecosystem of libraries, serves as a crucial language for implementing and developing Generative AI applications, as demonstrated by the examples involving Flask, OpenAI API, and LangChain.

    Python Tutorial with Gen AI for 2025 | Python for Beginners | Python full course

    The Original Text

    Transcript Tool: https://anthiago.com/transcript/

    Video Link: https://www.youtube.com/watch?v=-65r_3r-nN4

    Python Tutorial with Gen AI for 2024 | Python for Beginners | Python full course welcome to the world of python where creativity meets technology whether you are a beginner eager to dive into coding or a season programmer looking to expand your skills this journey has something for everyone please note we have added sessions being covered in this tutorial with timestamps in the description for your convenience to jump to the topic which excites you the most you will be mastering the fundamentals of python diving deep into advanced con Concepts and unlocking the secrets of powerful data structures and algorithms picture yourself analyzing data like a pro building intelligent machine learning models and exploring the fascinating Realms of generative AI but that’s not all we’ll also dwelve into python for automation simplifying everyday tasks and crafting beautiful interactive guis for your applications join us as we embark on this comprehensive python Adventure from Basics to Brilliance we have got you cover so let’s get started and code your way to [Music] Mastery in the python fundamentals we’ll start with the basics installing python Understanding Variables data types operators and flow control statements you will also learn about Python’s core data structures tles lists dictionaries and sets next we will dwelve into advanced topics like object-oriented programming inheritance and exception handling you will also learn file handling techniques to manage your data efficiently understanding data structures and algorithms is key we will cover arrays Stacks cues linked lists and essential searching and sorting algorithms like linear search binary search insertion sort quick sort and merge sort in Python for machine learning you will work with libraries like numai pandas matplot lib and cabon these tools will help you manipulate analyze and visualize data to gain valuable insights you will also explore the fascinating word of generative AI where you will learn the basics and how to apply python in creating generative models opening up new possibilities in AI in Python 4 automation we will focus on making your life easier you will learn to use selenium for web automation we will also cover GUI development using tkin bringing your applications to life why wait let’s quickly start with the first module in Python fundamentals this is where it all begins we will start by introducing you to Python’s syntax and Core Concepts you will learn about variables data types and control structures like oops and conditionals by the end of this section you will be comfortable writing simple python programs and ready to tackle more complex challenges we’ll start off this session by installing python into our systems and to install python we’d have to go to this particular site over here python or downloads let me just go ahead and click on this link so as you see since python is platform independent whether you have a Windows system or a Linux system or even a Mac you can download python for either of these operating systems and since I’m using a window system all I have to do is click on this particular link and python would be downloaded now after downloading python we would need an IDE so what exactly is an IDE IDE stands for integrated development environment now if you just download python we would also need some environment which would make our coding much more easier so if you have worked with other programming languages such as C C++ or Java then you would know that you would have used an ID for these languages as well so if you have worked with Java then you would have used an ID called as Eclipse similarly if you have work with C or C++ then you would have work with ID such as turbo C++ or Dev C++ so similarly we’ve got a lot of IDs for python so one such ID for python is pycharm and we can download pycharm from this particular link jet brains.com slpy IAM I’ll just click on this over here then I’d have to click on this download button and as you see we’ve got the professional version and the community version and if we want it for just single user development we can just go ahead and download this community version over here and similarly as you see over here we’ve got if you use a Windows system then you can download pycharm for Windows if you have a Mac then you can download pycharm for Mac similarly if you have a Linux then you can download python for Linux since I have a Windows system I’ll select this and I’ll go ahead and download the community version of this then we have something called as Anaconda which is actually a python and R distribution so if you want to perform any sort of machine learning task or data science task then Anaconda is a complete toolkit so this provides you with a lot of tools involving python so it will provide you an IDE called as Jupiter notebook and not just the ID along with the IDE it will also provide you with a lots of libraries libraries such as P plot cbon pandas and napai so you don’t have to manually install these libraries so once you go ahead and install Anaconda so you can install Anaconda from this particular site over here over here you see the products tab click on the individual Edition which is the Open Source One then just go ahead and scroll down so you have the download button over here again and since I have Windows system I’ll just go ahead and download the 64-bit graphical installer and since this is a lot of MB which is 466 MB and since I don’t want to use up my data pack I’ll just go ahead and cancel this because I already have Anaconda installed into my system now once we have installed Anaconda as I’ve told you guys Anaconda comes up with an ID called as Jupiter notebook so what is Jupiter it is a browser based interpreter that allows us to interactively work with python so all of our python code will be implementing in this Jupiter notebook and if you have to open jupyter notebook I’ll just show you how to do it so here on your search bar go ahead and type in Anaconda so You’ have to select this over here Anaconda prompt now in Anaconda prompt you would have to type Jupiter node book and let’s just wait for the browser based interpreter to open up so this what you see is called as the Jupiter notebook which is a browser based python interpreter and we’ll be writing all of our python code over here now if we want to open up a new python notebook click on this tab and select Python 3 now once this is done we have opened up our new python notebook so you have a lot of tabs over here so similarly if you want to create a new notebook select file then you have this new notebook option again you can go ahead and select Python 3 and as you see over here this is our new notebook I’ll just close this up over here then let’s say if you want to download the code file which you’ve written you have this download as option and over here normally whenever we want to download the Jupiter notebook we download it as iynb file which basically stands for python notebook I python notebook you can also download it as other formats if you want to just save it as a simple python file you can just select py over here you can also go ahead and save this file as a HTML doc or maybe a latex Doc and if you want to save you have the save as option over here and similarly you can go ahead and rename your notebook either you can select this or you can just click over here then you can go ahead and rename it so I’ll just write it as my python notebook and then I’ll rename this file over here now let’s go ahead and write our first Python program so to print something out on the console we would have to use the print command then I’ll give this parenthesis over here I’ll use double quotes and inside this I will given the command this is Sparta and I’ll just go ahead and click on run and as you guys see we have successfully printed out this is Sparta we have written our first Python program in Jupiter notebook now we can we are on our way to happily go and hack the NASA systems so this is our building Stone guys we can go ahead and do a lot of things with what we’ve learned with this now you have something called as a kernel over here so what exactly is a kernel you can consider this kernel to be the executor of this program so whenever you would have whenever you write a piece of code and you’d want to execute it you click on kernel and this is what actually runs your entire code then let’s say if you you want to add a new cell about this so this what you see is called as the cell and if you want to add a cell about this you click on insert then you have the insert cell above option similarly let’s say if I want to insert a cell below this I click on insert I select insert cell below and this is how I can add another cell so this was a basic intro about jupitor notebook so let’s start off by understanding what exactly are variables in Python now when you work with any programming language your first task needs to be to work with data isn’t it so whatever programming language you’re working with you are essentially working with data but the question over here is how do you actually store the data that you work with so let’s say you’re working at a company and you want to store the names of all of the employees so we start off with taking three employee names so let’s say we have John Sam and Matt with us and we’d have to store these names somewhere so where can we store them this is where a variable comes in so you can consider a variable to be a temporary storage space now what we’ll do is we’ll take this string value so this what you see inside double quotes is known as a string and we’ll take the string value and we will store this in this variable called as student either we can call the student or employee or whatever we want to and this variable will have a particular address associated with it and since this variable is a temporary storage space the values which are stored inside it can be changed again and again so initially we are storing this value John inside this variable employee or student then after some time we can go ahead and replace this value John with this value Sam similarly after some some time we are changing this value of Sam with this value of Matt and this is how variables work in Python so now let’s go to Jupiter notebook and I’ll give you a proper example of this here what I’ll do is I’ll create a variable called as where one I’ll give this equal to symbol and I’ll go ahead and store the value drawn inside this let me click on run now let me print out this print of wi one and let’s see what will be the result so we have successfully stored the value JN inside wi one and we were able to print this out and since W one is a variable it is a temporary storage space so that is why we can change the value which is stored inside this so now instead of John I want to store the value Sam inside this I’ll click on run again I’ll use print and then I’ll be printing out the value of V one and as you guys see initially we had John and inside this we were able to change this to Sam now again after some time I’ll go ahead and change this value to Matt now let me print out wi one over here print of wi one and as you guys see initially we had John then we changed it to Sam and finally we have changed it to Matt so that was a basic intro to variables now another thing to be kept in mind is every variable variable has a data type associated with it so when you’re working with data that data can be present in any format so when you’re working with numbers such as 10 500 – 1000 – 323 these are called as integers and when you work with decimal point numbers so decimal point numbers such as 3.14 15.97% Point numbers then we have something called as Boolean values so Boolean values are basically you have only zero and one or you can also tag them as true and false so you have only two values over here and those two values are true and false or you can also tag them as zero and one then we have strings so strings are something which you put in single quotes double Cotes or triple Cotes so these are the four four main data types over here in Python so let’s go ahead and look at an example of each of these now I’m going to start off by creating an integer variable so I’ll name this integer variable as num one and I’ll store the value of 10 inside this and just to see what is the data type of this I will use the type method and inside the type method I’ll be passing in Num one and as you you guys see this tells us that the data type of this particular variable is integer then I’ll go ahead and pass in a floating Point number or a decimal number so I’ll call this as let’s say decimate and in decimate maybe I’ll store in the value of 3.14 now let me go ahead and check the type of decimate so inside this I’ll pass in decimate and when I click on run you guys see that this is of floating type then we have the next data type which is of Bulan so here I will have maybe another variable called as log one and inside log one I will store in the value true let me hit run again and then let me check the type of log one so inside type I’ll pass in the variable log one and as you guys see this tells us that this is of bu bu basically means this is of Boolean or logical type and then we’ve got the character or string so this time I’ll have my variable as car one and inside this I will store the name let’s say I’ll store the name Arjun over here then let me check the type of car one and when I hit run you see that this tells us that this is a string type variable we also have another variable over here or another data type over here which is of complex type so complex is basically a data type where you have a real part and an imaginary part so let’s say if I write something called as 3 + 5j so here three would be your real part and 5j would be your imaginary part you would have learned about complex numbers in your primary or in your secondary school so normally in math this J is represented as I so You’ have something called as 3 + 5 I where 3 would be your real part five would be your imaginary part so in Python this I is represented with j instead of I so now I’ll go ahead and store this in a variable called as comp 1 now let me go ahead and check the type of this so type of comp one and I see that this is of complex type so we have successfully under Ood what are variables and we have also looked at the different data types of variable can have now we’ll go ahead to the next Concept in Python which will be operators and as the name suggests operators help us to perform simple operations on this data and we’ve got arithmetic operators relational operators and logical operators so we’ll start with the first set of operators which are the arithmetic operators let me go ahead to this jupyter notebook over here and what I’ll do is I will clear out everything which is present in the console so this scissors which you see if you click on the scissors symbol you’ll be able to cut out all of these cells now let me add a comment so what is a comment comment is something which is not executed by the python interpreter and you can add a comment with this hash symbol so after this hash symbol I am going ahead and writing arithmetic operators I’ll click on run and as you see this is not executed over here so whenever you add a hash symbol over here python interpreter automatically recognizes whatever follows hash symbol as a comment now if I remove this hash symbol and then if I click on run You’ see that we get this errow which tells us that this is invalid syntax because if we don’t add the hash symbol over here then python interpreter would consider this these two actually as two separate variables and since we have not declared any variable called as arithmetic or as operator this is giving us this error so I’ll just go ahead and add this hash over here now after this since we have to perform arithmetic operators and arithmetic operators basically constitute of plus we have have plus then we have minus then we have multiplication and then we have division now I’ll create two variables over here I’ll have first variable num one and I’ll store the value of 10 inside this then I’ll have the second variable num two and I’ll go ahead and store the value of 20 inside this now after creating these two variables let me perform the basic arithmetic operations so I’ll start start off by adding num one and num two so I’ll type num one plus num 2 and when I perform num 1 plus num two I get a result of 30 so basically if you want to add two numbers you have to use the plus symbol between those two operant and since 10 is stored in Num one 20 is stored in Num two we get a result of 30 then similarly I’ll go ahead and perform the subtraction op operation so here I’ll have num one minus num 2 and when I type num 1 minus num 2 I get a result of – 10 because 10 – 20 is – 10 going ahead I’ll also perform multiplication and to perform multiplication I’d have to type num one into num 2 and when I have num one into num2 over here which is basically 10 into 20 I I get a result of 200 then we are only left with division so to perform division I’ll have num one then I’ll use the forward slash symbol which denotes division then I’ll have the second operant over here which is num two and I’ll click on run and as you guys see when we divide 10 with 20 we get a result of 0.5 so these were some basic arithmetic operations now we’ll go ahead and and work with relational operators so I’ll just add another comment over here and I’ll add the comment as relational operators and what are the relational operators these help us to find the relationship between two operant so we can understand if one operant or the value of one operant is less than the other operant or maybe the value of one operant is greater than the other operant so we will have less than symbol greater than symbol equal to symbol and not equal to symbol now again we will use the same variables num one and num two let me just print out num one and num two over here for your sake and as you guys see we have 10 stored in Num one and 20 stored in Num two now I want to check if the value in Num one is less than the value in Num two so I’ll type num one I’ll use the less than symbol then I’ll type num2 over here I’ll click on run and as you guys see I get the result as true which means that num one is less than 20 which we get because 10 is obviously less than 20 now I want to check if the value in Num one is greater than the value in Num two and when I hit run I get a false value because 10 is not greater than 20 now going ahead I want to check if the value in Num one is equal to the value in Num two so this what you see is the double equal to operator you have to understand the difference between the double equal to operator and the single equal to operator so this is the single equal to operator and with the help of single equal to operator we are assigning a value to a variable but when we are using this double equal to operator this helps us to understand if these two values if the operant on the left hand side and the operant on the right hand side are equal to each other or not and when I click on run I get a false value because 10 is obviously not equal to 20 then going ahead I have the not equal to operator so I’ll have num one so not equal to operator is represented like this so I’ll have exclamation mark then I’ll have the equal to symbol then I’ll have num two over here and I get a True Result because 10 is obviously not equal to 20 so these were some of the relational operators going ahead we’ll work with logical operators so I’ll add a comment over here which would be logical operators and we have two logical operators which are and or let’s start with and so and is a logical operator which would give us a True Result only when the both of the operant are true but R is a logical operator which would give us a True Result when either of the oper end is true so let’s understand this in detail so this time I will be creating two Boolean variables over here I’ll have log one and in log one I’ll have the value true stored then I’ll have log two and in log two I’ll have the value false stored so I have log one and log two over here now I’ll perform the and operator on both of these so let me go ahead and type log one and log 2 and when I hit run I get a false value because log one is true log two is false true and false will give us a false result now let me see what will happen when I have log 2 and log one again I get a false result because false and true is also so false now let me check log 2 and log 2 log 2 and log 2 will also give me false because false and false is also false and finally I’ll check log one and log one log one and log one will give me a True Result because and operator gives a True Result only when both of the oper are true now we’ll head on to the or operator so this time I’ll have log 1 or log 2 now true or false will give me a True Result because or will give me a True Result when either of the operant is true then I’ll have log 2 or log one and this again gives me a True Result because false or true is again true then I’ll have log one or log 2 and this again will let me actually change this to log one or log one and this will give me a True Result because true or true is also true and finally we’ll have log 2 or log 2 and this is the only case where we’ll have a false result which is false or false so only in the case where both of your operons are false that is when you will get a false result when you’re working with the or operator so this was all about about the different types of operators in Python now we’ll understand what exactly are python tokens so python token is the smallest meaningful component in a program so when you combine all of these python tokens together that is when you get your final python code so the basic python tokens are keywords identifiers literals and operators so we have already worked with operators which were one of the tokens in Python now we’ll go ahead and understand what are keywords identifiers and literals so we’ll start with python keywords python keywords as it is stated are special reserved words so when I say special reserved words you can’t use these special reserved words for any other purpose which would mean that you can’t give the name of a variable or the name of a function or maybe the name of a class with these python keywords and you have some of these reserved keywords over here which are if def Dell true false while not or return so these are some of the Python keywords now let me just show you how to use these so let if I type in DF so as you guys have seen over here when I type DF this automatically turns into green so python interpreter recognizes this word def as a keyword now let’s say if I try to store something in this def is equal to 10 I get an error because since this is a keyword I can’t use this as a variable similarly let’s say if I have if if again is a keyword and that is why this turns into green then we have something called as identifiers so identifiers are basically the names used for variables functions or objects so till now we had created some variables called as V one or num one or log one so those all are identifiers so the names which you give to the variables functions or objects are known as the identifiers so let’s say if you have a person and the name of that person is Arjun or Sam or Matt so here the names of these people are the identifiers similarly as you need a name to identify a human being that is how you will also need an identify to understand or to call or invoke a variable function or object and this is the simple analogy between the real life and these python identifiers and there are some basic rules when you’re working with these identifiers so the first rule is you cannot have an identifier with special characters so you can have underscore but instead of un uh except underscore you can’t have any other special characters in the name of the identifier and also identifiers are case sensitive so let’s say if you create a variable called as W one with v in small Cas and then you create a variable as V one with v in capital K then both of them will constitute as different variables and then also you have another rule over here which states that the first letter cannot be a digit so these are some basic rules which normally a python coder keeps in mind so let’s go ahead and understand about these rules in Python so I have told you guys that special characters cannot be used in the name so let’s say if I have J personent and I have this over here and if I try to store the value 10 inside this let me click on run so you see that we have a syntax error over here similarly what I’ll do is I’ll have a variable called as N1 and inside N1 I’ll store the value 10 then I’ll have the value Capital N1 and inside this I’ll store the value 20 now let me print in both of these N1 with a small n and N1 with a capital N and as you guys see both of these values are different because both of those variables are different so this is about python identifiers then we have something called as literals and literals are just the constants in python so constant is a value which does not change so whatever values you are storing inside a variable that is called as a literal so here when you’re storing 10 into N1 10 would be literal similarly when you’re storing the value 20 into N1 20 would be your literal then over here when you’re storing the value such as 10 into num1 20 into num2 again they are your literals now we’ll head on to an interesting topic in Python where we’ll understand about strings in Python so what are strings strings are basically sequence of characters which are enclosed within single quotes double quotes or triple quotes and we already know that and we have already seen an example of python strings so let me just give you an example of all three of these where I’m creating a string with single Cotes double codes and triple codes let me go ahead and remove all of this over here because I like it clean let me just remove all of this stuff from over here now what I’ll do is I will create a new string variable called as St str1 and the value I’ll be creating with single codes and inside this I’ll just type hello world and I’ll print out s str1 over here and I have successfully created the string s str1 with Hello World then I’ll have Str str2 and in Str str2 I’ll be creating this with double codes and over here I’ll just type in this is Sparta and I’ll print out s str2 over here going ahead I’ll have another variable called as s str3 and this time I’ll create a multi-line string so if you want to create a multi-line string we can create it using triple codes so I’ll have triple codes over here and inside this I’ll just type I am going to France tomorrow let me run this and let me print out sdr3 right now so as you guys see I have successfully created a string called as I am going to France tomorrow and this what you see backwards sln that indicates a new line so after I am we have backwards sln which tells us that going to comes in a new line similarly we have backward sln followed by France tomorrow which tells us that this again is in a new line so this is some basic idea about strings in Python now that we know this let’s actually see how can we extract individual characters from a string so here we have created a string called as my string so the name of the variable is my string and the value which is stored inside this is my name is John now if we want to extract individual characters we have to understand the concept of index so here these characters are present at indices and the index value starts from zero so here m is present at index z y is present at index one the space is present at index two right so similarly all of these have a particular index assigned to them and the index value starts from zero and if you want to extract this particular character or the first character from a string we’ have to give the name of the string we’ have to give the parenthesis and inside the parenthesis we will given the index value that we would want to extract and since I want to extract the first character we have to give index Z and that is how we were able to extract this then similarly if I want to extract the last character so the index of the last character will be minus1 so either you can manually count the last value over here so that is basically time consuming instead of counting the index if you just want to directly extract the last character then you can just go ahead and give it minus one and that will automatically give you the last character which is present in the string so let’s go ahead and perform these operations in jupyter Notebook let me add in a new cell over here insert cell below and I’ll go ahead and create a string called as my string and over here I’ll have let’s say a string called as my name is John let me print out my string over here now after this I’d want to extract the first character so if I want to extract the first character I’ll just type my string I’ll give in parenthesis I’ll have one written over here and I’ll actually have to give zero because the first character is presented the zero what index so guys this is important you’d have to remember that in Python the indexing starts from zero so this is how I have extracted the first character now similarly if I want to extract the last character which is n then I will give minus1 over here and as you guys see I am able to extract the last character now similarly let’s say if I want to extract this a so this is presented 0 1 2 3 and 4 this is presented index number four so let me just given four over here and as you guys see I have successfully extracted this particular element from this entire string now we’ll go ahead and work with some string functions so the first string function is len which will give us the length or the number of characters which are present in the string so all we have to do is use Len and inside that we will pass in the name of the string so when we pass in my string this tells us that the length of the string is 15 and similarly let’s say if I want to convert all of the characters in The String into lower case we have the lower method so all I’ll do is type in my string. lower and this will convert all of the characters into lower ke and a method which is an analogous to lower is the upper method so I’ll type my string. upper and with the help of this I’ll be able to convert all of the characters into uppercase which you see in the result over here so I have my string ready now I would want to check the length of this and inside l n I will pass in my string and this would tell us that the length of the string is 15 now similarly if I so you see as you see over here we’ve got two Capital characters over here m is capital G is capital I’d want to convert them into lower case so for that purpose first I’d have to given the string name which is my string I’ll use the dot operator then I’ll use the lower method and when I click on run you will see that all of the characters have been converted into Lower Keys now similarly if I want to convert all of the characters into up upper case I’ll go ahead and type my string I’ll use a DOT operator and then I’ll have upper written over here I’ll click on run and as you guys see I have converted all the characters into upper case now we’ll go ahead and see some more function so we’ve got the replace method over here and we’ve got the count method so if I want to replace some particular character or some particular string with another string then we can use the replace method so again first we have to give in the name of the string which is my string I’ll use the dot operator then I’ll use replace method and it takes in two parameters the first parameter is basically that character which I’d want to replace so as you see over here initially we had y over here I want to replace that y with a so initially the sentence was my name is John but I have changed that to myone name is John so quite an interesting method isn’t it then we have the count method so here we have created a new string where we have stored the value hello hello world and if I want to check the count of the number of times a world occurs or number of times a particular substring occurs then we can just pass in the substring into this method so if I want to understand the number of times this substring hello occurs in this entire string so if I pass in hello this tells me that the substring hello occurs two times in the entire string over here so let’s work with this replace method and the count method so we already have this my string variable ready and let’s say instead of my name is John I would want to change the name over here so instead of my name is John I would want to change that to my name is Sam so I’ll have my string then I will use the replace method over here and we already know this takes in two parameters the first parameter is a substring which I’d want to replace so I would want to replace John and I’d want to replace it with let me actually keep it like this and I want to replace it with Sam and when I click on run you would see that I have successfully changed the substring from John to Sam then we have the count method let me create this new string variable over here I’ll have new string and inside this I’ll store hello hello world now that we have this what I’ll do is I will go ahead and use the count method so I’ll have new string and use the count method and inside this I’ll pass in hello and when I pass in hello this tells me that the substring hello is occurring two times now we have two more string functions over here so now we have the find function so the find function helps us to find the index or the starting of the index of a particular substring as you see over here if I want to know the starting index value of this substring part up I’ll just pass this entire substring into this method find and this gives us the value of 8 so let’s just understand this so now if I count the index it’ll be 0 1 2 3 4 5 6 7 and 8 so as you see this s is placed at index number 8 and that is what this find method gives us so let’s say similarly if I would have passed an S into this find method then this would have given us the result of 0 1 2 3 4 and five then we have another method called as split so the split method helps us to divide this string into a list of substrings on the basis of one split criteria so here we’ve got this entire string called as I like apples mangoes and bananas and I would want to divide this entire string into multiple subrings on the basis of comma so here whever this method encounters comma it will separate or segregate it into a substring so I like apples becomes one substring mangoes become second substring bananas becomes the third substring let’s go ahead and Implement an example of these two let me write s str1 over here and what I’ll do is I’ll just have a new value here let’s just say I’ll just type I love Piza and and I would want to know the starting index of this substring pza so I’ll have S str1 do find and inside this I’ll just pass in Piza and we get the result of seven so let’s just verify this 0 1 2 3 4 5 6 and 7 now we’ll go ahead and work with the split method so for that purpose we’ have to create a new string value and I’ll name it as fruit and here I’ll just type in I like apples guas bananas and I’ll also write maybe strawberries then I’ll use the split method fruit do split inside this I’ll give in the separator which will become comma and I’ve got a list of substrings I like apples becomes one substring guavas becomes the next bananas is the next substring and then we have strawberries as the final substring now we’ll go ahead and work with the next data structure in Python which is a list so when it came to a tuple that was an ordered collection of elements enclosed within round braces but a list is an ordered collection of elements which is enclosed within Square braces and that is not the only difference so tups were immutable that is when you created the elements inside a tuple you could not change them later on but when you create a list you can actually change the values which are present in it and this is how we create a list so L1 that is the name of the list which I’m creating and I’ll have square braces and I’ll have these different elements stored inside it let me delete all of these over here and let me start fresh for the list I’ll add a new comment which will be list and inside this I’ll name the object as L1 I’ll have square braces over here I’ll have one e and true let me print out L1 and this is a new list now let me go ahead and check the type of this inside type I will pass in L1 and as you guys see this tells us that this is a list now as we had extracted individual elements from a tuple similarly we can go ahead and extract individual elements elements from a list as well and it is the same process so over here we’ve got all of these elements and the indexing starts from zero so it is very important keep in mind guys so the indexing of a list or whatever data structure you’re working with in Python it starts from zero and if I want to extract the second element over here the index of the second element will be one because this is index number zero this is index number one and when I pass in L1 of one I’ll be able to extract this particular element from this entire list similarly if I want to extract a series of elements so I’ve got all of these if I want to start from index number two so this will be index number two so we’ve got 0 1 and two and this will go on till index number four so as I’ve already told you when it comes to python the outer limit is exclusive so when we give it till five we will be only able to extract the index number four so that is why over here we’ll be extracting 2 B and 3 let me create L2 over here and I’ll have some elements I’ll have 1 a then I’ll have two then I’ll have B after that I’ll have three and going ahead I’ll also have C over here I have successfully created L2 let me print this out and now if I want to extract let’s say B from this let’s see what would be the index it’ll be 0 1 2 and 3 so I’d have to give in L2 inside the parenthesis I’d have to give three and I am able to extract this particular element from the similarly if I want to extract the last element I’ll give in L2 I’ll give in minus1 over here and I able to extract the last element and if I want to extract a series of elements then in that case all I have to do is given an L2 and as we saw in the example if I want to extract 2 B and 3 so the index for this is 0 1 and 2 I’ll give it two over here and if I want to extract till three so this will be 2 three and four so that is why i’ have to give index number five as well and we are able to extract 2 B and three from this entire list now let’s see how can we modify a list so we have the same list over here and initially at index number zero we have the element one but if I want to change it to some other element all I have to do is given the index number and I have to assign a new value to that particular index number so as you guys see I am assigning the value of 100 to this particular index number and I’m able to change this value of one to 100 now we can also append a new element at the end or pop the last element and to add a new element at the end we will be using the append method so it have to given the name of the list we’ll use dot operator and then we’ll use the end method and we’ll just give the value which we’ want to append so when I type in Sparta over here this gets appended at the end of the list now similarly we can go ahead and pop the last element so if we have to pop the last element popping basically means removing the last element so we would have to use L1 do popop and it automatically removes the last element so as you guys see we had added Sparta but after using the pop method this Sparta value was removed from this list now we have the same list over here which is L1 let me actually I’ll have L2 over here not L1 let me print an L2 for you guys over here and I’d want to change let’s say this particular value here so let’s say instead of a I’d want Z so I’ll have L2 and the index for that is one and instead of a i’ want to store Z inside this and let me print out L2 again so initially we had a but after changing it we have Z over there now we’ll see how to append an element at the end of this list so we’ have to give L2 I’ll use dot operator then I’ll be using the epen method and inside this I’ll just add this word called as python let me print in L2 for you guys and I have added python at the end of this now if I want to pop this out I have to write l2. pop and when I click on run we see that this has been popped out and let me print out L2 again for your reference we see that the last element has been removed now there are some more modifications which we can perform on the list so let’s say if we want to reverse the elements which are present in a list so as you see in L1 we have all of these elements over here and if I want to reverse the order of these elements all I have to do is use the reverse method so I’ll type in L1 do reverse and when I print this out we see that the elements are printed backward then if we want to insert an element at one particular index value so when we use the append method we were able to add an element at the end of a list but instead of adding an element at the end of a list if we want to insert an element at some particular index then this is how we can do so I’ll have L1 do insert then I’ll give the index position where I’d want to insert so initially at index number one we have this value a but now at index number one I want to insert Sparta so this takes in two parameters first parameter is the index at which I’d want to insert second parameter is the value which I’d want to insert and as you guys see I have inserted Sparta at index number one now here as you see the rest of the elements have been shifted one index towards the right so a which was initially present at index number one is now present at index number number two two which was initially presented index number two is now shifted to index number three so each element shifts towards the right by one index value then we can also go ahead and sort a list so we have all of these elements over here now if you want to sort these elements in alphabetical order then we can just go ahead and use the sort method and this sort method sorts all of these with respect to alphabets so we have apple followed by banana followed by guava and then finally we have mango so let’s use the reverse method insert method and the sort method in jupyter Notebook so we have the same L2 over here now if I want to reverse this I’ll just type in l2. reverse and when I click on run this has been executed now let me print in L2 so as you guys see initially we had this particular sequence over here which was 1 Z 2 B 3 and c and after using the reverse method the elements have been reversed now we will go ahead and add an element at one particular index so if I want to add something at maybe index number three so now we’ve got 0 1 2 3 so we’ve got two which is present at index number three but now let’s say I’ll have L2 do insert and at index number three I’d want to insert great learning and let me hit run and let me print out L2 and as you guys I see at index number three I have inserted great learning and the elements which are followed after that shift towards the right by one index value now finally we’ll see how to sort a list so I’ll have L3 and inside this I’ll have some elements so I will have Apple after that I’ll have mango then let me actually change the sequence over here so let me start off with mango first then I I’ll have apple going ahead I’ll have guaa and then maybe I’ll have lii now this is the sequence which is present in this list and if I want to sort this out I would just have to use the sort method so when I hit l3. salt so this has to be a list and not a duple so this has to be square braces you guys have to keep that in mind let me change this over here let me cut all of this out and let me paste it over here and as you guys see this method has been executed and when I hit on run we have changed the order so we’ve got Apple followed by guava followed by lii and we have mango at the last now we can also perform the same concatenation and repeating operations on list as well so here we have L1 where we have the elements 1 2 and 3 then we have L2 where we have the elements a b and c and if I want to concatenate this L2 at the end of L1 all I have to do is use the plus operator and when I use L1 + L2 this is what I get I’ll have 1 2 3 a b and c and if I want to repeat the elements which are present in a list I would just have to multiply the name of the list with a particular scalar number so as you guys see I am multiplying L1 with three and I have repeated these elements three times so here I’ll just have concatenating a list and I’ll have I’ll just go ahead and create two lists over here inside L1 I’ll have 1 2 and 3 inside L2 I will have a b and I’ll also have C now I’ll perform L1 plus L2 and we have appended L2 at the end of L1 you have to understand that L1 + L2 and L2 + L1 would give you different results so now when I actually type in L2 + L1 you would see that we have appended L1 at the end of L2 so this sequence also changes when you change the sequence with the plus operator over here now let’s go ahead and repeat the elements so I’ll have repeat list and I let’s say if I want to repeat the elements which are present in L2 so I’ll just multiply L2 with let’s say five because I want the elements to be repeated five times so I have a b and c being repeated five times now we’ll head on to the main component which is about the different data structures in Python so we have Tuple list dictionary and set let’s start off with the first data structure which is a tuple so till now when we have worked with single variables you were able to store only one value or a single value inside a variable but with the help of these different data structures such as Tuple list set and dictionary we’ll be able to store multiple elements inside a data structure and it’s not that we can store only multiple elements of a single data type we can also store elements of different classes or different types into this data structure so let’s start with Tuple what exactly is a tuple is an a collection of elements enclosed within round braces and tuples are immutable what do I mean when I say tuples are immutable so what this basically means is if you create a tuple then you can’t go ahead and change any of the values present in it later on a tuple cannot be modified once you create it and this is the example of a tuple over here so we have round braces inside the round braces I have stored the elements 1 a and true so as you see I have elements of different types so we can store elements of different types into a tuple so let me create my first Tuple in Jupiter notebook I’ll type in let me actually have this in a fresh space I’ll add the comment Tuple and over here I’ll type in tup1 I’ll have round braces over here so first element is one then I’ll have Sparta then I’ll have true over here and then let me just print out T1 so I have created this Tuple now let me check the type of this type inside this I will pass in tp1 and this tells us that this is a tuple now if I want to extract individual elements from a tuple how can I do that well the process is pretty much similar as when compared two strings so if we want to extract the first element from a tuple so as you guys see over here I have created a tuple which comprise of all of these elements 1 a true 2 B false and if I want to extract the first element since the first element is presented index number zero I’d have to given the name of the Tuple and inside the parenthesis I’d have to given the index value which will be zero and I have extracted this particular element from this entire Tuple similarly if I want to extract the last St element so if I want to extract the last element I just have to give in minus1 so in P1 I’ll give in minus1 and with the help of this I am able to extract the last element now if I want to extract a continuous sequence of elements so here if I want to extract a true and two which is a continuous sequence of elements I’d have to give something like this so inside the parenthesis I’ll given 1 colon 4 so this is the starting value of the index this is the ending value of the index now here when it comes to python you have to keep in mind that the ending value is exclusive the starting value is inclusive so when you give one the starting index value a right so we have one and we have extracted this element but when you given four this only goes till index number three so that is why we have extracted only a true and two so two is presented index number three we have extracted a true and two but when we have index number four so index number four we have the value B but this is not extracted because index number four is exclusive so let’s go ahead and create a new Tuple and extract some elements from those tuples so what I’ll do right now is I will have a new Tuple called as tup2 and let me just store some random values inside this I’ll have 1 a true then I’ll have two B B and I’ll have false inside this so I have created T2 now if I want to extract the first element so that is obviously present at index number zero I’ll just type in T2 and inside this I’ll give in the index value which is zero and as you guys see I was able to extract this particular element from this entire Tuple now similarly if I want to extract the last element so I’d have to type in T2 I’d have to give in the parenthesis and to extract the last element I’d have to give minus1 and if I want to extract a series of elements so let’s say if I want to extract true two and B so here true the index value would be two so I’ll have T2 the starting index value is 2 and this two 3 4 5 so since this goes till 5 five is also included I would have to give 6 0 1 2 3 4 5 this will go till six let me hit run and as you guys see I have included true 2 B and false if I let’s say Wanted only till B so this will be 2 3 4 and if I since this has to be included I’ll just given five over here and I have extracted only true two and B now we’ll actually try to modify a tuple so initially I had told you guys that that a tuple is immutable now when I say a tuple is immutable I would basically mean that whatever you store inside it cannot be changed so here I’m actually trying to change the value which I had actually stored in a tuple so as you guys see T1 and inside this the whatever element was present at index number two I am trying to change it but we get an error over here and the error is duple object does not support item assignment so let me just print an tup2 over here I’ll hit run and let’s say if I want to change this particular value so I have this is present at 0 1 2 and three index number three so tup2 and index number three I’d want to change that from 2 to 20 let’s see what do we get we get the same error Tuple object does not support item assignment because Tuple is an immutable object and that is why you cannot go ahead and change whatever is stored inside a tuple so we have seen that a tuple cannot be modified now let’s go ahead and perform some basic operations on top of the Tuple so here we have the same Tuple where we have all of these elements over here 1 a true 2 B and false now if you want to find out the length of a tuple or in other words you would want to find out how many elements are present in a tuple then we can just go ahead and use this Len method so this would give us the number of elements El which are present in this Tuple and as you see over here we’ve got six elements and that is the result over here then going ahead we can also concatenate two tuples that is we can attach the elements of one Tuple to the back end of another Tuple so here we have tp1 where we have elements 1 2 and 3 then we have T2 where we have elements four 5 and six now when we are trying to concatenate all we have to do is use the plus symbol or the plus operator and when we use tup 1 + T2 we get the result 1 2 3 4 5 6 so let’s go ahead and perform these operations in Jupiter notebook so I’ll just create the tup again over here I have T1 and let me add in some elements so I’ll have 1 a true then I’ll have 2 B and then I’ll also have false over here so I have created this stuple now if I want to check the length of it all I have to do is use the L en method and inside this I will be passing in the object and as you guys see we have the result which tells us that there are six elements present in this Tuple now I’ll create two more tupal I’ll have tup2 over here and inside this I will have the elements 1 2 and 3 going ahead I’ll create another Tuple with the name T3 and inside this I will store the elements four 5 and six now I would have to perform tup2 plus T3 let me change the spelling over here and as you guys see we have concatenated these elements at the back end of T2 so this was a very simple operation now if we want to repeat the elements which are present in a tuple that is also something which we can perform so here in this Tuple we just have two elements which are Sparta and 300 now if you want to repeat these elements a certain amount of time then we have to multiply this with a scalar number so here when I’m multiplying tup 1 with three I get Sparta 300 Sparta 300 and Sparta 300 which basically means I am just repeating these elements three times now we can also perform repeating and concatenating at the same point of time so here we have tup1 and T2 so first what I’m doing is I am repeating the elements which are present in T1 so here when I use tup1 into three the elements are repeated three times so I have Sparta 300 repeating three times then I am attaching or concatenating tup2 at the back end of this so let me add a comment over here I’ll just have repeating elements in a tuple and now I have this tp1 over here let me just print out T1 and if I want to repeat these elements three times all I have to do is type in tup1 into three and let’s see so as you guys see 1 a true 2 B false so we have this once then the same thing is being repeated twice and the same thing is being repeated twice now similarly if I want the entire thing to be repeated five times I would have to multiply this with five so as you guys see I have repeated all of the elements five times now we’ll do repetition and concatenation at the same time I’ll add a new comment over here repetition and concatenation I’ll have two tuples over here I’ll have T1 where I’ll have let’s say a b and then I’ll have C then I’ll go ahead and create a new Tuple which will be tup2 and inside this I will have x y and then I’ll also have Z and now this is an interesting operation so I’ll have T1 into 3 plus T2 and let’s see the result so as you guys see I have repeated the elements which are present in tup1 three times so I have ABC ABC and ABC then I’m adding this at the back end of it and I get X Y and Z so this was another simple operation now we also have some simple Tuple functions so if you have a tuple and if you want to find out the minimum value and the maximum value which are present in it all we have to do is use the Min method and the max method so over here I have these elements and if I want to find out the minimum value which is present over here all I have to do is use the Min method and I pass in the tp1 object inside this and as you guys see this method tells us that the minimum value which is present in this duple is one going ahead similarly we use the max method and when we pass in tup1 this tells us that the maximum value which is present in this Tuple is five so I’ll have tup1 and let me add some numerical values inside this so I’ll add some random numbers in a random order over here so I have 8251 07 now if I want to I have to remove the C over here now if I want to find out the minimum value which is present in it all I have to do is use the Min method and inside this I’ll be passing in tup1 and as you guys see this method tells us that the minimum value which is present in this duple is zero similarly if you want to find out the maximum value I’ll use the max method and inside this I’ll again pass in tup1 and we get the result of eight this brings us to the end of this tutorial on tuples and python so dictionary is an unordered collection of key value pairs enclosed within curly braces and a dictionary again is mutable so what exactly are key value pairs let’s see an example of that so over here we are creating a dictionary where we have two key value pairs so the first key is Apple second key is orange first value is 10 second value is 20 so you can also consider it this way let’s say we have the name of the fruit and the cost of the fruit or maybe the quantity of the fruit so we have apple and let’s say there are 10 apples then we have orange and let’s say there are 20 oranges and you will be separating the key with the value with this colen over here now let me just delete all of this and let’s start fresh for our dictionary so instead of list I’ll just type in dictionary over here and let’s say I’ll create this dictionary like this and I’ll have um let’s say my first fruit is mango and I have 10 mangoes with me then I’ll have apple and let’s say I have 20 apples then I have lii and I have 30 lies and finally I would have strawberry and I would have 40 strawberries with me let me print out the result over here so this is our first dictionary which we have just created and just to ensure that we have actually created a dictionary let me check the type of it so type of root would tell us that this is of dict type which is basically a dictionary now once we have created a dictionary we can actually go ahead and extract the individual keys and values which are present over here so this is our dictionary and if you want to extract only the keys so this what you see on the left side of the colon those are our keys and if you want to extract only the keys all you have to do is use the name of the dictionary follow it up with the keys method and we’ll get all of the keys which are present in this dictionary similarly if you want to extract all of the values we would have to use the values method so when I type in fruit. values I am able to extract all of the values which are present over here so I’ll have fruit which is a dictionary which is already present and if I want to extract all of the keys I’ll just go ahead and use the keys method and as you guys see I am able to extract all of the keys which are present similarly if I want to EXT ex ract the values I’ll type in fruit. values and I have extracted all of the values which are present now since dictionary is mutable we can modify it so that would mean we can add a new element or we can change an existing element so here we had only four elements but if I want to add a Fifth Element so here we don’t have mango initially but if I want to add mango all I have to do is use the name of the dictionary then inside parenthesis I’ll add the new key so this what you see inside parenthesis I’m adding the new key and I’m adding the value to it so here as you guys see I have attach this new key value pair at the end of this dictionary similarly if I want to change an existing element so initially the value of Apple was 10 but if I want to change the value then inside the parenthesis I’ll just give in the key and I’ll assign a new value to it so initially we had 10 now we have modified it to 100 now we’ll see how to add a new element so I’ll have fruit over here let me just print it out we have four elements now let me add a new element inside this so I’ll have fruit I’ll have the square braces and let’s say the new fruit which I’ll be adding this guaa and let’s say I have 50 guavas with me and let me print out fruit right now and let’s check the result so as you guys see we have attached this new key value pair at the end of this dictionary and finally let’s see actually how can we modify an existing elements so we’ve got let’s say if I want to modify this particular key value pair so I have lii and the value of lii is 30 so I’ll have fruit inside this I’ll give in the key which is lii and I want to change 30 to 300 I’ll just assign 300 to this and let me print this out and as you guys see initially the value was 30 I have successfully changed it to 100 now we’ll go ahead and work with some dictionary functions so let’s say if we have two dictionaries over here we have fruit one and fruit two so in Fruit one we have Apple and Orange in Fruit two we have banana and guaa and if I want to append the elements of fruit two to fruit one or in other words if you want to concatenate the fruit two values to fruit one all we have to do is use the update method so I have fruit one and I’ll use update method and I’ll pass in Fruit two inside this so as you guys see we have appended banana and guaa to the end of fruit one then similarly we can go ahead and pop an element from a dictionary so we can uh so if we want to pop any key value pair so inside the pop method we would have to give the key which we want to pop so we had orange but I don’t really like oranges so that is why I went ahead and I popped out Orange so as you guys see orange is not present in this particular list now let’s create two more dictionaries I have fruit one and I’ll have two fruits inside this so I’ll start with mango and I have 10 mangoes then I’ll have apple and maybe I have 20 apples with me then I’ll have fruit two and in Fruit 2 let’s say I’ll start off with guaa and I have 30 guavas with me then going ahead I’ll have lii and I’ll have 40 Lees with me so I have created these two dictionaries so we have made a mistake over here let’s actually check what this mistake is so instead of the equal to operator I’d have to give colon over here that is important so I have created fruit one and fruit two let me print out fruit one and fruit two for your sake and once we have printed these two let me go ahead and actually epen the values of root2 to fruit one so for this I’d have to use fruit one then I’ll Us in the dot operator over here and after that I will use the update method and inside the update method I’ll be passing in Fruit two and let me print out let me close this first now let me go ahead and print out fruit one now as you guys see I have appended the values of frot 2 to fruit 1 now we have frot one already but let’s say if I want to pop out something from this so let’s say from this if I want to pop out the value of lii I’ll have fruit one then I’ll use the pop method so fruit one. pop so we have an error because we’d actually have to give a key inside this so because I’d want to pop out lii I’ll give an lii over here and we have successfully popped out lii from this now we’ll head on to the last data structure in Python which is set so set is an unordered and unindexed collection of elements enclosed within Square braces so when we say unordered so in whatever sequence you insert the elements in a set those that particular order does not remain intact and also when we say it is not indexed you can’t extract elements from a set with a particular index value because there is no proper ordering and also you’d have to keep in mind that in a set duplicates are not allowed so you can’t have the same element twice but if you actually given the same element twice what happens is the set takes it only once and uh we are creating one particular set over here and if you want to add a new element inside this so initially we are creating this set where we have all of these elements we have 1 a true 2 2 B and false and if I want to add a new element at the end of this or somewhere so I’ll just use S1 do add and this is how we can insert the new element inside this now let’s say instead of adding just one particular element if I want to add multiple elements at the same time so instead of the add method we will be using the update method and with the update method I am passing in these list of values which are 10 20 and 30 and as you guys see I have inserted 10 20 and 30 inside this but then again you have to keep in mind that the order is not maintained in a set so these are inserted randomly and if you want to remove a particular

    element you can just use the remove method and you will pass in the element that You’ want to remove again since there is no indexing you can’t remove elements with an index value you would have to give the value which You’ want to remove explicitly so let’s create our first set so I’ll have S1 I’ll just add some elements over here I’ll have a b c d e and f let me print out S1 for your reference and this is what we have now let’s say I’ll add some duplicates inside this and let’s see what happens so I’ll have a repeating three times then I’ll have B also repeating two times then I’ll have C repeating two times now if I print this out as you guys see we have only a b c d e and f even though a is repeating three times we will only have one unique value of a similarly even though B and C are repeating two times it’ll only have one unique value of B now if I want to go ahead and add add a new element inside this I’ll use the add method so S1 do add and inside this I’ll just add Sparta so when I use S1 do Sparta and when I print S1 so we have inserted Sparta over here similarly if you want to pop out something or remove something we will have to use the remove method so I’ll have S1 do remove over here and inside this let’s say if I want to remove the element e I’ll just pass in E over here and let me print out S1 again so we have successfully removed the element E from this entire set now we’ll work with some set functions so here we have two sets S1 and S2 in S1 we have the elements 1 2 and 3 in S2 we have the elements a b and c now if we want to combine all of the elements which are present in S1 and S2 then we can use the Union operator so S1 do Union S2 will give us a union of S1 and S2 and as you guys see in the resultant we have 1 2 3 A B and C similarly we have two sets over here and if you want only the common elements which are present in both of the sets so here we have 1 to six here we have 5 to 9 if you want the common elements I would use the intersection method so when I use S1 do intersection S2 you will see that we have five and six common in S1 and S2 and that is the result which we get let me have S1 over here and in this I’ll have 1 2 and three I’ll have S2 in which I’ll store four five and six now let me use the union operator so I’ll have S1 do Union and inside this I’ll be passing in S2 and as you guys see I have appended four5 6 at the end of S1 now similarly if I want to find out the common elements so let me make some modifications in S1 so in S1 let’s say I have from 1 2 3 4 and 5 and then S2 let’s see I have the elements 4 5 6 7 and 8 now if I want to find out the common elements which are present in S1 and S2 I’ll have S1 do intersect and inside this I’ll be passing in S2 and uh we seem to have an error over here so this has to be intersection and not intersect let me click on run so as you guys see by using the intersection operator we have the common elements which are four and five now we’ll understand about flow control statements in Python and then flow control statements we’ll have decision making statements and looping statements we’ll start off with decision making statements and as you can get from the name itself decision making statements would help us to make a decision on the basis of a condition and we have a very good example over here right in front of us so let’s say you would want to play football but it’s actually raining outside so the condition over here is if it’s raining outside then you can’t play you’d have to sit inside on the other hand if it’s not raining else it is not raining then you can go out and play football so this is a very good example of if El statement then let’s look at another example let’s say you have your main exam coming up and you go ahead and give a mock exam and in that mock exam if you score greater than 70 marks then your parents tell you that they’ll buy you an ice cream but on the other hand if you score less than 70 marks then you would have to give another mock test so this again is an example of IFL statement so now that we’ve understood how IFL statements work let’s go to Jupiter notebook and Implement them so here we have two variables A and B we have stored the value of 10 in a and 20 in B and we are trying to see if the value of B is greater than the value of B that is we are checking if 20 is greater than 10 and if that is evaluated to true it will just go ahead and print out B is greater than a and this is the syntax as you see you’ll give an if the keyword if you’ll follow it up with the condition and in the condition we are checking if B is greater than a so is 20 greater than 10 that is evaluated to true and since that is evaluated to true when I hit on run I’ll get this result which is B is greater than a now let me change the condition over here so instead of checking if B is greater than a I want to check if the value which is present in a is is greater than the value which is present in B so I’m basically checking if 10 is greater than 20 and obviously this evaluates to false and since this evaluates to false whatever is present inside the body of this if will be skipped out and when I hit run you’ll see that I’ll not get any result over here because this is evaluated to false so whenever if is evaluated to false you need something else so that is why we have this else keyword over here so here we are checking if a is greater than b and since this has been evaluated to false I’ll give an lse keyword over here and I will print out whatever will happen since this is false I would have to print out B is greater than a and when I hit on this you would see that I’ll get B is greater than a and which is actually right so this is about if else condition then we have another variation of if else which is if L if else so with the help of this we can compare multiple variables together or we can have multiple conditions together and this time I’d want to find out the greatest value among three values so I have three variables over here A B and C I’m showing the value of 10 in a the value of 20 in B and the value of 30 in C and once I do that using if I start off by checking if the value of a is greater than b and also if the value of a is greater than C so as you can look over here I am giving two conditions and those two conditions have been joined with the help of this and operator if a is greater than b and if a is greater than C and if that is the case I’ll go ahead and print out a is the greatest and if either of these is evaluated to false then with respect to and operator you know that if either of these is false or both of them are false then this part will be skipped so here if a is greater than b we are checking if 10 is greater than 20 that is obviously false and we here we are checking if a is greater than C so is 10 greater than 30 that again is false so false and false will be evaluated to false and that is why we’ll be skipping out this particular line then we’ll head on to LF and this time we are checking if B is greater than a and b is greater than C so B is greater than a 20 is greater than 10 this is evaluated to true after this we are checking if 20 is greater than 30 this is evaluated to false so true and false is again false and that is why we’ll skip this as well and finally we’ll enter the final L statement and we’ll just go ahead and print out C is the greatest so this is about if LF else then we can also go ahead and use the if statement with a tuple so here we have created a tuple where we have three elements a b and c and once we create this Tuple I am trying to find out if the element a is present in this Tuple so here if a in tube one then I go ahead and print out a is present in T1 and as you see since this is evaluated to false or in other words this element is present in the stuple I’m able to print out a is present in tp1 now on the other hand if I would want to check if an element Zed is present in this duple so here I have if Zed in tup1 print Zed is present in tup1 and as you see I don’t get anything because this element is not present so here what I’ll do is I’ll add the lse statement and I’ll print out Zed is not present in T1 and this time I’ll get the result because this is evaluated to false and we’ll print out whatever is there in the else condition and this time we are going ahead and using the if statement with a list so again we are creating a list L1 over here and we have these three elements a b and c and this time what we are doing is we are checking if the value which is present at zero with index of this list is equal to a and if that value is equal to a I would want to change that value to 100 so you see if L1 of 0 is dou equal to a I am assigning a new value over here and that value is equal to 100 and after I run this you would see that initially the value was a and I have changed that value to 100 now let’s say if I run this back again and if I would want to change this value from a to zed I’ll just have Zed over here and you would see that initially the value is a and this time I have changed the value to zed and finally we will be applying the if statement with the dictionary so here we have created a dictionary D1 where we have three key value pairs K1 K2 K3 the value of K1 is 10 the value of K2 is 20 and the value of K3 is 30 and with the help of the if statement I am adding 100 more to the first key over here so the condition is if D1 of K1 is equal to 10 I’m checking if the value for the key K1 is equal to 10 then I will add 100 more to this by using this condition so D1 of K1 is equal to D1 of K1 + 100 so I have an error over here I’d have to initialize D1 and as you guys see initially the values were 10 20 and 30 and after using the if condition I have added 100 more to the first value of the first key so those were decision- making statements now we’ll head on to looping statements and these are used to repeat a task a certain number of times and again we have a very beautiful example over here let’s say you have a bucket and you would want to fill up that bucket with a mug of water now what you’ll do is consider the mug and the bucket to be empty at this point of time first you’ll fill up the mug and you’ll pour this water into the bucket then you’ll check if the bucket is full or not after this again you’ll take a mug full of water then pour it back into the bucket again you’ll check if the bucket is full or not then next time again you’ll take a mug full of water pour it back into the bucket and again you will check if the bucket is full or not and this process goes on until the bucket is completely filled up with water and you will stop this only when the bucket is filled so here what you’re doing is you’re looping or you are performing the same task again and again until a condition is met we have another example over here let’s say you’re listening to your favorite song and you put that song on Loop so here the condition is the same song will be kept on playing until you either close the app or maybe use switch off your phone so this is the condition over here the song is on Loop until you close the app you stop the song or maybe you switch off your phone then we have a very interesting example so at the end of every month you will get credited with your salary amount so here what is happening is if the date is equal to 30th or 31st and if it is the last day of the month you will have salary Creed into your bank account this again happens in a loop so these are some examples of looping statements and we have two types of looping statements in Python which are fur and while we’ll be working with both of them so we’ll go ahead and start off with the for Loop so here we have created a list called as fruits and this has these three fruits over here apple mango and banana now with the help of this fur loop I would want to print out all of the individual Elements which are present over here so I’ll have for I in fruits print of I so here I what happens is initially the value of I will be equal to Apple then the value of I will be equal to Mango then the value of I will be equal to banana and this will end once I reaches the last element which is present in this list and that is how we are printing out each element which is present in this list so this is a very simple example of how we can work with the for Loop then we can also have a nested forur Loop where we’ll have one fur Loop inside another forur loop and here we have two lists again we have one list comprising of different colors and we have another list comprising of different items so the colors are blue green and yellow and the items are book ball and chair and what I’m doing is I have an outer for Loop which would help me to pick a color so here it is for I in color then inside the outer for loop I have an inner for Loop which goes for J in item which would help me to choose an item and I print out I comma J let’s understand how this for Loop works over here so initially value of I is equal to Blue and we enter the for Loop and the value of J over here will be equal to book so I print out I comma J it will be blue book then value of J is incremented it becomes ball and I print out blue ball again then value of J is incremented it becomes chair then I print out blue chair then I go back to the outer loop and blue is incremented then the color becomes green then I have green book green ball green chair again after this value of green becomes yellow then I print out yellow book yellow ball and yellow chair this is how you can work with nested for loop after the for Loop we have the while loop so while again would help us to repeat a particular task and this task is repeated on the basis of a condition and over here I am trying to print out the first 10 numerical numbers using a y Loop here I have initialized a variable called as I and I have assigned the value of one inside this variable and after this I am checking if the value of I is less than or equal to 10 and if the value of I is less than or equal to 10 I enter this y Loop and I print out I then I increment the value of I so let’s understand what is happening in this y Loop initially value of I is equal to 1 so the condition is is 1 less than or equal to 10 and since that is true I go inside the for loop I print out one then I value is incremented it becomes two then I go back and I check if 2 is less than or equal to 10 this again is true I head back I print out two then then I increment the value of I it becomes 3 then I’m checking if 3 is less than or equal to 10 this again is true I head back into the Y loop I print out three then I value is incremented it becomes four then again I am checking if 4 is less than or equal to 10 this is true I come back into the Y loop I print out four then I will increment the value of five it becomes five then we will proceed the same way till the value of I is equal to 10 when the value of I is equal to 10 I am checking if 10 is less than or equal to 10 and this condition is true I print out 10 over here after this I have I + 1 value of I becomes 11 and this time when I check is 11 less than or equal to 10 this condition fails and this is when I come out of this y Loop and this is the result which I get over here similarly instead of the first 10 numbers if I want the first 15 numbers I’ll just go ahead and change this value over here and you would see that I have printed out the first 15 numbers now using the Y loop I can also go ahead and print the two multiplication table here I have I and I’m assigning the value of 1 to I then I have a new variable called as n and I’m assigning a value of two to this new variable and and in the Y loop again the condition is while I is less than or equal to 10 and while this condition is true I will print out n into I is = n into I then I am incrementing the value of I so let’s again understand what is happening inside the Y Loop so initially value of I is equal to 1 so the condition will be while 1 is less than or equal to 10 which is true I come back over here and I print out n into I which will be 2 into 1 is = 2 so I print out this then I increment the value of I it becomes 2 is 2 less than or equal to 10 yes that is true I come inside then this time I print out 2 into 2 is equal to 4 then I value is incremented it becomes three so is 3 less than or equal to 10 that again is true so this time I will have 2 into 3 which is equal to 6 and I print this out and this process continues till I value is equal to 10 and when I value is equal to 10 I will have 10 is less than or equal to 10 which is true so here it will be 2 into 10 is equal to 20 and we’ll print that out and after that when we increment the value of I it will become 11 so it’s 11 less than or equal to 10 which is false and this is when we will come out of this while loop so these were some examples with the help of while loop now we’ll also see how to apply this while loop on top of a list so here we have this list L1 with all of these numbers 1 2 3 4 and five and I would want to add 100 to each individual element of this list so I start off by initializing this variable I I given the value of zero and this y Loop Will Go On tell the length of the list or in other words the number of elements which are there in the list what is the length it’ll be 1 2 3 4 and 5 initially value of I is equal to 0 so we are checking if 0 is less than 5 which is true we come inside the Y Loop here it will be L1 of 0 is equal to L1 of 0 + 100 so it will be 1 + 100 we’ll print out 1 + 100 over here then we have incrementing value of I it becomes 1 so is 1 less than 5 it is true so here we will have L1 of 1 is equal to L1 of 1 + 100 so it will be 2 + 100 which will become 102 and this is how we’ll go on and print out or add 100 to each element of this list now that we have built a strong Foundation let’s elevate your skills it’s time for advanced python Concepts get ready to dwel into objectoriented programming inheritance and exception handling along with efficient file handling techniques now we’ll head on to one of the most important Concepts in Python which is objectoriented programming now when you look around you you would see that you are surrounded with objects the laptop which is there in front of you that is an object the phone which is there in your hand that again is an object the bottle which is there beside you that again is an object now if you want to represent all of these Real World objects in the programming Paradigm you would need an objectoriented programming language so we would have a lot of object-oriented programming languages and python is also an objectoriented programming language because it allows us to represent all of these real world entities in a programming world now to understand the concept of object-oriented programming we would need to understand two main components of it which are classes and then we obviously have objects so let’s start with this term called as class so what exactly is a class simply put you can consider a class to be a template or a blueprint for real world entities and we have a very simple example over here let’s take the example of a phone now when we talk about a phone a phone would again have two things associated with it it will have some properties and it will have a certain Behavior associated with it now when I say properties the phone will have a color associated with it the phone will have a cost associated with it and the phone will also have a certain battery life associated with it and along with these batteries when I say a phone will have certain behaviors associated with it now what do I mean by behaviors I simply mean that with the help of a phone you can make calls with the help of a phone you can watch certain videos on it and all also some phones allow you to play games in it so this class this phone class has properties and behavior associated with it and what exactly is a class in Python you can consider this class to be a user defined data type so as we have predefined data types so we had looked at all of these predefined data types which were integer float Boolean and string so similar to these predefined data types we can create a user defined data type and that user defined data type will be this class so here what we doing is we are creating this class this user defined data type called as mobile and this user defined data type will have attributes and methods inside it so these attributes are nothing but the properties of the class and these methods are nothing but the behavior of the class now that this is clear let’s understand the next component of objectoriented programming which is is object so we already know what is a class now object is nothing but a specific instance of a class so when we say we have a mobile class the specific instances of this mobile so we have apple Motorola and Samsung so Apple Motorola and Samsung would be objects of this class phone or mobile and if you want examples of what exactly is an object so as we have these predefined data types so these are integers so a is an integer variable and I’m storing the value 10 inside this similarly B is an integer value and I’m storing the value 20 inside this so similarly if we have the mobile data type then for this mobile data type we have the objects Apple Motorola and Samsung so that was a brief intro to objectoriented programming now let’s see how can we actually create a class in Python so to create a class in Python we’d have to start off by giving this keyword called as class then going ahead we will give in the name of the class and by convention you would have to remember that the name of the class needs to be Capital the first letter has to be Capital so that is why we have given capital P over here so the name of this class is phone and inside this we are defining two methods so the with the help of these methods we can have the behavior of this class so I am having this first method called as make call and inside this method I’m just printing out making phone call and over here as you see this method takes in a parameter which is self so for now just understand that with the help of this self parameter you will be able to invoke the attributes which are present in this class just understand this for now and as we go ahead through objectoriented programming it’ll be much more clear to you guys so as we have cre created this particular method similarly we will create another method called as play game and this again takes in one parameter which is self and all I’m doing is printing out playing game so now that I have created my blueprint or my class over here I would have to create a specific instance of it or in other words I’d have to create an object of this phone class so I’ll just write down phone over here and I will store it in this object called as P1 and now that I have the object of this class I can go ahead and invoke the methods which are present in this class with the help of this object so when I type in p1. make call with the help of this I will be able to invoke this method and I am printing out making phone call similarly when I invoke p1. playay game I am invoking this method and I’m printing out playing game so this is how we can create a class and an object in Python so let’s let’s go to jupyter notebook and work with this example so my task would be to create a class so I’ll have class I’ll give the name of the class as phone I’ll give it a color over here and after this I would have to create a method and to create a method we already know that we will be using the def keyword and I will give the name of this method as make call and we know that this takes in only one parameter which is self and inside this I will just have a print statement which will be making a phone call and once I have this method I will go ahead and create another method over here so I will call this method as play game def of play game and I’ll have self over here again I’ll have a print statement and here I will write down playing a game so I have created created my phone class over here and now that I have created this phone class I would have to create an object of this so here I will have P1 is equal to phone so I have to give parenthesis over here and this is how I’m creating an object of this phone class and now that I have the object ready so with the help of the dot operator I can invoke both of these methods so I’ll start off by invoking the make call method so I will have make uncore call and when I hit on run you would see that I have successfully printed out making a phone call similarly now when I have p1. play game you would see that I have printed out playing a game so we have created our first class and we have also created the object for this class and now in the methods which were present in the previous class there were no additional parameters we had only one parameter known as self and with the help of that self parameter we were just able to access the attributes which are present and we did not actually have any attributes in the previous class so we will modify that so to our phone class are actually the methods which are present in our phone class he will be adding some additional attributes so we are adding a new method over here called as set color and this set color method over here takes in two parameters the first parameter is self because it is compulsory then we will have this new attribute called as color and with this color parameter what I’m doing is I will have an attribute called as color and I am assigning this color to the attribute color which is present in my phone class similarly I have another method called as set cost this again has two parameters first is self because again it is compulsory then we have this additional parameter called as cost and I would also have an attri rute called as cost in the phone class and what I’m doing is I am assigning this value of cost to my attribute cost in the phone class so now that I have assigned the value of color and cost to my attributes what I’d have to do is show the value of color and show the value of cost so now that I have set these I would need two methods to show the color and show the cost so that is why I will create a new method called as Show color and this only has the self attribute or the self parameter over here because I’m not assigning anything and all I have to do is return the value and if I have to return the value I’ll just use this keyword return and I’ll print out return self. color similarly if I would have to return the cost I would have this new method called as show cost this takes in only one parameter which is self and I’ll go ahead and return here as you see I will have return self. cost and these are the additional methods which I have and then I have the same methods which are make call and play game and inside make call all we are doing is printing out making a phone call and inside play game all we are doing is printing out playing a game so let’s go ahead and modify our phone class which we had created earlier I’ll delete these records from over here let me cut this entire thing or actually I can write it over here itself so these are the methods which were present earlier I would have to add four more methods inside this so to create a method we would have to use the def keyword and I would have to set a color to the attribute so I will use this method called as set color the first parameter itself because it is compulsory then I will have this additional parameter called as color and I am assigning the value of this color to the attribute color by using the self attribute or self parameter which I have passed in now similarly I will have another method over here so as I have set the color similarly I would have to set the cost as well so def I will have a new method called as set cost this would take in two parameters the first parameter would be self and the second parameter would be equal to cost and over here I will write down self do cost is equal to cost and this is how I’m assigning the value of cost and after this I would have to print out the value of color and cost I would need one method called as Show color and the parameter will only be self and what this does is it would just return out the color so it’ll be self. color then I would need another method called as show cost and this again would take it in only one parameter which is self and with the help of this I am returning out the cost so here I will have self. cost so these are the four additional methods which I have added inside this so seems that we have an error over here let me see what exactly is this so this is line number seven and this is set. cost so here I would actually have to give a comma instead of full stop over there and now we see that we have successfully created this now after creating this class I would have to create an object so I will have P1 is equal to phone and now that I have created this object with the help of this object I can access these methods and assign values to the color and cost so I’ll just invoke P1 do set color and I will set the color of this phone to be equal to let me keep it blue over here now similarly I will also set the cost of it so I’ll have P1 do set cost and I will set the cost to be equal to let’s say $999 now that I have set the value of color and cost I can print out these two values so let me delete this so I will have P1 dot Show color and now when I hit on run you would you would see that the color is blue similarly when I have p1. show cost you would see that the cost is equal to 999 so this is how we can have additional attributes and pass in values to the attributes which are of which are belonging to a class with the help of these additional parameters now there’s a special example or a special Concept in object-oriented programming which is known by the name of a Constructor so if you have worked with other languages such as C++ or Java and if you have learned about the concept of inheritance you would know about a Constructor so normally in C++ or Java a Constructor is a special method which would have the same name as that of the class and this would help us to initialize the values of the attributes during the object creation itself so that is what a Constructor in Python itself it’s just that the Constructor in Python the name of this method will not be equal to the name of the class so the Constructor in Python goes by the name of init method so here as you see this is our Constructor we have our init method over here and I have def so in it we have the prefix of two underscores and also after in it we will have two underscores over here so we have our Constructor ready and as I’ve told you with the help of a con Constructor we will be able to assign values to the attributes during object creation itself so obviously we will have some parameters inside this and with the help of these parameters we’ll be able to assign values to the attributes so in this employee class let’s say I would have four attributes called as name age salary and gender so I’ll have these four additional parameters over here and I am assigning the value of name to this attribute similar L I am assigning the values of age salary and gender now that the Constructor is ready and I have assigned the values I would have to show the values out and to show the values I have this new method called as employee details and I will create this method like this so I’ll have def employee details and I will pass in self inside this because I’m not assigning anything and this is the default or the and we definitely have to give the parameter inside this and inside this method I’m just printing out the name of the employee the age of the employee the salary of the employee and the gender of the employee and and once we create this class it will go ahead and create an object of it so here when we are creating an object as you see we have E1 is equal to employee and during the instantiating of the object itself as you see I am passing in the values for all of the attributes so as you see over here the name I’m assigning the name to be equal to Sam similarly the age I am setting it to be equal to 32 then the salary I am setting it to be equal to 85,000 and the gender I’m setting it to be equal to male and this is how I’m assigning all of the values during instantiating of the object and once I have created the object and since I have also given all of the values to the ET I can directly invoke the employee details method and when I invoke the employee details method you would see that I am able to print out all of the details name of the employee is Sam age of the employee is 32 salary of the employees is 85,000 and gender of the employes mail so let’s go to jupyter notebook and implement this concept of Constructor so I’ll just add this comment over here Constructor and I will create this new class so I will have class employee and inside this I will go ahead and create the init method def I will have two underscores then I’ll write down init then again I’ll have two more underscores over here so I’ll start off by giving the self attribute inside this then I will start off by giving the name attribute then I’ll give in the age of the employee after this I will give in the salary and then finally we have the gender of the employee and all I have to do is assign these parameters to the attributes which are present so I will have self. name is equal to name self. H is equal to H self do salary is equal to salary and self. gender is equal to gender again so I have created this Constructor over here after this I would have to create a new method called as show employee d details let me just write down the name of this method so I will have show employee details and this will have only one parameter which will be self and inside this method I’m going to print out all of the values of the different attributes which are present so I’ll start off by printing the name so I’ll have name of the employee is I’ll have self. name then I I will have age of the employee s here I’ll have self. AG then I will have salary of the employee is here I will have self. salary and finally I will have gender of the employee s and here here I will have self do gender so let me hit on run and I have successfully created this class where I have a Constructor inside this now I can go ahead and create an object of this so I will have E1 over here and what I will do is I will give the name of this class which will be employee the first value inside this should be the name of this person so let’s see this uh employees name is Sam then Sam is 28 years old and let’s say sam around earns around $775,000 let’s make the $75,000 and Sam is mail so I have created this object over here now that I have assigned all of the values I can go ahead and invoke the show employee details method here I will have E1 do show employee details and when I invoke this you would see that I have this result name of the employee is Sam age of the employee is 28 salary of the employee is 75,000 and gender of the employeer male now we’ll understand the concept of inheritance so simply put inheritance as when you derive some properties from something else and a real world example of inheritance would be you’ll be inheriting some of your features from your parents and your parents will be inheriting some of their featur teach from their grandparents or in other words let’s say you will sort of look like your parents in a way and your parents might look like your grandparents in a so you’re inheriting some physical features from your parents now if we have to relate this concept of inheritance in Python this basically means that we will have a child class and a parent class and the child class would inherit some features or all of the features from the parent class and we have an example of inheritance over here so what we’re doing is we are starting off by creating the parent class so the parent class is called as vehicle so we will have class of vehicle and inside this I have two methods the first method is the default Constructor and in this Constructor I have two additional parameters which are mileage and cost and I am assigning the value of mileage then I’m also assigning the value of cost and once this is done I will go ahead and create another method called as show details and inside show details I’m printing out I’m a vehicle then I’ll go ahead and print out the mileage of the vehicle and also I’ll print out the cost of the vehicle so now that the parent class is ready I would have to create an object of the parent class so here I have V1 is equal to vehicle and I pass in 500 and 500 so this 500 would denote the mileage of this vehicle so this might basically mean 500 m per gallon then we have the cost which is 500 again so this would mean that the cost of this vehicle is $500 so now that we have created this object we can directly invoke the show details method with this object so I have b1. show details and as you have in the result I’m awle mileage of the vehicle is 500 and cost of the vehicle is 500 let’s go to jupyter notebook and implement this let me delete all of these previous examples from over here let me keep it fresh and I will create this new vehicle or new class called as vehicle over here we will start off by creating the Constructor I would need the init method over here and the first parameter is obviously self after this I would need the mileage of the vehicle and I would also need the cost of the vehicle then I’ll go ahead and set out these two values so I’ll have self do mileage is equal to mileage after this I will have self do cost is equal to cost now that I have created this Constructor I would have to show the details so I will have show vehicle over here and this will just have one parameter which is self and inside this method I will go ahead and print out some basic things so first I’ll be printing out I am a vehicle then I will go ahead and print out mileage of the vehicle s here I’ll have self. mileage then I’ll go ahead and print out the cost so I’ll have cost of the vehicle s here I’ll have self. cost and this is how I have created this class and after creating this class I would have to create an object of it so I will have V1 is equal to vehicle and inside this I would have to pass in the mileage value first so let’s see this vehicle would give me around 120 m per gallon and the cost of this vehicle is around $800 so I have set these values over here then I can just go ahead and invoke the sh vehicle method so here I will have V1 dot show vehicle and when I run this we have an error over here let’s understand what exactly is this error so we have self. mileage we have self. cost inside this vehicle object has no attribute mileage so I’m setting Mi i l e a g e let me keep it over here now when I run this so as you see I have successfully printed out I’m a vehicle mileage of the vehicle is 120 and cost of the vehicle is 800 so we have created our parent class now it’s time to go ahead and create our child class so to create the child class we will again go ahead and give the name of this class which is car so we’ll have class of car and to inherit something inside the parenthesis so as you see this class we did not have any parenthesis over here but after this child class we’ll have a parenthesis and inside this we will pass in the name of the parent class which is vehicle and this child class will have a method of its own which is show car this takes only one parameter which is self and I’m going ahead and printing out I am a car with this method now once I create this child class I will create an object of it which is C1 now here as you see even though I don’t have a Constructor inside this child class but I’m passing in some values this is because since this car class is inheriting the vehicle class this will automatically have these two methods inside it so this car class will have the init method and also the show details method so this car class will have three methods in total which are the Constructor from the parent class then the show details method from the parent class and also this show car method which is explicit for this car class now since this also has the Constructor we would have to pass in the values for the mileage and the cost and as you see I am passing in the value for mileage which is 20000 and the value for cost which is 1,200 then I’ll go ahead and invoke the show details method with the help of this object of the child class so as you see this object is of child class but this method is of P parent class but since this child class inherits the parent class that is why we are able to invoke this method and when you see the result we have I’m a vehicle mileage of vehicle is 200 and cost of vehicle is 12200 and since we also have this show car method which is part of the car class we can directly invoke it so when I have C1 do show car I get the result I am a car so we already have our parent class over here now let me go ahead and create the child class as well so I will have class of car and I’ll have this parenthesis and inside this I will pass in this vehicle class then I would have to go ahead and create a method which is explicit to the car class so I will have Def and I’ll name this method as show car this takes in only one parameter which is self and inside this I will have the print method and I will go ahead and just print out I am a car and when I hit on run you would see that we have successfully created this class so after creating this class I would have to create an object of this so I will have C1 is equal to car and since this inherits this this will also have a Constructor so it have to pass in a value for mileage let’s say this car would give me a vage of around 300 M per gallon and the cost of this car is around um let’s say $10,000 so I’ll pass in these two values over here now that I have passed the values let me invoke the show details method this is actually show vehicle method which is there in the parent class so C1 do show vehicle and as you see I have the result I’m a vehicle mileage of the vehicle is 300 and cost of the vehicle is $10,000 and since we also have this particular method over here I can go ahead and invoke it I will have C1 do showard this has to be small C now when I hit on run you would see that I’m able to print out I am a car now we’ll see how to overwrite the inid method in the child class so in the previous example we had created a child class where we had only one method over here but what we’ll do is we’ll also have an init method in this this child class and this init method will take in four parameters the first two parameters will just be the two parameters for the parent class and since the vehicle class has mileage and cost parameters I’ll have them over here I’ll also have the self parameter and I will add two new parameters for the car class itself so I’ll have tires and HP now to pass in the values for the super class or the parent class I would need the super method so I will write down super dot in it which would basically mean that I am invoking the init method of the super class or I am invoking the init method of the parent class and inside this I am passing in mileage and cost so these are just values of the parent class which I’m passing in and after passing in the values of the parent class I’ll go ahead and assign the values for the child class as well so here as you see self. tires is equal to tires I am assigning the value of tires over here to the attribute of the car class similarly I am assigning the values of HP over here to the attribute of the car class and once I assign these values I would have to show them out so here I will have def of show car details and I will print out I am a car number of tires are self. tires and value of horsepower is this and after I create the template of this child class I can go ahead and create an object of this so here as you see I will have C1 is equal to car and I given four values over here the first value will be for the mileage of the vehicle class the second will be the cost of the vehicle class so here as you see I’m a vehicle and when I invoke C1 do show details so here even though show details is part of the parent class I’m able to invoke this because car class is inheriting from the vehicle class and here I have mileage of vehicle is 20 and cost of vehicle is $122,000 and I also have 4 and 300 and as you see when I invoke C1 do show car details I have the result I’m a car number of tires are four and value of horsepower s 300 so I’ll go ahead and create the parent class again and child class again over here so I will have class of vehicle and this will takeen this will have in it method so I’ll write down def over here I will write down in it and this will definitely have the self parameter over here and this has two values which are mileage and cost now I would have to assign these values so it will be self dot mileage is equal to mileage over here then I will have self do cost is equal to cost and this is how I am assigning the values for mileage and cost once I do this I would have to go ahead and print out the values so I will have a new method for it I will have def of show car details over here and this will only have one parameter which is self and I will start off by printing ier vehicle and after this we would have to print out the mileage of the vehicle is the mileage of the vehicle is here it will be self do mileage then I’d have to also print out the cost the cost of the wle S sure it will be self. cost and this is how we have created this template for the vehicle class now i’ have to create the template for the child class as well so here it will be class of car and since this is inheriting from the vehicle class I’d have to pass the vehicle as the parameter inside this and after that I would have to override the init method and since I have to override I need to create an init method of the car class itself and I’ll start off by giving the self parameter then I this will have mileage and cost for the parent class then it will have ties and HP which are exclusive to the car class itself and after this I will invoke the super method so this is with the help of this I’ll be able to invoke the init method of the super class so I will have super do init and inside this I will just just pass in mileage and cost and once I do this I would have to assign the values for tires and HP so it’ll be self. tires is equal to tires and self. HP is equal to HP and now that I have created this init method or overridden this init method I would need an explicit method for the car class itself let me give a space over here and this time it will be F of show car details and I’ll have self over here and after this this will actually only have self and nothing else and I’d have to go ahead and print out I am a car and after this i’ have to print out the number of tires are here it will be self DOT tires and after that I’ll have to print out the horsepower as well so here it will be the horsepower is and the value will be equal to self. HP and now I have created the parent class and the child class as well I’d have to create an object of it here it will be equal to C1 is equal to car and I’d have to give the value of mileage and cost let’s say the value of mileage is around 30 so it would give around 30 m per gallon and the cost I would see this is $5,000 and after that let’s see this car would have four tires and the horsepower of this would be equal to $499 and we have created the object of this now that we have also assigned the values I can go ahead and invoke the methods of the parent class and the child class I will have C1 Dosh show car details over here and you would see that I have printed out I’m a car the number of tars are four and the horse par is 499 so these are the details of the child class or the car class now I’ll print out the details of the parent class so here I will have C1 dot show w vle details and when I print this out this is so seems like I’ve overridden this I will keep the name as show vehicle details over here so this method in the parent class will be show vehicle details and this method in the child class will be equal to show car details and once I have done this you would see that I have this result I’m a vehicle the mileage of the vehicle is and the cost of the vehicle is equal to 5,000 going ahead we’ll look at the different types of inheritance so we have work with single inheritance now we’ll see what is multiple inheritance and what is multi-level inheritance so we’ll start off with multiple inheritance and then multiple inheritance will have a child which inherits from more than one parent class so let’s say if you have a mother and a father obviously you will have a mother and a father and you would be inheriting some of the features from your mother and some of the features from your father and thus what is happening over here is known as multiple inheritance so as you see if there’s a child class this child class will be inheriting some features from parent one and some features from parent two and this is what is known as multiple inheritance and let’s have a look at this over here so we are starting off by creating the first parent class class of parent one I have two methods over here in the first method I am assigning the value for string one so assign string one I have self and St str1 and with the help of this I am assigning the value for this attribute of Str str1 in this parent one class then once I assign the value for this Str str1 I’ll go ahead and show out this value or return this value with show string one so in parent class one I’m assigning the value for string one then I have parent Class 2 and with the help of parent Class 2 I am assigning the value of Str str2 first then I will go ahead and return the value of s str2 then I will have a child class I will name this child class as a derived class and this over here takes in two parameters or in other words this is inheriting from parent 1 and parent 2 and this again has two methods over here the first method is assign string three and I am assigning the value for string three over here then I will go ahead and show it out as you see I am returning or I am printing out self. st3 so parent class 1 parent Class 2 and child class and after that what I’m doing is I am creating an object of the derived class or of the child class and here I have b1. assign string 1 so even though assign string 1 and assign string 2 belong to the parent class I’m able to invoke them because child class is deriving from both of the parent classes so here I am assigning the value of one to string one I am assigning the value of two to string two and I am assigning the value of three to string three once I given the values I go ahead and show out the values over here so D1 do show string one I get 1 D1 do show string two I get two and D1 do show string 3 I get three now this is a bit confusing let’s go to jupyter notebook over here and let’s create our two parent classes and one child class so for this purpose I’d have to given this keyword class and I’d have to given the name of the first parent class which is parent one and after this I will create a method Def and I will name this method as assign St str1 this will have two parameters the first parameter will be self next will be S str1 over here and I’ll just write down self. St str1 is equal to St str1 over here and once I assigned the value I have to print out this value or show out this value so for that purpose I would need another method here it will be show St Str 1 and what I’ll do inside this is this will only have the self attribute and I would have to return the value of string one so this will be equal to return of self. sr1 and you would see that I have created the first class first parent class similarly I’ll go ahead and create the second parent class this time it will be equal to class of parent 2 and and here I will have def of assign St str2 it will be self I will have Str str2 over here and i’ have to assign the value this will be equal to self. str2 is equal to St str2 over here and I’ll go ahead and create the next method I will have show St str2 I’ll have self over here and i’ have to return s Str to and I have also created the second parent class now that both of my parent classes are ready I can go ahead and create the child class so here class of I’ll just name this child class as child because that is more intuitive and inside this I will be passing in both of the parent classes I’ll have parent one as well as parent 2 and now that I pass in both of the parents I’ll create one method exclusive for the child class itself and inside the parent class I’ll be assigning the string three assign s str3 over here this will have the self parameter and I’ll have S str3 over here and this is how I’ll be assigning the value self. St str3 is equal to S str3 and once I assign the value I would have to go ahead and print it out so here it will be show std3 it will be self over here and I will go ahead and I will return so again here I’d have to keep in mind that this is self of St str2 and here again it will be equal to self of sdr3 and as you see I have created all of the three classes two parent classes and one child class which is inheriting from from these two parent classes now I can go ahead and create an object of this so here I will have C1 is equal to child once I have created this object I can go ahead and invoke the methods so I’ll start off by invoking the method of the first parent class so C1 do assign Str 1 inside this I will pass in the value of one over here then I will go ahead and invoke the method of second parent class this will be equal to assign St str2 inside this I will pass in the value two then I will go ahead and invoke the method of the child class itself assign St str3 and inside this I will pass in the value three once I invoke all of this then I can go ahead and print the out so I will have show St str1 and you would see that I have printed out one then I will have C1 do show str2 then I would have we have an error over here so let’s check this properly self. s str2 we have we are assigning the value over here and we are returning this over here C1 do show St str2 name s str2 is not defined so what I’ll do is I’ll run all of these again because I had added the self parameters and this time we need to get the result and this time as you see when I have C1 do showst str2 I get two over here now similarly I’ll go ahead and invoke the third string so I will have C1 do show sdr3 and this time when I hit on run you would see that I get the result three so this is how we can Implement multiple inheritance going ahead we have something known as a multi-level inheritance and you can consider multi-level inheritance to be grandfather father child relationship and as a grandchild inherits his or her features from maybe his parents and those parents inherit their features from their grandparents so here you have multiple levels and this is what is known as a multi level inheritance so here we have three classes we start off by creating the parent class first and in the parent class we are assigning the name of this person and then we are showing out the name of the person then in the child class we are assigning the age of the person and we are showing out the age and as you see this child class is inheriting from the parent class then we have the grandchild class where we are assigning the gender and we are showing of the gender and here you see that the grandchild class is inheriting from the child class so here there are three levels child class is inheriting from the parent class and the grandchild class is inheriting from the child class now let’s go to jupyter notebook and implement this we have to start off by creating the parent class I will have class I’ll have parent over here and inside this I will create a new method called as assign name this will have self and then we will have name over here and inside this I’ll just write down self. name is equal to name this is how I’m assigning the name then i’ have to show out the name and for that purpose I will have show name this will only have self over here and I need to return self. name and I have created the parent class now after this I would have to create the child class so here I will have class of child I will create a new method over here and I will name this method as assign age I will have self I’ll have age over here and I need to assign this AG so here it will be equal to self. AG is equal to age then I would have to show out the age I’ll have to create a new method this will be equal to show age I’m writing down self over here and I would have to return this so this will be equal to return self. Ag and this child class is inheriting from the parent class that is why I’ll pass in the parent class as a parameter to the child class then finally I will create the grandchild class here I will have class of grandchild and this grandchild class will be inheriting from the child class this again will have two methods the first method will be assign gender and this is how I am passing in the two parameters I’ll have self and gender and here I would have to set self. gender is equal to gender then I will create a new method over here show gender and here I will only have self and I would have to return this it’ll be equal to return self. gendo and now that I have created these three classes over here I have my parent class the child class and the grandchild class I can go ahead and create the object of the grandchild class I’ll call it GC and I will invoke it like this and once I create this grandchild class I can assign the name age and gender so I will have GC do assign let me write it down again so here it is GC do assign name and the name which I’m setting or giving to this person is Bob then I’d have to give him some each so here it’ll be GC do assign age and let’s say Bob is 54 years old and I’ll also assign the gender this will be equal to GC do assign gender and the gender is male I have assigned these three things now i’ have to go ahead and show them out so here it will be GC do show name and I am setting the name to be equal to I don’t have to give anything over here I just have to invoke it and as you see I get the name of this person as Bob now let me also invoke the age over here gc. show AG you would see that the age of this person is 54 then I will have GC do show gender and here as you see the gender of this person is male so let me take you to the next slide with the introduction to the file handling okay so what do we mean from file handling so whenever I just talk about the file handling topics so we say that dealing up with a text files is completely known as file handling text files you all know right do the files which we have extension that do txt right that particular files are known as the text files so let’s say you wrote out some uh text onto a file and just save that particular text file now how to deal that with that particular text file with the help of Python Programming like let’s say if you just want to write some things into that file you want to read out that what’s written into that particular file or or any particular operation you want to perform onto that particular file so how you can do that particular thing in the with the help of Python Programming that’s completely known as file handling right hope you are very much Clear first of all that what file handling means so as mentioned the definition as well that deal with the text files is called as file handling right even in Python Programming we have one another name for the file handling and that goes as IO functions that is the input output functions so whenever I see file handling or IO functions they both actually mean the same thing that’s dealing with that text files do not cut confused into these things okay next so as I as well mentioned out that what are the places what are the things that uh come under the file handling what are the operations that you could perform so in the file handling we already have many functions in buil functions which helps us to operate out and do out the steps like opening of the file reading the text whatever is written uh writing something into the file appending the text basically altering out the text deleting out some text and all these operations you could completely perform form with the help of python right so as I mentioned that there are many different functions that are particularly involved up here now after that basically I have one more thing here and that is basically that what’s the IDE that I’m going to use and what’s the python version that I’m going to use up here to for doing out the Practical for the file handling see one very important thing to let you know that basically what are the online idees you are having that do not support out the file handling technique and the reason is that with the help of the py file that pypy is basically your python file into which you write out your coding stuff so any of the online ID if that particular ID is supporting the py and the dxt file at the same time then absolutely you could use out that IDE for writing out your W start online ID otherwise I would recommend you to download out a offline ID now there are many different idas which you can go ahead with like you can use out the uh py Jam you could use out the vs code you could use Jupiter notebooks whatever you feel like you could use let me tell you my particular specifications that I’m going to use so I’m going to use about the pyam IDE and the python version which I am using is 3.9.1 right if you have the same configurations well and good and even if you have some newer version of python then also it’s absolutely fine uh do not take out the python versions below than 3.7 okay some functions work there some functions do not work so I would recommend you to upgrade your python version above 3.7 hope you are very much clear with this particular that uh what’s are dealing with the text and what are the functions or operations which you could perform and basically what’s the IDE that I’m going to use up here for doing out the stuffs right so now basically I’ll be taking you to the next slide and there we are going to discuss about the open read and the write modes which we have in the file handling so let me take you to the brief discussion of these three particular topics that’s open read and write modes okay as the name suggest for the open mode so into this particular mode what you could do you could open out any text file with the help of this particular function that’s open so this particular open mode is used whenever you just want to open out a text file for reading or for writing for altering or for doing anything so you use out the open function at the very first point now one more thing uh which comes up here is that let me take a very a live example of this particular uh do not assume it as a text file let’s say I’m having out a book okay I want to read out a book so how can I read out the book I’ll be taking taking out that particular book I would be first of all opening that then reading out the stuffs whatever I just want right same particular case applies here onto the text file as well you will be saving out your text file onto the same folder where your python file has been saved out after that the very first step that comes is the opening of file writing reading altering all these things are the secondary part that you need to do like if you are not opening the file without opening your file how you could perform out any of the operations right so that’s the reason whenever you we do the file handling whenever we just deal out with the text files so the very first method but the very first step thatp users opening our text file so that particular thing is performed by the open mode which we have here in the Python programming language hope I just made this thing very crystal clear that what is this open mode and why we just use that here in the file handling right now next I’ll be taking you to the read mode after you have opened up your file let’s say you just want to read out some text from that file let me take the again example of the book when I have opened out my book so there can be two cases for opening out my book first can be I want to read out something from that book or even I just want to write out something onto that book right only two cases could be there so so whenever is your first case that you want to read out something from your book in the same case whenever you just want to read out something from your text file so into that particular case what we do we use out the read mode right so this is our mode which is used whenever you just want to read out the text which is already stored in your text file so we use out this read mode right hope I made this thing as well very much Clear regarding the read mode as well next your second case could be that instead of reading anything you want to write out something onto that book so same case goes for the file handling as well that instead of reading out your file you want to write something you want to add some more text onto your file so in that case the write mode actually comes in place so whenever you are willing to write out anything to add some more extra stuffs onto your text file so in that case we use out this WR modes this is used whenever you want to write the text in your txt file right so hope I made this right mode as well very much clear to you that what it is used for what’s the case when we use this out and why we just use out this right mode as well right so hope these particular three modes are very much clear that what are these how we perform out the functions how we go ahead with the operations now I’ll be taking you to my IDE that’s my Pam IDE and there I’ll be letting you know that basically how we could perform out the Practical how we can read write and open up the files using the Python programming language so this is the pyam IDE that we actually are having right um I’ll be giving you a quick overview regarding this particular IDE then I’ll be uh going ahead with the Practical so uh here basically let’s I just make out one of the folders this is the folder which I’m having so what I would just for making out a python file into which I’ll be writing out my code so for doing out this particular thing I’ll be clicking on this file okay and now here I’ll be doing out the right click so as soon as I do out the right click this particular box would appear now I would just go on to the very first option that’s new okay from this new Option I’ll be going on to the number fourth option that’s python file I’ll be clicking on that particular here you need to save out your file with any particular name let’s say I’m going to give out the name as file and that’s the let’s say file handling Okay click enter so yeah this is how your notebook actually appears out here whenever you have U made out any py file that’s your py file that’s a completely python file okay hope I’m very much Clear next so here we are dealing out with the text files so it’s necessary to make out one text file see now the two cases apply up here either you make make out a text file or basically you uh you basically uh like take out one part where you already have one of the text files and put on that particular part here so what I’m going to do is that I’m going to make out one new text file here okay so for that again the same procedure go onto your project do out to right click go on the new Option and now in this case go on to the very first option that’s file so whenever you just see out this file option take this as it’s TX file and let’s say my text file name is um text only and hit enter right so click on this and click on okay so my text file has been made and that is having the extension that’s txt this file okay so hope you are able to see this particular file right now what I’ll be doing here is that I would be putting okay let me do one thing yeah I’ll be putting on the uh things and writing out my fold the proof so I’ll be making out a variable L that’s if before that okay not here the text F before that I’ll be putting on some text into this file so I would just put on the text that let’s say uh this is the topic this is the topic of file handling now one more thing to notice out here that this is a text file into which you are writing so you do need not to put out any comment any hash sign or any um double inverted quotations or single inverted quotations nothing like that is at all needed because this is a text file if you were doing out the same thing onto the py file then it would have been a problem it would have shown you errors but as you had made out a text file so it doesn’t matters at all right hope I’m very much Clear let me take you to the file where we have the python file and let’s start writing up the code let’s say I just make out one of the variables now this is known as a file pointer okay make this as a file pointer f is equal to open now open is basically my function the very first function that we are going to see let me put on a hash here and let me wrote out here the first mode that’s the open mode okay so here we use out the open in uh inside this we putting out the double inverted commas okay now into that double inverted commas you’re going to write out the text file name so my text file name is text t. txt okay let me just do out one thing because I just need to rename this out so okay one second that is okay rename file let’s do that not right here so input okay let’s do the cancel F okay let’s go ahead with this particular thing only let’s see what is going to happen out so open text.txt okay that’s my file name come to the new line and this is how you open out your particular text file simply you need to write out a variable that this we just I just wrote out here as if because we call that as a file pointer that’s the reason I mentioned out of if other than that it’s not compulsory you could put on any variables of your choice we put on the assignment operator and after that we use out the open function for opening out this particular file right so as soon as the open file has been done next thing comes is that in which mode you want to open it out you want to open in the read mode or you want to open in the right mode so whenever you are willing to open the read mode in that case we write out here R and whenever you are willing to open it in the right mode so in that case we write out okay so we write out R for the read mode and W to the new let come on to the new come on to the new come on to the new line here and write out here if above f is equal to here goes that open into the bracket my file comes that what file you want to open so that is text txt okay putting out the comma here again double inverted commas quotations come out now here you mention out that uh what’s the mode in which you want to open let’s say I want to go on with the read mode so I just simply wrote out here R so it will automatically understand out that now you want to go ahead with the read mode it means that you are you want to read out your file you are opening your file for reading that let’s come to a new new line here and I’ll be making up a variable that what we will be do that particular variable will be reading out the text for me so let’s say my variable is content a t n content is equal to and my f is the variable in which I have opened out my file in the read mode and now here comes my read function content is equal to F do read come to a new line and simply be writing out here print and into the bracket I’ll be writing out here the variable that’s content because content is only the variable into which my file is being read right it is being read and it is being stored see okay before that let me quickly run out the program then I would let you know what I was just like trying to convey out from here okay no such file or directory I would just willing that it would be happening let me make out a new file click on new uh go on to the file write out here the name let’s say that’s demo hit enter yes now I want out the txt file and click on okay okay right here so let me just close out the file from here now click onto this particular write out here something like uh uh okay that’s demo and let me quickly do out one thing let me make out uh one more right here so that’s file demo. txt click enter so yeah now this is the complete correct file which has been made out please yeah one more thing to notice out here do not miss out the extension that you want to put on fine so as soon as you put on txt now this is your correct file which has been made out so I would just once again put on a text that this is um this is a okay this is a file handling topic let’s say this is my text okay topic now let come to the back here now I would just change on the name for my file that’s demo Dot and that’s txt and I would comment out the first line because now that’s not needed at all let’s run out our file here for a while so okay it’s indexing basically it’s setting up whatever you have written out here so it’s setting up all of that all those particular things so this is one of the things which comes here onto the like uh this P charm ID okay I would just click on the Run button Above So now let me just take you above so right now you are having your output that this is a file handling topic this was the text that we have written in the demo. txt file right this was the same text which we have written out and with the help of the read function we are able to display out this particular text here in this particular context in the console of my pyam ID I’m able to get out this particular text so this is how we perform out the read mode this is how you read out the text with the help of the file handling techniques right hope you’re are very much clear with the first fall read function right now what I’ll be doing is that I’ll be taking you to the next mode and that’s my right mode so what I would just do is that I would comment out all the above three lines because now I want out my file uh to be opened out in the W mode that’s my right mode so let’s say f is equal to here goes the open function my okay one second let me come out to this particular place my file name is demo. txt Right putting out your comma here and I want to open that in the W mode now after opening that into W mode I’ll be using out the right function that what is the text which you want to write into your ta text file so let’s say I just want to uh like write on the topics that write on the T at I’m learning file handling right let me come down once okay the text which I just want to write out so yeah that’s completed now one thing to mention here very importantly which I did not mention up above right I’m mentioning out that whenever you are opening out a file it’s important to close out that particular file as well okay it’s a good practice I would say see when I relate this to the example which I have taken for explaining you about the book so in that case what we were doing in that particular particular case we were having a book so we opened it for you want to read or you want to write inside that books that depends so whenever you have open that out after doing whatever the operations you want to do you’ll be closing out that particular book as well right same case applies here onto the text files as well that whenever you have opened out any text file so you would be closing that as well right so that’s the reason I have used up here f. close file now I’ll be running out my file here now here I would not be getting out any answer into the console file into the console of my Pym ID the reason is that i’ be directly getting out the text onto my demo. txt and here goes that I am learning file handling right I’m learning file handling this is the text right that we have written up right this was a correct sentence so I’m getting out that particular sentence written up here onto my demo. txt file right so hope you are very much clear with this particular function as well that how does this right function works so I hope that you are very much Crystal Clear regarding the open function how does that work about the read function and even about the write function right so uh now we have some more further topics some more further modes to learn about so I’ll be taking you to the presentation right away and then let’s discuss about the rest of the particular topics now let’s discuss about that how we can add the text onto that text file and even how we could count out that what are the number of characters that we have added onto that particular text file so let me take you to the next slide here and here we have the adding the text and Counting characters so let’s first of all discuss about adding that text so whenever I just want to add out any text onto that txt file so in that case we have a function named that’s append a p e n d so this is the particular append function which we use for adding out some data some uh text onto your text files right so basically whenever is your case that you want to add on these lines or you want to add on that particular lines so we use out the upend function so the mode which we write here is double inverted commas and a small a as for the read and write we use to write R and W so for the upend function we used to write here as a small a right so a small a whenever you see that small a written so quickly understand that out that this is the place where anything is being added or written something onto the text file now uh let’s say you have one particular case that whatever the text you are adding on you just want to transfer out or add on that particular text onto a new line so for adding the text in a new line we use out the operator that’s back sln so back sln is one of the other operators that’s used for changing your line to a new line and then adding whatever the text is required right so for the upend function it basically helps us to add the text in your txt file as I mentioned it is used for adding your text in the txt files next the mode used is a for appending means adding or writing some text to the file so whenever I just use out the a so it means that I’m using out my append mode it means that adding or writing out some text to the file then I have that for adding the text to the file in a new line we use back slash and before writing the sentence to be added so yeah this is one more case that comes up I have already told that but one thing I was left here that whenever you just want that whatever you have written up that comes onto a new line I mean to say that uh like a new sentence is being appeared in a new line you want to append the things onto a new line so in that case use out that back slash n in the starting of the sentence not at the last okay use that in the starting of the sentence then it will particularly take you to a new line and display your sentence in a fresh new line like right so hope I’m very much clear with the upend function that what it is used for and how we use that out what are the specifications and what is the mode that we used up here then we have the next topic that’s counting the characters now we have that how you can count out the characters so it comes with the help of the Len function l e n okay so Len is the function which is basic typically used for counting out the characters right that what are the total number of characters which you are having into your file so that particular uh operation that particular thing can be added can performed with the help of the Lin function so what do you do basically first of all you open up your file and you just read out your file using some function that we already have open and read functions after reading out the file and that what takes to written onto the file you just apply out the Len function okay you just simply apply out a len function and that particular Len function is being applied in a variable I mean to say that you put on a variable use assignment operator and the variable in which you have opened your file in the read mode with the help of that particular variable you use out the Len function and as soon as all the things are done up here we simply get out the total count of the characters which you have in your text file right so hope I am very much Clear regarding Len function as well let me go once again that it’s completely used for calculating for finding out the total number of characters whatever you have used in your text file right so first of all you open out your file then you use out whatever the operation you you want to use and after that you simply use out your length function so hope I’m very much Clear regarding the upend and the Len function that what are these two particular functions what are they used for and basically how to use them out so hope you are very much clear with these things now I’ll be taking you to the py charm IDE and they will be seeing a practical for the upend and for the Len function so now let’s see that basically how we can Implement out the upend mode and the lint function so upend mode is basically used for adding on some characters onto your file and the length one is used for calculating that basically how many characters you are happen here right so okay so okay what I would just do is that I would already add out first of all some text onto my demo file because that has been it is because I have commented everything so let’s say I’m going to write out here that I am I am learning I’m learning and here goes like let’s say file handling file handling okay fine this is one of the Tes that I already have out here so I’m going to use out now my append mode to add on some more text onto this particular place so the like the short form that we use is a the very first procedure that I’m going to do is that I’m going to open out my relevant file into which I want to append out that text so that’s for me demo. txt putting out a comma putting out a double inverted commas what’s the mode and mode is a so I’m going to put that out coming to the new line uh okay now here you have I could just take on one variable let’s say that’s um addore text one variable of mine here I’m going to use out F dot write function to write or to add anything onto my notebook onto my file inside this I’m going to write out that okay above what we have written that um what is already written I’m learning file handling okay let’s write out that this is a pin mode like this right let’s come on to a new line print out the addcore text here and at last I’m going to close out my file so F do close putting out the brackets like this uh what I would do is that I would add one back slash in here as well so that whatever comes comes into a new line and here it’s time for running out the program so okay this is basically coming because I have written out your addore T so it’s basically counting out the number of simple characters that I have added so yeah that’s actually okay let’s go on to the demo. txt and see what has come here that this is a pend mode basically here what I’m getting I’m getting a new line added here that this is aend mode and which I have added through the a function using this a mode I have added out that particular thing right so hope I’m very much careful that how the sus function actually works out right so I’m going to do one thing simply I’m going to remove out these relevant things from here so the houseful great and if I again run this out so again it would be basically it will run out here now downside I not got any option any answer but here this again the sentence has been added so the number of times you gun you are going to run this statement out you’re going to run this program out so it will basically add that much number of the statements onto your relevant text file right now this was how the append function actually works out now I’ll be showing you a like for the Len function so here goes the Len function let’s come down here I would again open my file but this time my file would be opened in the uh one second uh this time my file will be opened in the read mode because I want to um uh that that I want to just add on or count on some of the relevant things right I do not want to write or rep anything like that so I simply want to count the total number of characters that’s reason my file will be opening in the read mode okay so here I’m going to write out let’s my variable is data is equal to if dot read and in the bracket I’m going to write out the variable that we are having as if okay not this if I’m simply going to write out if do rone like this next I’m having total underscore count as one of my new variables and into that I’m going to put on first of all the function that we are having Len and the variable into which I have read out my data which I have into my file so that’s the variable is only data okay I will just read on dataor read let that be and here as well goes dataor read so inside the length function you need to write out that particular variable in which you have used out that read function which I have written up here okay and at last you are going to print out here the variable in which you have counted so that’s total count and I write out like this and simply last go here the closing of my file right so what I’m going to do is that I’m going to run out this relevant program here so my total characters the total number of characters which I am having into this file is 87 right I’m to toally having 887 characters onto this demo. txt file right and why I just G get out one more statement like this because I haven’t commented out this particular line that’s that’s the reason one more line has came here right so yeah hope I’m again very much clear that how to use out the append function for adding some takes and basically how to use out the Len function for calculating for giving you account that total how many number of characters are present into your file right I hope I’m very much clear with these things so let’s move on to the next topic and see out the next functions now we’ll be seeing a one another function here in this file handling and that’s the read line function now I’ll be telling you that what read line function actually is and how is that useful here fine now let’s see we had seen about how to read out the taste how to write out the text how how to add on some things onto that particular text right we had seen how we can append and all these things are absolutely clear now there could be one case that let’s say whatever text file you are having into that text file you want to read out the text line by line like in the first line whatever the text is written first all read that out then secondly comes out in the second line whatever the text is written read out the second text in the third line whatever text is written read out the so this can be a particular case that could be here that whatever the text are here you need to read down all of the Tes line by line right so for performing out this particular function we have a readline function in the file handling read line as from the name only suggest that it helps you to read out the text read out the lines or you TT whatever is present in the line line by line right hope I’m very much clear now next basically what we are having so how to use out this particular function so for using out this function firstly you need to open out the file in whatever you mode you just want read mode or write mode whatever you just wish out you could open out the file after that you need to use out that read line function so to read out the lines accordingly we use out the read line function that is mentioned and it will basically display the line uh lines in the form of like it willb the text in the form of line by line now uh let’s say into your first uh take first line it’s written uh learning file handling into the second it is written read write and open mode into the third it is written upend mode so whenever you are going to use our read line function so first of all it is going to display you the very first T that learning file handling okay next basically it is going to dis give you out the out uh like again you going to use out the read line function so it will be giving you the next output and that will be your very second line that is read write and open functions open modes right now then will be after again if you use read line then it is going to display you the third line so this is how this particular read line function actually works this is how these functions uh play a role and help us to read out the text line by line so after writing up the things and after let’s say I just said you that okay read out this file line by line so once we had seen about that read function right that is as one function that is used to read out the text which you are having but it defers that onto that particular function you read out your text uh in a one complete goal like if you use all the read function so at one complete time it will display you all the text which is written up into your F the case where you just want to read out your text line by line one single line by line so into that particular time you use out the readline function right so hope I am very much clear with these two particular things that we have about the read line function and basically how to read out the text line by line now what I’ll be doing is that I’ll be taking you to the pyam IDE and they will be seeing up the Practical for this read line function okay so here we are onto the pyam IDE and now I’ll be using up the read line function okay but before that I’ll be writing one or one or more two more sentences onto my uh file so f. write let’s say I’m learning file handling okay uh topics I would just write that topics are open read and write mode okay L okay that’s not open it’s open like this right and let me come to the new line I would just write F do write once again and let’s say I would write here that uh let’s say next is aend function right let’s say this these are three text which are written I would just run out this particular file up here but before that what I would just do is that I’ll be commenting out this particular place right so comment that out and now let’s come to the downside and run out our program so here we go okay so nothing would be displayed here as I told you because we have written onto the file and this is my file now here I haven’t used out the back slash in that’s why it is coming like that let me just quickly use out the back slash in in the starting back slash oh okay not like this like this and back slash in in the starting and now let me quickly run that out once again here so let me go on to demo. txt and yes now it’s coming up right so I’m learning text F I’m learning file handling topics are open read and write mode and next comes the upend function and one more thing if you just want to remove out these spaces so do not give any space between the back slash n and between the sentences now it comes appears to be absolutely correct right great now what I’ll be doing up here is that I want to read out the text line by line okay what is the thing that we perform so I would simply write first of all the variable name with the help of which I have opened out my F in whatever the mode it doesn’t matters out so let’s say here I have opened in the right mode so it doesn’t matters that what is the mode that you are opening using out Simply it matters that uh what is the variable that you have taken right so you have opened out your file and the variable is f Dot and now here basically use the read line function like this and this whole particular thing would go inside the print statement like this right now I would just run out this program here for a while okay one second it’s not readable where is my text file gone one second guys um where is that particular folder uh for the great learning let me just quickly open that out so here it is not neither here it is right here so demo. txt right these are the files now let’s quickly run out our program here for a while okay not readable let’s let’s check out that what is the thing that we are making up error as and why it’s basically not displaying as that thing okay so it goes print if do readline and after that bracket is completely done before that let me comment out these three lines and let me open it in the read mode first of all now it’s the time for running out the program once again so here we done and now here I got out my very first output that’s I am learning file handing so this particular output came because uh first of all that okay one another thing that um simply the opening of the file can be done in the read mode or the write mode but you could not use any functions like this so at that particular moment I need to comment out this these lines first of all because at the same time I cannot write and I cannot use the readline function at one particular point I could not use out these two functions together right that was the reason I needed to comment out these three particular lines so read out the very first line of my text file which I’m having and that was I am learning file handling yes this was the very first thing now let me come down here what if I again use out one of the printer statement right here if do read line and put on the bracket like this now if once again I run out my program see now what output I’m getting up here uh so that output which I’m getting is topics are open read and write mode so let me just just uh do it like this right here so when I used out my first read line function I was getting my very first line displayed next case when I just used out the SEC read line function second time so this particular line was getting displayed right so hope you all got the idea regarding this now let’s see if I just once again use out this print uh read line function so F do read line and the brackets now what will happen this particular will be displayed third time means my third line would be displayed up here for the upend function so verify as these are the only three lines I’m learning file handling topics are open read and write mode and the next and the last one comes here is as the upend function right so hope I’m very much clear about these uh these three readline functions that how we just read out the line read out the text line by line now what if I just once again use out my read line function as I was having only three lines into my uh text file but what if I just once again use out my readline function now basically it would not display you any other text because there was no other line in my text file in my demo. txt I was having only three particular lines neither it will display you the error nor it will display you the text simply it will keep that particular thing as blank I would remove out now this particular thing and at last my file is getting closed because we all know that whenever we just open out a file it’s basically we need to close that particular file as well right so hope you got out this particular idea as well regarding the readline function that what is this readline function how we use this out basically how we will be able to read out the text which is present in one single line and that is completely line by line right hope you’re very much clear now we’ll be seeing up the next topics now let’s discuss about the try and accept functions so this is a particular point from where we start dealing out with the take with the exception handling right so from here I’ll be letting you know about how to deal with the exceptions so the very first topic that comes under this particular one is try and accept so let me take you to the next slide here and here goes the try and accept statement now see whatever the block of code whatever the like code you are going to write inside the tri block that gets executed whenever your code is completely error free and if you have any error into your code then basically your except part gets executed see take it in a way let’s say you’re writing out any particular code so you just put on your main logic of the program inside the try function right after that you add on one exception as well that if basically there’s such some or error into your code which you have written out so just display out that particular exception to you in the form of an error it’s not an error in the form but I would say that it’s a form of exception that would completely occur out so in that particular case you are required to put on those particular um that particular print statement or anything inside the except so if you’re is not having any errors then basically your try function would run and it will display you the relevant output whatever is the required one but if your code whatever you have written out that is having some issues that is having some errors so in that case the control of your program will go inside the accept function and then your accept block will basically execute whatever the exception would be occurring that would come or if you have written any print statement inside that that would come whatever the things you have written inside the accept part that would be displayed as an output to you in the form of an exception right hope I am very much Clear first of all regarding the try and accept the statements after that one more thing comes here is that with one single try you could use any n number of accept functions except the statements actually let me elaborate this a little bit let’s say you are uh you are have on you had put on one try and accept statement okay you put on one try statement and one accept statement now the case is that let’s say you just want to put on more than one except statements so yes basically you are completely allowed to put on that particular part here as an output right so now with one single try it’s uh okay with one excepted stateements it’s compulsory to put one try function okay it’s it’s compuls ready to put a pair of try and accept other than that with one single try statement you could use any n number of statements according to your choice whatever you just wish out you could use that much n number of statements that is the accept statements with one single try right so hope I am very much clear with the usage of the try and accept the statements so whenever you are having no error into your program so you trying block actually gets executed and when you are having any error in your program so your accept statement gets executed if basically after that when the accepted statement gets executed so whatever the printer statement you have used either that will get executed or if you haven’t used out any printer statement so in that case you would be getting an exception as a form of a result okay now the other thing as possible which I mentioned is that a single Tri statement can have more than one except statements so it’s not compulsory to always put out a pair of try and except with one single try you could use 10 except 20 except 30 except whatever the except functions except the statements you want to use out with one single try you are always allowed to do out that particular thing right hope I made this thing very much clear to you regarding the usage of the try and accept functions as well that how to use them out now we have a syntax so how to use out what’s a Syntax for try and accept so first of all you put on your try keyboard right you put that out put out the colon come to the new line so as soon as you would be coming to the new line you would automatically be getting out some tab spaces so that spaces you’ll be getting out so as to confirm that yes you are inside the tri block right after that you put on your relevant Logics you put on your statements you just write out your complete code whatever you just want to write onto that particular place right after that come out of the trial statement and then put out your except now you could simply print out ex write out except and inside that print a statement or you could write out except exception as e now these two cases occur up here what are they Ed for so this except the exception as e it it is basically used whenever you want that whatever is your relevant error or exception that is occurring you want to see out that exception as an output so in that case we use out except exception as e and whenever is the case that basically you want that whatever the exception you are actually putting on after that you want out a print statement to be getting printed as an output so in that case you simply use out accept keyword and after that you just use out your relevant print statement right so as we have learned about the try and accept Theory now let’s move on to the Practical let’s see that how it’s completely used out how to implement that out so what I’m going to do here is that first of all I am going to take out two inputs let’s say a is equal to I would simply mention out here as input and inside that I’m going to write out a statement let’s say that enter the number okay come to the new line let’s TR just take out the second one I just write out here as let’s say input enter the uh number two let’s say this is number two and let’s say this is number one so here again comes out the colon and like this right now let’s say I’m writing out the program for addition of two numbers so here goes now my triy function coming inside my triy function what do I have here so I’ll be starting up here with the things that let’s say I declare a variable let say that c is equal to now above I had simply use out the input functions only what I want to write out the program for I want to write out the program for adding of two numbers but uh at none of the places I have declared that I want the inte I want the input to be in the integer format right this is the case which is not declared so now I have declared I would declare a new variable let’s say that is C here and after that basically what I’m going to do is that now as I mentioned that we will be writing out a program for the addition of two numbers let’s say i’ be taking up the two numbers from the in from the user here right now uh when I talk about taking the input from the users in the number format so in that case uh at none of the places I have mentioned that I want the input to be the in the integer format and if I haven’t mentioned out that particular thing so in that case it will automatically take in the string format when I use out the plus sign so it would not give me the addition it would simply do the concatenation of both of those numbers right this is the case which actually happens out what I could do here simply I could just do out the relevant type casting I could write it like this and simply I would write it out like this coming to the new line I would be using out one of the printer statements and inside that I’ll be printing here as C okay after this I’ll be using out my except exception as e right this is my first sentence which I just told you right away that we could use out this particular statement that is except exception as e and here I would be simply printing out e what I’m going to get out result you all as know out that I’ll be getting out some exception here the reason is that I haven’t mentioned out any data type here in the starting and I have mentioned here so it would say that we cannot add integer and a string either it shall be integer integer or it shall be string a string right let me run out the program and so show you how these things appear okay I need to give out the number one let’s say that’s two and here goes the six so what I got here unsupported operant type for int and St Str so it basically means that the operate type The Operators which you have used out here like the variables which you have taken that are not supported one is the integer and another one is the S Str right now this is the kind of exception it occurs when you had made out any error in your program the first way to get out the exception okay now second way is that you could simply write out out here accept and inside this you could add on a printer statement that uh error error in your tribe block you could print out one simple sentence like this as well when I run this out so here let’s say I enter out my number let’s say that’s three then five so what I got error in your Tri block but it’s not that much specific that it was giving me with the previous one that was except exception as e it’s not that much specific so in that case we always try to prefer write down except exception as if we always prefer to use that only right so hope you got out the idea how exception occurs now I would type c this thing as well and now let’s write out our program in the correct Manner and I would be running this at this particular place so what I got As in first all output that into number one let say that’s four six and now I got out my resultant output that’s 10 the reason is that I wasn’t having any errors so my tri block executed and if my tri block executed my exted block will not get executed because I wasn’t having any of the errors into my program which I have written out right hope I am very much clear with this particular try and accept statements to you that how we use this try and accept what’s the syntax that we use um and what’s the correct way actually for generating out the exception so the correct way is except exception as e right hope I’m very much clear now let’s move towards forward with the some other topics of the try and except function Sol that’s in the exception handling now let’s understand about the try with the else Clause so I just told you about the try and accept right away away some minutes before now let’s see that with the try and except how you could use out the else Clause at what particular time it would get executed and all the things let’s move forward okay so first thing is that you could use out the first of all try first of all the else Clause with the try and accept the statement yes that’s allowed what is the set of instructions what are things that you need to follow up here so whenever let’s say you use out the else Clause with the tri statement so when you want to execute a set of instructions whenever you do uh let’s say the case is that whenever you do not have any exception into your program after that uh after execution of the tri block you want one more statement to get printed as an output for you so in that case we simply use out this else Clause let me give you an idea let’s say you wrote out a program for multiplication of two numbers you add on your accept statements after that you use on the else Clause inside the else Clause whatever the printer statement you are going to use so in that particular case if you do not have any exception in your program if you do not have any error in your program then after the execution of the tri block that particular elsea statement will take place for the execution hope I very much clear with this particular thing right but that will only and only be executed when you do not have any error into your uh program when you when you your exception does not actually work then in that case only that else Clause will work the syntax is that you could simply write out first of all your try statement your accept statement after that put on the else keyboard after putting that out put on the col in and come to a new line automatically some spaces you will be detected and after that you could add your relevant print statement your relevant block of code whatever you just want to be get executed if your exception hasn’t been occurred so in that particular case you could use any of the things relevant to you right there is no such restriction that you would only use printer statement or you would only use the uh like write out some logical things nothing like that you could write out anything in that P particular case right so hope I am very much clear with the use of try with the S Clause that how you could use this out and what are the cases where it is particularly used out now let’s see out the Practical that how we can use out the try and accept the statements with the else Clause so I’ll be writing out first of all two inputs from the user so a is equal to int input and I would be writing out here let’s say that into the number okay into the number one let’s coming to the new line I’ll be taking another input and that’s in here comes the input and goes that uh enter the number two okay so here I go with the small n now I’m going to write out a program for finding out that a number is even or odd okay so I do not need the second statement right if I write out the program for finding that a number is even or odd so I do not need out the second number right it only works with the one so let’s go ahead and write that out so I put on my trial statement come inside my condition goes that if a person two is equally equal to zero basically if the number is divided by two and the remainder is zero so in that case print out with the help of f strings that um I would write out the number first that a is an even number right and if this is not the case in that case print out the S part. print F strings a is an odd number right these these can be two statements two cases which could appear that if the number is even it means that the remainder is zero in that case write out that this particular number is even otherwise print out it’s OD after that here goes the except I would write out except exception as e put on the colon come to the new line print out the small e that is my exception now after this try and accept here you use out the else Clause so like this it goes inside this you could write out anything I would simply put on a printer statement that uh that else Clause got ex uh execute Ed like this I could print on one of the statements at the SSE Clause got executed right so okay now what I would just do is that I would run out this particular program so it’s asking me to enter the number one let’s say that’s four see what it got me it gave me that four is an even number and else Clause got executed now why did my else Clause got executed because my tri block got executed completely in a complete manner because there was no error or exception in my program that’s what he saying this else part got executed now what I would do let’s say I just make out any error into my program let’s say if I just uh put on one single uh this okay now if I run out my program okay so it okay it is syntax ER 1 second is equal to and here let’s say I just make on B because B is one of the variables which we haven’t defined let say I made out B run that out to you asking me to enter the number one let’s say my number is three see what it is giving me name B is not defined this is my exception that is occurring right and my else Clause even did not got executed the reason I have already told you that whenever your Tri block will get executed whenever the portion whenever the logic which you have written ins the tri block that would get execute after that only your else part will get executed if you are getting any error or exception then your else Clause will not at all get executed right so I would again make out the relevant changes so it’s a and again it’s a and now if I just again run out my program so now in that case I would not be uh getting any error any exception like that let’s say it’s right so 45 is an odd number and here I got the statement executed that else Clause got executed right hope I am very much clear with the statement for like how does the TR except and else Clause work together hope I’m very much clear with this that which part will basically execute when right so we’ll see up the next topics now let’s discuss about the finally keyword so now we be seeing up that this is finally keyword in the exception handle let’s go ahead so yeah here we have um okay I told you about the try and accept functions in a very much detail even we had done out the Practical as well right that was completely over then we then I introduced you to the else part as right that the how to use out the else Clause with the trial statement so that particular condition was applied at that particular case whenever you just wanted to like uh like let’s say you are writing out any particular logic or particular code inside the tri blog and whenever you do not have any particular error into your program it means that your accept part doesn’t works out it doesn’t executes at all in that case the else part was getting executed now what happens with the finally keyword here see finally is a keyword that would execute either you are having exception in your program or you are not having exception in your program it does doesn’t at all matters for the finally keyword it means that finally keyword has to execute no matters you are having any exception or you are not having exception right you could use the finally uh keyword directly with the try and accept or either if you just want to add on the uh that else Clause so in that case as well you could just add on your else clause and your finally keyword after that no matters that you’re using it with the else Clause your reading with the try andex whatever you just wish out but one complete case actually takes place here and that’s compulsory that whatever you’re going to use out like whatever the program or whatever the code you’re going to write inside the try and accept no matters try r or except Rons finally keyword will should be run out right now let’s quickly pay a little attention to what’s WR up which I have written out here so finally a keyword which shortly executes after to the execution of the try and accept block of statement so it means that finally is one of the keywords that is shortly going to execute after the execution of the try and the except block of the statement as I told you that particular thing as well right now when I move towards the talking of the syntax that how to use that out so syntax is super easy and even the same as we have followed from the previous uh like topics simply first for you put on your trial statement put out your relevant code or the logic whatever you just want to put inside the tri block after that put on your except so I would always recommend you to put on your except statement as except exception as e it makes a relevant exception for you it generates out a relevant exception so if you have any exception into your program so it comes to you as a completely defined one so that you could got out a clear idea that okay this was the exception that was occurring so now I won’t make out this particular exception into my program right this is what actually happens out then after that we have the El part that exceptional if you just want to add you could otherwise it’s not at all compulsory to add out right and after using out whatever the relevant is condition you want to use or do not next statement comes about the finally keyword so at this particular place you use out your finally keyword inside the finally keyword you put out your relevant code or you just want to put out a printer statement or you just want to put out some logic whatever you just want to wish you could just put that inside the finally keyword after that you execute out your program so whatever runs either try or accept that doesn’t matters but you will finally be getting the answer the finally getting the output with either our either with a try or accept and the finally keyword will surely come at the last it means whatever you have written in the finally that will surely be executed and given to you after the uh after the uh run of try or accept right so hope I am very much clear for of all regarding the finally keyword that what is this finally keyword how we just execute that out and what’s the syntax what it is used for what conditions are applied and what not are applied right so hope this thetical part is very much clear now let me take you to the Pam IDE and let’s Implement out the Practical for the sake so now let’s execute out some practical for the try and accept part right so I’ll be using up the finally keyword here with the try and accept so let’s get started up here and okay let me just come down at this particular place what I’m going to do is that uh I’m going to write out a program first of all uh to find out which is the greater number among the two okay let’s say ass simple program so I’ll be taking up the int and here goes the input and here goes my enter the number right my post number goes here next coming to the new line what I’ll be doing is that I’ll be using second variable and into that again as well I’ll be taking out the input from the user so here goes that into the num CL and giving out a space like this right so num a and num num one and num two have been taken the input from the user then comes my try function so try my condition goes that if a is greater than b right so in that case I could print out an statement print I could write out with the if strings print if I could put on that a is a is uh that’s greater than and here goes the B right a is greater than b coming back adding on my is condition is what we could print is that print uh okay let me go above add this particular place go adding the F strings in the bracket it goes like okay F will be out not inside like this right so yeah I would write here B is greater let me come down at a place right here B is 1 second so B is greater than and here here as a okay so B is greater than a that conditions are satisfied coming out and using out my except function so except exception as e putting on the col in and printing out here as e right this much code you absolutely understood because we already have dealt with these types of code previously right now coming down onto a place here I’ll be using on my S Clause as well let’s say that is as well getting executed okay let’s first of all simply add on the finally after that I would add that as class so here goes my printer statement and I would just write that um finally uh keyword uh W keyword U let’s say this is my printer statement okay fine so I don’t think that we have any of the relevant exceptions into our program because we haven’t made out any of these so my output will be first of all I’d be getting whether this any of the conditions from here and at last this particular statement will be printed let me show you how so let’s run it out here so as you remember for doing running out weekly uh on the blank space we do all the right click click on the Run option and here we go so the number one I would enter let’s say 21 okay okay above one is as well not commented one second I’m so sorry let me terminate this out from here okay yeah remember the previous program as well we have written out so let me just quickly here as well let’s comment that out right now it’s the correct time for running out the program so click on run into the number one so let’s say that’s 21 here um that’s 34 so what show me 34 is greater than 21 because my second condition was getting satisfied it means the value for the B is greater than a right and at last a statement came here that finally keyword used right because this finally statement got executed now what I’m going to do is that i’ be making out some um changes into the program leter let see here okay okay yeah let’s run this out now okay so I’ll be entering out the number one let’s say that’s 34 and number is let’s say the let’s one okay I got my relevant exception that name c is not defined other than that my finally keyword again got executed at this particular place so it showed me finally keyword used so I as will mentioned you when we were discussing about the Tory that uh finally keyboard always gets EX uted no matters that what are we using no matters that trap block is running or the except block is running the finally keyw will surely run at the particular place right hope I am very much clear with this particular thing let me close it out here and now I would be making it again the same here and let me as well show you how to use out the L CL simply after the accept you could put on the SSE condition um and one second go back hit out genter and write out your relevant print statement so print or whatever logic you want to put anything works out here so I will just write out here print um else part got executed so here goes the executed right so here else part as well I have added on here the finally and now let’s run that out so let’s say my n one is 67 and num to say 32 and here goes that 67 is written on 32 after the try we have used our the else part so that’s the reason else part got executed fast then we were having the finally keyword and then basically my finally statement was present there so this particular statement got executed that’s finally keyword used right hope I am very much clear with the complete detailed explanation for the program as well that how to add on the finally keyboard how to add on the else conditions and all the things are very much Clear hope right fine so let’s see the other things as well uh so now basically I’ll be discussing about the summary of this particular module that we have discuss so let’s take uh let me just uh take you to that particular presentation as well now let’s take out a quick summary of whatever we have learned in this exception and file handling module so let me take you to the very starting from where we had started out and there will be seeing up all the things so we had started out with the in ction that what is uh file handling so file handling basically deals with the uh text files it means that uh this is completely known as when you are dealing with the text files means you are opening you reading writing appending all these operations are done onto a text file and this particular procedure is known as file handling the another name for file handling is python file input output functions file IO functions or file in input output functions whatever you just want you could use that out right so particularly we have different uh functions here like read write upend alter many other functions we are having okay let’s move forward and here we described out the open read and write modes so open was one of the mode that was basically helping us to open out a file as we been read out a book so first of all the first um procedure that we do is that we open out that relevant book same here whatever the file you are going to uh particularly operate first of all you’re going to open out that file so that particular thing is done with the open mode then we have the read mode so read was a mode which allows you to read out whatever is written into your program into your file into your text file and write was one of the modes which basically allows you to write out anything to write anything onto your text files so these were the three modes that we had seen we had seen the Practical for these that how to implement them out then we were having adding text and the counting characters so for adding out some text onto your text file we were having that function that’s append function so append was a function that basically helps you to add on uh the text onto your file right that as well we had seen out next we were having a function that was used for counting out the characters that total how many number of characters you are having into your text file so that function was Len function right now let’s come then we had seen about the readline function so this was one of the functions which basically allows you to print out your uh relevant written out text line by line whichever text you have written into your um like into your text file so it allows you to print out all the text line by line as an output for you right then we had moved on towards the exception handling we had seen about the try and accept functions so we had seen about the try and accept statements whatever the code written inside the tri blocks gets uh it will only and only get executed when there is no such error or no such exception occurred if you are having any exception then basically the tribe block will not get executed and you will be getting out your relevant exception we had seen two methods for writing out the exception first for accept simply write out the accept and inside the print statement write out your relevant statement and the second one was you could use except exception as e and you could print out e so it will automatically give you a relevant uh and the meaningful exception right so we had seen about the try and accept we had seen one one more thing here that with one single try you could use any n number number of except functions right then we had gone ahead with the try and the else Clause so I told you that how to use out the try with the else Clause so the part uh the block of code written inside the else Clause only and only gets executed when you do not have any exceptions into your program if you are having any exceptions then your s Clause will not at all get executed right this was what we had seen in the try with else clause and at last we had seen about the finally keyword that what’s the finally keyword how we just execute uh like um in the finally whenever you do not have any uh like uh if you’re using out the tri block or if you are using out the except function either your Tri blocks executes or your except block executes it doesn’t matters but what matters is that your finally keyword will surely get executed no matters you are having any exception or you are not at all having ex any exception in your program but your finally keyword will as well get executed right so hope I am very much clear with whatever I have just told you regarding this module of exception handling and the file handling right so hope you’re very much clear with all of the topics which I had shown even what practicals we have performed hope you all are very much clear with advanced concepts in hand let’s tackle data structures and algorithms we will explore arrays stacks qes and dinker lists and dive into essential searching and sorting algorithms to enhance your problem solving skills now let’s talk about our first linear data structure that is array so what is an array it is a linear data structure that means elements will be stored in a linear fashion right linear fashion now if you talk about array let’s take an example now let’s consider that this is how you represent an array in the form of a row right and let’s suppose it contains elements 1 2 3 and four right now with every memory location there will be some address right so let’s suppose these are the four elements right 1 2 3 and four and these are some addresses let’s suppose this is 100 and this is 104 this is 108 and this is 112 now if you talk about memory obviously these addresses will be are decimal and when you talk about this this particular array it will be somewhere in the memory with four uh or you can say four bytes of memory for each integer now if I take if I consider this integer and let’s suppose integer takes four bytes now these four bytes are available for each integer now this is an integer right now it takes four bytes now the second interest will start from 104 right because now again this will take four bytes then 108 again it it will take four bytes and 101 112 right so in memory it will be somewhere around but the thing that obviously you might be thinking okay sir let’s suppose this is our memory and now if we have four and four bytes that means 8 bytes here and 8 bytes here but they are available in chunks right this is

    one chunk and this is second chunk and rest of the memory is occupied can we store this array in your memory in the memory no because it needs contigous memory allocation that means when this is the scenario where in you have memory locations or memory locations available in a one big chunk right that means if you talk about this array it requires four into 4 that is 16 bytes are available but and they are available in a in a continuous memory fashion right or they are available in in such a way that it is a one single chunk of 16 bytes okay so then only you can store the elements at that location now obviously for Simplicity I’m taking this addresses as a integer number but in reality those are hexadecimal numbers okay so it is easier for me okay so now one more thing is that the elements are stored in a linear fashion right but can we access elements randomly yes with the help of indexes so if you talk about this array right 1 2 3 and 4 obviously this there will be a name associated with this array right now we have index 0 1 2 3 now why indexing starts with zero or why there is a zero and indexing always starts with zero now the question is that right now let’s try to demystify this fact that why indexing starts from zero why not it starts from one now if you remember right I told you that there will be a name associated with this array that is ARR now this ARR is nothing but name of the array and name of the array represents right it represents its Base address right now The Base address of this was earlier we spoke about it so it is 100 this is 104 this is 108 and this is 112 right so now this is 100 now let’s talk about how you access uh uh we will talk about in the coming slides we will see how to declare and initialize our array but let’s suppose if we talk about how to access this we use array and then the subscript and then the index okay the index is one now I told you name of this array represents the Base address so Base address is 100 now plus one now this one represents four bytes okay so the four bytes then what internally happens it will be it boils down to 10 100 + 4 that means 104 now 104 is not the first location it is the second location okay now similarly if you talk about accessing the second element or third element in the array it boils down to what array of two which is nothing but 100 + 2 now this two is nothing but 8 right 108 so you will be accessing the third element in the array now how can you access the first element so AR of zero now it boils down to 100 plus 0 because there are no bytes right so it boils down to this that array indexing starts from zero and now you know why and this is how you can access elements randomly so with the help of these indexes okay so now you might be thinking okay now we have an are can we store different elements right can we store let’s suppose this is can we store in this we will store integer then we will store a floating Point number then we can we store a character no if you talk about any particular array let’s talk about this array now the data type or the type of data that you can store in this array will be homogeneous that means you can only store similar elements okay so these are some facts and this is how array works and what are the addresses what are the indexes can you store different elements no you can only store similar elements in the array now let’s talk about the applications of array now you might be thinking so why do we need this array what is the uh what is the reason that we are using this array so basically when you talk about arrays now obviously when you have a scenario wherein you want to store your elements in a linear fashion right and that to you want to store them in a continuous memory locations right so that you can use your CPU or you can use your memory efficiently right not the CPU can use your memory and you can utilize your memory to the maximum right so you want to utilize your memory efficiently at that time you can use this but obviously it will have some drawbacks right it will have some drawbacks that is why we have different different data structures right so if you want to store your data in a linear fashion you can use arrays okay now it is also suitable for for the scenarios wherein you require frequent searching right if you want to search an element in an array you can directly go and access these indexes one by one right so in a linear fashion you will access okay is this the element that you’re looking for no is 20 the element that you’re looking for no is 30 the element that you’re looking for no is 40 the element that you’re looking for yes one by one you can access all those elements and try to search for the element that you are looking for okay so it is suitable for applications which require frequent searching now let’s talk about one dimensional so if you talk about 1D array it is can be related to a row like we saw in the example right so that is what is a onedimensional array it is represented in the form of row and we have addresses like 104 104 108 112 and 116 and indexing will be obviously 0 1 2 3 4 and then there will be a name associated with this array which is ARR and then you can store the elements in this array let’s suppose here the it is an integer array okay you can store only integer elements and the size of this array is five and you have stored the elements one 2 3 4 5 so now it can be related to a row wherein elements are stored one after the other like you see you have one then you have two then you have three and there those all of these numbers are in a continuous memory are available in a con location okay now when you talk about 1D array there is only one index used right when you try to declare and initialize your area at that time you will use one subscript okay so how you can use that let’s suppose if I if I talk about this special array that I that I have defined here right you will Define it a RR and then the sub or the index is that the number of elements that are present here which is five so only one subscript will be there or one index will be used okay so this is how you uh declare your array so now let’s talk about the Declaration and initialization of this array so obviously when you talk about the array there will be a name associated with the array and then the data type are you going to store integer values in that array then the one uh that one subscript or one index that we use and then definitely the size of the array so this is how you declare your 1D array now how can you initialize it there are different way you can initialize it obviously here you are declaring it then you might use a for Loop to initialize all the elements or you can declare or initialize your array at once so how do you do that so you will write let’s suppose this is the integer array and you don’t have to specify the size you can directly write the elements right and those elements let’s suppose those are 1 2 3 4 and 5 in this case when you’re declaring and initializing your array at once at that time this size becomes optional you don’t have to specify explicitly the size of the array but obviously the size of this array will be five okay now since you are declaring and initializing it at once so this is optional but in the case where wherein you are not initializing it at that time the size becomes very important and you have to mention this size explicitly okay now let’s talk about two dimensional are so also it is known as 2D so it can be related to a table like this or you can also say a matrix wherein you have rows and columns right now in this elements are stored one after the other in such a way that you can think of it as a 1D array now this is what 1D array right as we have already seen it right and inside this 1 and D array you have another 1D array right now this is known as 2D are so now how it works right let’s suppose you have numbers over here and you have four numbers 1 2 3 4 then you have 5 6 7 8 then you have 9 10 and 11 and 12 so this is a 2d array of having or and this will be similar to what of having three rows right you have you will have three rows not four rows you will have three rows and in that in those three rows right you will have what four columns 1 2 and so numbers will be like this 1 2 3 4 5 6 7 8 9 10 11 and 12 okay done so now obviously this will have let’s suppose this is 0 based indexing and this will have a zero index here one index here and one now internally what is happening it will be a 0 0 and it will be a 01 then 02 03 then 1 0 1 1 1 2 1 3 then 2 0 2 1 22 and 23 indexing right so similar to that we will have this index will be 0 0 this will be like this and this will be like this so if you want to access the element that is present at this location what you will do you will run two for Loops right one will be starting with from let’s suppose one will start from IAL to0 to the length of this outer array that is uh three less than three right so and the another from zero to the number of rows that are there okay so this one will be the outer loop this one will be the outer loop and this one will be the inner loop that means the number of columns that are there so this is for row and this will be for column that will start from 0o to less than four okay so this is how you iterate and it will be similar to this and now what about the addressing right what about the addresses that will be there so this will be let’s suppose if this is 100 now this will be 104 this will be 108 this will be 112 this will be 116 because internally it is treated as if they are again in a continuous memory location but this time around you have a 1D array and inside that 1D array you have another 1D array so for declaring it you will use two subscripts right so it will be something the name of the then two subscripts and now this will represent the number of rows and this will represent the number of columns so in this case the number of rows will be three and in this uh and the number of columns is four right so this is how you declare your what a 2d array so Dimension depends upon the number of subscripts you are using so this time around we are using two subscripts now let’s suppose you are using three subscripts right so similar to like this three three and three okay so this time around this is the 3D array and similarly you can have multi-dimensional array right and you just need to keep on adding the subscripts that’s it okay uh now we should learn regarding array implementation right we are solving three different problem statements here the first one is we are creating onedimensional array it is very simple and people can understand it in very easy way the second one we are concentrating on creating two dimensional array that means usually we use it for Matrix which includes rows and columns also we call it as M and N or M cross n all these three are the names which you can give it for two-dimensional array and also two dimensional array is used for different purposes at the last we are trying to sort search and insert delete the elements inside an array only which is having integers so these are the problem statements we are solving for arrays in Python so let’s quickly hop into the ID and check out the first problem statement that is how to create onedimensional array and insert elements inside that also put up the output whatever the input is given by the user on the screen so let’s hop into the ID now here I’m using Google collab in order to put up the first program right so we’ll rename this I’m naming this as one dimensional array so now we’ll come to this ID and we’ll type one dimension array example where you are including array size and you are asking the user what are the different inputs and then we are presenting the same inputs received by the user on the output screen so to quickly save the time I’m just putting up the code now so this is the python code where we’ll be using for one dimensional array I’ll explain what is happening here the first thing is we are asking how many elements to store inside the array for example it might be 5 6 10 so whatever the integer number is the whole number we can give it right so again we are asking assigning a variable for input so whatever the input is given by the user will be assigned to the variable called num right then we are assigning an empty array why because whatever the size has been defined by the user is put up here so if it is five it can take only five elements if it is six it can take only six elements that’s how it goes and immediately we’ll ask to enter the elements inside the array then we’ll be pushing through a for Loop and we’ll be using one important piece of code here that is ARR do upend upend in the sense will be assigning the elements one after the other at the back of the array we are not putting up the elements which is inserted by the user in middle or in the front or somewhere right so upend will always ensure the elements which is given by the user is put up at the back of the array one after the other right so next we’ll display whatever the array elements are so the array elements are again you have to push through for Loop because it has to uh just print the elements one after the other so let’s quickly run this program and check out how does this output look right so it is asking how many elements do you want to insert into array so I’m just putting up three as of now so enter then it will ask you for first number I’m putting up four and then the second number that is five then it is I’m giving seven okay so it will display 4 5 7 also you can modify this outputs by giving commas by giving uh spaces between those if not it can generally display this way four five and 7 so this is about onedimensional array in Python let’s see the second problem statement in arrays for python right so we are going to create two dimensional integer array where you can insert row number and column number and it will fill up the elements inside the array accordingly so let’s quickly switch on to the ID that’s Google collab and check out how does 2D array work in Python so here I’ve have named this particular file as 2D array and you can name it whatever you want and I’m putting up the code here so explaining the code for you that we have asked for row row numbers so how much the row should be in your Matrix I’ll take it as Matrix only because usually rows and columns will be using in Matrix so number of rows should be given by the user and we’ll store that number in our underscore that is row number again we’ll ask the user uh input number of columns right so whatever the number is given integer value whole numbers is stored in C num that means column number you can accordingly put up the variables as per the problem statements so here to keep it relatable I have used R num and C num next we are going to assign whatever the values we have right that is given by the user that is it might be a row number or it might be a column number we’ll assign that with the elements so to assign we’ll be using for Lo because one after the other it has to be printed right at last we’ll be printing the final array and final Matrix two dimensional array in 2D array I I just put up a abbreviation here so it’s understandable for you guys so T W2 D is dimension underscore array AR RR I not put up completely array it’s just ARR so this is how we initialize and the variables declaration and this is how we execute the program let’s quickly see the output of this so I’m running it is asking for the first time that is input number of rows so the number of rows I’m giving here is uh two and again I’ll enter it will ask for number of columns so then it is three here I’m entering that and it is giving you two rows and three columns also if you want you can arrange it as per Matrix so one after the other but here I’m showing it for you guys just with see you can count three columns you have and two rows right the bracket defines the rows here right the first bracket set of brackets is for first row and second set of bracket is second rowes so this is how 2D array Works in Python so we are going for the third problem statement which we are solving for array in Python so it says Implement search sort and delete operations on array of integers right so I’m breaking these three operations that is search sort and delete into three different programs to make it simpler rather than combining everything and making it to one huge program so first I’m concentrating on deleting elements inside an array of uh integers in Python so quickly we shall hop in to the IDE and see the program here Google collab is ready and the page is empty I’m just pasting this particular code in order to use time efficiently so explaining this code the first line it says enter the size of an array right we are first accessing an array size for example example it might be 10 5 8 as per the user command and then we are inserting so many elements in into that particular array say for example it is five right so we are inserting five different elements which we have already seen by now so next it is asking which element to delete right so we are telling an element a integer to delete then it will display the new array for you so fall Loops are there in order to keep the array in sequence and it might be printing or it might be taking input from the user both we are using for Loop only and upend is for putting up the elements into the back of an array right we are not inserting element in middle or somewhere in the front abruptly the insertion should not happen so one after the other sequentially in order to upend in order to insert the elements we use AR rr. upend so let’s quickly see what is the output of it so if you have entered any element which is not there in this particular array right so it will give you element does not exist in an array so this is how the program will work so let’s quickly see the output now so it is asking for aray size I’m giving three I’ll enter Then entering all the three numbers what I want to give okay it will ask you which value to be deleted right I’m giving value five so the new array is without five that is four and six so this is how it will work and immediately I’ll show you if you give any element which is out of the array bound how it will give you an error so I’m taking three elements again 5 7 and 8 right so it will ask you which value to be deleted I’ll say one one is not there in the array it is just 5 7 and 8 right so if you put that it will say element does not exist in an array right so this is how deletion will work in Python arrays after knowing how to delete element in an array so we have to see next how do you sort elements inside an array in Python so let’s quickly hop into the ID and check out how do you sort elements inside an array right so here we’ll start coding just putting up the code here so array is already defined the elements are 10 22 38 27 11 so on right so we have five elements here to be sorted in ascending order and you can also make it descending as well I’m showing you for ascending order so what is happening here I’ve just put up a comment for better understanding displaying elements of original array original array in the sense whatever it is here is displayed first right so next it is sorting by using for Loop right so so every element it will Chuck and it will try to compare with the next element if it is greater it will push up that particular element to the back and whatever it is lesser will come in front so this kind of exchange will happen and it will sort in ascending order so ascending in the sense from smaller to higher number so quickly it will display after sorting the elements of array sorted in ascending order are so and so so let’s quickly see what is the output of this particular code right so we have original array which we have given that is 10 22 38 27 11 and then we have the sorted array so in ascending order it is 10 11 22 27 38 so this is how it will sort the major function where it will be sorting is we are using this particular lines of code which I’m just highlighting in this particular ID where it will compare each and every element inside the array to the next one if it is greater it will push it to back if it is lesser than the compared array I mean array element it will push it to front so this operation will happen in this particular lines of code right you can also sort by using sort function directly as well so this is a simple example to know how sorting will happen in Python now we shall see how how do you search an element inside an array so here I’ve tried to put up occurrence as well so let’s quickly search and see elements in an array in Python ID okay so this is the code in order to search the element also find the occurrence of it right so here this is the array set so I’m giving the number 1 2 3 1 2 5 so you can see one two is been repeated those two integers are repeated so first it is showing up the created array whatever the array which is been given is put up in the first place and next what it is doing it is trying to find the occurrences of it so with the help of Index right so the element two the number two so where it is present and how many times right so first where it is present it will show that the second time will not be counted first occurrence will be counted so let’s quickly see the output of of this particular code right so here uh the new created array is so whatever the given array by the user is been put up in the first line and the second line it it is saying the first occurrence of two at position one why this is 0 1 2 3 4 5 right so two at the first time is present in the index value array one right again next it is searching for one where it is the first occurrence of one in Array is at Index point0 right so it is showing the output zero again you have one here that is 0 1 2 3 also you have second two in fourth position but still wherever it is available at the first is being uh demonstrated in this particular program right so this is how the occurrence is counted also the elements are searched in Python now let’s talk about advantage of arrays so obviously when you have indexes associated with the array right we have indexes so is it is easy for us to access any element right with the help of this index so if you want to access the third element we can directly go ahead and say a r of two and the 1D right so we can directly access elements with the help of indexing similarly it is easy for us to iterate through it right with the help of one for Loop we can iterate through all the elements right one by one that is there okay that are there in the array and similarly if you want to do the Sorting we can go ahead and easily iterate through one uh these elements one by one and look for an element if we are trying to search an element in the array let’s suppose uh we are searching for three so 1 by one we will search okay is this element three is this element three is this element three is this element three is this element three right and if this element is three we we can easily search and also for sorting right let’s suppose we want to sort this array what would it be it will be simply what if you have four here three here two here one here and let’s suppose you have zero here okay so now you want to sort this in the ascending order so what you will do you will use two Loops one will uh one will focus on this first element and then the second uh the second one will compare all the elements okay and then at the end of this thing you will have the largest element at the end of the array so sorting iteration searching it is easy and array you just have iterate through all the elements one by one now it is a replacement of multiple variables now what you mean by this thing let’s suppose you have an integer or let’s suppose you want to store uh the r number of 10 students right so what you have you would have done earlier prior to what when you don’t know the arrays what you would have done you would have said RO number one and then it’s or you can say S1 S2 S3 S4 and one by one you can store the role numbers in these integer variables right so as soon as our students increase now let’s suppose we are talking about here 10 students now as we talk about 100 now what happens if we talk about 500 are you going to uh write 500 variables integer variables S1 starting from S1 to S500 no it is a very inefficient way of doing so right so instead what you can do you can create an array and you can create an integer array and name it student student and and there in you will have the size which which obviously represents the number of students that are there and in this case it will be 500 now if you want to change it to uh tomorrow if you want to change it to 600 you can go ahead and easily change it to 600 right so it is the replacement of multiple variables so this is what it means now let’s clear the screen and now let’s talk about disadvantages there is one disadvantage that can be easily noticed is the size now obviously when you’re talking about 1D array right the size or any array right the size is there right so you you cannot exceed this size the elements cannot exceed this so let’s suppose you have size is five you can only store five elements right you cannot go more than that or beyond that now if you have a size 100 and now you’re trying to store only two elements anyway the 100 memory locations will be there for this array that means you are was ing your memory you are not utilizing it efficiently okay so this is what it means that size is fixed and you cannot store more Elms and if the capacity is more than occupancy most of the array gets wasted okay so these are two things apart from this you need a contigous memory allocation that means if chunks of memory are available here and there you cannot store an array which is let’s suppose here you have 16 bytes and here you have 16 bytes only if you have an array which is of 16 bytes that means if you have an array of size four that array can be stored here but if you have an area of let’s suppose size eight you cannot store four elements here and four elements here that will not be happening okay that cannot happen rather okay because it needs continuous memory allocation so there is one more disadvantage and the last but not the least is that insertion and deletion is difficult now why do you say that let suppose you have an array and you are having in this array 1 2 3 4 now let’s suppose you want to insert a value zero at this location now what you need to do you insert the zero and rest of the elements every element will be swapped so swapping is required right swapping is required plus there should be Memory available so that you can store that element else if uh there are only four elements and now you want to store zero and the size is also four and that time around what happens you will store one and rest of the elements will be swapped and you will be losing this value so it is very difficult to insert the value now same thing will happen when you’re trying to delete but at that time you will not be losing data but yes swapping is required right so let’s suppose you are you want to delete this location so what you will do or delete this number what you will do you will overwrite this with three you’ll overwrite this with four and let’s suppose if there is six you will over write this with six so at the end you will have 1 3 4 6 right 1 3 4 6 and one memory location is there and it will contain the same element that is six so next time around you will just override this so again the swapping is required so it is very difficult to insert and delete an element in the array now let’s see the concept of Stack now coming to the stack stack is a linear data structure which follows last in first out order that means the element which are inserted at last will be removed first that is Le order last in first out now insertion and removal of the element has done at one end I will explain you now so let’s see an example of a stack so here if I’m having 23 45 67 89 11 and let’s suppose I’m having 50 so these are the elements that has to be inserted in this stack so now what I will do so this is my empty stack let’s suppose that and inside this stack I will insert these elements by one so first I will insert 23 after 23 I will insert 45 then I will insert 67 after 67 89 11 and 15 so this is my stack now as I told you the element which is inserted at the last will be removed first that means last in first out or Leo order so you have seen here 15 is the last element that has been inserted here so now if you want to remove the element then 15 will be the first element that will be removed so for insertion we are using push so push was used to insert the element and pop will be using to remove an element from the stack so 15 is the last element that was inserted so now I will be using pop to remove this 15 so once 15 has been removed then I’m having element 23 45 67 89 and then 11 right so once again if I want to remove the element then my 11 will be removed so once again I will write here pop so always remember that push operation will be used for the insertion and pop operation will be used for the removal so whatever the elements I was inserting here 23 45 67 89 11 15 I was using push operation so if I’m writing push 23 then 23 was inserted then after that if I’m writing push 45 then 45 was in inserted and after that if I’m writing push 67 then 67 was inserted and in this way I can use the push function to insert the element now as I written here insertion and removal of the element has done at one end why if you see this was my stack right so this is my stack so whatever the element I was inserting in an empty stack I was inserting it through one end right and I was doing insertion through push operation now if I’m doing the pop operation then also I’m doing the pop operation through one end so that’s why you can see that here it is written that insertion and removal of the element has done at one end so this was the basic concept of Stack now let’s see the example of Stack so you can see here this is my pile of coin right so this can be considered as the example of Stack why because the last coin is removing first here so this follows last in first out so I’ll remove one coin one coin so if I’m reviewing step by step that means the last coin will remove first and in this way if I will follow then you can see that I can remove one coin one after other one after other and in this way this will be the example of Stack similarly the same example goes for the DVD if I am removing One dvd after other then this can be example of Stack so the DVD which was inserted at last will be removed first the same goes for the books the book which is on the top will remove first and after that if I’m going one by one from the top so you can see that the last book that was kept will removed first and in this way this can be the example of Stack so this was the basic example of Stack now let’s see some functions associated with stack so we are having push function so as I told you that if I’m writing here push 23 and let’s suppose this is my stack so this is an empty stack so it will insert 23 here so here you can see that it is used to insert the element X at the end of Stack so here instead of x if I’m writing 23 then it will insert 23 similarly pop function as I told you that pop will remove remove the element from the stack so it is used to remove the topmost or last element of the stack so if there is only one element in the stack 23 and if I’m writing pop then it will remove 23 right and also please remember that it will remove the topmost or last element in case of this stack we are having only one element so this will be the last element so if I’m writing pop then 23 will be removed but what if I’m writing here push let’s suppose that 25 then 25 will be inserted here and once again if I’m writing pop so this will be the last element so 25 will be removed so this was the basic idea about push and pop function now coming to the size so size function will give me the size or you can say the length of the stack next we are having top so it will give the reference of the last element present in the stack so let’s suppose that this is my stack and I having 23 25 and let’s suppose 27 so this is my last element here so top fun fun will give me the reference of this last element now coming to the empty function so empty function returns true for an empty stack so if this is a stack and if this stack is empty then the empty function will return us true right so this was the basic idea about functions in stack and what will be the time complexity for each function so here the time complexity for each functions will be bigo of one for push pop size STP and EMB so for for every function time complexity will be big of one so this was the basic idea about the functions now let’s see the stack implementation so there are several ways to implement stack in Python we can use list we can use collection module from where we can provide DQ class and we can also Implement through Q module so these are some ways from which we can Implement stack in Python so now let’s see the implementation using list so in implementation using list list in Python can be used as a stack so we can use list as a stack in Python so in Python we are having append and P function we don’t have any push function in Python so if you want to insert the element we need some function right so we can use the append function which is used to insert the element now coming to the PO function yeah we are having po function in Python and pop removes the element in the Leo order that means last in first out and as we know that our stack also follow the Leo order the elements which are inserted at last will be removed first so these two are the functions that we will be using here in list now let’s see the logic of this as I told you that list in Python can be used as a stack right so here I’m using list as a stack so this is my stack variable and this is an empty list and now as I told you that if you want to insert the element then you can use append so this was my UT list that is Tack and as we know that in Python list is denoted by square brackets so now what I will do I will write here stack. append and inside this happend if I’m writing X so X will be inserted in my list now now coming to the pop function if I’m writing here stack.pop and if I’m writing print and inside that if I’m putting it then whatever the element I’m having it will remove so let’s suppose that this is my stack and in this if I’m having X element so if I’m writing stack. popop so it will remove this x element right because I’m having only one element here so the last element will be removed from the stack so this is the basic idea from where stack can can be implemented using list now let’s see the Practical example so now for practical implementation I will be using jupyter notebook so I will click on here new and then I will go for Python 3 and if I’m writing here I’ll give the name here stack and let me comment it down first here I will write here hashtag and I will write here implementation using list so as I told you that stack can be implemented using list so I’ll create a stack variable and this will contain list this is an Mt list and after that I will write here stack do append and inside this if I’m writing here welcome and after that once again I’m writing stack. append and I will write here now [Music] two once again I will write this tag. append I’ll WR great learning so you can see that this is my append now if I’m printing my stack so I will click on run button so you can see that this is my list and earlier my list was EMP but now through append function I have inserted welcome to Great learning so now this is my list now what I will do here from this stack I want to remove the element so for that I will be using pop function so I will write here stack. pop and I will put this stack.pop inside a print function so I’ll write print and now let me execute this so on executing you can see that I am getting great learning so that means the element which was inserted at the last has removed first right and as I told you that PO will always follow the Leo order last in first out so if I’m printing my stack you can see that I am getting welcome to because great learning has been removed through for function if once again I’m writing here let me copy and paste this contrl C control V so once again I’m performing here stack.pop and if I’m printing stack then you can see that I will be getting welcome only so you can see that welcome I’m getting and here stack. pop if I’m doing then two has been removed right so clearly we can see that we can Implement stack using list through aen and po function so this was the basic idea about stack implementation using list so the another way the stack can be implementation using DQ so we’ll see the concept of implementation using correction. DQ so here Stacks in Python are created by the collection module which provides DQ class so now let’s understand this is a collection module so in Python I will write here from collections so from collections module I will import my DQ class right so I will write from Collections and then I will write here import DQ so DQ here is double-ended q and here append and pop operations are faster as compared to list why because the time complexity of DQ is Big of one whereas the time complexity of list is Big of N and also in list if you are inserting more element then the list will grow and it will go out of a block of memory so python have to allocate some memory so that’s why on inserting more element in a list the list will become slow so that’s why we come with the another way from The Collection module we import DQ and then so I will create my stack variable and inside that I will assign my DQ right so now I will perform the same operation that I was performing in list I will write here append and pop so always remember that DQ will be preferred more as compared to list because the append and pop operations are faster here right and rest all the concept is same so let me execute it so now let’s see the implementation with DQ so let me comment it down here I will write here implementation using DQ now after this as I told you that if I want to implement DQ then is a class right so I have to import it from The Collection modules so for that I’m writing here from collections import DQ and I will write stack variable and inside this I will assign the DQ now after this I will write here stack. end and let me write the value as XU and if I’m printing my stack so on execution you can see that I’m getting my DQ as X now let me append some more value so I will write here stack do append Y and after that I will write here stack do append let’s suppose Zed and once again if I’m executing so I will write here print stack and on execution you can see that I’m getting XY Z now let’s perform pop operation so I’ll write here stack dot pop and let me put inside this into the pin function so as we know that if I’m writing here stack. pop so the last element which was inserted will remove first so Z will be removed here so you can see that it’s Zed has been removed now if I’m printing my stack so I’m getting here only X and Y so you can see that list and DQ are the same the only difference is that DQ is faster because the append and PFF operations are faster in DQ so this was the basic idea about the stack implementation using DQ now let’s see the stack implementation using Q so here in implementation using q q module contains the Leo q that means last in first out so here basically what happens here it works same as the stack but it is having some additional functions so it is having some additional functions and work same as a stack right now we have seen that in list as well as in DQ we were using pop as well as append operation right but here to insert the element we will be using put operation so if I’m writing here put of three then that means it will insert three in my stack so similarly if I’m writing here get function so it will remove the element and as I told you that it works same as the stack so the last element will be removed first here now we are having some functions available in the Q module so the first function that is get so as I already told you in get function it is used to remove the element now coming to the max size so here Max size means the number of Maximum elements that are present in the Q coming to the next function we are having empty function so if a q is empty then it will return true or else in other case it will return false next full so whenever the Q is full it will give us true similarly put I have already discussed about the put that if you are inserting any element so you can write the put and suppose if I am inserting here two so it will insert two in a q now now Q size so Q size will give me the size of a q so let’s suppose that if you are having a three elements that are inserted in Q 3 2 4 so what will the size of the Q Q size will be three now coming to the logic so how can I import Leo Q through the Q module so I will write here from q and then I will write here import and I will write here leao and then I will write here Q after that as I told you that stack can be implemented through the Q module so I will create a stack variable here and I will assign here Leo Q so I write here Leo q and now if I’m writing here stack dot put and if I’m writing two so this means I’m inserting the value two in a stack similarly if I’m writing here stack dot get so that means I’m removing the value from the TX so this is the basic idea now we will see all these functions in the Practical coding example so let’s start with the coding part I will write here comment and inside this comment I will write here implementation using Q now after this what I have to do I have to import Leo Q from the Q module so what I will write here I will write from Q import Leo q and I will create a variable stack and I will write now Leo Q so after creating stack variable as I told you that if I want to insert the element in a queue then I have to use the put function right so I will write here stack dot put and I will insert here let’s suppose two so you have seen that I’ve already inserted a two element now let me insert some more elements so I will write stack. putut and I will insert three here and after this I will write stack. putut 4 so this is all about the put function right so we have seen several functions in Q so let me write here function so here I will write print and if I’m writing here stack do Q size so as I told you that Q size will give you the number of elements that are present present in the Q and I have inserted three elements so the Q size must come as three so on execution you can see that I am getting the Q size is three right now I’ve also told you about the max size function right so inside this if I’m writing here Max size Max size is equal to three and if I’m writing here once again print and if I’m writing stack Dot full so as I told you that full function will return true if my stack is full so here I have allocated the max size as three and I’ve inserted three elements that means my stack is full so on execution you can see that I’m getting true right because my stack is full now if I want to remove the element from the stack then which function I can use I can use here stack dot get and now if I’m once again writing print stack dot full will I get True Value no because I have removed one element so if I’m running it so on execution you can see that I’m getting a false value so here you can see that we have used the put function get function full function Q size function Max size so this is the basic idea about the stack implementation using Q now let’s try to understand Q linear data structure what is q q is a linear data structure that means all the elements in the queue are stored in linear fashion now it follows a principle of V4 that means there’s a restriction that whatever is the first item in is the first item that is to be out okay so now let’s try to make a cube let’s suppose you are in a cube and you’re waiting for a movie you’re waiting for movie ticket to buy okay so there is one person then there is another person right these are few persons here right and you’re waiting in a queue so now the first person who is in the queue will be the first person who will get his ticket right makes sense right so he will be the guy who will get his movie ticket first and he will be out of the queue then the next person who is in the queue is the next person who will get his tickets right and let’s suppose a new person comes in he’s not going to go ahead from this person rather is going to go behind this person then the next person comes he will go after this person and in the same same way so this is nothing but a P4 principle okay the first person in is the first person out okay now insertion will always take place from the rear end okay and if you talk about deletion it will always take place from the front end okay so this is our front end and this is our RAR cool so for examples buying a tickets from the counter or it can be a movie ticket or can be a bus station you are in front of a bus station trying to get tickets for your uh bus right these are some examples now there are four major operations when you talk about Q what are those major operations Let Me Clear My screen so NQ so you are going to insert an element in the cube this is what you mean by NQ theq you’re are going to delete an element okay from the cube then Peak first that you’re going to Peak the first element that is in the in the cube and Peak last means that you’re going to Peak the last element that is in the cube so you will have two pointers one is front and another is where and with the help of these pointers you’re going to NQ DQ Peak first Peak last you’re going to perform these operations now one major advantage of these operations these four operations is that all of these operations are performed in a constant amount of time that means time complexity of Performing these operations is before of one so that is why when you talk about competative programming Q is most commonly used data structure because of these things right because of its time complexity right you are able to perform your uh operations in a constant amount of time now let’s talk about applications of Q so it is used in scheduling algorithms of the operating system like first in first out scheduling algorithm is there round robin is there and we have multi-level q that is there in all these algorithms Q is used okay for storing the data or the processes it’s also used in maintaining playlists like when you have a playlist let’s suppose you have 10 songs in a queue right and after one song the next song which is in the que will be played and it goes on for like this right so for Main a playlist again a que is used it’s also used in interrupt handling uh let me take an example here you know the process State diagram of operating system so it is also used at that time so uh when you have an interrupt and therein if your process is is being executed at that time that Pro process is printed out and it is stored in a queue now the next time when this priority or this interrupt is handled once it is done then it starts picking up the process C which was in the queue and starts executing that in the meanwhile if there are some other processes that those processes will also be in the que so a queue is maintained and once the interrupt is handled they will start taking out that process that is that was being executed earlier and executes it and completes its execution and terminates the process so it is also used in interrupt handling after learning what is Q in Python theoretically let’s know how to implement lement that into practicality so Q will be having two different basic operations that is NQ and DQ so these things will be shown in with a simple example in Python so let’s quickly hop on to python ID that is Google collab for the reason I’m using it is visible for everybody to access because it’s online availability and it is open source so let’s quickly start the simple program for Q displaying two different functions that is NQ and DQ so here is the Google collab environment where you’ll be working so what we are doing in this particular code is we are creating a class called Q right we are also giving different functions for NQ and DQ NQ is nothing but entering or inserting values to the Q and DQ is deleting values from the Q right as you all know Q will follow F4 that is first in first out so wherever you want to buy a ticket for example in your Railway stations or anywhere you will stand in a queue right so whoever in the first will get the ticket first and he or she will move out of the queue it’s same in here as well but the elements are not humans it’s all integer numbers so whatever the number you put in first is the first number to get out right so let’s quickly see here we have two different fractions as I mentioned that is NQ and DQ and later you will display we are seeing three different functions displaying NQ and DQ so what happens here is we are using self. q. appen so here whatever the item whatever the uh number you give right it will be inserted to the back of the Q right it is maintaining the sequential process of inserting the numbers or the integers or the values you give in order to insert into the queue and while deleting you can use pop right is it upend for insert and PO for deleting and display is nothing but it’s normal print statement you will display whatever the Quee it is accordingly so let’s quickly run this program here I’ve just used certain numbers 1 2 3 4 five five numbers and the after deqing right what it should display it should remove one first and 2 3 4 5 should be displayed so let’s quickly see how it is right okay so as you can see whatever the uh Q is given is printed at the first place that is 1 2 3 4 5 and then after removing the first element right so the first person will be removed because it is ff4 so 2 3 4 5 is there so this is how a simple basic Q will work in Python so after knowing a basic Q implementation right let’s see one of the type of q that is circular Q implementation there are many types of cues but still I’m taking circular Q as an example and showing you the same operations of inserting and deleting elements from the queue so let’s quickly hop into the Google collab ID and check out the program how can we build a circular Q in Python here is the program for circular q and what are the different elements we have inside this program let me tell you the first part is class declaration so here my circular Q is the class right so class can be named accordingly or whatever you feel right so keep it very program oriented rather than keeping which is off topic so here it is my circular q and then again we have two different initialization that is for NQ as well as DQ so whatever the elements we use here right whatever the items we try to insert in the que we have to ensure whether the Q is full or the Q is empty and there is still space or not so all the conditions should be checked so let’s hop into NQ and check out what are the different conditions you have to check so the first thing is the Q is full or not so before inserting something say for example the Q size is five and the element six has been inserted then it has to show an error message that is there are only five spaces they are inserting Sixth Element it is not allowed hence the cube is filled so in order to print that we use this the circular Q’s Bui statement so the next part is you have to know how when it is empty right in order to have the DQ the main condition is whenever the uh elements are out of the Q then it has to be declared as the Q is empty so nothing to delete from the que it’s every all the elements or items are deleted already so the error message or the statement the user will be the circular Q is empty now there is nothing to pop out or delete or DQ so apart from that also you can also uh find if you are trying to print something right if it is mtq it does not have anything then you have to show up the no element in the circular Q found statement why because if there is no elements there is nothing to show our display the display function does not work the print does not happen so this is a basic idea of this particular code and accordingly we have used the iterations and the Declarations so next you have to look at the inputs what we are giving I’m trying to give 12 22 31 44 and 57 right so the five elements for the Q is being given and what you have to do is you have to check the initial values first you have to display the initial values what is the exact Q which you have given with the elements to the user and then which is deleted so the first element is deleted obviously but yes how the circular Q is different from the basic Q right so let’s quickly run the program in order to see the output okay if you could see the output here right so initial Q values so is whatever we have given here that is 12 22 31 44 and 57 so after removing an element so obviously the first in first out process the first element will be removed so it is 22 31 44 and 57 what is difference between a normal Q and A circular Q if you could see here right in the last space after 57 you have a space allocated so in normal Q it is not connected here the front and rear is being connected forming a circle right if one the first element for example 12 goes out the 22 will take the first place and 31 followed by 44 followed by 57 the last place will be empty right so it is in circular motion so whatever you want to insert again right so that will for example if you want to insert six right six will sit in the fifth position that is after 57 right this this will be connected circular motion that is front will be connected to the rear part so this is the difference between the normal basic q and the circular Q now let’s talk about advantages and disadvantage ages of Q first we are talking about advantages so it follows a principle of fif or the elements are stored in fif manner that means let’s suppose this is a Q and in this Q you have elements so the deletion will take place from the front right and the insertion will take place from the rear side so this is known as DQ the deletion and insertion is known as and Q operation and both these operation are performed in a constant amount of time so that is one of the advantages right and the insertion from beginning and delation from end takes a constant amount of time plus if we want to do Peak first Peak last all these operations are performed in a constant amount of time and this is most widely used data structure when we talk about CP that is competive programming when we talk about competive programming this data structure is most commonly used because of these features that all the operations that are performed like insertion deletion Peak first Peak class NQ DQ all these operations are performed in a constant amount of time now let’s talk about disadvantages since we are only able to delete or insert from the front and the rear that means deletion from front and insertion from the rear so the Restriction of insertion or any manipulation right we have a rest restriction over these right what these operations insertion and deletion so this restriction is always there and so this sta structure that is the cube is not is not much flexible right we are fixed we can delete and insert element in a fixed pattern or in the fif manner because it’s not much flexible so now let’s start with the Ling list so Ling list is a collection of group of nodes now what is node here so here you can see that this is a node so a node will contain a data as well as reference so each node contains data and reference which contains the address of the next node so this is a node and as I told you that node will contain a data as well as reference so let’s suppose I’m inserting the data here 20 and this is nothing but a reference or you can say that pointer so this pointer will contain the address of the next node right so as I told you that link list is a collection of nodes so this is nothing but a single node so let’s suppose if I’m having more than one node and if I’m connecting them then it will form a link list so we will see the linked list representation in the next slide now so link list is a linear data structure now coming to the last point we know that in Array as well as in list elements are stored at the continuous memory whereas in link list elements are stored randomly now let’s see the representation of Link list so as I told you that link list is a collection of nodes so let’s suppose that this is my N1 node this is my N2 node and this is my N3 node so each node will contain data as well as reference or you can say pointer so this is data and this is reference so now I will give the address of this N1 node so let me write the address of this N1 nodes at 20110 I will give the address of this as 2020 and I will give the address of N3 node as 2030 so these are the addresses so as I told you that each node will contain a data so let me assign here data let’s suppose 10 is here and a reference or you can say a pointer so I told you about Pointer that pointer will contain the address of the next node so what’s the address of next node the address of next node Is 2020 so I will write here 2020 so this pointer or this reference or you can say this link will contain the address of my next node and what’s the address of my next node Is 2020 now again this will be a data and this will be a reference of my N2 node so let me assign here data as 20 and what will be the reference the reference will contain the address of the next node so here it will be 2030 now again this N3 will also contain data so I will assign here 30 so now you might have a question that what should be reference here so now are you seeing any node after this N3 node do we have node N4 or N5 not right so this reference will be assigned to null so I can write here five because there is no next node is present there right now coming to head what is head so head will contain the address of my n node that means 20110 right so this is my linked list representation now the question arises that why Ling list so now why do we need Ling list because Ling list is having more efficiency for performing the operations as compared to list so what are the operations that we are performing in Ling list we can perform the operations like insertion deletion as well as traversal so it is having more efficiency in performing the operation so moving to the next Point as I already told you that in link list elements are stored randomly whereas in list or you can say in Array elements are stored at continuous memory now moving next accessing the elements in linked list will be slower as compared to list so if you want to access the element in link list it will be slower as compared to list why I will tell you the reason now let’s see this slide link list representation so here this is my N3 node right and if I want to access the data elements of this N3 node then I have to go from N1 N2 and N3 then only I can access the elements whereas in case of list we can access the element through indexing but in linked list it’s not possible so you have to go traversy right here traversal means that you have to go through each node so if you want to access the elements of N3 then you have to start with N1 then you will go to N2 then you can go to N3 so that’s why accessing the elements in linked list will be slower as compared to list now coming to the last Point here in link list utilization of memory will be more as compared to list so let’s start with the singly Ling list so Ive already showed you the representation of Ling list which is same as the singly Ling list so in singl Ling list I’m having here a data and reference in a node so let’s suppose that this is my N1 node this is my N2 node and this is my N3 node so as I told you that each node will contain data as well as reference so I will give here data let’s suppose 10 in node two I will give as 20 and here I will give as 30 and each node is having an address so let’s suppose the address of this N1 node is 1,000 it’s having 1100 and it’s having 1200 so this reference or you can say that link or pointer this will contain the address of the next node so this will contain 1100 similarly my this reference or this link will contain the address of N3 node so I’ll write here 1200 and here after N3 node do you see any node we are not having any node so here this link or this reference will null now coming to here what is head here so head will contain the address of my first node that is 1,000 so in singly Ling list the traversal is done only in One Direction so what do you mean by traversal traversal means that you are going through each node so let’s suppose that if you want to go to the N3 node first go to N1 then N2 then only you come to N3 you can’t directly jump to N3 you have to go through N1 and N2 then only you come to N3 now let’s see some operations in singly Ling list so we are having several operations in Ling list we are having insertion deletion traversal so insertion as well as deletion can be done at beginning at any specified node as well as end now coming to the Traverse I have already told you that traversal means you have to go through each node so going through each node of the link list is a traversal now let’s see the pseudo code of single link list so if you want to create a node in a single link list then what should be the code here so I will write first here class node so here I have created a class whose name is node so this class node will also be having a object right I will create object later on but let’s see let’s suppose this is my N1 node as I told you that a node will contain data as well as reference right so instead of reference I’m writing here next I’m taking a small word here so that it will be easy for coding now so this is my node creation Now what I will do here so in this class node you have seen that I am creating a init method or you can say a Constructor so to create it for first I will write a reserved word that is DF and then I will write in it method so I will write first DF and then underscore underscore init and then I will write underscore underscore then I will write self and then comma comma data so why I written here self I will tell you later on and I’ve also passed data as a parameter here so inside this I have written self. data is equal to data and self do reference is equal to none so why I have written this because my node will contain data as well as reference so I will write here in this method self do data is equal to data and self do reference I have written here next so I will take here next is equal to none so when you are creating a node let’s say this is a node N1 so initially it will be having a data and because I’m just creating a node as of now no I’m not linking this node so the link or you can say the reference will be none right so this is my initial node I have written here self. data is equal to data and self do next is equal to none right now this is a class whose name is node I can create the object so how to create a object I will write here N1 and then I will write here class name node and inside this class I will pass the parameter 7 so here now what will happen instead of self my object will pass here so my N1 will pass here instead of self so now it will be N1 do data is equal to and what’s my data data is seven now the next step we are having the self do reference is equal to none right let’s see here self do next is equal to none so instead of self my N1 is there so N1 will be pass here and N1 do next I will be having a none so this is nothing but a creation of my node now if you want to check then we can write print function and when you will write here node one data inside a print function you will see that you are getting the value as 7 similarly if you’re writing node one. reference inside a print method then you will be getting none so now this is the idea about how to create a node now let’s see this into a coding so I will be using here Jupiter notebook so I will go on here new and then I will click on Python 3 and here you can see that I’m getting a name Untitled 21 let me change it I will write here link list link list and I will write here python now let’s create a node so I will comment it down creating a node so first I will create a class and I will give the name as node and inside this class node I’ll create my edit method so I will write here DF which is a reserved word and then I will write here in it but before that I will write underscore underscore then again I will write underscore underscore and then I will write here self comma data so so why I have written here self so when I’m creating a class object that is N1 I’ve already showed you in the example so instead of self N1 is passed so as we know that we can create a multiple object of a class so if I’m writing here N2 or N3 then instead of self I can pass N2 and N3 also so now let’s create a node so I will write here self dot data is equal to data and I will write here self dot next I will write here none so this is my node creation so whenever I’m having a node it will contain a data and it will be having a reference so initially it is not linked so the reference is none so this is my class now I will create a object of node so I’ll write here N1 and I will write class name and I will pass data as let’s suppose 8 value here so if I’m executing it so on executing this N1 will go to self and this 8 will go into the data so my N1 do data is equal to 8 and my N1 do next is equal to none let me print it so if I’m writing here print and inside that if I’m writing my N1 do data once again if I’m writing print function and inside that if I’m writing N1 do next then you can see that I’m getting the data as eight and the next that is a reference I’m getting as none because I did didn’t link this node to any other node so this is the basic idea how to create a node now let’s see how to create a class of singly Link list so when will my singly link list will be empty so as I told you that if this is a node let’s suppose N1 this is another node N2 so N1 and N2 are connected with each other so we are having a head pointer which always points to the first node right so if there is no no head if head is none then my link list will be empty so now what I will do here so I will create a class and I will give the class name as let’s say singly ling list and inside this class once again I will write init method so I’ll write here def underscore underscore in it underscore and I will write here self now here I will write self dot head is equal to none so this is my condition to create a class so if the head is pointing to none that means it is not pointing to any node and it shows that link list is empy so now let’s see the creation of singly Ling list yeah so let me remove this now I will create a class so creating a ling list so if I want to create a link list I will create a class of Link list so I will write here class class singly Ling list so this is my class and inside this class I will once again write init method inside the singl Ling list I will create an init method so I’ll write def underscore uncore init underscore underscore and then I will write here self and when my link list will be empty so when self dot head is equal to none so this is the simple way to create a single Ling list class now so after creating a class now let’s create a object of this link list so I’ll write here SL single Ling list this object name and now I will write the class name so class name is singly and then Capital LL is there right so on execution always remember whenever you are creating an object and if you are executing it so inside this class in it method will always run I will show you the example if inside this init method let’s suppose if I’m writing here print gorov so I created the object here SL and right now if I’m executing then you can see that gorov is executing here right so always remember whenever I’m creating a object of class and whenever I’m executing it so whatever the statements are inside the init method it will execute so on execution what will happen here I will get here SL do head is equal to none right so SL is my object so instead of self SL will assign here so SL do head is equal to none right so this is my basic concept of creating a node and creating a ling list let’s talk about searching algorithms and the first searching algorithm that we are going to talk about is linear search algorithm so what is linear search it helps us to search an element in a linear data structure now let’s talk about one example wherein we will be searching some element inside the array so let’s suppose this is an array and the elements are 10 20 30 40 and 50 now if we trying to search an element that is 50 inside this array how linear search works is that it checks each and every element that is to be search right that is there in the element array right let’s talk about this example here 50 now 50 will be compared right we’ll check is this 10 equal to 50 no is this 20 equal to 50 know is this 50 equal to 30 is this equal to 40 is this equal to 50 yes so here we were able to do a linear search right we were searching for this element inside this array one by one we compareed first with 10 then 20 then 30 and then 40 and finally then with 50 at the end we were able to find this element in the array in a linear fashion now this is what is termed as linear search now let’s talk about linear search algorithm since it is a very straightforward or you can say a Brute Force algorithm right it’s a Brute Force algorithm of finding the element in the array so this is how it works right we have one for Loop wherein we will be what iterating through all the elements that is from 0 to n and inside that what are we doing we looking for the item that is that element that we want to search right let’s suppose This Is 50 right this 50 will be we were checking if this 50 is equal to the element that is a of I right and then if that is the case if we find out the element in the entire array we will return its Index right that is Index right that I we will we will return this I now there might be the case as well if we are at the end of the array and we have exhausted the last element as well and we were not able to find the 50 right let’s suppose this is 10 20 30 and 40 now the 50 is not present in the entire array at that time what we are going to return is minus1 so we will say that okay we will not able to find this element whenever we are returning this minus1 in this array and this minus1 indicates that we were not able to find that element now we shall see how to implement linear search in Python so linear search will work with the help of an array here so what we are doing is we are searching one single element in throughout an array in sequential manner so this is how linear search will work so here if you could see we have array we have number which one you have to search for and you have the starting position variable so in iterations Array will Move On and On by searching from one place to another place the first place to second second to Third and so on in total we have five different elements in an array that means four different places because array starts from zero 0 1 2 3 4 so index is of four and the elements are of five so we have to first take the key search element and we have to compare that particular element to all the elements inside the array right so if it is not matching the array is not matching with the number you are searching it will throw up an error called element not found if it is found it will show you index value where it is which place of an array it is there so let me quickly run the program for you so I’m trying to search the element one right so the element one is in index position three 0 1 2 and 3 so X is the variable which is used in order to find which number it it will just act as a key X will act as a key you can change this and check if you want to search for eight for example it is not at all there in the array so it will say element not found if you want to search two for example the answer should be zero right let’s check right so index value is zero it is sited in the first place of an array if you want to search for nine it is at the last place so it’s at four right so this is how the value which you want to search is always compared with all the elements sequentially one after the other so for example 9 is compared with two it’s not matching then it will go to the next one N9 is compared with four it is not matching and zero again compared with one not matching it will go to the N ninth place where it is situated right it will compare the elements sequentially so this is about linear search in Python now let’s talk about the time complexity of linear search now if You observe carefully let’s try to understand this best case right so now if you are looking for the element and let’s suppose these are the elements in the array now let’s suppose in best case what can happen you’re looking for 10 and 10 is the first element in the array now how many iteration did you require did it require to find you the 10 none right the constant time right only one single unit operation was done and you were able to find this 10 so this is the best case time complexity where the element that you’re trying to find is the first element that you search right in this case you you looking for 10 and 10 is the first element so this is your best case now what about the average case and the worst case now let’s suppose average case is that you were looking for an element which is at the middle point right let me just 0 1 2 3 4 now or we can put another another and looking for 60 now in this case or you can just skip it okay no need to add one more okay so now you’re looking for an element which is somewhere around in the middle okay in this case you’re looking for 30 okay so now if You observe you are only iterating half of the elements that is 5×2 which is nothing by n by2 since constant doesn’t play do not play any role when you’re talking about time complexity that is why average case still boils down to bigo of n now what what happens in worst case you’re looking for an element that is 50 and which is present at the end of the array or in the worst case you’re looking for something that is not present in the array that is 60 let’s suppose and in that case you will still iterate through the entire area and that is why the worst case time complexity in that case will be biger of n because you are iterating through the entire array and that element was not found you’re looking for 60 and that is not present there so you’re iterating through the entire area that is n operations are done so that is why it boils down to Big of n now let’s talk about the space complexity of linear search when we are trying to find the element in the array that is 10 20 30 and 40 and 50 we were not using any extra memory right we were not using any auxiliary memory that is auxilary memory or extra memory in order to find that element we were just looping around these elements one by one and we were doing it on this particular on the same array that we were given right since we are not using any auxiliary memory that can be in the form of what a stack a link list or an array or a string or a q we are not using these auxiliary memories because they don’t they are not required right we are searching for an element in this particular array that we given to us that was given to us right so the space complexity of linear search is constant right with we were find we were able to find it in a constant amount of space okay we are not using any extra space now let’s try to understand binary search algorithm what is binary search so binary search is one of the searching techniques right like we saw in linear search where in the time complexity of linear search was big of n right we were iterating through all the elements and and now this is a much more efficient algorithm as compared to linear search now again why do we need searching is the thing that let’s suppose if you have a given set of elements and you want to search if that element is present in your array or not that time right we can use either linear search or binary search now binary search is much more efficient and it is used on a sorted array or it can be used on an array wherein some order is maintained because based on that order we will divide our array right it is a searching algorithm which is or which follows the divide and conquer strategy right let’s suppose this is our array and now since it will be divided in such a way that we can neglect one part of it it right we will be dividing and then conquering that means we will be then searching for our element now let’s suppose we are looking for something that is now let’s take an example where in this array is written or the elements contained in this array are in such a way that we if we skip this part or the uh leftand side will be skipped or the right hand sides can be skipped in such a way that they don’t affect our output so every time in linear search our search space is reduced unless and until we find that element or the array is exhausted okay so our search space is redu to half in every iteration so this is what a binary search is we’ll look for an element in such a way that every time we are neglecting half portion of the array and let’s take an example when we have this entire array so first half that means if four elements if there are eight elements four on this side four on this side these four will be neglected then we have two on this side two on this side these two will be neglected then one on this side one on this side then this one will be neglected until unless and until we find that element or the entire array is exhausted right so this is how your binary search works now let’s try to understand B search algorithm so first we are going to understand the iterative approach and then we are going to understand the recursive approach so iterative as the name suggests we are going to use for Loops right we will start with a for Loop and it will iterate and we will iterate unless and until the beginning is less than the end right so because we will be updating our both beginning in some cases and in some cases we’ll update our end now what happens now since we know that in this iterative approach or in this binary search it doesn’t depend whether we are using iterative approach or recursive approach the logic will remain same right so we will be having this array and it will be in some order so that we can neglect some part of it it doesn’t have to be sorted always we can still apply binary search even if the array is not sorted but still some order is there so that we can neglect some part of it because again keeping this thing in mind that it follows the Paradigm of divide and conquer so now we have this beginning and end at place and we uh we will always iterate when beginning is less than end right so now what happens after that now we have this array and now what we will be able what we will do let me take new pointers so this is your big beginning and this is your end so this is your beginning and this is your end now you will be taking a new middle Index right M let’s call it m and now let’s name these things 0 1 2 3 4 so you will do what beginning plus n / by two so that some part of it can be neglected right so it will be two so your mid is at this position now you will see okay the element that I’m looking for is uh let’s suppose is 50 and the element that I am currently at is 30 so obviously it will be never from this side there is no chance that we will be able to find 50 from this side that is the left hand side right first we will check okay is this 30 equal to 50 no so this will never be executed right then we will check is my item that is there is this 30 less than or greater than 50 if it is greater than 50 right if it is if the element that is if item that is 50 is greater than 30 right which is in this case our 50 that is the element that we are looking for the item item is this point is greater than 30 so there is no point that it will be on this side so we will skip or we will neglect this half portion of the array so that is why our new beginning is updated it will be new it will be middle index + one that is middle index was 2 + 1 that is this will be our new beginning so we have smartly skipped the half portion of the array so now let’s rub all of these things and now let’s see what happens in the next iteration now we’ll keep this thing in mind that we are not looping or we are not exceeding this limit that is beginning should be always less than it should be always less than or equal to okay so this this condition should be maintained and similarly we’ll again divide our array and then look for the same things right first we’ll look for the element then we’ll skip some part of it so this is the iterative approach for binary search right now let’s look at the recursive approach or recursive algorithm for the same so again beginning the ground rule will remain same we will always iterate or we will always cursively call binary search unless and until this beginning is less than end done now what happens we’ll again find middle index that is beginning plus end divided by two then we will look for the element these three steps will remain same even if you’re using recursive approaches now what happens in recursive recursion right we again call the function again and again that is what is recursion so in this uh in this entire tutorial we will be covering ing recursion as well but in the later part part of the course here you can get a good intuition or let me give you a brief intuition about how recursion works so let’s suppose this is your activation record every time when a recursion uh is there an activation record is called so let’s suppose you have these three statements let’s suppose in your algorithm you have statement P1 P2 and P3 right and at P2 you are calling the function again right you’re calling this function again so now what happens an activation record is called he will check okay is this statement executed yes so one will be executed is second executed yes so second is executed but at second you calling this function again so at that time again a new activation record is created now this third step is left behind right now this will be covered when we come back or return from this function call that we called here so let’s suppose this function was here now in here you are returning right this function called let’s suppose this is not less than beginning uh beginning is not less than end so this will be some somewhat this case is relatable right this is similar to what we are looking for right let’s Suppose there is some s similar situation where in beginning is not less than and at that time you will be returning from this right now once you have returned you will be then calling this function again but this time around for this and let’s suppose this time around you will you are calling this one is executed Step One is executed now again this activation is record as called this activation report is one and then again this is called right this is executed again this two is called again a new activation record will be created and these three three steps the step third will be still left for execution so now here you return right then it will go back to this step right and then once you are done with this now there are two positions or two possible scenarios where you can return either you are returning from this function just like we have executed this condition and we return right another is that once you are done with this entire activation record at that time you will also return okay so these are two scenarios now you have executed this there is no step to be executed it will return now this left this was left behind this will be executed now nothing is to be executed it will go to the caller which was this and finally it will go to the main method wherein we call this at the first place this function okay so this is how an activation record is created a stack is maintained okay even if uh you might be thinking we are not using any extra space but whenever recursion is there an extra space that is in the form of Stack that stack is maintained so you need to keep this thing in mind while you are playing around with space complexity at the time when you’re using recursion okay so now with that being said let’s clear our screen and let’s see how recursion is called here so again now recursively what we will doing if now we have this mid right and let’s take an example of an array 0 1 2 3 and 4 10 20 30 40 and 50 so middle index will be 0 + 4/ 2 that is 2 so this is our middle Index right so this is zero uh this is our beginning and this is our end right so now we are hit hit right so we again check the 50 that we are looking this is our item that we are looking for okay so is 50 and this is our middle index is 30 greater than 50 no it is not in this case so this will never be executed this is not executed as well right now what about this condition the else part now what we will be doing we’ll be skipping since this 30 is less than we’ll be skipping this part and we will focus on middle index + one which is nothing but this so this will be our new beginning and our process will start moving right so now again then the same thing will happen unless and until this condition is false okay so this is how your binary search works when you’re using recursion so now let’s try to understand binary search and let’s see its demonstration okay so we are looking for 20 and this is our array right 10 11 16 20 and 2 now this array is sorted right so we can apply binary search Okay since we can neglect some part of the array based on some conditions okay so now our beginning in the first iteration what is happening our beginning is zero our end is 4 and our middle is this element now what we be looking for is 16 equal to 20 no it is not but 16 is less than 20 so we will skip this part in the next iteration what happens we’ll be focusing on these three elements right that is 20 uh from we will be focusing on this part rather if we say we’ll focus on this part right focus on this part okay so now in the second iteration what will be happening our beginning is updated now our new beginning is this point our end will remain at its own position now we’ll find the middle index so it will be 4 + 3 that is 3 that is seven right and divided by two it is 3.5 right so since this will be truncated right the truncation will happen and the integer that is there the middle index will be three so this is our middle Index right so you can see middle index is three now is this element that we’re looking for yes so we’ll return the index so we found our element at index 3 and hence we return three because if You observe carefully it is returning if the element is found it is returning the index so this will be returned okay so this is how binary search works after knowing what is binary search we’ll implement the same in Python quickly switching up to the ID so the binary search has four different elements are important the first one is array the second one is which is the element to search for which is stored in X and L low and high why because every array in order to have the binary search will be divided into two parts right it will go accordingly if the key that means whatever the element you are searching is matching the middle element it will exit the binary search immediately if not it will try to proceed with the search of that particular element in halves of the array like it will divide the array into sub arrays the right and left part it it will try to see and search for that element accordingly as per the key element is right so mid is equal to low + High minus Low by 2 so this is the basic formula which will be using in order to split the binary array in order to have the search right so if array of middle that means middle element is equal to equal to that means it is equal to the key element which you’re searching then it will immediately give you the middle element as the searched element so if else what happens if the middle element is lesser than x what it will do it will go to the right side of an array if it is greater than x it will go to the left side of an array right so it will try to search in halves like sub arrays here if you could see the array that is 3 4 5 6 7 and 8 and N9 you have all these elements inside the array what you have to search is four so four is the second element immediately you can see but accordingly you have to search as per the binary search rules what it will do it will first cut this particular array into two halves by using this formula and then it will compare the key element which you are trying to search with the Elements which is already present in an array in order to find so let me quickly run this okay it is telling the element which you are searching is present in index number 1 that means it is having the count of array index not on the element so 0 1 2 3 and so on so four is present in index value one so this is how binary search will work work in Python now let’s talk about the time complexity of binary search now in the best case now what is the best case now let’s take an array and in that array 1 2 3 4 and five these are the elements now the best case is that not that if the element like we saw in linear search this element when we are looking to search for this same element at that time that was the uh best case scenario for linear search right but in this binary search the the best case scenario is when your middle index is at the at this location and you’re looking for you’re searching three in the entire array so at that time this will take a constant amount of time and this is the best case time complexity in that case okay now in average case what happens right if you talk about this algorithm let me just clear out my screen so it follows a paradigm of divide and conquer so let’s suppose you have eight elements first in the array it will be divided into four because these four either it can be on the left side or on the right side will be neglected and then we deal about these then we focus on these things okay these four elements again it will be divided into two and two then we will DCT two elements then one and one then again there will be one of the element can be neglected so there are one and one so we focus on this element so the entire operation will be done or entire searching will be done in three steps right now if you I take an example and if I do a log 8 to the base 2 what should be the value of this obviously when I do this this can be written as 2^ 3 right and this can be written as 3 into log 2 to the base 2 now this is 1 and now you get the answer as three so this three and this three are equal that means if I talk about the worst case time complexity of binary search it will be somewhere around log n as it Al as we saw in three steps we were able to find the element and the log n that means log 8 is the answer of that is also three so you get the point right so the worst case type complexity of binary search is log n and same goes for the average case wherein it will be somewhere around log n / by 2 neglecting log nide by 2 neglecting the constant terms it will be again or it boils down to bigo of login okay now let’s talk about space complexity of binary search now when you talk about space complexity right we only think of auxiliary memories or you can say that or you can say that what any extra memory that you guys have used since we did not use any extra memory that can be in the form of array or it can be in the form of stack or it can be in the form of Q or link list or even strings right since we never use these extra memories in our implementation so the space complexity of binary search is bigger of one that is it takes a constant amount of space what is insertion sort so the question is that what is sorting you might be thinking why do we need these sorting algorithms so if I told you that you have a bunch of students right you have bunch of students and they each have their role number they are not present in what in a numerical order or you can say they are not present in some order I want that order to be maintained let’s suppose you have 1 to 10 students in those bunch of students and each are having role numbers from this range from 1 to 10 now some of them are absent and some of them are some of the role numbers have left the school but the role numbers are not changed yet now what I told you I told you please sort them or arrange them in such a manner so that I can easily understand which role number is after which either in ascending or in descending order suppose one is there two is there then six is there then eight is there then 10 is there so rest of the RO numbers I can easily depict okay these are the ones which either are not there or are absent so in order to do so we have these sorting algorithms in picture and one of those sorting algorithm is insertion sort now it is the simplest easiest and a Brute Force sorting algorithm now what do what you mean by glute Force glute Force means straightforward right in a naive way it means straightforward that means you’re not keeping into uh you’re not considering any efficiency or you don’t cons you don’t care about time complexity or space complexity you just straight away sort it with the most simpler and naive approach okay in this root Force algorithm what happens that let’s suppose let me give you an example right obviously we can sort with the help of this insertion sort algorithm you can obviously sort either in ascending or in descending model right uh let’s take one example we all know about the card game right wherein you have a bunch of Cards Right suppose you have a single card that is in your hand right and you have bunch of cards available on the table now you start picking those cards one by one obviously the one that is in your head is sorted because if I told you to sort a number one obviously there is only one element in the array or anything right in the link list I told you to sort it but if you’re having only one element that is itself sorted right you don’t need to sort that similarly what happens now this card is in your hand right it’s just like playing cars right now you have this one card in your hand and it is obviously sorted now what you will do in the next turn you start picking up one by one from these set of cards that are available on the table now let’s suppose this is zero okay I’m considering these numerical values so that because so that you can understand and you can just connect the dots right so what happens you have the zero and now you start comparing it now we are considering the scenario where you are trying to sort in ascending order okay so now let’s try to erase these things so that it’s easier for you to understand things okay so now you have these two elements right and now we are considering the case wherein you are trying to sort in asset name so you check okay if 0 is less than one yes it is now you swap them okay now you have zero and one now these are the two cards that are present and both of these are sorted so now insertion sort Works in such a manner that you will always have two parts right one is the sorted part obviously which is in your hand and one is the unsorted part which is on the on this deck right so similarly you can you will start picking uh elements or you can start picking these cards one by one and keep sorting them okay now this is one simple scenario wherein you can apply what insertion sort right this is the most simpler way one can explain or one can understand you this insertion sort algorithm now it is simple right now you start picking these elements and you keep sorting them and the at the end when all of these elements are exhausted you will get your sorted areay now let’s try to understand insertion sort algorithm so in this algorithm what happens obviously now we know that we will have two parts right one is the sorted part and another is the unsorted part right so obviously the one element that is present in your hand or the element or the card that is in your hand and there’s only one element there right the one element in your hand obviously that is sorted right so we will not consider that first element and we will start our iteration from the second element right now we understand why we are doing this that we are starting from 2 to n minus one or 2 to n depending upon the array that we are starting from either we can start from zero index or we can start from one Index right so we always start from element number two right and then what we will do we’ll just store this value inside temporary variable and then we will check if that element is less than the element that we have in this sorted part if that is the case then we will shift their positions right and we will get both now we will have two elements in the picture that is 0o and one and both of these will be sorted in ascending order and then what we will do we’ll consider the rest of the cases that is starting from three to so on to n okay now you might be thinking okay how does this thing happen let me take an example and let me show you how let’s consider this array that we have over here that is index zero these are all the indexes that we have and this is our array that is 23 10 16 11 and 20 so in the first step we are making now we are making partitions now this is our sorted part that is the first element and this is our unsorted part now what we will do in the first iteration this is our iteration number one because this is the case wherein we will start moving from second index that is first index if we consider from zero right so we consider from second element and so on to end right so now in the first iteration what we will do we’ll compare these two values okay let me just erase everything so that it’s easier for you guys so now we will compare these two now obviously 10 is less than 23 what we will do we’ll shift their positions now this is your sorted part and this is your unsorted part again we will do the same thing right so in second iteration what we will do here comes 16 now what we will do we’ll compare it first with 23 okay we know now okay 16 is less than 23 so now what we will do we’ll swap their positions so this is 16 and this is 23 now what will happen now 16 will be compared with 10 obviously it is not less than 10 so it will remain as its at its own position that is its new position at index one right so this is the second iteration and after second iteration this will be your sorted part as you can see that I have bolded this text right bolded the borders of these two these two elements and bolded the same for these three elements because this is the sorted part that we have over here and this is the unsorted part now what will happen in the third iteration that it will check for this number that was there it is 11 so for 11 what we will do we’ll compare it with what read this thing now we’ll take 11 into consideration and now we’ll check it we’ll swap them then 11 is here 23 is here we’ll check them we’ll swap them 16 is here 11 is here we check them so since 10 is less than 11 so nothing will happen so in the third iteration what will happen we will have 10 11 16 and 23 these are all sorted and we are only left with one element which is unsorted right now in the final iteration what will happen that 23 now this 20 will be at its original position that is there and rest of the elements will be sorted now since we have exhausted all the elements all the elements have been exhausted and we add the final step that is in iteration four we will have this array that is sorted after learning what is insertion sort let’s quickly implement the same in Python language so I’m using Google collab whether it is easy for everybody to access Google collab so need not install anything it’s right available in the online so let’s quickly switch to that Google collab ID for python so here you can find insertion sort the name for the file in Python extension so with that we already have this particular program which is easy for me to explain to you so here so we are considering a function called insertion sort right so the function is called whenever the data is being passed in order to sort the elements inside the data in ascending order right so in order to do that we have to write a proper function accordingly as inent s will work so how does that work you have already learned about it so in order to implement you have to use a for Loop so for Loop has a range so it will be always checking for the elements in inside the array one by one for comparison with the key element right so whenever it is finding the key element it will which is greater than the key element or which is lesser than it will swap accordingly right so we are using Y Loop in order to do that same work so we are swapping from the current position where it is being found which is greater or which is smaller accordingly we’ll swap it right so then we have the data which is been given here so the data is present that is 52 178 so what happens in this particular data is when it passes through this function every element will be sorted with the help of incision sort function which we have written here so first it will compare the elements and it will try to sort in ascending order say for example if you want to do descending order then you have to change just one single element that is this key should be greater than array element that’s about it nothing else no change so after that incision sort uh is the function is having the data which is present here so all the functions will be completed then we’ll be printing the final output how do you print once the function has completed sorting immediately it will be stored in the variable data itself so that particular data is being printed after sort element will be viewed right so this is just a print statement sorted array in ascending order so if you’re doing for descending you can make it a sorted array in descending order so let’s quickly check how this output look like so here you have sorted array in ascending order so that is 1 2 5 7 8 right from smaller to the higher number so let’s quickly make a small change here so that it will give us the descending in order let’s try to work on it right if you could see here key is greater than array element then you will be getting the descending order that is 875 2 1 so you can change it likewise okay I didn’t change the printing statement so I’m just changing descending order right run the same that’s been declared so this is how insertion sort will work in Python and the code if you could see it is very small and quickly it is eliminating all the variable initializations we make anything and everything you just want to have the function pass the data get it sorted and the output is done so this is all about insertion sort in Python now let’s talk about insertions or time complexity so in the worst case when all the elements are in fard manner and we need to sort them one by one so obviously we are talking about first the outer loop which runs from one to n and then the inner loop which runs backwards and in the last first we consider in the first iteration we only consider the zero element then as we move along it will be running from n to zero element right we will be considering the whole n elements so in that case the time complexity the worst case will be order of n squares because we have two nested Loops that is one is for Loop and inside that for Loop we have that we have that y Loop right so this is the in the worst case and it happens also in the average case where some part or the sorted part is already there and it is let’s suppose we have 5 4 5 6 7 8 and then we have the unsorted part so half of the elements are sorted and half of the elements are not sorted so it will be n² by 2 so we not considering the case where we talk about constants and we are negotiating the constants and in that case the average time complexity will be n² right but the most important thing that is there in this time complexity is the best case that means when your elements that is 5 6 7 8 9 and 10 when the elements in the array are already sorted what happens in this case if You observe the war loop that runs from 1 to n will be always there n is always there the time complexity the Big O notation Big O of n will be always there but in this y Loop wherein we were checking for if J is greater than or equal to zero and if AR R of J is less than 10 right in that case this will never be executed because this AR of G will always be less than will always be greater than 10 why because we talking about this element and we’re checking if this 6 is less than five no it is not if this s is less than six no is it it is not so this condition will always be false for all the elements so in nutshell we are just checking these steps only once in every iteration so that is for the reason that the whole time complexity in the best case will be bigger of N and not Big O of n² in the best case okay now let’s talk about insertion sorts space complexity if you have observed in algorithms and in implementation we never talked about any auxiliary memory right we were not using any extra space either in the form of array link list stack Q or anything right so that is for the reason the space complexity of insertion sort is B of one that is constant amount of space now let’s talk about insertion sort analysis wherein we will be analyzing comparisons number of swaps stable or unstable in place or out place so first let’s talk about number of comparisons required in this we will talk about two scenarios wherein we will talk about worst case and average case in worst case the number of comparisons required is n² by 2 now if you talk about average each case scenario It Is n² by 4 which is twice as much as this right it is two times if you talk about number of swaps that are required in insertion sort in again we will talk about two scenarios average and worst case in average case it is n² by 8 and in worst case it is n² by four these are the number of swaps required and if you want to check those if these statements hold or not if these equations hold or not you can always take an example wherein you will be considering both the cases even as well as odd so take an example and run through it now if you talk about stability of insertion sort it is a stable algorithm what do you mean by stable so if you have an array which contains 1 3 1 d and five in this array the relative position of these two ones that is this one and this one let me change a color and let me show you the relative positions of this one and this one will remain intact what you mean by this thing that whenever you are sorting it you can sort it in two different ways right this is also sorted and this is also sorted that means you can either have one 1 D3 and 5 or you can have 1 d one and 3 and five this is obviously that this this number is repeated but this is the first number this this occurred here the first time and here it is the second occurrence now you want to keep their relative positions intact right so both of these are sorted right but if you talk about stability this is known as stable and this is unstable okay now insertion sort whenever you are trying to implement insertion sort it is stable that means the relative positions of both these ones will be intact okay so if someone asks you if insertion sort is stable or not you will say yes why because the relative positions of the number that are of the numbers that are repeated remains intact now what about this in place or outplaced since we are not using any auxiliary memory right we didn’t use any stack Q Link list or array that is the reason that whenever you are not using any extra memory it is supposed to be in place algorithm so if an algorithm is sorted within the array that was there earlier that means you’re not using any extra space that algorithm is known as in place algorithm which is evident now in insertion sort as we are not using any extra memory so insertion sort is an in place algorithm now let’s look at the example wherein we will implement insertion sort if you can see we have this example over here wherein we have 6 5 3 2 8 10 9 and 11 and we have been given this K what this K signif signifies that the maximum swaps or comparisons needed for this three either on the left side or on the right side right the number of positions that it this three needs to get to its original position is three so this is a question that is known as nearly sorted array or k sorted array we do not need to sort all the elements in the array but we are specifically looking for those Elements which are not at its original position and if we want to get them to their original position the maximum comparisons or swaps that we require is three so if you see this three the original position of this three is this five that means in the sorted array it will be here similarly if you talk about this two the number of swaps that it should do is 1 2 and then it will it will be at its original position or you can say that 1 2 and three so max it can go to three positions okay so similarly it will be the same for all the elements so at most three okay and at least it can be that it will have it doesn’t move need to move at any location that it will have its own original position just like in 11 you see 11 is at its own position in the original array as well as in the swapped array so at most you have three positions let’s try to understand what is sorting and why do we require sorting so sorting is a mechanism wherein we will be sorting or arranging our data either in ascending order or in descending order right so let’s suppose you have a student you have 10 students and all those students have role numbers allocated from 1 to 100 and you want to know which role numbers are present and which are absent and which have left the college or school right so in that scenario you can easily Implement sorting right and you can understand when you have that sorting uh arrangement in place you can easily detect which elements or which students are absent or not right so here in you can use sorting so in this tutorial we are going to understand quck sort algorithm it is one of the most widely used algorithm it follows a paradigm of divide and conquer what do you mean by divide and conquer basically we will be dividing our array in such a way that every time we will be dividing let’s suppose this is an array and now we will be dividing into two then further we will divide it into two then further we will divide it into two and so on right so we’ll see in the algorithm part how we can implement this divide and conquer Paradigm and in this tutorial we will be implementing this quick sort using recursion we’ll see how we will recursively call those functions based on some pivot element now in this recursive call we’ll choose a pivot element let’s suppose you have have this array and we were choosing this element as pivot obviously you can choose any element as pivot right so it can be first element it can be uh last element it can be any random element but once we have chosen those that pivot now what we will do in each iteration right in quick sort what happens in each iteration this pivot will have its original position that means this will be the position in the original array as well let’s suppose this is our pivot now this pivot will have its original position after one iteration after that iteration is over and all the elements that are less than this pivot are on the left hand side and all the elements that are greater than will be on the right hand side now then we will be choosing another pivot now what are those pivots we’ll see in the algorithm uh more extensively what are how we can choose that pivot now let’s suppose we choosen we chose this pivot and this pivot is here and and after the second iteration what happens this pivot this will be our next pivot and this will be our next pivot now we will be having two pivots so this is how we indued that we are implementing divide and conquer approach okay with each step our problem gets reduced to two which leads to Quick sorting quick sort right or quick sorting algorithm okay so now we’ll be dealing with this subar and we’ll be dealing with this sub AR and now we’ll be implementing the same procedure on this subarray that means this is the pivot and this is a pivot right now let’s try to understand the algorithm of Quake sort so now we have this first of the method that is there that is known as Quake sort in which we will be calling this quick sort recursively again and again but first time around what happens we will check okay now we have this array always we will check beginning should be less than end because that way we can keep keep the track of things that okay this is the part that is already sorted and this is the part that is unsorted right and now we will be checking and after checking that we will be calling this method we will see what this method is we’ll see the algorithm and we will see how this partition happens and we will get the index of the let’s suppose we pick this element as pivot and after partition what happens this pivot has its original position at index 3 right and that will be returned and that will will be contained in this pivot Index right and now what happens now we know that this is its original position in the original array wherein we will get the sorted array this will be its original position that means this element let’s suppose is 8 8 will be at index 3 and this will have its original position after each iteration now first time around what happens this partition is called next time around what happens this quick sort algorithm is called again recursively first time on the left left hand side that means this portion now in this portion this will be your pivot okay you see beginning is uh we are sending the arguments as beginning and pivot index minus one that means we are not including this element because this has been already sorted we are not including this element and we are calling this function on this subarray again and this time around this will be our pivit and same thing happens similarly when we are done on the left with the left hand side now we’ll be moving to the right hand side that is we will be implementing it on pivot index + one that means this element from this element that is there to the end of the array and this time around this will be our pivot okay now with that being said this is what happens when we are implementing quick s but now what about this partition method let’s see how that happens so in Partition what happens we will be setting up the PIV PIV element that is setting up the element which is our pivot obviously you can choose any element but in this tutorial what I’m going to use and what you should try first that we should try to pick pivot as the last element obviously you can pick any element and it’s time complexity depends on which pivot you will be choosing we’ll see that in the time complexity part okay now we have set this pivot as the last element and now what we are doing we are saying that okay the pivot Index this uh this step refers to what is this is the index from let’s suppose this is the pivot index and what happens this will be our pivot okay what happens this pivot index maintains that order okay from this index from this index everything on the left hand side is less than the pivate and everything on the right hand side is greater than the pivate so we’ll see when we we will see an example and there in I will show you how this pivit index is very important okay now what we will do obviously at start it is at this position that means we are not we have no such scenario wherein we have some elements that are less than pivot and we have some elements that are greater than pivot okay so now let’s suppose this is our array and this is our pivot right and this is our P index that is the index PIV index and it is minus one right now okay now these two steps are done now what happens in the third step now we will iterate from beginning that is this point and we will check if any element is less than pivot if that is the case then what we will do we will increment this and swap those elements that is the area the first element and the index that is present at that means now if you see this step now we have incremented it first right now let’s suppose if any element that is less than pivot we first increment the pivot index that means that pivot index will be here and we will be swapping with a r of I and a r of I is also at this location so this element will be swapped with itself now you might be thinking okay so why we are doing this right why we are doing why we are swapping this with its own uh with its own position you won’t get the intuition in this step but in the next step you will definitely get the intuition now let’s suppose this is the thing that happens in the for Loop now let’s try to reiterate this now if an element is not less than pivot let’s Suppose there was here we had five and here we had three so it was less than and we swapped it very yourself now let’s suppose we have this element six and it is not less than P right and what happens over here so we will not be we will not execute this if block right and then we’ll have this AR of I now I will be here now I will be incremented and this time around we have two and P index is still here right now this time around it is less than two right and now what we will do we’ll increment first the P index it will be pointing here here and then what we will do we’ll swap swap these two elements right these two elements will be swap so now you have two here and you have six over here right so you see this is the reason why we have this pivot index at in place and why we are swapping them so in the first step it was uh it was that it happened due to the fact that the element was less than pivot and if the element would wouldn’t have been less than the pivot then we have incremented the I pointer and P index would have remained on minus one now finally what happens now when Once the entire iteration is complete and let’s suppose we have eight over here and we have then 10 now once the iteration is completed now what we will do we’ll swap these two elements that means five and six will be swapped and we have five here we have six here we have eight here we have 10 here and we have three here and we have two here so you see after one iteration all the elements that are less than pivot will be on the left hand side and all the elements that are greater than will be on the right hand side and finally we will uh return pivot index uh that is p index + one that means we’ll be returning this index so that this element is not considered or will not participate in any further iterations or any further recursive calls because you see if You observe carefully that we we’re sending pivot index minus one that is without five all the elements on on the left hand side and plus one that means without this index all the elements on the right hand side okay this is how partition works now you might be confused a little bit now let’s try to demonstrate this with the help of example so you see we have an example over here right we have 5 10 9 6 and 7 these are the elements in the array and we have this pivot here the last element we have chosen last element to be the pivot and after that what we are doing we have this end pointer and we have this beginning pointer also we have that pivot index which will be somewhere around here right that pivot index which will be minus one now this seven will be checked okay is 5 less than 7 yes five is less than 7 so it will be swapped with itself and pivot index will be incremented first and then swapped with it itself now pivate index will be here next time around our a our I pointer will be here first it will be here then what it will be incremented now we’ll be again we’ll again check okay S7 is 7 less than 10 no it is not so our I will be incremented I will be now here at this position right now again it will be checked no again it will be checked yes so now what happens 7 and six uh the six will be replaced with what 10 so you have this six in here obviously PIV index will be incremented first and then we have the six over here and it will be swapped with 10 so 10 will be here right done and finally when we are the end once the entire iteration this is the step one once the entire iteration is completed we have 5 six and then seven will be the last swapping that we did the last swap that we did If You observe here carefully this swap that we are doing this is the the one that is responsible for swapping this seven with the pivot index that is Pivot index + one that is this location and we have this seven over here and it will be replaced with nine so that is why we have nine over here and 10 was here and this is the array after first iteration now you might be thinking okay now this element is fixed now we will not never talk about this element because this has its original position in the sorted array as well now what we will be dealing with we’ll be dealing with this left part and we be dealing with this right part so now what happens in this part right and what happens in this part you see now we have new this is our beginning and this is our pivot because this is the last element that we will be picking and this is our end similarly this is our beginning this will be our PIV the last element in the in this sub array and the end will be here now we’ll be again doing the same step and this time around we’ll be checking okay Pate is less than no nothing will happen and then we will be we will be swapping this thing with itself right and now once this entire subar is completed we’ll not go any further because this time around beginning is not less than end both elements are at zero and 0 is not less than zero and now if You observe carefully this is the condition that we were setting at the start of the of the function that is the quick sort function and we be checking we were checking if beginning is less than end right so this is the importance of that similarly the same thing will happen happen over from this this side and again beginning will not be less than index end part and we will not go any further so after two iterations our entire array is sorted right so this is the step one after step two our entire are is sorted after learning what is quick sort let’s quickly implement the same in Python so here we are using python ID that is Google collab one of the ID mean to say and then we’ll implement that particular program there so let’s quickly hop into the ID now so here is the program for quick sort in Python so let’s understand how this program works right the first part we need partition to be made right any array in quick sort to be broken into two halves and we will start sorting in that particular different pieces so partition positioning will be done with the the help of array low and high variables so at the rightmost always we’ll consider the element as pivote element rightmost element of the array is a peot element that is the consideration so in order to do that we’ll use p is equal to a RR of H right so then point of for greater element so whatever the element is greater in order to compare we’ll be using this point in order to Traverse from all the elements inside an array keeping one PE element in consideration with comparing with that particular element we use this for loop system right if smaller than element is present which is smaller than PE we’ll use this IAL to I + 1 and immediately we’ll swap the element in the position which is there in I with J right that will be done with the help of a r of I and J is equal to J and I we’ll exchange change if you could see here I J is being changed to J and I so we’re exchanging the elements if it is smaller than the P element then swap p with I if it’s greater than P right if any element which is greater than po element wherever the I is pointing to that element will be swapped between the element and PE right in order to do that we’ll be using this particular condition then we’ll get back to the initial position where we started the partitioning right where we broke that array into two parts the partitioning is done there we’ll go back and we’ll try to start initial position then the quick sort function will come right so here in quick sort again we need three different elements AR low and high if low is less than high that is smaller element than peot is present it will all go towards the left side if there is greater element than prior is present it will go to right side so partitioning is done accordingly so this is a recursive call which we follow for quick sort right we’ll be having again array low P minus one Pi is p minus one so again for the right of the P we have a recursive call function which is declared here once all these things are done we have to give data in in order to sort something right we are here presently concentrating on sorting the array which is given in the ascending order right so the data set here is mentioned and it is been assigned as d right so the set is been assigned as D 9A 8 7 2 10 20 and 1 so these are the elements which we are trying to sort right we are printing the unsorted array that means however the input is present here that is printed as it is unsorted is array is equal to so and so which is already there which we are not performing any functions then we have print D that means immediately it will print then size is equal to length of D we’ll consider in order to print while we are printing right we have to print element wise so again we have to print it nine first8 next seven next and then two followed by up to one so after that is done we will send this particular data raw data which is unsorted data to the function called quick sort which we have created here right so that is been sent once that is sent it will follow all the procedures which is mentioned here all the functions will be passed with the data and then finally we will print sorted array in ascending order which is uh sorted using quick sort right so let’s quickly run this program and check out out what is the output so it will take some time in order to take the output so let’s quickly see okay so that is what I mentioned unsorted array is nothing but the array which is been given by the user and sorted array is also given after performing all the functions assigned for the quick sort so if you could see it is in ascending order starting from one and ending at 20 so this is all about quick sort in Python now let’s try to understand the time complexity of quick sort algorithm in quick sort algorithm we have now seen that partitioning of elements takes place and we are partitioning all the elements that means all the N elements if there are eight elements all the eight elements will be we iterate through all the eight elements right so partitioning them takes end time that is order of end time and then quick sort problem divides it into the factor by the factor of two right every time we are divid dividing it by two so the entire process or the time complexity of quick sort in best case and in aage case takes order of end time that is bigo of log n and same thing happens when we are talking about the average case as well but why this is n s in worst case that is the question right so let me clear it out so the question is that why this thing happens if you are picking either the smallest element in the array or the largest element in the array as pivot in that case you are traversing through all the elements again that means this n is already there for partitioning them that means you will be iterating through the array but the extra n and that means inside that n you’re again traversing through all the elements and swapping them because you have picked your pivot in worst case you can either pick it is smallest or the largest element in the array in both these cases you are you will be swapping all those elements with itself that this element will be swapped right this is this is the largest element right this is let’s suppose this is eight so nothing will happen right so these are this is smallest then it will be swapped with itself this is smallest this is this will be swapped with itself this will be swapped with itself this will be swapped with itself so all the elements will be swapped and finally this element will have its original position at the end right so this thing will happen if you are picking your pivot as the smallest element or as the largest element in the array okay so in this in these two cases this is not the case you are picking your pivots as random you picking your pivots randomly okay in nutshell when you are picking your element that is your pivot element as smallest or or the largest element in the array in that case that will be your worst case time complexity and it will be B of n² now let’s talk about the space complexity of quak sort now you might be thinking okay we are not using any extra space right we are not using any auxiliary memory like in the form of array stack Q Link list or anything right but for calling this function that is the quick sort function we are using recursion right we are calling this quick sort again and again right to quick sort calls are there for maintaining the call stack we require order of n space that is the time complexity will be big off and when we are using this approach and in the worst case this will be the scenario that all the elements will be on the call stack okay so in worse case the space complexity will be bigger of and but if we modify this approach of of storing the elements and calling the call stack and maintaining the call stack we can reduce it to beo of login now let’s try to analyze quick sort algorithm let’s first try to understand the stability so let’s suppose if you have this array 1 3 1 dash and 4 now an algorithm is said to be stable if both these one and this one both these in the sorted array will maintain their relative positions now you have the sorted array right and both one this one and this one are maintaining their relative positions which were ear in the unsorted area right so if that thing is maintained right if that thing is maintained the algorithm is stable else it is not stable obviously you can have another way with which this is also sorted but this is not a stable this is unstable algorithm and if you are sorting in such a manner and you have these things placed this algorithm is unstable so if you talk about quick sort algorithm quick sort algorithm is an unstable algorithm although so we can do some modifications and we can stabilize it or we can add we can make this algorithm as stable but as of now if you talk about quick sort algorithm it is an unstable algorithm what about in place and out place since we are not using any auxiliary memory right we are not using any extra space explicitly right in the form of array or link list or stack right or even CU we’re not using any extra memory right so this algorithm quick sort algorithm although we are maintaining a call stack wherein we are you maintaining a call stack and we have a space complexity of big of and but since we are not explicitly mentioning this these uh uh these auxiliary memories this algorithm is an in place algorithm and these are the two analysis that can be done on quick sort so in nutshell if you talk about quick sort right it is unstable algorithm what is merge sort if we talk about merge sort let’s try to understand first sorting sorting is a mechanism of giving order to your values right so let’s suppose you have some values random values 10 30 and then you have 5 2 1 and so on right you have these values and now you want to maintain some order so in order to to visualize this data let’s suppose you want to see uh the ascending order of it or the descending order of it that is what you mean by sorting so let’s suppose you have a class and in that class you have several Ro numbers and some of the RO numbers are not present and then you want to sort those role numbers uh in terms of ascending or descending order that is when you require sorting so this is the basic intuition behind sorting trying to give order to some kind of values or some kind of a data set right so in this particular tutorial we are going to talk about M sort so M sort is a classical sorting algorithm in this sorting every time your problem is divided into sub problems so that your problem set is reduced and then you will be focusing on that sub problem similarly every time when you’re dividing your sub problems you will keep on dividing it unless and until there is only one element left right if you compare it with simpler sorting algorithms like bubble is there insertion is there selection is there Quake is there when you talk about its time complexity as compared to these algorithms this is very much efficient now it follows a paradigm of divide and conquer what does this mean this means that first you keep on dividing your sub problems and then you will conquer those problems and then you will combine those things okay so here in we will see when you are trying to divide your sub problems and then when you have your problem set and those problem sets are conquered that means those problems are further when you talk about in this example those sub problems are sorted in this case and then you have conquered them and then you will combine them that is your merge phase wherein you will be combining your problem again and then for forming again a single sub problem so every time you will be dividing that sub problem you will be conquering it and combining it so this is how this divide and conquer Paradigm works so basically when you’re dealing with merge sort you are focusing on two functions that is your merge function and your merge sort function so now let’s talk about this divide that means you’re dividing your sub problems which continues unless and until there is only one element left because one element in itself is sorted right now this is your divide phase what about conquer basically you are conquering those idual sets and then merging those two sub problems into a single problem and finally you will be doing it on each step and finally you have your original array which is sorted now let’s talk about M sortor so first let’s talk about merge sort method so in this method what we are doing we are dividing our array into further sub arrays how we are going to do that we have basically if we have this array right and this array let’s suppose this contains eight elements right this is our array and let’s suppose this is our left pointer and this is our right pointer and now what we are doing we are dividing it so we need some kind of a in iterator wherein we will store the sum of and we try to calculate the mid value how would you do it in simple words we calculate left plus right ID by two that’s it right so we have this Division and then we will divide this part because we are calling this function again right on the left hand side so this is going to call on this side that is we will be talking about now only three elements so this is let’s or let’s take four elements on this side and four elements on this side so our mid will be 3.5 so we’ll be talking about elements from 0 to three so we will be talking about four elements and then further these two steps are remained why because we are implementing this in recursive fashion so now let’s suppose this is our first function call and you have these three steps one let’s name it one 2 and three okay so in the first function call 1 2 and three so this is the first function call and in this we’ll call in again this is the second function call and we are calling again these two remain right and we are calling again 1 2 and three right so in this case again we are calling it on these four elements and it will be divided into further two elements 0 and one right and in this case again this this is called this will be called on this these two elements right here we are talking about only two elements so this is a third function called and again we will be dividing it these two and three steps are still remaining so in the fourth step what we are dealing with only single element right and in this case we’re talking about only this element right and further we will not be able to divide it and in that case our left is not greater than our left will be greater than or equal to right and in this case it will will be equal to so we will return so now it will be returned right and then whatever was the left over right now we talk about this single element the other element that was left behind we’ll talk about that so again that will be divided into one right and again this will the second option I’ll just erase it because it looks a little bit messy so now what happens let me just put it again in red so here we called this fourth time and this time it returned right so in this case now we will be on the second step now again it will be called on that single element and again left is not great will be greater than or equal to in this case it will be equal to and then we return so again we are returning so we have these two elements one and one that means not one and one element but there is only a single element in both these arrays right why I’m saying that in these two arrays we will be we will check when we talk about merge okay we’ll see how that is implemented okay so now these these two function calls are done and then we deal with merge now before going into the merge let me show you a demonstration of how things look so you have these elements and here you have how many elements you have five elements right and now you’re dividing it into three and two now this is your first step the second step will be this so now will you go ahead and create this as your third step that means you will move on to this no because we saw unless and until left is not there is no left left right we will not go to the right so this is your second step then this will be your third step this will be your fourth step now you will move on to your fifth step right and now once you don’t have anything on the left nothing on the right then what happens will this be your sixth step no your sixth step will be merge so let’s move on to merge now so in the merge function if you see the algorithm for that is simple that you create two sub arrays that is the one is your left array and another is your right sub array now in this you have obviously in the last case if you have seen we have a single element here and a single element here now once we have deduced out the length of these aray aray s what should be the length of these sub arrays and we have declared the length or declared these arrays and then we have initialized these arrays once these three steps are done with then what we are going to do we are going to create three iterators i j and k and those iterators deal with I iterator will deal with left array J iterator deal with right array and K with the original array which helps us to insert the elements so once we we have everything in place what we are going to do the next step is comparing the values right if this element that is the element in left array is less than the element in the right array we are going to insert that in the original array so now let suppose you have 10 here and 23 here so 10 is less than so we are going to insert this and we increment the K pointer and now also our I pointer pointer will be incremented it was earlier it was zero and it will go to one right and now what happens now our our I is pointing to one and our length is also one so now that in that case when one of the array is exhausted the next array whichever is the left whichever is Left Right it can be either the left array or the right array those elements will be directly inserted in the original array because we know for the fact that both of these left as well as right arrays will be sorted in itself okay so let’s see what is the next step in the demonstration so here we we had our steps right and this will be the sixth step wherein we are going to merge this thing now will we will this be your seventh step no your seventh step will not be this your seventh step will be here this will be your seventh step now six step is done now you will be dividing it and you will be creating all those arrays now once you have your right array and there is no right because this left is already done and now you had your right left now this is also done now your eighth step will be this that you will be merging it this will be your eighth step then you will be merging it now will this be your ninth step no you have your this array that is your left array in place but what about the right array is this in place no it is not so now let’s try to calculate that now what will be your ninth step this left is done this is your ninth step then what will be your 10th step this is your 10th step will now there there’s no left right now we’ll move on to this right so this is your 11th step because this is the right side of it right and now when you don’t have anything on the right now you will be merging these two steps and this will be your 12th step which is over here so this is your 12th step that means you will be merging these two and the final sorted array is this array and it you will get this array in the 12th step so you see 10 11 16 20 and 30 now if You observe carefully you have these individual arrays one and one so now while you are merging them you are also sorting them so the left array is sorted and same thing happens on the right hand side as well if you see three and four these two elements are sorted in this left array so this is the reason in the right array right not the left array so now when you’re are merging them you will get again an array which is sorted in itself you see 10 16 and 23 so if one of the array is exhausted the next array elements can be directly inserted in your original array let me erase this and you see you have your left array which is sorted and then you’re merging it with the right array which is also sorted now if one of the arrays is exhausted the next array either it can be left array or the right array the elements from that array can be directly inserted in the original array because we know the elements itself if in either of the arrays either the left or the right are sorted okay so this is how you execute your merge function so here is the program for mer sort in Python so how does this mer sort work generally one single array will be broken into two different pieces again those two different sub arrays will be broken into Sub sub arrays so after that whatever the answers we get at the last will be combined together in order to finish the Sorting of that particular array so we are merging all the answers which we got from the sub arrays to make a final result so quickly let’s see what do we do in order to have a merge s in Python so first we want an array which is being passed through this merge sort function so what happens inside this function first the length of the array is been calculated once that is calculated it is been divided by two so it gets left and right parts of an array right so after sorting the array into two different halves we have mer sorting left side of an array M sorting right side of an array right then we’ll perform the V operation um here with the help of the looping systems so we’ll first try to check out whether we have the right array less than the length of an array of the left and then again left array it is less than length of the right so we’ll try to merge and we try to solve the elements then and there itself so later we’ll go back to the left and right parts of while loop here we have length of an array towards the left side we are checking whether it is lesser than or greater than and accordingly we are deciding where we have to merge the answers what we have got from the subar right so then we’ll always have a printing option of this particular arrays we’ll do that in the last before that in order to merge all the answers we have got from all the sub arays we’ll be using for Loop here right so all the array answers will be submerged and we’ll get the final sorted array which is of uh so many elements which is there in the uh input given by the user say for example five different elements were there in an array so after combining all the sub arrays answers we’ll get the five sorted ascending order elements in the array by using M sort so let’s quickly have a look at it how does this particular M sort will work so uh this is set of an array with eight different elements inside that which is not sorted we have to sort that once this array is been passed through the merge sorting function it will perform all the operations finally it will merge all the sorted arrays and it will display in the print list right so let’s quickly run this program and check out even though if if it is we are mentioning the words array but we are using list here in Python in order to store it right so this is the sorted array which we get so here we could see we don’t have a sorted array but here it is sorted in ascending order that is smallest to the highest so so this is all about the M sort next we move to python form machine learning this is where you will learn to manipulate analyze and visualize data using powerful libraries like numai pandas matplot lib and cbon unlocking insights from complex data sets now we’ll start off with this Library called as numpy which stands for numerical Python and as it is stated over here it is the core library for numeric and scientific Computing so whatever numeric or scientific calculations you have to perform numpy should be a go-to language and this Library called as numpy consists of multi-dimensional array objects and a collection of routines for processing these arrays so let’s go ahead and create our first numpy array so you can have a single dimensional numpy array or a multi-dimensional numpy array now we’d have to start off by importing this Library so to import numai we’ll type in import numpy as NP this NP which you see over here is known as the alas so we are importing the library numai with this alas NP now this numpy library has a lot of methods and one method is called the array method and with the help of this we will be able to create the numpy array all we have to do is type in np. Array and inside this I am passing in the list of value starting from 10 going on till 40 and I’ll store it in this object called as N1 and when I print it out I get the result 10 20 30 40 similarly I go ahead and create a multi-dimensional array so here we are passing in a list of lists so here we had a single list here we are passing in a list of list so as you see we have a list over here and inside this we have two more lists the first list comprises of the elements 10 20 30 and 40 and the second list comprises of the elements 40 30 20 and 10 and when I print out this is how I get this multi-dimensional array the first list is present in the first row and the second list is present in the second row now let me go to jupyter notebook and Implement these two let me just add a comment over here I’ll name it as numai now I’d have to import the numai library so I’ll have import numpy as NP and let’s just wait till this library is loaded now now that this is loaded I can go ahead and create the numpy array so for this I’ll have to use np. array and inside this I’ll be passing in a list of values so I’ll pass in 10 20 30 and 40 and I’m storing it in this object called as N1 now let me print out N1 over here and as you guys see I have successfully created this numpy array which over here the values are 10 20 30 and 40 and just to be sure I’ll go ahead and check the the type of this numpy array so I’ll have type inside this I’ll pass in N1 and as you guys see we get the result numpy do ND array so ND array stands for n dimensional array now we’ll go ahead and create a multi-dimensional array over here so to create a multi-dimensional array we’d have to pass in a list of lists inside this np. array method so I’ll have this outer list inside this I will create two lists so the first list let’s say comprises of the elements 1 2 3 and 4 and the second list comprises of the elements 4 3 2 and 1 now I’ll just print out N2 over here and I have created this multi-dimensional array where all of the elements from the first list are present in the first row and all of the elements from the second list are present in the second row now that we have created these numpy arrays we’ll now see how to create or how to initialize a numpy array with different ways so now let’s say if I want to initialize a numpy array with only zeros then we have a method called as zeros it’s very intuitive isn’t it so here we are importing the numpy array then we are using np. Z and it takes in two parameters so these two parameters basically indicate the dimensions of the numpy array so if I want to create a 1 cross2 numpy array where all of the values are zeros then I can just go ahead and type in np. Z and inside this I’ll pass in the dimensions which is 1A 2 and I get a 1 cross two numpy array where the values are only zeros similarly over here I am creating a five cross 5 numpy array where all of the values are zeros so I’ll just use np. Z and inside this I’ll pass in 5 comma 5 now let me go ahead and implement this in jupyter Notebook here I’ll add the comment NP do Z now I’ll go ahead and uh let me just create create this in N1 and inside N1 I’d have to use the np. Z’s method and over here I’d have to pass in the dimensions so the dimensions would be 1 comma 2 and let me go ahead and print out n one over here so as you guys see I have successfully created this numpy array now if I want maybe a numpy array with a different dimension so the method would be the same I’ll have np. Zer over here and inside this let’s say I want to create a 3 cross3 numi array which consists of only zeros and this I’ll go ahead and store it in this object called as N2 now let me print out N2 over here and we have a 3 cross3 numpy array which comprises of only zeros now if I want to initialize a numpy array with the same number then we can go ahead and use the full method so here I am using np. full and this takes in two parameters the first parameter is the DI di menion of the numpy array the second parameter is the value which we want to insert into this numpy array so here we are creating a 2 cross2 numpy array where the value is filled with 10 so as you guys see it’s a 2 cross2 numpy array where we only have 10 so it’s time for np. full I’ll just type in np. full I’m adding a comment over here then let me add NP do full and I’d have to give in two parameters let’s say I want 4 cross 8 numi array and I want the value five inside this now I’ll store this in this object called as N3 and I’ll just print out N3 over here and as you guys see I have created a 4 cross 8 numi array where the value is only five now similarly if I want to initialize a numi array within a particular range then I can go ahead and use the a range method so here as you guys see I am using np. range and this again takes in two parameters the first parameter is the initial value from which the range has to start so here when I give 10 as you guys see the range starts from 10 and when I give 20 so here again you’d have to remember that 20 is exclusive or maybe the second parameter is exclusive and since this is exclusive we’ll only have value starting from 10 and going on till 19 and that is why 20 is not included in this result over here now we can go ahead and add another parameter so here the initial value is 10 the final value is 50 and we have the skip value so the skip value is five which would mean that after 10 we’ll have 15 so 15 + 5 becomes 20 20 + 5 becomes 25 and that is how this keeps on proceeding now when we reach 45 when you add five more to 45 that becomes 50 and since 50 is exclusive over here that is why we end at 45 but on the other hand if we had given the final value as 51 then we would also have The Element 50 over here so let’s go ahead and Implement a range method over here np. AR range and I’ll store this in N4 now I’d have to use np. a range and I’ll go ahead and give in the initial value as let’s say 100 then I’ll give the final value as 200 and I’ll print out N4 over here and as you guys see we have all of the numbers in sequence starting from 100 going on till 199 now if I actually want the value 200 to be included in this as well let me make the final value to be 2011 and as you guys see this time the range starts from 100 and also includes 200 over here and we can also add a skip value now let’s say if I given the skip value of 10 so here after 100 we have 110 then 120 and this goes on till 200 now instead of 2011 if I keep the final value as 200 we see that this numpy array ends at 190 because 200 is exclusive now we can also go ahead and initialize a numpy array with random numbers and to initialize numpy array with random numbers is we can use random. randint so here we are invoking np. random. randint and over here we have three parameters and over here we have three parameters the first parameter is basically and over here we have three parameters the first two parameters basically indicate the range from which we would want the random numbers so we would want the random numbers in this r range of 1 to 100 and this third parameter would tell the python interpreter how many random numbers do we need so in the range of 1 to 100 we would need five random numbers and as you guys see this is the result which you get over here so we have 95 88 26 22 and 76 which are five random numbers generated between the range of 1 and 100 I’ll add this comment random now let’s go ahead and initialize an ire with some random numbers so I’ll have np. random. Rand in because I want a random set of integers and this will take in three parameters let’s say I would need values between 50 and 100 and I would need 10 random values over here and let me store this in N5 let me go ahead and print out N5 over here and and as you guys see I have 10 random values which are generated between the range of 50 and 100 similarly if I go ahead and run this again I’ll get a different set of values as you guys see we have a different set of values again when I click this we again have a different set of values that was all about initializing a numpy array with different methods now you can also go ahead and check the shape of a numpy array and to check the shape of a numpy array we have the shape method which again is very intuitive so here we are creating a numpy array where we are passing in a list of lists so in the first list we have 1 2 and three in the second list we have four five and six so obviously we will have a numi array where we’ll have two rows and three columns and this is what the N1 do shape gives us now if we want to change the shape of this then we can use the same method so here what I’m doing is I’m typing in N1 do shape and I’m changing the shape from two comma 3 to 3 comma 2 that is instead of having two rows and three columns I will have three rows and two columns so this same shape method can be used to check the shape of the numpy array and also reshape the dimensions of the numpy array so let me create a numpy array over here so we would have to create a multi-dimensional numpy array so this will be n6 is equal to I’ll have np. array and inside this I’ll have to create a list of list so in the first list I’ll have values 10 20 and 30 and in the second list I’ll have values 40 50 and 60 and I’m storing this in n6 now once that is done let me just print out n6 over here so you guys can see this numpy array now I’ll also go ahead and check the shape of it so I’ll have n6 do shape and we get the result that this is a numpy array where we have two rows and three columns now I can also go ahead and change the shape of this so I’ll have n6 do shape and over here I am changing the shape to be equal to 3 comma 2 then I’ll go ahead and print out n6 and as you guys see we have converted this from a 2 cross 3 numi array to a 3 cross2 to numi array so you had 10 20 30 in the first row we’ve got 10 20 in the first row here so similarly this is how this has been changed then we have some stacking methods over here we have v stack head stack and column stack so let’s just start with v stack so we are creating a numpy array N1 where we have the values 10 20 and 30 then we are creating the numpy array N2 where we have the values 40 50 and 60 now when we use v stack over here this again takes in two parameters where we’ll just pass in two numai arrays inside this so as you guys see we are vertically stacking over here so when I say vertically stacking I have one numi array on top of another numai array so because I’m using N1 comma N2 N1 comes at the top N2 comes at the bottom so this is how vertical stacking works then we can also go ahead and horizontally stack two numpy arrays so N1 and N2 we’ve got the same numpy arrays over here and instead of using v stack I’m using the head stack method I’m passing in N1 and N2 and as you guys see 40 50 and 60 so N2 has been stagged horizontally to N1 then we have the column stack so if we want to Stack these numpy arrays into separate columns so we have N1 and N2 and when I’m using column stack over here as you guys see N1 goes into the First Column N2 goes into the second column and this is how we can work with these stacking methods so I’ll have to create these two numi arrays over here I’ll have N1 which will be NP do array and inside this I’ll have 1 2 and three then I’ll also go ahead and create N2 so this will be np. array and inside this I’ll have 4 five and six so I have my N1 and N2 ready now I’d have to I’ll start off with vertical stacking so I’ll use np. v stack and inside this I’m passing in N1 and N2 and let’s see what would be the result so we have vertically stagged N1 with N2 N1 is at the top N2 is at the bottom we can also change how these are stacked so instead of giving N1 N2 let’s see if I give N2 and N1 you guys would see that N2 is at the top and N1 is at the bottom now we can similarly work with the head stack method so here I’ll type in np. hstack and inside this again I’ll pass in N1 comma N2 and as you guys see I have stagged N2 at the back end of N1 now if I want N2 first and N1 second I just have to change the sequence so inside NP do hstack I’ll given N2 comma N1 and I have 456 first and N1 is attached at the back end of N2 then we have column stack so I’ll just have np. column stack over here and inside this I’ll pass in N1 comma N2 and as you guys see I have N1 in the First Column N2 in the second column similarly if I given N2 comma N1 you will see that I have N2 in the First Column and N1 in the second column so this is all about stacking the numpy arrays now we’ll also work with intersection and difference methods so here again we have two numpy arrays so in the first numpy array we have the values from 10 to 60 and in the second numpy array we have the values from 50 to 90 and if I want the common elements between these two numpy arrays then I can use the intersect 1D method so here in the intersect 1D method I would just have to pass in these two numpy arrays and as you see in the result we get a new numi array comprising of the common elements in these two numpy arrays then over here I have N1 and ns2 if I want to find out all the elements which are unique to N1 then I can use the set diff 1D method so here in N1 we have the elements starting from 10 going on till 60 in N2 we have elements starting from 50 going on till 90 so here as you guys see 50 and 60 are common in N1 then N2 and if I want the elements which are unique to only N1 then I would have to use set diff 1D and I’ll pass in N1 N2 as you guys see I get only 10 20 30 and 40 because 5050 and 60 are present in both the numpy arrays I can also change the sequence over here so instead of passing in N1 N2 when I pass in N2 N1 then this will give me all of the unique Elements which are present in N2 and since 50 and 60 are common in both the NPI aray the resultant will be 70 80 and 90 because these are the only unique elements in N2 so let’s go ahead and work with intersect 1D and set diff 1D I’ll just add a comment over here intersect 1D and I would have to uh create two new numi arrays I’ll have N1 over here so I’ll have np. array and inside this I’ll pass in 1 2 3 4 5 6 and I’ll have N2 and inside this I’ll go ahead and create a list of elements where the elements start from 5 6 7 8 and and N so I have these two set over here now that my two numpy arrays are ready if I want to find out the common elements which are present in these two numpy arrays i’ have to use intersect 1D so I’ll have NP do intersect 1D and inside this I’m passing in N1 comma N2 and when I hit on run we see that the common elements between N1 and N2 are five and six now if I want only the elements which are common to N1 then I can use the set diff 1D method so here I’ll have NP do set diff 1D and inside this again I’ll be passing in N1 comma N2 and as you guys see I have 1 2 3 and four which are common to only N1 now similarly if I want all of the elements which are only common to N2 then I can have NP do set diff 1D and I’ll just change the sequence over here instead of passing N1 comma N2 I’ll have N2 comma N1 and the only unique Elements which are there in N2 are 7 8 and 9 now we’ll go ahead and perform some simple numpy array mathematics so we’ll see how to add two numpy arrays so again over here we have two numpy arrays in N1 we have 10 and 20 in N2 we have 30 and 40 now if I want the total sum of all of the elements which are present in both of these two arrays I can just directly use the sum method so I’ll have NP do sum and inside this as a list I’ll pass in N1 comma N2 and you will see that the resultant value would be 100 because 40 + 30 + 20 + 10 is equivalent to 100 now if I want to find out the individual sum along the rows and along the columns then I can use the additional parameter called as axis so if I want to sum the values along the column then I’ll set the axis to be equal to zero so when I have the axis value to be equal to zero as you guys see I have 30 + 10 = to 40 and 40 + 20 = to 60 similarly if I want to sum these up along the rows then I’ll set the axis value to be = to 1 and over here I have 10 + 20 = 30 and 30 + 40 = 70 I’ll add a new comment over here addition of numpy arrays and let me create new N1 over here so I’ll have np. array and inside this I’ll have 10 and 20 then I’ll go ahead and create N2 and inside N2 I’ll again have np. array and over here I’ll have 30 and 40 I’d have to pass this on as a list of values so I’ll have 30 and 40 inside a list so I have created N1 and N2 now it’s time to find out the total sum which is present along all of these so I would have to use NP do sum and inside this I’ll just pass in N1 comma N2 and actually I’d have to pass this as a list so I’ll have N1 comma N2 over here and as you guys see the resultant comes out to be 100 if I want to add the values along the column so I’ll have NP do sum then I’ll have this list over here I’ll have N1 comma N2 then I’ll have this new attribute called as axis and I’ll set the axis value to be equal to 0 and I get the resultant 40 and 60 because 30 + 10 is equal to 40 and 40 + 20 is equal to 60 now going ahead if I want to add the values along the horizontal rows so here it will be NP do sum and over here I’ll have N1 comma N2 again and this time I’ll set the axis value to be equal to 1 and I have 30 and 70 because 20 + 10 is equal to 30 and 40 + 30 is equal 70 now we’ll see how to do some scalar operations on these numpy arrays so here we have a num array where we have values 10 20 and 30 and if I want to add the scalar value one to each individual element of the numpy array all I have to do is add this value one to this numpy array and as you guys see N1 + 1 becomes 11 21 and 31 similarly if I want to multiply each individual element of a numpy array with a particular value so here if I want to multiply it with two I’ll just write down N1 into 2 so 10 becomes 20 20 becomes 40 and 30 becomes 60 and if I want to subtract a value all I have to do is perform N1 – 1 so 10 becomes 9 20 becomes 19 and 30 becomes 29 and if I want to divide it I’ll just have N1 divided by 2 so 10 becomes 5 20 becomes 10 and 30 becomes 15 so this was some basic idea about numai now uh let me just see what is there in N1 over here let me actually add some more elements inside this so I’ll have N1 is equal to np. array and inside this I’ll have 10 20 30 and 40 let me also print out N1 over here for your reference so now that we have N1 what I can do is I will just go ahead and add some scalar values to it and let’s say if I want to add five more to each individual element of the Stumpy array so I’ll just write down N1 + 5 and as you guys see 10 becomes 15 20 becomes 25 30 becomes 35 and 40 becomes 45 similarly if I want to subtract a value I’ll just write down N1 – 5 over here and 10 becomes 5 20 becomes 15 30 becomes 25 and 40 becomes 35 we can also go ahead and multiply something to this so I’ll have let’s say N1 into 10 let me print this out and as you guys see we have multiplied these values with 10 similarly I can go ahead and divide this with something so if I have N1 ided 10 so we see 10 20 30 40 becomes 1 2 3 and 4 so this is some basic scaler operation on top of the numpy arrays and we can also go ahead and use some mathematical functions so we have this mean function over here which would give us the mean value of all of the elements which are present so the mean value of all of this elements comes out to be 35 similarly if you want to find out the median then all we have to do is use this median method we have to use np. median will pass in the nampi and we see that the median value comes out to be 55.5 and if you want to find out the standard deviation I’d have to use STD and I’m passing in N1 inside STD and the value becomes 36.5 n so we have N1 over here and if I want to find out the mean value I’ll just have NP do mean and inside this I’ll be passing in N1 and as you guys see the mean or the average value of all of the elements which are present in N1 comes out to be 25 let me go ahead and create another numpy over here so I’ll have np. array and inside this I’ll be passing in some random values so let’s say these are all of the values which are present in N2 and if I want to find out the median of all of the values which are present so I’ll just use np. median method and inside this I’ll be passing in N2 and the median of all of these values comes out to be five similarly if I want to find out the standard deviations so here I’ll have np. STD and if I want to find a standard deviation of this particular numpy array so I’ll pass in N2 and you would see that the standard deviation of all of the elements of N2 would be 2.39 7 so till now we’ve worked with some basic numi array now let’s go ahead and work with a numpy matrix so here we are creating a 3 cross 3 numpy Matrix and to do that you’ll again need a list of lists so over here we’ll have 1 2 3 4 5 6 and 7 8 9 so so this first list goes into the first row second list goes into the second row and third list goes into the third row now that we have created this numpy Matrix let’s see how can we access individual rows and columns from this entire numpy Matrix so here let’s say if we want to access the first row again You’ have to remember that the indexing in Python starts from zero if we want to extract the entire first row we’ll just have N1 and inside parenthesis will pass in zero and as you guys see over here I have successfully extracted the entire first row similarly if I want to extract the entire second row then the index value for second row will be one and I have extracted the second row and if I want to extract a column then I’d have to do something like this so in a column I would want all of the rows so here from this numpy Matrix I would want the second column and all of the rows from the second column so here since I would want all of the rows I’ll just put in a colon over here and since I want the second column I’ll give in the index value as one and as you guys see I have extracted the entire second column over here similarly if I want to extract the entire third column the index value will be two and I have extracted the entire third column let’s perform this in Jupiter notebook so I’ll just write in numpy metrics over here now that this is set I’ll have np. array and over here I’ll have a list of lists so in the first list I will have 10 20 and 30 in the second list I will have 40 50 and 60 in the third list I will have 70 80 and 90 and I will go ahead and store this in N1 again now I’ll go ahead and print out N1 and let’s see what would be the result so this is our numpy array which we have just created now if I want to access individual rows from this I’ll have to give in N1 and inside parenthesis let’s say if I want to extract the third row then the index for the third row will be two I’ll just have two over here and as you guys see I have successfully extracted the entire third row now similarly if I want to extract the entire third column then this time I would have to write N1 and over here since I would want all of the records from the third column so I’ll just have a colon over here then over here so here you’d have to understand whatever is given on the left side of the symbol would indicate rows and whatever is given on the right side of this comma would indicate all of the columns so I want all of the rows and all of these rows need to be from the third column and the index for the third column is two and I have successfully extracted all of the elements which are present in the third column now we’ll see how to transpose a matrix so what is transposing transposing b basically means when you’re interchanging the rows and columns so here as you guys see we have 1 2 3 4 5 6 and 7 8 9 now the rows should be interchanged with the columns so here 1 2 3 which is present in the first row comes into the First Column 4 5 6 which is present in the second row comes into the second column 789 which is present in the third row comes into the third column let’s go ahead and perform transpose as well so all I have to do is use NP do transpose go and inside this I’ll just pass in N1 over here so as you guys see initially I had 10 20 30 which was in row this became in column 40 50 60 was in second row became the second column 70 80 90 was the third row which became the third column over here now we’ll see how to perform two matrices so over here we have N1 where we have the elements starting from 1 going on till 9 then we have N2 where we have elements starting from 9 going on until one now if we perform the dot operator on this which is basically matrix multiplication this is how the multiplication happens so the multiplication is row by column which would basically mean so here we have 1 2 3 here we have 963 so it will be 1 into 9 + 2 into 6 + 3 into 3 which will give you a result of 30 then again we’ll have row by column so here it will be 1 into 8 + 2 into 5 + 3 into 2 which will give you a result of 24 then it will be 1 into 7 + 2 into 4 + 3 into 1 which will give you a result of 18 and this is how this progresses and finally we’ll get this result over here and the dotproduct of N1 into N2 and N2 into N1 will be different so as you guys see this is the dot product of N1 N2 and this is the dot product of N2 N1 both of these will be different so I already have N1 over here let me go ahead and also create N2 so here I’ll write N2 is equal to np. array and over here I’ll have the elements in reverse order so I’ll have 90 80 and then I’ll have 70 going ahead I’ll have 60 50 and 40 after this I’ll have 30 20 N1 and I am storing this in this object called as N2 let me also go ahead and print out N2 for you guys over here so this is what we have now if I want to perform the dot product so I’ll have N1 I’ll actually have if I want to perform the dot product here I’ll have N1 dot dot inside this I’ll pass in N2 and as you guys see this is the dot product of N1 cross N2 but we know that N1 cross N2 and N2 cross N1 is different so when I perform ns2 dot N1 the result will be different from N1 do N2 so this was about metrix multiplication now we’ll go ahead and see how can we actually save a numpy array and then load it from somewhere else so here we are creating this numpy array where we have elements from 10 to 60 then to save this numpy array we just have to use the save method and over here we are saving this numpy array with this name called as mycore numpy so this takes in two parameters first parameter is the name by which we’ want to save this num array second is the array which we’ want to save now once that we save this to load this numpy array we will have to use np. load and over here we’ have to given the name by which we Sav this numi array so we save this numpy array as mycore numi and we’ have to give the extension which is do npy which basically stands for numpy and we go ahead and store this in N2 and we print out N2 we see that we have successfully loaded this numpy array over here now I’ll go ahead and save this so I’ll have NP do save and this as we have seen takes in two parameters the first parameter is by which I save this so I’ll have save N1 and I would want to store N1 so I have saved this now if I want to load this I would have to use np. load and over here I’d have to give the name of the numpy array so it will be savecore N1 do npy let me actually remove this over here and I will store this in let’s say N9 let me click on run over here so we have an error over here we have this error because I’d have to give this inside single codes now if I click on run we’ll get the result now if I print 10 and N over here we finally get the result so Panda stands for panel data and it’s the core library for data manipulation and data analysis so if you want to perform any sort of data analytical task pandas should be your goto library and pandas provides single and multi-dimensional data structures for the purpose of data manipulation so the single dimensional data structure is known as the series object and the multi-dimensional data structure is known as the data frame we’ll start off by understanding about the series object so series object is a onedimensional labeled array so we have already worked with the numpy array so in numai Array we had no labels along with it it was just a simple blank array where we had stored some values but over here in a series object as you guys see we have labels or you can consider them to be index with labels over here for these so first we’ have to start off by importing pandas which is the library and we are giving this alas as PD so import pandas as PD then if we want to create a series object will have PD do series and inside this I am passing in the values 1 2 3 4 and 5 and when I print it out I get this series object so when I check the type of this type of S1 this gives me pandas doc. series. series now over here you’d have to keep in mind that s is capital over here so if you’re given a small s you will get an error so let’s go ahead and create our first series object so I’ll just add the comment pandas now our first task would be to import the pandas Library so I’ll go ahead and type in import pandas aspd let’s just wait wait for this to be loaded properly now that we have loaded the library I can go ahead and create the series object so I’ll type in pd. series where s is capital inside this I’ll pass in the list of values so let’s say I’ll just have 10 20 30 40 and 50 and I’ll go ahead and store it in this object called as S1 let me print out S1 over here and as you guys see I have created this series object let me also check the typee of this so inside the type method when I pass an S1 you guys would see that this is a series object and over here we have the labels so the labels are 0 to 4 so by default the label or the index starts with zero over here so 10 is present at label or index 0 20 is present at index 1 30 is present at index 2 and that is how it proceeds further now since we have labels in a series object we we can change the how the label of the index looks like so over here we just had numbers starting from zero but instead of numbers let’s say if I wanted alphabets over here then I can add a new attribute called as index so over here with this index attribute I am setting the values to be equal to a b c d e so here initially we had index 0 zero has been changed to a then we had one one has been changed to B we have 2 two has been changed to C and that is how it proceeds so now over here let me go ahead and change these index value so it is the same command over here and with this same command all I’m doing is adding a new attribute called as index and I’ll pass in a list of values for the indices so I’ll have a b c d let me have D over here and and I’ll have e now when I click on run and I print out S1 over here you guys would see that I have changed the indices from 0 to 4 to a to e now we can go ahead and see how to create a series object from a dictionary so we have already worked with dictionaries we know that the dictionary is a key value pair so here we’ll just given pd. series and over here we have three key value pairs a B20 C30 so here automatically the keys are taken as the labels and these values are taken as the series values over here let me create a new dictionary so I’ll have D1 and inside D1 let me have four key value pairs maybe I’ll have a and 10 then I’ll have B and 20 after that I’ll have C and 30 going ahead I’ll have D and 40 now I’ll just go ahead and print this out as you guys see I have successfully created this dictionary now I would have to create a series object out of this so I’ll just have PD do series and inside this I’ll pass in D1 and let’s see what would be the result so these four Keys which were present became the labels and these values over here in the dictionary became the series values as well now we can also go ahead and change change the index position so similarly as we had actually changed the index values from numerical to alphabetical so when I given the index values as b c d a this sequence is maintained so I have B and C first so for B I have the value 20 C I have the value 30 and D we had not created any key with this particular index so that is why we have n a n over here and then we have a for which we have the value 10 so this is how we can maybe add a new index position or change the existing index positions so this is what we had over here this was our series object and now what I want to do is so instead of a b c d I would want let’s say C B A and D and if I click on run you guys see that the sequence has changed over here now we also see how to extract individual elements from the series object so here we have all of these elements starting from one going on till 9 and again You’ have to keep in mind that the indexing starts from zero so if I want to extract this particular element over here so the index value for this would be three so 0 1 2 and 3 and when I given the index value as three I am able to extract this particular element and if I want to extract a sequence of elements over here so if I want the first four elements then I’ll have S1 colon 4 this would mean that I am extracting all of the elements starting from index number zero going on till index number four and since four is exclusive so that is why we will only have till index number three so we’ll have index number one going on till index number three and if you want to extract elements from the back side here we’ll have S1 and we’ll type in Min -3 over here so minus 3 basically means third element from the end third element from the end when I given colon this would mean third element from the end going on till the end so this is the third element from the end over here so that is why I’ll have 7 8 and n and this is how I’ll be able to extract a single element a sequence of elements from the beginning and a sequence of elements from the back let me just create a new series object over here so I’ll have S1 is equal Al to PD do series and inside this I’ll pass in a list of elements I’ll have 10 20 30 40 50 60 and 70 now that I have this let me extract this element which is presented index number three so I’ll have S1 I’ll have parenthesis then I’ll just go ahead and give the index value so index value will be three and as you guys see I have successfully extracted this particular element now if I

    want to extract the first four elements then I’ll just give in the colon over here and I’ll have four and as you guys see I have extracted the first four elements if I want the last three elements over here then I’ll have S1 then I’ll have colon and I’ll type in minus 3 which would mean the third element from the last going on until the last element over here and I have 50 60 and 70 which are the last three elements now we can also go ahead and perform some simple operations on top of the series object so if I want to add a scalar value so initially we had the series object where the number started from one went on till 9 and if I just wanted to add the scalar value five to all of the individual Elements which are present in the series object all I have to do is use plus 5 and as you guys see 1 becomes 5 2 becomes 7 3 becomes 8 and so on and we can also go ahead and add two series objects over here I have S1 where the elements are from 1 to 99 and S2 where the elements are from 10 to 90 and when I perform S1 + S2 this would add the elements which are present at the same index position so here we’ll have 10 + 1 11 20 + 2 22 30 + 3 33 and this goes on till the last index position over here so let’s go ahead and perform some basic operations on top of the series object so I I already have S1 over here and these are the original values which are present in S1 now I’d want to add 10 more to these existing values so I’ll just type in S1 + 10 and as you guys see the values all of the values which are present in the series object have been incremented by 10 now also I can add two series objects together so in the series object I have seven elements I go ahead and create S2 where I’ll have seven more elements over here so I would have to type pd. series and inside this let me just have 7 6 5 4 3 2 and 1 let me print out S2 for your sake over here so we have S1 and S2 and when I perform S1 + S2 this is the result which we get so 10 + 7 becomes 17 20 + 6 becomes 26 30 + 5 becomes 35 and this proceeds to the last index value so that was all about the series object which was a single dimensional labeled array now we’ll work with a data frame which forms the major part of all of the machine learning data science projects so what exactly is a data frame it is a two-dimensional label data structure and if you’d have work with SQL or maybe Excel you would have dealt with tabular data and a data frame helps you to deal with tabular data in Python seamlessly so data frame because this is a tabular data consists of rows and columns now let’s see how can we go ahead and create a data frame from a dictionary so to create a data frame we’d have to use this particular method over here we’d have to type in pd. data frame where D is capital and F is also Capital so by any chance if you give maybe D as small or f as small you’ll get an error so both of them have to be in capital case and over here I have two key value pairs so the first key is name and then we have a list of values which are Bob Sam and Annie then we have the next key which is marks then we have the list of values for Marks which are 76 25 and 92 so here as you see the keys become the column names and the values become the records over here so name and marks become the column names and these values over here the list of values op Sam and Annie which are the values for this key become the records of this particular column similarly these values are there for this particular key and these become the records for this particular column so let me go ahead and create a First Data frame over here so I’ll just go ahead and type in data frame Now to create a data frame I’d have to type pd. data frame and D and F both have to be capital and inside this I’ll create a dictionary so to create a dictionary I would need Calli braces so the first key would be name and I’ll go ahead and give in a list of names over here let’s say the first name is Sam then we have Annie going ahead we have Jennifer now once we have the first key value pair I’ll go ahead and also add their marks over here so the second key would be marks and I’ll have a list of values over here let’s say Sam has got 50 marks Mar Annie has got 60 marks and Jennifer has SC 70 marks and I’ll go ahead and store this in DF let me print out DF over here and we have created our first data frame where the column names are name and marks and the values are these over here and let me go ahead and also show you guys the type of this object which I’ve just created so inside this type method I’ll be passing in DF and as you guys see this is p. c.f frame. data frame which basically means this is a data frame object now that we have created our first data frame we’ll perform some basic functions on top of a data frame we’ve got head tail shape and describe so we’ll just Implement all of these now to implement this we will be performing them on a data set called as the iris data set now to read any CSV file we have a method called as pd. read CSV so here I’ll have pd. read CSV and inside this I’ll give single codes and given the name of the file so the name of the file will be iris.csv and I’ll go ahead and store this in a new object called as Iris now let me hit on run let me check the first five records which are present in this Iris data frame so if I want to check the first five records which are present in this Iris data frame I need to use the head method so iris. head as you guys see I have the first fire records so this Iris data frame comprises of five columns which are SLE length seel width petal length petal width and species and we’ll have three different species which would be setosa ver color and virginica so this is how we can work with the head method now similar to the Head method we would also have the tail method which would give us the last five rows which which are present in a data frame so here I’ll type iris. tail and when I click on run you guys would see the index value over here the index value starts from 145 and goes on until 149 because there are 150 records in this Iris data frame so we have extracted the last fire records and we have printed it onto the console then if you want to check the number of rows and number of columns which are present in this Iris data frame we can just use the shape method so here I’ll have iris. shape and as you guys see this gives us a dimension value of 150 comma 5 which means there are 150 records in five columns then we’ll have the described method so here I’ll have IRS do describe and now when I click on run we have all of these numerical quantities over here so let’s say if I want to find out what is the minimum value which is present in the seel length column the minimum value present in this Seer length column will be 4.3 similarly what is the maximum value in Seer length column it will be 7.9 what is the mean value it will be 5.84 so these are some interesting metrics which we can find out with the describe method so now that we know this let’s actually see how can we extract individual records or individual columns from a data frame so for that purpose we can use the do iock and lock methods so let’s start off with the DOT iock method so do iock method with the help of it we can extract rows and columns on the basis of index so here iock basically stands for index location and over here as you see I have a comma and on the left side of the comma that would indicate all of the rows and the right side of the comma that would indicate all of the columns so from this entire Iris data frame I am extracting the first three records so here the rows would be 0 to three and three is exclusive over over here that is why I’ll have the records where the index values are 0 1 and 2 and similarly The Columns would be 0 to2 which would mean the column which is present at index0 which will be SE length and the column which is present at index one which is SLE width two again over here is exclusive so let me go ahead and extract some rows and columns with this iock method so we already have this Iris data frame with us let me again print out out the head for you guys so that will be easier for you to have a glance at this now let’s see from this entire data frame I would want the records from index number 30 going on till index number 40 and I would want the columns which are present at maybe index number three and index number four so index number three will be 0 1 2 and 3 this is index number three this is index number four so here I’ll have index number three going on till the end because this is the last column let me print this out and show you guys the result as you guys see I have extracted all of the records starting from index number 30 going on till index number 39 and the columns which you see starts from index number three which is this particular columns index and goes on till the end over here so this is how we can extract individual rows and columns with the dot iock method now we have the Dot Lock method so instead of giving the column index if I want to extract columns on the basis of their names then I can go ahead and use the Dot Lock method and over here when it comes to rows you see that I have given 0 to three here when it comes to Dot Lock you’d have to keep in mind that three is inclusive this is the only case where maybe you will find that the final value over here is inclusive so when I given 0 to three you will get all of the records starting from index number zero going on till index number three which is also inclusive and over here I’m giving in the column names which are SE length and petal length and that is what I’m extracting over here so I have this Iris data frame then I’ll just go ahead and use this do lock method now from all of these records let’s say I’d want all the records starting from index number 10 going on till index number 20 and the columns which I want to extract are seel length and petal length I’ll just have seel do length over here and the next column which I want would be petal do length let me hit on run and as you guys see I have all the records starting from row number 10 going on till row number 20 and 20 is also inclusive over here and the columns which have extracted are seel length and petal length now we’ll see how to drop a particular column so many of times it would happen that not all columns which are present in a data frame are important so from this entire data frame if we want to drop a particular column then we can just go ahead and use the drop method so here I’ll have iris. drop and I am dropping or removing the SE length column from this entire data frame so here when I set the access value to be equal to 1 this would basically mean that I am dropping a column so if you want to drop a row then you would set the axis value to be equal to zero and if you want to drop a column then you would set the axis value to be equal to 1 so if I if from this entire Iris data sets again I’ll show you guys the head of this so that you guys can have a glance at all of the columns which are present over here so from all of the columns if I want to drop the species column all I have to do is type in iris. drop and inside this I’d have to give in the name of the column which would be species and I’d have to set the axis which will be equal to one because I’m dropping a column and as you guys see I have successfully dro the species column from this entire data frame now similarly if I want to drop some particular rows which are present in this data frame so here as you guys see the index value it starts from zero goes on till four over here but if I want to drop the row indexes of 1 2 and 3 here as you guys see I have two parameters first parameter I’ll given a list of all of the indices that I’d want to drop so I’d want to drop the index value one index value two and index value three and the resultant which you see over here after zero we directly jump on to the index number four so now from this entire dat data frame this what you see over here I’ll just use iris. drop and I would want to drop the index values of 1 2 and 3 and I’ll set the axis to be equal to zero and when I hit run you would see that after zero we are directly jumping onto index number four so this is a very simple example of how to drop some records and how to drop some columns from your data frame now we’ll go ahead and work with some simple pandas fun functions so from the iris data frame if I want to find out the mean values of all of the columns I can just go ahead and use the mean method similarly if I wanted the median values of the records of all of the columns then I’ll just go ahead and use the median method similarly if I wanted to find out the minimum value I’ll use the Min method and if I wanted to find out the maximum value I will use the max method over here so very basic operations so when I use iris. mean this would give me the average values of all of the columns so average SLE length of the entire data frame is 5.8 average SLE width is 3.05 average petal length is 3.75 and average petal width is 1.19 similarly if I want to find out the minimum value of all of the columns so I’ll have iris. mean and this will give me all of the minimum values and if I want to find out the maximum values I’ll just go ahead and type iris. Max and this would give me the maximum value of all of the columns over here so I’ve got the mean value the minimum value the maximum value and I can also find out the median value so I’ll just type in iris. median and when I click on run over here this would give me the median values with respect to all of the columns which are present now we will get on with this Library called as m lip which is mostly used for data visualization and with the help of this Library we can create stunning plots such as bar plots Scatter Plots histograms and a lot lot more so we’ll start off by creating our first plot which will be a line plot so we would require two libraries over here the first Library would be numpy because we would want to create our data with this numai library then we would import this pip plot sub module from this mat plot lip Library so we’d have to type in from matte plot lip import P plot as PLT and the Alias which I’m giving for pi plot is PLT then I’ll create two numpy arrays over here the first numi array will be X and I’m creating this numpy array with the help of this np. a range method and the range will be from 1 to 10 and then I’ll create the next numpy array which is basically 2 * of X so all I have to do is multiply x with two and then I’ll get y so here we have 1 we have two 2 becomes 4 four 3 becomes six and this is how it proceeds now once we create the data all we have to do to create a line plot is use this PLT do plot so we’ll have the plot method in this P plot module so this takes in two parameters which are X and Y so we have already created our data X and Y so X will be plotted on the x- axis y will be plotted on the Y AIS and as you guys see over here x goes from 0 to 10 and Y goes from 1 to 20 over here and we see that there is a linear relationship between X and Y or in other words as the value of X increases the value of y also correspondingly increases so let’s go to jupyter notebook and create our first line plot using M plot LEP so so i’ have to start off by importing the required libraries so I would need numpy here I’ll type import numpy as NP and after this I would also require matplot lib so I’ll type from Matt plot lib import pip plot as PLT let me write those spelling properly over here and once I’ve imported these two libraries I’d have to create the data so first I’ll have X and this I’ll be creating with np. a range and the range will be from 1 to 10 since I want the numbers from 1 to 10 I’d have to give the value 11 because 11 will be exclusive over here then let me just go ahead and print out X for you folks over here as you see we have the number starting from one going on till 10 now if I have to create y y will just be 2 * of X so here I’ll have 2 into X and then let me print out y for you folks over here and as you guys see this is all of the elements are just two times of the elements which are present in X now since I have to create a simple line plot I would have to use PLT dotplot and this would take in two parameters which are X and Y so this data would be plotted onto the xaxis this data would be plotted onto the Y AIS and I’ll just show this as a result so as you guys see we have this object X mapped on the x-axis this object y mapped on the Y AIS and this is the corresponding line plot now that we have created this we can also add the title X label and Y label so we’ have to use this title method so we’ll have PLT do title and inside this we’ll give the title as line plot similarly we can also add X label and Y Lael by using these two methods and as you guys see we have added these two labels over here so let me add the title X Lael and Y label PLT so I’ll copy this entire code over here and then I’ll paste it over here now after this I’d have to add the title so for that I’ll use PLT do title and I’ll just give the title as first line plot then I’ll have X label so here I’ll have PLT do X label and here I’ll give the X label as xaxis then I would have the Y Lael so PLT do y label and inside this I’ll give the Y Lael as y AIS let’s run this and wait for the results so as you guys see initially this was just a bland plot without the title and the X and Y AIS labels now with the help of these three methods we have added the title The the x-axis label and the y- AIS label now we can also go ahead and change some more attributes with respect to this line plots so we have color line style and line width so initially as you see over here the color of this line by default is blue but if I don’t want the blue color and if I want some other color then I can just use this color attribute and assign it a new color since I’m giving it a value of G which basically means green color and as you guys see this is a green color similarly we have this next attribute called as line style so initially we have a solid line but instead of a solid line if I want a dotted line then I can go ahead and use a colon over here and as you guys see we have a dotted line right now and by default also the line width is one so we can go ahead and increase or decrease the line width so here we are setting the line width to be equal to two and this is the final result which we get so let’s go ahead and add some color line plot and line width I have the same code over here now what I’d have to do is add in some color so I’ll have this attribute called as color and I’m setting let’s say I’ll give it orange color to this now I would have to change the line style so I would want a dotted line instead of this solid line so I’ll give in a colon over here and I’ll also change the line width so I’ll give the line width as three so as you guys see initially this was the line which we had but now I have changed it to this so from Blue we have converted it to Orange from a solid line we have converted it to a dotted line and also you see that this is a thin line and we have increased the width of this line by two points now we have only created one line in one plot but we can also have two lines in the same plot so for this purpose I’ll have two y variables so X variable will be the same which will be the number starting from one going on till 10 but I’ll have two y variables y1 and Y2 y1 is 2 * of X Y2 is 3 * of X so now that we have y1 and Y2 ready I’ll have to make two plots because I’d want two line plots in the same graph so first I’ll have PLT dotplot and this line will be between X and y1 so this first line which you’re making it’ll be between X and y1 and for this particular line I’m setting the color to be equal to Green the line style to be equal to so this will be a dotted line then we’ll set the line width to be equal to two then we’ll have have our next line which will be between X and Y2 and the color of this line will be red and this will be a dashed line and we are setting the line width to be equal to three then again we have the title X Lael and Y Lael and we also have this new method called as grid so in the earlier plot as you guys see we don’t have any grid over here but when we set PLT do grid to be equal to True we’ll have a grid as well well and this is the resultant line which we get so here we have the green line over here which is this dash line then we then we also have this next line over here which is between X and Y 2 and this is the line and since I’ve also set the grid to be equal to true we also have a grid over here in this particular graph so I’ll go back we already have X now let me just print out X for you guys over here this is these are all the numbers which are present in X now we I y1 I’ll set it as 2 * of X and then I’ll have Y2 which will be equal to 3 * of X so I have y1 and Y2 ready now after this I would have to make a plot between X and y1 so I’ll have PLT dotplot and onto the x axis I’ll obvious obviously have X then onto the Y AIS I’ll have y1 and the color for the first line I’ll have green and let’s say the line width I’ll set this to be equal to 2 then I’ll have the next line which will be then I’ll have the next line which will be between X and Y 2 so here I’ll just write down X comma Y2 and for this line I’ll set the color to be equal to red and I’ll change the line width over here again so I’ll set the line width to be equal to 5 and I would just have to print it out so it’ll be PLT do show and I would also want a grid over here so before this I’ll set PLT do grid and I will set this to be equal to True let’s hit run and as you guys see I have two lines in the same plot now in the earlier example we had two lines in the same plot but if we actually want two subplots itself that is as you guys see over here this is one subplot this is one subplot and this line is present in the first subplot this line is present in the second subplot so this is also something which we can create so first we have X y1 and Y2 so these would be the same variables once we have our variables ready we we would have to use the subplot method so we’ll have PLT do subplot and inside this I am passing in 1 2 1 so here one two basically means that I would have two plots over here and those two plots would be present in this way so I’ll have one row two columns as you guys see I have one row and two columns so this is column number one column number two which are present in the same row then I will given the index of this subplot so this is index number one and for this first Index this will be the plot which we’ll be creating so for the first index the plot will be between X and y1 color will be green line style will be dotted and line width will be two and as you guys see at index number one we have this green colored line between X and y1 then we’ll have PLT do subplot which is our next sub plot and the first two parameters will be the same the third parameter here will set the index value which is two so this is what we’ll be getting over here and the second index will have the line plot between X and Y2 and the color as you see is Red Line style is dotted and line width is equal to two then we’ll just go ahead and print it out so we already have X y1 and Y2 ready with us now after this we would have to start off by creating a subplot so here I I’ll have PLT do subplot and I’d have to given the and here I’d have to given the dimensions over here so I’ll have 1 2 comma 1 so I’m creating my first subplot over here PT do plot and this plot will be between X and y1 and I’ll set the color to be equal to Yellow then after this I’ll go ahead and create the second subplot and the first two parameters will be one and two because i’ want these two plots along the columns and I’ll set the index to be equal to two then I’ll have PLT do plot and the next plot will be between X and Y2 and over here I am setting the color to be equal to Orange then I can just go ahead and show you guys the result result so as you see I have two subplots over here the first subplot is between X and y1 and the color of this line is yellow the next subplot is between X and Y2 and the color of this line is orange now if I want the subplots along the row and not along the column that is also I can set so all I have to do is make this change over here I’ll set this to be equal to 2 comma 1 and similarly over here here I’ll make this to be 2 comma 1 which means that I will have the plots along the rows I’ll have two rows and only one column when I hit run as you guys see I have two rows and only one column this is the first subplot this is the second subplot so that was a line plot which helped us to understand the relationship between two numerical entities so whatever we mapped onto the xaxis was a numerical entity and whatever we mapped onto the y- axis was also a numerical entity now we’ll go ahead and work with something known as a bar plot which would help us to understand the distribution of a categorical column so for this we are creating a dictionary called as student and it compris of three key value pairs we have Bob 87 Matt 56 and Sam 27 now we’ll go ahead and extract the names and values individually so the names of these students are basically the keys so I’ll have student do Keys which will give me all of these keys and I’ll go ahead and convert these Keys into a list so I’ll pass this into this list method and I’ll store the result in this names object similarly I’ll extract all of the values I’ll convert all of the values into a list or I’ll store all the values into a list and I’ll store that list in this object called as values so I’ve got names I’ve got values and to create a bar plot all I have to do is use PLT dob bar and the takes in two parameters the first parameter will have the categorical values the second parameter will have the numerical values since the first parameter compris of the categorical values I’ll pass in names over here and the second parameter will be the values and as you guys see over here on the x-axis I have the names which are Bob Matt and Sam and on the y- axis I have the corresponding values so VC that Bob has scored the highest marks followed by Matt followed by Sam now since we are creating a bar plot I’ll just add this comment bar plot over here and I’d have to create a dictionary so that we get the data for this bar plot so I’ll name this dictionary as student and we can create a dictionary with these curly braces over here so I’ll have the first student who is Bob let’s say Bob has scored 45 marks then we we have the second student Sam and Sam has scored let’s say 97 marks then we’ll have Matt and let’s say Matts has scored only 23 marks so we’ve got three key value pairs now that this is done I’d have to extract the keys so I’ll type in student. keys and I will convert this into a list so I’ll cut this I’ll put this inside the list and I will store this in a new object called as names now that I have names of all of the students I’ll go ahead and also extract marks of all of the students so I’ll have I’ll store that in this object called as values I’ll have to convert the result into a list and inside this I would basically have to extract all of the values so it’ll be student. values I have names and values ready and to create the bar plot I would just have to use PLT do bar the first parameter will be names and the second parameter will be values then I can just go ahead and show out the result so we have Bob Sam and Matt mapped on the xaxis and their corresponding values and we see that Sam has the highest marks and Matt has the lowest marks and now the plot which we had created earlier was very Bland and we can go ahead and add the title X Lael and Y Lael to it and also assign a grid so we’ll be using the same methods to add the title we’ll have PLT do title and to add the X label and the Y label we’ll be using PLT dox Lael and PLT doy label and we’ll also set the grid to be equal to true I’ll copy these two set of commands over here and I would have to set the title so here I’ll have PLT do title and and I’ll set the title to be equal to marks of students then I’ll have something on the xais PLT dox label and I’ll just have names and the x-axis then I’ll have something on the Y AIS here I’ll just write down PLT doy label and on the Y AIS I’ll just write down marks and I would also have to set the G to be equal to true so it will be PLT do grid and here I’ll set the value to be equal to true and as you guys see I have names on the x-axis marks on the Y AIS and I’ve also set the grid to be true now after this we can also create a horizontal plot so the plot which we had created earlier was a vertical plot so here we are basically doing two things so if we have to create a bar plot we have to use bar hedge instead of using just bar and we are adding a color as well so by default we had the blue color and if I want to change the color from blue to green I’ll use this color attribute and I’ll map the green color to this rest everything will be the same let me add a new comment over here which will be horizontal bar plot now that I have added this comment let me go ahead and copy everything I’ll paste it over here and instead of just having bar I’ll have bar H and I’ll set the color to be red and we have this bar plot over here so it’s just that we have to Interchange the label so on the x axis now we have the marks so let me keep this as marks and on the Y AIS we’ll have names let me change this to names and as you guys see we have successfully created this horizontal bar plot now that we are done with the bar plot we’ll head on to the next geometry which will be a scatter plot a scatter plot again is used to understand the distribution between two numerical entities and these and these entities are represented in the form of data points so we’ll be creating two lists over here the first list will be storing in X which basically comprises of the elements starting from 10 going on till 90 then we’ll have the next list a which comprises of some random elements and here you’d have to keep in mind that both of the lists have same number of elements else there’ll be an error and to create a scatter plot we’ll just use PLT do scatter we’ll pass this over here we’ll pass this over here as a second parameter and then we’ll just show off the result let me go ahead and add a comment over here so this will be a scatter plot Now to create this scatter plot I’d have to create the data so I’d have to store something onto the x axis so in X I’ll just have 1 2 3 4 5 6 7 8 and 9 and then I’ll have y and then y I’ll just have nine Rand ROM numbers over here so let me just store some nine random numbers which is done now to create the scatter plot I’ll have PLT do scatter and I’ll just pass in X comma Y and I would have to show out the result PLT do show and as you guys see I have created this scatter plot let’s start off with this particular point over here this particular point in indicates these two so I have X Y which is basically 1 and 5 so this intersection between 1 and five is where we’ll be getting this point so I have X I have y and this is the point I have then I have 2 comma 2 so the intersection of 2 comma 2 I’ll get this particular point then we have this point over here which is the intersection between 9 and 7 so as you guys see I have 9 and 7 over here now we can also go ahead and add some athetics or change the athetics of the existing points so we had same X and A these are the same list which we have it’s just that we are adding new attributes over here the first attribute as marker initially we just had solid circles so instead of having solid circles if I wanted a star then with the help of this marker attribute I’ll add star over here so similarly to change the color I’ll just have C and I’ll assign green to this attribute C and also I can change the size of this and to change the size of this I’ll be using S let me copy this entire thing I’ll be pasting it over here so these two will be the same I’ll add this new attribute called as marker and I’ll set the marker to b equal to Star and I’ll change the color color will be let’s say I’ll have orange again and I’ll have size to be equal to 200 let me print it out and as you guys see this is the result just to show you guys what happens if we increase the size instead of 200 if I keep it to be 500 you guys see that the size has increased again now instead of 500 what happens if I keep it to be 50 you would see that the size of the Stars has decreased now as we did with the line plot where we had two lines in the same graph we can perform a similar sort of thing over here where we’ll have two different sets of points in the same plot so for this we would need a new list we already have X and A then we’ll create a new list called as B again we have to keep keep in mind that the elements which are present in this list or the number of elements which are present in this list should be equal to the number of elements which are present in a as well as X then we’ll go ahead and create our first scatter plot by using PLT do scatter and the first cat plot will be between X and A and the first cat plot here these dots will be represented with stars then we’ll go ahead and create the second scatter plot which will be between X and B and these will be represented with circles and also we have different colors for both of these cater plots and also the size of the different data points will be different I’ll copy this entire thing over here and I’ll paste it down I’ll change this to be equal to y1 then I’ll have a Y2 as well and then Y2 I would need uh some some bunch of elements over here random nine elements so let me just have some random N9 elements let me just check how many elements do we have so I have 1 2 3 4 5 6 7 8 and 9 so these three lists are ready after this I’d have to go ahead and create the first catter plot which will be between X and Y one then I’ll go ahead and create the second scatter plot which will be between X and Y2 and I’m not adding the marker because we have already added the marker for the first one I just change the color for this so the color for second one let’s just keep it to be blue so I don’t have to add the color as well and I’ll change the size of this so for this I’ll set the size to be equal to 500 so as you guys see I have two sets of points over here the first set of point is being denoted by the small Stars the second set of points is being determined by these solid circles now instead of having those set of points on the same plot we can go ahead and create two subplots as well so we’ll be using the subplot method as we had used during the case of line plots so here we have these three lists then I’ll have PLT do subplot then we’ll have the same three parameters and since I want these plots to be present column wise in not rowwise I’ll have 1 comma 2 which means that I’ll have one row two columns and then I’ll give the index so at index number one we’ll have this scatter plot which will be between X and A so this is the scatter plot between X and A then I’ll go ahead and create the next subplot and the next subplot will be between X and B I’ll copy this entire thing I’ll paste it over here it’s just that I’d have to create a subplot now so I’ll have PLT do subplot and I want these plots column byse so I’ll have 1 comma 2 comma 1 and the first subplot will be this then I’ll go ahead and create the second subplot so I’ll have PLT do subplot and here I’ll be writing 1 comma 2 comma 2 and this is our first subplot and this is our second subplot so that was all about the scatter plot for categorical column but when it comes to histogram we’ll be using that to understand the distribution of a continuous numerical column and we’ll be creating this continuous numerical column or continuous numerical data with just a list over here so this is a very basic example I’m just creating a random list which comprises of all of these numbers and I’m storing it in this object called as as data and to create a histogram all I have to do is use this hist method so inside PLT doist I’ll pass in this list and when I show out this is the result which I get so here let’s actually have a look at this particular bin so in a histogram these are known as bins so here for this bin for the value three we have this value four or for the value three on the x- axis we have this value 4 on the y axis which would mean that this number three is occurring four times similarly if I look at this one over here so for this one on the xaxis we also have the value one on Y which would mean that one is occurring only once similarly we have four so this number four is occurring two times then we have eight which is occurring three times so let’s go ahead and create a histogram in Jupiter notebook I’ll just add a comment over here histogram and let me go ahead and create a list so I’ll store this list in L1 and I’ll have some random numbers over here so I have created my list and now that this is ready I can just go ahead and build out the histogram by using this method called as PLT doist and inside this I’ll be passing in L1 then since I just have to show this out I’ll just have PLT do show and this is the result so as you guys see this number three is occurring four times then we have this number six which is occurring three times and the rest of the numbers are occurring only once now we can also go ahead and change the number of bins which are present or the color of the histogram as well so to change change the color we’ll just use this over here so for the color attribute we are mapping in G which will give us this green color and initially we had 1 2 3 4 5 6 seven bins over here but instead of seven bins let’s say if I want to reduce the number of bins I’ll just use this attribute and set the number of bins to be equal to four and this is the result which I get I’ll copy this entire thing I’ll paste it over here now that I have created this histogram I would want to change the color of this so I’ll set the color to be equal to green and then it off so how many bars do I have 1 2 3 4 5 6 7 8 and 9 instead of having nine bars let’s say I would want only three bars over here or three bins over here I’ll set the bins value to be equal to three and as you guys see I have only three bins now let me set it to be five and I have 1 2 3 4 and five b this was all about histogram now the earlier histogram which we had created was with respect to a single list but if you want to create a histogram on top of a data frame then let’s see how can we do it so here we’ll be building this histogram on top of the iris data set so if you have to load any data frame we’ll have to use reor CSV since this is a CSV file so inside reor CSV I am passing in the name of the file and I’ll store it in this object called as Iris and when I use iris. head this will give me the first five rows which are present in this data frame now after that to create the histogram we’ll just use PLT doest and inside this so here we were passing in the list of numbers but here instead of passing in the list of numbers I’ll pass in the column so we have this SE length column which is present in the iris data frame so I’ll just pass in the seple length column and I’ll set the number of bends and I’ll also set the color to be equal to something and when I show it out this will be the result which I’ll be getting so here I’d have to load the iris data frame first so in the iris object I’ll just have pd. read CSV and inside this I’ll give the name of the file which will be equal to iris. CSV I’ll click on run so I’d have to import the panda data frame as well to use reor CSV method so I’ll have import pandas as PD and we have let’s just wait till this is loaded both the library and also the data frame now in the while it is loaded I’ll go ahead and write in the head method as well now if I click on run we’ll have a glance at the first five records which are present in this data frame and as we see we have seel length seel width petal length petal width and the species column and now I’d want to create a histogram for let’s see this petal length column over here then I’ll just have to type in PLT doist and inside this I’d have to pass in this petal length column so here so i’ given the name of the data frame first and using this parenthesis I’ll give the name of the column so the name of the column let me keep this to be small it’ll be petal length and I’ll set the number of bins to be equal to let’s say 50 and after that I’ll set the color of the bins to be equal to Green again and we can just go ahead and show the result so this should be be color and not C I’ll keep it as c o l o r all right so as you see we have successfully created this histogram for this petal length column now we’ll head on to the next geometry which is a box plot so this box plot basically gives is a five number summary so here in this result what you see this is the minimum value this is the 25% value this is the 50% value this is the 75% value and this is the maximum value so we’ll be understanding more about this as we progress through the session so first what we’ll do is we’ll just go ahead and create three list in the first list we just have numbers from 1 to 9 in the second and the third list we have randomly given some numbers over here and then we’ll create a list out of all of these three lists so inside this list method I am passing in 1 2 and three as a list and I’ll store the resultant list in this object called as data then if I have to create a box plot I’ll just use PLT dobox plot and inside this I’m passing in this data object which I just created and when I show it out this is the result which I get now when I compare these three boxes let’s actually understand the inferences over here so for this plot or this plot this box basically refers to this particular list this box tells us that the median value of the numbers which are present in this list is five the maximum value is N9 the minimum value is 1 similarly if we look at this particular box so this box refers to this particular list and this box tells us that the median value will be three the maximum value will be five and the minimum value will be one then if we have a look at this particular box this would tell us that the median value will be seven for this list the minimum value will be four the maximum value will be 9 so it’s time to create a box plot I’ll add this comment over here box plot and after this I’d have to create three lists so I’ll have L1 and inside L1 I’ll just have all the numbers starting from 1 going on till 9 then I’ll have L2 inside L2 I’ll just randomly given nine numbers and again I’ll have L3 and also in L3 I’ll just randomly given some numbers over here now once we have L1 L2 and L3 I’d have to create a list of lists so inside this this I’ll just be passing in L1 L2 and L3 and I will store this in a new object called as data so now that we have created data I can just go ahead and build out the box plot so I’ll have PLT do boxplot and inside this I’ll just pass in data then I can just show it out to you guys so we have these three box plots over here so the first box plot represents this particular list we see that the median value is five minimum value is one and the maximum value is N9 then we have this second list over here so for this the median value is five the minimum value is also one and the maximum value is 8 so and for this particular list it seems that the median value and the 75% value is same so for this the median value is 8 the minimum value is five and the maximum value is 9 so one another geometry which is analogous to the box plot is the whin plot so the only difference is to create a while in plot we’ll be using Vin plot method instead of box plot method and this is the difference between how these boxes and vience look and we can so normally if we don’t set the show medians to be equal to True will not have these lines or these indicators over here so we also have to set this to be equal to true so now here I’ll just copy this entire thing I’ll paste it over here and instead of having PLT dobox plot I’ll just have PLT dowh in plot and I’m printing out the same data and as you guys see I have created this while in plot and now if I want the median so I’ll have show medians and I’ll set this show medians to be equal to true and as you guys see I have added the medians as well and and after the viin plot we’ll have the pie chart and pie chart again helps us to understand the frequency or percentage of different categorical values so here we have two lists the first list comprises of all of the names of different fruits so we’ve got Apple orange mango and guwa then in the second list we’ve got the quantity of these fruits so we’ve got 67 apples 34 oranges 100 mangoes and 29 guav was and if I want to represent this relationship of the quantity of these fruits in a pie chart this is how I can do it so I’ll be using PLT do PI and here first I’ll be passing in the numerical entity so the numerical entity is this quantity and I’ll be passing that over here then I’ll have the categorical entity over here which is fruit so I’m assigning fruit to labels then I can just go ahead and show out the results and as we see in the result we see that the maximum percentage is of mango and the minimum percentage is of guwa so i’ have to create two lists over here I’ll create the first list I’ll have fruit and the first fruit will be apple the second fruit will be mango the third fruit will be orange and the fourth fruit will be ly now that we have created all of these fruits I’ll also assign the quantity so let’s say I have 53 apples I’ve got 43 mangoes I’ve got only 12 oranges and I’ve got 97 Lees so I’ve got these two ready now after this I’d have to create the pi chart so I’ll have PLT do PI first I’d have to given the new numerical object which will be quantity then I’d have to given the categorical object so I mapping fruit onto the labels then I would just have to show out this pie chart so PLT do show as you guys see this is the resultant pie chart so the maximum portion belongs to lii and the minimum portion belongs to Orange because we have 97 lies and we have only 12 oranges now now we can also go ahead and change the colors of these different sectors and also add the actual percentage in these different sectors to add the percentage we’ll be using Auto PCT so here for this autop PCT attribute I am using 0.1f now what this basically means is the 0.1 basically means that I’ll have the decimal values to one place so if I have 0.1 this will mean I have decimal values to one place if I’ll keep it as 0.2 then I’ll have decimal values to two places and after this I’m just adding this new attribute called as colors and the coloring it starts from the first label which is Apple over here orange then I’ve got blue which is for Mango then I’ve got black which is for guaa I’ll copy this entire thing over here I’ll paste it over here now to add the percentage I’ll be using autop PCT and here I’ll have to use percent 0.1 F let me add percent percent and after this I’ll also have to set the color over here so the colors I’d have to give a list of colors so let’s say for Apple I’d want to given green then for for mango I’d want to give in yellow then for orange i’ obviously want orange and for liy I’d want pink now when I hit on run let’s see what would be the result let me change this to colors instead of color so as you guys see I have the percentage over here because I had used autop PCT attribute and it seems that we have out of all of the fruits 47.3 of them are lies and this is the color indicated similarly out of all of the fruits only 5.9% of them are oranges so we have created the pie chart then something which is very similar to pie chart is the donut chart and to create this donut chart we’ll be using two pie charts over here so the data is same we have fruit and quantity it’s just that first we’ll go ahead and create our first pie chart which comprise of quantity and labels and I am adding a new attribute inside this which is the radius so I am setting the radius of this to be equal to two then I’ll go ahead and create another pie chart here inside this I’ll just pass in so first we have to pass in numerical entities so I’ll just have a list which comprise of only one element or one value which is one and this entire value or this entire list compris of only one color which is white and I’ll set the radius to be exactly half of the original Pi so I’m setting the radius to be equal to one and as you guys see this is the outer pie chart this is the inner pie chart so the outer pie chart has a radius of two the inner pie chart has a radius of one and since I’ve given a color of white this is what I have over here so you can give any random number over here that doesn’t really matter so you can give 1 10 5 it is all the same it’s just that keep the color as W because since a donut it just basically looks like a donut we have to give in the white color I’ll copy this entire thing I’ll paste it over here I’ll set the radius for this to be equal to let’s say four then I’ll have a new pie chart over here I’ll just give a random value let’s say I’ll give five over here and after this I’d have to set in a color and since there is only one value I’ll just give in white color over here and after this the only thing which I have to set is radius and I’ll set the radius for this to be equal to two let’s hit on run and this is the donut chart which we get so as you guys see the radius for the outer pie chart is four the radius for the inner pie chart is two let me reduce this let me keep this to be two actually and let me keep this as one and this is the resultant donut chart so cbor is another visualization Library which is built on top of M plot lip so if you want to work with cbon then we’d have to import matplot lip as well so to import cbon we have to type in import cbor as SNS and since this is built on top of mat plot lip we also have to import mat plot lip so we’ll have from mat plot lip import P plot as p LT and inside cbon we have this method called as load data set and we have some built-in data sets inside the seor library and one such built-in data set is the fmri data set and I’m storing this this new object called as fmri and then I’ll have a glance at this first five columns of this data set so we have these columns we’ve got subject time Point event region and Signal now out of all of these columns I’d want to make make a line plot between the time Point column and the signal column so to make a line plot with the help of the cbor library since I’ve given the alas for the cbor library as SNS I’ll type in SNS do lineplot and onto the xaxis I am mapping the time Point column and onto the Y AIS I am mapping the signal column and then I’ll have a new attribute called as data so basically I am building this line plot on on top of this fmri data set that I just go ahead and show out this line plot so let’s understand this properly so if you look at this line plot closely you would see that till the time point of 5 Seconds so let’s say if this time point is in seconds so till the time point of 5 Seconds the signal value is increasing but from time point of 5 Seconds to 10 seconds you have the signal value to be increasing and after 10 seconds this sort of stabilizes and only increases two so here if the value is min – 0.05 so from – 0.05 it goes up to close to zero so now that we are done with mt plot lip let me go ahead and create a new notebook over here and I’ll name this new notebook to be equal to cbon let me rename it I’ll delete this I’ll name this as cbon demo and we’d have to load the required libraries the first library is obviously cbon so I’ll have import cbon as SNS then i’ also need pip plot so I’ll have from Matt plot lib import pip plot as PLT let’s just wait for these two libraries to load and once we have cbon with us we’ll be working with the fmri data set so to load the fmri data set we have the load data set method meod which is part of the SNS Library so I’ll have SNS do load data set and inside this I’ll be passing in the name of the data set which is fmri and I will store it in this object called as fmri and let me have a glance at the first five records of it so I’ll just type in fmri do head and these are the different columns which are present and I’d want to make a line PL between the time Point column and the signal column and for this purpose I’d have to map the time Point column onto the x-axis and the signal column onto the Y AIS SNS do line plot and onto the xaxis I am mapping time point and onto the Y AIS I am mapping signal and the data is obviously fmr right then I would just have to show out the result so I’ll have SNS i’ actually have to give in PLT do show over here let’s hit run so we have successfully created this line plot and as we had already seen so till 5 Seconds there’s an increase in the signal value from 5 to 10 seconds there’s a drop and then it sort of stabilizes now we can also add a new attribute or new athetic called as Hue so here we had only one line and this one line arbitrarily the color of this was blue but if I want the color of the lines to be actually determined by a column what I can do is I can map a column onto the Hue athetic so we have the event column and we are mapping the event column onto the Hue athetic so now the color of the lines would be dependent on this event column and since we have two events over here we have the stim event and we have the Q event this blue color line represents the stem event and this orange color line represents the Q event and if we look at this blue color line over here you would see that till time point of 5 Seconds the signal value it goes up till maybe 20 and then it drops down to a Min – 0.1 and similarly if we look at this Q event you see that the peak is not so high so for the stem event it was almost 0.2 and when it came to Q it is only 0.05 but you’d also notice that the drop is not so steep so for the drop of the stem event it came from 0.2 and it’ll drop down to Min – 0.10 but for this particular signal Q the drop is very small it dropped from 0.05 to only minus 0.05 and also if you see the increase of it so once it dropped the increase of Q event is much higher when compared to the increase of this stim event so this is the same set of commands it’s just that I am adding a new attribute called as Hue and I’m determining the color of the lines on the basis of this attribute and I’ll be mapping the event column on top of this so as you guys see I have this event column and this is the same column which I’m mapping onto this Q ech event we see that we don’t have the event column so what I’ll do over here is I will cut this out I’ll paste it over here I’ll put in a comma and since this is a column I’d have to put this in double quotes now when I hit run so seems like we have an error again over here I have two commas let me go ahead and delete this comma and we have successfully produced this two line plots over here now we can also go ahead and change the style of how these lines look so now if I want these lines again or the style of these lines again to be dependent on a column I can just go ahead and add a column onto the style athetic so the color is also being determined by the event column and also the style is being determined by the event column so it’ll be the same command and I’ll be adding a new attribute to this I’ll have style and to this I am mapping the event column and you see that the Q event is being represented with this dotted line and the stem event is being represented with this solid line so now that we have created that we can also add markers on top of it so all we have to do is set the markers to be equal to True here in this command I’ll have this new attribute called as markers and when I set this to be equal to true you will see that so I have markers now when it comes to the stem event the markers are solid circles and when it comes to the Q event the markers are crosses so that was all about line plot with the help of the cbor library now we’ll go ahead and create a bar plot with the cabor library so to create this bar plot we’ll be actually needing the Pokemon data set so we’ll have to start off by loading the Pokemon data set so we’ll have pd. read CSV and we’ll store this pokemon. CSV file in this Pokemon object and once we store it in this object we can go ahead and create a bar plot for this is legendary column this is legendary column is a categorical column and that is why we are creating a bar plot and this is legendary column we have two categories 0 and one so zero indicates that the Pokemon is not legendary and one indicates that the Pokémon is legendary and onto the Y AIS I am mapping the speed column and from this bar plot it is very evident that legendary Pokémons or the speed of the legendary Pokémons is high higher when compared to the speed of non- legendary Pokémons so now I’d have to go ahead and load of the required data set so to load the data set I would need pandas so I’ll just type in import pandas as PD now once I have this I need to load the CSV file so I’ll Type in pd. read CSV and inside this I’ll give the name of the CSV file which is will be pokemon. CSV and I will store it in this new object called as Pokemon and once I’ve stored this let me have a glance at this so I’ll just type in pokemon. head and I’ll show you all of these columns so these are the different columns which are present over here so against Bug against dark against dragon all of these columns basically tell us how does a Pokemon perform against these types of Pokémons then let me scroll it to the last so here we have this s legendary column which would tell us if the Pokémon is legendary or not then we have this generation column which would uh tell us the to which generation does this Pokemon actually belong to then this column tells us what is the weight of this Pokémon in kg then this type one tells us what is the primary type of the Pokémon type two tells us what is the secondary type of the Pokémon and then we’ve got other columns as well so now for this bar plot we’ll be only working with this s legendary column and then the speed column so I’ll be using SNS do barplot and onto the xaxis I would have to map s legendary and then onto the Y AIS I’ll be mapping the speed column and the data onto which I’d want to build this bar plot is the Pokemon data set then I can just go ahead and show this out and when I hit on run you would see that I have successfully created this bar plot and again it is very evident that the legendary Pokémons the speed of the legendary Pokémons is higher when compared to the speed of non- legendary Pokémons so now we’ll create a bar plot between s legendary column and weight in kg column so s legendary column is again mapped on to the x-axis it’s just that here for the y- axis we are mapping weight kg column and this time it is it’s very very obvious that legendary Pokémons their weight is much much higher when compared to non- legendary Pokémons I’ll cut this out I’ll paste it over here and instead of mapping speed I have the weight kg column and when I hit on run you would see that the weight in kg of a legendary Pokémon is much higher when compared to the weight in kg of a non- legendary po Pokémon now we can also go ahead and determine the color of these bars on the basis of a column and we already know to do that we’ll be using the Hue etic or the Hue attribute and I want the color to be determined by the generation column so this time I’ll be mapping the generation column onto the Hue athetic and as you guys see we have seven generations over here starting from generation one going on until Generation 1 and since we have two different legendary status so zero indicates that the Pokémon is not legendary one indicates that the Pokémon is legendary for these two categories I’ll have seven bars each so for those Pokémons which are not legendary I’ll have these bars these seven bars indicating to which generation does the Pokémon belong to similarly for all of the Pokémons which are legendary I’ll have these bars indicating to which generation does the Pokémon again belong to and again on the y- axis since we have mapped speed we have the speed values corresponding to whether the Pokémon is legendary or not legendary so the same set of command I would have to use the Hue esthetic and onto this I’ll be mapping the generation column and as you guys see so this denotes all of those Pokémons which are not legendary this denotes all of those Pokémons which are legendary and it is very clear that legendary Pokémons they have much much higher weight when compared to non- legendary Pokémons and we have the distribution of Pokémons which belong to the different generations with respect to both legendary Pokémons and non- legendary Pokémons now going ahead we can also change how the different pallet look for these bars so we will use the pallet attribute so we’ve got these three different palletes over here so again the bar plotters between is legendary column and weight column and it’s just that instead of using a column to map it onto the Hue athetic we are using a new athetic called as pallet and we can directly use different predefined palettes so first we have the blues D palette with a capital B this is how it looks like then we have the rocket pallet and this is the resultant and then we have the VL pallet let me delete this Hue attribute over here and instead of that I’ll have pallet attribute and the first palette which I’ll be using is blues D and as you guys see this is the blues D palette similarly if I want maybe a more of red color then I I’ll be using the rocket palette then if I want maybe a light shade of bluish gray I’ll be using V lag and this is what I’ll be getting now instead of maybe using a palette or maybe mapping a color onto the Hue athetic I can just go ahead and use the color attribute and assign one single color for all of the bars which are present so I’m using the color attribute and I’m mapping the orange color to both these bars I’ll remove pallet and instead of pallet I’ll have the color attribute and I will set the color of these two bars to be equal to Orange and this is the result so we are done with bar plot as well now we’ll go ahead with the next type of plot which is scatter plot and we have already learned that a scatter plot is used to understand the relationship between two numerical entities and over here we are building the scatter plot on top of the Iris data set so we’ll load this data set up then we’ll be using SNS do scatterplot and onto the xaxis I am mapping the seple length column and onto the y- AIS I am mapping The Petal length column and the data onto which I’m building this scatter plot is the iris data set and again it’s very clear that as the seple length of the iris FL increases the petal length of the iris PL also increases linearly so here I’d have to load a the iris data set first so I’ll have pd. read CSV I’ll give it the name of the file which will be iris.csv now that I have loaded this let me show you guys the first five rows of all of the columns and this is the resultant and to create the scatter plot all I have to do is use PLT do scatterplot and since I want the scatter plot between seel length and petal length and I want seel length to be onto the xaxis so to X I’ll be using seel do length and on the Y AIS I would need petal length so let me give the name of the column over here which will be petal length and the data which I’m using is obviously Iris then I can go ahead and show out the result so let me just keep it as scatter over here and again this has to be equal to small D and not capital D let me hit on run over here and this is the resultant value now as you see over here we can go ahead and add in color and also change the style of this so initially we just had a simple plot between seel length and petal length but now I want these dots to be determined or the color of these dots to be determined by the speed species column so that is why I am mapping the species column onto the Hue athetic and after that similarly I am mapping the species column onto the style etic as well so here as you see we’ve got three different colors blue orange and green the blue color is being determined with the Sosa species then we’ve got this orange color which is for the wory color speci then we’ve got this green color which is for the vinica spey similarly we’ve got three different styles over here for setosa we have solid circles for ver color we have crosses and for vinica we have solid squares the same command which I’ve copied I’ve pasted over here I’ll add two more attributes the first attribute will be Hue and onto Hue I’ll be mapping the species column similarly onto style as well I’ll be mapping species Colum column and when I hit on run so I’ll have it as hu now when I hit on run seems like we have an error over here let me check it properly and this over here has to be just H now when I hit on run this is the resultant scatter plot which I get and now let’s say instead of having the colors to be determined by a categorical column if I actually want the color to be determined by numerical column I can also do that so here since petal length is mapped on the Y AIS and I want the color to be with respect to the petal length I’ll go ahead and map The Petal length onto the Hue athetic and as you guys see over here as the value of petal length is increasing the intensity of these points is also increasing so here on this lower left side over here we have all of this very light shaded circles and at the top right you have this high intensity or high intensity colored circles over here I’ll copy this I’ll paste it over here now I’ll want the Hue or the color to be actually determined by petal length itself let me keep the L to be capital and when I hit on run you would see that as The Petal length value increases the intensity of the color also increases let me just go ahead and also add the style over here so this time the style will be determined by the species column and you would see that we have three different styles for setosa this is setosa you have solid circles for wory color you have crosses and for vinica you have solid squares so this was about a scatter plot now we’ll go ahead and make a histogram or a distribution plot so a distribution plot you can consider this to be a combination of a frequency curve and a histogram and we have already worked with histogram where it came to matte plot lip we know that a histogram is used to understand the distribution of a continuous numerical value so for this we’ll be using the diamonds data frame so we’ll load up this diamonds data frame we’ll store it in this diamonds object and then we’ll have a glance at it after that will to create this distribution plot we’ll use disc plot and inside this I if I want to understand the distribution of the price column I’ll just pass it over here so I’ll have diamonds of price and as you guys see this would show us the distribution plot and as I’ve told you the distribution plot is a combination of a histogram and the frequency curve over here I’ll just add this comment and I’ll add distribution plot now over here to create this distribution plot I would need to load up the diamonds data set so I’ll have pd. read unor CSV and inside this I’ll be passing in the diamonds. CSV file and I will store it in this new object called as diamond now that I have loaded this data set let me have a glance at the first five records of this so diamond. head and these are the different columns which are present so I’ve got carrot which obviously tells us about the carrot of the diamond then we’ve got the cut type of the diamond we’ve got color Clarity depth table price so this is price of the diamond in US dollars so the price would mostly range from around $300 to around $188,000 then we’ve got x y and z X over here denotes the length of the diamond in millimet y denotes the width of the diamond in millimet and Zed denotes the depth of the diamond in millimet so once this is clear i’ have to make a distribution plot and since distribution plot is used for continuous numerical values I want to make a distribution plot for this particular column so I’ll have SNS dot dis plot and inside this I’ll be passing in Diamond I’ll have square braces over here inside this I’d have to pass in the colum which is price and I would just show this out I’ll have PLT do show and you would see that I have created this distribution plot over here now in the distribution plot let’s say if I want only the frequency curve without the histogram that also can be done it’s just that in the same command I would have to set hist to be equal to false and when I set his to be equal to false I’ll only get the frequency curve I’ll copy it I’ll paste it over here and I’ll add a new attribute called as H and I’ll set this to be equal to false and when that is done you would see the difference so this was a distribution plot which had both histogram and the frequency curve now this is a distribution plot which comprise of only the frequency curve now similarly we can go ahead and add a new color to this and to add a new color we’ll just use this color attribute and add this so this was the distribution plot which we had created and to this if I want to add color I’ll use this color attribute I’ll assign it R and you you would see that I have assigned it the red color now if I want a distribution plot without the frequency curve which would mean I want only the histogram then here I will set KDE equal to false so in the distribution plot either we can have both the histogram as well as the frequency curve or we can just have the histogram or we can just have the frequency curve to just get the frequency curve we’ll set this to be false to just get the histogram we’ll set KDE to be equal to false and we can also go ahead and Vary the number of bins which are present to vary the number of bins I’ll use this attribute called as bins and over here I’m setting the number of bins to be equal to 10 and I’m just setting the color to be equal to Green I’ll select this over here I’ll paste it and I’ll set KDE to be equal to false and when I do this you would see that I have only the histogram without the frequency curve if I want to change the number of bins which are present so since there are 150 records I’ll have 150 bins but instead of uh having all of this so let’s say if I want only 50 odd bins over here I’ll set 50 and as you guys see I have only 50 bins now let’s say instead of 50 maybe if I want only 10 bins so you will see that I have only 10 bins now let’s say if I want only five that also something which can be done I’ll set the value to be equal to five and we have only five bins over here and after this let’s say if I want to plot it on a different axis so till now we’ve been creating this distribution plot where on the x-axis or basically this was based on the x-axis but instead of having it to be based on the x- axis if I want to map it vertically then I would just have to set vertical to be equal to true I’ll have the same command over here and here I’ll set vertical to be equal to true I’ll remove this KDE equal to false from this I’ll also remove this particular retribute from this now if I hit on run you would see that I have mapped this distribution plot onto the Y AIS next we have a new geometry called as a joint plot plot so this joint plot is a combination of a scatter plot and a histogram so as you guys see over here I have a scatter plot in the center and I have a histogram at the top side and the right side and I’ll be creating this joint plot on top of this Iris data frame so once I’ve loaded this Iris data frame I want to create a joint plot between seel length and petal length so I just created a scatter plot so for scatter plot we are just use SC scatter plot method for joint plot it’s just that we have to use this particular method we are passing in the same columns now when it came to scatter plot we had only this particular part but when it comes to Joint plot here what you see for seple length you would have the histogram of the seple length column as well similarly for petal length you will have the histogram for petal length column as well so this is an interesting point about joint plot we already have loaded the iris data frame now I’ll have to use SNS do joint plot and onto the xaxis I’ll be mapping seel do length and onto the Y AIS I’d have to map something so onto the Y AIS I’ll be mapping petal. length and that is pretty much it I actually have to give the data as well so the data on which I’m making this is the iris data set then here I’ll have PLT do show and when I hit on run so seems like we have an error over here so Seance so L has to be Capital let me make it capital L and you would see that I have successfully created this joint plot where I have this scatter plot in the center and I have corresponding histogram for seple length on the top corresponding histogram for petal length on the right side and over here if I want to change the color that is also something which can be done and we’ve already seen this throughout so all I have to do is use this color attribute and I have to give in a color for this and if I like the olive color that is what I’ll be going ahead and mapping it onto this attribute I add this color attribute and I’ll have Olive and as you guys see I have mapped the olive color for this joint plot now for this if I want a regression line through the scatter plot and also through the histogram I’d have to use this new attribute called as kind and for this new attribute kind I am assigning this value rig so as you guys see I have this regression line which is passing through the length values and The Petal length values and also it is passing through both of this histograms so here I’ll have kind and I’ll have the value reg set for this kind attribute let’s wait for the result and you would see that I have added a regression line which goes through the histogram and also goes through the scatter plot once I have done this let’s go ahead to the next geometry which is a box plot and to create a box plot we’ll just be using this box plot method and we’ll be creating this box plot on top of the churn data frame so we’ have to load this data frame first so this data frame tells us about the different features of a telecom company and on the basis of this features we have to find out if the customer will churn out or will stick to the same company so here we have the churn column and the tenure column the Chon column I’m mapping onto the x-axis the tenure column I’m mapping onto the Y AIS and obviously I’m building this box plot on top of the churn data frame uh one interesting attribute about a box plot is that box plot can be mostly used to understand how does a categorical value change along with a numerical value so that is why here we have mapped this categorical column churn onto the x-axis and this numerical column tenure onto the Y AIS and we see that so this what you see is the median value which we have already seen so when it comes to people who do not churn out it seems that those people who do not CH out their median tenure or their tenure in general is longer than those people who actually churn out so I would have to load this data frame first I would have to store it in the ch object so Chan is equal to pd. read CSV and inside this I’ll have churn do CSV and then let me have a glance at the first five records of all of the columns and these are all of the columns and for all of these columns I’d have to make a box plot between this tenure column which would go on the y axis and the Shone column which would go on the x-axis SNS do boxplot and onto the xaxis side have to map obviously the tenure column onto the y- axis I am mapping the churn column then I’d have to use the data which is Chun and I’d have to show it out so I’ll have PLT do show so I actually made a mistake over here so tenure will go onto the Y AIS and Chon will go onto the X access now we get this box plot and we’ have already seen the inference it seems that those people who do not shown out this their tenure seems to be longer than those people who actually Chown out and now this time we’ll be creating a box plot between the internet service column and the monthly charges column so we have the monthly charges column onto the Y AIS and the internet service column onto the x-axis and we’ve got these three different categories in this internet service column so the internet service can either be DSL fiber optic so this no means that the people don’t have or people haven’t subscribe to internet service and it is very clear that those people whose internet services fiber optic they would have the maximum monthly charges similarly those people who do not have internet service their monthly charges is minimum so when you compare this box to these two boxes it is very evident that people who don’t have internet service their monthly charges are very very low I’ll copy the same command over here but on the yis I’ll have monthly charges and on the X access I’ll have internet service and this is the resultant box plot which we’ll be getting now we’ll go ahead and make another box plot between the contract column in the tenure column tenure column would go onto the y- AIS contract column would go onto the x-axis and we can have three different types of contracts month-to-month contract one-ear contract and a 2-year contract and if we look at the tenure of it so let’s actually look at the median values of the tenur so it seems that if the contract is of month to month then the median tenure is the lowest similarly if the contract is of 2 years then then the median tenur is the maximum and we are setting a pallet over here so again we can have different pallet so the pallet which I’m using is equal to set one onto the Y AIS I’ll be mapping the tenure column onto the x axis I’ll have the contract column let me write in contract column over here and uh the data will obviously be the churn data set and what I’d have to do is I’d have to use a pallet and the pallet which I am using is equal to set one and this is the resultant box plot now you see these boundary lines over here if we want to change the thickness of these boundary lines we can do that with the help of this line width attribute so here I am setting the line width to be equal to three if you compare this particular box plot with the earlier Bo plot it is very clear that the thickness has increased the same code I’ll actually have tenure and contract over here onto the x-axis I’ll be needing contract and I will use this line width and I’ll set this to be equal to 3 and you would see that the line width has increased substantially and after this let’s say if I want to change the order of how these boxes are present so here initially the boxes were present in the order of month to month one year 2 year but instead of this order let’s say if I want 2 year first followed by month to month then after that if I want one year then I can use this attribute called as order and inside this I’ll pass in the list which will comprise of the order in which I would want these boxes to be present so here I’ll remove the line width and instead of line width I’ll use this attribute called as order and first I’ll have two year after that I’ll have month to month then finally I’ll have the last box which will be one year and you would see that I have changed the sequence now if you want to add colors on the basis of a column which we’ve been doing throughout that can be done with the help of this Hue attribute and I want the color of all of these boxes to be determined by this payment method column so here I am mapping this payment method column onto this Hue athetic and as you guys see I have these four different payment methods I have electronic check mail check bank transfer and credit card so electronic check so this particular box or where all of these boxes where the color of the box is purple or dark blue that denotes electronic check then we have the box where the color is orange that would denote mail check and if the color of the box is green that would denote bank transfer and if the color of the box is readed that would denote credit card so here I’ll add this attribute called as Hue and I want the Hue to be determined with the help of this payment method column and you would see that I have four of these over here so I have four boxes with respect to these three different categories ready to explore the Forefront of Technology generative AI is our next STP we will demystify how AI can create new content and show you how to implement these Advanced models using python think of a magical box that could materialize everything you could imagine a box that can create a new video write you a story draw a lovely picture or even record a song it sounds like something from a story book isn’t it well it’s generative AI not magic hello and welcome everyone to this video where we are going to examine how this incredible technology functions how it is changing the world today are you prepared to discover ai’s magic let’s get started [Music] now so let’s start with quickly understanding the evolution of computers when the computers were created they were created as calculating machines for mathematicians and bookkeepers then then it evolved to understanding programming languages so that it can understand instructions human instructions but now it has evolved too incorporating humanlike intelligence as well as creativity mimicking humanlike intelligence is nothing but artificial intelligence and artificial intelligence combined with creativity is nothing but generative AI so let me make you understand with a very simple example what is generative AI transport yourself back to your childhood you had a lot of lot of toys to play with you would keep that toys in one box now also imagine that if you wanted some toy which is different you would not get in the market but what if I tell you that this box is a magical box and if you input your under understanding of what you want in your new toy with instructions it can create a new toy for you which is not available in the market now this toy can be a beer with unicorn features and wings what if it generates for you this magical box generates a toy which is very unique for you this magical box is nothing but generative AI generative AI actually is not a magic it’s a fast and rapidly evolving artificial intelligence system which creates generates transforms content that can be text video audio image Etc based on your input so if you want to understand it technically generative AI or gen aai functions by employing a neural network to analyze data patterns and generates new content based on those patterns neural networks are nothing but a mimicry or a replication of your biological neuron based on how it gets from brain the activity from brain and you do your work it’s nothing but a mimicry of that based on that mimicry it analyzes data patterns and generates new content for you let’s Now quickly see what is the difference between discriminative and generative AI suppose you have a data set of different images of dogs cats you provide this as a input to your discriminative AI which acts like a judge and it classifies all this into set of images between cats and dogs this is discriminative AI it classifies now let’s understand what is generative AI you have the similar set of cats and dogs but now your generative AI is acting like an artist it creates a new species of dogs for you that’s why generative AI is nothing but AI system that transform creates generates your own content based on your instructions like an artist now that you have UND OD what is discriminative AI and what is generative Ai and what is the difference between the two let’s understand why is generative AI or gen AI trending gen AI has impacted various Fields be it text audio video any input and those inputs in various domains like data management Tech Healthcare and entertainment it has creative applications such as as di chat GPT where you can input what you want and get output from it for example if you want to create an image what you think or perceive as a concept and you want it you give a prompt for your generative AI model and it’ll create that image for you so your input is a text but your output is an image that’s why it’s trending it does not depend how traditional AI is dependent on what form of input you give the same form would be your output however gen AI works on your inputs on your instructions that’s why it’s trending it is impacting a lot of fields be it creative field be it research field be it business professionals are using tools like chat G to create or generate code so that they can create something new the researchers are actually developing new and new large language models based on which we can create new generative models and can do new and new task each and every day that’s why generative AI is evolving rapidly and that’s why is close to Magic for everyone now that you have understood why it is trending now let’s understand how it it works we give an input to generative models gen AI works on generative models we give an input it can be text audio video any format those generative models are then preened on the data and they are fine tuned to do the task that you want it can be teex summarization it can be sentiment analysis it can be image generation it can be audio generation for your YouTube channel or analyzing your customer feedback if you are a brand or a marketing firm it can create codes whatever you want you give a prompt what you want explaining it that what you want and it fine tunes and gives you that task for you so this is how in nutshell generative AI model works so now let’s see what are the different types of generative AI first one is generative adversarial Network Gans it’s a type of AI where two models one generating the content and one judging it work together to produce realistic new data second is variational autoencoders this AI learns to recreate and generate new similar data third is Transformers Transformers is an a a i which learns to produce sequences using context fourth is diffusion model which generates data by refining noisy starting until it looks realistic now that you have understood what are the different types of generative AI let’s quickly walk through different applications of generative AI first one is content generation it creates it generates whatever textual or any code that you want customer support and engagement if you are brand firm it helps you with that data analysis and data science it helps with visualization it helps with analyzing any data it be it any data you want you are a brand firm or you are a technology firm it will help you analyze your data and create new automated task for you or it would create new perceptions for you to take over then it is code generation and software development we have research and information retrieval as well where it helps different researchers it helps different professionals to grow and retrieve extract information required from different or various data sources then we have machine translation if you are a person who do not understand a language and you’re watching something or reading something which is in different language which you can use generative models to translate text or audio or anything into the language that you require then we have sentiment analysis which actually takes feedbacks or any text that you have to give you is it a positive negative or neutral sentiment and so that you can analyze and take decisive decisions other domains here include Health Care transport everywhere it helps generative models generative AI is helping each and every domain in their perspective how they are applying this technology change in their domain so let’s Now quickly conclude what we have learned in this video we learned AI is a super set we have a subset called machine learning which trains your machines to do what you want but there the machines need your for input deep learning is a subset of machine learning which incorporates neural networks which mimics your neurons so that it can imitate human intelligence then comes generative AI which involves creativity introduces creativity in artificial intelligence system then comes large language models large language models are basic bits that you will learn in our upcoming videos so stay tuned just before we conclude let me tell you different generative AI tools which are in the market and which you can explore some of it are charity by open AI clae by anthropic AI co-pilot by GitHub Gemini by Google you can go and explore the world however stay tuned to our channel so that you can learn more about generative AI large language models prompt engineering and several different buzzwords that are there in the system let’s start with the first topic overview of python when you hear the name python you know the various applications of it first and foremost thing it is a high level programming language which is very unique compared to other high level programming language why almost it will use English like statements in order to execute the code it’s very easy to learn as a beginner this particular Python language now why do we use python in generative AI it’s not about generative AI it’s about python is already having a well supported set of libraries which is already in use since years with respect to domains like data science machine learning natural language processing deep learning Etc now artificial intelligence and generative AI is grabbing the libraries which we have already in Python other programming languages are also used but I could say python is a versatile programming language which makes life easy for the people working in this technological domain after understanding a overview of python let’s quickly hop on to the next topic introduction to generative AI applications which is the core concept which we have to learn generative AI refers to algorithms which enables machines to produce content that is not only new and original but also includes a reflective data and it will be always trained according to the requirement right generative AI deals with a lot of models what do these models include G that is generative adverse networks vaes variational autoencoders and Transformer based models such as chat GPT right what do we do with this generative AI applications it’s very important in order to train the algorithm or the machine in order to keep it updated the more you interact with this the more it gets trained that’s how simple it will work and generative AI helps you to generate your own models how you want to train that particular model you can train it accordingly just like simple example how does the scientist train the robots each robot will do its own different work right hope you would have seen the requirements are different the catering of requirements is different hence the models will be trained accordingly with the help of generative AI yes it includes lot of other Technologies deep learning neural networks Etc but still generative AI is also a base of it what is the significance creativity boost it creates enhances processes by providing very good content ideas new content ideas the new way to approach the problem efficiency it is giving a helping hand to human beings in order to be more efficient the more good you use the more productive you’ll be automates the content creation or saving time is very much important it’s a important resource now it aids to this particular Saving Time resource then personalization it generates particular personalized content as per your requirement as per the prompts you give to chat GPT that’s how it works right so it will cater various applications for the same this is the overall picture of generative AI applications now let’s talk about the next concept development environment setup how do we do this what is it all about you have to have a platform in order to work with you need to have a basement in order to build a building right so let’s learn how to to we build this particular basement so what does this thing consist it consists of few steps in order to set up a particular environment it is not dealing with much higher softwares or something from moon and stars it’s very simple you have to go to python official website and download the latest version for now it is 3.12 you can download that python into your local system system and you can execute this via command prompt right first step we have to open command prompt which we have in our local system and then we’ll have to navigate to the location where the python is installed hope everyone are having a clear idea of how do you work with Linux and Unix at least basic commands like CD change directory MK make directory so only these two commands are mostly used in this complete session I’m not going deep into advanced level of Unix Linux and all if you want to work with a command prompt you have to just use CD and MK command and you can make directory or change directory simple as that if you’re using Windows you can use command prompt or Powershell for Mac OS or Linux you can use terminal this is the platform you need in order to work with after navigating to the location where the python has been installed you can install all the libraries using pip Command right so pip install is a basic command and you can change the libraries you want accordingly first we will talk about numai numai is very well known amongst the domain called data science why the first thing is numai will always cater in order order to help the mathematical calculations also working with high level data structures and give you the complete access to the functionalities and arthema and Logics that is why data science is dealt with lot of data numbers and other elements we use numpy for the same then we talk about plask when you hear the word flask it is a library which is related to python where it is web based framework you can create web application using this particular flask framework that is the major help of using flask the next one is stream lit or stream liit this particular Library deals with visualizing the models created then you have torch torch vision and torch audio basically this Library cats computer vision models you can work with the model creation you can view the model and also you can add certain multimedia to the model created right you have this torch library in order to cater computer vision projects models Etc you have Transformers next Transformers will always help you in classification text summarization many other aspects again dealing with data and majorly we use all these liaries in machine learning artificial intelligence NLP natural language processing and deep learning also computer vision this is the applications of where this particular Library will be used using pip install we are always installing all this Library single-handedly not in Mass Library installation every Library will be installed along with the execution output command stating it has been installed it it will show you it is installed now still you don’t trust how do you check it verification of the installation is very important because since we are working with machine sometimes it might help you to have a better Vision when you verify if you don’t verify if the installation is crashed you never know it will affect your project so better verify once you install it’s very simple in order to verify as well you can open command prompt and type python double hyphen version it will ensure it Returns the installed python version what is this particular version you’re working with next you verify the installation of the libraries for that you have to just go open python interactive shell type Python and then import every Library which you have already installed if it is imported properly without any error then it is installed properly right so this is the overall development environment setup idea which you have to have and which you have to create in order to do coding in order to create certain applications or work with the project now we are in the command prompt in theoretical aspect we have known about various libraries in Python that is numai flask streamlet T Transformers let’s install the same libraries with the help of command prompt first if you could see it’s in a general path it’s in my personal path but yeah it is in C drive now in my personal laptop the location of the Python is being fetched for that I have to use CD command change directory paste the location where your python has been installed and then press enter when you do this the command prompt goes to this particular folder let’s start with the First Command pip install numile now since I’ve already been working with python a lot many times for many projects you will get a output just wait and watch I’ll click on enter it might take some time it will try to analyze what’s happening what they’re trying to install and requirement already satisfied this is what you’ll get the output that means numai is already installed in your particular system because we were already working and there is a warning message you could notice if you want to upgrade the particular Library which you’re using you can go for the version mentioned I am currently using 21.2 point3 it is suggesting upgrade for 24.1 point2 then what is the command for the same is also being mentioned here you can use that command we have now installed numai which was already existing it is given the message if in case it’s a new installation of Library how it will display let’s try it other libraries as well pip install flask and I’m giving enter let’s wait for the results again if you could see it states flask is already present that is satisfied again you have a warning regards to the version I have almost installed all the libraries but let me check for the next one stream lit if you could see how it is done downloading the streamlit library if you’re trying to install the library which is not in the current local system in your python this is how it will start loading if it is already existing this is the message which you got for numai and plk when you try to install streamlet Library which is not present in your python this is how it starts downloading and it takes 5 to 10 minutes at least to complete the download depending upon your system configuration likewise you can install all the libraries required for you into your system right so I have given two examples one how it will download the library which is not in your system if you already have downloaded the library how the message will pop up that is requirement is already satisfied that means it is already installed right so this is how you import your libraries in Python now in order to verify is your particular library is installed or not first it will try to prompt you that it is already existing if it is not it will start downloading as mentioned now again in order to verify that you have to go to python interpreter so I’ll click onto the same particular location type python here when you click enter it will go to the python interface where you can execute your code now what you do is you try to import numpy when you try to give this particular instruction to the python prompt inside the command prompt which we have logged in it will try to enter or import this numpy Library which is already existing when you type the statement import numpy if your numpy is present it will not throw up any error it will look just like this this indicates your numpy is there in the python Library folder this is how you verify the libraries which is already installed before using it or else if you mention in your code as well it will throw up an error if it is not installed before make sure you install the libraries then use it in your code this was a simple demonstration how you install and verify if the library is present in your python with the help of command prompt now let’s understand introduction to open AI GPT API how this particular thing works and what is open AI first of all what is open AI it’s a company where it will cater in order to work with chat Bots generative AI applications different kinds of models llms Etc basically it is dealing complete artificial intelligence domain which is booming nowadays open AI has a platform where you can generate the API keys and you can integrate those into your applications API features what are the features it will cater for text generation completion and conversation capabilities so talking about text generation it is always dealing with providing a new text which is not in your imagination with one small question say I want a poetry on so and so it will give you a complete poetry where it is not plagiarized it has been trained in that level it can think about writing poetry it has lots and lots of data behind how it is dealing with with that what is to be categorized there comes classification summarization and many other machine learning and data science models artificial intelligence models which is giving you the answer pre next completion if you give a prompt in a incomplete way it will try to complete that if you give with a spelling mistake it will correct your spelling and ask you back was this your IDE media do you want to search so and so thing it will question you back in a interactive conversational way what we do with chat GPT it’s the conversation how it will answer us with the help of already available data it has been trained on and it is updated every time you talk to it that’s how the model works with so these are the few features we have next comes to fine tuning and customization for specific task say you are building certain module which has been integrated to your application you’re using open AI platform you can

    generate your own model it can cater to your own set of questions say for example chat bots in some or the other shopping websites or juary shop websites it will try to ask you what you want the lots of chat Bots which will address and also will help certain percentage of customer care services without human Intervention which can be done with the help of machine 100% it will be solved other aspects it cannot a very good example for this is swiggy right you can give a set of questions which is already present where is my order delivery guy is not moving so when you put this my order is getting delayed it will give a set of answer which is already there what is the current status still if you’re not convinced by the bot answer you can go for a agent talk you can talk to a human being where they’ll interact they’ll call the delivery guy and ask what is the situation and update you something like that might happen before introducing your particular chat to directly to the agent they’ll try to solve with the help of Bot that means we are trying to reduce the work which is put upon humans we are using the technology in order to address the same this is a best example for the Fe feat which is currently in use in the apps which we use in our day-to-day life how do we get start with this API we have to just log on to open AI website create your account sign up or if you already have an account sign in login generate a API key and keep it why you have to generate a API key I’ll let you after $5 of content you have to pay in order to improvise your API key in order to improvise your API key usage right you want to know more about what is open AI how does it help for GPT API everything you can just go to the official API documentation and understand more about this now let’s understand how do we generate a open AI AP a key for that you have to go to Google type open AI login once you click on login if you have already logged in you will get two options one is to go to chat GPD another one is to for API you click on API once you click on the API this is how your open API platform look like you could see a menu here towards your left stating API keys if you click on that it will launch API keys before that I would like to tell you I was talking about the GPT models right so these are the models available for now GPT 3.5 turbo 0125 d106 and 16k these are the models you can select the model and you can work on let’s come back to API keys I’ll click on the API keys this is how the API Keys generation look like and if you want to create a new API key click click on create new secret key and you can name that particular one I’m naming it as demo you can also give the restrictions if you have to control certain things it can be a readon restricted or all just like the share option you have in your Google Drive for your Google content right so create a secret key and it will generate and display the secret key there you can copy and paste it in one particular notepad so that you can use it again and again it is taking certain time to generate the key once it is done it will display and you will also have an option called copy for it here we are it states API key is generated and you can copy the key you can press as done see you have to save the secret key somewhere because it won’t be viewed again due to security reasons that’s why you have to keep it discrete and noted in a notepad separately you cannot get it back again if you want to again you have to create a new API you cannot copy this complete API key again the created API key is listed in the list here again you have the options to edit the key you can just change the name and permission nothing else you don’t have access to again copy the complete key and you can also delete the existing key this is how your API key Page look like in open AI platform hope you are clear how to generate these and save it in a place and use it for your coding now let’s talk about flask chat gbt app we are integrating open AI source to our application that is the main agenda of it let’s understand more about this application how do we work with this and also look at the demo for the same what is the basic setup we need is as simple as that which is mentioned before you have to have python installed in your system and all the libraries mentioned to be installed in your system that’s the basic ideology for all the demonstration which is carried in the session Hereafter the components we need is flask for web framework as I mentioned we use flask of python library for web framework and then open AI GPT API for generating responses a simple logic we are taking a API key of open API putting that in your python code and then we are trying to execute the same first we will check how this particular code look like and what are the in detail step in our Google collab note I’m not executing this in Google collab I’m executing this in command prompt but for a better Clarity I’m using the online coding platform that is Google collab in order to have a good interactive and bifurcation between the text and the code right Google collab is a very good place in order to work in order to have a good python content on let’s check out the code and understand what all does it do in order to create chat GPT app using flask library in Python now here we are on the Google collab first step we have to is set up the environment as I told we will activate the virtual environment of python what is this python M V and V Let’s understand one by one python invokes the python interpreter which is already installed in your system then MV and V this option always tells python to run VV module as a script this module is used in order to create virtual environments on that that is why we try to invoke this next again you have V EnV this is the name of the directory where the virtual environment will be created and it is not mandatory that you have to keep the second V en EnV as it is you can change this to be ABCD also or you can also put it as virtual environment it is not mandatory that you have to use the same name but with M you have to use V andv this is mandatory and the second V andv is optional you can change the name accordingly naming convention can be changed according to the requirements next after doing that setup we create the flask application as I told you we are using only two commands of Unix since we are working in command prompt first is make directory M KD r that is we are creating a folder with the help of command prompt that’s it there is nothing great that’s happening creating a new folder the folder name is GPT chat app and you are trying to change the path change directory CD to that particular ular location and remember wherever you have installed your python software that folder itself these things to be created we have to first navigate to that particular python location then only we can create a new folder else there will be a execution problem and path issues after doing the folder creation we will have to create code file first is a python code file which which I would like to name it as app.py application.py again this is not mandatory you have to have AP you can name it accordingly but you have to remember what you have named while you’re executing this you have to remember the exact python file name including the cases it is case sensitive okay first we will import the flask elements first is flas library next request Json and render template then we will import the requests and also import time these are the libraries we’ll try to import which is available in Python to our particular code we will try to use request and render template also the timing in order to have the conversation between the system that is Char gbt which we are trying to create and our questions then we will initialize the flask application this is the flask application initialization syntax we will try to give a open API key this is a secured key which you should not share with anybody else or else they can utilize and you have to pay the bill for the same better you keep the API Keys very discret this is a random API key it’s a sample API key or else you can just put in your code enter your API key here this is far better rather than giving people your original API once you put this API key you will next Define the root of the homepage where it has to interact from obviously you cannot show this backend code to the user you have to have a front end you have a front end that is called index.htm there comes the second core file first one is having ap. py core python file next it has to be integrated to the front end that is index.html right we will render the particular HTML template that is why we’ll be using render template Library okay we have usage of these libraries everywhere then we’ll Define the root for chat end point which accepts the post request post request is nothing but what message you put to the GPT and what it has to respond back and this complete thing will happen with the help of Json then you get the response from gpt3 and remember there’s lots of GPT models which one you’re using you have to have the knowledge about it you have GPT 3.5 turbo 16 K you also have just GPT 3.5 turbo you have many kinds of model the current model which we use you have to mention it here and you also have to carry the input given by the user from the front end to the back end with the help of this messages the data should be transferred from the front end to back end what is the maximum limit of the response is 150 letters characters it’s not words okay it is very minimal if you want to make it more obviously you can make it 300 again it is according to the requirement you are curing for then you have to attempt to get a response from API which tries in case if it fails now comes the word of error handling the code which is not having error handling capacity is not a worthy code simple as that if something goes wrong first you have to let the system give you the message that something is wrong not directly land to a page it should be interactive and it should tell the user whatever you have entered is wrong or something has happened what has happened this particular responses should always be there for example I’m giving you possibilities it’s not that every coder will be knowing everything what they have to do right but still there are few standard error handling techniques when you work you have status code 200 when when this particular 200 status code comes 44 error comes 429 comes right how do you handle that what is the error what is the particular response you give for example if it is for 200 we can return the message to the user rather than going to a random wrong page you have to give a message an error occurred while processing the response from open API that means if your GPT is not connected prop it is not able to give response then you have to just not push that particular code to a error page you have to send an error message you have to tell this next you have 429 here we are trying to request open AI if fail status again we are trying to reattempt how many times back off retrying attempts are two we’ll try to do two time attempts and then we will go for sleep that means we are putting this particular system into sleep that it is not able to solve the more you work with it the more limit exceeding will happen and 429 also deals with if your particular open AI is out of limits it is not having any um limits left it is exceeded you have to buy new you have to put your building again it will say you have exceeded your current quota please check your plan and building details so that’s how you have to try to give error message to the user so that they understand something is happening we have to go and address because nobody will go to back end and debug what is the error right in the front end itself you have to show what is happening so this is sample example of error handling then if any other error comes more than this there are two errors which have listed if anything else comes up you have to just give the status code directly 4 not4 error or Internet disconnectivity error anything might come an error occurred while communicating with open AI is a standard default error message you can send if you don’t know what you have to do just put there is an error please decode then comes run the flask application this is the main method which we create for this code and the code execution starts from the main method here now comes the second part of it the front end which we had discussed already there and what are the complete content of that we’ll have a quick overview that is index.html here you can see people who know HTML will always know this doc type HTML you have head you have HTML Lang English and meta character set is always there you have style for your particular page and you also have the body here you have a chat box you have a text box you want a button it’s a simple thing you have to have a text box where the user will put their input click on send button so that it will interact the chat gbt in order to display the chat GPT message you have to have a label or again a text box so that’s how it will work it’s a simple JavaScript which is been used in order to have this interactivity that is fetching the information from the input and putting that to GPD and taking the response from the GPT and putting back on the front end to view for user so this is the simple fundamental function that happens in this script section how do you run this just you have to be in the location where you have created the folder that is GPT chat app right I’ll just go back and just give you overview GPT chat app that is the location where the command prom should be pointing out then you can execute python ap. py when you do this you will be able to access a browser where it is loading in this particular address what is this address why only 5,000 why not 4,000 you might question as you all know HTTP 127.0.0.1 will always deal with local hosting when you do this local hosting you have separate ports for every library or every kind of execution you do 5,000 is the port number which is allocated in every local system for flask library of python any flask web framework code execution you do it will launch on the Chrome with this particular address hope you had a complete detailed view of how this particular application will work a quick cap you have to install Python and necessary libraries then you have to create a main python code file that is app.py then client side that is frontend interface you have to make it index.html again this is as per your requirement this is a common name which we keep that’s why I’ve used the same you can create a simple HTML interface to interact with the chat GPT then you can run the application and check for the output right so now that we have understood what is the code at the back end front end and every aspect let’s execute this code and check for the output before going to the demonstration of flask chat GPT app let’s understand the folder structure I am here in the location where my python is been installed since we’ve already worked on you could see many folders here we are trying to create GPT chat app right according to our steps we’ve already done it so if you go to this particular folder you can find two different elements one is templates another one is app what is template template is actually the index file which we had discussed the HTML file app is a main program once you execute this the back file will be created if the execution is successful or not successful doesn’t matter once you run this through interpretor it will generate automatically that’s why it is present if you’re executing for the first time this will not be there okay now let’s hop on to the command prompt and check how does we work with this here we are in the command prompt and in the location where our file is am I right no I’m actually wrong we are in the location where python is now we have to enter to the folder created what is the folder we created we have to change directory to that particular folder GPT uncore chatore app this is the folder name which we generated right let me enter to that see now we are in that folder how do we execute the steps which I mentioned we have to type Python and we have to mention the app.py or the name which you have given to your main python file you have to mention that and click on enter once you click enter after executing this app.py file this is how the output looks like are we in the right output screen no it is just indicating that it is been executing it is running the location is we have to go to http 127.0.0.1 5000 the port number let’s quickly hop on to that location on our search engine any browser you can use you can go to this particular port number let’s hop on to that once you go click on HTTP the same ID where it has been launched across you will find the interface now what you have to do you have to communicate with the GPT so I’ll press hi and click on send button it will say hello how can I assist you today the next question which I ask is how are you when you do this when you send this and just a computer program so I don’t have any feedings but thanks for asking how can I help you so this is how it is trying to interact with the human being if you try to give something which is not existing still your chat GPT is not trained to that level it is a normal basic model I’ll say where do you leave I click on the send button you have exceeded your current Kota please check your plan and billing details it will not throw up this error really if your limits are exceeded right that is when it will show this error it will try to do that error handling which I’ve already mentioned so for the third conversation itself how did we get this message you might be having this particular doubt the thing is open API API key is not very much free to everything you only have access for five worth of conversation that can happen API key that can generate that is how you can converse after it exceeds $5 it will try to ask you to fill up and select the plan and do the billing right the payment should be done for the same so this is just a simple example you can enhance create you can buy a paid version and start building the projects and help your small scale business if you own any in order to have a private chat bot so customers can interact without any actual agent service required customer service you need not take it you can use the Bots there on your website right this is a simple idea this is how the execution looks like by now we have understood how does chat GPT while using flask how we can execute what are the code required and how the outputs look like now that we have understood and also saw the demonstration how how does a chat GPT app work when you create with the help of flask Library using python now let’s check out the next topic using the same flask how do you use text to image application here the simple idea is text to image generation involves creating images for textual description using AI models you will give a simple description here we are not focusing on description we are trying to get a image for the word which we give as I told cat dog any animal or what you want to fetch for significance of this particular application enhances creativity and design processes useful in various Fields like advertising entertainment and virtual environment say you want to uh get an image you can give a description cat which is sitting on a mat or dog which is sitting on a bed you can certain description you will get images in certain way or sketched cat image drawing of a cow so you can give a certain description to AI it will generate back the output for you how do you implement this particular text to image app first is we build a web application that converts text description into images again if you want to build a web framework it is about flask then you use open AI again HTML CS is for the front end that is very much mandatory and basic what are the prerequisites you want for this first is python to be installed in your system next you have to have a required library that is flas and open API and you have to have a API key from open AI this is the basic requirements it should have in order to start off with the development of this application now let’s understand what is the code for this particular app what is that purpose and what all we use here then later we will execute this hope we are clear now let’s quickly hop on to Google collab understand more about this application here we are on the Google collab first step if your python is not installed install your python if already exist ignore simple as that again create virtual environment we already had the description about each every element of this particular code statement then we activate the virtual environment by using this particular code here next comes installation of flask and open AI it’s very important to install the libraries which is necessary for your coding first place we’ll be using pip command to install flask open AI it’s a simple statement here the code line you can just execute the same then we have to create the project directory again nothing but the new folder it is named as flascore textor 2core image if you want to put some other name it’s left to you you have to go to the folder which you have created then only you can start creating your python code file and HTML code file first thing is main python application code file which is again named as app.py it gives you a proper signification it will not mix with the previous one because the folder is different so again we have to import the necessary libraries we initializing the flask application you have to have a open API key you can replace this your open API key into your original open API key how do we do that then you have rooting which has to go that is index HTML in this HTML file you have all the designs related front and related that has been fetched and you will be rooted with the help of function call generate image as a post method we’ll be using Json post method means the response which you get from chat GPT right either it might be your user input also access a post and also the response will be also post use open API to generate image based according to the prompt which is been received the size should be only this much and the number of images generated at on should be one only then the prompt which is given by the user will be pushed to open API it will get a response and then that particular image will be displayed if you want a detailed explanation of what is every line means I have it for you you can just read it what are the different elements we use and why do we use right next you have to create this HTML interface that is the front end again you have to have a text box a button and where you print your prompt and then you will put that inside the GPT it will fetch the output on the same screen right you have to have a simple text box and a button that’s it if you want to do more styling you can more welcome use CSS files and you can do it this is a general basic setup or the front end which you need and you have a script again your function call generate image here what happens it will fetch the information the prompt from the user and then it will put that particular prompt to open API once you get the response from open API it will push back the response on the front end this is the code for the sing right it’s just an interaction code between the front end and back end we’ll be using JavaScript this is how about the HTML file we’ll be using again if you want to run this code you have to type python AP . py and you have to be on the same folder where you have created at the start if you go somewhere navigate to some other location on your command prompt and if you try to give it will not execute let’s have a quick recap here first is we’ll be installing python next library is called flask and open AI later we will create a flask application that is app.py then we will also try to include the functionality of converting text to image that whatever the text we have given related image will be provided so we have to root for that and you have to have a simple HTML interface in order to have the connection between the user and the system then you have to access and run the code start the flag server and again you have to go to the same location that is the same IP address which ends with the port number 5,000 so this will be your particular location address it will run there you can execute the same now let’s quickly check how does this work in our demo now here we are in the python location of the local system we are trying to execute flask text to image app if you try to go to that particular folder you could find the same folder structure it is having a main python code and then you also have index in the template right once it is executed back file has been created so that is why they are here this is the structure now what we have to do go to command prompt type this particular location and try to execute with the help of python app.py command so I’m copying this location completely going to command prompt and changing the directory to the copied location now we are in the folder flask text to image application which has been generated we straight away try to execute this with the help of python app.py command once we do that we will click on enter this is where the flask server is been running it is active now we have to go to the location which is mentioned right here with 5,000 port number let’s hop on to that particular location once you come to this location you could see the basic HTML design which we have made and it’s our time now to give certain description regarding the image and try to generate the image I’ll give just one line of description if you want you can in detail description so that the GPT will give you a right perfect required image as per the command I’ll try to give mountain with skylight okay so let’s mention color also that is that will be good so mountain with green Skylight this is my description of a image which I need I’ll try to generate okay this is how we got the image from the GPT it has given the lights which is in green color on the sky and mountains are right here so this is how the descriptions will be taken care of the more precise description you give the more precise image you will get as an output so this is how text to image application will work with the help of flask open AI in your python which also help to generate the images for digital content creators or any kind of creative people who work in that particular Feld let’s understand how does Lang chain apps work what is Lang chain in Python overview of Lang chain Lang chain always streamlines the development process of the application and utilize the llms by offering a extensible architecture generally simple words langin is a library of python just like numpy and others okay it supports wide range of use cases generally we use Lang chain in order to create assistants chat Bots and many complex NLP tasks and data analysis aspect this is the application of using Lang chain then the framework is built with highly customizable Interiors of the code and then we also develop to tailor specific needs and we also integrate the same tailored code or module to the external data sources using API keys this is the simple work which we do with the help of L chain app it is just that we are using this library in order to create good model which acts to be a chatbot or personal assistant or any other requirement will be catered with the help of L chain application now in order to understand more about this we will try to know the case study for this Lang chain app let’s Explore More again I’m going towards Google collab let’s understand the code and later part we execute that in the command front here you have to note one thing we are not going to the Google Chrome where the IP address ends with 5,000 we are executing this in the command prompt itself this is one of the differentiation between what we have already seen and what we’re going to see let’s let’s hop on to Google collab now here we are on the Google collab what we are trying to understand what is the case study first thing personalized story generator that means it will try to take certain inputs from the user and try to generate the story for the user this project will take inputs might be the character names settings and theme of the story it will generate unique story every time you try to communicate with chat GPT 3.5 why I’m struck with 3.5 there is four and 40 that’s coming right but when you go to open API it’s still at the 3.5 version itself it is having 16k 1105 there is some other codes that’s going on it’s just a turbo GPT 3.5 you have many kinds of models but for now it is 3.5 in open API platform not talking about the chat gbt 4 or 4.0 okay don’t get confused with that steps in order to create this project very simple set up the environment collect the user inputs generate story using AI model and display the generated story simple as that first when we talk about the installing of libraries here we have to install open AI Lang chain it is related to open AI also related to python so pip install open Ai and L chain both of these libraries we have to install then in order to collect the input from the user you have to have two different python files here so that is the differentiation previous demonstration we had only one python file one front end file we used to work with the code here you have two files what does this do first one says user input. py that means we are trying to welcome the user and take the input from the user here you could see welcome to the personalized story generator you have entered the main character’s name please enter that then enter the setting of the story and enter the theme of the story for example Adventure mystery horror anything as such return the character setting theme to the particular file called story generator. py you are taking input with the help of one python file and you are trying to give that particular collected input to another python file that is story generator P from Lang chain import chain prompt text model then use user input as I told you user input file you have to take all the user input get user inputs what is collected character name settings and team this is collected you have to take this as a input and import that into this particular story generator python file this is the function where you can just create certain story according to the inputs given by the particular user then you will have a prompt you will have to work with a text model as I told you this is GPT 3.5 turbo here and Here Comes Your open API key you have to put your secret key here and then you have to execute this particular main block it will try to give you the story generated here in the print statement generated story is so and so a paragraph of a story will be displayed for you so this is this is how the main block will get executed and these are the commands or the codes which we use every code is having a self-explanatory comment that you can read and understand once again if you don’t follow it here okay this particular learning material or code is always provided no worries you can go back rework on this again then what do you do in order to display the story you have to execute the file how do you execute story generator. py here why are we not using python we have to use Python right it should be python story generator. py that’s how it will execute simple as that you will have a quick recap here first is environment setup that is you are having python in your system you have two libraries that is open Ai and L chain then you have to have your own API key which is discrete you have to create main script that is story generat data and you also have to create a subscript that is user inputs then you have to generate the story functionality that is open as chat GPT you have to use that and you have to give them the character name setting theme Etc it will develop the story and if you execute it will give back the story which is already developed as simple as that hope this is very clear for you now let’s see the demonstration what is the output and how it will work we are trying to execute personal personized story generator which we have already discussed that we are using the library called Lang chain here it is you have two different python files one is story generator one is user input it is already explained user input is used to take the input from the user and story generator is the main app you should not execute python user input. py you have to execute story unor generator. py that’s how you will get the output screen this is kind of special execution that every output is seen on the command prompt itself we need not navigate between any other locations for output what is this py cache if you click on this folder after you compile your code this is actually generated compiled python file will be generated so that is why it is here now let’s quickly hop on to our Command Prompt and try to execute this particular code file in order to do that first what we need is we have to copy this location where it is actually situated the folder of your app now we are on the command prompt we are changing the directory and pasting the location which we copied and clicking on enter we are in the folder called personalized story generator what we have to do we’ll try to execute python story uncore generator dopy once you click on enter this is how it starts executing welcome to the personalized story generator and I’ll type a name of the main character as Alise and it will ask setting of the story where it has to happen I can say Enchanted Forest I’ll give the location visualization idea for the GPT I click on enter it should be a mystery one or Adventure one or horror one whatever you can mention that I’ll mention as adventure story I’ll click on enter see the story is generated in this form you can read the story pausing the screen but yeah it will include the main character the setting of The Story also what kind of story what is the theme of the story it will try to give you the complete paragraph which you can use it for your requirement this is how a story generator will work using Lang chain you can create much more applications this is of one basic example hope this is clear for you we have executed the code we saw the output how does the story will be generated with the help of GPT which we have connected with the help of the API key let’s make life easier with python for automation you will learn to automate rep repeative tasks and even building user friendly guis what actually testing is let me take you to that right here okay so from the one simple word which we have here testing okay let’s first of all not go too much into the uh into the technical definitions or something like that if I just simply talk about that what testing is in simple General language right what do we say testing is basically to test out something right so testing means that let’s say you are having any idea or any app or something you have developed onto your own right now you just want to test that out that okay is this a thing which is ready to go in the market is this a thing that I can give to the people or something like that right so in the same procedure in the same way when we talk about testing in software development field what we see that whenever you are developing out any software any product any component let take it as a uh website take it as a application anything you are developing any product I’m just talking about here right what we have to do we have to analyze that right we need to see that okay what are the features I have added into that right after that we need to evaluate that okay what are the components in which basically we are having the errors or the bugs that are faced out why is that necessary to do that is necessary to do so that whenever your product goes into the market it is delivered into the market it is totally free of the errors or the bugs we all are familiar with that if there okay let’s say you made out a login page let me just quickly take an example here let’s say you made out a login page right in that login page basically let’s say everything is totally and clearly mentioned and you had just attached out some data base to that as well fine now whenever you are just running that out let’s say whatever the US username or whatever the password a person is saving onto that login page that is not getting saved again and again that is showing the error and you have already sent that in the market what is that that is a type of error which is coming into your product which you have already sent into the market right so that doesn’t uh go in a right way so this is the use for testing that why you new uh used to and why why basically you just need out to do out the testing and if you have done out the testing at your end for first of all you have just removed out all the errors you have removed out all the bugs which you were having into that after that now when you were just figuring out and giving it a test in that case what you just figured out that okay when I’m entering the username and when I’m entering the password so that is not getting out saved so I just need to figure this thing error figure this error or figure this bug whatever is there into that so that whenever it just gets delivered into the market whenever a person buys out this application or whenever someone logins onto this particular page so that particular person does not faces out any error right this is what we actually want what we want is that whatever the things which we are making that are absolutely correct and if a person is using those things a person does not should not actually face any type of difficulties or any types of error in that so this is the use this is the point where you need out a testing right right now here we are just generally talking about the testing thing we are not going that you are testing for a software or you’re testing for app or you’re testing for anything we just not going into that particular thing right so when whenever we just develop out a software component or you just develop out any project we need to analyze that we need to inspect its features we need to go through the features which you have added we need to evaluate whatever the what are the components you have put onto that particular application we need to analyze we need to evaluate that are these components are these features which you have added into this application added into this product are that errors or bug free and if yes so whenever and why why basically we just need to check out that is that error or bug free so that whenever you just get whenever this product of yours get delivered into the market so whenever the user uses that product product actually so they do not face any types of errors or bugs it is totally free of any error or any bug right so now this is the point where we actually need out the extensive testing of the software if I just talk about the software or if I talk about product so this is a place where you actually need out the soft testing of the software right now when is testing done I just explained you that what testing is what’s the use of testing now when is that actually done so testing is done whenever your application is built out right whenever you have built out your application after that you just test out give it different test cases give it different databases that all we’ll be discussing in um a little while so I would just give you an idea that you just give out different test cases you just def like give it out different databases and all those things so whenever your application is actually ready okay whenever your application is completely built out and that is ready to uh ready to be get tested so in that case we just do out the testing and we just deploy that into the different test servers or the test environments which we are having so that whatever the testing is to be done with that particular application we could just perform that out right let me just again quickly go over that what testing is and when is that done and why do we need that so once you develop out a software component or a product so we have to analyze and inspect its features and also evaluate the component for potential errors and bugs so that when it gets delivered in the market it is free of any bugs and errors it is the point where we need extensive testing of the software right and when it’s that done so testing is done when the application is built is ready to test and deployed in the test servers or the environment right this is what we uh I could just let see about the testing that testing is basically to test out its features that do you have any errors or bugs if yes then to clear that out and if no then it is absolutely ready to go into the market and it’s totally free of bucks and errors right hope you first of all just got the idea regarding what is testing now we’ll be discussing out that what manual testing is so we discussed already we had seen that what is testing now there are two types of testing which we have here in the selenium first of all is the manual one and second one is the automation so here we’ll be discussing about the manual testing let’s get started it let me just move on to the second one fine now um again I would see you that let’s not go to much into the technical things right here let’s simply understand that what is a meaning of word that is manual if I talk about talking manual in a simple English language so I would say manual means that anything which is done manually right anything which is done by you done by manually that simply talks about the manual and if I talk about testing so this is one thing which we have already discussed that we just imp Implement out the different test we just Implement out the different features we just do all of these things to make our product error and bug free right so here if I just say if I just combine the simple manual and simple testing things so I could just say without any technical definition or without any further like that things I could simply say that the testing which is done manually is called a manual testing isn’t that simple right yes that is so I would just request you not directly go into the technical definitions first of all try to analyze that okay what is the name of the topic simply the name of the TP topic is manual testing manual means anything to do that is manually testing you already know to test out your software to test out your products whichever you had made so that whenever they just get delivered into the market they are totally bug free right so this is where uh this man testing definition comes now if I just talk about the things in a very detail so I would say um that manual testing means that uh the application which you had made out the application which is actually developed by you the product which is developed by you so here will be particularly talking about the application okay so now from here onwards i’ be taking the word which is application so manual testing means that the application which is basically the application which is made that is tested manually by the testers this is simply what is there in the manual testing right so whenever you had made out any application and you just test that application manually right so that is actually called as manual testing now when you are doing out the manual testing so in that case what are the things that are to be performed so the test which are there actually they need to be performed manually in each and every environment using different different data sets in the starting as I mentioned about the data sets right that we take at different different data sets and even we just give out some test cases and then try to implement out the and test out our software manually test out the product test out the application actually manually right so whatever the test you are performing manually that is done under the manual testing whatever the test you are performing they need to be performed manually in every environment which you are having right now in every you’ll be giving out different different data sets after giving out that different data sets you will even note down the rate of success and the failure okay whatever the transactions you are giving whatever the data sets you are giving for each of the data set you will be noting down the success and the failure rate that all will be recorded right this is what happens in actually manual testing let’s say you develop out an application right let’s let’s that you just developed out any application in that application if you are if you just want to test that out so if you are going to go through the manual testing procedure so first of all you need to test that particular application onto every environment which will be having different different data sets for every environment and even you will be noting down what is the success rate and whatever the what is a failure rate for each of the transactions for each of the data sets on which you are performing out the things this is what is actually done in manual testing right all of the things are recorded but this is all about the manual testing first of all that what it is and how we just do out that particular thing next when I talk about manual testing so I would say that it is absolutely mandatory means it is very important it is important for uh every new developed software being before automated testing so now whenever we are going on and we just let’s see just developed out any new application you just developed out in new new software in that case it’s mandatory to go for first of all for the manual testing then go for the automated testing I would like it is mandatory for every new developed software to go under manual testing before the automated testing what is this automated testing that we are going to discuss in some few minutes right so let’s not go that too much into detail onto this automated testing but the way which I had told you that is uh breaking out the vs which is so you just break out the word automated testing into two parts first one would be automated and second one would be testing so if you are breaking out both of these and figuring out that what’s the meaning then absolutely you are right the definition which you are thinking for the automated testing that is absolutely correct right I’ll be proving this thing in within a some time right first of all let’s discuss about manual testing in detail now what happens in manual testing when you are doing everything manually you are uh testing in every environment you are testing on different data sets you are giving it different cases you are noting the success rate you are noting the failure rate all of these things when you are doing so that will absolutely require a lot of time and even a lot of efforts are required but when you are doing anything or manually when you are doing anything onto your own when something is tested manually by the testers so yeah it absolutely gives you the short of a bug free software because the machines are not too much that much automated that okay they give you the shorty of a bug free software but if you are doing anything onto your own so yes that gives you a complete short of a bug free software so if I talk once again about the manual testing so in that manual testing means the application the web application whichever is made by you or any application is tested manually by the QA testers so the test which you are performing that needs to be performed manually in every environment using a different data sets and the rate for the success and the failure transactions should be recorded as well why is manual testing so manual testing is mandatory for every newly developed software before automated testing this testing actually requires a great effort and time as well but it gives you the bug free software shity of a bug free software right this is about the manual testing now if I talk about the challenges that what are the challenges that manual testing is faced now there must be some uh challenges some limitations in the manual testing right that is the why we just in we were introduced to the an automated tool which was selenium right there should be some there must be some challenges in this particular testing and this was only the reason that why any automation testing was actually introduced used right let’s see that what are the challenges in the manual testing first it requires more time and more resources absolutely right thing so when you are doing anything manually right when you are doing anything manually that will absolutely require more time within if anything is done using any automation tool right so this was one of the challenges which was uh faced in the manual testing that it was actually requiring a more time and even the more resources as well right gy object size difference and color combinations Etc are not easy to find in manual testing so whenever you performing manual testing so in that whatever the GUI objects you have made out whatever the color combinations you had put on whatever the size differences you are facing what are the different color combinations you were trying to figure out all of these things are actually not easy to find in the manual testing right these are the things which are not really easy to find out in the manual testing right after that executing the same test again and again it is time taking process as well as tedious absolutely right so um let’s whenever you are just do out the things onto a manual testing so what we had seen there we saw that in manual testing what was happening first of all whatever the whatever the application you want to build out that was actually built after that when the test test were performed so in that case what was happening in that particular case whatever the test was done that first of all they were done in all the environments after that different different data sets were given off all that and after that different data sets you were needed to put down the success rate you were needed to record down the failure rate and all these things were actually done so these things were done manually so it was a time a very much time-taking process to do out the same test again and again same test again and again execute the same test so yeah it actually takes a lot of time and yeah that’s as well a little difficult and a little hard process as well so these were the three challenges which were faced in the manual testing so first of all it requires more time and more resources second the GUI object size difference and the color combinations are not easy to find in the manual testing and third executing the same test again and again is time taking process as well as TS right so here we discussed about the manual testing and the challenges now i’ be introducing you to that what is automation testing what do we mean from this term now again I just I would just not want you to quickly read out the slide no I do not want you to do out this thing first of all uh according to in simple English language think about what is actually automation automation means to automate any anything us using any machine right automating means to automate something and testing this testing we already are familiar with that what testing is so as basically we have that autom in automation testing we already have a framework we already have a tool set according to that only the test whichever are to be performed on whatever the applications they get automatically performed so in that you do not need to put down the rate of success the rate of failure you do not need to manually test on each and every data sets you do not need to manually give it on all the environments no these are not the cases which happen in automation testing automation actually on its own means that anything which is done automatically right so in this automation testing whatever the test you carry out all of them are actually done automatically right so let’s read out that as the name for the suggest automation testing takes the software testing app activities and executes them via an automation tool set or framework as I mentioned in the starting as well that in automation testing what happens in automation testing we just you just take out a software whatever software testing activities whatever you just want to do and we simply execute them uh through a automation tool set or a framework we already have so we just put out the things onto that automation tool set or framework and whatever are the test which you want want to perform for any software or any application whatever is done by that you could just simply perform those test onto that now if I just talk about in very simple words that what automation testing actually meant so we can say that it is a type of testing in which a tool which you have right that executes a set of task in a defined pattern automatically automatically is a essential word to add in the automation testing definition right right now this this is a type of a testing in which uh a tool automatically executes a set of tasks in a defined pattern which is automatically defined and it automatically performs and executes the set of task which are given to that right this is what comes in the automation testing now where is this method actually used out this automation testing method uses scripted sequences that are executed by test in tools already we have many scripted things written out here we have already scripted programs written out for this automation testing method we just simply use them out and they are executed by the testing tools which we have here right this tool execute examinations of the software report outcomes and compare the results with the earlier test runs now what are the things that automating testing tools can do so in that case it can execute the execute examinations of the software it can basically whatever the outcomes are there it can report that particular outcomes and even it can compare the result with the earlier test runs which were actually made out it can compare out that test and we can just see the comparison in the results for whatever the earlier test runs which you have carried out right this is what uh is here in this automation testing thing that what it what it does actually does so it executes the examinations of the software it reports the outcomes and even it Compares out the results with the earlier test runs right this is about the automation testing if I give you an overview for the automation testing so we can say that as the name suggest automation testing takes software testing activities and executes them via an automation tool set or framework so we have have a tool set of framework through which this automation testing actually takes place if I talk about this thing in much simpler words so I could say that it is a type of testing in which a tool executes a set of task in a defined pattern automatically so this automation testing method basically in this we use the scripted sequence that are executed by the testing tools and these testing tools what they do they help us to execute uh the examinations of the software the report outcomes and even compare the results with the earlier test runs right so hope you just got out the idea first of all regarding that what is manual testing what are the what were the challenges faced in manual testing that why we need to introduce the automation testing right after that the challenges we simply learned about that what automation testing is now in the next part of the video we’ll be discussing about the selenium in detail we’ll be going through the introduction to selenium so now we’ll be discussing about and I’ll be taking you through the introduction to selenium now this is the place where actually we start the selenium thing so let’s get started here let me just take you to the new slide and here we go uh first of all we’ll be going through that uh who introduced selenium that basically who is the founder or you can just say that uh who developed selenium after that we’ll be seeing that how that things are done and what is actually this selenium right so the selenium was introduced by Json Huggins in 2004 right so uh we can just say here that selenium was introduced by Jon hins in 2004 so he was an engineer at thought works so basically how does this idea coming for introducing something some automated tool so what he was actually doing he was doing his work on some web applications right he was just test making out some web application doing some work on his web applications suddenly he just required out some testing technique right he just suddenly required that okay I just need to go ahead with the testing on this particular web application which I am making now doing out the manual testing absolutely takes a lot of time a lot of efforts as well which we had already seen in the previous slides that manual testing takes first of all a lot of time and even a lot of efforts why because there which are the test which are actually there they are carried out manually first of all after that the test are performed on all the environments you need to put different different test cases for that after that whatever the rate are of the success or whatever the failure rates are there you just need to note them down and then just come to an output this is the thing which happens in manual testing and this is absolutely a long procedure a hard procedure and a lot of time-taking procedure so then basically just on how thought that there must be some tool in which we in which some automated things should be there so as to do out the testing in a easier and in a faster way so this is where Jon Huggins introduced selenium as an automated tool for the testing Frameworks right so if I just quickly talk about that who was who introduced selenium so name for that person is Jan Hagins he was an engineer at thought works so some one day he just thought of working on some applications and he required testing so at that case only he just developed out and introduced the selenium and automated testing framework or you can say as a tool right now whatever testing we do with the help of a Cel with the help of selenium or using selenium that are called a selenium testing again from the word it actually states that particular thing what is about the selenium testing so the testing which is done using the selenium right whatever the test which we perform using the selenium tool that are referred as selenium testing now what selenium actually is so it is an open-source tool first of all where you first thing that it is an open- Source tool a portable framework which is used for automating out the test administered on web web browsers see selenium is one of the first of all open source tool that is absolutely okay a portable framework that you could just use it at any place that is as well okay this is particularly used for automating out the test which are administered on the web browsers actually senum works on different different web browsers it has that capability to work upon different web browsers right whatever the test we have so basically it is actually used for automating out the test which are administered on the web browsers what are the testing uh web applications on which you can perform selenium on which you can just use out this uh framework which is selenium so that testing web applications are a shopping carts you can email programs like Gmail Yahoo all all these cases in all these places actually you could use out the selenium framework so first of all it is very open source tool so basically it means that you could just simply download that it is it does doesn’t ask you for giving out any paid work version or something that it’s totally available free of cost after that it’s a portable framework so you could just use that at any time at any case and this is used for automating out the test administered on the web browsers the testing app web applications which can be performed using selenium our shopping carts or email programs like Gmail yah these are the things which are actually performed using the selenium so hope you got the idea first of called that who introduced selenium so Jon hins was the person who introduced selenium in 2004 so who was he he was an engineer at the thought works why and how did he got out the idea for developing and introducing this selenium so he was doing some work on some web applications and suddenly he required some testing technique manual testing was a very uh long a very time-taking task and it actually requires a lot of time to do out that particular thing so this was a place there was a requirement and when Json Huggins developed selenium and automated testing tool so whatever the testing you do with the selenium these are termed as selenium testing and what is that selenium actually so it is an open- Source tool a portable framework which is used for automating out the test which are administered on web browsers it is only used for testing out the web applications such as shopping carts or email programs like Gmail Yahoo shopping carts you all are familiar with and nowadays everyone just prefer out shopping online and all those things so yeah these are the applications where you could just use out the cenum now why we should use selenium with python in the starting as well I told you that selenium is one of the tools which can be performed with the help of Python programming language and you could as well go ahead with the selenium tool when with the JavaScript as well so now what’s the use and what’s the advantages more which why and we should use cenum with python so let’s go and see that first of all we are all familiar with the Python programming language and there is no such doubt in saying that this is one of the important features of python that it is very faster and even easy to learn as well right it is a very simple language so this is the first reason that why we should prefer selenium with python so py python runs very faster absolutely right it is a very faster language it makes the use of indentation to initiate and end block so the indentation which we have in Python that is a very systematic thing even you just need to follow out as well very strictly so what what basically let’s say you just applied out some condition so when you just apply out that first of all conditions and whatever the block of code you want to uh apply basically inside that condition so first of all your condition comes on the first line and when you come to the next line so there is some space left in the starting after that you start putting out your code of block which you want to put inside that condition this is what indentation is and it actually helps us to see that okay where this particular code of block is ending where this particular is starting so it make us easy to analyze all of these things and this is why uh the use of indentation we be see right next it is very simple absolutely right the syntax which we have in Python is very much simple as well as compact yes the start simply and as well as very much compact compared to other programming languages hope you all are very much familiar with python for now so you must be very much familiar even with the features which I just told you it is very much fast to the indentation which we have here it is very simple to use as well as compact other if I just compare this with the other programming languages so these is the first reason that why we use the selenium with python second thing we have a tool which is called as web driver in actually selenium we have this tool right web driver this is a very important tool in selenium we’ll be discussing uh about this in the further videos right now for this particular thing you could understand that okay web driver is a important tool in selenium so this tool which I was telling you about web driver that has a very strong bindings of Python programming language actually right so uh this is the important tool for easy user interfaces that is web driver and it really has a strong bindings for Python programming language so this is one another reason that why we prefer selenium with python moving towards the third it runs rapidly while making a comparison of another programming languages so that is true that python actually runs rapid while making out a comparison of any other programming languages right the programming language is as well free and available as open source absolutely true so python is as well an open- Source language right whosoever needs that you could some just simply download that and use it freely in any of the environments yes this is as the case it’s not the case that you just particularly need this only environment to work for python no you could download any of the environments of your choice and there you could just download the Python programming language and simply you could just use that out so it’s we can say that it is as well free available as open source and you could simply download and use freely in any of the environments according to your choice and the last one it is easy to code and easy to read that is one of the important points one of the important features as well of the Python programming language that it is very much e easy to code out and even easy to read out right so hope you now just got out the idea regarding these Five Points these five reasons that why selenium with python let me just quickly give you an overview so python runs very faster and makes use of indentation to initiate and end blocks it is very simple as well as compact as compared to other programming languages right we have a very important tool which is web driver in selenium and that has the strong bindings for Python programming language Python programming language runs rapidly while if you just compare this with any other programming language so it runs rapidly this language is as well free and available as open source so if you just need out you could just quickly download that and freely you could just use that on any of the environments and at last it is very easy to code and the the syntaxes the programs which you write in Python program language that are easy to read as well so these were the five reasons that why we prefer selenium with Pyon now here basically we’ll be we have discussed about that what uh selenium is and why we should prefer selenium with the Python programming language in the next set of video we’ll be seeing about the advantages and the limitations for the selenium testing now we’ll be seeing that what are the advantages for the selenium testing let let’s go ahead with that first of all uh the very first Advantage for the selenium testing is that it supports the various programming languages to write the test scripts as mentioned by me in the previous video I had already told you where we discussed out the topic that why selenium with python there I mentioned that selenium is even um you can just do out the selenium testing with the other programming languages as well I took out the example for the JavaScript right so the same advantage is mentioned here that uh for writing out the test scripts there are many programming languages which are supported so whatever you are whichever language you are familiar with you could absolutely choose out that language and go ahead with that for writing out the test scripts another very important advantage and even I could just say a very good feature for selum is that it is supported on the various web browsers as well you could take M Firefox you can just go on to the Google Chrome whatever web browser you are actually using out the selenium is selenium actually supports out many of the and various of the web browsers right let’s say you take uh Firefox you take Google Chrome or any other whatever you just want to take out you can just as well go ahead with that same next it supports the pal test execution now what P test execution is in detail I’ll be discussing further for now we could just understand that uh selenium as well supports out the parallel test execution it means I could just give you an overview that parall you could perform many test onto a particular application uh this is I could just say a simple um definition regarding the parallel test execution right so selenium actually supports out that particular thing one another thing which is as well mention into the definition for the selenium as well that it is an open- Source software so you could just use that accordingly whenever you just wish out you could just download that and use it accordingly and it it basically it’s a totally open source software right next selenium as well supports out the different operating systems we mentioned I mentioned and told you that it supports out the various browsers with the supporting of the various browsers it as well supports and works on different operating system you take Windows you take Linux you take Mac whatever you just take out it supports all of these operating system right so these are some advantages of the selenium testing now what are the limitations as well I mentioned the starting that if something is having advantages it will absolutely have some of the either limitations so let’s see what are the limitations of selenium testing so I mentioned that it works on the web browsers right I mention out this particular thing so this is one of the limitation of the selenium that it only and only supports out the web based applications right whatever the application you are making it only and only supports out the application which are web based which are which basically work upon either Google or MOA Firefox it just simply supports out those web based applications only now whatever the new features are getting introduced into the cenum testing whatever new features are getting introduced they do not have aurity that they will work or not work right they may work out sometime they may not work out sometime so this is one of another limitations which is right now right right now these spacing for the selenium testing right and here are some important uh last three limitations about the selenium testing you can see that there are some used cases where the selenium doesn’t works out the selenium testing actually doesn’t works so first of the selenium cannot perform testing on the images the very first thing that it cannot perform out any of the testing onto the images the the code which is written behind this testing and all the things so that is not supports the testing on the images you you cannot automate out the captures using the selenium captur right now in today’s world right now everywhere we see out the captas whenever you just log in onto any of the web browser so you just any of the website they ask for the captas to fill out that right so selenium cannot automate that so capts are not at all automated using the selenium and at last that barcodes cannot be automated using selenium neither the Capt nor the barcodes NE neither of these are supported and can be automated using the selenium right these are some limitations for the selenium testing hope you got that but once again let me go over them so selenium supports the web- based applications only so whatever the applications you are having selenium only only supports out the web based applications it doesn’t supports out the applications which you have made onto your any of the environment or something like that it doesn’t touch that thing okay second the new features which are getting introduced so they are a little bit of ir responsible in that case we just have a doubt sometime that either they work or they may not work we are not too much or that much confirm about them so this is what we can say the irresponsibility of the new features now whatever the the selenium whatever basically selenium cannot perform out the testing on the images so if you just want to perform any test on the images so in that case selenium cannot do out that particular thing right the captures which you have these are not automated using the selenium and the barcodes are as well not automated using the selenium right so these are some of the limitations of the selenium tools finally we will cover GUI development you will learn how how to create interactive desktop applications using libraries like TK inter we will guide you through building your own GUI applications from scratch making your programs more userfriendly and Visually appealing so let’s start with graphical user interface so as I already told you about graphical user interface it allows the user to communicate with electronic devices through graphical representation when I’m talking about graphical representation stag means buttons and icons so the example of GUI is micro moft Windows as well as Mac OS and we are having several other examples also so basically what happens here here you can do the communications by interconnecting with the icons so this is the basic idea about graphical user interface now let’s see different types of graphical user interface libraries that are present in Python so we are having several libraries that are used in Python for the GUI we are having DK inter we are having KV Pi qt5 WX Python and Pi GUI so now let’s get some idea about TK inter so python TK inter is nothing but a standard GUI Library so basically when python is used in conjunction with TK inter it creates graphical user interface that is quick and simple also it gives the TK GUI toolkit a sophisticated object oriented interface so this is the basic idea of TK enter you just need to know that it’s a python GUI library and then we will see how to create a TK enter programming so now if you want to create a simple GUI application with python in TK inter so there are some certain steps that has to be followed first you have to import the module of TK inter right so you can just simply write from TK enter import srick and then you can import the TK enter module Second Step that we have to follow we have to create a main window then how to create a main window we will basically create a object of python TK in next after creating the main window now we can add multiple vets in a main window and after adding vets now we can enter into the main event Loop to perform action so now there are two primary approaches that user has to follow right so first as I already told you that you have to import the DK inter module and the module name is from TK enter import as so after importing the TK enter module now what we have to do we have to create a main window so how to create a main window I will just simply write here main window that is nothing but a variable so for creating a object I will just write here TK and parenthesis make sure that your T is capital and then in order to run the application I will just right here main window. main Loop so basically main Loop is nothing but an infinite Loop right that will run your application and then it also waits for an event and process it as soon as the window is open so this is the basic idea about how to create a basic python application with GUI now let’s see into the Practical example so what I will do now I will just write here from TK into import a so this is the basically TK inter module that I will import here and then after that I will create a main window so let me write here variable window and I will create here the object so for creating the object make sure that he is capital and now we’ll just simply write window. meain Loop so we know that main Loop is infinite Loop that will run the application so if I’m executing it you can see that we have created a simple GUI application right so this is the basic idea about python T so now you have seen that how to create a GUI app now let me here change the title so if you’re executing it now so on execution you can see that I am getting here TK right and if I want to give the title instead of TK great learning so I will just simply write here window do title and I will write here welcome to cre learning so now let me execute this and on

    execution you can see that on top we are getting welcome to create learning as a title so after this let me change the window size so I’ll just write here window do Min size and I will just write here let’s suppose bit as 100 and height is equal to 200 so if I’m executing it now you can see that this is the minimum size of this window and if I’m clicking on this button maximize then you can see that this is the maximum size so let me change the maximum size also so for that I’ll just write here window so I will just write here window do Max size and once again let me give your width is equal to Let’s suppose 3 00 and let’s take the height as 800 so on execution if you see this is the minimum size of the GUI app and this is the maximum size right so this is the basic idea about python TK inter so after creating first GUI app now it’s time to know about widgets so what are widgets so talking about widgets in general this is basically an element of GUI and in TK inter widgets are considered as a objects which represent buttons frames Etc so basically TK offers many controls and these controls are nothing but known as vets which we will be using GUI applications right so as I already told you that it represent buttons labels and text boxes so we are having different types of wigets that are available in DK enter that is label button entry check button canvas frame and many more so this is the basic idea about wigets so now let’s understand the geometric configuration of wigets so we have already got the idea about vget but if you want to organize the Vets so we need a geometry manager classes so primarily we are having three types of geometry manager classes that is pack grid and place so after adding viset I have to organize the widget so I will be using these three geometric manager classes so the first one is pack so when you are using pack functions it means that you are placing a visit on a top right coming to the grid it is used to organize the visit in the table like strcture so when I’m talking about table like structure that means row and column next we are having place so it is used to organize the visit at specific positions so I just write here X and Y so we if you are writing let’s suppose X is equal to 20 and Y is equal to 40 so that means from left to right you are placing a particular viget and now coming to the Y that means from top to bottom you are placing any viget so this is the basic idea about the geometric configuration of wigets so now it’s time to discuss about the different types of vets that are present in Python TK inter so first vet that we are having is label so when you are talking about label basically it is used to represent display box in which image or text is added so what’s the syntax of label so I will just write here simple label and in parenthesis I will write here Master comma options is equal to Value coming to the master master is nothing but the main window that you have created and here we can provide several options as an or so we can write in options BG command font image width and height when I’m talking about BG that means it’s a background right and also we are having FG also FG means foreground color so when you are writing FG is equal to Blue so your text will be of blue color right so this is the basic idea about the label widget now let’s see it into the Practical example so what I will do here right now once again I will just import the TK inter module so I’ll just write here from TK inter import as trick and then after that I have to create my main window so let me write here the variable name window and I will create the object s DK now I will just simply add widget here so I have to add here label widget right and we know that after creating the main window only we can add widget here so let me write here Elvin is equal to label so this is my label vidget right so in label here I have write here Master what’s my master master is nothing but the main window so I’ll just write here window and then in options I can leave several options so let me write here options let’s suppose I’ll just write here text and I will write here great learning [Music] so this is the label right and also if I want to organize the visit then I have to use the geometrical configuration right so let me use use here first el. pack and if you want to run the main application then I have to write here window do main Loop so if I’m executing it now so on execution you can see that create learning has been printed on the top of this window right in middle so this is about the pack geometry class now after pack if I’m just using here let’s suppose grid so we know that grid contains rows and columns right so what I will do here I’ll just write here row is equal to Z and column is equal to Let’s suppose one now if I’m executing it so on execution you can see that great learning has been printed here so as of now we have just started doing the coding so as soon as you will write more code then we can change the row and column also and then we are having one more geometry class that is place right so I’ll just write here place so basically in place if we want to organize the widget at any particular place so that we can give here X and Y and let’s suppose if I’m writing here x = 5 and Y is equal 10 so basically this is the position when I’m writing x equal to 5 that means from left to right you are placing your visit similarly when I’m writing your y equal to 10 that means from top to bottom you’re placing your visit let me execute this so on execution you can see that date learning has been printed if you want to place at any other position so you can just write here xal to 50 and Y is equal to Let’s suppose 100 and if you are executing it you can see that we are getting the output like this so this is the basic idea about the label widget so now let’s suppose we have already created a label widget of text grade learning right let me execute this so if you see this is a create learning here now if I want to add here the background color along with the foreground color then how to add it so I will just simply go on here options and I will just write here let’s suppose background color I want in blue so I just write here blue and and for foreground color I’ll just write here Simply Red let me put this blue under double quotes So now if I’m executing it you can see that on execution this great learning so foreground color will be in red color and the background color is of blue color also if you want to increase its width so you’ll just write width is equal to Let’s suppose 40 and now if I’m executing it so on execution you can see that its width has been increased so this is the basic idea so in label viget you can add more options in the form of background foreground image font many more yeah so after knowing label now if I want to add any image on my GUI app so what can I do so let me execute once again this so if you see here let’s suppose if I want to add any image here so what shall I do so first what we’ll be doing let me just remove this let me write here the variable name as I and I will just use here photo image so you can also see that is showing the photo image option right so inside this photo image what I will do here I’ll just write here file and if you see here so let me take a file from the desktop so if you see here this is the file basically and it’s in the and it’s a image file right so I’ll just go on properties and if you see this is the location so I’ll just copy this and the file name is python right so the image file is having the name python so what I will do here I just simply first put double quotes and inside that I have just copied it and pasted the location and the file name is python right so I’ll just write here python Dot and it’s in the PNG file so I’ll just write here python. PNG now so now instead of this back slash we have to just replace it with the forward slash so I will just write here here like this so now after this what I will do I will create a label so I’ll just write here El is equal to label and inside this label I have to first put Master right so my master will be here the main window so I’ll just write here window and then I will write here option so in option I will just write here image and what’s the image here so image is equal to i1 so now if I’m doing execution so on execution you can see that I am getting nothing why because I have to use here geometrical classes right so for that I will just write here let’s suppose el. pack so now you can see that in output we are getting the TK enter image here so this is the basic idea about the label wiget so the next widget that we going to discuss is button so basically button is used to display button in an application and it’s also having a very simple syntax we have to just write button make sure that b is in capital and then instead of Master our main window will be there and in options we can pass several arguments such as BG command font image width and height so here we can also use command so when you are writing command so basically when we are creating any function we will just assign that function name into the command we will understand better while doing the coding part so now let’s see the practical implementation how to create a button so for creating button I just simply write here let’s suppose B1 is the variable name I’ll write here button and I will write here window so window is my main window right and after that let me write here text so if I’m writing here text is equal to into and again I have to write here B dot I can use here pack I can use here place I can also use here GD but let me write here back now if I’m executing it you can see that this is my enter button so let me give here again the color so uh in background let’s suppose if I’m taking it as a green color and for foreground I will just take as yellow and now in execution you can see that this enter button is having foreground color as yellow and background is green so now we have seen that how to create a button widget now let’s see how to create an entry so what is entry so in entry let’s suppose if I have created a widget that is label widget and it name is username right and I want to give the entry through the user right so that’s why I will create an entry where I can write the value in the form form of a string as well as integer values too so let’s create entry here so I’ll just write here even variable and for entry I’ll just write here entry and in entry I’ll just write here window and then let me give here the width also so let’s suppose the width of this entry is 20 so I’ll just write here even do back and now I’m executing it so you can see that on execution I am getting this entry right so here I can write here string value as well as integer also so how we can use this entry so let’s suppose if you are making any website right and for that particular website uh if you want to access it you need username and password then we can use entry there and we have to create button also for that so let’s suppose that if you want to change the font style here so what I will do here I just simply write here font is equal to and uh let me write here Cali and let’s suppose that font size I want 20 so I’ll just write here 20 and now if I’m executing it you can see that the size has been increased so now if I’m writing here G it’s in Cali right now what can I do here if I’m writing here BD is equal to 5 so what is BD here so BD is nothing but it is used to represent the size of the border so when I’m writing your BD is equal to 5 then you can see that we are getting the entry button like this right so this is the basic idea about the entry so let’s make a simple GUI here so what we going to do first we going to create a label and after that entry and then we will create a button so in label I will be taking the name as employee name and entry I will just give any string values and when I’m clicking on submit button so what will happen whatever the string value that I have given in entry will be displayed so basically I will be using two labels here the first one will be having the name as employee name and another label will be having the name as nothing so whatever the string value that I’m giving to entry and if I’m clicking on submit so instead of nothing that value will be shown to us so this this is the basic idea so let me create a simple GUI now I’ll just once again write here from TK into import asck and now I will just create a main window let me give you the title also so once again I will just write here window. title and I will just write here welcome to Great learning and then and let me just uh write here window minimum size so I just write here width as let’s suppose 200 and then height I will just write here 400 so for the maximum size what I will do here here I will give the width as 400 let’s take the double of the minimum size and then height also I will write 800 and let me write here window. mean Loop because if you want to run the application then you have to write it right so so you can see that this is my output and this is the minimum size and the maximum size is this one and here on the title I’m getting welcome to create learning so these are the things that we already know right so let me create a label view so for label I will just write here L1 is equal to label and L1 is my variable right so inside this label let me write here master so Master will be here my main window and now in options uh let me write here text so in text uh let me give here let’s suppose employee name and then uh let me also give here foreground as well as background color too so foreground color let’s take here blue and background color as let’s suppose I will take as red and since this is a label basically it’s a widget so I have to organize this widget so for that I will just write here elen dot I can use also your pack but let me use this time place so in place I can place my vet at any specific position so uh let me take here x is equal to 0 and then Y is equal to 10 and on execution you can see that this is my label which name is employee name right now let me create an entry button here so for entry button I will just simply write here even is equal to entry where even is my variable and once again I will just write here window and uh let me give the and let me give here the font so I’ll just write here font so in font I will just change the font style so let me change the font style to Corbell and uh let me give the size as 18 and let’s use bd2 so why we are using BD so as I already told you that BD is used to represent the size of the borders right of an ENT so if I’m writing here BD is equal to 5 so let me write here even do entry so now what I will do now I have to place this entry right so I once again I will write here even dot let’s use here place and let me use here x is equal to 40 this time and Y is equal to 10 and if I’m executing it just now you can see that I’m getting this output right so it’s not coming perfectly so let me just adjust the size here so instead of 40 if let’s suppose if I’m writing here 80 and now if I’m executing you can see that I am getting here employee name as label and this is the entry right where I can write any name okay so let’s create a button also so for button I will just simply write here B1 is equal to button and let me give here the master as window and an option I will give here once again the text is equal to Let’s suppose enter let me give here the colors also for this button so for foreground I will use yellow and for background let me use green here so now we have to to organize this button wiet also so for that I will just write here B1 do place and let me write here x is equal to let’s take here 100 and Y is = to 40 I’m just taking random values here now if I’m executing it you can see that I’m getting like this one right so let me just rearrange it if I’m writing here x is equal to 120 okay now it’s coming in between and let’s take the Y value as 60 now it’s coming perfect right now if I’m writing here let’s suppose gorov and if I’m clicking on this enter you can see that nothing has been happening right so what I will do now I just create another label and this label will be here and I will just write here nothing now after that whatever the string values I’m writing here in this entry and if I am clicking on this enter then this instead of nothing the value must be the same that I have given so for that what I will do let me create another label so I’ll just write here L2 is equal to label and I will just write here window and after this I will just write here text is equal to nothing and let me give you a foreground as let’s suppose uh black and background let’s take background as brown and let me place this visit so I think X will be 120 here I will be taking and Y let’s take here 90 or let’s take 100 and now if I’m executing it you can see that label is not defined because L has to be Capital here now it’s good to go so on execution you can see that I’m having the another label as nothing now I have to do something that if I’m writing here gorov and clicking on this enter button then it should work right and instead of nothing I want here the value as cor up so for that what I will do here let me create a variable so now after this what I will do here uh I will just create a string where so let me create a variable V is equ Al to string where so when I’m writing here string where so basically that means we are dealing with the string values right so after creating here string where one so now what I will do here inside this entry in options I will just write here text variable and it will be equal to V because we are dealing with the string values right now I want that my button should work so for that I will create a function so whatever the function you are creating let’s suppose a function name is attech then I can use the binding functions and I will write here in button command is equal to attech attech now how to use this atte so I will create a function basically so I’ll just write here DF at Tech and now after this I will just simply write let’s suppose my variable is V right so if I want to get any value I will just write here V dog so let me create another variable X is equal to V dog right and now after this I’ll just simply write here so now what I will do here uh I just want to print this value so I have to write here print X now here as I told you that I have created two label the first label name was Employee name and another label it was written nothing so I told you that what whatever the value that I am putting into the entry it should be changed in the label whose name is nothing right so I’ll just write here L2 and I will use here config function and inside this config I will just write here text is equal to X now what will be your x value here so I have WR here x is equal to V do get right and V don’t V is equal to string where so basically we are dealing with the string values so now what I will do here I’ll just execute this and let me write here let’s suppose gorov and if I’m clicking on this enter button you can see that I am getting gorov here right and also if you see the output I’m also getting gorov in the output why because I have written here print X so now let’s make some few more changes here the name has been changed from nothing to G of and let’s change the color also so how you can change the color so in this config function itself I I will just write here background as let’s suppose um yellow and then foreground as let’s suppose blue now if I’m executing it so let me stop and rerun this and this time once again if I’m writing your gab and if I’m clicking on this enter you can see that the name has been changed along with the background color as yellow and for forr as blue so this is the basic idea about the label widget entry and the button right so this is a very simple GUI application so the next visit that we are having is check button the check button is used to show a selection of choices as check boxes let’s take an example let’s take an example of male and female we can use them as a check button now let’s see the syntax so the syntax is simple you have to just write the check button and inside the parenthesis your master will be the main window and then there are SE several options that can be passed through as an argument we can have title background as well as active background so this is the basic idea about check button now let’s see the Practical implementation so now you can see that this is the basic TK inter programming now what I will do here I will just create a variable here let’s take CV and I will just write here check button and inside this I will just write here window and then I will just write here text so as I was explaining about we can take the example of male and female so just let me write here male and then I’ll just write here CB dot let’s use your pack and now if I’m executing it on execution you can see that I’m getting mail as check button right so this is my check button so the next visit that we are having is frame so basically frame serves as a container and it is used to organize the visits now what is the syntax of frame it’s again simple you have to just write frame and and then master and then options so here as a argument we can pass several options such as BG BD cursor width and height right now let’s see the Practical implementation of this Frame so now once again I came back to py charm I just simply write here F1 variable let me create here frame and inside this I didn’t pass any Master neither options so I’ll just write here f1. back FS2 is equal to frame and I will just write here f2. pack now if you’re executing this then nothing will be executed right so what I will do now I will just create here let’s take an L1 that is label I will create here and inside this I will pass F1 as a master and then uh let me take your text so I’ll just write here text is equal to Great learning and if I’m writing here L1 do pack now so let me execute this so on execution you can see that I’m getting great learning now I will just create another label let’s take L2 is equal to label so I will just write once again here F2 and then let me write here text is equal to B and I will just simply write here L2 do pack so now if I so on execution you can see that I’m getting bottom so here you can see that basically I’m having two frame right dat learning and bottom but I want the bottom to be printed on the bottom side so for that what I will do here uh you can see this I have written here f2. back right so I’ll just write here side is equal to and I will just write here bottom and bottom will be in capital so now if I am doing the execution so on execution you can see that create learning is on the top of the window whereas whereas bottom is on the bottom side of the window right so the next visit that we are having is list box so list box is used to give a user with a list of options so basically in a simple word you can say that list box contains a list of options and as we know that list can contain different types of data values so so list box will contain a different list of options so next we are having syntax so what’s the syntax of list box once again we’ll write just list box and inside that Masters and the options that we can pass as an argument so what are the options that we can pass through an argument it’s like BG BG is background BD BD is used to represent the size of the water then we can also use Font as a option image width as well as height so now let’s see the Practical implementation of list box so in the list box practical example we will see how to remove the element from the list box and also how to insert any element or you can say items in the list box so now what I will do here I’ll just write here lb is equal to let me create a list box so this is my list box and I will just write here window and let me give the width so if I’m writing a width is equal to 20 and let me just write here lb. pack so here L should be Capital so this is my list box and if I’m executing it you can see that I got an utty list box now it’s time to insert the options in the list box so for that I will just create a list so let me give here the list as Elin and uh let’s insert several values so how to add several values in a list box let me write the name here so let me write here Tony I will write here Adin let’s take some few more names uh let me write here kga and let’s take one more name as it so these are the four values right and I want to insert in my list box so for that what I will be using uh we can use Loop right for Loop so I’ll just write here for I in my list name is Elvin right so so what’s the name of my list box so list box is having a variable name as lb so I just write here lb Dot and then I will just write here insert and let me write here end and I will just write here I now if I’m executing it so on execution you can see that I’m getting Tony admin ktia IA so what can I do now if I want to remove let’s suppose IA from here so let me create a button we have to create the button right so for button I just write here B1 is equal to button and I will just write here window and text is equal to Let’s suppose if I want to remove e so I’ll just write here remove and let me give the color also so I’ll just trite here let’s check the background color as red now for button also I have to write here B1 do back now if I’m executing it so you can see that this is the remove button but if I’m just clicking on this IA and then clicking on this remove button so it’s not removing so we know that it will not remove until and unless we are not using a command right so what is command command is nothing but a binding function so in button I will just write here command and let’s take that I will write here attech so here you can see that I have assigned at Tech in command so what does it mean it means that I have created a function whose name is edtech right so what I will do here okay let me just create a function here Def atte and inside this function what I will do here so I just simply write here lb so what is my lb lb is nothing but a list box variable and if I want to remove the element then I will use here delete function and inside this delete I will just write here anle so what do you mean by anle so that means so that means if you want want to select an item single item and if you want to remove one by one then we can use anchor here so if I executing it now so if I want to remove let’s suppose kka and if I am clicking on this remove button then you can see that it has been removed from the list box similarly for IA we can do it right so this is the basic idea about the list box so now after understanding label wiget buten widget and many other wigets it’s time to discuss about some other wigets so we are having different types of other vets available in Python DK starting from menu button menu message radio button scale scroll bar text top level spin box and pan window we’ll see some of the wigets in the Practical exam so let’s start so now let’s discuss about the radio button visit first so for creating radio button let me write here RP one and R has to be Capital so this is my radio but and as a master I will just write here window and after this I will just write here text is equal to yes and let me write here rb1 do back so on execution you can see that this is the radio button that has been created yes right let me create uh one more radio button so I will just write here rb2 is equal to radio button and once again as a master I will write here window and text is equal to no and obviously we have to place the visit right we have to organize the visit so for that once again I have to write here rb2 do pack so now you can see that I’m having two radio buttons that is yes or no but you can see that these both have been selected right so for that what I will do here I will just write here value is equal to Let’s suppose for S I’m giving the value one and value is equal to Z now if I’m executing it you can see that only one has been selecting at a time so now here if you see that these are the radio buttons right but uh let me create a button so that whenever I’m clicking on yes I want the value to be printed on my output so for that I will create a button so let me create a button so I’ll just write here B1 is equ Al to button and then I will just write here window and in button I will just give your text is equal to intoo and once again just let me write here b1. back now if I’m executing it then you can see that I got the button but if I’m clicking on this enter button you can see that nothing has been printed in my output so for that what can I do here uh let me create here a interval so I’ll just write here let’s suppose I’ll just write here V is equal to Inver so that means basically we going to deal with the integer values and after this I will just write here variable and we know that our variable is equal to V here so I just simply write here V variable is equal to V and inside the button when I’m writing command so that means we are using a binding function so so command once again I will give the name here attech and I will create the function name as attech right we know that whatever the function we are creating so let’s suppose we are creating Tech function so in the command we will assign the te here so let me just write here Dev Tech and inside this uh let me just write here simple print and V is my variable so I just write here V dog so we know that in yes we have a assign the value as one and for no the value is zero so now if I’m executing it so on execution if I’m clicking on yes and clicking on this enter so now this button will work so on execution you can see that I’m getting the yes value as one right that we have already assigned the yes value as one similarly if I’m clicking on no and then clicking on this enter button will give you the value zero so this is the basic idea about radio button so the next visit that we going to discuss about is the message box so not talking about the message box it is used to display the message box on the python applications right so what I can do here let’s create a message box so for creating a message box you have to import message box so I’ll just write here from TK into import I’ll just write your message box now my message box has been imported now what I will do here I will just simply first let me create an entry so I’ll just write here even is equal to entry and in this entry I will just write your window and let me also give you the font so in font I will just change the font style so font style is’s take as Cali and then font size as 180 right and let me give the width also so I’ll just write here width is equal to 20 and after this let me just organize this visit so I will be using even. back and now if I’m executing you can see that this is my entry box right now what I will do here let me create a button also so I will just write here V1 is equal to button and inside this button I will just write here window simple and then in this button I will just write here text is equal to let’s take into right and I will be just writing here v. pack okay so now what I want to display here in this message box so let’s suppose if I’m not writing anything and if I’m pressing on into so then it shows some message box or you can say some warning message right in the form of message box and let’s suppose that if I’m writing any string values then it shows that yeah it’s successful and it should print that part value so for that what I will do here I will just create here a in word so I’ll just write here simple V is equal to inware or instead of intw I’ll just create a string where right let me write here string value so I’ll just write here string we and uh so here what I have to write I’ll just write here text variable is equal to V right now we know that we have to write here command which is a binding function basically so command here once again I will create a function so I have to write the function name here and assign it to the command so what I will do now here I’ll just create a function here Dev at Tech and let me give the condition here so let’s suppose if I’m writing here if V Dot get so we know that V dog will give me the value is equal to equal to if I’m writing here empy so I will just write here message box now I will be using the message box here dot uh python provides several functions here for this so I just write here show warning so I will be using here so warning and here you can see that we are having the title message so in title I will just write here cin and after that I will write it’s empathy it’s simple right when you are not writing anything in the entry and if you are clicking on the enter button or the submit button then it should show something right please enter something it’s empathy so I just written here the instruction it’s empathy next I will give here the else condition else message box dot now um I can use here one more function that has been provided by python I just right here so info and then title I will just give here successful and then uh let’s suppose display I want to display here v.g so I just write here V dog so whatever the value is there I’ll get here right so now if I’m executing it now so on executing this is my entry right and if I’m not writing anything entry and clicking on this enter you can see that I’m getting caen in my title and it’s showing its empathy right so this is the message box similarly if I’m writing let’s suppose G VI now you can see that I have given here gorov in Cali 18 right so this is a Cali font style and if I’m clicking on this enter it’s showing successful gorov right so this is the basic idea about the message box let’s quickly recap what all did we learn in the session first we started with python fundamentals here we discussed regarding what python its variables data types operators tokens control statements and also basic data structures of python like tles sets lists Etc in next module that is Advanced python Concepts we focused on objectoriented programming Concepts like classes objects Etc and also we learned how inheritance works and how to handle the errors in exception handling also file handling in the next module that is data structures and algorithms we learned about arrays Stacks cues Etc and also few sorting algorithms and searching algorithms like binary search insertion sort Etc then in the next module that is python for machine learning we explore the libraries we use in Python for machine learning that is numai pandas matplot lip and seon then in in generative AI of python we also provided a overview of generative AI Concepts and python applications in this field python for automation dealt with selenium web Automation in GUI development we used python library that is TK inter in order to develop a web page so we learned all these Concepts from the basic version to the advanced version in Python hope this tutorial was helpful for you thank you happy learning

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Python Project Tutorials: From Calculator to Text Adventure

    Python Project Tutorials: From Calculator to Text Adventure

    The provided text offers a comprehensive introduction to Python programming through practical examples and project-based learning. It begins with fundamental concepts like printing, variables, strings, data structures, loops, and functions. The text then transitions into building numerous terminal-based projects, including a website checker, step counter, text formatter, grade calculator, word scrambler, music recommender, name generator, vowel counter, coin flip game, recipe generator, color mixer, age calculator, simple calculator, word association game, rock-paper-scissors, task manager, chatbot, text analyzer, currency converter, password generator, finance tracker, quiz game, Pomodoro timer, and a text adventure game. Finally, the material introduces building GUI applications using Tkinter, demonstrating basic UI elements and functionality through a “Hello, World” application, a simple calculator, and a task manager. The concluding section briefly introduces the setup for a full-stack application using React for the frontend and Python (likely Flask or a similar framework) for the backend, with a basic function and loop demonstration. Throughout, the emphasis is on hands-on learning and building a variety of applications to solidify understanding.

    Python Fundamentals Study Guide

    Quiz

    1. What is the primary purpose of the print() function in Python? Provide a short example of its usage. The print() function in Python is used to display output to the console. For example, print(“Hello, world!”) will display the text “Hello, world!” on the screen.
    2. Explain the difference between an integer and a float in Python. Give an example of each. An integer in Python represents whole numbers without any decimal points (e.g., age = 25). A float represents numbers that can have decimal points (e.g., height = 5.9).
    3. How do you assign a value to a variable in Python? Is Python a statically or dynamically typed language? In Python, you assign a value to a variable using the assignment operator =. For example, name = “Alice”. Python is a dynamically typed language, meaning you don’t need to explicitly declare the data type of a variable, and the type can change during the program’s execution.
    4. What is a string in Python, and how can you concatenate two strings? A string in Python is a sequence of characters enclosed in single or double quotes (e.g., “hello”, ‘world’). You can concatenate two or more strings using the + operator or by using commas within the print() function.
    5. Describe the purpose of a for loop and a while loop in Python. Give a simple example of a for loop iterating through a range of numbers. A for loop is used to iterate over a sequence (like a list, string, or range) a specific number of times. A while loop continues to execute as long as a specified condition is true. Example of a for loop: for i in range(3): print(i) (This will print 0, 1, and 2).
    6. Explain the role of if, elif, and else statements in Python. if statements are used to execute a block of code only if a certain condition is true. elif (else if) allows you to check additional conditions if the preceding if or elif conditions were false. else provides a block of code to execute if none of the preceding if or elif conditions were true.
    7. What is a function in Python, and why are functions useful? A function in Python is a block of reusable code that performs a specific task. Functions are useful for organizing code, making it more readable, and avoiding repetition.
    8. What is a list in Python, and how can you access elements within a list? Provide an example. A list in Python is an ordered collection of items, enclosed in square brackets []. You can access elements within a list using their index, starting from 0. Example: my_list = [10, 20, 30]; print(my_list[0]) (This will print 10).
    9. Explain the purpose of the try and except blocks in Python. Provide a brief scenario where they would be useful. The try and except blocks are used for error handling in Python. The code that might raise an exception is placed inside the try block, and the code that handles the exception is placed inside the except block. This is useful when you anticipate that certain operations might fail, such as trying to convert user input to an integer.
    10. What is a module in Python, and how do you import one? Give an example of importing and using a function from a built-in module. A module in Python is a file containing Python code that defines functions, classes, and variables. You import a module using the import keyword. Example: import math; print(math.sqrt(16)) (This imports the math module and uses the sqrt() function to calculate the square root of 16).

    Essay Format Questions

    1. Discuss the fundamental data types in Python (integers, floats, strings, booleans) and provide scenarios where each data type would be most appropriately used in a programming task.
    2. Compare and contrast the use of for loops and while loops in Python for iterative tasks. Provide examples of situations where one type of loop might be preferred over the other.
    3. Explain the concept of conditional execution in Python using if, elif, and else statements. Describe how nested conditional statements can be used to handle more complex decision-making processes.
    4. Discuss the benefits of using functions in Python programming. Explain how to define a function, pass arguments to it, and return values. Provide an example of a function that solves a specific problem.
    5. Explain the importance of error handling in Python using try and except blocks. Describe different types of exceptions that might occur and how to handle them gracefully to prevent program crashes.

    Glossary of Key Terms

    • Variable: A named storage location in memory that holds a value.
    • Data Type: The classification of data that determines the possible values and operations that can be performed on it (e.g., integer, float, string, boolean).
    • Integer (int): Whole numbers without a decimal point.
    • Float: Numbers with a decimal point.
    • String (str): A sequence of characters.
    • Boolean (bool): A value that is either True or False.
    • Operator: A symbol that performs an operation on one or more operands (e.g., +, -, *, /, =, ==, >, <).
    • Expression: A combination of variables, literals, and operators that evaluates to a single value.
    • Statement: A unit of code that performs an action.
    • Function: A block of organized, reusable code that performs a specific task.
    • Argument: A value passed to a function when it is called.
    • Return Value: The value that a function sends back to the caller after it has finished executing.
    • Loop: A control flow structure that allows a block of code to be executed repeatedly (e.g., for loop, while loop).
    • Iteration: A single pass through a loop.
    • Conditional Statement: A statement that allows different blocks of code to be executed based on whether a condition is true or false (e.g., if, elif, else).
    • List: An ordered, mutable (changeable) collection of items.
    • Index: The position of an item within a sequence (like a list or string), starting from 0.
    • Module: A file containing Python definitions and statements.
    • Import: The process of making the code in one module available for use in another.
    • Exception: An error that occurs during the execution of a program.
    • Error Handling: The process of anticipating and responding to errors during program execution, often using try and except blocks.
    • Syntax: The set of rules that define the structure of a programming language.
    • Indentation: The spaces used at the beginning of a code line to define blocks of code in Python.
    • Concatenation: The operation of joining two or more strings together.
    • Comment: A note added to the code to explain it, which is ignored by the Python interpreter (indicated by #).

    Python Setup and Fundamentals Guide

    Okay, I’ve reviewed the provided excerpts from “01.pdf”. Here’s a detailed briefing document outlining the main themes and important ideas:

    Briefing Document: Python Setup and Basics

    Source: Excerpts from “01.pdf”

    Main Theme: This source provides a step-by-step guide for setting up a Python development environment using VS Code, configuring a code formatter, and introduces fundamental Python concepts like variables, data types, basic operations, string manipulation, type conversion, user input, conditional statements, and loops. It also touches upon functions, data structures (lists, dictionaries, sets, tuples – briefly), error handling, and built-in modules.

    Key Ideas and Facts:

    1. Development Environment Setup (VS Code):

    • Installation: The guide walks through a basic installation process, mentioning specific steps for Windows users: “if you’re on Windows you would like to check these two items by default they are unchecked and you would like to check them”. Mac users are also addressed, noting minimal differences.
    • Workspace: VS Code is recommended as the coding workspace, and instructions are given on how to download it.
    • Python Extensions: Several essential VS Code extensions for Python development are highlighted:
    • AutoPEP 8: A formatter extension.
    • Python Debugger: For debugging Python code.
    • Python: The core Python language support.
    • Pylance: A language server providing rich features.
    • Code Formatting Configuration: Configuration for AutoPEP 8 is explained. Users need to open VS Code’s command palette (Ctrl+Shift+P or Cmd+Shift+P), search for “Open User Settings (JSON)”, and then paste a specific JSON snippet at the bottom of the file. This enables automatic code formatting upon saving: “what this will do whenever we save uh whenever we save our Python files it will actually format the file so that our code looks clean”.
    • Folder Icons: The “Great Icons” extension is mentioned for improved folder and file icons in VS Code.
    • Terminal Access: Instructions on how to open the terminal within VS Code using Ctrl+J or Cmd+J are provided.
    • Python Installation Verification: Users are instructed to check if Python is installed correctly by running specific commands in the terminal:
    • Windows: python –version (although the excerpt just says python)
    • Mac: python3 –version
    • A successful installation will display the Python version; errors like “command not found” indicate an issue.

    2. Basic Python Concepts:

    • Variables and Data Types:Strings: Used for storing text, assigned directly (e.g., name = “Alex”). Python doesn’t require keywords like const or var as in JavaScript.
    • Integers: Used for whole numbers (e.g., age = 25).
    • Floats: Used for decimal numbers (e.g., height = 9.5).
    • Booleans: Represent true or false values, which must be capitalized (True or False).
    • Print Statement: The print() function is introduced for displaying output.
    • String Concatenation:Using the + operator requires manual addition of spaces.
    • Using a comma , automatically inserts a space between the concatenated items.
    • F-strings are also demonstrated for cleaner string formatting: f”Hey my name is {name}”.
    • String Slicing: Accessing individual characters or substrings using index (e.g., name[0] for the first character, name[-1] for the last).
    • String Methods: Examples include .upper() for converting to uppercase and .lower() for converting to lowercase.
    • Basic Operations: Arithmetic operations (+, -, *, /, %, **) are demonstrated with integer variables.
    • Assignment Operators: Shorthand operators like x += 15 are explained as equivalent to x = x + 15.
    • Comments: Using the # symbol for single-line comments.
    • Order of Operations: The standard mathematical order of operations (PEMDAS/BODMAS) is implicitly followed.
    • Floor Division: The // operator performs division and rounds down to the nearest whole number.
    • Multiple Assignments: Assigning values to multiple variables in a single line (e.g., i, j, k = 1, 2, 3).
    • Swapping Values: A concise way to swap variable values (e.g., m, n = n, m).
    • Comparison Operators: Checking equality (==), inequality (!=), greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=).
    • Logical Operators: and, or, and not are used to combine or negate boolean expressions.
    • String Slicing (Advanced): Demonstrating slicing with start, end, and step (e.g., text[1:6], text[7:], text[::-1] for reversing).
    • String Formatting (.format() method): Using placeholders {} and .format() to insert values into strings, including ordered and indexed placeholders.
    • Math Module: Importing and using the math module for functions like math.pi, math.sqrt(), math.floor(), math.ceil(), and math.pow(). The built-in round() function is also shown.
    • Type Conversion: Converting between data types using functions like int(), str(), and float(). The type() function is used to check the data type of a variable.

    3. User Input and Output:

    • input() function: Used to get input from the user. The input is always treated as a string by default.
    • Prompting the User: Providing messages within the input() function to guide the user.
    • Type Conversion of Input: Emphasizing the need to convert input to the desired data type (e.g., int()) before performing operations.
    • Working with Multiple Inputs: Using the .split() method to handle multiple values entered on a single line, separated by a delimiter (like a space).
    • Default Values: Setting default values for variables based on user input (e.g., checking for an empty string).
    • len() function: Used to get the length of a string.

    4. Conditional Statements (if, elif, else):

    • Syntax and Indentation: Python uses indentation (spaces or tabs) to define blocks of code within conditional statements. No curly braces are used.
    • Basic if statement: Executing a block of code if a condition is true.
    • else statement: Executing a block of code if the if condition is false.
    • elif statement: Checking multiple conditions in sequence.
    • Nested Conditionals: Placing if statements inside other if or else blocks.
    • Logical Operators in Conditionals: Using and, or, and not to create more complex conditions.
    • in operator: Checking if a value exists within a sequence (like a list or string).
    • Ternary Operator: A concise way to write simple if-else statements in a single line.
    • Comparing Strings: Using == to check if two strings are equal.
    • pass statement: A null operation; it does nothing. Used as a placeholder where a statement is syntactically required but no action needs to be taken.
    • Truthy and Falsy Values: Checking the boolean value of a variable or expression (e.g., an empty string is falsy).

    5. Loops (for, while):

    • for loop: Iterating over a sequence (like a range of numbers, a list, or characters in a string).
    • range() function: Generating a sequence of numbers.
    • Iterating in reverse order using range() with a negative step.
    • Looping through a list.
    • Reversing a list using reversed().
    • enumerate(): Getting both the index and the value while iterating.
    • Looping through dictionaries using .items() to access key-value pairs.
    • List Comprehension: A concise way to create lists based on existing iterables.
    • zip(): Iterating over multiple lists in parallel.
    • while loop: Executing a block of code as long as a condition is true.
    • break statement: Exiting out of a loop prematurely.
    • continue statement: Skipping the current iteration and moving to the next one.
    • Infinite Loops: Creating loops that run indefinitely (using while True) and using break to control their execution.

    6. Functions (def):

    • Definition: Functions are defined using the def keyword, followed by the function name, parentheses for parameters, and a colon.
    • Reusability: Functions allow for code to be reused multiple times.

    7. Data Structures (Brief Introduction):

    • Lists: Ordered, mutable collections of items, created using square brackets []. Basic operations like accessing elements by index, updating elements, appending, removing, slicing, concatenation, and repetition are demonstrated.
    • Dictionaries: Unordered collections of key-value pairs, created using curly braces {}.
    • Sets: Unordered collections of unique elements, created using curly braces {} or the set() constructor. Duplicates are automatically removed.
    • Tuples: Ordered, immutable collections of items, created using parentheses (). They cannot be changed after creation.

    8. Error Handling (try, except, finally):

    • try block: The code that might raise an exception is placed inside the try block.
    • except block: Specifies how to handle a particular type of exception (e.g., ValueError, ZeroDivisionError). Multiple except blocks can be used to handle different errors.
    • finally block: The code inside the finally block is always executed, regardless of whether an exception occurred or not.

    9. Built-in Modules (import):

    • random module: Used for generating random numbers (random.randint()), choosing random elements from a sequence (random.choice()), and shuffling sequences (random.shuffle()).
    • time module: Used for time-related operations (time.sleep() for pausing execution).
    • sys module: Provides access to system-specific parameters and functions (e.g., sys.version for Python version, sys.platform for the operating system).
    • string module: Contains useful string constants (e.g., string.ascii_lowercase, string.ascii_uppercase, string.digits, string.punctuation).
    • math module: Provides mathematical functions.

    Overall Impression:

    The excerpts provide a comprehensive initial guide to setting up a Python development environment and learning the fundamental building blocks of the Python language. It covers practical aspects like environment configuration and essential programming concepts with clear explanations and code examples. The inclusion of exercises and project introductions at the end suggests a hands-on approach to learning.

    Python Setup and Basic Concepts

    Installation and Setup

    Q1: How do I install the necessary software for this course? For this course, you will need to install Python. If you are on Windows, during the installation process, make sure to check the options to add Python to your PATH and install pip. On macOS, you would typically use python3 –version in the terminal to check if Python 3 is installed. For the coding workspace, Visual Studio Code (VS Code) is recommended. You can download it from the official website.

    Q2: Are there any recommended VS Code extensions for Python development? Yes, several VS Code extensions are highly recommended for Python development in this course. These include:

    • Python: Provides language support for Python.
    • Python Debugger: Enables debugging of Python code within VS Code.
    • Pylance: An extension that offers rich type information, autocompletion, and error checking.
    • AutoPEP 8: A formatter extension that automatically formats your Python code to adhere to the PEP 8 style guide.
    • Great Icons (optional): Enhances the appearance of folders and files in the VS Code explorer.

    Q3: How do I configure VS Code to automatically format Python code on save? To configure automatic code formatting on save, you need to modify the VS Code user settings (settings.json) file. You can open this file by pressing Cmd+Shift+P (macOS) or Ctrl+Shift+P (Windows) to open the command palette, then type “Open User Settings (JSON)” and select it. At the bottom of this JSON file, paste the following configuration:

    “[python]”: {

    “editor.formatOnSave”: true,

    “editor.codeActionsOnSave”: {

    “source.organizeImports”: true

    }

    },

    “python.formatting.provider”: “autopep8”

    Ensure that the autopep8 extension is installed for this to work. After pasting and saving this configuration, your Python files will be automatically formatted every time you save them.

    Basic Python Concepts

    Q4: What are the basic data types introduced in this course? The basic data types covered at the beginning of the course include:

    • Strings (str): Used to store text, enclosed in single or double quotes (e.g., “Alex”, ‘Hello’).
    • Integers (int): Used for whole numbers (e.g., 25).
    • Floats (float): Used for decimal numbers (e.g., 9.5).
    • Booleans (bool): Represent true or false values (True or False, with the first letter capitalized).

    Q5: How do I perform basic operations in Python? Python supports various basic operations, including:

    • Arithmetic Operations: Addition (+), subtraction (-), multiplication (*), division (/), modulus (remainder of division, %), and exponentiation (**).
    • Assignment Operations: Assigning a value to a variable (=), and shorthand assignment operators like +=, -=, *=, etc. (e.g., x += 15 is equivalent to x = x + 15).
    • Comparison Operators: Equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators return boolean values.
    • Logical Operators: and, or, and not are used to combine or negate boolean expressions.

    Q6: How can I get input from the user and display output in Python? You can get input from the user using the input() function. This function displays a prompt to the user and returns their input as a string. If you need to work with the input as a number, you’ll need to convert it using functions like int() or float(). To display output, you use the print() function, which can take strings, variables, or a combination of both. You can concatenate strings using the + operator or use commas within the print() function for automatic spacing. F-strings (formatted string literals) provide a concise way to embed expressions inside string literals by prefixing the string with f or F and placing expressions inside curly braces {}.

    Q7: What are conditional statements (if/elif/else) and how are they used in Python? Conditional statements in Python allow you to execute different blocks of code based on whether certain conditions are true or false.

    • if statement: Executes a block of code if a specified condition is true.
    • elif statement: Short for “else if,” allows you to check multiple conditions in sequence. It is executed if the preceding if or elif condition was false and the current elif condition is true.
    • else statement: Executes a block of code if all preceding if and elif conditions were false.

    Python uses indentation (whitespace) to define the blocks of code associated with each conditional statement. Proper indentation is crucial for the correct execution of your code. You can also use logical operators (and, or, not) to create more complex conditions within your if, elif statements. Nested conditionals (if statements inside other if statements) are also supported.

    Q8: What are loops (for and while) and how can I use them to iterate through data? Loops in Python are used to repeatedly execute a block of code. There are two main types of loops:

    • for loop: Used to iterate over a sequence (such as a list, tuple, string, or range). It executes a block of code for each item in the sequence. The range() function is commonly used to generate a sequence of numbers for iteration. You can also iterate through lists in reverse using reversed() and get both the index and the item using enumerate().
    • while loop: Executes a block of code as long as a specified condition is true. It’s important to ensure that the condition eventually becomes false to avoid infinite loops.

    You can control the flow of loops using break (to exit the loop prematurely) and continue (to skip the current iteration and move to the next). Python also supports list comprehensions, which provide a concise way to create lists based on existing iterables.

    The Foundational ‘Print Hello World’ Program

    The sources indicate that print hello world is a very first basic program. The process to execute this program, as outlined in the sources, involves the following steps:

    • Saving the file: After writing the code print hello world, the user needs to save the file.
    • Navigating to the directory: Using the command line, the user should change the current directory (cd) to the folder where the saved file is located.
    • Running the file: The program is executed using the Python interpreter. The command to run the file is python <filename> on Windows and python3 <filename> on Mac.

    The source shows an example where, after navigating to the correct folder and running the file named (implicitly) containing print hello world, the output “hello world” is displayed in the terminal. This demonstrates that the application is working correctly.

    Using the ‘cd’ Command to Change Directories

    Based on the sources, the command cd is used to change the current directory in the command line or terminal.

    Here’s how it is discussed and used in the provided material:

    • After saving a Python file, to run it from the command line, you first need to navigate to the directory where the file is saved. This is achieved using the cd command.
    • Source provides an example of this process: “so what do we want to do just like we are here right we would like to cd into this folder and then we would like to run this file right which is this one…”. This indicates that before executing the Python script, the user used cd followed by the directory name to move into that specific folder in the terminal.

    In summary, the sources highlight that cd is a fundamental command for navigating the file system via the command line, which is a necessary step before executing a Python script that has been saved in a particular directory. After using cd to move to the correct directory, you can then run the Python file using the python <filename> (on Windows) or python3 <filename> (on Mac) command.

    Running Python Files: A How-To Guide

    Based on the sources, to run a Python file, you generally need to follow these steps:

    1. Save the file. After writing your Python code, you must save it with a .py extension (e.g., my_script.py).
    2. Navigate to the directory. Open your command line or terminal and use the cd command to change the current directory to the folder where you saved your Python file. We have previously discussed the cd command and its purpose in navigating the file system [History].
    3. Execute the file. Once you are in the correct directory, you can run the Python file using the Python interpreter. The command you use depends on your operating system:
    • On Windows, you typically use the command: python <filename>.py.
    • On Mac OS or Linux, you usually use the command: python3 <filename>.py.
    1. Replace <filename>.py with the actual name of your saved Python file. For example, to run a file named hello.py, you would use python hello.py (on Windows) or python3 hello.py (on Mac/Linux).

    The sources provide numerous examples of this process in action:

    • Source demonstrates running a file containing print hello world by first using cd to enter the folder and then executing it with python3 followed by the filename.
    • Subsequent sources like,,,,,,,,,,,, and consistently show the use of python3 <filename>.py or python <filename>.py after presumably navigating to the correct directory to execute the Python scripts for various projects.

    If Python is not correctly installed or the system cannot find the python or python3 command, you might encounter an error like “command not found”. In such cases, you need to ensure that Python is installed properly and that its installation directory is added to your system’s PATH environment variable. You can check if Python is installed correctly by running python –version (on Windows) or python3 –version (on Mac) in your terminal. If you see the Python version displayed, it indicates a successful installation.

    Python Variables: Definition, Types, and Manipulation

    Based on the source “01.pdf”, let’s discuss variables in Python.

    In Python, a variable is a name that refers to a value. You use variables to store and manipulate data within your programs. Here are some key aspects of variables in Python as highlighted in the source:

    • Assignment: You create a variable and assign it a value using the assignment operator =. For example:
    • name = “Alex”
    • age = 25
    • height = 9.5
    • is_student = True
    • As shown, you simply write the variable name on the left and the value you want to store on the right of the = sign.
    • No Explicit Declaration: Unlike some other programming languages, Python does not require you to explicitly declare the data type of a variable before you assign a value to it. The data type is inferred automatically based on the value you assign. In the examples above, name becomes a string, age an integer, height a float, and is_student a boolean.
    • Basic Data Types: The source introduces several fundamental data types that variables can hold:
    • Strings (str): Used to store sequences of characters or text. They are typically enclosed in single or double quotes (e.g., “hello world”, ‘Alex’).
    • Integers (int): Used for whole numbers without a decimal point (e.g., 25).
    • Floats (float): Used for numbers with a decimal point (e.g., 9.5, 29.99).
    • Booleans (bool): Represent truth values, either True or False. Note that True and False are capitalized in Python.
    • Naming Conventions: The source emphasizes that the standard convention for naming variables in Python is to use snake case. This means that variable names are written in lowercase, and words are separated by underscores (e.g., first_name, user_choice, years_to_100). This is different from the camel case convention (e.g., firstName) used in languages like JavaScript.
    • String Manipulation: If a variable holds a string, you can perform various operations on it:
    • Concatenation: You can combine strings using the + operator. When concatenating with +, you need to explicitly include any desired spaces.
    • Comma in print(): When using a comma , to separate multiple items within a print() statement, Python automatically inserts a space between them.
    • Indexing and Slicing: You can access individual characters in a string using their index (starting from 0 for the first character) within square brackets []. You can also extract substrings using slicing (e.g., text[1:6]).
    • String Methods: Python provides built-in methods to modify or inspect strings, such as .upper() (convert to uppercase), .lower() (convert to lowercase), .capitalize() (capitalize the first letter), .replace() (replace substrings), and len() (get the length of the string).
    • Case Sensitivity: Python is a case-sensitive language. This means that variables with the same spelling but different capitalization are treated as distinct variables (e.g., name and Name would be two different variables). This also applies when comparing strings.
    • Multiple Assignments: Python allows you to assign the same value to multiple variables or different values to multiple variables in a single line:
    • x = y = z = 0
    • i, j, k = 1, “hello”, True
    • Swapping Variable Values: You can easily swap the values of two variables in one line using simultaneous assignment:
    • m = 10
    • n = 20
    • m, n = n, m # Now m is 20 and n is 10
    • Type Conversion: You can change the data type of a variable using built-in functions like int(), float(), and str(). For example, to convert a string representing a number to an integer, you can use age_number = int(age_string). Similarly, you can convert a float to an integer using price_int = int(price_float), which truncates the decimal part. It’s important to note that attempting to convert a value to an incompatible type can result in a ValueError, as demonstrated when trying to perform arithmetic between an integer and a string.
    • f-strings (Formatted String Literals): The source introduces f-strings as a concise and readable way to embed expressions, including variable names, directly within string literals. You create an f-string by prefixing a string with the letter f or F and then placing expressions you want to evaluate inside curly braces {}. For example:
    • first_name = “John”
    • last_name = “Doe”
    • full_name = f”{first_name} {last_name}”
    • print(f”Hey my name is {first_name} and my last name is {last_name}”)
    • f-strings are generally preferred over older methods of string formatting like concatenation with + because they are more readable and efficient.

    In summary, variables in Python are dynamically typed and are used to store various data types. Python has specific conventions for naming variables (snake case) and offers flexible ways to manipulate the data stored in them, especially strings. Understanding these fundamental concepts is crucial for writing any Python program.

    Python String Slicing Explained

    Based on the sources, string slicing is a technique used to extract a substring (a contiguous sequence of characters) from a larger string by specifying a range of indices.

    Here’s a breakdown of string slicing as discussed in the sources:

    • Basic Syntax: To slice a string, you use square brackets [] following the string variable name. Inside the brackets, you specify the start and end indices, separated by a colon :.
    • text = “Python programming”
    • substring = text[start:end]
    • Start Index: The start index indicates the position where the slice begins. Python uses zero-based indexing, meaning the first character of the string is at index 0. If the start index is omitted, it defaults to 0 (the beginning of the string).
    • End Index: The end index indicates the position where the slice ends. However, it’s crucial to understand that the character at the end index is not included in the resulting substring. The slice goes up to, but does not include, the character at the end index. If the end index is omitted, the slice goes to the end of the string.
    • Example: As shown in source:
    • text = “Python programming”
    • print(text[0:6]) # Output: Python (characters from index 0 up to, but not including, index 6)
    • Here, the slice starts at index 0 (‘P’) and ends before index 6 (‘ ‘).
    • Slicing to the End: To get a substring from a specific index to the end of the string, you can omit the end index:
    • text = “Python programming”
    • print(text[7:]) # Output: programming (characters from index 7 to the end)
    • Negative Indexing: Python allows you to use negative indices to refer to characters from the end of the string. -1 refers to the last character, -2 to the second-to-last, and so on. You can use negative indices in slicing as well:
    • text = “Python programming”
    • print(text[-1]) # Output: g (the last character)
    • Step Value: You can also include a third optional argument in the slice, the step value, separated by another colon. This specifies the increment between indices:
    • text = “Python programming”
    • print(text[0:10:2]) # Output: Pto r (every second character from index 0 to 9)
    • Reversing a String: A common use case for slicing with a step is to reverse a string by using a step of -1 and omitting the start and end indices:
    • text = “Python programming”
    • print(text[::-1]) # Output: gnimmargorp nohtyP

    In summary, string slicing provides a powerful and flexible way to access and manipulate parts of strings in Python by specifying the start, end, and optionally the step of the desired substring. The end index is always exclusive.

    Python for Beginners – Build 35 Resume Projects

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Python Fundamentals: Variables, Data Types, Operators, and Functions

    Python Fundamentals: Variables, Data Types, Operators, and Functions

    These excerpts provide a comprehensive introduction to Python programming concepts. They cover fundamental data types like numbers, strings, tuples, lists, dictionaries, and sets, explaining their properties and operations. The text also explores essential programming structures such as variables, operators, conditional statements, loops, functions (including predefined, user-defined, anonymous, and recursive), modules, packages, and exception handling. Finally, it touches upon file handling for various data formats and introduces the basics of object-oriented programming, including classes, objects, methods, inheritance, and polymorphism.

    Python Basics and Data Structures Study Guide

    Quiz

    1. Explain the concept of a variable in Python. How does Python handle data types for variables?
    2. What are the key differences between the core numeric data types in Python: integer, float, and complex?
    3. Describe the characteristics of a string in Python. What does it mean for a string to be immutable?
    4. Explain the concept of indexing and slicing in Python sequences (like strings, lists, and tuples). Provide a brief example.
    5. What are the primary differences between a list and a tuple in Python? When might you choose to use one over the other?
    6. Describe the structure of a dictionary in Python. How are elements accessed in a dictionary?
    7. What is a set in Python, and what are its key properties? How does it differ from a list?
    8. Explain the purpose of the if, elif, and else statements in Python. How do they control the flow of execution?
    9. Describe the functionality of a for loop in Python. How is it used to iterate over sequences?
    10. What is a function in Python? How do you define and call a function?

    Quiz Answer Key

    1. A variable in Python is a named storage location that holds a value. Python is dynamically typed, meaning you don’t need to explicitly declare the data type of a variable; the interpreter infers the type based on the value assigned to it.
    2. Integers (int) are whole numbers without a decimal point. Floats (float) are numbers with a decimal point. Complex numbers (complex) have a real and an imaginary part, represented in the form a+bj.
    3. A string in Python is an ordered sequence of characters enclosed in quotes (single or double). Immutability means that once a string is created, its individual characters cannot be changed directly. Any operation that appears to modify a string actually creates a new string.
    4. Indexing allows you to access individual elements in a sequence using their position (starting from 0). Slicing allows you to extract a subsequence of elements using a start index, an end index (exclusive), and an optional step. Example: my_list = [10, 20, 30, 40]; print(my_list[1]) (output: 20), print(my_list[1:3]) (output: [20, 30]).
    5. A list is a mutable, ordered sequence enclosed in square brackets, allowing for element modification, addition, and removal. A tuple is an immutable, ordered sequence enclosed in parentheses, meaning its elements cannot be changed after creation. Tuples are often used for fixed collections of items.
    6. A dictionary in Python is an unordered collection of key-value pairs enclosed in curly braces. Each key must be unique and immutable, and it is used to access its corresponding value. Elements are accessed using their keys within square brackets, e.g., my_dict[‘key’].
    7. A set in Python is an unordered collection of unique elements enclosed in curly braces. Sets automatically remove duplicate entries and support mathematical set operations like union, intersection, and difference. Unlike lists, sets do not maintain the order of elements and cannot be indexed directly.
    8. The if statement executes a block of code if a specified condition is true. The elif (else if) statement checks an additional condition only if the preceding if or elif condition was false. The else statement executes a block of code if none of the preceding if or elif conditions were true.
    9. A for loop in Python is used to iterate over each item in a sequence (such as a list, tuple, string, or range) or other iterable object. It executes a block of code for each item in the sequence. Example: for item in my_list: print(item).
    10. A function in Python is a block of reusable code that performs a specific task. It is defined using the def keyword followed by the function name, parentheses for parameters (optional), and a colon. A function is called by using its name followed by parentheses, potentially passing arguments.

    Essay Format Questions

    1. Discuss the importance of data structures in Python. Compare and contrast the use cases for lists, tuples, dictionaries, and sets, providing specific examples where each would be most appropriate.
    2. Explain the concepts of mutability and immutability in Python, focusing on how these properties affect the behavior and usage of different data types like strings, lists, and tuples.
    3. Describe the role of control flow statements (conditional statements and loops) in Python programming. Illustrate with examples how if/elif/else and for loops can be used to solve common programming problems.
    4. Discuss the benefits of using functions in Python. Explain the process of defining and calling functions, and elaborate on the concepts of function parameters and return values.
    5. Imagine you are developing a program to manage a small library. Describe how you might use various Python data structures (lists, dictionaries, sets, tuples) to store and manipulate information about books and borrowers.

    Glossary of Key Terms

    • Variable: A named storage location that holds a value in memory.
    • Data Type: The classification of data that specifies which type of value a variable can hold and what types of operations can be performed on it (e.g., integer, string, list).
    • Integer (int): A whole number without a decimal point.
    • Float (float): A number with a decimal point.
    • String (str): An ordered sequence of characters.
    • Immutable: A property of an object whose state cannot be modified after it is created.
    • Mutable: A property of an object whose state can be modified after it is created.
    • Index: The position of an element within a sequence, starting from 0.
    • Slicing: A way to extract a subsequence of elements from a sequence using a range of indices.
    • List (list): A mutable, ordered sequence of items enclosed in square brackets.
    • Tuple (tuple): An immutable, ordered sequence of items enclosed in parentheses.
    • Dictionary (dict): A mutable, unordered collection of key-value pairs enclosed in curly braces.
    • Set (set): A mutable, unordered collection of unique elements enclosed in curly braces.
    • Conditional Statement: A statement that executes a block of code based on whether a condition is true or false (if, elif, else).
    • Loop: A control flow statement that allows code to be executed repeatedly (for, while).
    • Function: A block of organized, reusable code that performs a specific task.
    • Parameter: A variable listed inside the parentheses in the function definition.
    • Argument: The actual value that is passed to a function when it is called.
    • Return Value: The value that a function sends back to the caller after it has executed.
    • Module: A file containing Python definitions and statements (with a .py extension).
    • Package: A directory that contains multiple Python modules and an __init__.py file, used to organize modules.
    • Identifier: A name used to identify a variable, function, class, module, or other object.
    • Keyword: A reserved word that has a special meaning in Python and cannot be used as an identifier.
    • Indentation: The spaces or tabs used at the beginning of a code line to define blocks of code in Python.
    • Operator: A symbol that performs an operation on one or more operands (e.g., +, -, *, /, ==, >).
    • Operand: The value or variable on which an operator acts.
    • Expression: A combination of variables, operators, and values that evaluates to a single result.
    • Statement: A complete line of code that performs an action.
    • String Formatting: The process of embedding expressions inside string literals.
    • Escape Sequence: A sequence of characters that represents a special character (e.g., \n for newline, \t for tab).
    • Raw String: A string literal prefixed with r or R, where backslashes are treated as literal characters.
    • Concatenation: The operation of joining two or more strings or other sequences together.
    • Repetition: The operation of repeating a string or other sequence multiple times.
    • Method: A function that is associated with an object of a particular class.
    • Class: A blueprint for creating objects, defining their attributes (data) and methods (behavior).
    • Object: An instance of a class.
    • Recursion: A programming technique where a function calls itself.
    • Anonymous Function (Lambda): A small, unnamed function defined using the lambda keyword.
    • Exception Handling: A mechanism to deal with runtime errors gracefully using try, except, else, and finally blocks.
    • File Handling: The process of reading from and writing to files.
    • Mode (File Handling): A string indicating how a file is opened (e.g., ‘r’ for read, ‘w’ for write, ‘a’ for append).
    • Pickle: A Python module used for serializing and de-serializing Python object structures.
    • JSON: A lightweight data-interchange format.
    • CSV: Comma Separated Values, a file format for storing tabular data.
    • OS Module: A built-in Python module that provides a way of using operating system dependent functionality.
    • Path (File System): A string that specifies the location of a file or directory in a file system.

    Briefing Document: Review of Python Basics and Data Types

    This briefing document summarizes the main themes and important ideas discussed in the provided source “01.pdf,” which covers fundamental concepts in Python programming, including variables, data types, operators, control flow (conditional statements and loops), functions, modules and packages, exception handling, file handling, and the OS module.

    1. Variables and Data Types:

    • Variables as Memory Allocation: A variable in Python is essentially a named memory location used to store data. Each variable has an identification number (obtained using the id() function) that represents its memory address.
    • “variable is nothing but a memory allocation if it’s a memory allocation which store the data that means it release some identification number as well so we can easily find it out like a ID there is the ID function where find it out the identification number when I pass ID of a so you can see here this is the ident identification number”
    • Data Type Dependency on Memory ID: The identification number (memory address) allocated to a variable can depend on its data type. Variables with different data types are likely to have significantly different ID numbers.
    • “reason we change the data type so it release the memory allocation memory uh ID depend on their data type if I’m taking the different data type the that will also be number D is equal to 19 + 19.8 and check the ID of D that will be the uh different identification number it’s not only the few digit is change so lots of values change also reason we change the data type because the D is a floating data type”
    • Main Categories of Data Types: Python has several built-in data types categorized as:
    • Numeric: Integer (int), Complex, Float (float).
    • “numeric dictionary Boolean set and sequence type where we have the main data type is a integer complex and Float integer complex and Float”
    • Dictionary: Key-value pairs.
    • “if I Define the same value with key value pair is equal to key value pair like 5 colon 4 uh 4 colon 7 and 9 colon 8 so if you define like this that will be considered as a dictionary”
    • Boolean: True or False.
    • “dictionary bullan set is also there”
    • Set: Unordered collection of unique elements.
    • “if I Define with the curly brasses curly brasses directly passing the values that is considered as a set”
    • Sequence Type: Ordered collections:
    • String (str): Sequence of characters enclosed in single or double quotes.
    • “if I’m defining with a double quotes so like hello here so that’s a string data type”
    • List (list): Ordered, mutable sequence of items enclosed in square brackets.
    • “you define B is equal to in square bracket that will consider as a that will consider as a list”
    • Tuple (tuple): Ordered, immutable sequence of items enclosed in round brackets.
    • “if I Define with a round bracket that will be considered as a tle”
    • Comments: The hash symbol (#) is used to denote comments in Python code, which are ignored by the interpreter.
    • “what exactly the mean of this hash this hash symbol is nothing but a comment we are defining as a comment”

    2. Keywords:

    • Reserved Words: Keywords are reserved words in Python that have specific meanings and cannot be used as identifiers (variable names, function names, class names).
    • “keyword is reserve word which used for one specific purpose fake purpose cannot be used as a identifier cannot be used as a identifier now the question is what exactly the mean of identifier let me make as a uh markdown so that markdown in the sense it will taking one statement it will not consider as a code okay normal statements so cannot be used as a identifier so what is the identifier identifier so identifier in the sense any variable name any um you know uh function name okay any variable name any function name any class name that will be consider as a identifier class name that will be considered as a identifier”
    • Checking Keywords: The keyword module provides access to the list of Python keywords. keyword.kwlist returns a list of all keywords.
    • “there is one module is called as keyword module keyword module keyword module and when you pass it here keyword do KW list then you’ll find it out how many keyword is available for false none true the lots of keyword is available is Lambda lots of keyword is there which is stored with a square bracket one container and that container we are calling list”
    • Number of Keywords: The number of keywords can vary between Python versions. In the described context (likely Python 3.9), there are 35 keywords.
    • “when I run it will showing you 30 five keyword is available okay so whenever you using any uh ideally right now I’m using the Jupiter notebook ideally so that time you can easily check it”
    • Identifier Restrictions: Attempting to use a keyword as an identifier will result in a syntax error.
    • “can I use uh any keyword like uh if I’m using if Okay small i f if we are using IF is equal to 8 can I define it like this no it will giving the error because because this if is reserved for only the conditional statement you can’t use as a identifier you can’t use as a normal variable”

    3. Indentation:

    • Block Definition: Indentation (using whitespace, typically four spaces in Jupiter Notebook and PyCharm) is crucial in Python to define code blocks, such as the body of conditional statements (if, elif, else) and loops.
    • “indentation in the sense is Define the block I have some program uh like if program uh conditional statement program if a is greater than let me first Define it a is equal to 9 and B is equal to 8 and I have a program is if a is greater than b okay so in other programming language we are using the curly brasses and put inside a statements so here curly brasses is not acceptable so we have a indentation we have to use a colon after the colon it automatically taking the four space okay automatically taking the four space”
    • Block Scope: Statements within the same indented block are considered part of that block. Changes in indentation signify the beginning or end of a block.
    • “if I’m writing here the hello okay so if you want to Define anything like hello if I’m writing here and hi if I’m writing here and he if I’m writing here print he okay this hello hi hey yeah we can say line number 5 six 7 that’s comes in if block so defining the block we are using if uh colon and automatically taking the for space if you’re using the back space and write it down here print by so that line number nine will consider as a uh outside of the block so that bu is not in if condition okay by is not in if condition”

    4. Input and Output:

    • input() Function: The input() function is used to obtain user input from the console. It always returns the input as a string.
    • “we have another function is input function input function will always take the input as a string”
    • Type Conversion: To treat the input as a different data type (e.g., integer), type conversion functions like int(), float(), etc., must be used.
    • “so cannot be multiplied with string and integer so we have another function is int so likewise other data type like int float complex we have a function as well I’m using int open the bracket and close it here when I run it it asking the question enter the value of x I’m passing here five then it will be calculate and showing the answer is five”
    • print() Function: The print() function is used to display output to the console. Multiple items can be printed in a single print() statement by separating them with commas.
    • “result is is nothing but your double uh statements or string and is your number so we have to separate it with a comma when I run it asking the question enter the value of x 6 so um sorry not to display uh X we have to display y let me run it again enter the value of X4 and accordingly is getting the answer is 17”

    5. Operators:

    • Definition: Operators are symbols that perform operations on operands (values or variables).
    • “operator is nothing but the symbol which is responsible to do any operations for example I have a five + 7 so the plus is a responsible to do addition operation so this one we can say it’s operator this one is operator and five and seven is called as a operant operants”
    • Categories of Operators: Python supports various categories of operators:
    • Arithmetic Operators: + (addition), – (subtraction), * (multiplication), / (division), ** (exponentiation), % (modulo), // (floor division).
    • “the first one is arithmetic operator as I said that the symbol are available plus minus the star so I think you guys is already aware about that so the meaning of the plus is nothing but using for addition minus is for subtraction so we can directly use this kind of symbol so only this double star uh modulo that percentage symbol and double slash is something new for you”
    • Assignment Operators: Used to assign values to variables (e.g., =, +=, -=, *=, /=, etc.).
    • “assignment operator”
    • Relational (Comparison) Operators: Used to compare values and return a Boolean result (>, <, >=, <=, == (equal to), != (not equal to)). Also referred to as comparison or conditional operators.
    • “third operator is a relation relational operator somebody is also calling the comparison operator because you are comparing a isal to 5 is there b is equal to 7 is there if you’re writing a greater than b you can also write B is less than a that means you are comparing the answer is always The Logical format what is a logical logical format can be true can be false true or false answer right your answer will always be the true and false concept you comparing like a is less than b a is greater than or equal to B this is the symbol for not equal to this symbol for not equal to this is symbol for equal to equal to in the sense a is equal to 5 is already assigned”
    • Logical Operators: Used to combine or negate Boolean expressions (and, or, not). Always result in a Boolean value.
    • “the next operator is a uh logical operator this concept will be clear okay so a equal to equal to B A is less than greater than when you just run it you will get the either true or false answer why it’s a is H so A and B is not the same that’s why it’s giving the answer is false a is greater than b so the answer is giving the true a is not greater than b okay maybe I didn’t run the this command that’s why yeah it’s taking the previous A and B values you can see here a is not equal to equal to B I wrote it that getting the answer is false where a is greater than b so a is five and B is a 7 that giving the answer is false because five is not greater than 7 okay if is not equal to you can also write the concept like if uh uh a equal to equal to 5 actually the a is a five I assign the values and I’m also writing the five when you just run it you’ll getting the answer is true okay the next operator which I discuss in this uh uh operator as well let me just remove it the next operator is uh yeah okay so next operator is logical operator as I said that this is always giving TheLogical format we can say the value is a bullion true and false is a bullion okay logical operator is and or not”
    • Identity Operators: Used to check if two variables refer to the same object in memory (is, is not). Work similarly to == and != for values within the range of -5 to 255 due to Python’s object interning.
    • “identity operator is very similar to the equal to and uh uh is not is very similar to not equal to so it identify the values like a is equal to 5 and B is equal to 7 and C is equal to a five again so a is C so it very similar to the equal to equal to it also giving the bullan answer okay G given the bullan answer which is a true and false bullan is nothing but a true and false values that we are calling bullan values”
    • Membership Operators: Used to check if a value is present in a sequence (in, not in).
    • “membership operator membership operator is in not in okay the value is a particular member variable or not suppose I have one variable which have a list is a 5 7 8 9 so five in a or not like this practically you can easily understand”
    • Bitwise Operators: Mentioned as less frequently required for general programming but important in specific fields. Symbols like &, |, ^, ~, <<, >>.
    • “one operator is also there is a bewise opor which is not much required for um for pro as a programming perspective if you belong to the any edic field so that time is really important but still I’ll give you the resource where the bewise operator is also available so you just run it you will also understand the beat wise as well but this main operator list try to understand it in a theoretically and practically both the way”

    6. String Data Type:

    • Immutable and Ordered: Strings in Python are immutable (cannot be changed after creation) and ordered sequences of characters, allowing access to individual characters using indexing (positive and negative).
    • “so let’s discuss about the next topic which is a string so string is one of the very important data type in Python programming language so first thing is that it’s a immutable data type immutable in the sense once you define you cannot change it and second thing is that it’s a ordered data type ordered in the sense it have a index position starting with zero and goes till the length of the string minus one and we also have a negative indexing as well starting from minus one till the beginning of the string”
    • Slicing: Substrings can be extracted using slicing with the syntax [start:stop:step].
    • “we discuss about this slicing I disclose the concatenation and we have also one more thing is a repetition okay we can also repeat it with star symbol slicing we are using the square bracket”
    • Concatenation: Strings can be joined together using the + operator.
    • “concatenation we are using this uh Plus operation is used for concatenation plus operation is used for concatenation”
    • Repetition: Strings can be repeated multiple times using the * operator.
    • “reputation we can use the star so for example I have the value is uh variable is a I want to multiply with a five then you’ll getting India India India five times so that is a reputation”
    • Methods and Functions: Strings have numerous built-in methods (accessed using dot notation, e.g., string.upper(), string.lower(), string.capitalize(), string.count(), string.index(), string.title()) and functions (e.g., len(), type()) for various operations.
    • “let’s go to discuss about uh method and function in a string uh string data type so let me just conclude what I discussed okay so I discussed this slicing I disclose the concatenation and we have also one more thing is a repetition okay we can also repeat it with star symbol slicing we are using the square bracket concatenation we are using this uh Plus and reputation we can use the star so for example I have the value is uh variable is a I want to multiply with a five then you’ll getting India India India five times so that is a reputation okay a small small thing is there to go in a very details way again you can go to the data type if you already aware about these things so you can skip or you can just make this video is a 2X or 1.x faster and just complete it and go through this uh Jupiter notebook and run this file you’ll get a better idea but don’t forget to run this uh uh jupter notebook file okay so here I already explained okay so you can just run it just shift enter shift enter run it you’ll and you’ll understand each and everything but again in case if you make any doubts if you not understand anything you can just put your question on a comment definitely I’ll reply you okay so let’s discuss about uh okay I need a space all right I need a space keep because I don’t want to remove it okay so let’s discuss about the next thing is a function and method which is very very important every data type have a different different method and function okay let me first write a method and some function so actually the method and function both are a same there is no any difference this have a very small differences there if function is always defining like a def here I’m not explaining you the details of the function whatever in build function whatever in build method is available in a string that I will discuss it here okay so why the both are a difference see method is always calling with a DOT like I have suppose a is a variable a is a variable which is the India okay so method is always using like a Dot Upper a do lower a do capitalize okay a DOT count a do index small I there is no Capital index so other uh method is also available but when you’re talking about um function so function is like a len and we are passing the values a len means length we have um uh mean we have a mix okay so these are and we have a type so actually this uh uh function is a very common for for all the data type so when you just enter in the list tole dictionary so you’ll also find it out the same type of function that will not change but the method will always change the difference between the uh function and method is that if function is always defining like this and then function name f n if I’m writing defining like this if the function is separately available we are calling function simple but if the function is available inside a class we are calling the method class class name is a okay if it’s available inside a class we are calling method so when I start the objectoriented programming I’ll go in a very details way just now you can just understand like this the method is always using with a DOT and a function we directly call it and to know that which one is a function you can directly understand like if you find it out any statement after this bracket that is a function that can be either function or method if you’re using dot method not using function okay so few method I will discuss it here and uh after that I’ll stop okay because uh no need to explain each and every method when you just run this uter notebook you can easily understand okay so as I uh Define all two variables a which is India B which is a country and I want to make it a Dot Upper so that will make it is a upper case okay it will be make as a upper case a do lower that will make it a lower case small I and uh currently the a variable is already the capitalized capitalize in the sense first letter is a capital the second letter uh remaining letter is a small one let me make one variable is equal to I’m using Python programming language okay so let me just use small I when you just use a c do capitalize okay sometimes uh when you just forget your uh you know that method spell when you make it spellings little bit wrong so it will giving you the error so the best practice is that after this variable put a dot put a tab you’ll getting a different different suggestions I’ll make a capitalize I don’t need to write everything capitalize is already there okay when I just run it you’ll get the values I’m using Python programming language so the first L make it as a capital the rest of the later will make as a small one so C do title there is one more valuable uh”
    • String Formatting: Methods like f-strings (formatted string literals) can be used to embed expressions within string literals for formatted output.
    • “so string formatting is also very important which we already discuss in the input output so this F string is nothing but a string formatting”
    • Escape Sequences: Special character sequences (e.g., \n for newline, \t for tab, \\ for backslash) are used to represent characters that are difficult to type directly.
    • “skip sequence in the sense suppose if you are uh writing some statement and the value will not be print in the place of that uh uh that symbol it will be showing the meaningful result let’s say for example I’m writing here I um working in Python programming language okay which is uh dynamically type language okay I just want some statement after the uh High I want to print in next line I’ll use the slash n so sln is basically going to the next line right it’s not like that it will be print the slash okay and after the statement again I want to in this next line it will be in the next line I want to uh dynamically typed language have some uh space as well SLB B for backspace okay B for back I’ll make the t t for a tab okay so when you just run it you’ll get the values is because of the sln is giving the next line because of this sln is giving the again next line because of this SLT is giving the again some spaces here so that’s why it’s giving the uh uh that meaningful result not exactly is the printing the values right so this one we are calling the skep sequence so I just uh Define it here a different different type of skip sequence like slash n for next line/ t for tab SLB for back space SLR for reserved and uh Slash a for alert so different different skip sequences there sorry it’s a skip sequence okay skip sequence”
    • Raw Strings: Raw strings (prefixed with r) treat all characters literally, without interpreting escape sequences. Useful for file paths and regular expressions.
    • “what is a raw string so raw string in the sense sorry draw string in the sense if if uh you want all the statements as it is I don’t want to print the meaningful result I want as it is values so just before you have to write r that will consider everything as a raw string so the value is not displaying it here sometimes it’s very useful whenever you’re reading some particular files from local machine or in particular server okay suppose if you are reading and uh somewhere the double slash is available single slash is available so python is giving some meaningful result but you don’t want you want as it is uh path so that time we are using the r especially for uh reading the CSV file in the data science we mostly using the r to avoiding the errors”

    7. Tuple Data Type:

    • Immutable and Ordered: Tuples are immutable and ordered sequences, similar to strings but can contain items of different data types. Defined using round brackets ().
    • “so let’s discuss about the next topic which is a tle so tle is one of the smallest data type like uh uh like a number number is very small that was only uh you know uh the integer float and complex but the tle have some values but it’s not as much of uh big topic so let me just cover it in this video only okay so again the tle so first we have to clear that what is the definition the Supple is again it’s a immutable and ordered data type ordered data type okay when you’re talking about the how the tle is defining the tle is defining in a round bracket suppose I have a value is a five a 8 7.0 and high okay so that I store in the variable T so uh uh first of all this tle is a comes under the sequence sequence means is a particularly order and that collecting the data right so the five is a integer 7.0 is a float high is a string so that is a that’s why the tole is become a uh sequence which store the different different data type even inside a tle you can also store the list as well dictionary as well anything so the tle list dictionary in the set that working as a container so the container is nothing but a sequence okay so here as I said it’s the ordered data type so again have a positive and negative indexing is there okay sorry uh oh why it’s showing like this okay have a positive and negative indexing so 0 1 2 3 have a positive indexing and as well as we have a negative indexing as well -1 – 2 – 3 – 4 so like the tle we can also apply the uh slicing order like uh the T slice with uh uh two you’ll get some values obviously you’ll get the value is 7.0 right you’ll get the value is 7.0 in in case if you pass the T of three you’ll get the high but in case if you pass the minus 3 then the value will change that will be 8 right so we can also apply the concatenation here we can also apply the multiplication here so this symbol is used for concatenation and this symbol is used for repetion okay so and uh the likewise the uh string we have also the method and function are available so the best part of this uh tle is that not a best part but uh yeah uh the tle have a very less method when you’re talking about a method have very less and a function okay so method have very less just we have a count count method like T do count and T do index that’s it we don’t have other method is here so again is a tle uh again is a immutable so that means we can’t change the values in between and uh the function when you’re talking about so it will be the same as the which we discussed in the string that like U uh what is the max values what is the mean values what is the um a type okay there is also one more function is available which is a tle which will be responsible to convert any data type into tle okay okay so mean Max is there yeah and we have a len which is very very important Len which will show you how much uh data is available inside a tuple so let me just clear it in the practical way it’s very uh small data type we can see so again you have to refer the data type Jupiter notebook you’ll find it out the practice uh um notes there okay so tole I can Define it here in the round bracket I’ll make this the T1 be Define in the round bracket 47 7.9 and um uh high is here we can Define it and he we can Define it okay so let me also Define one more variable is a T2 where I can Define it 4 5 6 2 there is”
    • Indexing and Slicing: Supports positive and negative indexing to access elements and slicing to extract subsequences.
    • Operations: Supports concatenation (+) and repetition (*) similar to strings.
    • Methods and Functions: Has limited built-in methods (count(), index()) but works with common functions like len(), min(), max(), type(), and the tuple() constructor for type conversion.
    • Immutability: Key characteristic, meaning elements within a tuple cannot be changed after the tuple is created. This provides data integrity.

    8. List Data Type:

    • Mutable and Ordered: Lists are mutable (elements can be changed) and ordered sequences of items, defined using square brackets []. Can contain elements of different data types.
    • “now let’s discuss about the next topic is a list that is the fourth data type list so list basically a a mutable data type so whatever we discussed the previous one is a tle string and numeric that was a immutable data type but this one is a mutable data type mutable and ordered data type again so mutable means changeable ordered means have a positive and negative indexing so list is always defining with this is LS is a variable defining with a square bracket that can be the any data type you can just write it like integer 5.8 is a float High okay and uh 7 five so you can Define like this so it’s a square bracket all right so have a positive and negative indexing as well because of it order so we have a 0 1 2 sorry three and four I have a NE negative indexing as well minus1 -2 – 3 -4 – 5”
    • Indexing and Slicing: Supports indexing and slicing like strings and tuples.
    • Operations: Supports concatenation (+) and repetition (*).
    • Methods and Functions: Has a rich set of built-in methods due to its mutability, including append(), insert(), remove(), pop(), clear(), count(), index(), sort(), reverse(), and functions like len(), min(), max(), type(), list() (for type conversion).
    • “so same as the tle we have also this method and function are available but because of this mutability because of the changeability we can change the values so we have so many method and functions are available so here I will discuss a few method and function and later you can just run the Jupiter notebook file you’ll understand easily so method and function our function is the same that will be Len that will be mean that will be Max that will be uh list list also the function okay and uh that will be the type okay this function very common we we we are using in uh every data type so only this one like for a list we have a list function for tle we have a tle function string we have a s Str function okay so method when you’re talking about so method is um uh we have uh like U we have a DOT count okay we have a um we have a index that that is a common it’s there for a tle and uh in string as well and we have also the other method is like append which is very famous append insert okay so many method are available so again when you just run the jupyter notebook you’ll find it out all the methods there so few method I will discuss here and I’ll discuss in some practical implementation here okay you’ll find it out in the Jupiter notebook each and everything okay as I implemented almost every method here but still if you’ll get any confusion I’m there so let me create one uh heading is a list okay so LS is a variable I’m making some values here 6 comma 3 comma 8 comma 4.7 comma uh H comma K okay so if I want to change the value is H to hello so what is the position of H which is the 0 uh 0 1 2 3 4 that is the fourth position so fourth I want to which is nothing but a h i want to make as a hello hello and later when I just check the values is LS so yes I can see the value is hello so it’s very simple we can just do it right we can just up change the values because of it’s a mutable data type we can easily change the values which is defined inside that right so here H is changed so whatever index is defining for H so in the place of H is showing the hello but you can’t do it in a tle okay”
    • Mutability: Allows modification of elements after the list is created. Elements can be added, removed, or their values changed.
    • Nested Lists: Lists can contain other lists, creating multi-dimensional data structures.
    • “like I want to create one list inside a list that is also possible list inside a list so that is called as a nested list”
    • List Comprehension: A concise way to create lists based on existing iterables.
    • “one more important topic is a list comprehension list comprehension is nothing but a very short way to define a list based on some conditions”

    9. Dictionary Data Type:

    • Mutable and Unordered: Dictionaries are mutable collections of key-value pairs, enclosed in curly braces {}. They are unordered, meaning the order of items is not guaranteed.
    • “now we reach till fifth data type which is a dictionary so dictionary is a little bit different with tle and list what is the different different is that there is no any positive and negative indexing is there in the dictionary that means dictionary is unordered data type but it’s a mutable data type if it’s a mutable let me just write it first it’s a mutable and ordered data type sorry unordered data type unordered data type okay so there is no any positive indexing for a dictionary no any positive and negative indexing so dictionary is a defining like key value pair with curly brasses key colon values okay Curly brasses close”
    • Key-Value Pairs: Each item in a dictionary consists of a unique key and a corresponding value. Keys must be immutable data types (e.g., strings, numbers, tuples).
    • Accessing Values: Values are accessed using their keys within square brackets (e.g., dictionary[‘key’]).
    • “if I want to access the value is a 15 so I will write D of a okay we’ll write a d of a then you’ll get the value is a 15 if I accessing the value is a d of Z so you can get the value is 19 this way we are accessing the values”
    • Operations: Does not support direct concatenation or multiplication like sequences.
    • Methods and Functions: Offers various methods like keys(), values(), items(), get(), pop(), popitem(), update(), clear(), and functions like len(), type(), dict() (constructor).
    • “when I just perform some operation methods here like a D1 do Keys you’ll get the all the keys here okay so r k T5 these are keys and what is the values E1 dot uh values you will get the values here again if you uh if you already aware about that you just the run this file which is I shared with you the dictionary run this file and run this video in a 2X or 1.5x you don’t require to listen each and everything right these are very basic basic things is there and just when you run you’ll understand so I’ll just give you the complete overview here okay so suppose if I want to add some values so there is no any upend option is there right there is no any upend I want to add some values how can I add it so the D1 pass any keys I’ll passing the keys here like uh U I’m assigning the values here 1,000 so when I just run it and get the values here D1 you’ll get the U in the last values okay so like way we can also assign the values so dictionary have some um you know applications so I’ll discuss about that again I will not each I will not write each and every line code so I’m taking the help from here suppose if I’m defining the values here okay here this is the list okay so question is why I’m creating a list here why not sorry this is the tle uh why I’m creating the tle here why not a list the reason behind that tle is very much”
    • Applications: Useful for representing structured data with labels (keys). Can be used to simulate tables when combined with lists.
    • “when you just enter in the data science so everything you are dealing with a table and the table we are reading in the data frame format then you’ll start the operations so if you start the operation if you already aware about that yes this table is nothing but a collection of the keys and values right”
    • Dictionary Comprehension: A concise way to create dictionaries.
    • “likewise the list comprehension we have a dictionary comprehension as well so dictionary comprehension is also the same syntax for variable in sequence you can also put the condition as well but the only changes is that you have to define the key and values that’s it”

    10. Set Data Type:

    • Mutable and Unordered: Sets are mutable, unordered collections of unique elements, enclosed in curly braces {} or created using the set() constructor. Duplicate elements are automatically removed.
    • “now the last data type is a set so set is again a mutable data type but it’s a unordered data type and it always store the unique elements unique in the sense duplicate values is not acceptable and it’s defining with a curly bras but the only difference with dictionary is that dictionary is a key value pair but here directly you are passing the values”
    • Uniqueness: Sets only store unique elements. If duplicates are added, they are ignored.
    • Unordered: Elements in a set have no specific order, and indexing is not supported.
    • Operations: Supports various set operations like union (|), intersection (&), difference (-), symmetric difference (^), and methods like add(), remove(), discard(), pop(), clear().
    • “so sets support so many operation like Union intersection difference symmetric difference and all the Venn diagram concept you can apply it here so Union means combining the two set but again it will store only the unique element intersection means common values difference means the values is there in a but not in b or values is there in b but not in a symmetric difference means except the common values rest of the values will be displayed”
    • Methods and Functions: Includes methods for adding (add()), removing (remove(), discard(), pop()), and updating sets, as well as set operations. Functions like len(), min(), max(), type(), set() are also applicable.
    • Set Comprehension: A concise way to create sets.
    • “there is one more application one application is there in a set which is the uh set comprehension likewise the dictionary comprehension we had we had a uh list comprehension so set comprehension is also there comprehension okay set comprehension so set comprehension the syntax will be the same like the and list so whatever for we have to use for that particular variable in sequence sequence can be any range any uh other sequence like a list or tle or or list or tle mainly so here just you have to write variable and curly bres is mandatory for a set”

    11. Data Type Summary (Mutable/Immutable, Ordered/Unordered, Container/Sequence):

    • Mutable: List, Dictionary, Set
    • Immutable: Numeric (int, float, complex), String, Tuple
    • Ordered: String, Tuple, List
    • Unordered: Dictionary, Set
    • Container (holds multiple items): String, Tuple, List, Dictionary, Set
    • Sequence (ordered container with indexing): String, Tuple, List

    12. Conditional Statements (if, elif, else):

    • Flow Control: Used to execute different blocks of code based on whether certain conditions are true or false.
    • if Statement: The starting point of a conditional block. The code within the if block is executed only if the condition is true.
    • “the condition is always start with if we are writing if take a space and write it down a is greater than b okay that uh your condition after the colon it automatically taking the four space that we are calling indentation and we consider as a block if I write the statement as a print hello and when you just run it and it will be check the condition is satisfied or not”
    • elif Statement: Used for additional conditions to check if the preceding if or elif conditions were false. Multiple elif blocks can be used.
    • “but I want to write one more condition so obviously I have to go back and write one more condition which is the L if L if C is greater than b colon let’s write some statement statement is hey and if I want to one more uh uh if I want one more condition I’ll write it the L if is a is greater than C there is a three condition I mentioned there and simultaneously you can just write a many condition is here okay hello hey hi”
    • else Statement: An optional block that is executed if none of the preceding if or elif conditions were true.
    • “and after that we always using the ls this is the good practice in case if you’re not using it will not show you any error so likewise here it’s not showing any error so same wise here it will not show any error if you’re not using else just write down here else colon and your statement print hello uh just buy else is always execute if any block is not executed previously if any block is not executed that time is working”
    • Top-to-Bottom Evaluation: Python evaluates the conditions in order from the if statement downwards. Once a condition is true, the corresponding block is executed, and the rest of the conditional block is skipped.
    • “python is always working the top to bottom okay it always check that line number one is um um satisfied or not satisfied a is greater than b the meaning is that a is greater than b B that is false Okay C is greater than b which is true a is greater than C which is also false and else is always is a default one in case previous one is not executed that time it will run okay so here the block number two here the third one line number three is execute it when you run it it giving the answer is he but what happened is all the condition is satisfied all the condition is true like a is greater than b okay A a is less than b uh and C is greater than b and uh a is also less than C that all the condition is satisfied so what will be the answer the answer will be the first one because according to the rules here which we discussed if the first condition is satisfied is giving the statement and terminate the Block it’s not entering the next block because l l if will check if the first condition is dissatisfied right if the first condition is satisfied is giving the statement and terminate the block but here the L if is after the false of the first condition so here it giving the answer is hello one day again the last one if no any condition is satisfied that time the buy will execute which is the default one if nothing is there nothing no any condition is satisfied like uh I’ll just change it here uh greater than less than greater than so all the condition is false there is no any true condition and uh so when you just run it it giving the answer is by because nothing is satisfied here this is the way of conditional statement we are using”

    13. Loop Statements (for, while):

    • Iteration: Loops are used to repeatedly execute a block of code.
    • for Loop: Iterates over a sequence (e.g., string, list, tuple, range).
    • “basically the for Loop is used for the iterating of the process if anything is available in a uh you know in any container if you want to iterate it if you want to display the one by one we are using the for”
    • range() Function: Generates a sequence of numbers, often used with for loops. range(start, stop, step). The stop value is exclusive.
    • “sequence is uh scq limes right scq is equal to range so range is one of the function which provided the sequence from one number to another number with interval so if I’m passing the range is like 1 till 15 yeah 11 so python have a rules that it never end with last values it end always before one so if I’m I’m writing the 1 to 11 that means it’s a 1 2 3 4 5 6 7 8 9 it never goes till 11”
    • Iterating Through Sequences: for loops can directly iterate over elements of lists, tuples, strings, etc.
    • “the same thing is applicable for any list as well so in the list I have a multiple values for V in list print V so it will print the one by one”
    • Nested Loops: Loops can be nested within other loops to iterate over multi-dimensional data structures.
    • “if you have a list inside a list inside a list that is a 3D so you have to apply the three for Loop to read each and every element”
    • while Loop: Executes a block of code as long as a specified condition is true. Requires careful management of the loop condition to avoid infinite loops.
    • “while loop is also for the iterative purpose but it will be continue the process until the condition become false so we have to initialize one variable and provide the condition here and inside a block we have to either increment or decrement that variable so that particular condition will be false otherwise it will be run infinite time so we have to take care about the while loop”
    • break Statement: Used to immediately exit a loop.
    • “if any condition is satisfied in between I want to just stop the loop I can use the break statement”
    • continue Statement: Skips the rest of the current iteration of a loop and proceeds to the next iteration.
    • “if any condition is satisfied I just want to skip it I don’t want to stop the loop I just want to skip and continue the next iteration I can use the continue statement”
    • else Clause in Loops: Can be used with for and while loops. The else block is executed if the loop completes normally (without being terminated by a break statement).
    • “there is one more interesting thing is that the else block is also available with the for Loop and while loop but it will be execute only if the loop is completely executed if the loop is break in between the else block will not be executed”
    • Real-life Example (Login Attempt): The source provides an example of using a while loop for login attempts with a counter and the possibility of pausing the process using the time.sleep() function after a certain number of failed attempts.
    • “I’ll just use the while count is greater than zero I’ll ask the username input enter your username and password input enter your password okay and uh if my condition is username is equal to equal to hurry and password is equal to equal to hurry 1 2 3 my login will be successful print login successful and I’ll break it I don’t want to continue the process anymore but in case if my username and password is wrong is more than three times then the process should stop for particular duration I’ll just make it minus okay then it will showing that uh it only three times is remaining only two times is remaining this kind of you’ll get only three attempts okay the same statement I’ll apply for if the username is wrong my username is wrong and I’ll make it comma uh this particular attempt is remaining all right so okay this okay so if my usern is wrong uh all right I forgot to write F this is string formatting if my usern is wrong two attempt is remaining one attempt is remaining zero attempt is remaining in the sense is the last one it should be stop it here so I have to put one more condition if the count become a zero I have to stop it what I can do it here uh so anywhere like uh the breaking the loop if my loging successful I can also write L if Al if we can write Al if the count is equal to equal to zero the time please wait for 10 second currently I’ll just write in the 10 second okay 10 second and after that process should start again all right so I’ll do the same process for uh this is for password I’ll do the same thing for username as well I do it username as well but the your process should stop for 10 second so how can we do that so there is a like library is called as a Time import time okay so you can directly write the time do slip your process will stop for particular duration so I’ll do it here time do slip for 10 second so automatically it’s a second how can we know that you can just write time. slip and bracket you can just check it what exactly is there it’s a second I click on here inside and click shift tab this option is available in the Jupiter notebook only and if you have any other ideally you can find it out other options okay let me just remove so the process will stop for 10 second and after that it will again start so it’s on you if you want to put the timing you can okay so currently it will be stopped for 10 second process will yeah only two attempts is remaining one attempt is remaining I’ll write hurry password I’ll make it wrong this is the last attempt zero attempt is remaining that means there is no any attempt this is the last one I’ll write it here uh hurry password is hurry wrong let me just write it just wait for 10 second the process is still running it’s not like that your process is totally end up it’s running for 10 second it hold for 10 second you can increase it now it’s ask asking the password again hurry I will write it and again it’s waiting for 10 second because it’s already been uh uh already been you know um is a kind of uh uh already your count was Zero that that’s why it’s waiting for 10 second but it should not be it should not be after 10 second it give the three times option right but here I’m passing the wrong password still is waiting for 10 second so that one glitch is there there I have to just change it uh it should be make it zero again uh it should make it uh three again all right so I use it hurry 1 2 3 and let’s make it yes”

    14. Functions:

    • Code Reusability: Functions are blocks of organized, reusable code that perform a specific task. They help in modularizing code and making it more readable and maintainable.
    • “function is nothing but a block of code which perform a specific task and it is reusable”
    • Types of Functions:Predefined (Built-in) Functions: Functions that are already available in Python (e.g., print(), len(), type(), int(), range()).
    • “predefined which is system is already given the function we are just using it right mean Max int print whatever you you already use it that is a predefined function you just just call and pass the values you’ll get the answer”
    • User-Defined Functions: Functions created by the programmer using the def keyword.
    • “we can also make the function which is we are calling user defined function that means we are defining the function the third one is anonymous function we’ll discuss in um practical manner a function without name so here you can see the uh you can see the def and function name”
    • Anonymous (Lambda) Functions: Small, unnamed functions defined using the lambda keyword. Often used for simple operations where a full function definition is unnecessary.
    • “the third one is anonymous function which is also called as a Lambda function we can define the function without name with Lambda keyword with Lambda keyword”
    • Recursion Functions: Functions that call themselves during their execution. Requires a base case to prevent infinite recursion.
    • “the last one is a recursion function that things we’ll also discuss”
    • Defining Functions: Use the def keyword followed by the function name, parentheses for parameters (optional), and a colon. The function body is indented.
    • “let’s make a block is a depth EF and function name is a equation equation okay as I told you bracket is very very important parameter it’s on you if you want to Define you can otherwise you can leave it and uh like equation is the equation is y equal x squ plus 2X you required X as a input so I’ll just pass it here parameters now your block is ready I’ll just print it y okay I’ll just print it Y and I just run it you’ll not get any answer function have a rules without calling the function function will never execute okay so here I can see this is defining the function defining the function”
    • Calling Functions: Functions are called by using their name followed by parentheses, passing arguments if required.
    • “I want to call it so what I’ll do I have to define the values X is equal to some some something or else you can directly uh pass the values equation I’ll pass the value is five so you’ll get the answer accordingly so this one we are calling the function this section calling the function okay this SE is calling the function”
    • Parameters and Arguments: Functions can accept input values through parameters defined in the function signature. When calling a function, the actual values passed are called arguments.
    • Return Statement: The return statement is used to send a value back from the function to the caller. After a return statement is executed, the function terminates.
    • “the dev hello there is a function and if my number number is a = to 4 and B is = 8 and return is equal to a + and before return let me just use the print print A+ B and when you just call it hello you’ll get the answer is 12 and after the print statement uh my statement is there um um hello world okay this normal statement hello world after 12 is showing that hello world but in case if you use the written statement for a plus b return statement for a plus b hello world where hello world will never display the reason behind that because after the print after the written statement whatever things is there will never be execute”
    • Lambda Function Syntax: lambda arguments: expression.
    • “we are writing the Lambda Lambda and we’re passing the arguments like uh a sorry a comma B and then you uh doing some operations let’s understand it here so syntax is not much important but you should know that how we are writing so just we have to write Lambda what operation you want to perform I want to perform the operation of um a cube of any numbers yeah we can say U addition of XY Z so I have to Define it X comma y comma Z colon colon what operation you want to perform I want to perform is the 2 into x + y + z that equation I want to perform when you just run it we’ll find it out the function is created”
    • Use Cases for Lambda Functions: Often used with functions like filter() and map() for concise, inline operations.
    • “Lambda keyboard is using if your function is very small and you are it iteratively calling it the time we are using you don’t need to Define it separately you in that function itself in that predefined function itself you can use the Lambda”
    • Recursion Function Example (Factorial): The source demonstrates a recursion function to calculate the factorial of a number, where the function calls itself with a decremented value until a base case is reached.
    • “suppose I have a number is n and uh here I pass the value is a five and every time when you call this process when you call this function I’ll make it n minus one at the same time I can also print that n as well so that I can track it how much value is there so when I just run it you can see here the 5 4 3 2 1 and and it’s going till down there is there is a limit is a 3,000 so it will be going down and run it again and again right so what I can do hit here so when you scroll down down down down it’s a 7,000 it’s going to more than 7,000 the reason behind that I increase the limit which is the 8,000 right I increase the limit that’s why it’s going till here after uh 7,000 7,000 something and is going this statement is maximum recursion depth exced while calling the python object my target is that I want to stop in after five steps so I can use it here the condition if my n become a zero that time I will return return just um uh recursion done okay the statement is is a recursion done that’s it so because you know that after the return statement any statement is written there it will never accept okay so when I just run it it will going till uh 1 only what exact what is the meaning is that it’s not acceptable after the return statement like I think I already discussed but again I’ll tell you in the short way the dev hello there is a function and if my number number is a = to 4 and B is = 8 and return is equal to a + and before return let me just use the print print A+ B and when you just call it hello you’ll get the answer is 12 and after the print statement uh my statement is there um um hello world okay this normal statement hello world after 12 is showing that hello world but in case if you use the written statement for a plus b return statement for a plus b hello world where hello world will never display the reason behind that because after the print after the written statement whatever things is there will never be execute”

    15. Modules and Packages:

    • Modules: A Python file with a .py extension. Contains Python code (functions, classes, variables). Used for organizing code and reusability.
    • “module is nothing but uh any python file which have a py extension is called as a module so you have so many python file you create but sometime we creating the Jupiter notebook file that Jupiter notebook extension is i p y and B so that is not a module so python file should be in the py extension that will consider as a module and the module can consist of function classes variable anything because that is the file inside a file so many things we can write it”
    • Packages: A directory containing multiple modules and a special file named __init__.py. Used for structuring larger Python projects and preventing naming conflicts between modules. Sub-packages are packages nested within other packages.
    • “package is nothing but one uh directory we can say where consist of the multiple modules but there is one special file is called as init so that name is init it should be available there that we are calling packages so for example I have a file which is the uh a which is the file name is a. py B do py c. py so these are all a modules but make sure that there is one more file should be underscore uncore init uncore uncore py if this one is also present and it’s available in the particular folder uh suppose the folder name is okay yeah suppose the folder name is main so main is nothing but a package which consist of the multiple modules”
    • Libraries: Often used as a general term for a collection of related modules and packages that provide a set of functionalities.
    • “someone is also calling this one is a library someone is also calling this one is a library so library is nothing but is a collection of the packages”
    • Importing Modules: Use the import keyword to bring modules into your current script. Specific names can be imported using from module import name.
    • “so I want to call it this function with different file so calling is a file both load and calling is available in the same location I want to call the load module so how to call any module we are using the import keyword import load”
    • Finding Module Location: The sys module and sys.path list can be used to determine the directories where Python looks for modules.
    • “there is one um library is in build Library which is a CIS CIS is a system so let me just open the python here directly sorry here anywhere you can open it I can write it here okay so import CIS system sis. paath so it will showing you the path where exactly all the libraries and all the python packages are available”
    • Installing Packages with Pip: pip is the package installer for Python. Used to install and manage third-party libraries from the Python Package Index (PyPI). Command: pip install package_name.
    • “p is working as a package manager which is responsible to install any kinds of packages or libraries so if you’re using the python more than uh 3.4 version so that means PP is already there you have to just use it so if you want to install a package like pandas so you can write it you can write it like pip install pandas that’s it”
    • __name__ Attribute: A built-in variable that holds the name of the current module. When a script is run directly, __name__ is set to “__main__”. If a module is imported into another script, its __name__ is set to the module’s name. This is often used to include test code or main execution logic within a module that should only run when the module is executed directly.
    • “there is one more topic is a name attribute I think you saw this name attribute in a Python programming language is many places so let’s understand it how we can use this name attribute so suppose if you have vs code okay if you have a vs code and uh yeah and I have one file which is a um load file load. py so the variable is 55 and DEP is equal to info and uh pass the statement is this is this is uh load module okay and one more function is available def add and pass to parameter a comma B simple and just um you know perform some operation result is equal to a + b and later return the values return the result okay now it’s done but I want if if I if my target is to create the complete a module which will be the load module but I want to test it that whatever function I wrote it that is perfectly fine or not okay so here uh I just want to um you know test it this add function is properly running or not so what I’ll do I’ll just call the function 5 comma 8 and when I run it so hopefully we will not get any answer because I return the values but when I just print the statement like uh print the statement directly so we will get the answer is 1 so which um I was expecting 13 now the answer is also 13 everything is perfect but here I just want to call the load module let me just call it import load okay and when you just import load let’s see what will happen so import load I just load the I just uh you know load the load module that name is the same I just imported the load module but here I was not expecting that answer should be the 13 because I didn’t call the add function here I didn’t call the add function let me just call the add function uh print load do add I’ll pass the values is 5 comma 12 so I’m expecting the answer is 17 so here I got the 17 but the same time I also got the answer is 13 as well which is wrong not wrong but uh I have to see that where exactly this 13 so 13 is basically I just printed here in the load module just”

    16. Exception Handling (try, except):

    • Handling Errors: Exception handling is a mechanism to gracefully manage runtime errors (exceptions) that can occur during program execution, preventing the program from crashing.
    • “exception handling is basically used for to handle the unexpected event during the program execution”
    • try Block: The code that might raise an exception is placed within the try block.
    • “I’ll just use a try try means I’ll just normally try the normal flow of the program so normal flow of the program I’ll right okay so in case any exception occurred except”
    • except Block: If an exception occurs within the try block, the code within the corresponding except block is executed. You can specify the type of exception to catch.
    • “except exception accept exception uh the time I can I can write it here print Infinity okay I can I can write it anything so if I pass 6 IDE 0 you’ll get the answer is infinity whatever you write it here you’ll get the answer accordingly”
    • Real-life Analogy: Likens exception handling to a car’s fuel indicator: it doesn’t solve the problem of an empty fuel tank but alerts the driver to take appropriate action.
    • “if the petrol is finished the fuel indicator is giving the answer you’re giving the uh instruction that yes your petrol is finished find it out some nearby petrol pump so the fuel indicator is not solving the problem but is giving the instruction that this is the exact instruction you can handle via petrol so this is one of the example”

    17. File Handling:

    • Opening Files: The open() function is used to open files for reading or writing. It takes the file path and the mode (‘r’ for read, ‘w’ for write, ‘a’ for append, ‘r+’ for read and write, etc.) as arguments.
    • “file handling is basically we have to perform the operation with the file like a reading the file writing the file updating the file and deleting the file so these are the four basic fundamental operation we can perform it and to perform this operation we have a inbuild function is called as open function”
    • File Modes: Different modes determine the operations that can be performed on the file. ‘w’ overwrites existing files or creates a new one, ‘a’ adds to the end of an existing file.
    • “your mode should be w w means writing the files and then line by line I’m just writing the file this is my first program this is my second program I’m writing there so make sure that you have to close it otherwise it will be impact to the another files if you opening in a new section so yes I close it that means file is properly written sometimes we are not closing the files so what happened ke if you write the program uh if you apply the uh writing some uh you know lines in a text but you forget to close it so that time the file exactly not writing in the text file okay so now file is a properly written”
    • Closing Files: It’s important to close files using the close() method to release system resources and ensure data is written to the file.
    • “make sure that you have to close it otherwise it will be impact to the another files”
    • with open() Statement: Provides a convenient way to work with files. It automatically closes the file even if errors occur. Recommended practice.
    • “one more option is also available which is a with open we have to use a with open it and if you using the withth open you don’t need to close it the reason because you’re writing entire section inside the block if you’re coming out the block that means file is already closed so this is also one of the good practice”
    • Reading Files: Methods like read() (reads the entire file), readline() (reads one line at a time), and iterating over the file object (reads line by line) are used to read file content.
    • “so if I want to use the reading okay so make sure that I should have some files okay uh so python. txt I create I have one files is a big file so you can also find it out files on um GitHub okay before going to the python. txt let’s also read something okay you can also read the my my text.txt as well let me show you how we can read it so with Way open you can also use it with open I’m just opening the file which name is my txt do my text.txt my my text.txt and mode is equal to it’s r r for reading okay and then uh uh we have to define the Define the you know as a variable as F3 I’m just defining it okay F uh F50 I’m just defining maybe it will be impact the next one because 3 four I just use it there so I’m just use the F50 F50 dot read line and then read line that’s it so it will be uh store somewhere let me store in the where one okay so we can also check that is the first line which will be printed where one so this is my first program so whatever I write it I can also read it as well”
    • Writing to Files: The write() method is used to write strings to a file.
    • Appending to Files: The ‘a’ mode allows adding content to the end of a file without overwriting existing data.
    • seek() Method: Allows changing the file pointer position to a specific offset, enabling reading or writing from different locations within the file.
    • “here we have a a method is a seek method seek method is exactly we can just Define the positions from where you want to start it like I I I decided that I want to start some particular position which is the python is interpreted objectoriented high level programming language suppose I want to start from there so how we can start it like we our cursor is always starting from here but I want to start here so seek will Define the position in a python yeah we can say here in this location and then it will”

    18. OS Module:

    • Operating System Interaction: The os module provides functions for interacting with the operating system, such as working with file paths, directories, environment variables, and running system commands.
    • “OS module is also one of the very important module which will be interact with your operating system like creating the folder deleting the folder and uh checking your current directory changing the directory so so many operation we can perform it”
    • Common Functions: Includes functions like os.getcwd() (get current working directory), os.chdir() (change directory), os.mkdir() (make directory), os.makedirs() (make multiple nested directories), os.rmdir() (remove directory), os.rename() (rename file or directory), os.path.join() (join path components), os.path.exists() (check if a path exists), os.cpu_count() (get the number of CPUs).
    • “OS module is also one of the very important module which will be interact with your operating system like creating the folder deleting the folder and uh checking your current directory changing the directory so so many operation we can perform it let me just show you a few practical examples so currently my current directory is this so if I want to check it OS module first you have to import OS and OS do get CWD so you’ll get your current working directory if you want to change your directory OS do chdir and pass the path wherever you want to go your path will change and next time when you just check it os. getet CWD so you will get it the current location which is the recurrent learning video on python tutorials and some operation we can also perform is like um I want to make some directory I want to make some uh you know some folders directory is a folder so how we can create it uh current location is this this in the sense here nothing in no any folder is available I want to create any folder how we can create it mkd sorry OS do mkd and pass the values you can you can write anything uh I’m just writing temp directory temp di so when you just run it so temp directory will generate yeah temp directory is generated so with the help of os module you can perform some operation with your computer so that python is providing that kind of facilities other programming language is also there just I’m telling you the python have the OS moduel and uh in case if you have the multiple directory for example my current location my current directory is this and I want to create the multiple directory inside a directory like path is equal to I want to create um you know new dir inside the new di I want to create one more directory is a okay analysis okay inside analysis and then um uh we can say Titanic okay this folder I want to create it but if you’re using the. M KD let’s see it will work or not so os. mkdir path let’s see it will showing throw the error the system cannot specify the path because your current directory is this and inside that you want to create the multiple directories so which is not possible with a mkdir so different method is available which is a Mech DS Mech directories so when you just using the me directories so you can create a folder inside a folder inside a folder like that so let me show you yeah new di inside that analysis inside that Titanic so this way we can create it okay so in case I don’t want any directory suppose I don’t want a temp directory still there so OS Dot .rm and provide the path which is the temp Dr so that directory will delete RM di sorry rmd so that directory will delete as well so this kind of option is also available to you can also delete it okay there is one only one directory which is the new d new dir there is no any temp di because I deleted I want to change this name I don’t want you know this new IR I want to rename it so OS dot rename it okay so rename it so what is the file name new di I want to change it as a new only okay rename n a m e rename sorry so new di I just change it is a new so change it here so that kind of option is available so let me just do the last two method and after that I’ll I’ll show you the list you can easily explore it suppose my current directory uh my current directory is this current di let me just do it o.get CWD okay this is my current Di okay I want to perform some operation uh so like uh I want to add it like new Di and with all the locations right so how we can do it like inside a DI there is analysis folder is available so we can also join the path as well so OS do os. paath do join so my current directory current Di with whatever path you want to join it suppose I want to join with a new so it will be joined like this okay so you can perform the analysis and based on that so. path. jooin you can joining with the two One Directory with any other directory in case I want to create One Directory which name is a new which is already exist let me R OS do mkd which is name is new let’s see what will happen so it will throw the error and saying that cannot create a file when that file is already exist so there is a condition we can also apply it okay if os. paath do exist and you can write it that new in case if new is exist the time in case if new a exist if os. path. exist the time will not create okay okay okay let me just try uh okay so it’s kind of if path the the different path is exist then you can create it like I want to uh this one is just tell you like the path is exist or not like I want to check that yes or no so it will show yes that path is exist so in case the new is exist inside that I want to create one uh location like here like here inside a new I want to create it inside new folder I want to create one more folder which name is uh data science okay inside a new I want to create it in a data science likewise okay let me tell you new okay temp Dr and then data science okay so like this live it let me make it the very simple I want to create new directory os. mkd which is name is new I want to create it but it’s already exist so it throw the error so what I can do if not os. path. exist new if it’s not there then it’s create otherwise don’t create so it will not throw the error in case new is not available so currently new is available it’s not enter inside the location but in case if it’s not available I delete it when you run it here so it will be create but inside nothing is there because just now it created okay so when you just apply this kind of condition if not os. path that exist then it’s created otherwise don’t create so in OS module lots of method is available so I just provided the link in a Jupiter notebook W3 schools you can also explore it like anything like I want to know that CPU count okay with a CPU count you can also use it how many CPU is there and you can also perform the operation there is a four CPU kind so likewise I want to also check that in my PC how many CPU count is there so OS do CPU count okay it’s not counts think yeah there is a 12 CPU count is there in their system is a four so likewise you can also explore it other method which is available here”

    19. File Handling (Pickle, JSON, CSV):

    • Pickle: A module for serializing and de-serializing Python object structures (pickling and unpickling). Allows saving complex data structures to a file and loading them back. Pickle files are binary format.
    • “pickle is used for preserving the data so like if you save this kind of data into any format like a Json format or normally the text format you can see easily right if you double click with a notepad you can uh see that kind of files but pickle is always store”
    • JSON: JavaScript Object Notation, a lightweight data interchange format. Python’s json module allows encoding and decoding JSON data. Commonly used for web applications and data transfer. JSON files are human-readable text format.
    • “Json is also for the same purpose if you want to transfer the data from one application to another application so Json is mostly used in web application”
    • CSV: Comma Separated Values, a simple text format for storing tabular data. Python’s csv module provides functionality to read and write CSV files.
    • “CSV is also one of the very famous file format if you want to store the tabular data in a very simple way like in Excel format so CSV is very useful”

    This briefing document provides a comprehensive overview of the fundamental Python concepts covered in the source. Further exploration and practical exercises, as suggested by the source’s reference to a Jupyter Notebook, are crucial for solidifying understanding.

    Python Fundamentals: Variables, Data Types, and Operators

    1. What is a variable in Python, and how does it relate to data types and memory allocation?

    In Python, a variable is essentially a name that refers to a location in the computer’s memory where data is stored. When you assign a value to a variable (e.g., a = “hello” or b = 15), Python allocates a portion of memory to hold that data. The data type of the value (like string, integer, float) determines how this memory is allocated and interpreted. Each variable, being a memory location, also has a unique identification number, which can be retrieved using the id() function. The amount of memory allocated and the identification number can differ based on the data type of the value stored in the variable. For instance, a string might require more memory than an integer, leading to a larger gap between their identification numbers when created.

    2. What are the main built-in data types in Python, and can you provide examples of each?

    Python offers several built-in data types, categorized mainly as:

    • Numeric:Integer (int): Whole numbers without decimal points (e.g., 15, -3).
    • Float (float): Numbers with decimal points (e.g., 19.8, -2.5).
    • Complex (complex): Numbers with a real and an imaginary part (e.g., 3 + 5j).
    • Dictionary (dict): Unordered collections of key-value pairs (e.g., my_dict = {5: 4, 4: 7, 9: 8}).
    • Boolean (bool): Represents truth values, either True or False.
    • Set (set): Unordered collections of unique elements (e.g., my_set = {1, 2, 3}).
    • Sequence Types: Ordered collections of items.
    • String (str): Sequences of characters enclosed in quotes (e.g., “hello”).
    • List (list): Ordered, mutable sequences of items enclosed in square brackets (e.g., my_list = [1, “hello”, 3.14]).
    • Tuple (tuple): Ordered, immutable sequences of items enclosed in parentheses (e.g., my_tuple = (1, “hello”, 3.14)).

    3. What are keywords in Python, and what are the rules for using them?

    Keywords in Python are reserved words that have specific meanings and purposes within the language. They cannot be used as identifiers, such as variable names, function names, or class names. Python has a predefined set of keywords (e.g., if, else, for, while, def, class, True, False, None). You can see a list of Python keywords using the keyword module and keyword.kwlist. Attempting to use a keyword as an identifier will result in a syntax error.

    4. What is indentation in Python, and why is it important?

    Indentation in Python refers to the spaces or tabs used at the beginning of a line of code to define code blocks. Unlike many other programming languages that use curly braces {} to delimit blocks, Python relies solely on indentation. Consistent indentation is crucial because it determines the structure and execution flow of the program, especially within control flow statements like if, for, and while, as well as in function and class definitions. Incorrect indentation will lead to IndentationError and will change the logical grouping of statements. The standard convention in Python is to use 4 spaces for each level of indentation.

    5. How do you take input from the user and display output in Python?

    In Python, you can take input from the user using the input() function. This function prompts the user with an optional message and returns the user’s input as a string. If you need the input to be of a specific data type (like an integer or float), you need to explicitly convert it using functions like int() or float(). For example:

    name = input(“Enter your name: “)

    age = int(input(“Enter your age: “))

    To display output in Python, you use the print() function. You can print strings, variables, and the results of expressions. You can also format the output using f-strings or the .format() method to include variables within strings. For example:

    print(“Hello,”, name)

    print(f”You are {age} years old.”)

    result = 10 + 5

    print(“The result is”, result)

    6. What are operators in Python, and what are the main categories of operators?

    Operators in Python are symbols that perform operations on values (operands). Python supports various types of operators, including:

    • Arithmetic Operators: Used for mathematical calculations (e.g., + for addition, – for subtraction, * for multiplication, / for division, ** for exponentiation, // for floor division, % for modulo).
    • Assignment Operators: Used to assign values to variables (e.g., =, +=, -=, *=, /=, %=).
    • Relational (Comparison) Operators: Used to compare values (e.g., == for equal to, != for not equal to, > for greater than, < for less than, >= for greater than or equal to, <= for less than or equal to). These operators return Boolean values (True or False).
    • Logical Operators: Used to combine or modify Boolean values (e.g., and, or, not).
    • Identity Operators: Used to check if two variables refer to the same object in memory (is, is not). Note that for small integers (-5 to 255), Python might reuse the same object ID.
    • Membership Operators: Used to test if a value or variable is found in a sequence (in, not in).
    • Bitwise Operators: Used to perform bit-level operations on integers (&, |, ^, ~, <<, >>).

    7. Can you explain the concept of immutability and mutability in Python data types and provide examples?

    Immutability: An immutable data type is one whose value cannot be changed after it is created. If you perform an operation that seems to modify an immutable object, you are actually creating a new object with the modified value. Examples of immutable data types in Python include:

    • Numbers (int, float, complex): When you add two numbers, you get a new number object.
    • Strings (str): String operations like concatenation or slicing create new string objects. You cannot change individual characters of a string in place.
    • Tuples (tuple): Once a tuple is created, you cannot add, remove, or modify its elements.

    Mutability: A mutable data type is one whose value can be changed in place after it is created, without creating a new object. Examples of mutable data types in Python include:

    • Lists (list): You can add, remove, or modify elements of a list directly using methods like append(), insert(), remove(), or by assigning to specific indices.
    • Dictionaries (dict): You can add, remove, or modify key-value pairs in a dictionary after its creation.
    • Sets (set): You can add or remove elements from a set using methods like add() and remove().

    The distinction between mutable and immutable types is important for understanding how data is handled and how variables behave in Python, especially when passing objects to functions or assigning them to multiple variables.

    8. What are the key characteristics and operations associated with Python’s sequence data types (strings, lists, tuples)?

    Strings (str):

    • Ordered: Characters in a string have a specific order, and you can access them using positive and negative indexing.
    • Immutable: Once a string is created, its characters cannot be changed.
    • Operations:Slicing: Extracting a portion of a string using index ranges (e.g., s[1:5]).
    • Concatenation: Combining strings using the + operator (e.g., “hello” + ” world”).
    • Repetition: Repeating a string using the * operator (e.g., “abc” * 3).
    • Methods: Numerous built-in methods for string manipulation like upper(), lower(), capitalize(), count(), find(), replace(), split(), join().
    • String Formatting: Using f-strings or the .format() method to embed variables in strings.
    • Escape Sequences: Special character combinations like \n for newline, \t for tab.

    Lists (list):

    • Ordered: Items in a list have a specific order, accessible by index.
    • Mutable: You can change the elements of a list after it’s created.
    • Operations:Slicing: Similar to strings, but returns a new list.
    • Concatenation: Using the + operator to combine lists.
    • Repetition: Using the * operator to repeat list elements.
    • Methods: Many methods for modifying lists: append(), insert(), remove(), pop(), sort(), reverse(), extend(), count(), index().
    • List Comprehension: A concise way to create lists based on existing iterables.
    • Supports nested lists (lists within lists).

    Tuples (tuple):

    • Ordered: Elements in a tuple have a specific order, accessible by index.
    • Immutable: Once a tuple is created, its elements cannot be changed.
    • Operations:Slicing: Returns a new tuple.
    • Concatenation: Using the + operator to combine tuples (creates a new tuple).
    • Repetition: Using the * operator to repeat tuple elements (creates a new tuple).
    • Methods: Fewer methods compared to lists: count(), index().
    • Tuples are often used for fixed collections of items and can be more memory-efficient than lists in some cases. They can also be used as keys in dictionaries (unlike lists).

    Jupyter Notebook and Python Fundamentals Illustrated

    Based on the provided source, here are some basics of Jupyter Notebook:

    Installation and Launching:

    • Jupyter Notebook can be installed in several ways, including using Anaconda or Miniconda, or directly via the command prompt using pip.
    • The source recommends using Anaconda for data science as it conveniently installs Jupyter Notebook, along with other useful tools like Spyder, and comes with many pre-installed libraries such as pandas, NumPy, Matplotlib, and Plotly.
    • To install Anaconda, you can go to the official website and download the free version. The installation process involves a simple double-click and following the on-screen instructions.
    • After installing Anaconda, you can find the Anaconda Navigator in your applications (e.g., under the “Anaconda3” folder in Windows). From the Navigator, you can launch Jupyter Notebook.
    • Alternatively, you can directly search for “Jupyter Notebook” in your operating system’s search bar to open it.
    • When launched, Jupyter Notebook typically opens in your default web browser.

    Interface and Usage:

    • Jupyter Notebook opens in a specific local directory on your computer (e.g., your C drive user directory).
    • The interface allows you to navigate through your files and folders and create new notebooks.
    • To start writing Python code, you can create a new Python 3 notebook.
    • Code is written and executed in cells. You can write Python code in a cell and run it by clicking the “run” button or by using the shortcut Shift+Enter.
    • Jupyter Notebook files have the extension .ipynb.

    Key Features and Concepts Illustrated in the Source:

    • Basic Python Concepts: The source uses Jupyter Notebook extensively to demonstrate fundamental Python concepts such as:
    • Variables and how they store data according to their data types.
    • Data types, including numeric (integer, float, complex), string, list, tuple, dictionary, and set, with examples of how to define and check their types using the type() function.
    • Memory allocation for variables and how to check their identification numbers using the id() function.
    • Keywords and how to list them using the keyword module.
    • Indentation as a way to define code blocks in Python.
    • Comments using the hash symbol #.
    • Operators: Jupyter Notebook is used to practically demonstrate different types of operators:
    • Arithmetic operators (+, -, *, /, %, //, **).
    • Assignment operators (=, +=, -=, *=, /=, etc.).
    • Relational (Comparison) operators (>, <, >=, <=, ==, !=) which always return a Boolean value (True or False). These are also referred to as conditional operators.
    • Logical operators (and, or, not) which work with Boolean values.
    • Identity operators (is, is not) which compare object identity (memory location).
    • Membership operators (in, not in) which check if a value is present in a sequence.
    • Data Structures: The source provides examples in Jupyter Notebook to illustrate the properties and operations of various data structures:
    • Strings: Including slicing, concatenation using the + operator, repetition using the * operator, and various built-in methods (e.g., .upper(), .lower(), .capitalize(), .count(), .index()) and functions (e.g., len(), min(), max()). It also covers string formatting, skip sequences (e.g., \n, \t), and raw strings (using r’…’).
    • Tuples: Showing how to define them using round brackets, their ordered and immutable nature, positive and negative indexing, slicing, concatenation, repetition, and the available methods (.count(), .index()) and functions (len(), min(), max(), tuple(), type()).
    • Lists: Demonstrating their definition using square brackets, their mutable and ordered nature, indexing, slicing, concatenation, repetition, and numerous methods (e.g., .append(), .insert(), .remove(), .pop(), .sort(), .reverse(), .clear()) and functions (e.g., len(), min(), max(), list(), type()). The source also introduces list comprehension as a way to optimize code.
    • Dictionaries: Illustrating their definition using curly braces with key-value pairs, their mutable and unordered nature, accessing values using keys, and various methods (e.g., .keys(), .values(), .items(), .pop(), .clear(), .get(), .update()) and functions (e.g., len(), type(), dict()). The source also touches upon how dictionaries can be used to represent tabular data and introduces the pandas library in this context.
    • Sets: Showing their definition using curly braces (for non-empty sets; a blank set is created using set()), their mutable and unordered nature, the property of storing only unique values, and various operations (union, intersection, difference) and methods (e.g., .add(), .remove(), .update(), .intersection(), .union(), .difference()). The source also mentions set comprehension and frozensets (immutable sets).
    • Control Flow: Although not a primary focus of the initial overview, the source implicitly uses conditional statements (if, elif, else) and loops (for) in the Jupyter Notebook examples to demonstrate various Python concepts.
    • Functions: The source explains different types of functions (user-defined, built-in, recursive, anonymous/lambda, return statements) and mentions their coverage in the video tutorial. It also differentiates between functions (like len(), type()) and methods (called on an object using dot notation, like string.upper(), list.append()).
    • Modules and Packages: The source introduces the concept of modules and packages as Python’s way of providing strong standard libraries. It mentions using the keyword module to explore Python keywords and briefly introduces the pandas library for data manipulation.
    • File Handling: The source demonstrates basic file operations (opening, reading, writing, closing) for text files in Jupyter Notebook, along with functions like open(), close(), read(), readline(), write(), writelines(), seek(), and tell(). It also introduces different file modes (e.g., ‘r’, ‘w’, ‘a’). Furthermore, it covers working with pickle (for preserving Python objects in binary format), JSON (a common data exchange format), and CSV (Comma Separated Values) files within Jupyter Notebook.
    • Exception Handling: The try and except blocks for handling errors are introduced with a practical example in Jupyter Notebook.
    • Object-Oriented Programming (OOP): The source begins to cover OOP concepts with examples in Jupyter Notebook, including defining classes and creating objects, defining methods (including the special __init__ constructor), inheritance (single and hybrid), inner classes, and different types of variables (instance, class, static) and methods.
    • Recursion: The concept of recursion (a function calling itself) and the recursion limit in Python are explored with examples in Jupyter Notebook.

    In summary, Jupyter Notebook is presented as an interactive environment that is highly suitable for learning and experimenting with Python programming concepts, especially in the context of data science, due to its ease of use, immediate feedback through cell execution, and integration with essential libraries. The source uses Jupyter Notebook as the primary tool for demonstrating a wide range of Python topics, from basic syntax and data types to more advanced concepts like file handling, exception handling, and object-oriented programming.

    Python Data Types Explained

    Based on the information in the source “01.pdf” and our previous discussion about Jupyter Notebook [Me], here’s a discussion of Python data types:

    Python has several built-in data types that are fundamental to the language. The source categorizes these mainly into numeric, dictionary, boolean, set, and sequence types. It further elaborates on these categories, highlighting their key characteristics.

    Main Categories of Python Data Types:

    • Numeric Data Types: These represent numerical values.
    • Integer (int): Whole numbers without any decimal point (e.g., -2, -1, 0, 1, 2). The source mentions that when you check the data type of a whole number in Python, it is shown as int.
    • Float (float): Numbers with decimal points (e.g., -1.02, 1.5, 5.86). If a number is defined with a decimal, Python considers it a float.
    • Complex (complex): Numbers with a real and an imaginary part, represented in the form a + bj (where j denotes the imaginary unit) (e.g., 5 + 7j). In Python, the imaginary part is denoted by j, unlike the mathematical convention of i.
    • Sequence Types: These represent ordered collections of items.
    • String (str): Immutable and ordered sequences of characters. Strings are defined using single quotes (e.g., ‘hello’), double quotes (e.g., “India”), or triple quotes for multi-line strings. They support positive and negative indexing starting from 0 and -1 respectively, allowing for slicing. Operations like concatenation using + and repetition using * are also supported. The source discusses various string methods (e.g., .upper(), .lower(), .capitalize(), .count(), .index()) and functions (e.g., len()). String formatting, skip sequences (e.g., \n, \t), and raw strings (using r’…’) are also mentioned.
    • List (list): Mutable and ordered sequences of items. Lists are defined using square brackets [] and can contain items of different data types. They support indexing, slicing, concatenation, and repetition. Due to their mutability, lists have numerous methods for modification, such as .append(), .insert(), .remove(), .pop(), .sort(), .reverse(), and .clear(). The source also introduces list comprehension as a concise way to create lists.
    • Tuple (tuple): Immutable and ordered sequences of items. Tuples are defined using round brackets (). They support indexing, slicing, concatenation, and repetition. Compared to lists, tuples have fewer built-in methods, mainly .count() and .index(). Tuples are often used for security purposes when the data should not be changed.
    • Mapping Type:
    • Dictionary (dict): Mutable and unordered collections of key-value pairs. Dictionaries are defined using curly braces {} with keys and their corresponding values separated by a colon : (e.g., {‘a’: 15, ‘b’: 18}). Keys in a dictionary must be unique, but values can be of any data type. You access values in a dictionary using their keys (e.g., D[‘a’]). Dictionaries do not support direct concatenation or multiplication like sequences. They have various methods like .keys(), .values(), .items(), .pop(), .clear(), .get(), and .update(). The source also mentions dictionary comprehension and how dictionaries can represent tabular data, leading to the introduction of the pandas library.
    • Set Types: These represent unordered collections of unique items.
    • Set (set): Mutable and unordered collections of unique elements. Sets are defined using curly braces {} (e.g., {5, 8, 7.9}). A blank set is created using the set() function, as {} creates an empty dictionary. Sets automatically remove duplicate values. They support operations like union, intersection, and difference, and methods like .add(), .remove(), and .update(). The source also discusses set comprehension.
    • Frozen Set (frozenset): Immutable and unordered versions of sets. Once created, you cannot add or remove elements from a frozenset. They support the same operations as regular sets but without modification methods.

    Mutability and Order:

    The source emphasizes the concepts of mutability (whether the object’s state can be changed after creation) and order (whether the elements have a specific sequence).

    • Mutable Data Types: Lists, dictionaries, and sets are mutable, meaning their contents can be modified after they are created.
    • Immutable Data Types: Numeric types (integer, float, complex), strings, and tuples are immutable, meaning their contents cannot be changed after they are created. Any operation that appears to modify an immutable object actually creates a new object.
    • Ordered Data Types: Strings, lists, and tuples are ordered, meaning the items in the collection have a specific sequence, and you can access them using index positions.
    • Unordered Data Types: Dictionaries and sets are unordered, meaning the elements do not have a specific sequence, and you cannot access them using numerical indices. In dictionaries, you access elements using keys.

    Checking Data Types:

    The built-in type() function is used to determine the data type of a variable. As shown in the Jupyter Notebook examples discussed previously, you can use type(variable_name) to see whether a variable holds an integer (int), a float (float), a string (str), a list (list), a tuple (tuple), a dictionary (dict), or a set (set) [Me, 6, 12].

    The source highlights that understanding data types is crucial as it affects how data can be stored, manipulated, and the operations that can be performed on it. The practical examples in Jupyter Notebook, which we discussed earlier, effectively illustrate the creation, properties, and operations associated with each of these fundamental Python data types [Me].

    Python Variable Identification Numbers in Memory

    Based on the information in the source “01.pdf” and our previous discussion about Python data types [Me], here’s a discussion of variable identification numbers in Python:

    In Python, when you create a variable and assign it a value, that value is stored in the computer’s memory. To keep track of where each piece of data is stored, Python assigns a unique identification number to each object in memory, including the values held by variables. You can think of this identification number as the memory address of the data.

    The source explicitly mentions the id() function as a way to find out the identification number of an object (and thus, the value held by a variable). For example, if you have a variable a assigned the string “hello” and a variable b assigned the integer 5, you can use id(a) and id(b) to see their respective identification numbers.

    Key points about variable identification numbers from the source:

    • Memory Allocation: A variable acts as a temporary container that stores data. This storage occurs in memory allocation.
    • Identification Number: Every piece of data stored in memory is assigned a unique identification number.
    • id() Function: Python provides a built-in function called id() that allows you to retrieve the identification number of an object. You pass a variable (which holds a reference to the object) to the id() function to see its identification number (e.g., id(a)).
    • Data Type Influence: The source demonstrates that the data type of a variable can influence its identification number. When comparing the identification numbers of a string variable (a = “hello”) and an integer variable (b = 15), a significant gap might be observed between their IDs. This suggests that different data types might be stored in different regions of memory.
    • Memory Allocation and Data Type: The memory ID released depends on the data type. Different data types might lead to different patterns in memory allocation and thus potentially larger differences in their identification numbers.
    • Small Integer Range: The source provides an interesting observation about the identity operator (is) and identification numbers for a specific range of small integers (-5 to 255). Within this range, Python might use the same memory location for variables with the same integer value. However, for values outside this range or for different data types (like strings and integers), different memory locations and thus different identification numbers are typically assigned. This behavior is related to Python’s internal optimizations for commonly used small integers.

    In essence, variable identification numbers in Python provide a way to uniquely identify where the value of a variable is stored in memory. The id() function allows you to inspect these numbers, and you can observe how factors like data type can affect memory allocation and the resulting identification numbers.

    Python Operators: An Overview

    Based on the information in the source “01.pdf”, here’s an overview of Python operators:

    The source introduces the topic of operators by defining them as symbols that are responsible for performing operations. It gives the example of 5 + 7, where + is the operator and 5 and 7 are the operands. The source categorizes Python operators into six main types:

    • Arithmetic Operators: These operators are used to perform mathematical calculations. The source lists the following arithmetic operators:
    • + (Addition): Adds two operands (e.g., 4 + 7 results in 11). It can also be used for string concatenation (e.g., “hi” + “hello”).
    • – (Subtraction): Subtracts the second operand from the first.
    • * (Multiplication): Multiplies two operands.
    • / (Division): Divides the first operand by the second, resulting in a floating-point number.
    • % (Modulo): Returns the remainder of the division of the first operand by the second. The source refers to this as the “modulo operator”.
    • ** (Exponentiation): Raises the first operand to the power of the second. The source explicitly mentions this “double star” operator.
    • // (Floor Division): Divides the first operand by the second and returns the integer part of the quotient, discarding any remainder. The source highlights this “double slash” operator.
    • Assignment Operators: These operators are used to assign values to variables. The source provides the following examples:
    • = (Assignment): Assigns the value of the right operand to the left operand (e.g., a = 7).
    • += (Add and Assign): Adds the right operand to the left operand and assigns the result to the left operand (e.g., a += 5 is equivalent to a = a + 5).
    • -= (Subtract and Assign): Subtracts the right operand from the left operand and assigns the result to the left operand (e.g., a -= 7 is equivalent to a = a – 7).
    • *= (Multiply and Assign).
    • /= (Divide and Assign).
    • %= (Modulo and Assign).
    • **= (Exponentiate and Assign).
    • //= (Floor Divide and Assign).
    • Relational Operators (Comparison Operators, Conditional Operators): These operators compare the values of two operands and return a boolean value (True or False). The source lists these operators:
    • > (Greater than).
    • < (Less than).
    • >= (Greater than or equal to).
    • <= (Less than or equal to).
    • == (Equal to): Compares if two operands are equal. The source distinguishes this from the assignment operator =.
    • != (Not equal to).
    • Logical Operators: These operators perform logical operations and are often used to combine the results of relational operations. The source identifies the following logical operators:
    • and: Returns True if both operands are True. The source notes the importance of using brackets when combining and with other logical operators to avoid ambiguity.
    • or: Returns True if at least one of the operands is True.
    • not: Returns True if the operand is False, and False if the operand is True.
    • Identity Operators: These operators check if two operands refer to the same object in memory. The source mentions:
    • is: Returns True if both operands refer to the same object. The source notes it is similar to == but works with memory allocation. It highlights that for integers in the range of -5 to 255, is might behave similarly to == due to Python’s memory optimization, but this might not hold for values outside this range or for different data types.
    • is not: Returns True if both operands do not refer to the same object. It’s similar to != in that it checks for non-identity based on memory location.
    • Membership Operators: These operators test if a value (operand) is found within a sequence (e.g., list, string, tuple). The source lists:
    • in: Returns True if the value is present in the sequence. The source gives an example of checking if 5 is in a list.
    • not in: Returns True if the value is not present in the sequence.

    The source also briefly mentions a bitwise operator, noting it is not much required for general programming but can be important in specific fields like electronics. It provides a resource for further learning about bitwise operators.

    In summary, the source provides a foundational understanding of various Python operators, categorizing them by their function and illustrating their basic usage with examples. It emphasizes that practical implementation in a Jupyter Notebook (available via a link in the video description) is crucial for a deeper understanding of these operators.

    Python Indentation: Defining Code Blocks

    Based on the information in the source “01.pdf”, here’s a discussion of indentation and blocks in Python:

    Indentation is a fundamental concept in Python that is used to define code blocks. Unlike many other programming languages that use curly braces {} to delineate blocks of code, Python relies solely on whitespace (spaces or tabs) at the beginning of a line to indicate which statements belong to a particular block.

    The source emphasizes that in Python, curly braces are not acceptable for defining blocks. Instead, after certain statements that introduce a block (such as if, elif, else, for, while, and function definitions), a colon : is used, and the subsequent lines of code that belong to that block must be indented.

    Key points about indentation from the source:

    • Defining Blocks: Indentation is how Python determines which statements are part of a specific block of code, such as the body of a conditional statement or a loop.
    • Colon Precedes Indentation: Statements that begin a new block of code are always followed by a colon :.
    • Four Spaces (Typically): The source notes that in environments like Jupyter Notebook and PyCharm, an indentation of four spaces is automatically taken after a colon. Other IDEs might default to two or eight spaces, but consistent use within a project is crucial.
    • Block Delimitation: The level of indentation determines the scope of a block. Statements with the same level of indentation are considered part of the same block. If a line is not indented at the expected level, it is considered to be outside the current block.
    • Example (from the source):a = 9
    • b = 8
    • if a > b: # Colon indicates the start of a block
    • print(“hello”) # Indented – part of the if block
    • print(“hi”) # Indented – part of the if block
    • print(“hey”) # Indented – part of the if block
    • print(“bye”) # Not indented to the same level – outside the if block
    • In this example, the print(“hello”), print(“hi”), and print(“hey”) statements are inside the if block because they are indented. The print(“bye”) statement is outside the if block because it is not indented to the same level.

    Blocks in Python are sequences of one or more statements that are treated as a single unit. They are defined by their indentation level. Blocks are typically associated with control flow structures (like conditional statements and loops) and function/class definitions.

    Significance of Indentation:

    • Readability: Python’s use of indentation makes code very readable and enforces a consistent visual structure, making it easier to understand the flow of control.
    • Syntax: Indentation is not just for readability; it is a syntactic requirement in Python. Incorrect indentation will lead to IndentationError and will prevent the code from running.

    In summary, indentation in Python is not merely a matter of style; it is a core part of the language’s syntax used to define code blocks. Consistent and correct indentation is essential for writing valid and understandable Python programs.

    🔥 Complete Python Course (Basic to Advanced) | Full Tutorial with Practical Implementation 🚀

    The Original Text

    nowadays Python programming language is booming day by day and it’s required for every industry in Automation in data science in data analysis artificial intelligence and many I made this course for absolutely for beginner from basic to advanc level in easy way and you’ll find it out the complete resource on a GitHub that link is available on description let’s first discuss what topic we’ll cover in this video so for first we’ll start with the overview where we will discuss about history and why should we learn a python what their competitor why should we learn a python for data sign if the lots of programming language is available then we’ll install the required software like py Cham Jupiter notebook anac iser software will anac iser providing the Jupiter notebook and spider so initially we’ll start with a Jupiter notebook and after that we’ll also discuss somewhere pyam as well depend on the requirement and then we’ll discuss about their basics of pythons like uh we’ll discuss variables keyword indentation commands so these are a topic we’ll first discuss and after that we’ll enter the very very important topic which is the backbone of data science data types so numeric string tle dictionary set these are data type this is the primary data type we can say we’ll discuss then and after that conditional statement and loop okay so all the uh Topics in a condition statement and loop will cover and after that functions so user defined function inbuild function recursion function Anonymous function return statements so lots of topic is there in a function will cover each and everything and then package and modules so python become strong because of large standard libraries so library is made with packages and module so we’ll discuss that topic as well so user Define predefine and find search path and some names SP lots of topic is there we’ll discuss in this video tutorials so we can say these are a topic it’s a Basics and intermediate and advanced python is also there which one is file handling will discuss exception handling objectoriented programming and multi-threading let’s discuss about the complete overview of python so here in the market so many languages is available but why should we learn a python right so our entire tutorials is a focused on the data science I’ll take the example of the data science and uh in that direction we’ll discuss so basically machine learning and artificial intelligence obviously it comes under the data science will build on the mathematical principle like a calculus algebra probability statistics so we have to choose those programming language which is very much compartible with this mathematical concept because because nowadays the this Ai and automation concept is booming right so we have to choose those language so python obviously is the best for them but what is the reason before going to the python let’s understand the concept of the machine learning okay so machine learning basically a application of artificial intelligence when a machine can learn automatically from the previous experiences let’s say for example you meet a person after 5 year okay and suddenly you start predicting that the person can be the Priya Raju and moan any any name you are just start predicting and how we able to predict predict it the reason behind that because that kind of pattern is already store in your brain so accordingly you are predicting okay so in a real life so we have the large amount of data let’s take example of Instagram so Instagram the huge amount of data they are recognizing the pattern and accordingly is giving the suggestion to you right so in that algorithm in that pattern Rec in pattern calculation we required a those programming language which should be compatible with the mathematical concept so python is one of them but why python why only the python why not other like only the python can be used in the data science the answer is no we can use the any programming language language in a data science in a machine learning but we have to choose those which is comfortable with mathematical concept so let’s understand that so in a real life there is a two type of categories there of a programming language the first one is a static and the second one is a dynamic what is a static static in the sense so we’ll take example of java in a static and python as a dynamic okay Java is a static programming language and python is a dynamic programming language so basically we are writing uh Java like uh in this this is the script of a Java we’re writing the code but here when you’re assigning the value is 9 uh in a and four in a b so after the calculation a divide by B the answer should be the 2.25 but you’ll get the answer is two the reason I already defined the data type okay Define the data type which is a integer because of the integer it will print only the two so what is the concept behind that because it will take the values the 2.25 it will take the inputs but after translation after the translation it will start comparing the target values which is the C this is the target variable and value is a 2.25 comparing with the input values which is a in C after comparing whatever values we get it and that is showing as a result which is a two all right so the translator in a Java programming language we are calling the compiler okay so compiler is nothing but a translator not in a Java in every programming language compiler is exist but in a Java we normally calling compiler in a python we have a different name but the compiler is there a different name is is inter uh interpreter okay so we understand that the Java is a static programming language but how the python is working so python is a dynamic programming language it directly take the inputs translate it and getting the result how basically is working you can directly assign a is equal to 5 b is equal to 8 and C is equal to B / a and when you just divide it you’ll getting the proper decimal values I’ll I’ll show you some real example okay I’ll show you some real examples so that you can easily understand suppose I have a value is a is equal to 5 b is equal to 8 and C is equal to B / a and get the value C that will getting 1.6 that is the proper floating values but when you just take the data type of a you’ll get the values is integer when you check the data type of B you’ll get the uh data type of B which is a integer but when you check the data type of C which is a float automatically converting the data type so the conclusion is that in the static and dynamic in a static programming language we have to take care about the data type where the dynamic programming language we don’t need to take care about the data type but again so as I said that the every programming language have a compiler means the translator have also the compiler but why it’s not comparing you the input values the reason behind that so whatever source code you have so again the compiler will help you to convert your source code into bite code and there is a virtual machine is available in every programming language like in a Java we we have a jvm in a python we have a pvm python virtual machine so here the compiler and virtual machine is running together that’s why we are directly getting the answer and in a jvm jvm the translator and virtual machine is a running separately so that is the reason we are getting the result based on the input values so which one is a dynamic programming language which one is a static programming language we can say the python JavaScript JavaScript and Java is both are a different language script and Par language and Julia these are a dynamic programming language where is a Java C C++ these are a static programming language okay so these are all these all are a dynamic programming language are matlb Julia SAS JavaScript scalap python this all are a dynamic programming language then again why we choose a python right for a data sici why not we are choosing the r language why not a mat Li so again these all are good to choose uh um uh for data science but every programming language have a limitation python have also the limitation but it our label almost every field if I want to use a Python programming language in the web development via Jango and a flask we can you easily implement the web application if I want to go in a cloud infrastructure that python option is available the data analysis is there in a testing field is there so almost every sector python is available because when you go in a Technology field so it’s not like that you’re you’re doing the analysis on uh you know some uh some PP and dashboard you show in and finished no behind that the lots of pipeline is running so in the pipeline so python is also very good in a data engineering as well so that is the reason python is become very famous and most of the companies are using this python so let’s conclude it what exactly the python this this will be the definition of a python so python is nothing but whatever discuss we’ll just summarize it here so python is first of all high level programming language high level in the sense those language which requir the translator is a high level so almost every programming language is a high level there is two language is a low level and high level low level in the sense machine language which is a binary which computer understand but we are not writing the code in a binary format we writing the code in the normal format like A + B / by 2 so it is a high language source code for computer the high language is not for human not for a student it’s for a computer the computer is saying that hey I understand only the binary and you’re writing something so I need a translator so that is the reason the computer is saying that it’s a high level second one is interpreter language which we just discussed and uh uh for translator we are using this interpreter and uh dynamically type language okay just now we discussed and it have a large standard libraries almost every field the python is available just because of large standard libraries okay so for if I want to do anything just have to install and start implementation it’s not like like that easy but yes the option is available to work in any any uh domain with python so it’s a portable portable in the sense it’s comfortable with any operating system we can use with the uh Windows Linux and Mac so even in the Linux it’s by default the python is available the reason behind that I’ll discuss in a history of python okay but it’s a comfortable with uh all the three operating system a the main thing of the portable language is that suppose I wrote a program in uh Windows operating system and one of my friend is using uh Mac or uh Linux so they don’t need to change any kind of environment they can directly use it if they have a python okay so that is a meup portable it have EXT uh extensibility features in the sense inside uh python I can also write the r language I can write a Java language J python option is also there so we we can extend our Python programming language so python is also supporting the objectoriented concept which is very very important for a software development almost 99% of software is making with objectoriented Concept so that is the reason Java is become very very famous because of this oops concept and again the last concept uh last topic is a free and open source free means you don’t need to pay anything open source in the sense so that source code is available for us we can also check it okay so whenever you install the python that any if you have any libraries like if you install the pandas okay which is famous libraries for a data science if you install the pandas so behind the pandas uh whatever source code is there we can also check it so that is the meaning of source code so let’s discuss about the history of python so history of python is very interesting uh like U um the python is old language or a new language uh in nowadays the two languages are very very famous is Python and Java right so Java is is a versatile language like almost every field is available it’s capture the market um it but the people are thinking that uh the Java is old but answer is no Java is not old python is old those who made the python the name is a good when rasim goo van R so this guy is made started implementation in 1989 so he was a mathematician he was a very uh you know famous mathematician he got many medals in the mathematics uh he did a masters in uh mathematics and computer science so he start doing researching in the mathematics so his main focus was that to um you know solving the mathematical problem so when he started the uh python in 1989 when the Java comes in the market is the 19 1996 1996 okay so it started in the 1989 and uh and he released the first he released the first version is is python 0 .9 which supportable for one andux after that he released for everyone he released for everyone and uh uh you know that be supported for Windows Linux and uh uh Mac all the operating system and this um the Python programming language is start becoming famous after 2000 okay when he released the 2.x version so even nowadays we also confused that which one we have to use 2.x or 3.x that I’ll discuss uh after this after this history so here when he release the 2.x this Python programming language is properly enter in uh software development field before that he was this programming language was in research field in uh mathematical research you can say uh so before the Java is entered in a you know in um a software development field that’s why we think that the Java is very old but actually the python is old programming language actually this guy is working in a many famous companies like uh Google Microsoft Microsoft and Dropbox so these famous companies is worked on it so python has become famous when he was in Google he was in Google in a 2000 5 to 2012 2005 to 2012 he was doing half of the work to developing the python so when he released the 3.x version of 2008 so that time the python start booming in all over the world especially in the data sign and artificial intelligence field because the Google company already doing a lots of research in artificial intelligence so he got a very good opportunity and uh python we Implement python is become The Versatile programming language and the people I start using after 3.x version so sometime we confuse that what is the difference uh between the 2.x and 3.x actually when he released the uh 2.x version 2.x so he forget to implement the object Orient concept so later when he try to implementing the object oriented Concept in a 2.x so the P the programming become very unstable so that is the reason uh he started a 3.x version and even the many people already started the 2.x that’s why he didn’t deleted that uh version but we can say the 2.x is a kind of Legacy but if you are a new if you are a beginner it started the uh this Python programming language so obviously you have to use 3.x you don’t need to use 2.x okay because when you write the the syntax of the Python 2.x and 3.x is a little bit different so like if you want to write a p uh print statement for example uh print hello world hello world so in a threo x we are writing like this but if you want to write in a twoo x so you have to write print double quotes hello world so there is no any bracket so the syntax is also different because the 2.x is not completely supporting the objectoriented programming language installation of Jupiter notebook we have a multiple option is available you can choose anything so like we have a anaconda let me just change the color yeah we have Anaconda we have a minond okay so multiple option is there so uh even you can directly uh install the Jupiter notebook via the command prompt as well but we will choose the Anaconda okay because Anaconda is giving the three options uh for a data science which is really useful for us so you will find it out a jupyter notebook first okay you’ll find a jupter notebook and you also find it out the spider which is very helpful for uh data science even uh in Anaconda is a pre predefine the many libraries so which you don’t need to install it like a pandas naai M plotly this libraries is preinstalled you don’t need to install it separately so many uh option is there so you can use it so we’ll install here the Anaconda and after that we start the installation solution of spider and Jupiter notebook so let’s do that so you can directly go to the official website anakonda just go there you’ll find it out the free download option just click on download that’s it your downloading process will start when you just check it let me just cancel it this my my downloading is already um on I put on a run so it will taking a Max to Max 1 minute to complete it and after that I’ll start the installation so yeah downloading process uh is completed I think so I can also check it in a download no it’s still downloading I think it’s taking ah now it’s downloaded so you can go there it’s showing the uh this icon double click on it it’s very simple just double click on it okay so it’s verifying and just click next I agree just me next you don’t need to change anything just next install you can just add the uh anakonda 3 in your environment if you’re not adding it’s not creating any problem just click install that’s it now it’s completed I think just click next so you can see here the Anaconda Jupiter notebook logo is mentioned there just finish it okay it’s giving the for uh registration for free and so many option don’t do anything just close now back click on Windows you’ll F and all app whatever which one you are using Windows 10 Windows 11 you’ll find it out the Anaconda 3 folder so where we have a anaconda Navigator where in case if any jupter notebook or spider anything is not working properly go to the anakonda navigator that work like a setting and you can use it so anakonda prompt is a kind of command prompt okay so I think it start opening the anakonda Navigator even I I’ll show you as well so in this anakonda prompt you can install any libraries with Pip or k environment so this is like a uh prompt which we have a local machine as well so this Anaconda is provided their own prompt okay this one is the Navigator let me just close it this one yeah Navigator the so many option is showing in case if anything is not working you can launch it from here like py Cham or um yeah py Cham professional R studio so many option they are showing but you don’t need to do anything this one let me just close it okay back to that uh folder again Anaconda 3 yes so you’ll find it out a jupyter notebook and a spider you can just open on it so for Jupiter notebook you can open from here or else you can directly search Jupiter notebook okay will search Jupiter notebook in this location so automatically the Jupiter notebook is opening on the default location which is uh it’s showing the uh prompt here it will open the uh Jupiter notebook in this location C drive you can see the serving notebook from the local directory C user your PC name all right so you can check it so where is it store opening so always opening in this location so my jupyter notebook is open here you can just directly click on new Python 3 and start writing the code is here sometime your jupyter notebook is opening on a you know Chrome or sometime opening on a Internet Explorer so which one is your default um uh web browser okay so you can write here print hello world let me just type it here hello world and click on run you’ll get the answer here so now Jupiter notebook is a properly installed let’s discuss about the third module basics of python where we’ll discuss about variable keyword indentation data type commands and lots of things the very first one is a variable variable is nothing but a memory allocation which store the data according to their data type okay for example I have one one data I want to store somewhere so we need a container right so variable working as a temporary container which store the data and we need a data type to Define that so if you storing the data and is defined with a data type like hello stor in a five store in a b so hello is store according to string data type and it is storing in the variable a five is storing in a b according to integer data type so whenever we Define the data types it it release the identification number so every data is storing in memory allocation and Define the identification number let’s understand with the uh practical implementation so I already created the proper Jupiter notebook file you can easily find it out on a description in the link that Jupiter notebook file okay so hope you know that how to open the Jupiter notebook in particular location so I have created my proper folder so file is available Basics so when I click on there so I can easily open my basic file that’s i p ynb file we can say jupit notebook file let me create a new Cale and write it down here no normal variables like a is equal to hello so when I click this button I can easily run it and when I just check the data type so there is a function predefined function is a type so even the type this topic is there function so where we’ll discuss we’ll create a function as well to check that what exactly the data type we are using the type of a so that’s a string data type when I check that b is equal to 15 and check the type of B then you can find it out it’s a integer data type and python is showing as a int so now as I said variable is nothing but a memory allocation if it’s a memory allocation which store the data that means it release some identification number as well so we can easily find it out like a ID there is the ID function where find it out the identification number when I pass ID of a so you can see here this is the ident identification number okay when I just check the ID of ID of B that is a identification number of your B variable and a variable actually the when you see the differ between the Vari variable a and variable B so lots of Gap is there because a is a string data type whereas B is a integer data type suppose I have a variable um C is equal to 0 and check the DAT uh check the identification number so ID of C this is the ID of c and a simultan I’m also checking with idea B then you’ll find it out the lot Gap is not there Gap is there obviously because the number is a different so when I just check it here you can see the last few digit is only change okay last few digit is only change but when I check with this one the lots of Gap is there reason we change the data type so it release the memory allocation memory uh ID depend on their data type if I’m taking the different data type the that will also be number D is equal to 19 + 19.8 and check the ID of D that will be the uh different identification number it’s not only the few digit is change so lots of values change also reason we change the data type because the D is a floating data type when I just check the type of d That’s a float data type so these are a variable hope you understand how the variable is working but I as I said when I Define any variable like a b is equal to 15 and it store the data according to the data type so now what is a v uh what is a data type so in a Python programming language the lots of data type is there so here python have a main category of the data type numeric dictionary Boolean set and sequence type where we have the main data type is a integer complex and Float integer complex and Float okay so we have also the another data type like a dictionary bullan set is also there and set oh yeah set is there string list and tle that’s comes under the sequence type so these are nothing but just a category so in the coming videos we’ll discuss about very details with each and every data type dictionary set string taple each and every data type will discuss in a very details way but let me just show you how the list is look like how the uh you know uh tle is look like how the string is look like so as you already see that how the integer look like if any number which Define without any decimal that’s a integer so here so data type we have different different data type is available like uh we have a string like if I’m defining with a double quotes so like hello here so that’s a string data type you define B is equal to in square bracket that will consider as a that will consider as a list if I Define with a round bracket that will be considered as a tle if I Define with the curly brasses curly brasses directly passing the values that is considered as a set if I Define the same value with key value pair is equal to key value pair like 5 colon 4 uh 4 colon 7 and 9 colon 8 so if you define like this that will be considered as a dictionary okay so that’s a dictionary that’s a set okay so what exactly the mean of this hash this hash symbol hash symbol is nothing but a comment we are defining as a comment so that’s a set that’s a topple that’s a list okay so these are a different different data type and this one is nothing but a string so in the coming video we have to discuss in the very details of each and every data type now let’s discuss about the keyword what exactly the keyword so keyword is nothing but a reserved word which can be used for only specific purpose and cannot be used as a identifier let me just write it down keyword keyword is reserve word which used for one specific purpose fake purpose cannot be used as a identifier cannot be used as a identifier now the question is what exactly the mean of identifier let me make as a uh markdown so that markdown in the sense it will taking one statement it will not consider as a code okay normal statements so cannot be used as a identifier so what is the identifier identifier so identifier in the sense any variable name any um you know uh function name okay any variable name any function name any class name that will be consider as a identifier class name that will be considered as a identifier okay so now the question is how many Reserve word is available in Python programming language so here in the lots of Reserve word is available but U to check that how many Reserve word is there so there is one module is called as keyword module keyword module keyword module and when you pass it here keyword do KW list then you’ll find it out how many keyword is available for false none true the lots of keyword is available is Lambda lots of keyword is there which is stored with a square bracket one container and that container we are calling list okay so as I said keyword is a reserve word which is used for only specific purpose cannot be used as a identifier used as a identifier what exactly the mean cannot be used as identifier like if I’m creating any normal variable able okay let me create one normal variable my variable name is a equal to 8 so I can easily Define it to not create any problem we can easily Define it here a is equal to 8 can I use uh any keyword like uh if I’m using if Okay small i f if we are using IF is equal to 8 can I define it like this no it will giving the error because because this if is reserved for only the conditional statement you can’t use as a identifier you can’t use as a normal variable so if is used for one conditional statement like if L if else true false is used for one bullion purpose okay so uh class is used for only object oriented break is only for breaking the loop so these are a reserved word which cannot be used for a normal purpose now I want to see that how many Reserve word is there I want to check that how many values is available first thing is that you have to just calculate one by one so this is the uh not a right way so the better is we can use like this there is one function is called as alen length which which will be count each and every element and tell you how many element is available in that container so I’ll use the keyword KW list and when I run it will showing you 30 five keyword is available okay so whenever you using any uh ideally right now I’m using the Jupiter notebook ideally so that time you can easily check it and uh you know the future definitely the python will increase the keyword okay so right now I’m using Jupiter notebook which is considered a 3.9 version which have the 35 keyword is available hope you understand this keyword now let’s discuss about indentation what exactly the indentation indentation in the sense is Define the block I have some program uh like if program uh conditional statement program if a is greater than let me first Define it a is equal to 9 and B is equal to 8 and I have a program is if a is greater than b okay so in other programming language we are using the curly brasses and put inside a statements so here curly brasses is not acceptable so we have a indentation we have to use a colon after the colon it automatically taking the four space okay automatically taking the four space if I’m writing here if you’re going with any other ideally uh maybe it will taking a two space eight space but in a Jupiter notebook and a py Cham is taking the four space if I’m writing here the hello okay so if you want to Define anything like hello if I’m writing here and hi if I’m writing here and he if I’m writing here print he okay this hello hi hey yeah we can say line number 5 six 7 that’s comes in if block so defining the block we are using if uh colon and automatically taking the for space if you’re using the back space and write it down here print by so that line number nine will consider as a uh outside of the block so that bu is not in if condition okay by is not in if condition so here we can see we are using by outside of the block because I I’m not using this four spaces so that’s called as a indentation if you have a single line of commands so we normally using like uh I have a statement is a print hello world and just use the hash if you’re using the hash it will be considered as a single line command like if you run it here you’re getting the answer is hello world I’m using the hash it here so it will making as a comment so single line comment we are using hash and for a multi-line comments suppose if you have multi-line of statements uh I have a same thing yeah this one I want to make a comment so for that you have to use a triple codes 1 2 3 okay start with triple codes and end with 1 2 3 normally when you just run it without using this triple codes it will showing the answer perfectly showing the answer hello hi whatever the program is there accordingly showing the answer but if you’re using the triple codes one 23 and one two 3 run it you’re not getting the answer you’re not getting the answer here actually it’s showing the as it is statements uh this triple quotes is also used for string as well so in a string time we’ll discuss that okay but it is also used for a multi-line commands so this one is a single line commands and this one is multi-line commands we are also using the double triple codes like this one this one okay so we also using a double triple Cotes so it’s a multi-line commands we are using that’s the same thing but we mostly using this hash for a single line comments if you have the multiple lines we using the multiple lines as a single single line of comment with hash now input and output function so suppose if we have any normal uh function like Y is equal to 3 x + 5 so mathematically equation is there I want to apply some input and output function in this equation so where X will working as a input because when you are passing the different different input like 2 3 4 if I’m passing two so two 3 6 + 5 11 when I passing different different input accordingly you’ll get the output so here let’s take X as a input X I’m taking as a directly 8 Y is equal to 3x + 5 3 3 into x + 5 3 into X so Define the multiplication we are using star symbol x + 5 and I want to display it so for a display for output purpose we are using the print statement print function I’m writing here x so when you run it this one run button or else we have a shortcut is a shift enter when you click this cell you’ll find it out the shift enter run the cell and select below as well I’m using it here shift enter okay shift enter so it will showing the answer is eight now I want to this one as a user input so we can take is a input there is a input function and pass the statement is enter the value of x take a space so this one will taking the proper user input and pass it here 3 into x + 5 and accordingly generate the answer is 8 if I’m passing uh you know two isas here so let me just run it asking the question enter the value of x I’m passing the value is a five I’m expecting that three 5 15 + 5 is a 20 but it’s showing the error reason because this input function by default taking as a string so in saying that cannot can only concatenate with a string to string not to string with integer actually the X variable is taking as a string when I just Che it here type of X that taking as a string and X is taking as a string three is a integer so cannot be multiplied with string and integer so we have another function is int so likewise other data type like int float complex we have a function as well I’m using int open the bracket and close it here when I run it it asking the question enter the value of x I’m passing here five then it will be calculate and showing the answer is five now I want to Define it properly uh like result is five so the result is five is the statement I’ll Define in the double quotes result is and then five will display here so result is is nothing but your double uh statements or string and is your number so we have to separate it with a comma when I run it asking the question enter the value of x 6 so um sorry not to display uh X we have to display y let me run it again enter the value of X4 and accordingly is getting the answer is 17 now this time when I just check the type of X showing the answer is integer not to string okay so where X uh X is taking as a input we took the in function is input function so this is the input and this one is the output okay this one is output So based on this uh input output and uh variables so I Define the exercise so you can easily find it out description in this video and that uh exercise is also available you just run it then you’ll understand with a different different scenario of variables data type and input output function continuing the basics of python last topic operator is remaining let’s finish in and after that we’ll discuss about the next topic is a data type so operator is nothing but the symbol which is responsible to do any operations for example I have a five + 7 so the plus is a responsible to do addition operation so this one we can say it’s operator this one is operator and five and seven is called as a operant operants okay so there is so many symbols is available in the programming language so we categorize in many way so that categorize in such a way so that we can easily remember it so there is a six main operator is remaining the first one is arithmetic operator assignment operator relational operator logical entity and membership operator so one operator is also there is a bewise opor which is not much required for um for pro as a programming perspective if you belong to the any edic field so that time is really important but still I’ll give you the resource where the bewise operator is also available so you just run it you will also understand the beat wise as well but this main operator list try to understand it in a theoretically and practically both the way the first one is arithmetic operator as I said that the symbol are available plus minus the star so I think you guys is already aware about that so the meaning of the plus is nothing but using for addition minus is for subtraction so we can directly use this kind of symbol so only this double star uh modulo that percentage symbol and double slash is something new for you so let’s discuss about that and we’ll also discuss some um you know uh this basics of this operator as well in a very short way let’s jump on a practical implementation like if you’re writing here the 4 + 7 then you’ll get the answer when you just run it you’ll get the answer is 11 so plus is a responsible to do the adding two things adding two numbers right but if in case if you’re using this both the this operator Plus for a string like a hi plus hello which is responsible to do the concatenation that we’ll discuss in the very details in in string topic but plus is responsible to do some operations right I was discussing about you know the modulo operators and uh uh uh and flow division operator so I have created the complete uh you know Jupiter notebook file so you will find it Out Below um below this video you’ll find it out the link you can go through it and just run this file you can see here what is the mean of plus minus everything is there I properly created here you just click here and run it you’ll find it out the answer in case if you find it out any confusion anything you can just put the comment below okay so everything just you have to run it you’ll understand easily this how the symbol is working right so this is the arithmetic operator and uh when you go to the next operator so that we have assign assignment operator assignment in the S assigning the values right if I’m writing the a is equal to 7 the meaning is that 7 is assigning to the a right so but if you’re writing the a is equal to is equal to 7 that is meaning is totally different you are comparing a with seven so here the assignment operator is available like uh uh when you just use a equal to 7 at the same time if you’re using A+ is equal 7 the meaning is that plus is equal to 7 is that a + equal 5 is meaning is that a equal a + 5 so again you will understand very easy way if you’re doing the Practical okay so I’ve created the uh all the assignment here all the arithmetic operator if you’re using is equal to like minus is equal to Star is equal to SL equal you’ll get these values a minus equal 7 is equal to a = to a – 7 right it’s very simple way go back to the Practical so here you’ll find it out the assignment operator as well so like here a is equal to 5 and B is equal to 3 I have assigned it where is that here short shortcut key to run this U jupter notebook shell shift enter so a five is assigning to the A and three is assigning to the B A is equal to a plus b so you’ll getting the answer is a whatever values is of available five + 3 that 8 will be assigning to the a you can see here but B you’re not assigning anything so B will be the original values which is three right this all the values I have just created you can you can perform with all the operation all the combination you’ll find it out in confusion you put the comment you will find it on on a GitHub we will put the comment or uh uh YouTube itself you know comment you can put it I’ll reply you okay I consider that is very easy things because in our school days we already learn this kind of things addition subtraction multiplication so only this module and flow division is something new for you when you just run the jupyter notebook you can understand easily let’s talking about the third operation third operator sorry not operator operation third operator is a relation relational operator somebody is also calling the comparison operator because you are comparing a isal to 5 is there b is equal to 7 is there if you’re writing a greater than b you can also write B is less than a that means you are comparing the answer is always The Logical format what is a logical logical format can be true can be false true or false answer right your answer will always be the true and false concept you comparing like a is less than b a is greater than or equal to B this is the symbol for not equal to this symbol for not equal to this is symbol for equal to equal to in the sense a is equal to 5 is already assigned let me assign some other values uh C is equal to 18 and D is equal to again 7 okay so if I’m writing the C is not equal to a so obviously C is not equal to a right so you can see you’ll get the values is true because C is 18 and a is five which is really not equal to True uh not equal to uh both the values you’ll get the answer is true it’s really not equal okay so this kind of thing is mostly using in the conditional State this name have also one more name is a conditional operator yeah have this one have a three name relational operator comparing comparison operator and the third one is a conditional operator conditional operator okay so we can provide the condition and you’ll get the values is either true or false answer go back to the Jupiter notebook you can just perform the operation like here when I just run it uh result is always in the bullion bull this this value is a bullion okay this values is a bullion logical in the sense logical is a operator where true and false is the value is a bullan values again uh see the next operator is a uh logical operator this concept will be clear okay so a equal to equal to B A is less than greater than when you just run it you will get the either true or false answer why it’s a is H so A and B is not the same that’s why it’s giving the answer is false a is greater than b so the answer is giving the true a is not greater than b okay maybe I didn’t run the this command that’s why yeah it’s taking the previous A and B values you can see here a is not equal to equal to B I wrote it that getting the answer is false where a is greater than b so a is five and B is a 7 that giving the answer is false because five is not greater than 7 okay if is not equal to you can also write the concept like if uh uh a equal to equal to 5 actually the a is a five I assign the values and I’m also writing the five when you just run it you’ll getting the answer is true okay the next operator which I discuss in this uh uh operator as well let me just remove it the next operator is uh yeah okay so next operator is logical operator as I said that this is always giving The Logical format we can say the value is a bullion true and false is a bullion okay logical operator is and or not okay and or not so a b let’s consider as a variable and if your value is a true if a is a true B is a true so A and B become a true so this is the rules which you are following in The Logical operator it is there in uh physics and Mathematics everywhere in a programming language we are not changing anything whatever you learn in engineering or uh 12th so that is the same thing here so A and B if anything let me just remove if anything is a false that will be the false see here true false that value will be the false in case if the value is true and true and false the answer will be the false if any one values is a false and operator will give the values false here is a false that’s why it become false if everywhere is a false like here and here is become a false or operator is a totally opposite if anywhere is true giving the answer is true so here both are true that’s why is true one is true that’s why is it true one is a true that’s why is a true and false both the places have a false that obviously it become a false okay so where when you’re talking about the not operator is a totally opposite false to true true to false go back you can find it out the Practical as well so you can see here I performed the lots of operation here so here it’s uh uh something interesting so here I assigned the values a b c c and a both are a same values here I assigned a is greater than b so which is a buan values that can be true that can be false So currently a is greater than b which is uh obviously uh you know it’s a false because five is not greater than C and B is greater than C with seven is greater than five that is all that is true so false and true that become a false so likewise you can just run it you’ll find it out the values not operator is a totally opposite right so not operator like you getting a false this one become true okay so here sometimes a confusion is that uh we use the this symbol and sometime we use this symbol what is the confusion both are same or both are different okay let’s discuss that so to discuss that so we have to go a little bit in bewise operator as well so as I said that it’s not more important as a programming perspective but you should know about that you don’t need to go in a very details way because the beat wise operator is very very big concept okay so here um we have a two symbol we have a a and b and we have a this one as well we have a o r we have a this one as well we have a not we have this one as well so this one we can say it’s a logical values logical values and this one is a b binary values uh B twice values okay this one I can say logical and this one is say the beat wise values so if you write the values is like this let me just compare it suppose the a value a is uh you’re writing here we already have a ABC right so the a uh through okay a is greater than b which is uh false and B is greater than C which is true then getting the answer is a false but when I just go with that this uh different symbol and symbol this one you also become false I have to get some example where getting the opposite values then I can explain you in Easy Way ah I think this one is the best one 15 is greater than 16 which is a false 5 is less than 8 that’s it true so it here the giving the answer is false because this one is a uh this one is a false and this one is a true so answer become a false so when I just perform the operation is here here is the and symbol and here is the and is giving the different answer what is the reason it’s giving the two different answer as I said that is both are very similar not the same okay so so but if I’m just use the bracket here you’ll get the answer is false what is the difference here so don’t be confused anywhere you can use and anywhere you can use and symbol the only you have to take care about the brackets but why is Valu is changing first I’ll just explain you and after that I’ll go to the next operator okay the value is changed because so you can see here the and operation is directly apply The Logical right this one is taking a true uh this one is taking the false this one is taking the true this one is a true this one is a false but when you’re talking about this one so actually it will be in because of the order wise so preference of this uh uh relationship operator like less than or greater than is a below as compared to this so this one is a bwise operator the actual the 16 and five is uh apply the operation and getting some different number how it’s working I’ll show you okay why it’s not removing okay sorry so you can see here let me just remove first okay you can see here if I’m just perform the operation is a 16 and five you’ll get the different values right 16 and five and when you just perform the operation is a 16 and 5 you’ll get a zero why is it totally different the reason behind that when you just perform the operation the the 16 and five will converting in the binary format so uh there is a proper concept is there like uh the 16 if you want to convert in a binary so uh we have the binary format like uh uh Power of Two only so 2 to the^ 0 2 ^ 1 2 The Power 2 2 The Power 3 like this so this one become a 1 2 4 and uh this one become 8 so if you find it out the value of 16 okay and this one will be 2 to the power uh 4 that will be 2 2 4 2 8 2 16 so when you’re talking about the binary concept so you’ll get the value is somewhere around 1 0 0 0 0 that is a 16 and when you’re talking about a 5 so 4 + 1 become a 16 so 1 0 1 one then it perform the and operator so I’ll not go in a very details way the explanation of how the number is converting the binary and then performing the operation there is a lots of function is available not a lots of Function One function is available to explain this concept okay so when you just use the bin of 16 you’ll get this 1 0 0 0 and when you just use the bin of five you’ll get this and it will perform the operation and and operation 0 and one is become a zero right 0o and one become a zero so when you perform the operation here you’ll get a two different number that is the reason is giving the different values so avoiding this kind of confusion the conclusion is here avoiding this kind of problems we always use a bracket if you’re using this and symbol okay otherwise to understand in very details way I can make a separate video I’ll not go in a very details one here so you can just run is the bwise operator how the number is converting how the value is performing the add operation and or operation okay so here I just giving this some example you can use it I’ll go little bit details here you can just run and you can understand I wrote it just now 4 hours ago you can just use it let me discuss the last two operator which is uh identity operator and one more operator is there which is the membership Operator Let me just remove this one again if you want to know in the very details of uh dewise operator I can make a separate video for this but this is not much required for as a programmer because we already have a function Okay so identity operator is very similar to the equal to and uh uh is not is very similar to not equal to so it identify the values like a is equal to 5 and B is equal to 7 and C is equal to a five again so a is C so it very similar to the equal to equal to it also giving the bullan answer okay G given the bullan answer which is a true and false bullan is nothing but a true and false values that we are calling bullan values membership operator membership operator is in not in okay the value is a particular member variable or not suppose I have one variable which have a list is a 5 7 8 9 so five in a or not like this practically you can easily understand so identity operator is basically working with the memory allocation which is is or is not let’s say for example here I have some values when I just run it so a equal to equal to B which is a five and 7 is getting the answer is false but a is B is also giving the answer is false which is correct a is not equal to B we can also make it similar as a is not equal to B but again this operator is working for the minus5 to 255 range only if you have the values more than that that time this is and not is will be not be similar as uh not equal to and equal to I have this values you can get the values is true 7 and seven but again you have a two different values which is a uh 399 okay n and making equal to equal to You’ll getting the pro but n is M the answer is no the value is different so uh the last video I have explained about ID how the memory allocation is working in a variable concept when you just see the V uh the memory allocation of n will be the different as memory location of M so minus 5 to 55 the range the memory allocation is working the same where is this one is working not same okay same thing here so you can see if the value is this one a uh G is equal to the big statement if the memory location are same then it will be considered as equal to the whole conclusion of this identity operator is that this identity operator working with a memory allocation if the memory loation are same it will be say that is and if not same is not okay you can’t directly uh say that the is is nothing but equal to is not is nothing but a not equal to is and is not working with the memory basis and equal to not equal to working with a number basis value basis the last one is a membership operator as I disc as I said it’s uh the particular value is a part of the member or not so in and in not so here I have a big list and checking that a five the particular member or not so saying that yes it’s member but 77 no so but I wrote the statement is not in see the Python programming language is very similar to the English language right so the whatever operator whatever uh statement you writing is will be it’s uh you know the state have some sense it’s not like that whatever here is writing the in here is a not not like that it’s a proper statement in not uh sorry not in okay in not is not make sense but not in have a sense is it true and so likewise I also perform some operation like um the t u a i displayed in the you know the sequence wise here I will not explain that why is display in the sequence wise it’s a for Loop okay so the coming video you can easily find it out how the for Loop is working in the Practical and theoretical way with the flowchart I’ll go in a very very details way okay so I request you please run this all the operators uh all the operator I’ll share with you these files on a GitHub you just download it and run it we entered in a fourth module which is a data type so we already discussed the last uh we already discussed the data type in the last video so I think this PP you already aware about that because in last video the overview of python I already discussed right so what is integer what is a complex what is a float so the data so many data type is there especially in a python so you’ll find it out some some new word like the set set string list tle and dictionary is also there just not there in C+ plus Java and C language so which is something new for you so in this uh from from this uh modules so we will discuss in the very details of the data type right in the last video we just give the overview so here we have to enter in the depth of the data type and see the data type is very very important concept for a interview perspective because 99% probability that the interviewer will ask the question related to a data type actually this data type is using in almost every places if you are making any application with a Python programming language uh this data type like um uh it’s easy to implement it’s easy to understand but you can easily forget as well the reason because this small small topic is there okay it’s not much difficult we’ll learn it one by one and we’ll make the proper structured way because of the lots of data types so python is creating the data type uh with two different categor the first one is immutable data type the second one is a mutable data type so which one will consider as a immutable data type like a numeric numeric data type numeric data type will consider in a immutable string we also have and we have a tle okay and mutable we have a list we have a dictionary we have a set so mainly three is a main data type is there so numeric is also one of the category we are not saying that numeric is a data type so in a numeric we have uh mainly the integer float and complex so let’s understand it all the data type is one by way this completely a six data type is there three 4 5 five and six okay let’s understand the first one is a numeric data type so numeric data type you can understand is like a numbering concept which is learned in a 10th class or e8th class that’s normal numbering concept numeric data type data type I think you aware about the real number right so real let’s start with the real number a number which is really in exist in the real life we can we can say it’s a real number it’s a very simple definition of the real number right that number can be uh start with minus infinite to any number like uh uh minus 2 you can say uh – 1.01 – 1.005 like any number which is exist in the real life and zero is also considered that and one and 1.5 any number till infinite so inside a real number we have a different different uh subset is like a whole number integer number currently I’m in a you know numbering concept I didn’t enter in the programming concept okay let’s first understand with the numbering concept and after that we’ll relate with all the topic with the programming language so that we have a real number okay so inside a real number we have also the different different numbering concept is there the first one is integer okay so integer integer is basically there is no any decimal point is there like it start from minus infinite 2 it will take a minus 2 and after that minus one okay and after that zero then 1 2 till infinite there is no any decimal point and we have also the whole number is also there natural number let me just first Define natural number natural number is basically start with 1 2 3 4 till infinite that is a positive values is defined as a natural number why it’s saying that the positive values is defined as a natural number because uh you know in in a naturally we are using the only positive things right so you can relate like this this is is not a definition okay third one is a whole number whole number which is start from 0 to infinite this is the normal numbering concept I think you learn in e8th class or seventh class okay so and those number which is which cannot be defined in a real life we are calling the complex number the simple way we can say like if you have a number is like a root of five some values is there right 2.2 3 is there I don’t remember the exact number uh 2.2 something is there right you’ll get the some values but when you’re talking about uh uh the value of minus5 root of minus5 that cannot be Define so that we are defining is like root of minus1 into root of 5 so root of minus1 we are calling the Iota in a mathematics which is defined with the I and root of 5 whatever value is there so in suppose if I’m considering 2.2 something 2.23 so 2.23 I that will be the values so that we are calling is a complex number okay complex number so when you enter in the real life def uh this is the real life numbering concept we are using using but when you’re talking about the uh programming perspective so we have a only integer float and complex okay so we have int so whatever we learn in integer that is nothing but in uh that we defining here the in we have a float like if any values minus1 min-2 sorry min-2 will be the less – 2 -1 0 1 2 like this till infinite okay so floating point that can be any decimal points so like a minus U it is also start from uh you know infinite and if you have a minus two that will be considered as an integer but if you have- 2.0 that will be considered as a flow any value which we have some decimal point we are calling the floating Point number okay so minus 1.02 so kind of that values the complex number complex number how we are defining complex number like in a real life we are writing the a + I but here we are defining the any number 5 + 7 G we are not using the I okay uh that can be any number like 5 + 7 I so any number which is defining the uh Iota concept we are calling the complex number let’s try to understand with some practical implementation okay suppose if you have any number like a five if you have any number is like uh uh 5. 86 any number which have some decimal points we are calling the float if you have uh numbers like um uh 5 + 7 G which I which I said okay so when you just take the data type so we can just type here the type A you’ll get the values as integer I and T and A type of B and you just check it that is a float type of C which is a complex to understand the very details way uh so I already created one files the data type you can find it out um on a GitHub link okay so I’ll share with this file so you’ll find it out like which one is integer float and complex and as well as uh you’ll find it the type Cur one conversion as well like if I want to convert float to integer integer to float float to concept so how can we do that we Define it here okay so hope you understand the concept of uh uh in numeric data type where mainly we are using the three data type here in float and complex let’s jump on the next data type which is a which is a string data type okay so string data type is uh uh is mostly using in a data science perspective as well because let’s let me give you one example is a chat GPT I think you guys is already aware about that chat GPT so chat GPT is a totally based on the natural language processing concept it’s little bit a high level of a data science but let me just give you the um overview why the string is very very important see the chat GPT is always needed the string Command right statements you you’re writing some statement and accordingly it will be finding the pattern and giving the result to you and if I’m talking with you so that is The Voice command the voice command first is converting in a text and after that you’ll perform some operations so wherever the text is available we are apply the string command so that is the reason string become very very important because it can be easily connected with real life because the real data uh text is also there so let’s discuss the string data type okay string so first of of all string is a immutable data type immutable data type and ordered data type so like in um interview perspective the interviewer is asking that what is a string string is immutable and ordered data type which is very very important to say that this order data type okay some data type is unordered as well so I’ll discuss that like U dictionary and set that is the unordered so here what is the mean of uh immutable immutable in the sense we can’t change anything if you define the value is like I give I take the variable a is equal to India first first of all let me discuss that how to define the string string is always defined with the single quotes like I’m writing here the end single quotes we can also Define with double quotes as well make sure that if you started with a single quotes end with a single quotes if you started with a double quotes end with a double codes and uh if you have a multi-line of statement and string is there so we can also Define that with uh triple codes one line two line three line wherever ever you end 1 2 3 so same as for uh single quotes as well 1 2 3 one line two line three line so you can increase the line two three okay so that we are defining the string and string have uh uh as I said that it’s immutable that means you can’t change the values in between why I’m saying this word because of the ordering concept so this end if I Define it here if I Define it here i n d i a so where we have a positive and negative indexes available so index is always start with a zero in a Python programming language this uh concept is not for one string everywhere in the python the value is always start with a zero so the value started here here 0 1 2 3 4 so that is the index and when you’re talking about the negative index that is always start with a minus one because there is no minus 0 right so minus one that is starting with the right hand side min-1 -2 – 3 – 4 – 5 it’s five okay so you can access the values as well so those data type have the positive and negative indexes there that we are calling the ordered data type okay so string have the positive and negative Index right so here because of the this ordering concept we have a different different operations as well so we can easily slice it so in case if I’m just printing let’s do it in a practical way okay so string I have one string which is the India okay so because of this uh ordering concept so we can also slice it as well so a square bracket to using up slice always using the square bracket as I said that the index is always start with a zero so if I’m writing the zero in between the square bracket you’ll get the Valu is I right if I’m writing the a of uh uh three a of three so let me just write here a of three so which is the small i 0 1 2 3 okay so in case if I’m writing the minus two so min-1 -2 so you’ll also get the values the same minus one uh sorry min-2 that you’ll get the same values min-2 right so the index of U the value of index 3 is I the value of index minus – 2 is also I right the same values because we have a positive and negative index as well so in case if I want to assign some values if in a of two which is a B I want to make it a capital letter can I do that capital D it will giving you the error so if it Define the particular order you can’t change it if you can change it that’s become the mutable if I change the order if I change the values in between that become IM mutable so obviously um in Python everything is possible if you want to change it it’s possible in a different way but the string is not giving you any kinds of method to change the values directly you can’t assign directly here okay that is the reason is a immutable immutable inmutable means in the sense non-changeable mutable means changeable all right okay so based on that we have a different different operation is available we have a concatenation suppose India and uh I have a um B statement which is the country country okay so the a plus b if I’m performing the operation I’ll get the Valu is India country okay India country in in the in case if you want to Define some space in between so a plus you can provide the space the plus operation is used for concatenation plus operation is used for concatenation if I’m using so India space country perform the operation like this so let’s go to discuss about uh method and function in a string uh string data type so let me just conclude what I discussed okay so I discussed this slicing I disclose the concatenation and we have also one more thing is a repetition okay we can also repeat it with star symbol slicing we are using the square bracket concatenation we are using this uh Plus and reputation we can use the star so for example I have the value is uh variable is a I want to multiply with a five then you’ll getting India India India five times so that is a reputation okay a small small thing is there to go in a very details way again you can go to the data type if you already aware about these things so you can skip or you can just make this video is a 2X or 1.x faster and just complete it and go through this uh Jupiter notebook and run this file you’ll get a better idea but don’t forget to run this uh uh jupter notebook file okay so here I already explained okay so you can just run it just shift enter shift enter run it you’ll and you’ll understand each and everything but again in case if you make any doubts if you not understand anything you can just put your question on a comment definitely I’ll reply you okay so let’s discuss about uh okay I need a space all right I need a space keep because I don’t want to remove it okay so let’s discuss about the next thing is a function and method which is very very important every data type have a different different method and function okay let me first write a method and some function so actually the method and function both are a same there is no any difference this have a very small differences there if function is always defining like a def here I’m not explaining you the details of the function whatever in build function whatever in build method is available in a string that I will discuss it here okay so why the both are a difference see method is always calling with a DOT like I have suppose a is a variable a is a variable which is the India okay so method is always using like a Dot Upper a do lower a do capitalize okay a DOT count a do index small I there is no Capital index so other uh method is also available but when you’re talking about um function so function is like a len and we are passing the values a len means length we have um uh mean we have a mix okay so these are and we have a type so actually this uh uh function is a very common for for all the data type so when you just enter in the list tole dictionary so you’ll also find it out the same type of function that will not change but the method will always change the difference between the uh function and method is that if function is always defining like this and then function name f n if I’m writing defining like this if the function is separately available we are calling function simple but if the function is available inside a class we are calling the method class class name is a okay if it’s available inside a class we are calling method so when I start the objectoriented programming I’ll go in a very details way just now you can just understand like this the method is always using with a DOT and a function we directly call it and to know that which one is a function you can directly understand like if you find it out any statement after this bracket that is a function that can be either function or method if you’re using dot method not using function okay so few method I will discuss it here and uh after that I’ll stop okay because uh no need to explain each and every method when you just run this uter notebook you can easily understand okay so as I uh Define all two variables a which is India B which is a country and I want to make it a Dot Upper so that will make it is a upper case okay it will be make as a upper case a do lower that will make it a lower case small I and uh currently the a variable is already the capitalized capitalize in the sense first letter is a capital the second letter uh remaining letter is a small one let me make one variable is equal to I’m using Python programming language okay so let me just use small I when you just use a c do capitalize okay sometimes uh when you just forget your uh you know that method spell when you make it spellings little bit wrong so it will giving you the error so the best practice is that after this variable put a dot put a tab you’ll getting a different different suggestions I’ll make a capitalize I don’t need to write everything capitalize is already there okay when I just run it you’ll get the values I’m using Python programming language so the first L make it as a capital the rest of the later will make as a small one so C do title there is one more valuable uh one more method is a title everything makees a first uh letter is a the capital of the particular word okay so again to go in a details way so you have this one string data type which is immutable and ordered so you can find out here all the different different uh method and description is also there it’s very understandable way so when you just run it shift tab shift um shift T shift enter when you just run it you can easily understand so again I’m also there in case if if you face any problem okay so these all the options are available till here okay so suppose I have a statement um Rahul got 78% marks in mathematics exam okay so this is the one statement I want to make this statement and dynamic way like uh the name which is a Rahul okay and marks I’ll make a m variable which is 78 78 which is the integer and U subject which nothing but a mathematics okay this three variables I want to make a statement in the dynamic way so making the statement Dynamic may we have a three way to define it let’s discuss the first one string formatting uh the first one is we can print here so the first one is Rahul so Rahul is a subject so I’ll make the name is a string so I’ll make percentage of s okay that percentage of s I’ll make the dynamic way uh Rahul got 78% marks so percentage of s percentage of I I for integer marks in mathematics exam which is the percentage of s s for a string and I for integer percentage of s exam okay and after that after a double quotes you have to use a percentage and in a bracket you have to pass the values 1 by one the first one is a percentage of s which is nothing but a name I’ll pass it the name is here second one is a m which is nothing but a marks third one is a subject I’ll pass the subject here the name will be passed in the percentage of s m will percentage of I which is a integer and subject is uh that mathematics so here showing the 78 so the best part of is that in case if you have some decimal uh numbers is here like let’s say for example you have a 78.6 7 um when you just run it till you’ll get the answer is let me just run it here again still you’ll get the answer is 78 the reason behind that because I pass the values is the integer right so in case if you want the proper decimal point you have to make is a float you have to make as a float so by default that floating point is giving the uh till of five decimal point yeah six decimal points so it’s on you you can easily U you know dynamically you can just change it I can pass it here only the two decimal point so 0.2 when you just pass it you’ll get the values is uh 78.6 7 in case if you’re passing the one decimal point so 67 it will be convert into seven because after six the seven values is greater than uh 0.5 that will be uh round of seven okay but here the percentage symbol is not visible so when you just pass the one percentage it will be show through the error because unsupported format uh because here the considering that if you pass the 1 percentage the meaning is you will pass some um you know the data type here as well that can be string that can be float that can be integer so avoiding this confusion we always using the double percentage so that will consider as a percentage okay so one percentage will consider as as a like you will pass some data type but actually you don’t need to pass some data type right so double percentage you can use it so here uh uh like uh s we are calling string I we are calling the integer so again in the data type already the values is available you can just make it string integer float character everything is there okay so this is the one way to defining this string formatting there is also one way is also there to defining the string formatting you will just write a print and uh whatever values is available like Rahul so he’ll make double quotes always there because of the spring uh this one curly brasses ra you will consider as a dynamic values Rahul got curly brasses percentage marks in cly bres exam okay so here the curly presses you have to pass the values here like I will pass the value is uh name I will pass the value is uh M I will pass the value is subject just before you have to write F this is also the very simple way but again if you make the statement is a very Dynamic especially in the number format so this one is also useful you can also use this one one also so both are very same you can use it sorry we have to give this space and yeah full stop so again everything you can find it out here uh you you can easily find it out here uh one more thing one more is also there uh dot format option okay so you can just use it the same thing here curly brasses got Rahul got curly brasses exam uh cly Braes marks in cly bres exam okay and after that just do format and you’ll pass the values is one by one okay so here when I just pass the value is um uh name first what will be consider and then M then subject again so okay s j c so again you will be confused that what is the difference between in that okay this option is available then why I will use this so this one also have some uh plus point is that suppose um here you have to pass the proper order otherwise the value will be swapped so but in case if you pass the different order I’ll pass m is here I pass name is here I’ll pass a subject is here itself when I just run it you’ll find it out the value is 78 got Rahul percentage it’s very weird statement right so actually the by default uh this one uh the indexing point is also there 0 one and two so when I just run it so by default the values is there but actually this uh the zero index it should be it should be here so I’ll make it zero here okay so you can see you’ll find it the two times because I also pass the zero is here as well so here I’ll make the one so you can change the order as well so all three have a different different positive points uh we can use it okay so one more topic is also there in uh string formatting so uh string formatting is done in a string there is also one more topic it’s very small topic is um skip sequence and uh raw string skip sequence and raw string let me put the heading here skip sequence and raw string okay skip sequence in the sense suppose if you are uh writing some statement and the value will not be print in the place of that uh uh that symbol it will be showing the meaningful result let’s say for example I’m writing here I um working in Python programming language okay which is uh dynamically type language okay I just want some statement after the uh High I want to print in next line I’ll use the slash n so sln is basically going to the next line right it’s not like that it will be print the slash okay and after the statement again I want to in this next line it will be in the next line I want to uh dynamically typed language have some uh space as well SLB B for backspace okay B for back I’ll make the t t for a tab okay so when you just run it you’ll get the values is because of the sln is giving the next line because of this sln is giving the again next line because of this SLT is giving the again some spaces here so that’s why it’s giving the uh uh that meaningful result not exactly is the printing the values right so this one we are calling the skep sequence so I just uh Define it here a different different type of skip sequence like slash n for next line/ t for tab SLB for back space SLR for reserved and uh Slash a for alert so different different skip sequences there sorry it’s a skip sequence okay skip sequence and what is a raw string so raw string in the sense sorry draw string in the sense if if uh you want all the statements as it is I don’t want to print the meaningful result I want as it is values so just before you have to write r that will consider everything as a raw string so the value is not displaying it here sometimes it’s very useful whenever you’re reading some particular files from local machine or in particular server okay suppose if you are reading and uh somewhere the double slash is available single slash is available so python is giving some meaningful result but you don’t want you want as it is uh path so that time we are using the r especially for uh reading the CSV file in the data science we mostly using the r to avoiding the errors okay so that string topic is finished so we covered everything we covered uh four thing here the first one we discuss about uh what is the String which is the immutable and Order data type and uh we discuss about the positive and negative indexing that can be easily slice and uh concatenate and repeating values and uh we also discuss the method function and we discuss the string formatting skip sequence and Ross string so that complete the details again if you have any confusion you can just put on a comment and uh please refer that um uh jupter notebook file your confusion will be cleared there okay so let’s discuss about the next topic which is a tle so tle is one of the smallest data type like uh uh like a number number is very small that was only uh you know uh the integer float and complex but the tle have some values but it’s not as much of uh big topic so let me just cover it in this video only okay so again the tle so first we have to clear that what is the definition the Supple is again it’s a immutable and ordered data type ordered data type okay when you’re talking about the how the tle is defining the tle is defining in a round bracket suppose I have a value is a five a 8 7.0 and high okay so that I store in the variable T so uh uh first of all this tle is a comes under the sequence sequence means is a particularly order and that collecting the data right so the five is a integer 7.0 is a float high is a string so that is a that’s why the tole is become a uh sequence which store the different different data type even inside a tle you can also store the list as well dictionary as well anything so the tle list dictionary in the set that working as a container so the container is nothing but a sequence okay so here as I said it’s the ordered data type so again have a positive and negative indexing is there okay sorry uh oh why it’s showing like this okay have a positive and negative indexing so 0 1 2 3 have a positive indexing and as well as we have a negative indexing as well -1 – 2 – 3 – 4 so like the tle we can also apply the uh slicing order like uh the T slice with uh uh two you’ll get some values obviously you’ll get the value is 7.0 right you’ll get the value is 7.0 in in case if you pass the T of three you’ll get the high but in case if you pass the minus 3 then the value will change that will be 8 right so we can also apply the concatenation here we can also apply the multiplication here so this symbol is used for concatenation and this symbol is used for repetion okay so and uh the likewise the uh string we have also the method and function are available so the best part of this uh tle is that not a best part but uh yeah uh the tle have a very less method when you’re talking about a method have very less and a function okay so method have very less just we have a count count method like T do count and T do index that’s it we don’t have other method is here so again is a tle uh again is a immutable so that means we can’t change the values in between and uh the function when you’re talking about so it will be the same as the which we discussed in the string that like U uh what is the max values what is the mean values what is the um a type okay there is also one more function is available which is a tle which will be responsible to convert any data type into tle okay okay so mean Max is there yeah and we have a len which is very very important Len which will show you how much uh data is available inside a tuple so let me just clear it in the practical way it’s very uh small data type we can see so again you have to refer the data type Jupiter notebook you’ll find it out the practice uh um notes there okay so tole I can Define it here in the round bracket I’ll make this the T1 be Define in the round bracket 47 7.9 and um uh high is here we can Define it and he we can Define it okay so let me also Define one more variable is a T2 where I can Define it 4 5 6 2 there is very small values so the T1 if you access it uh access it with the two so the value 0 1 2 which is a 7.9 you value will generate sorry it’s a T1 not a t 7.9 value will be generate but again I want a 7.9 High both the values so you will start with two you will start with two and end with high start with two and reach till three so the p have a rules that never end with last values so if you’ll write three that it will be considered as a range the 2 to three it will start with a true but not end with three okay so it will showing the value is 7.9 so you have to write here the four this rules is not for only tle it’s rule for everywhere in the python always start with the index always start with zero first thing second thing never end with that values always ending with before one because of that let me write it here always start with index zero and in case if you pass the range is uh 9 meaning is that the value will start from 0 to 8 okay so here when you just pass the two colon 4 the meaning is that prob with two and three like this so this will be applicable for everywhere in the python not for a tle only okay even the last data type which is discussed that was a string it’s also applicable for them also okay so we can also apply the concatenation T1 + T2 you’ll get the value is the complete concatenation we can also apply the uh repetition as well so if I’ll multiply with the three then you’ll get the three times that same values okay so these are the values are available and uh uh we have a different different method as well before going to that method let me just uh do the uh negative indexing and some more values try to get it suppose I want this both the values uh 6 7.9 and high from negative indexing so T1 this is the values I want to start with 6 so that I will take the value is -1 – 2 – 3 -4 so I’ll write write the minus 4 here and when you just print it you’ll get the Valu is minus uh you’ll get the values is six and when you click only colon then it will be start with a uh minus 4 till last okay but I don’t want a high so this the value is minus1 and I want to end with here so when I just write a minus 2 min-2 so you know that it never end with the last values is always always ended before one right is before one so definitely it will end here you can run it and check that so better do you have to use minus one minus one in the sense it end with min-2 this is the rule of python is that never end with last values and always start with zero if you are not defining and the values is always start with the same same if you are defined this is the way and one more thing suppose I want to make this order as a reverse how we can do that so the E1 you can make it and when you make a colon that meaning is that start with the zeroth index end with last okay and after that suppose uh okay after colon you can also increment and decrement so by default start with a zero 1 2 3 in case if I’m writing the two so it will be jump like this 0 2 4 and I just run it you’ll get the values like this but if you make it minus one okay the first place become the minus one then again it will be decrementing so it will make the reverse order okay oh sorry T1 the T1 values of this and you can make is the reverse order all right here if uh if I’m applying the method and function the P1 dot count okay count you can just count it how how many times that values is available I want to know that how many times high is available will tell you yes it’s a one time is available right so in case if you have a multiple times high is there like uh uh T2 how much is there okay so let me make a T3 is equal to T T1 + T2 T1 + T2 so this one P3 I want to perform some operation how many times four is available let’s make it p 3 dot count and make it four you’ll get the values is two okay so again the t uh when you apply the index order dot index index will tell you where is exactly the position suppose I want to know the position of 7.9 7.9 it will give you the value is sorry it’s a T3 give you the value is 2 0 1 2 but the confusing part is here confusing part is here if I want to know the position of six P3 dot index of 6 what will be the values it’s one but I want to know that what is the position of this if you’re walking in the Jupiter notebook click on it here and inside a bracket just write shift tab you’ll get the complete notification okay so it will tell you where the position are starting and where is ending so ending is a huge number and starting is a zero so always start with a zero but if I’m start traveling from the one uh sorry 0 1 2 if I’m traveling from the two so that means it will not check the previous one so here if I start a start T start is equal to two if I’m writing so that means the the two is here it will check only these part so what is the position of six in this part okay start s t take no keyword argument okay let’s remove it so you don’t need to pass the uh keyword there okay so when you just pass the two here the meaning is that it will be start from two only from here it will not check the previous one so this way we can find it out a last thing uh I didn’t uh discuss that why it’s called as a immutable because I have the D1 in case in the place of 7.9 which is the second position I want to replace with some values like U hello so you can’t do it that is not supporting in the tle so tle is mostly used for a security purpose uh the value when you’re defining in uh any any metadata or we can say any storage place we always using the round bracket so that when you start working on a python it should not be changed by mistake to assigning some values okay so now the tle topic is finished this is uh the topic is there in a tle so if you have any confusion you can put on a command we completed the three data type numeric string and tle so now let’s discuss about the next topic is a list that is the fourth data type list so list basically a a mutable data type so whatever we discussed the previous one is a tle string and numeric that was a immutable data type but this one is a mutable data type mutable and ordered data type again so mutable means changeable ordered means have a positive and negative indexing so list is always defining with this is LS is a variable defining with a square bracket that can be the any data type you can just write it like integer 5.8 is a float High okay and uh 7 five so you can Define like this so it’s a square bracket all right so have a positive and negative indexing as well because of it order so we have a 0 1 2 sorry three and four I have a NE negative indexing as well minus1 -2 – 3 -4 – 5 so when you’re talking about the operations so it have also the same operation like a tle and string so we can perform the operation like uh slicing so like LS of for the particular slice if you do it uh like one so you’ll get the answer is 5.8 so if you the slice is the multiple values um like uh 2 to 5 so you’ll get the answer is high so that will be the single quotes here High 7 and five that is the more than one values is always in the square bracket okay so we have also the uh uh this symbol plus symbol which is denoting as a concatenation concatenation okay and uh the star symbol which denoting as a repetion okay that will repeat the Valu in the many times and uh the same as um same as the tle we have also this method and function are available but because of this mutability because of the changeability we can change the values so we have so many method and functions are available so here I will discuss a few method and function and later you can just run the Jupiter notebook file you’ll understand easily so method and function our function is the same that will be Len that will be mean that will be Max that will be uh list list also the function okay and uh that will be the type okay this function very common we we we are using in uh every data type so only this one like for a list we have a list function for tle we have a tle function string we have a s Str function okay so method when you’re talking about so method is um uh we have uh like U we have a DOT count okay we have a um we have a index that that is a common it’s there for a tle and uh in string as well and we have also the other method is like append which is very famous append insert okay so many method are available so again when you just run the jupyter notebook you’ll find it out all the methods there so few method I will discuss here and I’ll discuss in some practical implementation here okay you’ll find it out in the Jupiter notebook each and everything okay as I implemented almost every method here but still if you’ll get any confusion I’m there so let me create one uh heading is a list okay so LS is a variable I’m making some values here 6 comma 3 comma 8 comma 4.7 comma uh H comma K okay so if I want to change the value is H to hello so what is the position of H which is the 0 uh 0 1 2 3 4 that is the fourth position so fourth I want to which is nothing but a h i want to make as a hello hello and later when I just check the values is LS so yes I can see the value is hello so it’s very simple we can just do it right we can just up change the values because of it’s a mutable data type we can easily change the values which is defined inside that right so here H is changed so whatever index is defining for H so in the place of H is showing the hello but you can’t do it in a tle okay so uh we can also uh slice it many things like LS of four what is the values is a hello LS of U uh 2 which is the8 from the eight I want to Define with the last values so you can see the last value is this one but the same time if I want to print till a f only five only so it’s saying that it’s LS not s saying that 8 4.7 and hello right 8 4.7 and hello it reach till four only right so it will take two it will take three it will take four it will not reach till fifth values okay so we can also perform the operation like a concatenation repetion as well I have LS2 which the list is containing the 742 and when I just perform the ls plus LS2 so you’ll get the value is is combining the all right so adding the all the values is here so concatenation we can perform we can perform the repetion as well it’s repeting the values right we can perform the functions as well here all the functions in LS I want to know that what is the length LS so you’ll get the values is six there is a six elements are available so uh inside the list tle dictionary and uh set so that values we are calling the elements right this elements are available there is a six elements and uh when I just perform the operation of minan of Ls so you’ll get the error because these all are a number but the hello and KY is a in these both are a string so string cannot be compared uh to each other right less than greater than symbol is not supported between the string and integer so what we can do it here so we can just remove it or else you can just take the any other like other variables so here when I just check that what is the minimum values in LS2 so which is the two and LS uh LS2 what is the uh maximum values what is the maximum values so you will get the values is seven so we can find it out this way but again so when I just uh competiting with the tle and list for example you have a tle I already Define I think let me just use H T1 tle I want to change the values Pi I want to make is a he uh not a he I want to make as a uh hello okay so we can directly convert this tle into the list so when I just make the list of T1 so it will make the complete list but in a list we can perform the operation right so I can store in the temporary variable temp variable I store it you can store in any variable obviously temp I store it and I just pass the value is the position of high which is a 0 1 2 3 so when I I just pass the value is three you’ll get the answer is high and when I just perform the assigning the values perform the this assignment operation hello here and check the value syst temp you’ll get the hello later again you’ll perform the operation like a tle you’ll convert your list into the tle so make the values this okay so we have this option like uh the the list we have a list function tle we have a tle function so because of that we can change the values right so lots of method is also there that we discuss the um function so method is also there for example I have a list LS I want to add some values so ls. pend we can add some values here suppose 400 I want to add it I can add it here LS the 400 is added but at the same time I want to add in the position of the two or we can say just beside the eight so you have to find it out the position where you want to add it 0 1 2 3 in the position of three I want to add it so upend is always adding in the last but when I just use the ls do insert so this is also one of the function insert so you can what what you want to add you can just take the help from Jupiter notebook shift tab it will tell you where you want to add sorry first where you want to add index is where you want to add I want to add in the position of three what you want to add I want to add a th000 more so when I just run it and check the values LS th000 is adding in the position of three okay so the 4.7 is just transfer in the one position so that means it’s a mutable right the position is changing it here but when you’re talking about the list and a when you’re talking about the tle and a string that cannot change the positions okay because that is a immutable data type insert we can perform we can perform the uh deletion operation so here uh append and insert we mostly use for adding the values if I want to perform deletion operation so that remove operation here I want to remove hello we can directly remove it here and check the values as LS so value is removed so that value is removed from the uh from the values we I just passed the values here but I want to pass the index so ls. pop is there pop is taking the index I will pass the 0 1 2 3 4 5 I’ll pass the index is five the pass the index is five then it will be removed the uh K values take the ls that is remove the key and here you can perform the main Max right there is no any string values is available you can perform it Max obviously it’s a th000 so you can perform here okay so likewise we have lots of method is available remove is there pop is there okay so other method is there you can just check it in uh data type see sorting method is there if applying the ascending order desc ascending order right we can apply the clear clear will remove the all the values there you just run it the shift tab shift Tab run it you’ll understand everything it’s very simple way so much method is there again you don’t need to remember each and everything but you should know that that option is available in a list okay you don’t need to remember because a very small small thing is there okay so in a list there is one topic is very uh famous which is the list comprehension list comprehension so this list comprehension is mostly used for optimizing the code so whatever code you can write in a three four lines because of the list comprehension you can make it to one line and it’s very fast okay so suppose I want to display the 1 to 10 number it’s is very simple example 1 to 10 number so with the help of for loop I can display it here for I in range I can pass the values is here one to uh 11 so this is the rule of Python programming language is that is always starting with that particular values and if you’re not assigning that will be considered a zero and never end with last values okay because the range of 1 to 10 1 to 11 the 10 values should be printed so another programming language why it’s not like that because their indexing start from one and here indexing start from zero so when I print I so it will showing you the 1 to 10 number okay I want to perform the operation based on that I want to perform like U make a container of the uh uh you know the cube of all the values so when I just make it Cube because when you have the particular list and perform the uh I want to store in okay like that suppose if you have the particular list I want to make it all the values as a QBE format but in that time when you just directly perform the operation like um suppose I have a LS and when I just performing a q to the power of three it will show it saying that it’s not supported so in that situation what should I do so we can directly uh you know display all the values display all the values with a for loop it’s iterating all the values is one by one what is happening here it’s printing all the values one by one right so like here LS is this LS is this I want to perform the cube operation based on that so can you do that no it’s not possible when you just apply it here Cube it will giving you the error so likewise we have the one one of the requirement is that I want to make a list of the cube of 1 to 10 so here when I just make it cube of 1 to 10 so the displaying all the values I’ll make one particular uh uh list like LS three is a blank list and every time I’ll just append it append with LS3 sorry not LS3 I I will append it uh let make it a result okay here I’m just using the for Loop but after completing up this data type I have to go in the very details of conditional statement and loop so again if you have a confusion if you if you are okay with that then you can just listen to it otherwise you can just pick the video of uh the for loop as well I have already created not already created after that you’ll get the a for Loop okay soend why it’s not working okay I have to write ls. up okay so when I just check the ls you’ll get the LS3 LS3 not LS LS3 you’ll get the values 1 8 2 7 like this so but with the help of list comprehension you can easily do it just one line of code list comprehens some syntaxes like that so so you’ll apply the for Loop for variable in sequence so before just we have to write variable okay this is the sequ this is the uh syntax of the list comprehensive so what I’ll do I will just use use the for I in range which is a uh because range is a sequence range which is the 1 to 11 and before that whatever want to print you can I I’m just printing the only I so that showing the 1 to 10 number but I want to print the cube so it will showing the cube okay so as I said that in the ls I want to make it the cube directly I can’t do it but if I apply the list comprehension there for uh B in LS3 so before when I just pass the One V you will get the as ittis values because it’s a iterate the all the values is 1 by one right it’s iterate so you have a freedom to do anything with a V so I I’m applying it here the three you’ll get the values is completely the cube of all the values right so here LS3 previously was like this uh not a LS3 let’s perform the ls you can see here previously the ls was like this let apply the cube in all the values is one by one so we with the help of list comprehension we can perform this operation and in real life in the real application we are using this one so based on the list comprehensive I’ve just given some exercise you can just try it it’s very easy I also given the solution as well here you can just try to understand and do it okay so each and everything I just apply it here you can check out and just run it one by one you’ll understand everything here okay so I also created the 2D and a 3D list right so 2D and 3D list let me just explain a little bit 2D and 3D list 2D and 3D list okay so L if you have a list like this that means it’s a one day list okay it’s a one day onedimensional only if you have LS2 is like uh a particular list is there the list inside a list so list inside a list like 4 6 8 comma in the second list we have 3 5 7 like this so that means there is a two rows there’s two rows and three columns so that means it’s a 2 cross three Matrix is there right so when you have the same kind of with a three dimensional as well like uh LS3 so this is the particular one di uh it’s a 2d list I’ll make the 2D list in the same time I will also make it this 2D list in a one particular wrapper so let me change the values like a 15 16 18 13 15 and 17 and everything I’ll make one particular rapper then it’s become a 3D list right in a 3D list that we can say that it’s a this was the two rows three columns and I apply the one more D there right which is the two so 2 cross 2 cross 3 the value will be 2 cross 2 cross 3 it will be right we can if you want to iterate it the for Loop will really help you so it like if I want to slice the values LS uh LS3 this is the values I want to change in the position of 16 okay I want to change the position of 16 first you have to find it out so which uh uh Block it’s available this is the F this is the first block this is the second block so available the second block that means 0 1 so I find it out this block and again it’s a which block it’s uh because I want to change the value is 15 right again is a which block is a zero Block it’s a one block it’s one again what is the position it’s a one I want to change the values is a I change the values when I just take the ls three obviously LS3 get the values is high so this way we are performing the list operation and we can also iterate with the for Loop like for I in LS3 so it will be iterate the one list will be iterated here one bracket will be trated here like print I you will get the values in one list one list and the second list right so again I I want to iterate it so for G in I print G so when I just display so you’ll get the value is G iteration so again I can iterate it the uh with a G as well for K in J so then you’ll get the value is a okay so you’ll get the all the values so if you have a three dimension you have to apply the three for Loop to read each and every element okay so this is the list uh 3D 3D I also completed and list comprehens I also completed all the methods and function as well please check out this Jupiter notebook your all the doubts will be clear now we reach till fifth data type which is a dictionary so dictionary is a little bit different with tle and list what is the different different is that there is no any positive and negative indexing is there in the dictionary that means dictionary is unordered data type but it’s a mutable data type if it’s a mutable let me just write it first it’s a mutable and ordered data type sorry unordered data type unordered data type okay so there is no any positive indexing for a dictionary no any positive and negative indexing so dictionary is a defining like key value pair with curly brasses key colon values okay Curly brasses close so if I want to Define any dictionary so we can Define the dictionaries like this so suppose the variable is a D in a curly bres I’m defining the value is a colon 15 comma B colon 18 comma Zed colon 19 again I told you there is no any restriction for a data type if you started with any string is here and I want to put it here is a integer you can do it there is no restriction any data type as I said in a tle uh topic itself right so likewise the tle this is also one of the container but it’s contain the data as key value paay so likewise uh couple and list so we’ll also discuss about the operations their method and function so when you’re talking about the operations so we can also slice the values but with a key because there is no any 0o 1 1 2 3 there is no any positive and negative indexing is available right so we can’t directly access it suppose if I want to access the value is a 15 so I will write D of a okay we’ll write a d of a then you’ll get the value is a 15 if I accessing the value is a d of Z so you can get the value is 19 this way we are accessing the values but here you can’t perform the operation of the concatenation and multiplication means this star symbol you can’t perform it this plus symbol you can’t perform it so I’m going to remove it this part okay so likewise the uh list and uh uh tle we have also the method and function is available so let’s talking about method and function method and function so here this is not a sequence right which have a positive negative indexing and defining in the one particular uh uh brackets so we can’t use it mean and Maxes here but we can use a length we can use a type we have also own uh function which is a d CT D this function is also available so when you’re talking about the method so also have lots of method is available here like uh we have a do keys keys like if I have a diction if I have a variable is a d so d dot oh it’s not running okay wait wait a minute just me just open it again there is some web problem okay so let me just continue with this okay so we have a keys we have the values as as well okay we have pop okay we have the uh uh Keys values and items so the lots of method is available let’s do it practically let me jump on a practical implementation this one and uh we’ll also discuss about their application as well where we can implement the dictionary in the real life okay okay yeah let me just remove it these things all right let me create one file here all right I already created let me just take it experiment yeah again you have to follow this uh J notebook which is already there in a description you can just uh access this link via GitHub and just run it you’ll find it out all the information here right but I’ll just run it few um you know method and function here yeah the same dictionary a same Jupiter notebook let me create here dictionary I’ll create one dictionary is the D1 is equal to in a curly bres suppose like uh uh R and pass the value is 19 K I’ll pass the values 8. 8.9 I’ll pass the value is e colon hello I will also pass the value is the 5 colon 19 so there is no any restriction for the data type so like you started with the here the string so the key always should be a string it’s not like that you can pass anything here this this restriction is not there in anywhere in the python all right so D1 if I want to access any values I’ll pass the Valu as a k you’ll get the values as 8.9 and D1 I want to access the hello you’ll get the value is oh what’s a what’s the problem oh sorry I just accessing the value is hello with t that that is not possible I can access the T I can access with the T and get the value as a hello right this way it can be performed okay so when I just perform some operation methods here like a D1 do Keys you’ll get the all the keys here okay so r k T5 these are keys and what is the values E1 dot uh values you will get the values here again if you uh if you already aware about that you just the run this file which is I shared with you the dictionary run this file and run this video in a 2X or 1.5x you don’t require to listen each and everything right these are very basic basic things is there and just when you run you’ll understand so I’ll just give you the complete overview here okay so suppose if I want to add some values so there is no any upend option is there right there is no any upend I want to add some values how can I add it so the D1 pass any keys I’ll passing the keys here like uh U I’m assigning the values here 1,000 so when I just run it and get the values here D1 you’ll get the U in the last values okay so like way we can also assign the values so dictionary have some um you know applications so I’ll discuss about that again I will not each I will not write each and every line code so I’m taking the help from here suppose if I’m defining the values here okay here this is the list okay so question is why I’m creating a list here why not sorry this is the tle uh why I’m creating the tle here why not a list the reason behind that tle is very much secure secure in the sense we can’t change the values so most of the time we are using the tle as a key in a dictionary okay so it’s not like that you can’t create a list you can but most of the time we are using the tle because of the security purpose okay so T1 is a tle I want to make each and everything as a dictionary so you know that the Cur if you make the curly brasses like blank curly brasses so uh for example uh D2 blank cly brasses when you check the data type that you’ll also get the data type is a dictionary dict here is showing the dick not a complete name is a dictionary all right so this D1 I want to make as a uh dictionary uh dictionary key so D2 which is is nothing but a dictionary Dot from keys there is a one method from keys I’m passing the values is Keys which is a T1 so you’ll get the values is like this name as a none because I didn’t pass anything colors is a none like this so I’ll make I’ll store this values in the D3 even you can store in the same variable as well it’s on you right so let’s assign some values so D3 I’ll D3 this I want to assign some values in place of the name so I’m assigning the value name okay here you can assign directly any name like Rohit you can assign it or else you can create the completely a list here it’s on you like inside that you can Define one key in the values you can pass n number of n number of uh uh n number of uh uh you know sequences sequences in the sense in in one sequence you can create it like a list on top you can Define the multiple values what is my target here I want to create a complete table I think you you aware about Excel you you seen Excel somewhere right at least I’m expecting this this time okay so in a table suppose I have one name I have a a phone number and I have a um marks something like this so okay so the keys is this this values is always constant this is nothing but a column name so when you’re talking about the terms of the dictionary that will be calling this part as a keys and all the values will consider as a values okay so values of the particular key so likewise I want to Define some values suppose the name is name is uh name is like RIT okay uh Manoj John and their likewise their values is available so you can Define it so in a terms of dictionary when you’re talking about let me just remove unnecessary things yeah okay I want to create the complete table in the terms of the dictionary which will be the pair of uh list and dictionary okay so I have many names available I’ll I’ll just copy paste from uh this file so this one I’m just defining here okay I’m just defining all the values so name I Define the M marks I Define the 90 subject are defining the uh ma maths and college are defining the Mumbai University okay so when I just run it so you’ll get not info obviously uh you have a D3 so d 3 D3 D3 okay when you just check the D3 you’ll get the values like this at the same time if you have a multiple values you can also Define it m I’m defining with uh the other name is John so again if you have one values you don’t need a container container or a sequence we can also say if you have one values we don’t need a container if you have multiple values we need a container which can be either which can be either list or tle I’m just making here a list okay you can make a tle it’s not a problem okay and I’m just passing the values 56 and any values so maths let me pass the English okay let’s store it and uh Mumbai University and uh okay IIT Bombay all right so when I just run it so you’ll get the values is like this so here the m and John you’ll get the same time if I want to make it this as a table this is not look like a table but there is a library which you’ll learn maybe um after U uh 8 nine videos topic is the package and modules there is inbuilt packages available is the pandas import pandas okay I’m importing the pandage which is inbuild package and already installed in a Jupiter notebook and Anaconda if you’re walking with any other libraries then you will not get it par directly you have to install it import pandas and pandas do data frame so I’ll just make it completely a frame okay frame and when I just pass the values at D3 run these values you’ll get the complete table where the name College subject and marks is considered as a keys where the other thing considered as a values so when you just enter in the data science part currently you are just learning the Python programming language when you enter in the data science so everything you are dealing with a table and the table we are reading in the data frame format then you’ll start the operations so if you start the operation if you already aware about that yes this table is nothing but a collection of the keys and values right so mes John Mumbai University iitb this is nothing but a values where is the name and college that’s considered as a keys so let me just Define is the variable DF data frame and DF when I just pass the values do Keys you’ll get it the keys because this is nothing but that data frame is also nothing but a combination of list and dictionary so when I just pass the DF do values you’ll get a values as well see everything is a values there is no any Keys okay and some other method is also uh adding here like a DF do columns which is not there in the dictionary but this is the method of data frame you’ll get the values but you can see the keys is very much similar to the uh DF do columns but if you perform this operation dictionary do column you will not get it this is the uh method of a data frame don’t apply it I I’ll just remove it here okay when you enter in the data science then you’ll understand here just try to understand that a table is a made with a dictionary and list so again I request you to run all the files you don’t need to create anything if you want to do experiment do it but at least run this file this all the values is available like uh how we can get the keys how we can get the values items how we can perform pop clear operation how can get the values how can add some values how can update the values right so this is one one line of code you just run it you’ll understand easily each and everything like here creating some particular data frame is here so likewise uh list we have also the dictionary comprehension dictionary comprehension is the same as list comprehension the only thing is that the syntax will change the reason structure of the dictionary and a list both are a different let me first remove it this I’ll write the syntax and after that finish the dictionary comprehension all right okay dictionary comprehension okay dictionary comprehension when you’re talking about uh the syntax the syntax is very very same call brasses so again we have the key values pair suppose if you apply the operation is like for each in sequence so here you have to perform the key colon values is mandatory I want to print one to 10 number okay I want to print one to 10 number but at the same time I want to show the values as well suppose if you have a list you performed like for I in range which start from one end with 11 and before you just wrote the values is I and you’ll get the one to 10 number this is I you’ll get the one to 10 number but I want the cube of this all the number with the same numers should also be defined like one colon 1 2 colon 8 3 colon 27 4 colon 64 like yes so that was a list here I’ll make the curly Braes make sure that you have to use key values pair so I colon I’m just using IQ okay I Cube you’ll get the values is like this 1 28 327 464 5 6 like this until 1,000 okay so disc dictionary comprehension is the same as list comprehension while the structure is changed the set is a mutable mutable and unorder data type it’s a unordered data type okay so unordered meaning is that there is no any positive index there is no any negative index mutable means we can change the values easily in between we can delete it we can update it we can append it we can insert anywhere right but it’s unordered the meaning is that we can’t put any index so that we can access it so defining the set we are defining like this is the curly brasses even dictionary is also defining with the curly brasses but have a key value pair and here curly brasses and with the values directly 5 8 7.9 High K like this so this is a set so there is no any uh you know the same data type concept is there in the entire python again when I said the statement the many times again I’m saying there is no any data type okay so that don’t be confused anywhere so when you’re talking about the method and operations so I think set concept you already learn in a school days set the right so whatever you learn like a union section and difference and a difference update um you know this same thing same concept is also available in the programming language as well we talking about the operation so we can iterate it with the for Loop like other programming language for Loop concept we can apply it but we can’t access with any index even in the dictionary we had key so that we can access it here we don’t have any key if you want to take a values is one by one you have to apply the for Loop okay even the for Loop you can apply in any data type so when I will not write it here okay we’ll directly jump on a method and functions okay let me just write it here method and function again there is issues okay method and function so method is basically uh let’s first discuss is the union okay and the second one we have a intersection I’ll just write a small small there is not a capital intersection we have a difference okay we have a difference here is a point okay so set if you have defined with the S so s dot s dot s dot so let me just explain you a little bit about what is a union what is a intersection and what is a difference okay so suppose I have one set S1 which Define the value is a 5 7 8 and 10 I have a different set S2 which Define the value is uh uh 3 7 uh 11 and 9 okay so when you just created the complete set let me change the color complete set so there is one set where the values are available is a five 7 8 and 10 there is one set S1 and when you’re talking about the S2 so S2 is like this okay 7 is also available it’s a 3 11 and 9 when you’re talking about uh uh the um Union when you’re talking about the union so Union will take s One Union which Define the symbol like this in S2 and in a programming language we are writing the statement and s do Union with S2 okay so it’s there is no any symbol concept is available in a set programming set data type so in a set theory in a mathematics that symbol was there so the value will be the value will be uh 5 7 even 8 is also there 10 and three and 11 okay 11 I already defined okay okay here let me just write it at 10 11 and 9 okay so this will be the union so Union is basically like this we’ll take all the values we’ll take all the values better the same way we’ll take all the values here all right okay so when I talking about uh the next concept is a uh intersection so S1 intersection S2 so it will take only the common which is the common values here so here only one values is available which is a seven so intersection will be like this this part only seven okay only seven will consider as a intersection so this part will be a intersection and you know and if you’re taking entire things that will be the union if I’m talking about the difference there is also one more concept is a difference so difference uh let me just make blue color okay so if have a value is S1 and S2 here is a 7 uh 5 10 and 8 3 11 and 9 okay if I have a value is this is the S1 and this is the s 2 and if I value is S1 difference S2 if I Define like this so from the S1 I’m removing the S2 okay so it will take the value is one Le this only this part it means only the seven will remove okay because from the S1 I’m removing the S2 so 7 will remove so you will get the values is a five 10 18 so here is the very interesting concept is there he uh if I have a toule I have a list as a container so what is the need of a set so what is the special qualities there in a set concept the special Quality quality is there the set never take a duplicate values okay so I can also write somewhere let me just write it okay can I take it the next page it’s very difficult okay all right so let me go to the Practical implementation I’ll just write on a practically itself let me just remove all those things all okay all ranks yeah all right so we are learning the last concept is a set so set is always take a unique values I can write here okay so set first of all it’s a mutable let me write here itself okay it’s a mutable and unordered data type unordered data type and the second concept is that set always take always take unique values unique values there is is no any duplication you’ll find it out in the set concept but in a tle and list you can put a duplicate values as well that is a speciality of a set concept set data type if I Define the set is like the you know curly brasses 4 6.8 and K and nine so this is the set okay it always take a unique values here is sometime confused that I’m just using the curly brasses as a set curly brasses as a dictionary as well with the key value pair but how to define the blank set and blank dictionary it’s very very confusing right so suppose I have the uh a variable temp variable TMP variable so which is the curly bres so what will be the data type of temp it will be the set or dictionary so when you just check the data type okay when you just check the data type of attempt so you will get a dictionary it’s not a set but how to define the blank set so blank set is defined like S2 is equal to set set function bracket see this function we are using to define the blank set when you check the data type here S2 you’ll get a blank set okay we still here so let me just create one more a set here S3 is equal to 4 6 uh 9 okay 1 6 okay six let me just take it one more time 6 6 6 and uh uh 15 so when I just print the value is S3 you will get one this six is one time it’s not showing the multiple time here in case if you have a list if you have a tle it will showing the multiple time but the set will take always the unique values so I have S3 I have S1 I want to perform some operation here like uh Union intersection so S 3 dot Union if I pass the value with S1 you will you will get the values with the combination of S1 and S3 but the value will be always unique you can see here the 1 15 5 6 which is a combination of uh S3 and S1 here there is no any particular order is available because of the because of uh the order is not available so you can’t access it in case if you’re passing the S3 in square bracket of four you will get a error because there is no any POS positive and negative indexing is there but you can apply the for Loop here like for uh variable V in s so if you want to iterate it you can iterate you can iterate it but you can’t access any values with a square bracket like uh list tle string and a dictionary with a key that you can’t do it here okay so again uh you can perform the U difference as well we can perform the intersection as well like S1 do intersection intersection you’ll pass the values as S3 you will get the common values here which is S4 and 9 if I perform the S1 do difference okay difference so there is the two things difference and difference update so if I pass the values S3 so whatever values there in S3 which will be uh s which whatever value is there in S3 in S1 that will be removed so here the 6 and 8 you’ll get the values you can get here S1 and S3 so S1 and S3 here you can see the S1 which is a four and 9 which is a common that’s why it’s remove the four and 9 from the S1 with S3 and now I said that there is the two values the difference and difference update the difference when you apply so the value will be as it is it will giving you the result S1 is as it is S3 is as it is but if you apply the values S1 do difference update difference update and pass the value is s uh3 you will get the update updated values in S1 okay previously the value was not updated as I said that we can add some values as well we can update some values as well we can remove some values as well right so if I am talking about S3 so I want to add some values S3 do add and pass the value is suppose 56 so you can add it okay because it’s a mutable data type if I want to remove anything as three. remove I can easily remove it like nine you can remove it if I want to uh update I already discuss there is lots of things is there intersection Union difference okay so you can perform it so again you have to run this J notebook where is the last section yes last section here you will get the two difference set and performance all the operation here the one by one you will easily understand all right so you can see you’ll find it out each and everything thing yes uh the one thing I forgot to discuss with you the mean and Max and Len the same concept we can also apply it here so mean Max you’ll find it out the minimum values you can find it out the maximum values like S 3 mean of uh what is the minimum value of S3 you get one what is the maximum values of S3 you’ll get uh S3 but again don’t forget to run that jaal notebook okay that give you the more clear idea as compared to this video right video just giving you the complete overview but the details if you want to go the jiter notebook is important for you so before going to the next topic like uh uh looping concept first you have to understand the entire data type concept from the Jupiter notebook this is the set so there is one more application one application is there in a set which is the uh set comprehension likewise the dictionary comprehension we had we had a uh list comprehension so set comprehension is also there comprehension okay set comprehension so set comprehension the syntax will be the same like the and list so whatever for we have to use for that particular variable in sequence sequence can be any range any uh other sequence like a list or tle or or list or tle mainly so here just you have to write variable and curly bres is mandatory for a set okay so let me just create it I want to print just one to uh uh uh one to 10 number one to 10 number let me just print it here for I in range I’ll pass the values of 1 comma 11 is same like a list and dictionary comprehension only the syntax is little bit Chang this because of data type is change when I just run it 1 to 10 number in case in case if you you have a list okay instead of this uh set I printed the values is instead of the I I I’m just want to print high so the high will print 10 times you can see here high is printed the 10 times but if you apply the same concept for a set you will get only the one times it will be generate 10 times but the accept only the one times go here and run it so you’ll get only one times that is the speciality of a set which is always take the unique values never take any duplicates okay the last concept of the set is that Frozen set the Frozen set is basically it’s not a new thing here it’s we can say let me just remove H we can say it’s a it’s a immutable set so whatever we we had a set concept that was a mutable so here we have immutable set immutable set but again if it because of the set it will be unordered immutable set and that is the unordered okay it’s a immutable set that I was talking about Frozen set okay that concept is a frozen set so let me just discuss it little bit and after that uh we’ll stop this video because the Frozen s concept is already there in the Jupiter notebook okay the heading is frozen set Frozen set which is nothing but immutable set and unordered okay it’s immutable set and unordered so suppose I have one particular set which is the S4 and I Define the values like this okay there is so many duplicates is also there if I print the S4 you will get the only unique values so this is the set which is a mutable but uh if I want to make S5 as a frozen set Frozen Frozen set yeah Frozen set I’ll pass the S4 and run this values so S5 is a frozen set S4 is a normal set let me also Define one more set it here so 5 6 2 1 yeah that is enough okay so can we perform the operation in between the Frozen set and set answer is yes so only thing is that it’s immutable so if I want to add anything in S5 which is a frozen set do add and pass the values is anything like uh 1,000 that will showing you the error because Frozen set has no any attribute of addition but because of this set we have we can perform the operation of like a union intersection difference this values we can easily perform so like if I want to perform the difference operation here S5 do difference you can see the common values is a five and six so S5 difference with S6 which is the set normal set which is the mutable set we can say and S5 is the immutable set when you perform the operation you will get a Frozen set values okay but but if you do it Ulta if you do it the opposite one S6 dot difference and pass the values s five then you’ll get a set because I perform the operation the difference on S6 okay S5 on S6 so S5 is nothing but a frozen set S6 is a set that’s why it’s perform like this again you can you just run it it a small thing is here you will get the values finally we completed all the data types numeric string tle list and D dictionary and set but after completing of all the data type and we we discuss in the very details way but we have a confusion sometimes which one is a mutable which one is immutable which one is ordered which one is unordered because so many things is there where we can add it where we can remove it so in this video we will focus on that at the complete mind map of all the data type so that if you’re going for interview you just have to refer this slide you can easily understand all the data type in the same time okay so let’s start it so numeric data type we have a mainly three main function uh first we’ll start start with a function so we have a int okay we have a float we have a complex this three function and string we have a St Str function tle we have a tle function list we have a list function dictionary we have a dict function set we have a set function this all the data this all the function is available for all the data type all right when you talking about how we are defining so integer we are defining like a 5 7 like this the float we are defining like 8.9 with some decimal point complex we are defining like a 7 + 8 G okay string we are defining always with the if if you start with the single quotes always end with the single quotes if you start with the double quotes end with the double quotes tle we are defining with e is equal to round bracket 5 8.9 like K anything you can Define it there is no any common data type concept is there in the entire Python programming language so list we are defining like L is equal to square braet 5 8 7 9 7.9 square bracket we are using dictionary we are using the key value pair like a d is equal to key values okay key values pair and when you talking how we are defining the values D is equal to like 5 col 8 okay 7 col 9 for a set we don’t have a space so I’ll just change the color okay the set we are defining the value is s is equal to curly bres 58 directly you can pass the values this way we are defining the all the data type okay so when you’re talking about the mutability and immutability okay mutability and immutability which one is a mutable and which one is a immutable right so the numeric data type is a immutable string data type is a immutable tle data type is a immutable list is a mutable dictionary is a mutable set is a mutable okay see it is a mutable and the next one is a ordered and unordered so let me just write here ordered and unordered okay ordered which one is ordered which one is unordered so numeric data type there is no any concept of ordered and unordered likewise the uh numeric uh for mutability as well to numeric there is no any meaning of mutab and immutability because it’s not a collection anything like string is a collection of some letters or El uh okay so if you have only one letter so at least it will be defined the index is a zero right so so string tle have a meaning is that it’s a mutable and immutable ordered and unordered so likewise in a numeric we don’t need any discussion for numeric immutable and mutable string is a ordered okay couple is a ordered list is a ordered dictionary is unordered set is unordered clear set and dictionary is unordered whereas a tle list and dictionary is a ordered all right so if you want to add anything again so dictionary uh uh sorry numeric there is no any meaning of that we can add it or we can’t add it likewise the string as well so here tle dictionary tple list tle list dictionary and set this will be considered as a container because it contains some values right so like the tole we are containing the values is a round bracket list is a square bracket dictionary is a key value pair set is also containing the values whereas where is tole let me again make it different color tole list and set it’s called as a sequence okay why it’s called as a sequence because we are directly pass the values so only dictionary we can’t as a sequence because it’s a key values pair if you just iterate the values you will get a only key if you’re not passing the dot items you’ll get only keys so that’s why we are not considering as a uh sequence but dictionary is a container so this is the way where we can remember it if that is not more important like which one is a container which one is not container because that is not a question question is that which one is mutable because giving you the information that we can’t change the values it’s a immutable if you have a mutable that we can change the values that is giving the information right so the container sequence is just tell you that we can we can just uh you know wrap up the multiple values at the same time if you’re talking about the add the values so no we can’t add any values okay I’ll pass I’ll write the no we can’t add any values there because it’s a immutable but in a list if we want to add anything so we have a multiple option is available like uh append okay we have insert okay in a dictionary there is no any direct concept is available and and and we have also the extend option is there extend okay when you’re talking about the dictionary we can’t directly add anything but like this b square bracket key and pass the values is values we can add it right there is no any direct method is avable label but we can do it and uh set yes we have uh s do add method is available this method we can add the values but in a tle we can’t add anything the last thing is the delete so there is no any meaning of uh the deletion of in numeric and string because these are not a container but if when you’re talking about the tle so tle no we can’t delete anything because of it’s immutability we can’t delete anything but in a list we have a lots of option is available we have a clear option okay let me just change the color okay we have a clear option we have a uh remove option we have a pop option okay this option is available in a list and in a dictionary we have a pop option is is available okay and uh in a set we have also the pop option we have also the remove option that is a remove and we have a discard option as well okay but the important is that yes in a list dictionary and a set we can delete anything we can add anything and uh uh in a tle we can’t delete anything we can’t add anything okay so this is the complete mind map where we can just remember it and we can perform the operation this this data type operation in any applications let’s understand the conditional statement the conditional statement there is a three keyword we are using the first one is if second one is L if third one is else okay before starting the Practical implementation first we’ll understand the flow chart of uh conditional statement in a diamond shape we are always using a condition so condition is always start with if that can be two scenario that can be true and false if the condition is true then giving the statement okay giving the statement and pro uh the block will terminate after giving the statement it will not checking any other block it directly terminate okay here I’m just writing here and but if your condition is false I want to give a one more condition uh to check that time we are using L if we are not using IF in case if you’re using if that will be the separate block L if is also the condition then it can also be the same same scenario can be true can be false if it’s true then giving the statement okay giving the statement and the same thing after giving the statement program will terminate and after the uh Al if condition I want to give one more condition then again you have to use the AL if but in case if you don’t want to give any other condition you just want to stop it you can directly top this block but we always doing this practice that uh the the last one we always using else so else is not a block else is just giving the statement in case any block is not uh you know if any if any block is not passed so here I’m just using else else is just giving the statement if any block is not satisfied then else block will run and then directly terminate after the else block you can’t write any other condition in case if you’re writing that will be considered the separate block the separate section that is not a relation of the particular block so whatever we discuss that is one block is mentioned there this is complete one block like if the one condition is satisfi then giving the statement not satisfi then giving the any other state any other condition to check that the condition is satisfied or not let’s try to understand in the Practical scenario how exactly is working and then we’ll also enter the real life scenario as well let’s say for example I have a three variable a is = 5 b is = 8 C is = 19 this three variable is there and the condition is always start with if we are writing if take a space and write it down a is greater than b okay that uh your condition after the colon it automatically taking the four space that we are calling indentation and we consider as a block if I write the statement as a print hello and when you just run it and it will be check the condition is satisfied or not so a is greater than b means 5 is greater than 8 is it true or false so obviously is a false that time is not giving any answer but if the condition is satisfied like a is really less than B that means five is really less than eight then it’s giving the answer is hello okay but here I put only one condition in case if you have a multiple condition is there if a is greater than b okay if a is greater than B that time will print normally hello and and uh when you click enter after this print statement it’s automatically the same block this is the same block but I want to write one more condition so obviously I have to go back and write one more condition which is the L if L if C is greater than b colon let’s write some statement statement is hey and if I want to one more uh uh if I want one more condition I’ll write it the L if is a is greater than C there is a three condition I mentioned there and simultaneously you can just write a many condition is here okay hello hey hi and after that we always using the ls this is the good practice in case if you’re not using it will not show you any error so likewise here it’s not showing any error so same wise here it will not show any error if you’re not using else just write down here else colon and your statement print hello uh just buy else is always execute if any block is not executed previously if any block is not executed that time is working so python is always working the top to bottom okay it always check that line number one is um um satisfied or not satisfied a is greater than b the meaning is that a is greater than b B that is false Okay C is greater than b which is true a is greater than C which is also false and else is always is a default one in case previous one is not executed that time it will run okay so here the block number two here the third one line number three is execute it when you run it it giving the answer is he but what happened is all the condition is satisfied all the condition is true like a is greater than b okay A a is less than b uh and C is greater than b and uh a is also less than C that all the condition is satisfied so what will be the answer the answer will be the first one because according to the rules here which we discussed if the first condition is satisfied is giving the statement and terminate the Block it’s not entering the next block because l l if will check if the first condition is dissatisfied right if the first condition is satisfied is giving the statement and terminate the block but here the L if is after the false of the first condition so here it giving the answer is hello one day again the last one if no any condition is satisfied that time the buy will execute which is the default one if nothing is there nothing no any condition is satisfied like uh I’ll just change it here uh greater than less than greater than so all the condition is false there is no any true condition and uh so when you just run it it giving the answer is by because nothing is satisfied here this is the way of conditional statement we are using but again do you think that we are using this kind of conditional statement in the real life answer is no we are not using this kind of conditional statement this is the way where we are learning we are starting the conditional statement to connect with the real life let’s take one example and we’ll understand it how exactly the conditional statement and other things like a loop is working the example which I selected that logging with username and password login with user name name and password okay this is the real life scenario where we’ll implement the conditional statement let’s say for example one one different example and obviously the practically we will use with the username and password login if you’re going for applying in a passport in a passport office there is a first condition you should be eligible for 18 year old okay if you are 18 year old the first condition if you you satisfied then it will be check second condition do you have a pan card do you have a AAR card do you have any other government documents it’s like that like it is like condition inside a condition right so here in the real life we always using condition inside a condition that scenario we are calling nested conditional statement the statement which we are using calling nested conditional statement nested conditional statement okay as we discussed in the last slide so where we decided that if and L if is using for a condition where is the else is giving only the statement okay else is not giving any kind of condition only the if and L if given the condition let’s understand the flow of the Ned conditional statement the flow is always start like this if you have a condition condition one okay so obviously the condition is always start with if that can be true scenario can be true can be false okay if the uh condition is false that time if you want any other condition we always using Al if okay if you Al if that can be true that can be false that we understand in the last uh slide but if the condition is true and I want to give one more condition okay that I can write here condition two I want to use one more condition what will be the keyword is it be a if or if the answer is if okay so previous slide what we had we had is like if the condition is satisfied given the statement and not satisfied that time Al if or else right so there was no any confusion Al if is always coming from the false but if I want to give one more uh condition that time which keyword we have to use the keyword which you are using it here if if the condition is coming from if the condition uh as I said that Al if and uh if I have right Al if and if I have so both are giving the condition right both are giving the condition but if the condition is coming from the true part and if the condition is coming from the false part so we are using accordingly if is coming from the true then we are using if the condition is coming from the false we are using Al if hope this one is clear so condition is always the two scenario the true and false okay if the condition is true and again if you want to give one more condition condition number three so again we have to use if but let me just stop it here if you want you can just increase it accordingly I’ll give the statement okay I give the statement and give the statement if the statement is given OB obviously the program will terminate I’ll make the termination is here end program is terminate in the end it will be check in other block if you have okay so here given the state condition is uh the statement is uh ended again if it’s a false I want to give any condition so what will be the keyword condition number three what will be the keyword the key keyword will be Al if keyword will be Al if right why the AL if because is coming from false part so if its condition can be two possibilities can be true can be false if it’s true and here here if I want to give any other condition then what will be the uh keyword condition number four what will be the keyword keyword will be if because it’s coming from the true part okay and and then let’s give the statement I I don’t have much space so I’ll just close it here okay okay and then program will terminate and after the false if the condition is a false it here don’t worry we’ll understand with the Practical scenario and that is a real life example so you can easily understand okay so giving the false if the false and if you want to give the condition L if you can use it or else I am just using here the else with statement okay else is not giving any other Condition it’s just stop the block the same time here this El if I after the El if I want to give the condition I want to give the condition let’s condition number five okay condition number five then it will be start with if not Al if why because the uh uh the condition is coming from the true part okay so it’s giving the statement after giving the statement program will terminate after the statement program is always terminate it not take any other section okay and here is a false was there here is a true was there after the false it’s on you you you can pass any else block you can otherwise you can D directly terminate the block here I am also terminating the block without giving any else part let’s make it a else part it here okay else part and terminate the block so this one looks little bit clumsy but uh it’s very easy when you start doing the practical and uh when you start doing any practice okay uh let me just summarize it what exactly the conditional statement is working with Nate conditional statement see we have a if we have a l if and we have a else okay if and L if is giving the condition these both are giving the condition and this is giving the statement is not giving any condition and this one this condition is always start in it start always using the if always if we never start with L if and if the condition is coming from the true part we are using if if the condition is coming from the false part we are using Al if very simple let’s try to do with the Practical scenario okay so to understand the Practical scenario with the Ned conditional statement again uh this is I have to write one more heading uh which is nested conditional statement nested condition okay nested condition don’t worry you’ll find it out this uh this Jupiter notebook below the link in this video okay so suppose I have one username which is very simple just like um a hurry and the password which is defined it here currently we are not connecting with any database in the future we definitely will do that I’ll continue with this this example in a very broad level hurry 1 23 it’s very this is the password is very simple hurry 123 I want to perform the operation condition if the first one I have to take a user so uh user input user input I I take the example is input please enter your username okay the same time the password uh password input PS WD I can write it here it’s just a variable input please enter your password okay the condition we can just perform it here if your user input is match with double is equal to you are comparing you match with user if it’s match with you can just writing it this currently I’m just checking that my program is performing well or not user name is correct so when I just run it it’s uh asking that will please enter your username username I’m just writing the hurry and is asking the password is hurry 1 2 3 so the username is correct that means whatever I wrote the program that is perfectly fine okay so when I can say that my uh when I can say that uh you log in it successfully if your username and a password is correct right so if your username if your username uh is match with hurry and if your password WD input is match with password okay this password and his input is matched with password that time I can say that print login successful logging successful okay all right so let’s run it again and check that so here you can say the if inside if you perform it right if you have a multiple username multiple password that time you can just perform accordingly L if can also be performed I I’ll show you so when I just run it your intro username which is the hurry password is uh hurry 123 okay so logging successful but again when I just run it there is one issues if my username is incorrect still is asking that what is your password if my username is Inc correct it don’t need to ask the password right so if I’m my password is a correct so it’s very difficult to know that which one is wrong so it’s not showing any other any information you can see here it’s not giving any other information so this is wrong we have to use else so that we can tell to the user hey your password is wrong your user is not wrong the first issues you have to solve it here is that it should not be asked the password until your username is correct okay my username is correct the time it’s ask the password right if I run it my username is incorrect it not ask the password that I perform well but if the username is incorrect I have to give the proper answer hey my username is incorrect so the line number three If it’s incorrect I have to use else block for that else I have to write there my usern name is incorrect okay my username is incorrect not my username username is incorrect that’s it username is incorrect some explanatory symbol okay username is incorrect right so if I’m writing anything any uh not proper username then it’s saying that username is incorrect but again if my username is correct like hurry and it’s asking that enter your password so if my password is incorrect that’s not giving the proper information I have to write something here for the line number five if block else print the user uh print the uh password is incorrect okay password is incorrect so that we can know that which which is my incorrect uh you know section is there if I’m writing the hurry password is incorrect the password is incorrect is mentioned there let’s run it again hurry already 1 2 three now properly loging logging successful and in the real life scenario if you have any proper software so after the logging successful you can start is any other function any other block to uh different application you can start from here right from this uh username and password login in case if you have a multiple username multiple password then what will perform so I can also perform like I have a multiple username I’ll copy it I’ll not do anything with this blog multiple username that uh another username is a kind of uh John John 123 right so here I will use because the first condition is not satisfy I want to check one more condition that will be the L if L if user input is uh if user input is match with um the previously it’s checking with the user So currently I’m checking with John that was uh hurry and that was uh Harry 123 right previously I defined a different variable but I I can write it out directly here here John if my um user input is a match with John okay the time I will ask the password again the same thing I’ll perform I’ll ask the password what is your your password if my password is also match with here I will use if not Al if go back again you can check it if your condition is coming from the two the time we have to use if right here if the condition is coming here L if if the condition is coming from the true we have to use f okay so you can use it here just give me a minute yes sorry for that let’s continue I will use the condition here if my password p a s WD input is match with is match with John 1 2 3 if match with the John 1 12 3 the time I can write the statement print um logging successful okay Lo logging successful that can that time I can also write it so here you can say I use the if inside if I use the AL if inside the if right I also use the else as well so username is incorrect like if not match with not match with John the time is giving the username is incorrect but in case if your password is incorrect and here password is incorrect I didn’t write anything let me just write else password is incorrect okay when you just run it is asking that your enter your username I’m writing the John okay it will also accept the hurry as well and here I am writing the hurry 123 so that’s saying that password is incorrect because it’s already enter in this block and is checking this kind of password okay it cannot be checked the previous one let’s run it again hurry H 1 12 3 is giving the loging successful John John 123 is logging successful it’s perfect L fine so hope you understand the scenario and when you’re talking about the future persp future perspective of this login with the username and password the simple thing is that what can be the uh future scenario the first thing is that if you have the thousand of user thousand of password then how we will maintain it so you can use the dictionary to maintain it in a temporary basis and that permanent basis you can use the database second example is that if my username and password is incorrect so the block is like down block is uh stopped directly like a password is incorrect stopped but it should be ask the password one more time right so the time we are using the loop so the next con concept which you are using which is a loop and where we’ll discuss that how the loop we can perform with the user and login pass uh this example there is two types of loop is available let me back two types of loop is available while loop and for Loop okay before going to the Practical implementation we’ll first discuss about the flow chart so the every Loop there is a condition and after that we are entering the uh block right the condition is always showing in a diamond shape and statement is a rectangle shape so here the condition is not defining with if the condition is defining with a while if you have a condition and uh condition and I’ll Define it a while okay so that can be two scenario that can be uh true that can be false here I’m writing the false uh because of the reason I I’ll show you because it will be very difficult to define the structure that’s why the false I’ll write in here can be true can be false if the condition is a false that time is also giving the statement okay the time is also giving the statement and you can also write one more condition like with while we can also write a condition with if anything you can write it but if you write the condition with a while let me write properly while if you write the condition with while then if the condition is true the giving the statement giving the statement after the giving the statement back to the loop again it’s never terminate the difference between the conditional statement and loop is that Loop is end Loop is continuous uh running whereas the conditional statement is ending okay so the loop is end when the condition become a false the one only one way to condition uh there is only one way to stop the loop if that condition become false okay this is the way our Loop is working so when you’re talking about a nested Loop so nested Loop is also the same but before going to the nested Loop let’s first understand some practical scenario the last video we discussed uh uh this example I’ll continue with the same example before going to this um you know real life example I’ll give you some small uh you know exercise suppose a is equal to 5 is is equal to 7 and B is equal to 9 is there okay and we wrote uh I just want to print 1 to 10 number not to this number with this number I want to print one to 10 number so I want to start with one okay is equal to 1 so while there is a condition while there is a condition condition condition is a should be less than r equal to 10 so one is really less than r equal to 10 a while is working kind of condition with a loop right condition with a loop and and write the statement is uh High okay so the number a is equal to 1 which is true which is giving the true section to uh true statement here and it will enter inside a loop and 1 a is always one so when you just run it here when you run it here it will be run in uh like a infinite Loop because a is always one and 1 is always less than or equal to 10 so it enter in the infinite Loop so you can see here your process is still running so it will never stop so I have to forcefully stop it here and then I’ll tell you where how the proc process will working okay I’ll make the increment I’ll make the increment here a is equal to a + 1 a is equal to a + 1 so what will be happen he starting the a is equal to 1 and also print the a as well a is equal to 1 the next time is become a equal to a + 1 so a + 1 is become a two two is assigning to the a it not ending the loop it will running again and again again and again and until the condition become a false here the condition become false like if the number become 11 which is not less than or equal to 10 then the program will stop when you run it 1 2 3 4 5 6 9 8 10 okay program will stop so you can just write down here uh program finished finished okay program finished so when this blog will end then program will finished okay and this we are using so as I said that this is the why while is working as a condition with a loop if it’s a condition so I can use the default statement like else so yes I can also use LS it here and instead of writing here I will use it here so when the condition will be running it will be run that statement and when the condition become a false the for this condition become false I can use it here okay so like this this one is working so when you’re talking about the nested Loop how how the nested Loop is working so here I will just focus on the while loop okay for Loop we’ll discuss later I’ll focus on the while loop here so nested Loop how it exactly is working I’ll just connect with the real life scenario as well let’s first understand with the uh flowchart okay let me remove yeah so I have a loop I have first condition condition with the while okay that can be true scenario can be false can be true okay if it if it’s false so it’s on you you can uh write uh statement without else with else as as well so I’m just writing here with else else statement again State else okay else is always giving the statement No any condition so the condition number one and if the condition become true I want to give a statement you can give the statement okay according to the rules it will be back and repeat it again and again and this will be end but I don’t want to uh repeat it only this block I want to add one more condition is here so what I will do I will write one more condition condition two that again it will be used with while okay and that can be two scenario like uh the previous one can be true can be false false okay if it’s a true then it will be repeat the block it will be repeat the block I’ll give the statement okay I’ll give the statement after giving the statement it will be jump it here but again just think about it if here the condition become false I give the statement the program will terminate the answer is no program will not directly terminate here when you just directly terminate if you’re writing that means it’s a false this is not like that it will program terminate if you have a loop inside a loop this one is Loop inside a loop we can say nested Loop we are working right The Heading name is listed Loop if the program uh the second Loop because it’s coming from here after giving the statement here if the block if the uh condition is a false given the statement after giving the statement it will be back it will be back it will be back to the loop if the first one is also false then it’s okay if the first one is also false then given the statement then ter end the block this is the way our nested Loop is working okay so to understand the scenario I’ll give you one example and with that example we’ll understand okay I want to print 1 12 100 number uh I want to okay I want to print a 1 to 100 number 1 to 100 number okay in the 100 number starting 1 to uh 20 I’ll print hello and 20 to 40 I will print a high and then 40 to uh 70 again I’ll print hello okay 70 to uh 72 uh 72 100 again I want to print here okay I want to print uh let me also put one more condition 70 to 90 print hey again 90 to 100 I want to print hello again you can see I want to print hello three times one time two time and three time so in between high and ha is also there so how can we perform with a nest uh you know nested Loop is here in this example let’s try to understand okay so instead of writing the loop I can write it here uh while and for but I’m working with the while okay I’m working with the while all right I want to print one to 100 number let’s print one is is equal to 1 and uh while a is less than Ral to 100 and just print the hello first okay hello and with that number a all right and uh let’s increment it is very very important otherwise it will be enter in the infinite Loop let’s run so 1 1200 it printing in the hello but my question was that in a 20 to 40 I want to print high okay and 70 to 90 again I want to print he let’s first focus on this 20 to 40 hi and again hello 20 to 40 so I will enter inside the loop again while a is greater than 20 and a is less than or equal to 40 okay less than or equal to 40 if it satisfy the time I print high and with increment a is equal to a + 1 so what will be happen so it’s printing 1 to 100 number 1 2 3 4 5 6 100 right but in between if the this block is satisfied like 21 so again 21 let’s print a as well 21 High 21 22 High 23 high like this this program will run repeatedly when the number become the 41 it back to the again Loop is this satisfied or not yes it satisfied that means the 41 is also less than or equal to 100 then again hello will print when hello will print run the block like this 21 to 40 like this okay and again hello will back because because I want to print the 40 to 70 hello again so I will not write the separate program for hello okay because previous block it already running the hello the same thing I have another condition which is for 70 to 90 he so I’ll write a while a is greater than 70 and a is less than or equal to 90 I’ll print the value is a a again you have to a is equal to a + 1 you run it so high which is start from 21 to 40 again 71 to 90 again hello this is the way our Loop is working okay this is the way our Loop is working this is the process so hope you understand let’s try to understand how can how can we Implement with our real life scenario which I promised in the last video Yeah copy just paste it here I want to incrementally running this program okay incrementally running this program so let me just remove one block let’s make hurry only okay so what happen if my username is incorrect it show knowing that username is incorrect but it should be giving the option for to pass username one more time this is not showing it here so what I can do I can apply the loop is here okay I will ask the usern name in a loop while I’ll make it true directly I’ll make it true when I just make it true yes I’ll make it through it here when I make it through so it will be uh you know in on a looping format so it will ask the question username username is hurry which is incorrectly I’m passing it here so it’s saying that username is incorrect okay when I just pass hurry again so it’s asking the password password is incorrect if I’m passing here it’s saying that password is incorrect and again is asking the username let’s pass the hurry hurry 1 123 proper password logging successful after logging successful it should not ask anything so what is a good practice of writing the code writing code is that make a small code to run it and check that where is the mistake and Sol it one by one okay so if the my loging successfully so make sure that you have to break the loop okay if here I’m forcefully breaking the loop because I’m not performing any numbers is here so that after some time the number become a false it will never be false so that’s why I’m breaking forcefully okay so again username wrong is asking the username is incorrect so hurry I will write hurry 1 2 3 so logging successfully into breaking the loop okay so here I want to perform the nested while loop because you can see when just run it hurry uh wrong use uh yeah hurry and password is wrong the time it should be ask only password but it’s asking that password is wrong and asking the username one more time it’s not make sense so what I can write it here hurry hurry 1 2 3 let me just close it I will perform the condition that uh this password uh password condition separately while true and everything I’ll put inside a loop so what will be happen if my username is correct if my username is correct it will be enter in the second Loop okay entering the second Loop and if password is wrong it’s not going back directly it will be repeating only the password part let’s run it oh sorry let’s run it my username is hurry password is wrong it’s asking the password only okay if my password is correct hurry 1 2 3 so what will be happen logging successful is a break the loop but it’s breaking this Loop only but the problem is not breaking the first Loop that’s why is asking the username you can see it’s breaking this Loop there is a two while so obviously there is will be the two break okay so after the break I have to break one more time is here okay in this condition here as well if my blogging successful I have to break two times inv Violet okay sorry here I can’t use it because uh this will be here why I think so yeah why I can’t use it because it will be the same block password is incorrect okay wait a minute I make it wrong this is for inside because it it was impacting to this else that’s why I was not able to use it so username is hurry password is incorrect is asking that you ENT enter your password only I’ll write the password is here hurry 1 2 3 it’s showing that login successfully properly Stop the Loop so I am giving you one uh task for you so you have to apply the numbering the the task is that I will explain in after two three videos okay I’ll explain after two three videos not a two three videos after the uh loop ending so the task is that apply oh not here sorry I it code hurry hurry 1 2 3 the task is very simple you have to apply the number part apply number in apply number apply count if username and password is failed in three times okay if the username and password is failed in three times so you have to stop the uh apply the count block account block if the username password is a failed in a three times so your your process should be blocked for some particular duration just imagine that if you’re applying the username and password in ATM machine it’s blocking for some particular duration like 24 hours so you here you have to block for just 20 seconds 30 second right so I’ll explain you after two three uh after completing of the for Loop so whatever flow chart is showing it here for while loop that is also applicable for for loop as well the syntax of the for Loop is a little bit different uh when you compare with the while loop so basically how exactly the for Loop is working so for Loop is working like for this is the Syntax for any variable you can write it and uh that sequence in sequence and the code whatever um block you are writing so I’m just writing here the block so you can whatever uh you know block or any statement you want to write you can so here the sequence can be anything sequence can be your range sequence can be your list sequence can be your tle sequence can be your set right anything so basically the for Loop is used for the iterating of the process if anything is available in a uh you know in any container if you want to iterate it if you want to display the one by one we are using the for so let’s try to understand the Practical implementation so yeah for Loop suppose I have a sequence sequence is uh scq limes right scq is equal to range so range is one of the function which provided the sequence from one number to another number with interval so if I’m passing the range is like 1 till 15 yeah 11 so python have a rules that it never end with last values it end always before one so if I’m I’m writing the 1 to 11 that means it’s a 1 2 3 4 5 6 7 8 9 it never goes till 11 okay so I’ll just use a for any variable I’m writing the v in sequence so when I just print that variable so it will be printing the one by one it’s printing like 1 2 3 4 till 10 okay so that sequence can be anything so simply uh the range is very famous so let me first discuss about the range and after that I’ll also discuss other sequence as well so like here I want to print 1 to 10 let me again print and then we’ll discuss the details for I in range I can be it’s one of the variable 11 print I okay so one to 10 number here if I want to print 10 to 15 it will be going like start from 10 and end with 60 then it will print a 10 to 50 okay because it’s never end with the last values so after this comma it always the value is one because always incrementing with one okay but in case if you want to increment with the two like 10 12 14 16 like this let me write the 26 okay 1 to 26 so I want to increment with y two so 10 12 it will be going like this right so this is the incremental way so if someone is saying that just print 10 to one so the process will be also the the same like for I in range 10 we will start with a 10 end with one when you just print it you will not get any values because after 10 it’s become 11 but you put the ending value is one so I’ll pass it here so it will not print anything so you have to decrement the value is a minus one so it will start from 10 and reach till min-2 not a uh it will reach to two not a one because the python is never end with the last values okay so you can’t pass any decimal point is here in increment and decrement always the integer values in case if you pass 0 1.4 will showing you the error so float object cannot be interpreted as an iner always try to understand the error statement it give the lots of information okay I’m just removing it here 10 to 1 10 to2 is printed as I said the other sequence is also acceptable here uh for examp example I have a sequence is a is equal to our statement is India India so if I’m just printing the uh this uh statement India in a for Loop for uh for V in a V is nothing but a one uh variable a just nothing but India so when I print V it will be showing the value I and d i a The separately okay because it’s it’s passing as a sequence and it open it here the same thing is applicable for any list as well so in the sense if you have a uh any list is available where five whatever values is available inside that that can be string as well okay and when you just apply the for for v in LS and print the value as uh V and it will be showing the value is the sequence order for example I want to perform the value as U um 1 to 10 number with the square up of each number like I have a list okay LS is equal to IM me just write a list which have the range of one to 11 so it will be showing like this okay this is the list but I I don’t want to list like this I want to list of the square of each values square of one is one square of two is 4 square of 3 is a 9 square of 4 is a 16 like this so let me store the value is any variable is a LS so when you directly applying the ls is a square of two it will be showing the error because that operator is not supporting so how can we do like this so that time the for Loop is really help us so we can like for each any variable each is one of the variable each in LS and when you just print each showing the values like 1 to 10 number but I want the square of each values so you can just perform because these are the one by one number the square of each values when I just run it is going to square but again the square of this values I don’t want the separately I want in the proper list like the original format so it will be showing like this the that time we can use the comprehension concept we can use the comprehension concept here I’m just using least comprehension list comprehension concept basically list comprehension is nothing but applying the loop in a list okay so whatever I I printed here I can I can just wrap up with the list how can we wrap it so the for each in LS was there so whatever you want to print you have to just write before you have to write before LS okay this is the this is the syntax of the list comprehension and whatever uh data type you want I want a list just make a square bracket it will be showing you this all the values sorry not LS that output you want to print so each will be showing like this so each is showing the all the values but if you want to apply the square it will showing you the square the syntax of the list comprehension the very simple simple uh very simple is that where where uh just uh uh sequence uh just iteration for where in sequence and before just write where that’s it this is this this is the syntax of uh the list comprehension but when you’re talking about the double comprehension the concept is also very similar just you have to perform the square uh round bracket it will be a uh tle comprehensive the comprehension concept is very famous and uh very useful in a real life scenario when you’re making any kinds of projects okay back to our topic um to understand the nested for Loop last concept is remaining in uh loop next for Loop how can be perform I selected one of the example the example is uh pattern concept I think you learn this pattern in a college test pattern okay pattern concept the pattern concept uh let’s try to understand how the pattern concept is working and after that I’ll Implement in a practical way so the pattern concept suppose I have this box okay I want to show you pattern here is a star here is a star here two star here is a three star and here is a four star okay as we know that uh our index in a python is always start with a zero so the index of the Python will be can we change the color yes we can change the color yeah so the index is always start with 0 0 1 2 3 so this index is also start with a 0 1 2 3 so the basically I want to display the pattern in the order but this pattern is made with a 3 cross 3 Matrix right so first you have to make a 3 cross 3 Matrix and then apply the logic there so let’s understand it okay how to remove okay so let’s understand it here first I’ll create 4X 3×3 pattern for each in range which is start from always zero so if you’re not passing anything to always start with a zero and I want to reach still three then I’ll write four colon Print Star okay when you print star it will be showing like this uh the one line uh like um the vertical way is printed here when you just apply the for Loop inside that for each uh better to pass rows and column wise the first one is rows right how many rows you have to print one 2 3 4 so here I’ll pass R here I pass a c for c c for column R for rows let me again explain that this is called as a rows and this is called as a column okay column 1 2 3 four row one row two Row three row four okay this is the way we are using all right so I’ll just use a row basically row and column in range pass the value is a four and again colon and just print it so when you just print there is a 4 cross 4 Matrix obviously it’s not a 3 cross 3 it’s a 4 4 cross 4 Matrix so I’ll remove uh this one this one four cross 4 Matrix because there is a four call uh four section is there python is starting from the zero that’s why uh I make it 3 cross 3 but actually it’s a 4 cross 4 okay when you just print it then you’ll getting the 16 stars but it’s not in a cube format why it’s not Cube format the reason behind that everything is a printing in the next line so in a print there is a keyword is a end keyword is available that by default is a sln if you want to know you can just shift click uh click anywhere inside this bracket and click shift tab you’ll find it out what is the default values available which is slash n when you just remove the slash n with a space okay by default is a slash and will you remove it and make it is just a space it will will be the horizontal V but I don’t want all the stars is the horizontal V I want only this all four values is the horizontal V the next all four values the horizontal V the next four next four like this so what I will do I will use the column is a horizontal way but the rows I will use as a vertical way if I’m not passing anything the automatically the end is equal to sln is there so when you just run it automatically four cross for Star will Beed so till here we understand now I want to make the pattern so make the pattern so it’s very simple how can we do that see here you can understand um yeah here you can understand the first point first point is a position of the 0 and zero so what exactly is happening the the row is taking the value is uh first one first is taking the value zero and uh then it will be enter in this this for second for Loop that will also taking the value zero right so that’s why the printing the position of the star zero the next time again going back and it’s taking the one that’s why it’s one 2 3 so I don’t want the second for loop as a four I don’t I want the dynamic based on the previous one okay I want to make a dynamic based on the previous one let me just remove okay so how can we perform it so basically so whatever rows is coming I want so after that this one this part should be only one so that it will be print only the position of zero and it will be ended and back to the for loop again okay back to the for loop again and when this row become a one so this one should be two let’s make it plus one this one should be two so that’s why the column will take only zero and one okay take you only zero and one so when I just run it we showing like this and when the row become a three so this portion become a four when this become a four when this one become a two this become a three if it become a three then it will be print one this in the position of 0 1 2 so that’s why is printing the 012 and it will be reach till four only that’s why showing the push star like this you can think about a different way of the uh opposite star so again don’t try to memorize it any stars try to understand the how the star pattern is working the reason behind that the star pattern is there in your syllabus because with the star we can easily understand how the nested for Loop is working how the nested concept is is working because the star is not only display with the for Loop we can we have other uh concept is available to display the star let’s discuss the apply the count block if the username and password is fail in three times so we already generated the code where is that yeah this one so I want to apply the block if the username and password fail in a three times So currently hurry if I’m passing hurry 1 2 3 and showing the value is logging successful what if the uh login uh your username and password is wrong more than three times so I’ll apply the count processes here count is equal to Zer and in every uh wrong username on a password it should be increment where is the username and password wrong here else the password is incorrect so I’ll just make it uh count is equal to count plus one and password is incorrect um one this attempt is remaining attempts are remaining okay so I’ll just make it here F so uh let’s do it in case if my password is wrong let me just see that uh the statement is correct or not um hurry and password is wrong then it’s showing that password is incorrect only one attempt is remaining so this is not a right way okay okay hurry 1 two 3 so what I’ll do better I’ll just fix it the uh three times if your username and password is wrong is more than three times then the process should stop for particular duration I’ll just make it minus okay then it will showing that uh it only three times is remaining only two times is remaining this kind of you’ll get only three attempts okay the same statement I’ll apply for if the username is wrong my username is wrong and I’ll make it comma uh this particular attempt is remaining okay this particular attempt is remaining all right so okay this okay so if my usern is wrong uh all right I forgot to write F this is string formatting if my usern is wrong two attempt is remaining one attempt is remaining zero attempt is remaining in the sense is the last one it should be stop it here so I have to put one more condition if the count become a zero I have to stop it what I can do it here uh so anywhere like uh the breaking the loop if my loging successful I can also write L if Al if we can write Al if the count is equal to equal to zero the time please wait for 10 second currently I’ll just write in the 10 second okay 10 second and after that process should start again all right so I’ll do the same process for uh this is for password I’ll do the same thing for username as well I do it username as well but the your process should stop for 10 second so how can we do that so there is a like library is called as a Time import time okay so you can directly write the time do slip your process will stop for particular duration so I’ll do it here time do slip for 10 second so automatically it’s a second how can we know that you can just write time. slip and bracket you can just check it what exactly is there it’s a second I click on here inside and click shift tab this option is available in the Jupiter notebook only and if you have any other ideally you can find it out other options okay let me just remove so the process will stop for 10 second and after that it will again start so it’s on you if you want to put the timing you can okay so currently it will be stopped for 10 second process will yeah only two attempts is remaining one attempt is remaining I’ll write hurry password I’ll make it wrong this is the last attempt zero attempt is remaining that means there is no any attempt this is the last one I’ll write it here uh hurry password is hurry wrong let me just write it just wait for 10 second the process is still running it’s not like that your process is totally end up it’s running for 10 second it hold for 10 second you can increase it now it’s ask asking the password again hurry I will write it and again it’s waiting for 10 second because it’s already been uh uh already been you know um is a kind of uh uh already your count was Zero that that’s why it’s waiting for 10 second but it should not be it should not be after 10 second it give the three times option right but here I’m passing the wrong password still is waiting for 10 second so that one glitch is there there I have to just change it uh it should be make it zero again uh it should make it uh three again all right so I use it hurry 1 2 3 and let’s make it yes sleep for 10 second and after 10 second after 10 second Make It Count is equal to is equal to 3 all right Make It Count is equal to is equal to three and it’s here uh where is wait for 10 second and the same thing we have to apply for password as well so this is for username I think yeah this is the username I think so we have to do for oh sorry not here it already there just count is equal to is equal to 3 not is equal to is equal to is equal to is equal to is a compar comparing it’s assigning the values so let’s wait for yeah one attempt 0 attempt there is no any attempt is remaining I’ll pass the hurry I’ll pass the wrong password it’s wait for 10 second if the 10 second will end up then again it should be Q next three attempts okay so let’s see yeah two attempt is remaining one attempt is remaining hurry 1 2 3 so this way we can perform the operation with a Time basis okay so you can also apply the different different operation this is from the totally scratch in a real life we just adding some front end otherwise the back end we are applying the same process this is the good way to learn our programming language all the concept try to always connect with the real life so function is nothing but a block of organized code where we can reuse your code again and again use for performing single related actions so we’ll also discuss in a practical manner so let me first show you how the function we are writing in Python programming language so we are first defining is a def def keyword we are using to define the function now after that function name like I’m writing the hello you can write anything any function name and then parameter we are defining like parameter 1 parameter 2 parameter 1 parameter 2 right and important to you have to make the uh colon after that it will taking a four Space 1 2 3 4 automatically taking the four space when you click enter and you can write your uh function body okay whatever C code you want to perform you can just write it but function will run when you call it so here when you just calling hello when you’re just calling the function and if you define the two parameters so you have to pass two arguments okay so suppose I pass the five and six that is the arguments so the five will store in the parameter one and the six will store in the parameter two okay sometime you confuse that which one is a parameter which one is the uh arguments so this is not a much complicated thing right so let me just remove it I also Define the same like here I Define the function with function name there is a three main parameter and after that when you just writing the function name the argument one will we passed to the parameter one so parameter we are defining arguments we are passing so hope this one is clear right so argument two will pass to the parameter two three will pass to the parameter three so in the function there are so many uh types are available not so many four main types are there so the very first one is a predefined function and predefined which is system is already given the function we are just using it right mean Max int print whatever you you already use it that is a predefined function you just just call and pass the values you’ll get the answer okay I’ll give you one small trick to Def to identify which one is a function see if you have a statement and after that this bracket is there that you call as a function okay so mean is a function Max is a function print is a function after that is bracket should be there because that function is a calling all right so we can also make the function which is we are calling user defined function that means we are defining the function the third one is anonymous function we’ll discuss in um practical manner a function without name so here you can see the uh you can see the def and function name so we can create a function without name but this have a different purpose first we’ll discuss the purpose and after that we’ll show you how we can make the anonymous function the last one is a recursion function that things we’ll also discuss let’s jump on the Practical Implement implementation how we can uh uh function can perform in practical way suppose I have a very small program a is equal to 5 b is equal to uh 4 and C is equal to a + b and uh I’m just printing and just print C and you’ll get the answer is 9 5 + 4 is equal to 9 but here if you want to uh just addition of different numbers for example I want to add 15 and uh 28 so every time you have to change this like 15 here and uh uh 23 so this is very simple program just imagine that if you have very complication fun any um equation is there like like I need annotation yeah okay uh like um y = x² + 2x this is the function uh this is the one uh mathematical function this is not a programming function okay I want to create this program like if Y is equal to x² + 2 into X okay this is the function and uh I Define the x is equal to uh 15 and when you just print it okay and uh pass the value is y and you’ll get the answer is 255 and in case if you change the values is 25 you’ll get accordingly this answer but imagine that this function I want to use somewhere else this mathematical function don’t be complic don’t be uh confused I I’m talking about this is the mathematics function and which function we are talking about here which is a programming function okay here I write a mathematical function programming function I didn’t started so suppose this mathematical function I want to reuse any other places to make a different applications uh so it will be very complicated to to to call it or else you can just copy paste and write the code again so this is not a right solution so what I can do I can make one block and call this block again and again right so I have a different uh equations instead of saying the function I’ll say the equations that will be better okay uh Z is equal to y + 2 y + 28 this this is the one of the equation is there so what what I’ll do I’ll just write here Z is equal to uh and different places I want to use it y + 28 so I’ll get the answer z I’ll get the answer here 709 uh 703 so here when I just change the value is like two you’ll get the answer is eight the next time value is change accordingly but I have to use this uh program again and again so better to better to uh you know I have to run it then you number will change so better I can make a block and call that block again and again so let’s make a block is a depth EF and function name is a equation equation okay as I told you bracket is very very important parameter it’s on you if you want to Define you can otherwise you can leave it and uh like equation is the equation is y equal x squ plus 2X you required X as a input so I’ll just pass it here parameters now your block is ready I’ll just print it y okay I’ll just print it Y and I just run it you’ll not get any answer function have a rules without calling the function function will never execute okay so here I can see this is defining the function defining the function okay I want to call it so what I’ll do I have to define the values X is equal to some some something or else you can directly uh pass the values equation I’ll pass the value is five so you’ll get the answer accordingly so this one we are calling the function this section calling the function okay this SE is calling the function in case if you change the value is like uh 25 you’ll get the answer accordingly right this is the way our function is running when you’re talking about how the block is executing what is the processes behind that so process is running like this it will check the line number one okay and then go to the line number uh sorry line number one is just uh comment so obviously it will start from line number two which is a comment right comment will not execute it will just run not giving you any any answer so first we’ll check the line number two and after that directly jump on the line number six if the function is defined there it will never enter inside right and uh after line number six it’s calling to the function then again back to the line number so now you are calling so that’s why it giving you okay yeah that’s why is giving you uh it it will be uh giving the line number two again okay and then it enter the line number three like this and then line number four as well so this is the way our function is working right it’s not like that top to bottom approach so yes python is working the top to bottom approach but if the function is there it will first checking that where the function is calling and after that entering the inside you can understand one of uh uh I’ll give you one one more example the use case of uh this function let me first remove it okay suppose you are making the calculator is a simple example you making one calculator addition subtraction multiplication and division there is a four different different function you define it but at a time you are using this subtraction so other things will not execute because you define inside a block if you define inside a block so this one this one this one you can save your memory you can save your time because the other program will not execute only the sub program subtraction will execute so imag that if you have a big project and in a project at a time everything is not running so part part wise the values are running right which one is required you can just call it but you have to Define you can’t remove it and you can’t write the code again and again that is the reason function is very very important so in a function there is so many topics is there which I have to discuss here so very first topic is a return statement okay written statement other next topic which is uh we can say um so user Define which we already did equation is a user defined function and uh um uh we can say uh system defined function a predefined function we already used it like in float print these are a user uh these are system defined like a print I’m using here as a function which is system defined the two type is done and the third type which is anonymous function function and the fourth one which is a recursion function the fourth one which is a recursion function okay recursion function there is a these topics we have to discuss let’s discuss the about a return statements let me just remove it okay so return statement uh I’ll take one example like hope you remember about math there is a um library is called as a math and if I want to know the square root of some particular values like math.sqrt I’m just giving you the use case why written statement is important and what kind of problem what kind of problem is solving by the written statements see if you running any any any if you’re learning any topic first try to find it out why you are learning what kind of problem is solving this topic so like sqr I want to know the square root of 25 you’ll get the answer answer easily which is a 25 and I can store it somewhere I’m storing in the variable R and uh print this statement as a um s root of 5 is R very simple statement is there uh square root of 25 is 5 very simple statement okay I want to create like this because here the sqr sqrt is a kind of method so method and a function both are the same okay here is little bit confusing like method and function both are the same or different so method and function both are the same let me just Define it method and function so you can say method when when we Define a function inside the class we are calling method okay so here we here I created the equation which is not inside a class class I am talking about object oriented class and object which is this one is not inside a class so we are not calling equation as a method we are calling function only so here why I’m calling square root of his a method because math is work like a class uh math is a work like object which class is defined somewhere okay with the object I’m calling method so method we are defining as a function don’t be confused method is a defining as a function but only difference is that it’s inside a class so if I I have a value is like a I have a value is like a def um ABC dep ABC I Define it okay and inside you write a body so this one ABC is a function but if you define like a class class name class name is suppose U uh temp okay class name is a temp and you define it and inside that there very uh important uh parameter you always right is a self so now here the ABC become a method why is a method because you are calling like a t is equal to Temp okay you created the object here and the T dot ABC you can use it this ABC with the help of object which is a t that is is the reason I was saying sqrt is a method so now currently you’re not aware about a object oriented in a python so you just consider that method and function both are the same as a defining purpose only difference is that that method is inside a class so currently we are not inside a class so we’ll call as a function okay okay so I want to make uh the program like this okay suppose I created the function which is the Squire root okay let me change the name the same like I want to create it X as a parameter and I’ll make it it it’s a square root so um answer is equal to x to the power of x to the power of 1x 2 anything which is the power of 1 by two which is nothing but a square root okay let me again clear it like a square root of 5 which is nothing but 5 to the power of 1 by 2 or we can say 5 to the power of 0.5 the same thing and you can write it here just ANS okay when I just call this function as square root and pass the values is 25 you’ll get the answer is 5.0 which is a correct answer but can I write like this can I store it in R variable yes we can store it can I write like this print um square root of 25 is um R can I make like this you’ll getting the answer is 5.0 here but here I’m getting the value is none let’s try let’s understand it here here we print it the print meaning is that the the actual work of the print is nothing but displaying the values that’s it it’s not doing any other thing it’s just display the values so when you just um uh you know call this function here is square root 25 when you just pass it here you’ll get the answer is ANS is get the answer is five and here just display it’s not doing any other thing it’s not store any values so what I want the square root square root should store some values so that it can be defined it here so we can solve this problem via written statement see the word saying everything return meaning is that returning the value to the function so inste of writing the print I will say return so I’ll return this value to the square root so then you’ll get the values as it is like this okay so return return basically can be used any other function this function when you just calling it this function when you call it I can store it somewhere else so that we can reuse it hope you understand the return statement concept okay let’s jump on Anonymous function let’s jump on Anonymous function a function without name is called as an anonymous function what is a mean Anonymous function that we are defining we are defining anonimous function with Lambda keyword with Lambda keyword okay Lambda keyword what is the um you know syntax of that let first discussed okay we are writing the Lambda Lambda and we’re passing the arguments like uh a sorry a comma B and then you uh doing some operations let’s understand it here so syntax is not much important but you should know that how we are writing so just we have to write Lambda what operation you want to perform I want to perform the operation of um a cube of any numbers yeah we can say U addition of XY Z so I have to Define it X comma y comma Z colon colon what operation you want to perform I want to perform is the 2 into x + y + z that equation I want to perform when you just run it we’ll find it out the function is created okay function is created how can we um uh you know call it this function right function is created it’s saying that function you can um defining the function without any name here there is no any name but if you want to call it you have to store somewhere I’ll store in the result variable I’m directly storing it there and with that variable I can call it result is equal to um result is equal sorry result when you just run it it will saying that a function you have to pass some parameters I’ll pass the parameter 5 comma 7 comma and 3 I’ll get the answer is 20 because this one is taking as a argument these are the parameters and these are the operations I perform it the question is if we already have a diff param diff parameterized function is available in um in this topic then why we required this Lambda why we required answer is here let’s understand the example where we can use it this kind of function um suppose you want to create one function which is very small and you want to call it again and again why you required the separate function if you function is very small you can just Define it here call it the same time okay how can we use it let’s define one variable which store many values okay yeah this values I want to uh take only the um you know even numbers so many method is there there is one a famous function in build function is there which is the filter function I can directly use it filter function okay filter function so when you just uh put your cursor in between and type shift tab you’ll find it out suggestions so first you have to define the function and what you want to iterate it I want to iterate a and function you have to Define it okay let’s define the function name is a uh even this is the function name even and a I want to pass it so even you have to Define it right even let me Define separately def even and I’ll pass as a any parameters because whatever Val U value you have the whatever list you have iteratively one by one it will be pass and giving the answer so if x is a modulo of 2 is equal to equal to Z that means it’s a even number and should be written written as a x okay when I just run it oh so compressed so I’ll just use the list and I’ll get the answer I’ll got only a even number from this section so there is a 13 and 21 is a odd number but here I got only even number with the use of filter function but you can see here this function is very small so you define separately and it took a three line more space and obviously the calling from the different function from the different places obviously is a memory consuming so what I can do I can use the Lambda keyword so I can use the same thing filter I’ll pass the values here Lambda X and then condition I’ll apply it X modul of 2 is equal to equal to 0 and what you want to pass I want to pass a just pass it a that’s it and make it list and you’ll get the answer is yes same thing so here I defined with the five line of code here I Define the one line of so both are doing the same thing only difference is that with the help of Lambda keyword I can reduce my number of cod so that I can save my memory and space because this is your learning phase start now you are just learning but when you go in the project perspective so we have to take care about the optimization as well your project should not be take much time your project should not be take much memory so Lambda is one of the good solution so one thing the definitely you are uh in your mind uh in case if your uh number of code in a function which very big right like uh the function is very complicated here is just three line if you have a five line six line that is okay if you function if your function itself have almost 20 to 50 line so in that time should we use the Lambda keyword the answer is no so Lambda keyboard is using if your function is very small and you are it iteratively calling it the time we are using you don’t need to Define it separately you in that function itself in that predefined function itself you can use the Lambda a function call itself is calling a recursion function let me also write it here a function call itself calling recursion function let’s do it in a practical way suppose I have a function uh function name is U um process okay and inside the process this normal print statement is there hello world all right so when you just call it this process function process then as expected answer is a Hello World here the process is working like this your function um work like this line number one then four again line number one two after line number two in case if I’m calling the same function again same function again the function name is process same function again so what will be happen so it will be call the function again one line number one line number two and three again one 2 3 again 1 2 3 1 2 3 1 2 3 it will be like a loop but actually it’s not a loop it’s a recursion recursion have a limit what is the limit let’s discuss it first so here you can see uh the process is stopped now means there is some limit to stop when you scroll down you’ll find it out the maximum recurs and depth ex exceeded while calling the python object so there is a limit so limit you can also find it out with the CIS module Sy CIS means system CIS do get recursion limit get recursion limit get recursion limit you’ll find it out how much limit this recursion have so every system every system in the sense uh if you’re working on a Jupiter notebook and if you working on a normal python if you’re working on a py charm if your environment is a different then recurs limit can be the different but you you can use this command to find it out the recursion limit I can also increase and decrease the recursion limit so increase the recursion limit is very simple just you have to pass set recursion limit and I’m passing it here 8,000 okay 8,000 and later when I just check that what is this recursion limit it will showing the 8,000 but the question is how can we decrease it suppose this H world is showing 3,000 times I want to check one the five times or six times so that means I’m decreasing the rec person so there is um one beautiful feature is there written statement with the help of written statement we can perform the recursion process suppose I have a number is n and uh here I pass the value is a five and every time when you call this process when you call this function I’ll make it n minus one at the same time I can also print that n as well so that I can track it how much value is there so when I just run it you can see here the 5 4 3 2 1 and and it’s going till down there is there is a limit is a 3,000 so it will be going down and run it again and again right so what I can do hit here so when you scroll down down down down it’s a 7,000 it’s going to more than 7,000 the reason behind that I increase the limit which is the 8,000 right I increase the limit that’s why it’s going till here after uh 7,000 7,000 something and is going this statement is maximum recursion depth exced while calling the python object my target is that I want to stop in after five steps so I can use it here the condition if my n become a zero that time I will return return just um uh recursion done okay the statement is is a recursion done that’s it so because you know that after the return statement any statement is written there it will never accept okay so when I just run it it will going till uh 1 only what exact what is the meaning is that it’s not acceptable after the return statement like I think I already discussed but again I’ll tell you in the short way the dev hello there is a function and if my number number is a = to 4 and B is = 8 and return is equal to a + and before return let me just use the print print A+ B and when you just call it hello you’ll get the answer is 12 and after the print statement uh my statement is there um um hello world okay this normal statement hello world after 12 is showing that hello world but in case if you use the written statement for a plus b return statement for a plus b hello world where hello world will never display the reason behind that because after the print after the written statement whatever things is there will never be execute okay if I’m writing the normal ABCD any thing so it will be never execute so that is the uh functionality of the return statement we will use this functionality in a recursion function to stop the recursion so here the recursion is STO because when the number become a zero when a number become a zero and at that time it return the recursion done and when the line number three is executed after that whatever you wrote it will never execute that is the reason it’s not going in the next one but here the question is why the recursion done is not showing right why the recursion done is not showing the reason behind that it’s a return if it’s a return so you have to display it okay uh I have to uh I think process I have to also return yeah process app to also return that time uh whatever is returning the values it will also display so that that’s why the recursion done is also showing it here one um uh famous example is there uh uh for a recursion function uh which is mostly um you know interviewer is asking this kind of question what is the factorial number okay let me just explain what is the factorial number so factorial number you can understand like this uh it’s a application of application of recursion function okay so with the with the help of recursion function we will use it so instead of write this the factorial number with the recursion function okay recursion function so we’ll use the um recursion function uh yeah I need a pen so hope you know about a factorial number I’ll give you a short way so suppose I have a factorial number of five factorial number of 5 is equal to 5 into 4 into 3 into 2 into 1 it is going till one only it’s not more than that the answer will be 120 okay 54 20 22 3 60 602 120 but we can also write like this the 5 factorial is equal to 5 into 4 factorial till 1 till one it’s not going Beyond one it not accepting the zero because if you multiply with the zero you’ll get the answer is entire zero and uh if you have a six factorial that is a five uh 6 into can I remove okay 6 into 5 factorial if you have a 7 factorial 7 into 6 factorial so we can also do like this because the 5 5 into 4 factorial the meaning is that 5 into 4 factorial is 4 into 3 into 2 into 1 so we will use this kind of uh application uh to understand the factorial number like I’ll create a function Factor name is a fact fact only okay and I need a number I’ll Define the number is n colon I’ll return the value as the N into means N means exact number the four uh yeah if if if you take the example of five factorial then 5 into factorial of four fact of 4 which is the n minus one right if I pass the values is six the 6 into factorial of five if you pass the value of 7 7 into factorial of uh 6 but again so when I just call it here the fact which is value is a five I’m expecting the number is 120 the problem is here when you just run it you calling this function again and again again and again there is no any limit to stop it so that’s why it’s not defining anything it’s not showing any values the reason I’m not printing anything I’m not displaying anything every time is returning the values so I want to put the limit so that I can get the exact number so how can you define the limit so limit we can Define like this if number become so again you can you can see here it’s maximum recursion depth X exceeded because it’s not showing the number it’s not showing any values the reason I return the statement not to print so if number become a one for that time I will return one if I return return one if you multiply with one then will stop it’s not going any other things so when I just run it get the answer is 12 okay so you can take as a input uh uh okay in put okay I and PT okay anything you can take the variable input enter the number for factorial vectorial okay so I can Define it and that should be the integer that should be the integer here I’ll pass I and PT I n PT so when I just run it it’s asking that what enter the number for factorial I’m passing the eight is here I get the factorial is the 4320 so this way we can understand the recursion function so package and module this topic is playing the very important role for uh all the uh Frameworks and advanced level up Python Programming because uh whenever uh you enter any kinds of uh any framework work any any technology like a web development or data science so many libraries so many modules are there so here we will learn that how modules and package we can make it we can build it like uh from Custom based we’ll try to make the modules and packages so first one is a module module is nothing but uh any python file which have a py extension is called as a module so you have so many python file you create but sometime we creating the Jupiter notebook file that Jupiter notebook extension is i p y and B so that is not a module so python file should be in the py extension that will consider as a module and the module can consist of function classes variable anything because that is the file inside a file so many things we can write it we can write a function we can write a class we can write a variables anything we can write it there and what is the uh package package is nothing but one uh directory we can say where consist of the multiple modules but there is one special file is called as init so that name is init it should be available there that we are calling packages so for example I have a file which is the uh a which is the file name is a. py B do py c. py so these are all a modules but make sure that there is one more file should be underscore uncore init uncore uncore py if this one is also present and it’s available in the particular folder uh suppose the folder name is okay yeah suppose the folder name is a uh for example folder name is main so main is nothing but a package which consist of the multiple modules so let’s understand in the practical way I’ll create the multiple modules and I also explain you how we can create the packages as well like this there is a package I can also create the multiple packages as well for example like this example here so open change close these are a modules even in it is also the modules that is consist of the one folder which is called as a image Right image so image is a package the likewise the image there is also one more package it’s called as sound we can also say the package but the problem is I here I mentioned that the sub packages because you can also create the init file and make the one more folder which is the game so can you say this this one is a package this one is a package also become a package but the problem is here the main folder other folder inside that folder two folder is also available we can say it’s a package uh it’s a sub package okay so in case if you calling this one is a package that’s not wrong why I’m calling here the sub packages because it’s consist up the one more packages that’s the only reason okay someone is also calling this one is a library someone is also calling this one is a library so library is nothing but is a collection of the packages so if I clarify this what exactly it meaning so module is nothing but a consist of the multiple function and classes whereas the package is the collection of the modules whereas the library is a collection of packages or packages we can also say that it’s a collection of sub packages let’s do it practical implementation of package module and library in uh uh you know in a coding way so you can open your any ideally I’m just opening the visual studio visual studio and open one particular location where you want to walk it this package module and Library concept okay I’m just opening one folder uh yeah this is the location here nothing is available okay okay I think I selected the files I have to select the folder yeah exercise now it’s done I already selected here all right let me close it unnecessary files yeah I’ll I’ll create one file python file so file name is calling. py okay so in this file I’ll try to call any other modules let me first create any modules here even the calling file is also called as a module but the variable file is not a module because the extension of calling is a py where is the extension of variable is i p y and B so that is a jupyter notebook file both are a different so only calling is a module let me create one more file which is the load module yeah we can say load file I’ll create some function def info and uh some statement here print this is load module okay simple because variable uh sorry uh module is nothing but a collection of of any function any variable any classes anything you can write anything here this is the function I can also Define one variable here a is equ Al to 55 and uh let me also Define one more function def add and pass some uh parameters r n comma M okay and uh R is equal to result is equal to n + m and I will return return R okay so there is two function with one variable let me just write it first here two different function and one variable okay so I want to call it this function with different file so calling is a file both load and calling is available in the same location I want to call the load module so how to call any module we are using the import keyword import load so when you just use the import load let’s see what will happen you can run the file here so hope you have completed the configuration if you not then uh you can just refer uh my video for uh installation of vs code in case if you’re facing any issues you can also uh you know text on a chat all right let me just remove the unnecessary things so nothing is printed here because I didn’t print anything let me also print just normal statement print um hey that’s it hey so when I just call this this function uh this um you know this load module when I just call it here you’ll get the values is here only here but I want to call the info function I want to call the add function so if you if you want to call the ADD and info function so load do info you have to write it so when I just write the load. info you’ll get the answer accordingly so first is a he because I import it and uh in load module the statement was already there here and inside the function info the statement was this is the load module so that’s why it’s sprinted I want to call the a variable as well because I Define one variable which is the 55 a as a variable and 55 is a value so load. a you can also call it but uh that value you want to print it previously the info function is printed something here is just Define the values so I can also print it and see what exactly the values so I’m expecting that the value should be 55 so yes value is a 55 we can also call it that variable as well we can also call the function with the parameter to passing the arguments so load do add and pass the uh arguments because two parameters defined it here n and M I’m passing the parameters three uh passing the argument as three and 8 so I’m expecting the answer should be 11 so yes okay so here is not printing any answer the reason behind that I return the values I can store somewhere I’m storing the ens wer answer and that answer if I want to print it print answer so you’ll get the answer accordingly that is the 11 so hope you understand the concept of module and when you go into the next topic which is a package that is a collection of module so when I just create one uh folder inside that I’ll create a multiple python file and uh one init file as well then it’s become a package so let’s create it I’ll follow this structure load I created here so let me create other two files which is play and pause and I’ll put inside the sound folder that’s become a package okay let me create one more file which is the play. py I’ll create one function name which is the info let’s create an info so that you can remember it everywhere I’ll write the function which is the info it’s not compulsory that you have to pass the same function everywhere just for remember purpose I’m just writing here print this is but the statement I’ll make it different this is play module okay and let me also create one more file uh pause. py def info I’ll make it print this is um pause module okay all right so this is the files I’ll create one folder for them the folder is sound I’ll create it the folder name is sound sound I’ll pass all the modules play load play and pause inside that function inside that folder folder name is sound but here the sound is not a package because I didn’t Define one a file which is the init so in it is nothing but a Constructor so what is a Constructor so Constructor is whatever statement you write inside the Constructor it will automatically call you don’t need to call that function even the Constructor concept will also come in objectoriented programming that time I’ll discuss very details way but here you can just understand I’ll create a Constructor the file name is init.py that I don’t need to call it it would automatically called okay so inside that I’ll create a file which is theore uncore netore dopy so let me just write it all the statement to everywhere first uh let me just write it okay so so first in a Constructor let me write something so the statement is this is um sound package simple this is sound package and in a load I already defined so many functions pause let me just Define a def info the state m is this is okay pause module that’s it and play is already there is a play module so now I can say the package is considered as a uh so sorry sound is a considered as a package so now I want to call the info function which is available in a sound package because the sound is a completely a package let me also delete it here because it’s already available inside okay play uh load play and pause so yeah so sound is a package so let me make as a comment all right okay so if I want to call the info function which is available in the sound so you have to first call the sound okay you have to call the sound so when you just call the sound so let’s see what will happen so answer is showing that this is sound package so how it’s happened because I didn’t call anything I just import the sound but this Constructor is automatically called which is the in it so inside the init the statement is this is the sound package so that is the reason the first statement is this is sound package okay so again that info function is available in a pause module for example let’s let’s take it a pause module I want to call the info function which is there in the pause module so I have to go inside pause okay pause so you can pass as a reference as the uh a you can Define any variable so later a do info so when you just call a do info which is nothing but a sound. pa.info so here you’ll get answer which is this is a pause module there is also one way uh another way to uh call any function from sound import pause because the previously you are entered the sound and then enter the pause so here from the sound you enter only a pause so this one is a better way to call any modules any function okay so I’ll just directly pass it here pause doino that’s it you’ll get the same answer so in a real life whenever you importing any uh any packages any libraries like pandas numai anything so sometime we are using the from sometime using directly import so now you understand why I’m using a from why I’m using the import directly okay so now here the sound is consider uh working as a package so I can make sound as a sub package as well so you can create one more folder which is the image let me create it and later I’ll show the hierarchy as well so by the way this underscore Pi cache it’s just generating the cach that what function you called it so here you can see I called the pause I called the init so that is the reason it’s generated so previously you can also see what I called I called the pause and in it so that packages are generating here is also the package I call the load so that is the cpython so you don’t need to care about that it automatically generating so let me also create one more folder which name is image okay that folder name image image so inside that image I’ll create a three different file which is the open change and close with in it let’s create it from here here it will be quite faster first I have to create the Constructor okay and it then I’ll also create the uh open. py then I also create change. py then also create I think close is there open change and close py okay three files I generated here so sound uh sound is a package now here image is also considered as a package because I created The Constructor I put inside multiple module as well so I’ll put this packages this image and sound I’ll try to make it as a sub package with game as a main package okay so I didn’t write any function there but the same thing you can also follow for image as well so game here consider as a main package if I’ll pass the image inside a game let me just drag and drop control C cut I’ll pass inside image okay I’ll replace it the same thing and make sure that if you have the image and sound as a package if you want to make game as a uh library or a main package you have to create one more file which is the init then it will be considered as a package or Library underscore init.py make sure that double underscore in the both side so now game game is a library image and sound is package or we can say game is a package image and sound as a sub packages so you can say anything let’s back to our calling function yeah yeah calling modules where we are calling the different different uh uh functions so if I’m writing the same statement from sound import pause so do you think that it will be work absolutely not the reason behind that because previously the sound is a aable sound folder is available in the same location where the calling function was available yeah calling uh file was available so when I just run it here it will showing that the no module name sound reason previously sound was the same location now sound is available in the game location but if you’re using the game Dot Game dot sound because it’s available inside a game so it will work right it will work so let me also write something in uh in it of game currently game Constructor don’t have any kind of statement that is the reason is not printed here let me just write some statement in a game Constructor in init.py so the statement is this is game Library okay game library now so when I just call this again function calling function so the statement is also here this is game library because it’s available inside uh um inside a game library that this Constructor in Constructor is there in the game library that is the reason is printing here now uh this game library is available in the same location where this calling Fun calling file is available imagine that if the game libraryies is available in the different location if you want to call it again so it’s showing the error right so in that situation we have to find it out one a global place where I can put my library and I can access anywhere in my laptop so let me just change the place of the game Library So currently is here I’ll just copy cut not a copy directly cut it here and let me just put it outside because here the calling file there is no any game Library so when you just run it it will showing the error no module name game so how exactly this working because uh you know whenever you importing any inbuilt library that is pandas numai mat plotti anything in that situation uh this library is available in the global location so that we can access it so let’s take the example of fondas and how exactly is working So currently yeah so currently uh I have a many libraries so n pandas M plotly let me just open first the command prompt command prompt here okay so I am using the Anaconda prompt so I’ll just open the Anaconda prompt here so I already have a pandas but still I’ll just write a pip install pandas okay okay so it will showing the statement is that uh the pandas is already installed and it’s also showing the statement um you know the location where exactly the install C user your um you know PC name and anakonda 3 lip and side packages so the same thing here there is one um library is in build Library which is a CIS CIS is a system so let me just open the python here directly sorry here anywhere you can open it I can write it here okay so import CIS system sis. paath so it will showing you the path where exactly all the libraries and all the python packages are available so this is showing the complete list let me just open it one by one for each in. paath 1 2 3 4 print each Okay so that will showing the one by one so you can go to any location so main location is this one lib lib is Library let me copy it and open your uh Windows Explorer File Explorer and paste it there in the URL see user your PC name Anaconda 3 and lip so it showing the location here I want to see that where exactly the pandas is available you can see it here the pandas is available C user PC name anakonda 3 lip and site packages so here definitely site packages one folder is available you can just search it site packages yeah site packages and you can also search the where exactly the pandas pandas yes here is the pandas so you can find it out other libraries like a matplot li numai numai yes you can see the numai and matplot Le if you searching you’ll also find it out somewhere M plot lab okay so exactly if you are importing the pandas numai mat plot Li that all the code is coming from in this location if you just checking the pandas so inside a pandas sometime we are using a pandas do data frame so where exactly the data Frame data frame is a code so go to the pandas you’ll find it out somewhere go to the pandas and uh here is the API here you can find it out like so many things is available you can easily find it out there right so in the core I think uh data frame is available yeah you can see the reshape is available uh interchange is available indexes is available group by we are using D types we are using see inside that the lots of code is available so we are just using that particular code right so with the help and here here pandas is nothing but a library so inside a pandas so many packs of packages and packages is there with in it you can see with in it when you go to the inside of core you’ll also find it out in it somewhere okay in it in it is there see here is the init is there now so these packages libraries you can find it out where exactly is available with the help of CIS uh packages CIS Library so now the my target is that keep uh the game library is available in the local location I want to change it I don’t want in that uh you know local location so that if I’m access and we change the location so it will be showing that no module name a game so what we can do I can put this game folder in any of this location any of this location lib side packages anything I’ll directly put the lib let me go there yeah so Li so I’ll directly pass it here hope the game yeah it’s not available I’ll just put paste it here game paste it here so in your system anywhere in in in a system when you just run it it will not get any kinds of error okay still showing the error I think I have to refresh it kind of uh okay uh you can just access it uh let me just run it again it should not be it should be accessible okay let me just write it here import game okay it’s so okay let me just pass it in the side packages here is not working then I’ll just pass in the side packages side packages a lips or lips yeah okay let me just directly paste it here team okay go inside a lip and game okay let me just run it again here import G yeah this is the game Library so you can access it there is uh and the current location is my RT and my RT is the PC location and here I’m trying to access the game it’s accessible so here is not accessing why there is some some issues there is some issues let me just try to use it import game this normal import game so hope it should be available so I don’t know why it’s showing this error okay so there is some issues so better I can go in the particular location uh let me exit it so where exactly I’m working I can also search it here uh the location is the location is uh D recurrent learning python exercise okay I can just go there D CD recurrent learning Python and exercise okay so in this location I’m trying to just access the game libraries is it accessible or not so I’m just uh I think there is some issues in uh you know in in versions so I think I’m just using a different version here and different version there so that is the that is the problem because I just copy the path from a command prompt here so let me just import it here it will definitely work yeah here is working so if I’m just accessing game from game import sound dot play Sorry dot here I can’t use it uh but here I can definitely use it do sound import play okay so this is the sound packages that is also working here the play play do info yeah this is the play module is perfectly working fine here so if you put your libraries inside U uh the global location so you can access anywhere in your PC so p is working as a package manager which is responsible to install any kinds of packages or libraries so if you’re using the python more than uh 3.4 version so that means PP is already there you have to just use it so if you want to install a package like pandas so you can write it you can write it like pip install pandas that’s it so you have to write it pip install whatever libraries if you want to install just write it down so pip uh will install that particular libraries in global location in your system so there is one more topic is a name attribute I think you saw this name attribute in a Python programming language is many places so let’s understand it how we can use this name attribute so suppose if you have vs code okay if you have a vs code and uh yeah and I have one file which is a um load file load. py so the variable is 55 and DEP is equal to info and uh pass the statement is this is this is uh load module okay and one more function is available def add and pass to parameter a comma B simple and just um you know perform some operation result is equal to a + b and later return the values return the result okay now it’s done but I want if if I if my target is to create the complete a module which will be the load module but I want to test it that whatever function I wrote it that is perfectly fine or not okay so here uh I just want to um you know test it this add function is properly running or not so what I’ll do I’ll just call the function 5 comma 8 and when I run it so hopefully we will not get any answer because I return the values but when I just print the statement like uh print the statement directly so we will get the answer is 1 so which um I was expecting 13 now the answer is also 13 everything is perfect but here I just want to call the load module let me just call it import load okay and when you just import load let’s see what will happen so import load I just load the I just uh you know load the load module that name is the same I just imported the load module but here I was not expecting that answer should be the 13 because I didn’t call the add function here I didn’t call the add function let me just call the add function uh print load do add I’ll pass the values is 5 comma 12 so I’m expecting the answer is 17 so here I got the 17 but the same time I also got the answer is 13 as well which is wrong not wrong but uh I have to see that where exactly this 13 so 13 is basically I just printed here in the load module just for experimental purpose so if you doing the experiment so don’t direct don’t directly write it the experimental uh print statement so what will happen ke um if you write anything and import it somewhere so definitely that uh statement will uh call it so I can use it name attribute if underscore uncore name underscore uncore is equal to is equal to mean what is the meaning of main so main is nothing but main is nothing but the own file okay main is nothing but the own file so when I just run it here definitely you will get the answer is 13 okay I here you’ll get the answer is 13 why the 13 because I just passed the values is five and 8 so here I pass the condition if my name attribute is a main main is same file that time it will be called this function add but so what will happen when you just importing this file import load that time your name attribute is not a mean your name attribute is become a load okay your name attribute is become a load so how you can you can see that uh so here here when I just print it you will not get the answer is 13 you’ll get the answer is only 17 so let me just verify it ke uh that that time the name attribute is a main and uh uh when you just call the uh when you just import the load module that time is become the file name so print uncore uncore name okay I’m just writing here name attribute what is exactly the name attribute here so definitely here I didn’t pass any kinds of condition so that if you just use it in any other module in a calling Fun calling file so this one will definitely print so let me first print it here so when I just print it so here you’ll get the answer is the name attribute which is a mean all right the name attribute which is a main here I got the answer is mean but at the same time when I just calling here and uh run it so definitely this line will execute line number 10 will execute and when I just call it you will get the answer is load and what was the condition the condition was that the name underscore uncore name is equal toore main that means in the within a file it will be called but if you import it somewhere that will not call because the name attribute become the file name okay that condition I didn’t pass it here so hope you understand the entire uh package and module concept if you face any issues anywhere if you just importing something package Library anything so you can uh you know put the comment below we’ll definitely I I’ll solve your problem exception handling is a unexpected event is exactly an unexpected event which occurs the execution which occurs during the execution of the program that disturb the normal flow of the instruction so there is a lots of keyword is available to handle this kind of exception so the keyword is a try except else and finally so there is a four main keyword to handle the exception so let’s say for example uh this is the real life example how exception is occurred suppose if you’re running the car and car is suddenly stopped that can be any unexpected event maybe your engine failure maybe the petrol is finished can be anything but suppose if the petrol is finished the fuel indicator is giving the answer you’re giving the uh instruction that yes your petrol is finished find it out some nearby petrol pump so the fuel indicator is not solving the problem but is giving the instruction that this is the exact instruction you can handle via petrol so this is one of the example so let’s try to understand in the practical way like any unexpected event is occur to how we can handle it so open the Jupiter notebook I’ll just create a new file to better exception handling oh that file is not available yes the file is not there let me just create it exception handling exception handling okay suppose I have a very simple program a is equal to uh yeah a is equal to I’ll take a input okay as a integer enter the first number just normal program I’m just writing it later I’ll just create some issues and try to solve this B is equal to ENT andp put enter the second number okay and R is equal to a + b simple not a plus b a divided by B and I will print it R just normal answer yeah we can say I can also write it the proper statement answer is okay so asking the first uh first number that is the four I’m just passing it here second number is a three answer is 1.333 okay so it’s very normal program so you’re not expecting that you’ll not get uh you not get any kinds of error but let me run it program and 5 divide by 0 so that time it’s unexpected event is occurred so I was not expecting that I’ll get error right because it ask the question enter the first number I pass a five enter the second number I pass a zero zero is also the integer but I got the answer is zero division error so different different error is exist we’ll discuss in uh you know in pptq how many types of error is there but here the error is occurred so how can I handle it because 5/ by 0 that invalid it’s kind of invalid so if you using uh the calculator any calculator your mobile calculator you can also check it 5 / by 0 Let’s see what will be the answer the answer is showing that Infinity so if you check the 5 ID by 0 in your mobile maybe you’ll get a different answer maybe it will showing the invalid all right so different different statement is showing so instead of showing that red color error if I’m just showing the infinity that will be better so that is handle the error it’s not solving the error right so here uh so 5id by 0 the value is not exist so I will try to handle it if I’ll face this kind of issues how we can handle it so I’ll just use a try try means I’ll just normally try the normal flow of the program so normal flow of the program I’ll right okay so in case any exception occurred except except exception accept exception uh the time I can I can write it here print Infinity okay I can I can write it anything so if I pass 6 IDE 0 you’ll get the answer is infinity whatever you write it here you’ll get the answer accordingly so it’s not solve the error because 6 IDE by 0 the number is not exist but I’m just handle this kind of error if this kind of error is exist how can we handle it so in case the error is like here is saying that enter the first number by mistake I pass the value is uh T which is the string and here you’ll get a different error but instead of showing the error is showing the infinity that is like not sensible right so different different types of error is exist so here the common standard exception is available which is the zero division error which I just discussed name error it occur when the N name is not found and indentation error like if you conditional statement of for you are using the time is occurred and input output error is there eoff error is there so kind of some common standard error is available and uh we can handle it with their own name instead of writing The Only Exception so exception is just we can see the main class so where the different difference of classes is available zero division name error and indentation so let’s understand it in a deeper way in Practical implementation so here I use the try I use the accept there is a five keyword let me also write it here four keyword four main keyword try except try except and uh finally and what else yeah else else so these are the four main keyword is available in exception handling so try and accept we already used uh let me go first deeper here because the statement was that if I’m passing a t the still is getting the answer is infinity so in that situation how to handle it let me copy paste there in that situation instead of passing the exception I will pass it the uh exact error so in case if I uh face the issues okay you know so if I’m just pass it here okay T so I’ll get a value error so here I’m not getting the answer so because uh you know here I directly use the exception so when I just run it here separately here pass the R so it will you will get a value error so you can copy and paste instead of passing the exception I will just pass the value error so it will handle the one value error so I’ll write it here please enter any numeric number okay please enter any numeric number number when I just run it here and if someone is passing the any uh statement like T so it’s throwing the statement please enter any numeric number because I passed the any statement right so uh but uh if you’ll get any um zero division error then 5/ by 0 then it will showing the zero division error so again you can Define it zero division error separately okay accept zero division error so if you’ll face any kinds of um you know zero division issues so it will showing the answer is please enter any nonzero values okay so this one is perfect but here is very common question ke uh in the starting phase how you know that what kind of error will occurred because it’s a unexpected event right so in unexpected event how can we find it out the solution is that so whatever you know value error I know a zero division error I know so I can write it but your save side you can also Define it here except exception if zero division will not catch zero uh value error will not catch then exception will definitely catch because it’s a main class and inside that other classes available like a zero Division and value error so so here I’ll just write it down this normal statement invalid okay in case if value error zero division is not captured then exception will definitely captured so one thing is also here uh let me just pass okay you’ll get this kind of answer so one thing you can uh Define it here ke let me just copy the first one okay we can also pass the references as well so here instead of passing the infinity I’ll pass the invalid okay so invalid so if if someone is passing the W any U string values in an integer so definitely it will not convert it so it’s showing the invalid I can also pass the references as e and at the same time I can also Define it e so you’ll get the uh answer is T if I pass so you will get the answer is invalid even let me just Define it here uh colon so that it will be separate okay if someone is passing so it’s showing that invalid literal for integer with a Bas 10 so that means you have to pass any best 10 number but if I’m just passing any zero division issues okay so it’s showing that the invalid and uh the E is representing that division by zero so that error is also we can check it with passing the references um here is not compulsory to pass e you can pass any variable there okay try and accept we understood uh how we can use the uh else and finally block so let me also use it here okay all right so if you’re using U you know the accept after accept we can also use the else block try accept or else else okay so here I’m just writing the normal statement is um uh uh program finished program yeah program done just normal statement so what will happen here the uh program completely done yeah successfully completed successfully program successfully done because when I’m just passing any statement like 2 divide by 6 uh definitely we’ll get the answer is 0.333 and program successfully done but in case if I’ll get any kinds of error any unexpected event is occurred like w if I pass it so it’s showing that invalid and that time the program is not successfully done okay so we can apply the looping concept here uh like the program is running it again and again until we are not done so like if I’m just pass it here the while loop while true so it will be running again again and again but when the program is done that time it will be break so the else is also playing the important role in some situation uh where if everything is if not getting any kinds of error so that time else we can use it so here when I just pass W it will get the error at the same time it’s showing that invalid okay um it’s showing the invalid and again when I just pass the first number properly the two divide by 0 so it also giving the invalid division by zero but when I just pass the proper number so it’s showing that program successfully done and uh the while block is finished this way is working and finally block is kind of uh it doesn’t matter exception is there or exception is not there the finally block is definitely called okay so else is kind of if you not have any exception that time it will be run but the finally block no matter what is happening in the program but finally block will definitely execute so I will choose this one this program because it looks good because zero division is separately and value error is separately okay yeah so finally is Shing that um U like kind of um if I’m making any any any games okay if I’m making any small games so in that situation uh try accept is working very well and finally is playing the very important role create a very small game to generate a random number and we have to guess it so where I will Implement all the keywords of exception handling like a try accept else finally I will use it everything let me first create one random number so we can import import random and uh random do Rand range so we can provide the range from where to where you want to generate the random number I want to generate 1 to 100 so it will generate the random number in between so whenever you run it you’ll getting always different different random number so uh random number we can say just I I’ll I’ll pass it num that’s it okay so my target is that when I just uh when I just pass passing the input if the random number is match then it will showing the statement is that okay I guess the number but if it’s not match then it’s less than and greater than it will giveing the answer so user I will take it as a integer input please guess the number okay please guess the number and if the user that uh you know the guessing the number is greater than num then what I have to say number is greater than than uh guessing number you know your number is a greater than guessing number your number is greater than guessing number yeah random number whatever you can write anything random number okay so and L if we can also apply it if user is less than num the time I can write it here your number is less than random number okay random number okay all right so when I just run it it will asking the question is that please guess the number I’m guessing the number is a five it will saying that your number is less than random number right so that means whatever number I have to guess it the next time make sure that it should be more than five but when I just when you just run it here again so your random number will generate again right so we don’t know previously maybe the random number was uh 25 but now the random number is two so make sure that your random number should not generate multiple times okay again your random number is um less than your your number is less than random number so I will just apply the true uh while loop I’ll I’ll apply it and make sure that it will be running the multiple times while I’ll make it true okay I will make it true so it will asking the question that please guess the number and here the random number will be the static it will not generate every time but your while should be the uh you know your your while loop should be stopped when the number is match with your user is match with user is match with number so what I have to do I have to uh directly write it here um we can we can directly write it the if condition everywhere it’s very simple program you can modify it according to you if user is equal to equal to num the time I have to write it here uh okay we can write it yeah you you got the number you got the random number okay that’s it when the number is match so let me run it again please give guess the number the number I’m guessing that seven saying that your number is less than random number okay uh so that means I have to guess it more than 7 I’m guessing the 50 again is saying that your number is less than random number that means is more than 50 I’m guessing the 80 saying that your number is less than random number I’m guessing the 90 your number is greater than RDA number that means this number exactly in between the 80 and 90 right I’m just guessing the 85 so your number is greater than the random number that means it’s the 80 to 85 I’m guessing the 82 your number is less than that that means 83 can be or 84 only two only two option is there 82 or 83 or 84 is less than that that means the answer is 84 yeah you got the random number I got the random number but again it’s not stopped because I didn’t break it I have to break it here okay I have to break it here let me uh let me just terminate the program because I didn’t use the break condition here break keyword here let me just use it break when you got the number so in this small game what we can what we can uh you know add new things or new features the first thing is that in case if I’m running it and someone is passing the T I got the error so in error we can’t afford it like any kinds of error right so what we can do we can directly uh apply try okay we can apply try in everywhere and accept block because exception handling is very common for any kinds of program so whenever you building any applications so we don’t know what kind of exception will come we can’t show the red color error we have to tell them yes it it’s kind of invalid you have to write it uh you know new things uh the proper statement proper number so whatever is mentioned there in a program I’m just writing there the exception okay and just normal statement is invalid invalid right let me also put the colon here because this not looks good it’s like it’s mixing here so everywhere let me put colon colon colon okay great so try we use it except we use it and uh so if you pass it t that means it not make sense like when I just run it it passing the T so it’s invalid right uh so if you got the number exactly the random number so I want to know that how many attempts I did so that things like if I pass T that means by mistake I just pass the T it’s invalid that should not be countable so what I can do it here let me just break it uh what I can do it here the else I can use it if not exception the time I have to just Define one count here the count is equal to zero so whenever your program is run your try is run so that time the count is increased count is equal to count + one okay every time you count is increased and uh when you got the exact number uh when you got ex here I have to do it because if suppose in the first attempt you got the number so you’ll get the answer is yeah I you got the number but uh what is the attempt so attempt will be the one because zero attempt is not possible right so uh yeah you got the number after count this uh string formatting I’m just use it here so I got it like after how many attempts I got the number okay so this number should increase here let me just run it when I just pass the number is uh 50 okay your number is less than random number that means it should be higher I’m passing the 70 less than that then let me pass the 80 is less than pass the 90 is greater than okay so maybe the number is quite similar no it cannot be is it greater than that uh okay I have to go back lesson number 82 number is lesser 83 is the final answer so yeah you got the random number after seven attempt okay so I have to just make it you know here else I use it except I use it try I use it I want to use the finally block so what finally block does so finally block is basically uh your exception is occurred not occurred it doesn’t matter but the finally block will execute is a kind of if if you um if you open any files exception is occurred or not occurred make sure that the file the program is finished the file should be closed because the next time file will open so here is a game so it kind of every steps there is some checkpoints so I can just write it here just normal checkpoint that means one person will do second person will do the kind of I can make it just checkpoint like one person is finished so the checkpoint will occur doesn’t matter the exception is occurred or not right if the triy block will run finally will run accept will run finally will run else will run finally will so let me just run it when I guessing the number I’m guessing the number is a 50 one checkpoint is done right so it will Shing that yes the first one and second one is totally the separate so guess the second number it’s less than that I can make it 90 greater than 80 less than oh it’s kind of the random number is generating the inbit 85 let me just run it okay yeah you got the random number after four attempt right so hope you understand here where we can use the try in case if you have any kinds of issues you can see here suppose if you run it the program and uh you P guess the number less than that but by mistake you pass the number is uh some string so it will showing that invalid the checkpoint will still run but it will not increase the count this will will considered as attempt because it comes under the else where is this exception which is comes here will not consider as attempt so the next time when I just suppose if you got guess the number so this part will not be considered this part will not be considered hope you understand let me just finish this game okay less than that that means it’s more than that 90 okay it’s kind of every time time is generating the number is in between uh okay five attempt you can see here one attempt okay two attempt three attempt four attempt and here the five attempt this invalid is not considered here hope you understand this try except else and finally block last topic of exception handling is uh nested exception handling nested exception handling okay let me just use the same program which I discussed in the last to last session the same thing I will just use it here all right so here the normal program is like division of two number if the number when someone is passing that any string instead of any number then it’s showing the error and that time is handled by value error but in case if denominator someone is passing the zero so that time the any value cannot be divided by zero so that time the values is passing as a please enter any non-zero values so what I can do I can make it as a Ned exception handling as well because the last one I just make it um exception invalid in case both the um in standard exception Handler is not ex uh you know considered that time the exception will considered I can also make it let me just remove it this one here it’s not required I can also make it the separately the tri block everything I’ll just put inside a tri block and then I can apply it here the exception except exception so it’s become a nested exception handling like try inside a try you are just using likewise the nested for Loop nested conditional statement nested condition is means if inside a if nested Loop is Loop inside a loop nested exception handling is try we can say exception inside exception here I also use the same thing here so in case if this kind of exception is not handled that time the main exception class will accept yeah considered me just write it here the error okay so let me just pass it here suppose any value error if I’m passing so that time please enter any uh numeric values so this part is handled and I just run it 5 / by 0 that time this value is handled but in case if you passing any those kind of values which is not acceptable in value error and zero division error that time this exception will considered likewise if I’m not passing anything that is also so the value error how I explain you this one okay suppose if I’m just removing this part only zero division error is um you know available inside a try inside a try and I just pass it t here so that time is showing the values is one error because zero division error is not considered the value error exception so this entire block is become the error so which is handled by this exception class so hope you understand this exception inside exception sometimes like if you have a bigger program like any application if you are making it this Ned exception handling is really required so before going to the file handling let’s understand that how many types of files is which is available in our computer Industries so many format is there to handle the uh data with first one is it txt and uh PDF we have PDF we have CSV we have do Json we have XEL we have do pickle we have okay so these are a format which is available so this format to store the data so data can be anything so data can be stored in the different different format so if I’m storing one data which is like uh uh the college Rel College data where the student marks is there student subject is there student name gender age everything is available I’m storing the data so we can decide the particular format so if I decided the format is a csb so that csb is considered as a file CSV file right so at the end we are generating one file that can be any file like txt PD fcsb anything so these files we can perform some different different operations so what is operation Operation can be I want to open the files I want to close the file I want to delete the file okay I want to delete the file I want to you know update some file I want to newly write some file so these are operations so I created one complete Jupiter notebook so where you don’t need to re you don’t need to listen the entire video you can also refer that Jupiter notebook you’ll understand easily how the file handling is working so here the first one I just mentioned there first we’ll start with the tech uh text file so how we can write it update it read it everything okay to deal with this kind of operations we required some functions so these are a few important function is available the first one is open and close so if you want to perform any kind of operations so first you have to open some file okay and after that you can perform some operations and then make sure that you have to close that file as well so this this function is also very important so here I didn’t write the description what is the mean of read read is just reading the file read line means line by line reading the file okay write is writing the files right line space one by one writing the files but see and tell is something different so that is the reason I mentioned the description as well seek means defining the exact position and uh tell is find the position which is tells us ke where exactly the uh cursor is available don’t worry we’ll discuss in the practical way so one thing is also required which is a mode okay the mode mode is nothing but ke like what operation you want to perform I want to perform writing operation read operation appending op operation rri Operation sorry yeah rri operation or write in binary operation so here is available so I have one file which um my txt txt my text.txt I am try to write something okay so let me just do it one by one so first I will just create one files I don’t have any files there go back here you can see I I don’t have any kinds of txt file okay I don’t have any txt file so I will just create it one files so here this is the first operation open it will just open the particular file make sure that your mode should be w w means writing the files and then line by line I’m just writing the file this is my first program this is my second program I’m writing there so make sure that you have to close it otherwise it will be impact to the another files if you opening in a new section so yes I close it that means file is properly written sometimes we are not closing the files so what happened ke if you write the program uh if you apply the uh writing some uh you know lines in a text but you forget to close it so that time the file exactly not writing in the text file okay so now file is a properly written so we can also check that ke properly written or not my txt my text.txt so yes file is a properly written there so this is the first way to write the files we have to separately open it and then uh write something and then close it so one more option is also available which is a with open we have to use a with open it and if you using the withth open you don’t need to close it the reason because you’re writing entire section inside the block if you’re coming out the block that means file is already closed so this is also one of the good practice so if you’re using the with open that time you don’t need to close anything okay so I’ll also do it like let me just use it my text. TX my text one.txt let me just create uh new files it will be better so that you can easily compare it let me run it here you can also check it my text one txt so yes this is my first program this is my second program that is properly written here okay so yes this way we can write something but if you already written something I want to update it like I want to add some values which is the line number third line number four so first make sure that you have to open that file mode is very very important so here I just use a w mode meaning is that I’m writing something a is I’m appending something okay when I just use the uh third line and fourth line and if you close it and after that you can see you will find it out ke third line and fourth line is also available but in case if you’re just passing the W instead of a so what will be happen your previous statement will delete it and third and fourth will create so don’t apply the w every time w means writing means if you already exit it will be overwrite again so if you want to just up uh update something so you have to write a so I just use the my txt do my text.txt okay then I’ll check my text.txt yes this is the third line this is the fourth line actually directly starting where the um you know the cursor ended previously it’s ended here that’s why it started here and I use the slash n so that’s why it’s showing the slash in next time so now we we understand the read read line WR right line everything uh okay read uh right we we did that so if I want to use the reading okay so make sure that I should have some files okay uh so python. txt I create I have one files is a big file so you can also find it out files on um GitHub okay before going to the python. txt let’s also read something okay you can also read the my my text.txt as well let me show you how we can read it so with Way open you can also use it with open I’m just opening the file which name is my txt do my text.txt my my text.txt and mode is equal to it’s r r for reading okay and then uh uh we have to define the Define the you know as a variable as F3 I’m just defining it okay F uh F50 I’m just defining maybe it will be impact the next one because 3 four I just use it there so I’m just use the F50 F50 dot read line and then read line that’s it so it will be uh store somewhere let me store in the where one okay so we can also check that is the first line which will be printed where one so this is my first program so whatever I write it I can also read it as well but this is very small line of program so uh I can’t explain you you know much here so that’s why I I created I have one files which is the python. txt yeah this one python. txt this is the Big File okay so let’s let’s understand it how we can read it line by line and uh some particular uh specific location if I want to select it I want to read it then how we can do that for example I want to read only the python support modules python support modules and packages which encourage program modularity like this L this line I want to read it how we can read that kind of line uh you know to skipping other things and directly read this one so first of all you have to read first of all you have to open that file if you’re using the read meaning is that it will read entire things and I’m just closing it here and I just use the print I use the print the reason I want to display everything so now everything is a display but in case I want to display a few things only right so here I store let me store in the variable T why I’m storing variable T because I want to perform some operation there okay uh because everything is available in the string format you can also check the data type so what is the type of T which is the string right so like I want to know that how many times pythons is available in the entire book yeah we can say entire text okay so let me just check it so it’s available at the two times so what is the position of that that uh that pythons the first position which is the 2002 so what is the second position of the pythons so you can directly write here 12 23 means the next position so actually why I’m writing here the 213 because it will be start from 213th position so what will be the next so 66th position have a pythons okay so there is a two places pythons is available you can easily find it out here string with string operation okay so here we have a a method is a seek method seek method is exactly we can just Define the positions from where you want to start it like I I I decided that I want to start some particular position which is the python is interpreted objectoriented high level programming language suppose I want to start from there so how we can start it like we our cursor is always starting from here but I want to start here so seek will Define the position in a python yeah we can say here in this location and then it will printed the uh that particular line so I I Define the position which is the 216 okay let me also check that what exactly U showing that particular line so when I just do that 1216 the current position so which is tells us so tell it will tell you ke look what is the exact position and seek is defining the position set the position so when I just set the position when I just set the position and uh from there it will be starting so python designed the philosophy EMP prise code readability with its notable use of significant indentation so this is the line where is that okay yeah python design I want to read this one because I use that what exactly the position of um uh you know the first pythons so that was starting from it will tells us this one 212 but uh it will tell you the entire 212 entire pythons not the P I want to know that the position of P this P so that is the reason I just use the plus 4 and uh use it here 22 + 4 so likewise you can also check it and you know in case if you’re not write writing the plus 4 so what will be what will happen so it will showing that g dot and then values so you can just count it 1 2 three four after four position it should start then I’ll make it plus four that is the reason I did that okay so you can print it everything here so because I just use the C is equal to 216 that is the reason is printing after that okay that is the reason is printing after that so hope you understand how the text operation is working in Python programming language to handle the operating system operations in your computer so there is a module is available in Python which is the Os Os module so in OS module the lots of method is available to perform the different different operations so we can find it out this kind in any module I want to know that how many method is available you can use a DI function Dr OS so these are the supporting attributes so don’t check this one and here the ABC aert access CHD close chmod lots of method is available so every method have their own work so here in this video we will discuss about the few method and we perform some operations like let me just do the first one the OS do get CWD so get CWD will tell you like what is the current location your Jupiter notebook is working in some particular location what is that location there so the D drive recurrent learning and python so in case if I want to change the location how we can change it so this is the location here D drive okay D drive recurrent learning suppose I want to change it I want to enter in the directly in a python okay I want to enter not only yeah Python and exercise I want to enter in that location okay recurrent learning python exercise this location I want to enter it okay it’s already there in the python let me change any any other location back not in Python video I want to enter and Python tutorial so there is no any files is available I want to enter in that location so copy it that path go there and uh we can use it os. C HD change directory so you can provide the path here so when I just use the change directory your path will change and next time when you just check it os. getet CWD so you will get it the current location which is the recurrent learning video on python tutorials and some operation we can also perform is like um I want to make some directory I want to make some uh you know some folders directory is a folder so how we can create it uh current location is this this in the sense here nothing in no any folder is available I want to create any folder how we can create it mkd sorry OS do mkd and pass the values you can you can write anything uh I’m just writing temp directory temp di so when you just run it so temp directory will generate yeah temp directory is generated so with the help of os module you can perform some operation with your computer so that python is providing that kind of facilities other programming language is also there just I’m telling you the python have the OS moduel and uh in case if you have the multiple directory for example my current location my current directory is this and I want to create the multiple directory inside a directory like path is equal to I want to create um you know new dir inside the new di I want to create one more directory is a okay analysis okay inside analysis and then um uh we can say Titanic okay this folder I want to create it but if you’re using the. M KD let’s see it will work or not so os. mkdir path let’s see it will showing throw the error the system cannot specify the path because your current directory is this and inside that you want to create the multiple directories so which is not possible with a mkdir so different method is available which is a Mech DS Mech directories so when you just using the me directories so you can create a folder inside a folder inside a folder like that so let me show you yeah new di inside that analysis inside that Titanic so this way we can create it okay so in case I don’t want any directory suppose I don’t want a temp directory still there so OS Dot .rm and provide the path which is the temp Dr so that directory will delete RM di sorry rmd so that directory will delete as well so this kind of option is also available to you can also delete it okay there is one only one directory which is the new d new dir there is no any temp di because I deleted I want to change this name I don’t want you know this new IR I want to rename it so OS dot rename it okay so rename it so what is the file name new di I want to change it as a new only okay rename n a m e rename sorry so new di I just change it is a new so change it here so that kind of option is available so let me just do the last two method and after that I’ll I’ll show you the list you can easily explore it suppose my current directory uh my current directory is this current di let me just do it o.get CWD okay this is my current Di okay I want to perform some operation uh so like uh I want to add it like new Di and with all the locations right so how we can do it like inside a DI there is analysis folder is available so we can also join the path as well so OS do os. paath do join so my current directory current Di with whatever path you want to join it suppose I want to join with a new so it will be joined like this okay so you can perform the analysis and based on that so. path. jooin you can joining with the two One Directory with any other directory in case I want to create One Directory which name is a new which is already exist let me R OS do mkd which name is new let’s see what will happen so it will throw the error and saying that cannot create a file when that file is already exist so there is a condition we can also apply it okay if os. paath do exist and you can write it that new in case if new is exist the time in case if new a exist if os. path. exist the time will not create okay okay okay let me just try uh okay so it’s kind of if path the the different path is exist then you can create it like I want to uh this one is just tell you like the path is exist or not like I want to check that yes or no so it will show yes that path is exist so in case the new is exist inside that I want to create one uh location like here like here inside a new I want to create it inside new folder I want to create one more folder which name is uh data science okay inside a new I want to create it in a data science likewise okay let me tell you new okay temp Dr and then data science okay so like this live it let me make it the very simple I want to create new directory os. mkd which is name is new I want to create it but it’s already exist so it throw the error so what I can do if not os. path. exist new if it’s not there then it’s create otherwise don’t create so it will not throw the error in case new is not available so currently new is available it’s not enter inside the location but in case if it’s not available I delete it when you run it here so it will be create but inside nothing is there because just now it created okay so when you just apply this kind of condition if not os. path that exist then it’s created otherwise don’t create so in OS module lots of method is available so I just provided the link in a Jupiter notebook W3 schools you can also explore it like anything like I want to know that CPU count okay with a CPU count you can also use it how many CPU is there and you can also perform the operation there is a four CPU kind so likewise I want to also check that in my PC how many CPU count is there so OS do CPU count okay it’s not counts think yeah there is a 12 CPU count is there in their system is a four so likewise you can also explore it other method which is available here this is the last topic of file handling so here we will discuss about an different type of files like a pickle Json CSV okay so let’s create one dictionary so dictionary where inside some list is also available so this is the normal dictionary so pickle is used for preserving the data so like if you save this kind of data into any format like a Json format or normally the text format you can see easily right if you double click with a notepad you can uh see that kind of files but pickle is always store in the preserving method so like if I’m creating this kind of data as a complete data frame I want to preserve it and store somewhere so that in future we can also use it so usually this pickle we are using if you are generating any kinds of you know the model uh machine learning model or uh uh any any kind of model deep learning we are not using because for deep learning we have a separate extension is there so L take the example of machine learning if you created the machine learning we have a lots of steps is available and that entire steps that entire model uh steps we can preserve it with a pickle extension so here I’m just taking the one of the simple example which is dictionary I want to preserve it so again if you want to write any kinds of files we have to go with WB right in binary because it’s a pickle file and that have a two with two extension we can use it first one is pickle second one is Sav okay we can when you just store it uh uh that file will be stored in the pkl format so dick. py uh di. pkl the file is stored and you can’t see directly even if you’re opening on notepad you can’t see that kind of files so we have a two extension is available Sav and Pon okay so in case if you downloaded this file and uh you want to open it normal notepad any other any kinds of you know applications let me just download it and and I will show you how exactly look like so that was a dictionary that was a dictionary and now it’s visible like this actually that was not dictionary that was a data frame but it’s not visible properly if you want to see this kind of data again and if you share to anyone and they want to load it how we can load that kind of files so again we have to open first of all that uh files like with open Lim just use it with open file name file name is nothing but uh di. saav okay and we have to define the mode mode is equal to read in binary and pickle okay that we need one reference as F okay so pickle do load that F I want to load it let me store in the result values let me see result yeah so now we can see that kind of files so directly if you try to open it you can’t so because it’s store in the preserving method okay likewise we have also the another type of files which is a Json Json one of the data format where we can store in uh Json format right it’s a different type of format is there so Json always is working with the um curly brasses inside that key value payer option is available okay so we have a two types of data is available first one is uh structured data second one is unstructured data so this kind of data this word table kind of structured data but in case if you have a sector sector we have an HR and in HR the multiple set sector is there yeah we can say multiple type is there so you will you need to create one more table and then Define it HR how many types of HR is there right but the plus point of the Json is that ke name is a bob but the language instead of defining only one we can also Define the multiple languages as well okay and likewise inside the dictionary we can also make a dictionary so like the language is there inside the language I can also create one dictionary like in in a language I can also create a dictionary for example I can also make like this okay in English what is exactly the marks is there 45 so likewise they totally the unstructured we can Define the data in any way but if you have a table format so we have a limitations like English have a 45 a French have 56 the kind of we can also Define it so the Json is giving the lots of freedoms to Define our data set okay so nowadays the most of the data is available in the unstructured format so we preferring the Json extension okay so let me just show take it as it is yeah all right so we can also write it let me just create one normal files uh per . Json I just create normal file I’m just writing it here and dump now it’s dump and you can also do like this with with um you know with keyword you can also do that either or you can just open the file and then dump it okay so reading the files like file is already created the person. Json you can also see that so Json file you can easily see yeah they look like this okay so Json data is like little bit bigger even uh I can also show you uh some data set okay let me just go to uh covid 19.org one famous website okay covid 19.org okay I’m I’m just going with that okay this is a official website okay any other website is there co9 .org yeah covid 19.org okay I’m not able to find it out any H yeah yeah this is the uh one website is there when you scroll down and you scroll down so you’ll find it out the database here most of the data you’ll find it out in the Json format because unstructured data we can easily store in the Json extension okay see this is the format of the data so the most of the here the data is available like um you know the key and that values that is also considered as a key for that values this one is a key for that values like that we can also Define it like there is no any limitation to Define our data set if you’re doing with the same thing for a table so we have to create multiple tables for that so that is the reason Json is a quite booming and quite famous data type is there okay so we can also read it simply we have to read that data set so when I just read it look like this but if you want to see in uh same as it is the Json format so we can also apply the indent indent means taking a space for each uh sections we can say so to showing like this the name is a bob language and inside a language we have the different languages is there so we can also see that kind of data and and indentation it’s totally Upon Us ke how many indentation you want to pass it you want to pass the four it will be taking the four space otherwise it will be taking a two spaces so we have also uh another type of data which is the CSV is a quite famous I think uh uh everyone know about that CS V and Excel CSV is the comma separated values okay so here I created one complete CSV files so this is the field I Define it and this is the row and I want to create the complete table CSV tables which I will open on Excel uh software okay so I created the complete data let me first show you the data before writing the CSV format okay how it look like so row is look like this so this is the data I created it here and this is nothing but a field okay so field which is the name ear branch and marks so here I here I just just normally open it with with keyword student. csb is my data uh file name and I want to just write it and for next line I don’t want to Define anything and a dict writer when I just pass it and every time I will just write it uh write header and every rows that rows values one by one it will be passed into the um csb so the file is created you can also see that in a student. csb go back and uh we can see C student. csb is created here let me first download and I’ll show show you how it exactly look like the CSV file is created and the data will be look like this you can see okay name ear branch and marks so okay that is nothing but a field yeah name ear branch and marks that is a field and all the rows is also present here the name I Define it as uh Saga here I Define it a 2022 20 and Branch I Define it marks I Define it accordingly it’s created okay accordingly it’s created here you can see with the help of file handling we can also create the CSV file as well so likewise here I also Define it okay new line I want to add it so mode I make it upend and uh new line I didn’t Define anything so next time it will be appending that values so let me just refresh it okay uh here it will not refresh let me open in the same location here student okay so yes Manish math I Define it here okay Manish and Branch 2024 and math and marks I Define it here okay so likewise I also create uh the same thing for a data frame in case if you have uh uh this is the way of file handling techniques to create the CV file but one more way is also available to directly generate the CSV file like if I created the complete data frame with the pandas so it look like this I want to generate into the CSV so data set make sure that this uh this data set type this data type should be in a data frame pandas data frame okay so this is one is a pandas frame. data frame so if this kind of format of data is available you can directly pass data set in 2 csb if you pass the 2 csb so that directly will generate the files you can also see that yeah movies. CSV so the file will be generated in the Excel format sorry CSV format but in case if you want to generate the Excel format as well that is also quite simple why is not opening yeah it’s open okay by default is generating the uh index in case if you make it reset index Tex that will not visible there again uh this kind of things we’ll discuss when we start the pandas and numpy topic okay so let me show you how we can create the Excel file data set do 2XL you can Define it and uh you can write it here the movies do x x LSX that is extension previously it was a CSV that is a comma separated values Excel and csb both are different things both are different format is there okay when I just create it so you can see this one is a uh this one movies is the Excel which have a 5.07 KB that file size is little bit bigger compared to the CSV file this one is a CSV file but when you just click on it you’ll find it out the data is the same but only thing is that the Excel is creating their own format like this one is making at the board B this one is also making as a bold like that okay so this one is also the format of reading the any Excel file of CSV file I’ll just normally open it with the mode of R when I just make it mode of R and uh every time I just make it header and print it that sorry reader then and I print it one by one so you can see that the file so yeah so this way we can and why I just use this next because I want to just print it next one by one okay all right so hope you understand this all these structures but in case if you’re facing any kind of issues because uh this one I was little bit faster reason it’s the extra yeah we can say the different uh format of the data set is there sometimes useful sometimes not useful and uh I just explain the Json CSV and pickle maybe you also use different types of you know formatting but again the the structure the the process which I explain for this kind of formatting of the data set that will be the quite similar for that other format as well okay so hope you understand this video but in case if you’re facing any issues you can write it down in a comment definitely I’ll solve your problem so let’s first discuss about uh the topic which we’ll cover in object-oriented programming series Okay so so we’ll first discuss about what is the object then classes what is the inheritance and also we’ll also discuss uh Constructor polymorphism will discuss abstraction and encapsulation there is a completely a six main topic is there in object oriented we will cover one by one very first one what is the object and classes okay so class is nothing but so the uh topic name is objectoriented right right so first we’ll discuss what why the name is object oriented so object oriented is nothing but almost everything in uh programming language we consider as an object so like if I’m I’m a human my name is uh my name is X someone name is a y so that person is nothing but object so some classes is also there like the person is a x the person is a y the classes will be the human right we can say simply human human so suppose one car is there like here I I given the example as a maruti Audi BMW so these are a car the car is considered as an object where the car is a class this maruti AI BMW is an object the car is a class the same thing here so object which is defined here the Happy angry sad whereas the emotion is a class so the one real thing is which is really exist in a real life so we can say it’s object and some classes is also exist so class is nothing but it’s a blueprint of object whereas a object is a instance sub class instance is nothing but the example so object is a one example of a class so maruti is a example of car Audi is example of car BMW is example of car example is like instance of a that car so let’s understand in the practical way we’ll do it in the python okay so let’s do it in the practical way I’ll I’ll take the same example maruti Audi and BMW I will not take any other things so just consider here the maruti Audi BMW is an object and car is a class we’ll create it accordingly I’ll try to connect with that how programming uh in programming language objectoriented we are implementing so let me create one Jupiter notebook so it’s taking a time yeah now it’s created let me just take it oops concept okay so very first one is object and class we’ll try to Define it in a p in Python programming language we are defining with the uh class and class name so class name I’m writing here the car whereas the object will be BMW Audi and these are the things okay so car is a class so inside that we have a different different uh uh you know object is there so inside a car we can just Define it we can Define it the multiple functions so inside there is a class to multiple function is available right so the function can be the engine the function can be uh you know uh some indicate fuel indicator uh function can be the seat how many seats are there how many doors are there right so there is a door I’m defining the the function is a door so it’s very important to write it down here the self okay in Python programming language self is very very important whenever you’re defining any uh method inside a class so I’ll explain you later why the self is important for this self and then normal statement this car have four doors that’s it okay and and Def engine self is very important print this car have very powerful engine okay and uh let me also Define one more things is here uh seat self print normal statement this car have five seat yeah now then we just create an object so object can be uh BMW I’m defining it here BMW is equal it’s a object so car is a class so we Define the object like call the class and using the bracket and after that with the BMW what are the functionalities available you can directly call it okay so BMW do door so currently I just Define the very high level just door engine and seat but you can also Define it the very specific things is there ke in a BMW uh what is the engine is there so BMW have very powerful engine as compared to the marutti Suzuki or atata anything right so we can also Define the separate separating and we can call to that specific engine and specific seat okay and uh uh then I just call the B I just call the only door function so door will open right when I just run it so this car have a four doors okay so if the BMW i define it BMW do seat then it will call the seat only so this car have a five seat okay so likewise uh suppose if you have a different car car okay and I Define it car and uh T do door when you just call it so you’re not expecting that it should be the answer is a four seater a four door there is only two door right but here I directly Define it the four door because it’s a uh you know just default value I just pass it that I printed the values but you can pass the arguments if you just calling the uh door function and your object is a thar so definitely it should it should be pass only the two door not a multiple door is there so likewise we can also Define it so we can pass the arguments here is showing the four door because I Define it so here my main Moto was that ke how we can create a class and object so car is a class door method seat is a door engine seat is a method I can directly Define it here this is nothing but a class and these are we are calling the method and these BMW is nothing but an object BMW is nothing but a object object calling okay so this way we are defining here the the confusion is that what is the meaning of self and uh in case if I’m not writing the self so what will be the impact here so suppose if I’m deleting the self let’s see what will happen throw the error it throw the error is saying that the car. door takes zero positional arguments but one was given so that means so here the self is very important to Define it because so by default when you are creating the object it’s passing some arguments okay so that’s why it’s saying that take a zero positional argument but one was given but means here is passing some arguments but uh uh you know but I didn’t Define a I didn’t Define any positional argument so here I didn’t Define anything so that mean we have to define the self so what is the use of Self in case if I just use it here the self what is the purpose of that so self is a kind of self is a kind of one parameter so it’s using for passing the references so here I Define it self is a parameter is a reference to the current instance to the class and used to access the variable belonging to the classes so it’s very simple like if I have any variable uh the variable is um door is equal to four instead of defining here directly the four I just Define it as a variable I pass it the door okay I pass it the door and uh the same thing for a seat I also Define it seat is equal to 5 okay I Define it the seat okay all right so when I just run it so you’re getting the same answer nothing will change because I just create the one variable and Define the variable here same thing here I create the variable define it here but in case I want to use this door variable in a seat function is that possible so absolutely not we can’t can’t do it because it’s a local variable so it’s useful only inside that function any other function if you want to use it it’s not possible there so how we can use it so like here I want to instead of writing here okay instead of uh uh instead of writing this car have a five seater and door and doors are door is it possible to write it so it thr throw the error if you’re calling the seat function definitely throw the error yeah it’s saying that door is not defined in a seat function why it’s not defined because you can see here this car have this uh this this function have the variable seat but door don’t have but if you’re using any kinds of variable and attach with a self so it’s become a kind of global for the entire classes not outside of that so self is kind of passing the references so that it can be used inside a classes self. door instead of writing the door if I’m writing the self. door you can access why not access okay uh self. door is not accessible it should be self. door self do okay let me also Define it yourself I think half of you can’t use it door is not defined mhm okay so it’s throw the error in the first U function uh bmw. door okay so here is throw the air because I Define the self. door and here I’m calling the uh one door so that’s why throw the error so now the problem solved I use a self door and that variable I can also access easily any other function as well but make sure that that all the functions should be available in the same classes if the classes are different so then it create the problem but again that problem have also the solution is that uh we can use the inheritance that we’ll discuss later okay hope you understand that how the self parameter we are using what else we can also discuss it here there is one topic is called as a method and function and uh uh some you know some myth is there ke method and function both are the same uh both are the same or different and if it’s same then why we are using the two different name right it’s very confusing things so method and function both are same first of all let me just clear it it’s the same nothing different so only different is that if you’re creating any kinds of function with like uh Def and uh some function name is like a fun okay let me just write it there if you’re defining the function def function name is a great okay this is the function name and inside that whatever you want to write you can I’m just writing the pass so this one we are calling the function this one we are calling the function but in case if you’re defining any class okay class name is a and inside that if you’re defining this kind of function and with a cell parameter which is mandatory in objectoriented programming language if you are writing in a python so that time the grd become a method okay so here grd we are not calling it a function it’s a method so the difference is that the function and Method both are a same only when we Define the function inside a class is called as a method let me also write it when we we are defining the function inside the class we are calling method very simple so method or function both are the same only thing is that when you’re defining the function inside a class we are calling method there is also one more perception not a perception one statement is quite uh popular in Python programming language only which is in a python python is an object-oriented programming language that is true but almost everything in a python is an object with their properties and Method now it’s very confusing almost everything what is a mean almost everything so that means if I’m defining any variable that is also the method uh that is also the object yes as I said like when we start the objectoriented programming I said ke uh almost everything thing in uh in the world which is really exist is called as a object right so here if I’m defining any kinds of variable this normal variable a is equal to uh 15 and B is equal to hello these are two variables so according to that statement can I say that A and B both are in object it’s true both are object so why it’s object see if it’s object then definitely some classes will be there right because python is objectoriented programming language I said that A and B both are object that means some classes will be there so when you just check the type A so definitely you will get a class actually you writing in the Jupiter notebook that’s why it’s not showing that complete statement when you just print it print a so you’ll get it yes so int is a class of a so a is object integ a class some method will be there some function will be there method will be there not function okay because when you define the function inside a class that become a method so where is the method so when you just using the DI function which we used in I think last classes uh for OS module so di of a you’ll get it all kind of method these are attributes which we are using in object oriented in in class and object and when you scroll down you’ll find it out the object as well uh sorry method as well these are the method two type real image everything right so hope you remember in the uh data type um in string so lots of method is there I I make I separate it okay these are a function these are a method a method is always calling right method is always calling so here when I just use the DI R of B of B you’ll find it out the lots of method I think some method you already aware about that so like uh join we usually implement it split we usually implement it where is that yeah upper lower these we are implemented these are a method so if it’s a method I want to call it how we can call it here so B is a object okay I want to call one method Dot because B is object if you’re calling any method we have to use a b dot what is that upper so this is the method so when I just run it yes it’s working but that classes is the inbu classes so int string di list these are inbuild classes so hope you understand this concept is that why I’m saying that int uh why I’m saying that almost everything in a Python programming language is considered as an object a Constructor is a special type of method which we are defining with the init so I think we already discussed the init in package and module so there we didn’t create it in a class and object but here the Constructor which we defining that will be the class and object so the Constructor is executing when you’re just creating the object of the class so even don’t need to manually call The Constructor so let’s do it practically this is not a theoretical topic let’s do it practically here I’m taking the same example the car and what I just discussed let me just do the copy paste and try to just create one problem and that problem will solve with the Constructor okay so these are the uh code and here I Define the door and that that self. door I’m just using inside the uh yeah the seat yeah in the seat method I’m also using the self. door but what happen if you’re not calling the door method and you’re just directly calling the seat method so of course it will be give the error so here is showing that bound method oh okay because I Define it the door method and door function uh door variable both are a same so never do like that uh so better I’ll just do a doors okay and here I also use doors so here you’ll find it out you’ll get the error all right let me just remove it this one you’ll get in the error because you didn’t call the door method and you’re using this door variable so in that situation what is this uh option for you because anyhow you have to call the do method then you can use whatever you have Define inside that particular method so avoid this kind of scenario we are using the Constructor so Constructor we are defining with underscore underscore and it underscore underscore inside that cell parameter is always be there and whatever you’ll write print this is Constructor okay this is Constructor so whatever you’ll Define inside the Constructor this in it and that will automatically print so let me just remove it both okay let’s just create uh create the object BMW is equal to car so when I just run it automatically this method is run it’s executed it even I didn’t call it here so let’s call the BMW BMW dot the seat method seat method if you’re calling so there is a variable is self. seat okay self. seat which is Define it uh Define it here here directly and seat number better uh let’s change the variable name seat number so don’t make it the variable name and Method name are same otherwise the conflict will happen and you will be confused that why the getting the different error yeah different answer okay the better of door I’m also taking the door number yeah number of door okay number of Doors number of doors okay so here when I just run it and check the error car object has no attribute of number of doors the reason behind that because I didn’t call the uh car method and directly calling the seat method here so the seat uh there is a two variable I’m just using SE seat number which is defined in the same method but the number of door is defining the different method so avoiding this kind of conflict if you’re just using the same variable in the multi multiple method so don’t use it here I can directly use it inside a Constructor because Constructor will always always execute and you can directly call any method you not depending on any method here so when I just run it yes this this car have a five seat and doors are four so right so this kind of Constructor we are calling the non-parameterized Constructor so the Constructor is also defining the two types the very first one is a parameterized second one is a non-parameterized parameterize simple you have a parameter non-parameterized you don’t have a parameter right so which I Define it here this one is considered as a non-parameterized Constructor red Constructor and if I want to make it a parameterized Constructor whatever you passing the number of doors and uh seat number so that you can pass as a parameter so let me just Define it and uh we are doing the Constructor let me also make the heading properly here so this is the Constructor topic okay so I want to make it parameterized Constructor here so this is normal non-parameterized Constructor let me just remove it my target is to make it parameterized Constructor so whatever is Define the number of doors so instead of taking the manual Valu is the four I’m taking as a parameter wise uh suppose the door number okay door number suppose that variable I’m just taking it and at the same time so all the variable which I Define it even the self dot seat number that I also take it inside the Constructor itself okay let’s take the seats number let me take the number of seat directly number of okay so uh here I’m getting the values as a parameter so let’s not take it this one now so this is the normal method I’m just making it so inside a door I’m just printing the number of doors engine just normal statement and Method and seat and we are printing the seat and doors both so here I don’t want to make it manually four and five let’s make it some Dynamic way so in the place of four I’ll just pass it the uh door number and here in the place of five I’ll pass it the seat number so whatever value will be store in the seat number and door number that will be that will assign into the self do number of doors and self. number of seat so here when I just pass when I just call the Constructor when you don’t need to call the Constructor automatically call let me just create the object of it BMW is equal to car and here you have to pass the parameter because if you not you have to pass the argument if you’re not passing the argument it will throw the error what is saying that the car do in it missing two positional argument which is the number of door number and seat number so I’ll pass the two arguments let me pass the four door and five seater okay four door and five SE when I just run it so this this is Constructor that statement is run that means there is no any problem in the Constructor so now when I just pass the when I just calling any method like bmw. seat so that running properly okay bmw. seat oh the core object has no attribute seat number okay it’s not a seat number it’s a number of seat okay when I just run it yes this one properly working the main Moto of it here if you are just defining the parameter inside the Constructor it’s become a parameterized Constructor and if you define the parameter of course you have to pass the arguments okay and uh you can also make it default parameter as well so you can make it get zero in case if I’m not passing anything if I’m passing the four and five of course I’ll get the four and five as a seat number of seat and number of doors but in case if I’m not passing anything so in the time is taking 0 0 so that default value we can also Define it so this way we are doing the Constructor let’s first discuss about the definition of The Inheritance so inheritance is a mechanism which inheritance is a mechanism in which one class classes acquire the property of another classes so there is so many examples available for inheritance so I’m just took one of the example of uh parent and child so likewise the child is always acquiring the uh you know all the kind of property and uh other stuff of uh his father so the kind of uh this kind of classes this child this classes we can say the child classes okay and it’s we are also calling the derived classes it’s already mentioned there derived classes and uh we also calling us yeah child classes and and this one we are calling the base class we calling the parent class parent class okay so likewise we have a two different name we are using it so there is a multiple type of inheritances exist in the objectoriented programming language okay I already mentioned there the super class we also calling sub classes we are calling of this child okay so there is a multiple types of inheritances exist single inheritance multiple inheritance multi-level inheritance hierarchical inheritance and Hybrid inheritance so here the the complete map of the single inheritance and multiple all kind of inheritance map is available so we will discuss about the Practical implement how we can perform the inheritance concept with the Python programming language okay let’s jump on the coding yeah practical implementation okay I required one classes uh let me just take a very simple classes is U father okay so I’ll take the very uh you know real life scenario situation where you can easily relate it like I create a I will create father class I will create a child class I will just create object of the child so that you can easily real relate with the real life example father and so whatever if you want to create any kinds of Constructor you can otherwise it’s not required currently okay so we’ll create one method so method name is like the father have car okay so the child can acquire the car car and self is very very important self and let me let me also pass some parameters car model your model I’m just passing it here and just normal statement okay father have a car here I can also Define the model as well so this is one class I’ll also create one more class which is a child okay child and here I will inherit the father let me first create the child later I’ll inherit how to inherit I will also discuss in a deeper way as well so def a child have a bike okay let me also pass the self and bike model yeah yeah model we can say model and print the statement is I have a bike and that also Define the model as well simple so I’ll create one object the child object the ra name is Rahul Rahul is a object of child class now I create it if I want to call any method which is available in the child class I can easily call it Rahul dot bike okay I can easily call it so now when I just run it you’ll get the statement is I have a bike so whatever bike he have so we can easily get it okay so here we can simply perform the operation and also check that key uh this one is running or not there is a error why it’s Error because we defined one parameter but didn’t pass any kinds of arguments so I will pass the arguments what kind of bike you have I’m just passing the fet simple so I have a bike FZ but can Rahul call the car method is that possible so no Rahul cannot call the car method because it’s available in the father class so I can simply create I can simply inherit that father class in a child class so you can just create just round bracket and inside that whatever class you want to acquire it you can directly write out there that class name so I’m writing the father so when I just writing the father inside the child bracket that means I’m just acquiring the all the property of child uh all the property of Father inside the child so when I just run it so I should get the answer now okay so father is also have one parameter so I have to pass the arguments so I’ll just pass the arguments as like maruti simple maruti okay when I just pass it so father have a card which is a maruti so simply Define it here so one class acquire the property of another class so property of another class in the sense property can be any variable can be any method can be any function method and function both are a same so you you can call to any kind of the inherit Constructor as well you can call it okay so this kind of inheritance we are calling the single inheritance we are calling single inheritance we can just Define it single inheritance simp so let me also take one more example of another inheritance and after that I’ll Al assign you the one kind of task for you you can try it and check that key uh you can you know you can easily do it this inheritance sing multiple or multi whatever you can do it or not so single inheritance is done and now I have to take some complicated in inheritance let me take the Hybrid inheritance because when I just take this hybrid inheritance so hybrid will be cover the hierarchical inheritance which will be this and uh it also cover the multile inheritance as well so I’ll just go with Hybrid inheritance and uh you can also do the practice of multi-level inheritance and hierarchal and multiple separately as well okay let me just also Define the uh here headings so that you can also do it so you will get this Jupiter Notebook on GitHub just go to the description you’ll find it out one link just go there and find it out this this Jupiter notebook code okay so uh I’ll go with the Hybrid inheritance inheritance inheritance okay it’s one sorry okay so Hybrid inheritance I’ll just try to create it I’ll just take this uh same example this one okay now this one is a single inheritance I’ll also create one more class which have simply mother and Def car self model not a model I’ll not a car I I’ll take something else uh scooty okay scooty suppose mother have a scooty uh so I can also Define it here the self and model I will not pass anything I can directly call the scooty and print this normal statement okay so mother have scooty okay simple so child here the child is acquiring the father only and uh let me also create the object is Rahul is equal to child and Rahul will call to father rul will call to sorry not a father rul will call to car r can Rahul can also call the bike as well so he will not get any kinds of error okay so model is required so I’ll just pass it here okay so maruti I’m just passing the same thing and there I’m just passing the F said so this one is working but if a child try to acquire the property of scooty Rahul do scooty so you’ll get the error while getting the error because here the child is not acquired the property of mother so I’ll here I’ll try to just acquiring the property of mother as well just do the comma and pass the mother so what will be happen so child is acquiring the property of both mother and father both okay so simply you have to just pass the both the classes inside that bracket uh which one which class you want to inherit so this kind of inherit this kind of hierarchical uh this kind of uh inheritance we can say it’s a hierarchical inheritance because I have a father I have a mother and here is a child so it it acquire the property of father and mother okay so when you’re talking about uh other inheritance like if you have a multiple things so so this it’s not a hierarchical inheritance we can say it’s a multiple inheritance multiple inheritance okay so when you’re talking about the uh you know any hierarchical inheritance so one kind of class should be there so let me also create one more classes and father and mother both can acquire that kind of property so we can say uh we can say Define that um uh property we can simply we can say property so property can acquire in inside a property function so father and uh mother both can acquire the classes so we can also Define it here so that become the hierarchical inheritance so upsider one we can say it’s a this one so this one we can say it’s a hierarchical inheritance and uh in inside one we can say this downside one it’s a multiple inheritance so let me just remove it clear all the drawings yeah so let’s create one kind of class and just Define it this one is this hybrid this currently it’s a not a hybrid this one we can say it’s a yeah this one is a multiple inheritance I can also Define it here multiple inheritance multiple inheritance I have to also pass The hierarchical Inheritance as well inheritance okay so currently I just use the uh multiple inheritance let me also Define The hierarchical Inheritance as well so I can just take it here one of the classes which is okay property okay okay properties I can M it because maybe I think some in build function or in build classes can be so I will not take the property I’ll just take the properties I’ll create one function is a uh okay land Square self I Define it here so that property father father mother both can acquire it at the same time child can also acquire it so I can also Define it here uh just normal statements so we have five acre ACR I think we can call call it property in India okay the normal statement is there so currently that properties classes cannot uh you know it’s not connected with a father and mother if they want to acquire it they can also directly pass it here the properties so this is just a normal example how the all kind of inheritance is working properties okay so simply uh properties that spelling is Miss wrong I think okay properties so there is no any error so in case if I’m creating the object of father and mother anything So currently uh the object of child is already present so let me also directly call that Rahul do properties Rahul do land Square so when I just use the land Square I don’t need to pass any kinds of arguments so Rahul can also access it why father and mother so same time if I creating the object of father and mother like f is equal to father and uh F do land square if I’m just calling it so you can also access it so inheritance one of the simple topic of objectoriented programming language so here I just use it the Hybrid inheritance inside a Hybrid inheritance so multiple and hierarchical is a covered so multi thing is remaining in the multi-level so you can also try it and let me know if you’re facing any issues but I’m I’m I’m damn sure you will not face any kinds of issues because hybrid is covered kind of all kind of uh levels because the multiple is already there so you have to just increase one of the labels so it become a multi-level inheritance let me also create one heading multi- inheritance okay so polymorphism is nothing but like many form the simply we are calling is the many form right many form we can directly say so when you’re talking about the definition of the polymorphism is simply if you have any kinds of function and the same function name being used in the different way let me just remove it that annotation yeah the same function name being used for a different types so not only the function uh usually we are using for a function that we are also calling the method but we can also do the same for operator as well so polymorphism can be achieved in the many way so the very first one I think you already aware heard about that that is Method overloading method overloading and uh the second one we are calling method overriding okay so and we have also the another type is also aaable uh that uh we have Operator overriding Operator overloading Operator overloading and uh we have also one more type is there the duct type okay why I mention there the operator overloading and duct type here because in this video and in the series we going to discuss about the method overloading and method overriding which is very very important um in the polymorphism perspective but you can also learn it the method over loading and duct type as well but if you want any if you want a video for uh operator overloading and duct type I can also make it it’s not much as much complicated okay so these are the way to achieve the method or uh to achieve the polymorphism concept let me also write it here this way we can achieve polymorphism okay so uh polymorphism we can also understand it there is two way the first one we can we can try to understand with without classes okay without without classes which I will discuss in this video okay without classes and we also discuss in with classes as well okay so with classes like here method overloading and method overriding that we can do it with classes but let’s understand it very simple way how polymorphism is working in um you know normal examples so let me also let me take one heading polymorphism okay startingly I will not make it much complicated overloading overriding we’ll discuss later but let’s understand it how polymorphism is working with a simple function without any classes polymorphism okay let’s say for example if you have um you know inbuild function is a len so Len is a function if you you pass a simple statement is hello okay so it will give you the answer is the length of five length of hello which is uh five but in case if you’re passing the length of any uh list 5 6 8 9 there is no any restriction to pass any kinds of data type I can directly pass a string that is also acceptable I can pass a list that is also acceptable if I’m passing Apple dictionary set anything that is acceptable so the one function have a multiple form to define the length of uh the length of container that container can be the string that container can be the list dle anything suppose if I’m defining any function here the def def add function I’m passing the values is x y and Zed okay x y and Zed and just uh returning the values return x + y + z so when you call this function the ADD and pass the values is 4 comma 5 comma 6 then you’ll get the answer like addition of all the values which is a 15 but in case if you’re passing only two values two arguments but you define the Define the three parameters but if you pass the two arguments definitely you’ll get the error but if you directly Define some uh you know default parameters which is if I’m defining the none here so that is acceptable here uh not a none uh none type cannot be added let me pass a zero okay pass zero so you’ll get the answer is 5 + 4 so the same function I can add two values the same function I can add add three values as well so here the add function being being like uh the same function is uh you know working for multiple forms so we can use it right so this there is a two techniques is a method overloading and Method overriding so that is a two different totally different techniques we’ll try to understand it and do it in practical way so how exactly the method overloading is working so let’s say for example if you have one class okay class and the class name is a uh for example is a cal So Cal class so inside a cal clock I can Define it multiple methods uh multiple methods but the name will be the same so the name is like def me method name is ADD I can pass the parameter is like X comma Y at the same time I can also Define one more method which is ADD and this time I will pass the parameter is X comma y comma Z okay okay so if you have a two different method is available but the name are name are same so when you create the object of the class so the class name is suppose C Class name is a cal and object is a c Cal okay and uh C do add if I I’m just calling it if I’m calling the add function with three parameter 5 comma 7A 8 then definitely you will get the addition but the same time if I am just calling the addition U addition of only two number suppose if I’m not passing the eight okay so function is already defined the add function is already defined so definitely this function should call but in a python this kind of you know normal overloading is not supporting the reason behind that the python is a interpreted language and it always take piing the current positions current positions values so here the current one is the add which is defined with the three parameters and if you pass the two parameters the previous one is not accepted it’s being overloading but if you’re defining if you’re passing the only two parameter it not able to call the previous one so the complete overloading is not acceptable in the Python programming language let’s try to understand in the Practical implementation okay uh the topic is Method overloading let me just uh write it down the heading here method overloading okay so the class I Define it the same example I will take it class calc okay inside a cal I’ll uh I’ll Define the uh function add and pass the parameter is a comma B okay and uh make sure that you have to pass the self uh par meter which is passing as a reference which is compulsory in in the Python programming language I’m just passing it here return of A+ B okay so and then I’ll Define the ADD and pass the uh parameters a comma B comma C okay there is three parameters and uh return the values A + B + C and me also Define a without parameter def add and self inside I’ll just pass the A and I will return return e that’s it so very simple example is there the method overloading so method overloading let me also write it down the definition here a see okay uh method name are same but different parameter method name are same with different parameter okay so here I make the same thing method name are same but the different parameter if I’m just I the structure is ready now I’ll create the object object is let me just see take it as a c is equal to Cal okay all right so here Cal is a class and the C is object I will call the method add the C do add and pass the parameter 4 comma 5 comma 7 so I’m expecting the answer is uh 19 5 + 4 9 + 7 16 so yes the answer is correct the 16 but here I pass the three parameters so definitely this method is run okay but in case if I’m passing only the two parameter what will happen so if I’m passing the only two two or ments this one is arguments okay let me also write it down here this one is a arguments and what you are defining is a parameter okay so here I’m passing the only two arguments and uh the line number five is already defined the um you know only two parameters so I’m expecting the answer should be nine but here is the getting the error it’s saying that the missing one required positional arguments which is C so why it’s showing this kind of error the reason behind that you can see here so the last method is defined three parameters and I pass only the two arguments so it’s not able to check the previous one because of the Python is a interpreted language so if I’m passing the parameters it taking the current one for example if I have the a is equal to 7 and a is equal to 8 so if I run it so it’s being completely over overridden okay so if I’m printing the a so it will getting the answer is eight so what will happen is the first eight is a store in the first seven is a store in a variable when I’m just storing the eight in the same variable so what will happen the previous values is removed so the same thing happening is here so previous value is not considered it’s taking the current one only so what I can do uh so simply the method overloading is not completely acceptable in the Python programming language but if I want to achieve it so we have a you know different way we can achieve it see in a python everything is possible but something is direct or something is indirect so the directly method overloading is not possible but indirectly you can use it so what I can do it here if you if I have a three variables so all three variables I Define it a b and c but make sure that you have to Define some parameters I’m I’m just defining it here zero okay I’m just defining it here zero uh or else or else I can uh directly Define it here is equal to none nothing is there because I I’m not defining anything right so uh is equal to none but a is not equ equal to none here so because if I’m just passing the only a values so it’s not is equal to none okay uh so it’s not none is equal to none it’s is equal to is equal to none hope so it’s correct okay all right so uh this way is not possible so what I can do it here let me just uh okay so let me take it as it is now I’ll Define it a different function here so what I I’m expecting that what is my target my target is very simple is that if I’m passing the three parameters it should be the addition of three number if I’m passing the two values it should be the addition of two if I’m passing the one values it will be just print as it is so how is possible I can Define it the same class let me just Define it C A LC class and Def add and uh pass the self and Define the a comma B comma C simply I I I can use it here the condition wise so if the a is not is equal to none okay and B is equal to is equal to none and C is equal to is equal to none so in that time I will return simply a okay and L if if a is not equal to none and B is not equal none and C is not is equal C is equal to is equal to none so in that time I will return a + b okay it’s not a comma it’s a and at the same time Al if a is not equal to none and B is not equ equal to none and C is not is equal to none in this time okay I will uh return the value A + B + C great so let me just create the object C is equal to calc C1 let me just check it because the previously I Define it so I don’t want to mix up here C1 is equal to calc all right C1 dot add if I pass the three parameter 5A 4 comma 6 Okay so you the answer is a 16 what happened def C where is that def C line number 20 where is the line number 20 oh oh sorry oh it’s not a def it’s a class sorry it’s 15 I’m getting the answer is 15 okay previously it was yeah 15 and if I’m passing the only two arguments it’s giving the answer is okay the C positional arguments is defining it here uh okay 11 line number 11 if I’m passing the ADD and pass the parameter a comma B comma C okay is equal to none let me Define it is equal to none let me Define it is equal to none and let me Define it so the 5 + 4 is getting the answer is nine so if I’m passing only one arguments five that’s it boom right your how it’s happening because if I’m passing the values and in case in case if I’m not passing anything let me also defining else is here none so if I’m not passing anything so it will not give the error it getting the none so what will be happen so if I’m not passing any parameter so default values is none none none okay so all the condition I Define it ke if one values is present a is present it will be return B A and B both are present it will be return c a b c both are all three are present A plus b plus C is working is here and in case if you’re not passing anything so the answer is none so this way method overloading can be achievable but directly the concept was method name are same but with a different parameter if you define it so that is not achievable directly in Python other programming language yes it’s achievable in Python is not possible so the next topic which we have to learn it here method overriding which is mainly used in almost every applications so in the last video in meod overloading what we discussed is ke the same method and same uh different parameter was there method name was same but different parameter but here in the polymorphism is being little bit more complicated method name are same and the same parameter as well right so the problem was the previous uh topic is the overloading that was not acceptable completely so the question is how it acceptable is the over overriding because here the method name and that parameter both are same it’s acceptable because the classes are different you can see here so there is a class is a shape class inside a sh shape class in there is a class uh sorry there is a method is a draw draw draw and there is no any parameter is available here right so I can say the same method but and the same parameter as well but all three method are available in the different different classes like Square class Circle class and he hexagon class so this method overriding is achievable with the help of inheritance concept so we already covered The Inheritance you can also just refer that videos as well so let’s do in a practical way how method overriding can be achievable in polymorphism topic okay let me just write the heading here method overwriting okay so suppose if you have a class class name is a father and uh father have um you know uh one method uh car okay and yes self parameter is very very important in object oriented in Python if you’re writing the code and a simple statement is that hey I have a car okay the car name you can also Define it here suppose the car name is um uh you know uh BMW okay simply BMW and there is also one more class which is a child and child have a bike okay and let just just normal statement here I have uh um sports bike Define it here uh the same thing is a BMW okay the father have a car and the child have a bike if I’m just calling it here uh you know the object of a child simply Rahul is equal to Rahul is a object the child is a class okay Rahul do bike and and uh uh when you just uh call the bike then you’ll get the answer is I have a sports bike which is a BMW and you can also inherit the father as well okay and if you inherit then Rahul can also access the property of a father so Rahul doar if I’m just calling so you can also access the car as well so Rahul is saying that I have a sports bike which is BMW and he’s also saying that hey I have a car which is is BMW so there is no any overriding till now but if I’m creating the a car method inside a child which is also present in a father car and the same method name and the same parameter like here I didn’t pass any parameter and in the child car I’m also not passing any parameter here so when I just writing here the different statement I have car Mercedes okay Merced is bench so here if I’m just running this this method the car so this time it will not calling to the father it will directly call which is available inside the child so here the statement is being changed I didn’t change the calling method here is the same thing as a car previously was also car but here it’s not showing the BMW it’s showing the I have a car which is Mercedes previously it was showing the BMW why it was showing because it was not present at that time and it inherited from the father and that is the reason it’s showing that yes I have a car which is BMW actually the child don’t have a car fathers have a car but now the child have their own car instead of writing the I have a car I have my own car okay Mercedes-Benz so the here the car method this car method is overridden from the father car so this way we can achieve the overriding concept here so there is also the techniques is available the uh operator overriding so there is also one more issues is exist which is uh if you’re creating the class we always creating the uh Constructor which is defined with underscore uncore init so every classes when we Define The Constructor the always the name are in it so when you define it so by default that init is being overridden but sometimes we have to we have to use both the Constructor so in that time how to achieve it how to solve that kind of problem so we’ll discuss in the next video so if the method overriding we have some issues like if we are creating the Constructor and uh uh how we can if the Constructor is also overridden then how can we use the previous Constructor yes parent class Constructor so super function is solving that that problems the super function is used to give the access to method and property of the parent or sibling classes so it’s kind of giving the access it’s accessing related things is there so this things you’ll understand in the practical way okay it’s not a theal topic so let’s let’s discuss about some scenarios where super function we can implement it let’s say for example I have a class okay it’s not a capital letter I have a class class name is um uh same is the father and the father I have one Constructor this time I’ll just use a Constructor last time I didn’t use any kinds of Constructor there so I’ll use a Constructor in it okay so make sure that you have to write a self here self and then just write it uh some some variable uh self. car okay that car name is I I will use it um I’ll use it maruti very simple simple car is here maruti Suzuki Suzuki okay all right and uh a function name is um you know info so inside that I’ll just writing here print I have a car and the car name I can also mention here the self dot car very simple okay same time I’ll also create one more class that is child the child have a Constructor uh the diff in it I’m so sorry yeah here I’ll just use it and it I’ll just pass it here so so you know that if you just um you know apply The Inheritance concept here it will be over done right so let me just complete the code and after that I’ll apply the uh inheritance concept here Def and and then uh self. bike that bike name I can also Define it here Duke okay Duke okay all right and then the same time def info okay I’ll just use the same thing here same function info okay and same parameter here I just didn’t use any parameter I here I’m also not using any kinds of parameter I’ll just use it here I have a bike okay I have a bike let me just write here the bike info right bike info I have a bike and and and I can also Define it here the self. bike simple all right okay let me just create object is Rahul is equal Al to child okay object is created Rahul do bike info I create the object yes I have a bike which his name is Duke all right so here I didn’t apply any kinds of inheritance concept so let me just use the inheritance when I just use the inheritance so I can also acquire the property of father so when I just use it here the self dot okay self dot uh sorry not a self do father I just use it here the father I’m so sorry yeah so yes Rahul can access the property uh yeah we can say the function which is bike info I can Rahul can also be access Rahul doino as well which is nothing but a car info let me also write it here the car info okay car info can also be access what is the issues here the child object ch object no attribute of the car why why it’s showing that no attribute to the car because here the two things is overridden here I have a car info okay that I can also access it but the problem is it will be needed self. car which is not available in this init okay let me just Al let me just write a very simple statement I have a car that’s it okay so you’ll not face any issues I have a car that’s it I have a car so Rahul can also the acquire the property of car info can be acquired but if the Rahul need any kinds of variable from the init from the init like Rahul want to access Rahul want to access the a car can yeah bike let me first take the bike bike yes Duke can be access simply can be access here can can Rahul be access a car which is available here is it accessible the answer is no Rahul cannot be accessed the car uh is it required a function here oh not a function okay sorry rul cannot be accessed the car because this in it here I just use the init because of this init and here I also use the init this is the same method is available with the same parameter it’s become overridden in a child class so you cannot be access it but still I want to use the car uh car variable I want to use it how can we do do that so you can use the super function super function super function. net super dot yeah and it so when you just use it uh okay there is some issues I can directly use it here super super is a function yeah and then use it in it okay uh sorry in it is not as in exactly this in it you have to write it here in it yes Suzuki I can also access it so I I can I I can’t directly write it like this you have to print it so when you just print a car bike you’ll get the bike and if you print a car you can also access the car so the simple is here when you just write it here the self. bike that means you can uh self bike you can easily access it because of this init is inherited from the father in in it so that means it’s become overden so the previous one is not access ible but still if you want to access it the super is nothing but your parent you know from where you are acquired it this is applicable for any kinds of hierarchal um inheritance any kinds of inheritance if you just using it the same concept will be applied the super is like child super is nothing but a father so the father variable can also be accessible if you are using the super function okay super function if you just using it you can access it and now if I’m just writing it here um self. car so that means you can access it so here I can see I have a car which is a Maru Suzuki okay I have a car is the maruti Suzuki but again it is applicable if you’re just using the same function is a car info okay and uh self is very very important car info and just write it here um I have a car which name is I can write it here uh B BMW okay so here showing the BMW because this same function is overridden okay but uh this function is not available definitely it will go back from father class and and use it but here the self. car is also available and uh which is available in a Constructor and Constructor is already overridden to to overcome this kind of problem to solve this kind of problem we are using the super function so super function is nothing but the giving the access the variable method and uh any kind of attributes from the super classes yeah father classes so the definition is also saying the same thing the super function is used to give access to Method properties of a parent or a sibling class okay the super function return return an object that represent the parent class okay so here super is represent that I I’m I’m a parent class okay whatever you need you can just call me so this way uh super function is working so let’s directly jump on the Practical implementation here okay so this this topic is very very simple uh we’ll just create the inner class we’ll just create outer class and try to call it how we can call it and what is the procedure we’ll uh discuss it step by step let me create one class class name is very simple just laptop okay inside a laptop and just for a confirmation um you know so that we I just call it so that we’ll get a confirmation so I’ll create one Constructor because Constructor is automatically called we don’t need to call it make sure that you have to write a self here and the statement this is this is laptop class just normal statement and after that it’s on you if you want to create any kinds of function you can create it but I’ll directly create a class here so class class name is HP okay inside that laptop there is a class is HP and uh again I’ll create object uh sorry not object Constructor I’ll create a Constructor and Define some name here print this is HP class inside the laptop class okay it’s very simple laptop class great and we can also create one one more function function name is info okay and this normal statement here hey this is is info function simple this is info function so the creating the inner class and outer class is really very very simple just we have to Define it which one is inner class and which one is outer class so this one you can understand like this look like outer class yes it is outer class it is outer class and this one we can say it’s a inner class okay sorry inner class all right so how to call it like my target is to call the info function which is available HP that HP is available in a laptop so let me first create object of laptop L is equal to laptop laptop okay or create it and uh if you create it the object so Constructor will definitely call yes uh this is laptop class it’s a class yeah this is laptop class okay and I want to call the info function fun which is available inside HP so H is equal to l do HP okay l. HP right because HP is not you can’t directly call the HP because which is available in outer class so first you have to call the outer class and after that you can call to the another uh inner class okay uh the laptop we don’t have any kinds of attribute okay all right then I can directly write here okay sorry uh HP is a capital letter yeah HP class this is HP class which is inside the laptop class and with h i can directly call the info that’s it so hey this is the info function and this is the one way to call the a function which is available inside the inner class but we can also directly call it here ke we directly create the object is H is equal to laptop okay dot uh HP we can call like this so inner class and outer class we can we directly create the object and that object will be the inner class which is HP and we can directly call the H Dot info if this kind of topic is there so definitely we have also the variable exist so the likewise instance function we have also the instance variable okay instance variable and uh we have also the class variable the same time we have also the static variable again this this kind of things we will discuss in the practical way then you’ll understand easily how the instance function class function and static function is working okay so let’s jump on the Jupiter notebook okay here so I’ll create very very very simple uh function here so the topic is instance function class function and um static function example if you creating any kinds of class with normal class uh class name is uh okay uh class name we can say the first class okay first class okay then normal the first class uh the class name is there and you also Define some one Constructor in it and pass this self and here you define it self dot um a is equal to and self do name is equal to you define it and name is hurry okay and Def info I Define it here and simply I Define it here he hurry right self. name I I I’ll just pass it okay self. aray so simply I just create the object let me create the object object name is FC is equal to first class and FC do info so here hey harry so he har is calling it here so I create the class this kind of um uh this kind of function this info function which I Define it s self as a reference it’s passing the references so that we can use the any kinds of variable within a class so this method we are calling the instance method so instance method and I also Define one variable which connect with self so this variable is instance attribute we are calling okay instance attribute or instance variable instance variable we can say inance variable let me create one more uh variable variable name is like um uh College college name okay college name is IIT Bombay I Bombay okay all right and U uh this instance function here you create it instance variable you also create it if I want to call it print and call it here the um instance variable can I just call it so FC do uh name if you’re calling so you can also get the values as hurry so that is also accessible with object as well okay I created the uh one variable is a college name and I want to access it that uh let me just directly write it here so what kind of variable we are calling actually this kind of variable we are calling the um you know class variable class variable we are calling and I’ll just pass it here college name so when I just call this I Bombay is accessible but uh this uh class variable this one is a class variable I if I want to use it any method how can you use the method okay this one is a class variable how can you use the method So Def class okay not class info one I can write it here better I can write it info one and this one in info two okay info one I can just call it here yeah info so again college name is not connected with a self right so in case if I’m writing it here self and uh just just pass it uh you know print college name and here I just pass it simply the self do self. college name so obviously it’s not accessible let me let me just call it FC do info2 okay um that is accessible here it’s accessible it’s not a problem I Bombay is also accessible college name uh but again this kind of variable is a uh class variable because it’s not connected with self self reference is not connected with that here self if you’re forcefully writing it here yes it’s accessible but this function we can’t call it as a um you know instance method yeah instance function we can’t call it if you write it here CLS see LS and then uh then this kind of method is called as a class method okay that is also accessible hope so uh but uh you don’t need to Define it yourself obviously CLS you have to Define it and it’s accessible so CLS if if you’re passing the reference as a CLS then it’s a class method but if you’re passing the reference as a self that is the instance method okay so there is also one more variable and one more mode method we can also use it uh def uh info 3 okay and if you’re not passing anything and make sure that uh we have also you know decorator we can also Define it so for a security purpose so here we can also write it class method okay so we easily uh Define it here decorator is a class method what we are calling we let me also write it out here decorator decorator okay all right so here I’m not passing the CLS I’m not passing the self so this kind of method and and whatever variable I’m just Define it here inside that um I’m I’m defining it a variable here U like um what is name role number yeah role number not a role number and we can say uh the ID student ID which is maybe the confidential student ID I’m just Define it here 1 121 09 okay 12109 this is the confidential and it should not be accessible outside okay and when you just run it you’ll getting the error not getting the error because I didn’t call it so fc. info3 when I just call it is getting the error like saying that take zero positional argument but one was given because because whenever you creating any kinds of method that automatically expecting that it should be either CLS or a self but here I’m not passing anything okay so that is the reason it’s making that here the take zero positional argument but one was given okay so uh one was given that this kind of error will coming definitely will come so how to overcome this kind of problems so this kind of method is called as a static method this kind of method is called a static method there is no CLS there is no self but how to uh you know solve this problem how to create it so there is a decorator you have to Define it here static method and when you just Define it then to not create any kinds of issues and this variable uh student ID if I want to call it static variable okay fc. student ID so that is not accessible there is no attribute because it’s defined in the static method and and it’s not passing the reference with self and uh this class variable that was available in the outside of that that is a global variable and Global variable for that particular class but here you can’t you can’t call it so where you you can use this kind of um method so simply I can just write it here this is uh my my student ID is student ID when I just pass it and and and showing that hope so is showing that yeah okay okay here is showing that uh student ID my student ID is this one okay and this variable we call calling as a uh static variable hope you understand this this all the method and function this one is a class variable and then class method we have we have a instance variable able we have a instance method we have is a static variable we have a static method we have okay so now it’s done and uh you please refer the Jupiter notebook uh if you don’t want to watch all the videos uh you know complete videos just refer the Jupiter notebook run it and try to understand but you face any issues go back to the video and check it where you’re stacking exactly and now we reach the topic of encapsulation we already covered the many topics like um you know the object class polymorphism inheritance okay so now we we have to discuss about encapsulation so encapsulation is really very very important for a security purpose hiding the information so let’s first discuss about the definition here so this one is saying that this put a restriction on accessing variable and Method directly can prevent The Accidental modification of the data to prevent The Accidental changes an object variable can be only be changed by an object method so those type of variable we are calling the private variable so this is the normal definition we will discuss about in a practical implementation as well so you can just imagine that you have a capsule inside a cap you know medicine capsule so whatever information is whatever uh ingredient is there so we can’t see that that is the hidden but we can see the medicine so likewise here we have hidden information we have important information like a method and variable which is available in the class and we can’t directly uh you know we can easily access the method and variable so we provide some restrictions so that the people cannot be accessed directly so there is the multiple way to protect it so we have a topic is called as access modifier so there is a three access modifier is available public protected and private okay so when you’re talking about how uh public protected private is working so before let’s understand that ke how we can access any kinds of method and variable see there is uh some access attribute is available so if you just use this uh get attribute has attribute set attribute so you have a you know you have opportunity you have option to you know get the any kinds of variable or method easily okay with the help of objects obviously and we have also the one more option is available building uh class attribute we can see all kinds of method and variables we can see that so we just try to protect the method and variable so we have access modifier so there is there is a restriction is providing this protect and private so what kind of restriction they are providing so we need talking about the first one is a public member the public member uh kind of variable a variable which is the publicly so the public member of a class let me just take the annotation yeah public member of a class are variable are available to everyone anybody can easily access it so they can access from outside of the class and they can and also other classes too so there is no any restriction for the public public member like whatever whatever variables and Method we have created that is the public member previously what we created that was a public member protected member is providing some restriction there so you can see here the protected member are those members of the class that cannot be accessed outside of the class but can be access from within the class and its subclasses so it’s providing the uh some restriction but not the entire restriction right it you can access it you can access it within the class but outside of the class you can’t access it but when you’re talking about the last one which is a private member so a private member of a class are only accessible within the class even sub classes can also not be accessible so when I just summarize it everything let’s understand the simple way the summary of uh the public protected and private public member Public Access modifier can be accessed with the same class same packages even sub classes and other packages as well so when you’re talking about the protected so protected cannot be access with to other packages but it can be easily accessed with a sub packages sub uh sub classes and same classes as well private access modifier a private member can be accesses within a class only it cannot be accessed from the any other packages or same packages or or any other classes so we already discussed in the last video is that how can we make the um you know static method and how can we make the class method that is the way where we can just uh you know protect our our methods and and some variables so there is also one more topic is called as uh private variables that I’ll I’ll discuss in the practical way let me jump on the Practical implementation to discuss about that inbu attributes the last video we discuss about this instance function instance uh class function and static function so here uh we discuss in the details way and just try to access it some some variables and Method so you can see here when I just use it the object the object here FC FC is the object so when I just use this fscore uncore the dict uncore uncore obviously I have to use the dot do dict you can see here so whatever variable is present here the variable is uh the name actually the instance variable is showing only it’s not showing any kinds of uh the class variable so that is also here you can say it’s protected so so kind of the public private and protected is the category to achieve that category we have a different different uh topic is available so here uh so you can say the class variable class method and static variable static method is also one of the way to achieve the en encapsulation so you can see here when I just use the dict so here uh any any college name is not showing it’s showing only the name right so we have also I also Define some uh documentation so with the help of FC dot FC doore uncore yeah so we can also see the documentation as well so with the help of object we can easily find it out you know other things we can easily access it any kinds of documentation any variables or anything here so like I want to find it out what is the is it uh you know the main module is there or any other module is also there so underscore uncore module you can also check it okay sorry it’s not a DF it’s FC doore module so you can also say check that so I’m working with the same files that’s why it’s showing that U main is here I can also check that the classes as well FC doore class so yes it’s showing that my class name is a first class with the help of object I can find it out anything easier right so I can also check that uh you know there is the variable name is an variable name is name okay variable name is a name okay so I can also find it out has attribute is that attribute is available or not so object I have to Define it FC and name I’ll just pass it it’s saying that yes that attribute is available so I can find it out that variable get attribute I can just use it and pass the values as FC and uh uh what is the actual values of the name is showing that hurry so so you can see here so with the help of object I can find it out any information that I want to protect it okay so like uh a set attribute I can Define it set attribute so outside of the U class with the help of object I can also create one variables instance variable FC and I can Define it here uh any any variable name is like a ro number which is not there hope so which is not there uh yes there is no any role number yeah role number kind of variable is not there so I can make it role number and and Define it the role number is a one 12 one okay so I I set the one variable when I just check that the FC FC doore dict you can see here there is two variable is present previously it was only hurry now the role number is also present right so and and I can also delete it so FC dot FC sorry Del Del attribute delete attribute I just want to remove the FC have to pass it and just pass the role number okay and now it’s deleted hope so uh DF is not defined it’s FC and the next time when I just check this here so it’s showing only hurry it’s not showing other things so this way we can easily access any kinds of uh variables any kinds of methods we can easily access it in a continuation of encapsulation we we discuss about access mod fire where we discuss a public member we discuss a protected member we discuss the private member as well so here we discuss about a private variables how we can make it suppose if you have a class class name is uh College okay college and I Define The Constructor and here I Define the name is equal to uh Rahul so this this is the variable name that variable you can say is the instance variable okay so instead of name I’ll just use a self. name so that I can easily access it within a class and uh create a object object name is like um you know C is equal to college and when I just try to access it like a c do name so you can easily access it which is Rahul and here I just want to write it uh create one um object self dot college ID okay college ID I just want to write it IIT Bay 001 56 suppose this is the college ID is there and uh when I just try to access it the college ID we can easily access it right C do college ID so you can easily access it but what happened uh okay what happened if in case if I’m just writing here the double underscore so what will be happening you can’t access it here is showing that the college ID is uh the college object no attribute of college ID let me also use it under double underscore here still you can’t access it so that means this kind of Vari uh in case if you’re using the two underscore before the any variable you can’t access it but normally if you’re writing any variable a is equal to 5 it’s it can be accessible and print a yeah and if you just using a double underscore a and here if you’re using the double underscore a so that is also accessible here is not a issue but if you’re doing the same thing in your object oriented so it’s become a private variable private variable so private variable have a only only one way to find it out only one way to access that kind of variables which is you have to call the college so with the help of object you can’t access it so here when I just try to access only the name so you can easily access it but if I want to access the C doore uncore college ID so you can’t so the time you have to call the object object name is a college underscore College you have to write it then you can access it so the private variable cannot be accessed directly so again the encapsulation is the way encapsulation is is a techniques where we have a different different topic is available like private variable static method and static variable so that that topic will help you to achieve the encapsulation because in Absolution is a techniques to protect our variable and methods so let’s let’s discuss about the how data abstraction is working data abstraction and encapsulation are synonyms as data abstraction okay so you can just understand with this images so encapsulation is also protecting the method as a variable but abstraction is providing the complete restriction so that you can’t enter it so to re to reach abstraction we need need encapsulation so that means first you have to protect the variable as a method and then you can provide the complete Shield right so just abstraction is is totally give the Restriction okay so so here is the definition is also there let’s let me just read it so that you’ll get a better idea and after that definitely I will discuss about the Practical implementation substraction is used to hide the internal details and show only the functionalities abstracting something abstracting something means to give the names to things so that the name capture the basic idea of what a function or whole program does is give the just basic idea okay to achieve the abstraction so there is a two way so an abstraction abstract class can have both the normal method and Abstract method both can be present an abstract method yeah we cannot create an object of abstract class so how to achieve that abstraction let’s discuss about it so data abstraction let me just create uh the yeah heading data abstraction okay so data abstraction um let me create one a class the class name let me just write it here a computer okay computer and yeah inside a computer let me just Define one uh you know one method normal method is a processing okay and self is very very important and just write it the normal statement anything uh just pass okay so this is not abstract class or abstract method it’s not here so when we can have a abstract method normally whatever you writing here okay whatever you are writing here um print this is computer class okay and I can also write the object C is equal to computer so when you just uh call it C do processing and you can easily access it so now let me just create this this class as a abstract class so you have to import from ABC abstract Blaze class import uh let me just take the ABC Capital so which is the main class is there and Abstract method as well abstract method abstract method yeah so here I will just inherit the ABC so now now the computer become the abstract method so when I just here the abstract method is available okay and uh this class this uh computer become the abstract class but if I want to make the method so I have to use the decorator so abstract method so here so when you just make so when you just make this um processing as a abstract method and uh your class become become the completely abstract class so you here is saying that cannot Institute abstract class computer with abstract method processing you cannot use it okay so you can’t create object of any abstract class if when when we are calling the abstract class if you inherit from the ab ABC abstract Base Class and if you have a method which is the abstract method so that time we are we are calling it’s the abstract class an abstract method so if I want to just create any um any other you know I want to use it this is computer class I I just want to use it so I can also create a different um um different classes here let me just create a laptop okay simply laptop and this time I will inherit the computer so computer is inherit the abstract Base Class and laptop is inherit the computer and and simple I’ll just write it here the processing anything I can just use it now here what will be happen so here is kind of um overriding so because I just inherited that so overriding here just using self I’m just using it here and uh whatever things is there I’ll just pass it is high so instead of creating the object of a computer I will just create the object of a laptop so let me just comment it here lap L is equal to laptop L do processing when I just use the processing you’ll get high because it’s become completely inherited if it’s completely inherited the previous one will not be used okay but in case instead of processing if you have a processing one and if you want to inherit the then again is facing the same problem you cannot okay you cannot even not only the um not only the calling the methods so if you’re still running so you cannot create the object of the class the laptop class as well because laptop is inherited the a computer computer is a completely abstract class right because it’s inherited the abstract Base Class so here you cannot use it until unless you’re not overriding the abstract method so here I’m just overriding the abstract method now I can create object I can also call any method as well so this is the way to deal with any abstract class so again if you have any kinds of U uh method which you want to protect it you can just use abstract me abstract method with ABC abstract Base Class finally we reach the last topic of this entire python series that is multi- threading multi-threading is very very very important for a software development even um uh you’ll find it out the separate job for uh you know for multi- threading so even when you’re entering uh the multi- threading let’s first discuss about the multitasking how exactly it’s working because dayto day day-to-day life when you’re using the computer so different different task we are just doing it let’s say for example I’m just doing a task is a word processing and and sending some emails and doing web browser and uh our in virus is running so that entire thing is running on the operating system right and that operting system is is is on a CPU course so even so multi-threading is not only the software topic uh like if you are a programmer you should know that it’s not like that if if you’re using a computer the multitasking and multi- threading is running inside your computer so let’s let me first show you how exactly the multi trading and multitasking is running in your computer you can just go to the taskbar you uh shortcut keys control alt delete you’ll find it out the taskbar go there the taskbar will open here so in your computer it will be open and just go to the performance and let me just maximize it yeah you can see here uh yeah you can see I’m just running the 312 process with the threads so what exactly the threads thread is nothing but a lightweight process so I I’ll discuss in details let first understand that like if I’m just running the multiple process my computer is running the 312 process which handles the 177,000 1 lakh 73589 right and that how much core is there 10 core so core is nothing but how your whatever process you’re running is the Distributing in the different different core that is CPU core okay and every process have a different different threads that means small process is there inside one process for example you can go to the processes 312 processes running go to the processes you’ll find it out which 312 processes there so five is ongoing processes there five apps is running I just open the uh PowerPoint I open the taskbar WhatsApp Zoom is running so much things running is here and and another process is also running the background process because sometime you confuse that here is the 312 process where exactly is that see some background process is also there and other processes also consuming right so after uh the entire process is 312 is showing it here so let’s understand it like how trades is working threads is nothing but just a small process of the particular process it’s confusing let me show you how exactly is working let’s say for example I choose the uh normal things is is game okay in the game I choose the I just open one game so game is a one process okay game is one process but inside that the multiple process is running that is consu that is producing the sound that is Graphics that is U when a player is uh you know it’s a different different opponent is there the multiple prer is playing the games so kind of like the multiple Pro multiple small small processes running inside the game app your game process so simple here so what exactly the multitasking multitasking refer to the ability of operating system to perform a different task at the same time because here in the operating system I’m just running the multiple process at the same time right so as I said that uh when you’re going for a job so you’ll find it out the multi- threading is also very much required if you are entering the software development that can be the web development that can be the application development with a python not only the python if you are going with a Java that time is also required the multi-threading right so you can also search it on no.com I just took the screenshot here but yeah lots of job opening is there for multi-threading that is also required and uh the interviewer is asking the question right yeah so here I took the example is a word processor okay word processor one one example I just taking it here inside that so Graphics is there and responding to keystroke and grammatical check that is a small small process which is there inside a word processor right so that small small processor that small small process is called as a dats simple word what exactly the threads so thread is nothing but a lightweight process thread is a lightweight process which is independently flow up execution that means there is no dependent that when uh Graphics will complete then the cas keystroke will run no there is independently running so whenever you making the application so threading is playing the very very important role because we have to we have to achieve the independent iny right so here so there are two types of multitasking in operating system that is a process best and thir best right so what is a process best because the multiple threats running on the operating system simultaneously for example for example is listening the song and playing the game is is two different process running in the operating system but when you’re talking about the threats so thread is a single process consisting of the separate task right for example a FIFA game is a consisting of the multiple threads so one game is a consisting of multiple threads is there so now the question is what is threat even I already explain that thread is nothing but a lightweight process so inside the threads in inside inside the one process the multiple threats is there is this example is mentioned there so the question is where we can use it what is the purpose of thread when we required the threads is there so threads when we have a multiple task need to achieve you have multiple task and you want to independently running it so task don’t have interdependency right if I’m running the one particular function there is uh four function is there fun one and fun two and fun three and fun four okay so I so python is a procedural programming language and functional as well and objectoriented as well but usually is following the top to B bottom approach when the fun if in case if I call the first function second function third function fourth function accordingly it will run but there is no any relation between the function one to function two it can be independently run so in the time we required a mult the training so that is the mean of interdependency it should not be any kinds of interdependency between the two different function right so there is two way to create the threads in the Python programming language the first one is without creating a class directly with a function we can create it and with a class we can achieve the threads okay so there is the four things which is very very important to know about that how to perform the multi- threading task so run there is run method which is which method is entry point of the threads and start method which is start the thread when you call the run method it will start the trade and join method it will be uh wait for Threads to terminate it and is alive will check that your thread is still alive or not and get name get the get name will return the name of the threads so this five method will help you to perform the entire thre operation uh let’s take example suppose I have a two task the task number one read um read data from database and there is a 10 data I have to read it right and the second task I have um read um PDF file okay that is also 10 PDF I have so there is a two different task and uh this one is taking every every database every data is taking a 0.5 second that means it will taking okay 0.5 second one task will take 0.5 second one task will take so it will taking a 5 Second okay all right and the second one second one read the PDF so 0.3 second is taking so it will taking completely a 3 second okay so I’ll try to optimize it in this Pro this problem right so the complete if you if you’re running the entire task it will taking the completely 8 second I’ll try to reduce it to make it around U uh 5 Second okay let me take the four and this will be 4 second so total will be total will be 9 second great so let’s do the do the Practical implementation here def I’ll create one one uh function def um read data okay read data and simple I’ll just create of follow for I in range and yeah the 10 data I have to read it and print the normal statement read the data from database okay and uh I + 1 because it will start from zero right and let me also take the time import import time or I can yep and every time it will take time do slip 0.5 second 0.5 second I’m uh so every process is will taking a 0.5 second and then def read PDF for I in range 10 I will pass and time do sleep and 0.4 second and print the statement um read the data from uh PDF okay read the data from PDF and and then I plus one as well because it will start from zero all right great so yep I’ll just print it here start time strd start time is equal to uh time dot time I think time do time is there let me let me just print it time do time yeah so time. time it will starting time it will take and uh mhm start time and at the end when I just call it I’ll just call this function rate data and rate PDF okay and I will take it here the end time is equal to time. time time. time and uh process took time process time I can make it here end time minus start time okay so it will start the process so first it read the entire data from database and then uh the PDF will start and here it’s taking a 9 second why it’s taking the 9 second the reason behind that the reason behind that because the first one took the 5 Second and second one took the 4 second that is the reason it’s more than 9 second is taken here so how can I reduce it this time so we can use the multi-threading to reduce this particular time is here so there is a librar is called multi- threading from multi threading multi threading import I’m taking a everything like even I required here the thread so I’m taking the Trad here oh sorry uh better I can just take this libraries in the outside so that it will be oh sorry uh I can undo it yeah undo delete yeah here I can pass my library okay multi-threading spelling is wrong M ready okay it’s not a multi- Threading uh sorry it’s a Threading only sorry yeah threading import thread So currently whatever process I have it’s not inside the thread so I’ll try to make it the inside thread so so this one is not required this one is just taking the time this one I’ll just make it comment and I’ll make it a thread so T1 thread so inside a thread because there is no any class so you have to decide the target what is the target the target is uh read data the second one is T2 is equal to thread what is the target the target is read PDF and uh the simple one let’s start a T1 dot start uh start the U uh thread here t1. start and then t2. start when I start this two different threads so here the threads will run independently there is no relation between the read PDF and read data so the same time your data from the DAT reading data from database and reading the data from PDF will start the same time so you can save a lots of time is here you can sa then tar 4 second let me just run it oh what what’s the issues spelling is wrong seriously mhm d h r e a d hey it’s it’s correct oh okay spelling is wrong yeah sorry yeah so it started the time is started here the process time okay uh the problem is here it’s attached with um uh the completely this time is attached with our threads okay I have to also explain one more thing is here so there is always one thread is always active which called as a men thread so because of this because of this threading so this one is also considered as a Threading this one is also independently run this file right this is independently run this file in that situation what I have to do I have to just join it t1. join t2. join so what happen is here so T1 and T2 is joined it’s not joined with the men threate right so here this this entire this process this this two Lines line number 27 and 28 is not joined with T1 and T2 so T1 and T2 is running the independently so that is the reason you can see here it’s reading the PDF then database PDF database and sometimes the PDF two time as well the reason is taking only 2 second and the entire process is ending with 5 Second only previously it took a 9 second I save a 4 second so that means if you have the big process if you have a big process so and and imagine that imagine that uh currently I have only two process you have a 15 process and all the 15 process have independently running so what which process is taking more time inside that the other process can also be finished it so multi- threading is saving a lots of memory lots of time so that’s why it’s very very important for a software development so usually the software we are making with class class and object read data from database that it will take 10 second and the second one read PDF from read data from PDF yeah we can say read PDF files that is taking a 4 second there is a 10 files and every files is taking 0.4 second the total time is 9 second okay so I’ll try to create with the class here the multi-threading so simple uh class name is class of read data and uh make sure that if you’re if you’re using the multi trading so you have to inherit the thread so let me just use the prom threading import thread and from time UT C okay great so I have to import thread here okay all right and inside that you have to create a function yeah we can say method def run method okay run method self is very very important okay and after that give me a minute after that just we have to run the file for I in range and the 10 times and uh let’s apply the slip 0.5 second right this normal statement okay read data from database database right this is one threade I just created and the second one is class read PDF and thread colon def run and pass the self is here for I in range pass the values is 10 okay the same thing I’ll just use it here slip 0.4 second and print the statement read PDF data that’s PDF data all right and how it will be run just call create object of that particular thread T1 because the every class is already become the thread because it inherited from thread class is equal to read data okay so if you’re not using this a threat inheritance let me just show you okay let me just show you I’m not using inheritance of the thread and uh let’s create the object T2 do T2 is equal to read PDF so when I just run it and let’s run this file here T1 dot T1 dot run okay and t2. run and uh just just normally Define it here the timing okay start start time is equal to time dot okay time sleep and time let me just take both time and yeah just time you can just take it and uh at the last time this end time okay that is the end time and uh just print it here process time that is like uh end time and time minus start time okay so it will it will tell you like how much time it took to complete the entire process we know that here it will take uh the 9 second because the 5c first read data will take and the second one 4 second will take so yeah it’s took the 9 second yeah let me also just Define it the number which file is reading I + 1 because it’s starting from the zero so that’s why I’m just mentioning here the I + 1 great so let’s apply the inheritances here let’s apply the threading with inheritance uh um concept so we just inherit the thread and let me also inherit the thread so if you use the method is a run and uh you don’t need to call the run so now the entire process has become the complete thread right so this this entire classes become the thread this this one is a thread and this one is a thread so now I want to run it then t1. start that’s it t2. start that’s it now when you just start it the process will start again yeah so this one uh this print statement this is completely a separate threats that is the reason that is also being independent so that is it’s running first so let’s make it dependent there is if I’m asking that how many threads is running is here so there is a three thread is running the first one is read data second one is yeah we can say T1 T2 and the third one is men thread so men is always there so all menth is being independent so making the T1 and T2 is a dependent you can make it t1. join and t2. join so now you both will join and the men threate is independent so the line number 27 will be running dependently right when the T1 and T2 will finish then 20 line number 27 will run then you’ll find it out how much time it took it here the last time it took 9 second now it’s taking only a 5 Second so this is the beauty of multi-threading so let’s also discuss the other things um how many threats is there we can also check it and U uh yep so if if I’m just printing it here in in the next line I can just directly print it here print uh like a T1 do um um I can check it like is alive or not uh we can we can normally check it so it will tell you yes it’s it’s false because uh the process is end so it will tell you yes it’s a false so if you’re running inside that in inside that um uh threads like here if if I’m just after start if I’m just running so definitely give the answer is the the threads is running or not the T1 thread check all right 31 thread check it will check the true because the threads is running right so with the help of is alive we can check it the threads is running or not so we can also check the other things like um uh the active count okay so here when I just check it in between anywhere we can check check it here um number of threads we can check it number of thread so we can check it so simply we can just pass it here active count there is a method is called as active count okay so active count when I just run it it will tell you the number of threads is eight is running yeah like number threat is 8 is running because the complete active count is 8 is here okay and uh so the same time like here uh what is the thread name is I can also check that what is the exactly the thread name is here so at the same time I can also check it here the current thread dot get name okay current thread. getet name so we can also check it like which thread is running that uh particular line and here you can also check it right so when I just run it it will tell you okay so it’s showing that 32 3T like the thread number is showing that and the last one is a men thread so now here some somewhere like number of threads is there because the entire thread is not stopped that is the reason it’s showing that is a eight so when I just checking the outside how many threads is running outside here so it’s showing six so the six is running the two is not considered here so hope you understand how exactly this working right so active alive we can check that that particular thread is running or not how many uh active thread is there and what exactly the thread name is here you can easily check it with all the methods oh

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • Analyzing Restaurant Data and Movie Trends

    Analyzing Restaurant Data and Movie Trends

    “01.pdf” outlines a Jupyter Notebook project focused on analyzing a Zomato restaurant dataset. The session aims to teach data visualization techniques like bar charts, line graphs, histograms, box plots, and heatmaps to extract insights. Specific questions to be answered include identifying popular restaurant types, understanding customer ratings, analyzing order frequency based on dining options, and determining spending patterns. The initial steps cover uploading the data, importing necessary Python libraries (Pandas, NumPy, Matplotlib, Seaborn), and reading the CSV file into a Pandas DataFrame for analysis and visualization.

    The second source details a Python-based data analysis project using a Netflix movie dataset. The goal is to answer questions about movie genres, popularity, and release years by employing exploratory data analysis (EDA). The process involves importing libraries (NumPy, Pandas, Matplotlib, Seaborn), loading the data, and performing data cleaning tasks such as format conversion and handling missing/duplicate values. The project then focuses on data visualization and statistical analysis to identify popular genres, highly-rated movies, and trends in movie releases over time.

    The third source presents a Python project centered on analyzing an e-commerce dataset to understand customer behavior and sales trends. The objectives include performing data analysis and visualization, specifically monthly sales and profit. The initial steps involve setting up the environment by opening Jupyter Notebook, uploading the dataset, and importing essential Python libraries, including Pandas and the Plotly visualization library. The project intends to clean the data and then generate insightful reports through various visualizations.

    Python Project Analysis Study Guide

    Quiz

    1. According to the “Python Complete Crash Course,” what is the primary reason recruiters focus on project experience during interviews?
    2. Name at least three Python libraries mentioned in the “Python Complete Crash Course” that are commonly used for data analysis. What is the general purpose of each?
    3. In the Zomato data analysis project, what was the initial step after uploading the dataset into the Jupyter Notebook? What Python function was used for this?
    4. Explain the purpose of the user-defined function handle_rate in the Zomato project. What data transformation did it perform?
    5. Based on the Zomato project analysis, which type of restaurant (listed in the ‘listed_in(type)’ column) receives the majority of food orders? What evidence supports this conclusion?
    6. According to the Zomato project, what is the general rating range (out of 5) that the majority of restaurants receive? What visualization was used to determine this?
    7. In the Uber case study, what was the initial problem in Paris in 2008 that led to the idea for Uber?
    8. Describe the evolution of Uber’s service from its initial concept to the different types of ride-sharing options available today, as mentioned in the case study.
    9. Identify at least three ways Uber utilizes data science and analytics in its operations, according to the case study.
    10. What were the months identified in the Uber project analysis with the least number of Uber bookings? What possible reason was suggested for this trend?

    Quiz Answer Key

    1. Recruiters focus on project experience because projects demonstrate practical application of skills and the amount of work a candidate has actually done, which is more telling than just theoretical knowledge in a short interview.
    2. Pandas: Used for data manipulation and cleaning, providing data structures like DataFrames. NumPy: Used for numerical computations and mathematical operations. Matplotlib and Seaborn: Used for data visualization, creating graphs and charts.
    3. The initial step was to create a Pandas DataFrame by reading the Zomato CSV file into the Jupyter Notebook. The Python function used was pd.read_csv().
    4. The purpose of the handle_rate function was to clean the ‘rate’ column by extracting the numerical rating value as a float and removing the ‘/5’ suffix. This converted the rating into a usable numerical format.
    5. Based on the count plot visualization, the ‘Dining’ type restaurant receives the majority of food orders, as indicated by the highest bar representing the count of this category.
    6. The majority of restaurants receive ratings between 3.5 and 4 (out of 5). This was determined using a histogram visualization of the ‘rate’ column, which showed the highest frequency of ratings within this range.
    7. The initial problem in Paris in 2008 was a snowy evening with limited public transport, leading to frustration and the idea for a technology to easily book rides.
    8. Uber initially started as a ride-sharing platform where costs were divided among passengers going in the same direction. It gradually evolved to allow on-demand booking of individual rides and expanded to offer various options like UberX (affordable), Uber Pool (shared rides), Uber Black (premium), UberXL (larger groups), Uber Freight, and Uber for Businesses.
    9. Uber utilizes data science for TA estimation (arrival time prediction), price prediction, route optimization, driver-rider matching, and fraud prevention in payments.
    10. The months identified with the least number of Uber bookings were November, December, and January. The suggested reason was the cold weather and snowfall during these winter months, particularly since the data is US-based and Paris was an early international expansion location.

    Essay Format Questions

    1. Compare and contrast the objectives and methodologies of the Zomato data analysis project and the Uber case study analysis. What were the key insights gained from each, and how could these insights be valuable to the respective businesses?
    2. Discuss the importance of data cleaning and preprocessing in both the “Python Complete Crash Course” examples (Zomato and Uber/Netflix). Provide specific examples of cleaning techniques used and explain why these steps were crucial for accurate analysis.
    3. Evaluate the role of data visualization in understanding and communicating the findings of the Zomato and Uber/Netflix project analyses. Describe at least three different types of visualizations used and explain what information each visualization effectively conveyed.
    4. Analyze the business implications of the findings from either the Zomato or the Uber project. How could the identified trends and patterns (e.g., popular restaurant types, peak booking times, popular movie genres) inform strategic decision-making for the company?
    5. Reflect on the process of conducting a data analysis project as demonstrated in the provided sources. What are the key stages involved, and what skills are essential for a data professional to effectively execute such projects from data acquisition to insight generation?

    Glossary of Key Terms

    • Library (in programming): A collection of pre-written code that provides functions and tools to perform specific tasks, saving programmers from writing code from scratch. (e.g., Pandas, NumPy, Matplotlib, Seaborn, Plotly).
    • DataFrame (Pandas): A two-dimensional, tabular data structure with labeled rows and columns, similar to a spreadsheet or SQL table. It is a primary data structure in Pandas for data manipulation and analysis.
    • CSV (Comma Separated Values): A simple text file format in which values are separated by commas and each line represents a row of data.
    • Jupyter Notebook: An interactive web-based environment that allows users to create and share documents containing live code, equations, visualizations, and narrative text. It is commonly used for data analysis and exploration in Python.
    • Data Cleaning: The process of identifying and correcting errors, inconsistencies, and inaccuracies in a dataset to improve its quality for analysis. This can involve handling missing values, removing duplicates, and standardizing formats.
    • Data Preprocessing: The steps taken to transform raw data into a format suitable for analysis. This can include cleaning, transforming, integrating, and reducing data.
    • Data Visualization: The representation of data in a graphical format (e.g., charts, graphs, maps) to make it easier to understand patterns, trends, and insights.
    • User-Defined Function: A block of code defined by the programmer to perform a specific task. It can be called multiple times within a program to reuse the code.
    • API (Application Programming Interface): A set of rules and protocols that allows different software applications to communicate and exchange data with each other.
    • Data Analyst: A professional who examines data to identify trends, answer questions, and provide insights to help organizations make better decisions.
    • Data Scientist: A professional who uses scientific methods, algorithms, and systems to extract knowledge and insights from data in various forms.
    • Machine Learning: A subset of artificial intelligence that enables computers to learn from data without being explicitly programmed.
    • Algorithm: A step-by-step procedure or set of rules to solve a problem or accomplish a task.
    • Feature (in data): An individual measurable property or characteristic of a data point. In a table, features are represented by columns.
    • Insight (in data analysis): A meaningful and actionable finding or understanding derived from the analysis of data.
    • Count Plot (Seaborn): A type of bar plot that shows the counts of observations in each categorical bin.
    • Histogram: A graphical representation of the distribution of numerical data, where the data is grouped into bins and the height of each bar represents the frequency of values within that bin.
    • Box Plot (Seaborn): A standardized way of displaying the distribution of quantitative data based on five summary statistics: minimum, first quartile (Q1), median (Q2), third quartile (Q3), and maximum. It can also show outliers.
    • Density Plot (Seaborn/Distplot): A visualization that shows the probability density function of a continuous variable, providing a smooth estimate of the distribution.
    • Value Counts (Pandas Series): A method that returns a Series containing counts of unique values in a Pandas Series (a single column of a DataFrame).
    • Map Function (Pandas Series): A method used to substitute each value in a Series with another value, which can be derived from a function, dictionary, or Series.
    • Group By (Pandas DataFrame): A powerful method to group rows in a DataFrame that have the same values in one or more columns, allowing for aggregate calculations on these groups.
    • Reset Index (Pandas DataFrame): A method used to reset the index of a DataFrame to a default integer index. The old index can be kept as a new column.
    • Drop Function (Pandas DataFrame): A method used to remove rows or columns from a DataFrame based on specified labels or index.
    • Concatenation (in data manipulation): The process of joining two or more datasets (e.g., DataFrames or Series) along a particular axis.
    • Categorical Data: Data that represents categories or groups (e.g., restaurant types, movie genres).
    • Numerical Data: Quantitative data that can be measured or counted (e.g., ratings, votes, revenue).

    Briefing Document: Analysis of Provided Sources

    This document provides a summary of the main themes, important ideas, and key facts presented in the provided excerpts. Quotes from the original sources are included where appropriate to illustrate the points.

    Source 1: Excerpts from “01.pdf” (Python Complete Crash Course in Hindi | 5 Python Projects)

    Main Theme: This source is an introduction to a Python crash course focused on building five real-time projects, specifically aimed at individuals preparing for interviews in data analysis or data science roles. The emphasis is on practical implementation and building a strong portfolio of projects to showcase skills to recruiters.

    Important Ideas and Facts:

    • Course Objective: The primary goal of the course is to equip learners with practical Python skills through project-based learning, making them more confident and prepared for job interviews.
    • Quote: “The objective of creating the course is that whenever you go for an interview Are recruiters interviewing you for half an hour? I can’t ask the whole Python question. That is why they ask the most questions on your projects and your It is the projects that tell this story that how much work you do that’s why this”
    • Project-Based Learning: The course includes five “real timer” projects, implying they are designed to simulate real-world scenarios. One specific project mentioned is related to Zomato data analysis.
    • Quote: “There are five real timers inside it Projects include such as JumT”
    • Beginner-Friendly Approach: The course is designed for beginners with no prior “HiFi coding” experience, starting from the very basics and gradually advancing.
    • Quote: “Starting from the very basic level, here we will talk about things We will start with this and gradually advance The whole project will reach this level Every single line of code that is written in I have told you one line at a very basic level. explained in very simple language so that Complete all your five projects easily”
    • Focus on Implementation: The course emphasizes the practical application of Python concepts in the projects.
    • Quote: “There will be a lot of focus on implementation, whatever We are talking about all these projects”
    • Interview Preparation: The projects are designed to help learners answer interview questions effectively, as recruiters often focus on practical experience demonstrated through projects.
    • Quote: “if you complete it then you will see it in real time The idea would be that a data analyst is a How can a data scientist work inside a company? if we work with it then this whole Projects are its Notes Data Set Code file all things description box below It is already mentioned and this course Your confidence level after completing it It will definitely get a boost, so let’s get started”
    • Zomato Data Analysis Project: The excerpt details the initial steps of a Zomato data analysis project, including:
    • Understanding the context of Zomato’s business (restaurant partners, customers, delivery partners).
    • Identifying potential insights a data professional can extract (e.g., customer behavior, revenue growth, offering coupons).
    • Demonstrating the process of uploading a CSV dataset into a Jupyter Notebook.
    • Creating a new Jupyter Notebook file and naming it “Zomato Project”.
    • Adding a heading “Zomato Data Analysis Project”.
    • Importing essential Python libraries for data analysis: Pandas (as pd), NumPy (as np), Matplotlib.pyplot (as plt), and Seaborn (as sns).
    • Reading the Zomato CSV data into a Pandas DataFrame using pd.read_csv().
    • Addressing potential errors in file names (e.g., spaces).
    • Displaying the first few rows of the DataFrame using df.head() to understand the data structure (restaurant name, online order status, table booking, ratings, votes, cost for two, listed dining type).
    • Data Cleaning and Manipulation:
    • Creating a user-defined function handle_rate to extract the numerical rating from the ‘rate’ column, which initially includes “/5”.
    • Using the .apply() method with the handle_rate function to clean the ‘rate’ column and convert it to a float.
    • Data Visualization and Insight Extraction:
    • Analyzing the ‘listed_in(type)’ column to determine the most popular type of restaurant using sns.countplot(). The conclusion is that “Majority of the Restaurant Falls in Dining Category”.
    • Analyzing the ‘rate’ column using plt.hist() to understand the distribution of ratings. The conclusion is that “Majority restaurant Receive ratings from 3.5 to 4 for majority of restaurants which Ratings are between 3.5 to 4 out of four It is getting a good rating”.
    • Analyzing ‘online_order’ and ‘book_table’ using sns.countplot() to understand customer preferences for online ordering and table booking.
    • Analyzing the ‘location’ column and visualizing the number of restaurants in each location using plt.figure() and sns.countplot().
    • Analyzing the relationship between ‘approx_cost(for two people)’ and ‘listed_in(type)’ using plt.figure() and sns.barplot().

    Source 2: Excerpts from “01.pdf” (Continued – Uber Data Analysis Project)

    Main Theme: This section transitions to an Uber data analysis project, focusing on analyzing ride data to extract insights relevant to the company’s operations and customer behavior. It also connects this project to preparing for data science/analyst job interviews by simulating a take-home assignment.

    Important Ideas and Facts:

    • Interview Simulation: The project is presented as a way to prepare for the first round of data science/analyst interviews, where companies often provide case studies or assignments.
    • Quote: “If you get a call for that then this project Through which our round number one is Where the company will give you a case study How to solve the assignments she sends how do we submit it should make projects like this that we prepare ourselves for this round Was able to create potential and for this round You should be so capable that we can take out the round So these things are also included in today’s project You will get to learn how to The company provides case studies,”
    • Uber Company Overview: A brief introduction to Uber Technologies is provided, including its nature (American multinational transportation company), services (courier, food delivery, freight), headquarters (San Francisco), global presence (over 70 countries, 10,500+ cities), user base (150 million+ monthly active users, 6 million drivers), trip volume (2.8 million+ daily trips), and revenue ($37.2 billion for 2020-23).
    • Uber’s Origin Story: The excerpt recounts the founding of Uber by Camp Garrett in 2008, inspired by the difficulty of finding transportation in Paris during a snowy evening. The initial idea involved ride-sharing to divide costs, which later evolved. Travis Kalanick joined as a co-founder, bringing technological expertise. Uber launched its beta version in San Francisco in 2009, with the first booked ride in June 2010. International expansion began in 2012.
    • Uber’s Service Types: The different types of Uber ride-sharing services are mentioned: UberX (affordable), Uber Pool (shared rides), Uber Black (premium), Uber Group/UberXL (larger groups), and expansion into Uber Eats, Uber Freight, and Uber for Businesses.
    • Uber’s Revenue Model: Uber earns money through commissions on rides, subscriptions, and advertising.
    • Role of Data Science in Uber: The importance of data science in Uber’s operations is highlighted, specifically in:
    • ETA (Estimated Time of Arrival) Estimation: Using machine learning algorithms to predict arrival times.
    • Price Prediction: Using data analysis to determine ride fares.
    • Route Optimization: Algorithms are used to suggest efficient routes.
    • Matching Drivers and Riders: Data science helps in creating optimal matches.
    • Fraud Detection in Payments: Ensuring secure transactions.
    • Uber’s Data Science History: Uber started working on data science and analytics around 2010-2011, with Kevin Novak as the head of data science.
    • Uber Data Analysis Project – Data Loading and Initial Cleaning:Loading an Uber dataset into a Pandas DataFrame named data_set using pd.read_csv().
    • Handling missing values in the ‘PURPOSE*’ column by filling them with “NOT OK” using fillna().
    • Converting the ‘START_DATE*’ and ‘END_DATE*’ columns to datetime objects using pd.to_datetime() with errors=’coerce’ to handle potential invalid date formats.
    • Extracting the date and time components from the ‘START_DATE*’ column into new columns named ‘date’ and ‘time’ using pd.DatetimeIndex().
    • Categorizing the ‘time’ column into ‘Day Night’ categories (Morning, Afternoon, Evening, Night) based on specific time intervals using pd.cut().
    • Removing rows with any remaining null values using data_set.dropna(inplace=True).

    Source 3: Excerpts from “01.pdf” (Continued – Uber Data Analysis Project – Data Visualization and Analysis)

    Main Theme: This section focuses on performing data visualization and extracting insights from the cleaned Uber dataset to answer specific business-related questions.

    Important Ideas and Facts:

    • Data Visualization Setup: Setting up the environment for data visualization using Matplotlib (as plt) and Seaborn (as sns).
    • Analyzing Ride Categories and Purposes:Creating subplots using plt.figure() and plt.subplot().
    • Using sns.countplot() to visualize the distribution of rides by ‘CATEGORY’. The insight is that most Uber rides are for “Business” purposes compared to “Personal”.
    • Using sns.countplot() to visualize the distribution of rides by ‘PURPOSE’. The insights include that the most frequent purpose is “Meeting”, followed by “Meal/Entertain”, “Customer Visit”, “Errand/Supplies”, “Temporary Site”, and “Office Supplies”. Null values (labeled “NOT OK”) are also present.
    • Analyzing Ride Timing:Using sns.countplot() to visualize the distribution of rides by the created ‘Day Night’ categories. The insight is that most Uber rides are booked during the “Afternoon”, followed by “Evening”, then “Night”, and the least in the “Morning”.
    • Analyzing Monthly and Weekly Trends:Creating a ‘month’ column by extracting the month from the ‘START_DATE*’ column using pd.DatetimeIndex().month.
    • Creating a dictionary month_label to map numerical month values to month names.
    • Mapping the numerical ‘month’ column to the ‘month_label’ to get a categorical month column.
    • Using pd.DataFrame() and groupby() with count() to get the count of rides per month.
    • Creating a line plot using plt.plot() to visualize the trend of Uber rides over the months. The observation is that rides are least booked during the winter months (November, December, January).
    • Creating a ‘day’ column by extracting the day of the week from the ‘START_DATE*’ column using data_set[‘START_DATE*’].dt.weekday.
    • Creating a dictionary day_label to map numerical weekday values (0-6) to day names (Monday-Sunday).
    • Mapping the numerical ‘day’ column to the ‘day_label’ to get a categorical day column.
    • Using data_set[‘DAY*’].value_counts() to count the occurrences of each day.
    • Using sns.barplot() to visualize the number of rides booked on each day of the week. The insight is that Friday has the highest number of bookings, while Saturday and Sunday have the least.
    • Analyzing Ride Distance (Miles):Using sns.boxplot() to visualize the distribution of ride distances (‘MILES*’). The initial box plot shows a wide range of distances, with most rides concentrated at lower mileages but outliers going up to 175 miles.
    • Creating a filtered DataFrame (data_set[data_set[‘MILES*’] <= 100]) and using sns.boxplot() to focus on rides within 100 miles. This provides a clearer view of the distribution within this range.
    • Further filtering for rides less than 40 miles (data_set[data_set[‘MILES*’] < 40][‘MILES*’]) and using sns.distplot() to visualize the density distribution of these shorter rides. The insight is that most rides are within the 0-10 mile range, with a significant number up to 20 miles, indicating that people primarily use Uber for shorter distances.

    Source 4: Excerpts from “01.pdf” (Continued – Netflix Movie Data Analysis Project)

    Main Theme: This section introduces a new project focused on analyzing a Netflix movie dataset to extract insights about movie genres, popularity, and release trends. This is also framed as a potential data analysis task within a large media streaming company.

    Important Ideas and Facts:

    • Context: The project simulates working as a data professional within Netflix.
    • Netflix Company Overview: A brief overview of Netflix is provided, including its origin as a DVD rental service in 1997, its expansion into streaming in 2007, its global reach (available in over 190 countries, including India), its profitability ($2.4 billion profit), its large subscriber base (283 million+ paid memberships), and its use of data to understand customer behavior and patterns.
    • Dataset: The project involves analyzing a dataset of around 9,000-10,000 movies with information such as release date, title, overview, popularity, vote count, average vote, genre, and poster URL.
    • Analysis Questions: Five specific questions need to be answered using the dataset:
    1. Which are the genres that most people have liked (based on the number of votes)?
    2. Which is the movie that is most popular, and what is its genre?
    3. Which is the movie that has the least popular rating, and what is its genre?
    4. Which is that movie in which year were the most films made (most released)?
    5. (Implied but not explicitly numbered) Analyze the vote average column to categorize movies based on their average rating (e.g., popular, average, below average, not popular).
    • Tool: Python and Jupyter Notebook are to be used for solving the questions.
    • Data Loading: The excerpt shows importing necessary libraries (NumPy as np, Pandas as pd, Matplotlib.pyplot as plt, Seaborn as sns) and loading the movie data from a CSV file named “tmdb_5000_movies.csv” into a Pandas DataFrame named df. The lineterminator=’\n’ argument is used in pd.read_csv().

    Source 5: Excerpts from “01.pdf” (Continued – Netflix Movie Data Analysis Project – Data Exploration and Cleaning)

    Main Theme: This section details the initial steps of exploring and cleaning the Netflix movie dataset.

    Important Ideas and Facts:

    • Initial Data Exploration: Displaying the first five rows of the DataFrame using df.head() to understand the data structure (budget, genres, homepage, id, keywords, original_language, original_title, overview, popularity, production_companies, production_countries, release_date, revenue, runtime, spoken_languages, status, tagline, title, vote_average, vote_count).
    • Handling Date Format: Acknowledging the need to bring the ‘release_date’ column into datetime format, though the implementation is shown later.
    • Exploring Genres: Examining the ‘genres’ column, noting that multiple genres are listed within a single string, separated by commas and spaces (e.g., “Action, Adventure, Science Fiction”). This highlights the need for splitting and processing this column for effective analysis.
    • Checking for Duplicates: Using df.duplicated().sum() to check for duplicate rows in the dataset. The result of 0 indicates that there are no duplicate movies.
    • Basic Statistics: Using df.describe() to get descriptive statistics for numerical columns (popularity, vote_count, vote_average, budget, revenue, runtime). This provides insights into the distribution and range of these variables (e.g., average popularity, maximum votes).
    • Irrelevant Column Removal: Identifying columns that are deemed unnecessary for answering the posed questions: ‘budget’, ‘homepage’, ‘id’, ‘keywords’, ‘original_language’, ‘poster_path’, ‘production_companies’, ‘production_countries’, ‘release_date’, ‘revenue’, ‘runtime’, ‘spoken_languages’, ‘status’, ‘tagline’. These are stored in a list columns_to_remove.
    • Dropping Columns: Using df.drop() with axis=1 and inplace=True to permanently remove the identified irrelevant columns from the DataFrame.
    • Categorizing Vote Average: Creating a user-defined function categorize_vote_average to label movies based on their ‘vote_average’ into categories: ‘popular’, ‘average’, ‘below average’, and ‘not popular’. This involves calculating percentile-based thresholds (minimum, 25th percentile, 50th percentile, 75th percentile, maximum) and using pd.cut() to assign labels. The function is applied to the ‘vote_average’ column to create a new column ‘vote_category’.
    • Splitting Genres: Processing the ‘genres’ column to split the comma-separated genre strings into individual genres. This involves:
    • Using df[‘genres’].str.split(‘, ‘) to split the strings into lists of genres.
    • Using df.explode(‘genres’) to transform each list of genres into separate rows, effectively creating a long format DataFrame where each movie-genre combination has its own row.
    • Resetting the index of the resulting DataFrame df_exploded.
    • Casting the ‘genres’ column to the ‘category’ data type.
    • Unique Genre Count: Using df_exploded[‘genres’].nunique() to find the number of unique genres (resulting in 19).

    Source 6: Excerpts from “01.pdf” (Continued – Netflix Movie Data Analysis Project – Data Visualization and Analysis with Plotly)

    Main Theme: This section introduces the Plotly library for creating interactive visualizations to answer the remaining questions about the Netflix movie dataset.

    Important Ideas and Facts:

    • Introduction to Plotly: Explaining the benefits of using Plotly for dynamic and interactive visualizations, noting that it’s a valuable skill in the industry. It mentions the use of plotly.express for high-level plotting and plotly.graph_objects for more customized graphs.
    • Importing Plotly Libraries: Importing the necessary Plotly modules:
    • plotly.express as px
    • plotly.graph_objects as go
    • plotly.figure_factory as ff (commented out but mentioned)
    • plotly.subplots (implicitly used later)
    • plotly.colors
    • plotly.io as pio
    • Setting Default Theme: Setting the default Plotly template to ‘plotly_white’ using pio.templates.default = “plotly_white”.
    • Re-loading Data (Optional): Showing the re-loading of the “tmdb_5000_movies.csv” dataset into a DataFrame named data (though the previous df_exploded DataFrame with processed genres is the one primarily used for analysis).
    • Analyzing Most Frequent Genre:Using df_exploded[‘genres’].value_counts() to get the count of movies for each genre.
    • Using px.bar() to create a bar chart showing the frequency of each genre. The ‘genres’ column is on the y-axis, and the count is on the x-axis. The chart is displayed using fig.show(). The insight is that ‘Drama’ is the most frequent genre, followed by ‘Action’.
    • Analyzing Vote Average Category:Using px.bar() to create a bar chart showing the distribution of movies across the created ‘vote_category’ (popular, average, etc.). The ‘vote_category’ is on the x-axis, and the count is on the y-axis. The chart is displayed using fig.show(). The insight is that most movies fall into the ‘average’ vote category.
    • Identifying Most Popular Movie:Finding the movie with the maximum ‘popularity’ using df[df[‘popularity’] == df[‘popularity’].max()]. The most popular movie is identified as ‘Minions’ with a popularity score of approximately 875.
    • Identifying Least Popular Movies:Finding the movie(s) with the minimum ‘popularity’ using df[df[‘popularity’] == df[‘popularity’].min()]. Several movies are identified with a popularity score of 0.
    • Analyzing Movie Releases by Year:Extracting the year from the ‘release_date’ column using pd.to_datetime(df[‘release_date’]).dt.year.
    • Counting the number of movies released each year using .value_counts().
    • Using px.bar() to create a bar chart showing the number of movie releases per year. The year is on the x-axis, and the count is on the y-axis. The chart is displayed using fig.show(). The insight is that the number of movie releases generally increased over time, with a peak around 2014-2016.

    This briefing document summarizes the key aspects of the provided sources, highlighting the learning objectives, methodologies, and insights gained from the Python-based data analysis projects on Zomato, Uber, and Netflix movie data. The use of quotes and explicit mention of important code snippets and conclusions from the analysis are included to provide a comprehensive overview.

    Python, Data Analysis, and Interview Preparation

    Frequently Asked Questions based on the Provided Sources

    1. What is the primary goal of the Python Complete Crash Course in Hindi, and who is it designed for? The primary goal of the Python Complete Crash Course is to equip individuals with practical Python skills, particularly for data analysis and data science roles. It emphasizes learning through hands-on projects to build confidence and demonstrate practical abilities in interviews. The course is designed for beginners with no prior HiFi coding experience, starting from basic concepts and gradually advancing to real-time projects. It aims to help learners understand how data analysts and data scientists work within a company.

    2. Why does the Python course emphasize project-based learning, especially for interview preparation? The course focuses on project-based learning because recruiters in interviews often concentrate on candidates’ projects rather than asking exhaustive theoretical Python questions due to time constraints. Projects effectively showcase a candidate’s practical skills, the amount of work they’ve done, and their ability to apply Python in real-world scenarios. Completing projects demonstrates a tangible understanding of Python and boosts confidence for interviews.

    3. What are the key Python libraries highlighted in the Zomato Data Analysis Project, and what are their primary uses in this context? The key Python libraries highlighted are: * Pandas (as pd): Used for data manipulation and cleaning. It provides data structures like DataFrames, which are essential for reading, processing, and analyzing structured data like the Zomato CSV file. * NumPy (as np): A library for numerical computations and mathematical operations, often used in conjunction with Pandas for data analysis tasks. * Matplotlib.pyplot (as plt): A fundamental library for creating static, interactive, and animated visualizations in Python, such as plots and graphs to understand data patterns. * Seaborn (as sns): A data visualization library built on top of Matplotlib, providing a higher-level interface for creating informative and attractive statistical graphics.

    These libraries are used to import and read the Zomato dataset, clean and manipulate the data (using Pandas and NumPy), and then visualize various aspects of the data to extract insights (using Matplotlib and Seaborn).

    4. What were some of the key data cleaning and preprocessing steps performed in the Zomato Data Analysis Project? Several data cleaning and preprocessing steps were performed, including: * Importing necessary libraries: Pandas, NumPy, Matplotlib, and Seaborn were imported to handle data manipulation and visualization. * Reading the CSV file into a Pandas DataFrame: The Zomato dataset was loaded for analysis. * Handling rating data: A user-defined function was created to extract the numerical rating from the “Rate” column, which initially contained additional text. The .apply() method was used to apply this function to the entire column. * Handling missing values: The project identified and handled missing values in the “Purpose” column by replacing them with “Not OK”. Later, it demonstrated dropping rows with any null values using dropna() to ensure cleaner data for analysis. * Converting date and time formats: The “Start Date” and “End Date” columns, initially in object format, were converted to datetime objects using pd.to_datetime(). Errors in the date format were handled by setting invalid dates to “Not a Time” (NaT). * Creating new date and time-related columns: New columns for “Date” and “Time” were extracted from the “Start Date” column. * Categorizing time into day periods: A new “Day Night” column was created to categorize rides into “Morning,” “Afternoon,” “Evening,” and “Night” based on the time. * Handling duplicates: Although no duplicates were found in the Netflix movie dataset, the process of checking for duplicates using .duplicated() and .sum() was demonstrated.

    5. What were some of the key insights derived from the Zomato Data Analysis Project through data visualization? Several insights were gained through data visualization: * Restaurant Type Preferences: Dining type restaurants receive the most orders. * Rating Distribution: The majority of restaurants receive ratings between 3.5 and 4. * Booking Category and Purpose: The “Business” category has the most Uber bookings, and the primary purpose for booking is for “Meetings.” * Booking Trends by Time of Day: Most Uber rides are booked during the “Afternoon.” * Booking Trends by Month: Uber bookings are lowest during the winter months of January, February, November, and December. * Booking Trends by Day of the Week: Friday is the day with the highest number of Uber bookings, while Sunday has the fewest. * Trip Distance Analysis: Most Uber trips are within the 0 to 20-mile range, with a noticeable peak in the 5 to 10-mile range.

    6. What are the different rounds typically involved in data science/analytics job interviews, as mentioned in the sources? The sources mention that data science/analytics job interviews generally involve three to four rounds: * Round Number One (Assignment/Case Study): The company provides an assignment with problems to be solved, often a case study, which needs to be submitted within 24 to 48 hours. * Round Number Two (Technical): If shortlisted in the first round, candidates appear for a technical interview. * Round Number Three (HR/Managerial): If successful in the technical round, the final round is usually with HR or a hiring manager.

    The Zomato project aims to help candidates prepare for the first round, where they might receive a case study or assignment requiring data analysis and insight extraction.

    7. How did the idea for Uber originate, and what were the initial concepts behind it? The idea for Uber originated in Paris in 2008 with Garrett Camp on a snowy evening when public transport was scarce. Frustrated by the lack of available transportation, he conceived the idea of a technology or app that would allow people to book rides and have a driver come to their location. Initially, the concept involved ride-sharing to divide costs among passengers traveling in the same direction. However, the idea evolved to its current form where users can book a ride directly to their specific location.

    8. What role does data science play in Uber’s operations, and what are some specific applications mentioned? Data science plays a crucial role in various aspects of Uber’s operations, including: * ETA (Estimated Time of Arrival) Estimation: Machine learning algorithms analyze various factors to predict how long it will take for a driver to arrive at a user’s location. * Price Prediction: Data analysis and modeling are used to predict the cost of a ride based on factors like distance, time, demand, and traffic. * Route Optimization: Algorithms determine the most efficient routes for drivers to take passengers to their destinations. * Driver-Rider Matching: Data science helps in making optimal matches between available drivers and ride requests. * Fraud Detection: Analyzing data helps in identifying and preventing fraudulent activities related to payments and bookings.

    The sources emphasize that data science is essential for Uber to function effectively, as it enables accurate estimations, predictions, and optimizations that enhance the user experience.

    Data Visualization for Insight: A Project Analysis

    Data visualization is a key aspect of the projects discussed in the sources, playing a crucial role in understanding data and extracting meaningful insights. The sources illustrate several ways data visualization is employed:

    Tools and Libraries for Visualization:

    • The primary libraries mentioned for creating visualizations are Matplotlib and Seaborn. Seaborn (sns) is frequently used for generating various types of plots.
    • Plotly is also mentioned as a library that can be used for visualization in the context of the e-commerce project.

    Types of Visualizations and Their Purposes:

    • Count Plots: These are used to display the frequency of different categories within a dataset. In the Zomato project, a count plot is used to determine which type of restaurant (e.g., dining, cafe, buffet) has the majority of customer orders. Similarly, in the u data analysis project, count plots are used to see which category (business or personal) and for what purpose (e.g., meeting) people book rides the most.
    • Line Graphs: Line graphs are used to show trends and relationships between two variables. In the Zomato project, a line graph visualizes the number of votes received by each type of restaurant, helping to identify which restaurant types are most liked by customers. A line plot is also used in the u data analysis project to analyze the monthly booking trends.
    • Histograms: Histograms are used to display the distribution of a single numerical variable. In the Zomato project, a histogram is used to understand the distribution of ratings given by customers to different restaurants.
    • Box Plots: Box plots are useful for comparing the distribution of a numerical variable across different categories. In the Zomato project, a box plot is used to compare the ratings given for online and offline food orders.
    • Heat Maps: Heat maps are used to visualize the relationship between two categorical variables using color intensity. In the Zomato project, a heat map shows the relationship between the type of restaurant and whether orders are placed online or offline, indicating preferences.
    • Bar Graphs: Bar graphs are used to compare the values of different categories. In the u data analysis project, bar graphs (count plots, which are a type of bar graph) are intended to visualize the most frequent categories and purposes of Uber bookings. Seaborn’s catplot (categorical plot) is used to show the distribution of movie genres, where the height of the bars represents the frequency of each genre.
    • Dist Plots (Distribution Plots): These plots are used to visualize the distribution of a single variable, often combining a histogram with a kernel density estimate. In the u data analysis project, a dist plot is used to analyze the distribution of ride distances (in miles) booked by users.

    Purpose of Data Visualization:

    • Extracting Insights: The primary goal of data visualization in these projects is to extract meaningful insights from the data. By presenting data visually, patterns, trends, and relationships become easier to identify and understand.
    • Answering Questions: Visualizations are created to help answer specific questions related to the data. For example, in the Zomato project, visualizations are used to answer questions like which restaurants are ordered from the most, which restaurant types receive the most votes, and which restaurants have the highest ratings. Similarly, in the u data analysis project, visualizations help answer questions about booking categories, purposes, times, and distances.
    • Data Exploration and Analysis (EDA): Data visualization is a key component of Exploratory Data Analysis (EDA), as mentioned in the movie data project. It helps in understanding the characteristics of the data, identifying potential issues, and forming hypotheses.
    • Communication of Findings: Visualizations are a powerful way to communicate findings to others, such as recruiters or stakeholders in a company. Graphs and charts can convey complex information more effectively than tables of raw data.
    • Supporting Decision Making: The insights gained from data visualization can support better decision-making within a business context. For example, understanding which restaurant types are most popular can inform business strategies for Zomato. Similarly, understanding peak booking times for Uber can help with resource allocation.

    In summary, the sources highlight the significant role of data visualization in analyzing datasets, extracting actionable insights, answering specific business questions, and effectively communicating findings using libraries like Matplotlib and Seaborn, with Plotly also being a potential tool. Different types of visualizations are chosen based on the nature of the data and the specific questions being addressed.

    Zomato Data Analysis: Visualizing Restaurant Insights

    The Zomato data is used in a Python Complete Crash Course to illustrate how a data analyst or data scientist can work within a company. The objective of using this data is to answer specific business-related questions through data analysis, including performing visualization and extracting insights.

    According to the source, the Zomato data includes information such as:

    • Restaurant names.
    • Whether an order was placed online (yes/no).
    • Whether a table was booked (yes/no).
    • Ratings given by customers.
    • The number of votes received by a restaurant.
    • The approximate cost for two people.
    • The listed type of restaurant, such as dining, buffet, or cafe.

    The project involves several steps with the Zomato data, including data cleaning. One crucial cleaning step is converting the data type of the rating column to extract only the numerical rating and remove any extraneous text like “bye f” or “f s l f”. The source also mentions checking for missing values, and in this specific dataset, no missing values were found.

    The core of working with the Zomato data, as demonstrated in the source, is to extract insights by answering specific questions using data visualization. The source provides examples of the following visualizations created using the Zomato data and the insights derived from them:

    • A count plot is used to determine which type of restaurant receives the majority of orders, revealing that dining-type restaurants have the most orders.
    • A line graph visualizes the number of votes received by each type of restaurant, showing that dining restaurants received the most votes.
    • A histogram displays the distribution of customer ratings, indicating that the majority of restaurants receive ratings between 3.5 and 4.
    • A count plot examines the approximate cost for two people, suggesting that most couples spend around ₹00 on an average order.
    • A box plot compares ratings for online and offline orders, concluding that offline orders receive lower ratings compared to online orders.
    • A heat map visualizes the relationship between the type of restaurant and online/offline orders, indicating that dining restaurants mostly receive offline orders, while cafes see more online orders.

    These examples directly align with our previous discussion on data visualization, illustrating how different plot types are used to explore the data and answer specific questions. The Zomato project emphasizes the importance of visualization in understanding customer behavior and restaurant preferences. The insights gained from these visualizations can then be used by a company like Zomato to inform business strategies, such as offering more coupons for offline dining or focusing on improving offline dining experiences based on lower ratings.

    Zomato Data: Analysis of Restaurant Types and Preferences

    The sources discuss restaurant types primarily within the context of the Zomato data analysis project. The data includes a column specifying the listed type of restaurant, which includes categories such as dining, buffet, and cafe. The source also mentions an “other” category.

    Here’s a breakdown of the discussion around these restaurant types:

    • Categories Identified: The main restaurant types identified in the Zomato data are:
    • Dining
    • Buffet
    • Cafe
    • Other
    • Analysis through Visualization: The project uses various data visualization techniques to analyze customer behavior and preferences related to these different restaurant types:
    • A count plot reveals that dining-type restaurants receive the majority of customer orders. This indicates that, in terms of order frequency, dining establishments are the most popular among customers in the dataset.
    • A line graph showing the number of votes received by each restaurant type indicates that dining restaurants have received the most votes. This suggests that dining restaurants not only have more orders but also receive more feedback from customers, potentially signifying higher engagement or a larger customer base.
    • A heat map visualizes the relationship between the type of restaurant and whether orders are placed online or offline. This visualization shows that dining restaurants mostly receive offline orders, suggesting customers prefer to dine in at these establishments. Conversely, cafes see more online orders, implying a preference for takeaway or delivery from cafes. The behavior for buffet and “other” categories is also visualized in this manner.
    • Business Implications: The analysis of restaurant types allows for the extraction of actionable business insights:
    • Knowing that dining restaurants are the most popular in terms of orders and votes, Zomato can understand its strongest segment.
    • The finding that offline orders are more common for dining restaurants could inform strategies related to in-house dining experiences and potential partnerships with these establishments.
    • The observation that cafes have more online orders can guide strategies for optimizing online ordering and delivery services for this type of restaurant.
    • The lower ratings for offline orders compared to online orders suggest that Zomato might need to investigate and potentially work with restaurants to improve the offline dining experience.

    In summary, the analysis of restaurant types within the Zomato data, facilitated by data visualization, allows for a deeper understanding of customer preferences regarding different dining experiences and ordering methods. This information can be crucial for Zomato to make informed business decisions and tailor its services to meet customer demands effectively. This aligns with our earlier discussion about the role of data visualization in extracting meaningful insights from the Zomato data [previous turn].

    Zomato Data: Customer Rating Analysis

    Customer ratings are a significant aspect discussed in the sources, particularly within the context of the Zomato data analysis project. The Zomato data includes a column for ratings given by customers [previous turn]. The analysis of these ratings, often through data visualization, provides valuable insights into customer satisfaction and preferences.

    Here’s a breakdown of how customer ratings are discussed and analyzed:

    • Data Cleaning of Ratings: The Zomato project involves a crucial data cleaning step for the rating column. Initially, the ratings in the dataset contain extraneous text, such as “bye f” or “f s l f” appended to the numerical rating (e.g., “4.1/5”) . To use the ratings for analysis, a user-defined function is created to extract only the numerical part of the rating by splitting the string and taking the first value. This cleaned rating is then converted to a floating-point number .
    • Visualization of Rating Distribution: A histogram is used to visualize the distribution of customer ratings for restaurants in the Zomato data . This visualization helps to understand the range of ratings and the frequency of each rating. The analysis of the histogram reveals that the majority of restaurants receive ratings between 3.5 and 4 . This indicates a general level of customer satisfaction, with most ratings falling within a positive range.
    • Comparison of Online and Offline Ratings: A box plot is employed to compare the ratings given for online and offline food orders . The analysis of this box plot indicates that offline orders tend to receive lower ratings compared to online orders . This suggests potential areas for improvement in the offline dining experience or differences in customer expectations between online and offline orders.
    • Importance of Ratings for Businesses: The source emphasizes that rating is very important for businesses as it reflects how much customers like their product . Understanding customer ratings helps companies gauge satisfaction levels and identify areas where they might need to improve their offerings.
    • Categorization of Vote Averages (Movie Data – Related Concept): While not directly about Zomato ratings, the movie data project discusses a related concept of categorizing vote averages into labels like “popular,” “average,” “below average,” and “not popular” based on defined criteria . This demonstrates another way in which numerical ratings or scores can be analyzed and transformed into more easily understandable categories of customer sentiment.

    In summary, the analysis of customer ratings in the Zomato project is a key component of understanding customer feedback and preferences. Through data cleaning and visualization techniques like histograms and box plots, insights are derived about the distribution of ratings and the differences in ratings between online and offline orders. These insights are crucial for businesses to assess customer satisfaction and identify areas for potential improvement. The importance of ratings is highlighted, and a related concept of categorizing ratings is seen in the movie data project.

    Data Analysis Through Zomato and Movie Projects

    Data analysis is a crucial process discussed extensively in the provided sources, particularly through the practical examples of the Zomato data analysis project and the movie data project. It involves a series of steps aimed at extracting meaningful insights and answering specific questions from data.

    Here’s a breakdown of the key aspects of data analysis as illustrated in the sources:

    • Defining the Objective: Both projects begin with a clear objective. The Zomato project aims to understand customer behavior and restaurant preferences by answering specific questions related to order frequency, ratings, and costs. Similarly, the movie data project seeks to answer questions about movie genres, popularity, and release years. Having a clear objective guides the entire analysis process.
    • Data Collection and Understanding: The first step in data analysis is having access to the relevant data. The sources describe using a Zomato dataset containing information about restaurants, orders, ratings, etc. and a movie dataset with details like movie titles, genres, popularity, and release dates. Understanding the structure and content of the data is fundamental before proceeding with any analysis.
    • Data Cleaning and Pre-processing: This is a critical step highlighted in both projects.
    • In the Zomato project, data cleaning involves extracting the numerical rating from a string format and converting it to a usable data type [previous turn, 4]. The source also mentions checking for and finding no missing values in the initial Zomato dataset.
    • The movie data project emphasizes several pre-processing tasks, including handling missing values, changing the format of date columns, and removing potential white spaces in categorical columns. It also involves removing irrelevant columns that do not contribute to answering the research questions. Additionally, the movie project demonstrates creating labels from numerical data (vote average) to categorize movies.
    • Data cleaning and pre-processing ensure the data is accurate, consistent, and in a suitable format for analysis.
    • Exploratory Data Analysis (EDA): The movie data project explicitly mentions performing Exploratory Data Analysis (EDA). EDA involves initial investigations to summarize the main characteristics of the dataset, often using visual methods. The movie project demonstrates this by checking for duplicate values and calculating basic statistics (like mean, min, max) for numerical columns to understand their distribution.
    • Data Visualization: Both sources heavily emphasize the role of data visualization in data analysis.
    • The Zomato project uses various plot types like count plots, line graphs, histograms, box plots, and heat maps to explore relationships and distributions in the data and answer specific business questions [previous turn, 2, 5, 6, 7, 8, 9, 10, 11]. Visualizations help in understanding patterns and trends that might not be apparent from raw data alone.
    • While not explicitly detailed, the movie project also leads to answering questions, implying the use of visualization to derive those answers (e.g., identifying the most frequent genre or the year with the most movie releases would likely involve some form of aggregation and visual representation).
    • Insight Extraction and Interpretation: The ultimate goal of data analysis is to extract meaningful insights from the analyzed data. In the Zomato project, visualizations lead to conclusions such as dining restaurants being the most popular, offline orders receiving lower ratings, and most couples spending around ₹00 on average orders. These insights can then be used to inform business decisions.
    • Answering Specific Questions: Both projects are structured around answering a set of predefined questions. This highlights that data analysis is often driven by specific inquiries that need to be addressed using the available data.
    • Business Relevance: The Zomato project is explicitly framed within a business context, demonstrating how a data analyst or data scientist can work with real-world data to solve business problems. The insights gained are directly relevant to Zomato’s operations and strategies [previous turn]. The e-commerce sales analysis project excerpt further reinforces the business importance of data analysis, highlighting its role in understanding sales, profit, and customer behavior for e-commerce companies.

    In summary, data analysis, as demonstrated in the sources, is a systematic process involving defining goals, collecting and cleaning data, exploring its characteristics, visualizing patterns, extracting insights, and ultimately answering questions to support informed decision-making, particularly within a business context. The emphasis on data cleaning, visualization, and the derivation of actionable insights are recurring themes throughout the examples provided.

    Python Complete Crash Course in Hindi | 5 Python Projects

    The Original Text

    hello everyone i am swati and welcome to the All the people can benefit from this through interviews and This has happened for a job as well but with this python’s The objective of creating the course is that whenever you go for an interview Are recruiters interviewing you for half an hour? I can’t ask the whole Python question. That is why they ask the most questions on your projects and your It is the projects that tell this story that how much work you do that’s why this You can get Complete Python Crash Course There are five real timers inside it Projects include such as JumT There will be a lot of focus on implementation, whatever We are talking about all these projects When we start a project, Beginners have these questions in their mind It happens that we are very good at coding If I don’t know, can we do these projects? If you find it then definitely you have nothing to worry about You don’t need this crash course No HiFi coding required Starting from the very basic level, here we will talk about things We will start with this and gradually advance The whole project will reach this level Every single line of code that is written in I have told you one line at a very basic level. explained in very simple language so that Complete all your five projects easily If you get it then these projects which are going to happen It will be very beneficial for you because when You can do these five projects yourself. If you complete it then you will see it in real time The idea would be that a data analyst is a How can a data scientist work inside a company? if we work with it then this whole Projects are its Notes Data Set Code file all things description box below It is already mentioned and this course Your confidence level after completing it It will definitely get a boost, so let’s get started This is Python Complete Crash Course That’s alright so let’s start today’s class In today’s class when you complete your project If you complete it then your confidence level will increase This will definitely increase your confidence in yourself You will believe that yes now I have the science data The things that are analyzed should be understood today’s class is starting so we before you start zomato.in means Who supplies their food through Zomato If yes then that is from 2 lakhs onwards 26000 which is the restaurant near jomat If we have partners then we can understand that The data which is in crores is also of the customers Available for more than Rs 2 lakh and Rs 2.5 lakh Around Nearby About We can say Delivery Partners who are restaurant data If it is available then now it is available for you here The situation is that you want to enter any data in Zomato. Are you working on a driven role? The record of the data of the customers is yours Available now and as a data professional You need to extract some insights from this data Some EDF needs to perform visualization and there are some questions which are If you have to answer the questions then first First, I would like to show you that our The project is today’s class, in what way So in today’s class it will be visible in this way We will be coding each and every thing To rectify it in some way The graph you see on my screen With the help of these graphs we are will perform visualization whether it is a bar Whether it is a graph, be it a line graph or then in this way we have How to plot a histogram Insights have to be extracted in this way all the visualizations box plots all of these Visualizations you see Heat Map How to make this with data from Zomato We will learn all this in today’s class you must be coming to the point that here But we have some questions about this to help us solve the project First of all we have given Is there a restaurant or a buffet or dining? All this data is a type of restaurant we have here it’s been many times now This happens even in direct interviews Some such questions for you to solve are given specifically for your round number Like in forest mainly you ask such questions Now you will get to see it in the interview Solve these questions from this data The first question is what type of question you have to answer Restaurants do the majority of customers Which are the restaurants to order from? Which is the type of restaurant that Majority of customers order food how many votes has each type of restaurant Received from the customers now we are Talking about restaurants, there are different types Are there any restaurants, some have buffets Is there a cafe like full-fledged dining? If it is a restaurant, how many votes did you give? the customer has because look whenever someone The company comes to you, we come from Zomato we take the food and then it comes to us How many votes were given for a feedback option? How many people have told us well here We have to find out from this data what are the Ratings Now whenever we order food if yes then we give rating to Zomato that this How we liked the restaurant food If you don’t like the food out of five then give me three We gave a rating of 4.5 if we liked the food So, which are the restaurants that High ratings from the majority of customers It has been provided, you have to extract the data I have asked a question from Jomat observed that there are many couples who They order their food online How much amount do you spend on one meal So what size is it when ordering? Suppose the customers are from 00 to If you order food worth up to ₹5000000 It is possible that some people order online from Sometimes people take it offline also Which like mode has the highest ratings People give food or order it online is there in it or people go to eat If you eat in a restaurant, then this is there in it You also have to do an analysis of the six The question is which type The restaurant that has the most people Order food offline so that Jomat can make money like this Give some more good offers/coupons to the customers So that these people can get food wherever they want offline order online from where you order also start and the revenue of zomat is If he is able to grow then I have answered all these questions and the data set is given in front of you I will also tell you the data set here Let me show you this data set that we have is available But our Jupyter notebook is here We opened our Jupyter Notebook and Here I have created a new folder which is this I have created the folder, now The first thing we will do in the folder is how to upload data set then how to get the data Let’s see how to upload the set If yes, then I went here and clicked the upload button What you are seeing here is the upload We clicked on this Zomato data here I selected it and opened it and I I am uploading this data here we have this in csp format The data has been uploaded to us by Deposit Right now next we will work here I am a new folder which is new for us the file is ip by file so I am here Python 3 Our file, I will give it here I will open it and in this there is untitled now If this is a file, we would name it is zomato We named it as Zomato Project Now the first thing that we do is to we have uploaded it here and one of our Pass ip y n b cutter notebook Now first of all we have opened the notebook We give the heading here that today We are going to make that is the Zomato Data Analysis Project So I’m Writing Here is Zomato Data Analysis Project We would have written it in the proper heading format so i am writing this in a format of A heading Jupyter Notebook How do you install You have to do it, the link to the video is also below It is given in the description box How will you do the installation with that also You can come and check out the first one now work that we have to do here step number One of our main goals to make this project is to We will need some libraries nearby So first of all we will add those libraries here If you import then step one will happen We use the libraries which are imported These are going to be done in different ways We will use the libraries here the most The first one that we will have is a library here. That is Pandas now given this data set If we need to clean some data then If we want to do manipulation then for this we can use pandas Using the library NamPie Python is a library that provides numerical If it is useful for mathematical operations then We’re using measurement here Matt Plot Lip and Sea Bon Jo Visualizations I I was showing you so many graphs Here is the matte plot lip and seaborn If you make it with the help of the library, then every I learned what a library does. Now we have explained to you that every library what will we do here, we will import it so that All these files are in our Jupyter Notebook what should happen to all the libraries Once activated, import Panda SPD Now look Panda S PD that’s why I wrote Because panda panda is such a big word, we every If you cannot write the place then give an abbreviation for it A short form has been created PD i.e. Panda, we are with everyone’s abbreviation People will import the library just like that import measurement do not write measurement again and again so we write it as NP After this, we will import it here. matte plot lip so i’m writing here Import Matte Plot Lip pie plot s flipped by this name and then We will import C bonds I am writing here important business what have we done with all these libraries Imported successfully now libraries So now we have reached step number two. It will happen that we have the right to this data We have the data of Zomato CSP what are we supposed to do with this data If you want to bring it in this notebook then data frame Read what to do on this We will have to create data frame so I will write the steps here Number two we will create the data frame now The data of that Zomat should be transferred to this In this file in Jupyter Notebook, we have We will bring you here for this, most of the people here First, what I’m doing is creating a data frame I’m creating a variable named this Inside PD, PD means Panda library PD dot head I will do it here pdi d Reed I’ll pay here because now we have to What should we do with this data that we have? It is available, if I want to read this data then I I will write on this page read under CSV why i wrote csv because the file The format is in CSV format now what is the name of this data that we have So the name of this data is Zomato we have a file with this name data.csv If it was close we would have run it here so it is showing certain error here The error is because jomat is after that space is dot csv so here we People should slightly modify the name of the file as it This is the name of the file, write it here let’s run it now alright so Now this error is gone because the file name Now it is okay, now we will do it here We print it and enter the data here Now if I print the frame then In this way the data comes to us now what should I do as I am Here, if I call data frame then this The data has reached us properly So what we’ve done so far are two things. I have learned to do this project I have imported the libraries I need and This data was available to us. We have our own Jupyter Notebook here We have already read this data Now that we have the data, we can Now we will start working with the data First, look at the data carefully The name given means that the restaurant This is the name given to all these restaurants There are 147 of your restaurants in this data which It is included now if you order online If you have ordered online then yes or no means If someone has ordered offline then no How many people have booked the table here? I have booked a table and eaten here Its record is given how many ratings it has given After eating food from your choice menu, this You have ratings, how many votes you have cast This is available and enough to feed two people It costs, here’s how much it costs and the listed type of restaurant What type of dining does the buffet serve? What does the cafe serve and what type is it? We have this data available here Right now look, first of all we have to understand two things We learned how to create this data frame and we have libraries here I have imported it, till here everything is clear If yes, then let me know in the comments If you have any doubt then ask in the comments Now what will be our next task here? The next task is to use this data What we have to do is first see the questions Let us take a look at what needs to be solved first The first question to be solved is which This is such a restaurant which attracts maximum people what type of food do you order from the restaurant so now what are we here for We will do this thing to solve this question We will solve it but before that we will give you some data Professional, it is your job to ensure that this data Is there any missing value in Is this data clean? Is this data accurate? There are no such outsiders in these to find out everything so that we can use this data Let us be sure that yes, what we have We have provided the best data we took everything out and then If we can solve the questions then we will The steps that we are doing are data Cleaning Now the first thing that we need to do here You should see the ratings, see in which order It is 4.1 or 5 so now what is this, look at it in everyone bye f bye f bye is written so This is something that is there for us somewhere The hurdle will create so what do we want These are the ones who sleep under everyone like boyfriend it can be seen written f s l f We want to remove the rating as it is If someone gave 4.1 then it is just 4.1 and this is It is written in the denominator that this thing should be removed So now what is this step for us here? If you want to perform then now whatever step you have to take We are going to perform in this what do we need to do is to convert Convert the Data type of which column rating column so Column Name hey off column rate okay what do we need it for in this we want that only and Only the ratings given by the customers If 4.5 is given then 4.5 should come out like here 4.1 So we want 4.1 to come or slf If what is written is removed then this thing will be considered the most important First we clean the data, now its what do I need to do for this then I I am writing the code here, please pay some attention to it see i wrote here deaf most First we are creating a user defined function whenever we call a user defined function let’s create user def function means this There is a function like this, look here I am If I am writing print again and again then this print is already built in by python it’s inbuilt inside us we’re just picking it up and using it again and again so If I need to print something again and again then you don’t need to write any function yourself But user defined function means this it happens that we have the advantage that a This time I wrote a code for the user Now whenever I get this inside the defined function I would like to use it if I can Here, we are calling a user defined function will create this user defined function We will give it a name, let’s suppose it handles rate handle rate rate mean rating and its Inside here, we pass in the arguments Let me explain the value to you a little bit now please be patient this code is one by one line of code Don’t be worried if I explain it to you now The string we have inside value The value is I will write the whole code first After that, every line I will tell you Don’t be afraid at all as I keep explaining. I will explain one thing to you from the basic level itself. After this, we will write the value here. is equals to The value of zero and then we are going to Right Here Return float value return float The value is up to here, we can run it and see yes alright no errors yet now In this I will write one more line data frame The name of the data we have is data frame Inside that there is a rate column, inside this we People will put equals to two minutes, just be patient Take it, I am explaining what I am writing I want to write the code first Then I will explain to you that every What is the meaning of the line, we are inside it will apply dot apply handle handle rate Ok, now by running the code we have written Let’s see, now I am explaining this line by line. look at what i wrote now if you Here you will see directly what we need was that the direct floating point number which is If it is 4.1 then directly 4.1 will come, which is slf if now he goes away before everybody else we will see how it was coming earlier It was 4.1 5/5 Look, this thing was there in everyone now The code for this has been removed from here I have written about it, now I will tell you every single Let me explain the meaning of the line first Created a user defined function here We named it R and kept handle rate inside it I passed a value now here it is The value you are seeing is equals to string now here string is we know that this data Given the type we have the string type If there is data then we have written the string here value dot split means split function what does that do in the string like if you Anything you want like here it was written 4.15 So if you want to cut from slf We don’t need this thing from here If you want to split it then here it is The split function helps in 4.1 You are separated, now what do we need If we only want 4.1 then what value did we write is equals to value row means row position Your 4.1 was available and you can return it gave this value now what did this thing do We have collected the entire rate which we have. We had this column named this rate We need to implement this in the column of So what did we do here that this thing called rate We have columns inside this data frame what is the name of the columns we have in data frame Inside this we have the column name there is a rate so I have written the rate here inside it What did I do by applying this handle applied the rate function so that We should not do this thing again and again read and if it gets easy then this function we wrote and then look after writing The things we wanted were direct from here The values ​​we get here Now what else do we want Now we need to check that the data set is Is there any missing value in this Is there any value null that every whether the value is proper or not is something we People will now check their data cleaning What should I do for this inside the step I will write a summary of this thing, if you check the full If you want to do it then we write it for this frame d if now understand this info from function name I am coming that he will give the information so now we People can see here what we have Total columns are six columns here six seven Columns are available Name object inside it Means the name is obviously a string Type of data is online order yes no This is also string type data booked is it a table yes no then this is also a string This is the type of data, now this rating was 4.1 3.5 It comes to us floating point The data shows how many people voted This is a type of food for two people What is the approximate cost? This is also an integer and the listed type is Hotel Dining what type of cafe is it so this Object type is done, now here you If you look here it is written no null This means that all the values ​​in this data set is available no value is missing Here, this thing has become clear to us now Now we are sure about this thing So let’s give the data we have collected to ourselves The correct data is available now, the first question whatever it is this is our question that we What should we do about that restaurant? I want to find out which majority customer If you order food then now we do it for you People write this as the first thing here These are the types of restaurants that we know If you want to take it out here then I will write it here I will give you the type of restaurant first We are going to solve the question Now how do I get this thing to show me So I want to show it to you here After making the graphs, first of all I am here I write data frame dot head and call it first So if we take this, we have all the columns that they all have arrived when we put our heads down In this data we have total 148 values but when we apply the head then it The initial five data are given to us It shows here, now here we have the graph To create the graph I want to create I am using the C bun library and I am we wrote bun as import c bun s n s so I wrote here s ns dot here But we want to make a count plot here By counting a restaurant here make equal to I will write here I’ll write the code once for the data frame then well you have this thing I will continue to explain and what we have What type of restaurant is column buffet type cafe type dining type so this There are values, we talk about these values ​​here Let’s write it down first, now make this We want the count plot to be overturned dot x label and write it here Type of restaurant run by See, we only have two lines of code I wrote it and this graph came to us. I am trying to explain the meaning of this graph to you. first of all let’s understand the graph a little Then let’s understand its code, first look at it Comes to you here at X Access It is a buffet type restaurant or a cafe type restaurant if it is other or dining type then we From which type of restaurant did you have to take it out? Most of the orders are for food so now we Here you can easily remove it that Dining type restaurant is the best among these More food is ordered which is more than 100 If there is any plus then we can easily reach this conclusion Here you can find out who the majority customer is order food from a restaurant from From a dining type restaurant then The cafe restaurant is at number two The reason people order food is other and then de buffet okay so this data We had to bring the analysis out here Now we have extracted its code a little bit Understand this, now look at a plot where my By counting the numbers he is telling that the There are more than 100 people having dinner so what is this going on here count and he is telling me by doing this, when we A plot has to be made where we can see the exact If you are writing the value by counting then here We plot the count of the curve on x What did I need in Access? Which type of restaurant is listed in type to Look here we have this list end type The column was titled Buffet This is what we have done, we have put the name of the column as In access we needed the label below that What should we name it? We will have to name it type of restaurant so here x What did I do to assign labels in Access Written here type of restaurant whenever you If we used to make graphs earlier, we would have done something like this used to write na this is x axis so there We used to write the type of restaurant Exactly as we have written here And this is the graph that has come to us So the first question that was given was What was the conclusion? This is its conclusion turned out that Majority of the Restaurant Falls in Dining Category Maximum number of meals in dining category People are afraid to eat here in restaurants If we get to know about this thing here then We have solved our part number one question I have solved it, now it’s a matter of how many votes which is every type of restaurant when people If you eat food then obviously you vote Which type of restaurant do you go to the most? Every type has the most votes here Now the restaurant of has got this thing what do we need to do here for this For this we will have to write the code here Let us write its code here and use it let’s analyze so the next insight which What we have to figure out is that every How much do you like the type of restaurant? Vote for each type of restaurant It has been received so I am writing here data frame dat head is alright so what to do now No, we have to make a graph like this In which there are two things, visualization which We have to perform, we have to do something like this that the x axis remains with us what type of restaurant it is and If we want to take out votes in that access then For this, we have a line graph here. We will make such a line graph that Which restaurant got how many votes There are lines from this type of visualization If we want to show it through a graph then now here Two columns mean a lot to us The first list and type in which the restaurant type and another column in which each How many votes has the restaurant received then you Here you can see 775 787 in this way Show us the votes here Now here is the graph which we have for this key I want to make it for this I have posted the code here I am writing the code one line at a time to explain it do not hesitate at all, If you understand, let me know in the comments You can go but what did we do here? Grouped I created a variable called data. See Now first I will explain the graph then this code So look what we need How many types of restaurants do we have? Four restaurants that order buffet There are some cafes, some dining and some Everyone has other types of restaurants Look what you will see in the va axis In Y Access you will see every number of votes How many votes a restaurant has received then we You can see here that it is of buffet type He has got more than 2500 votes now. The dining here is the most It has received more than 20,000 votes Votes have been received for dining type The restaurant and other restaurants Its approximate cost is around 10000 It got votes so we had to make it that we wanted votes to come in this axis and type of restaurant here so that we can Can you guess which restaurant? type got the most votes so here Pay Dining Restaurants are the most Liked by most customers and most Most votes have been given to him now Every single code that we have written for this understand the line firstly I have read it here What is grouped in the name of grouped data? I have created a variable here called data Now in this data frame we have two columns: We meant that there was a column of liston type And the second column we had was for boards What we did was group by both the columns. that is, by doing both together, I put it and wrote the names of both the columns Listing Type and Votes are the two columns I wrote the name and made both of them equal now After this the result was equal to Dr. Data frame now these votes are inside it We passed this group data that we had created to it Now this is the plot we have made Look at this, we have made a line graph, so c This is the line graph of equal to green We wanted to keep the color green so I used It is written green here, now let’s suppose you If you want to keep the color red then you can make it red also You can keep it or it depends on your wish, see now If it turns red, then it depends on you It does whatever color you want to keep right now So we color it green That’s why I wrote this for color here now its marker what does marker mean Look, there is green at every point here You should see the green coloured dots every time you look at them. The place to see the dots here is green getting it right this are the dots isi it is called marker so we have to know the type of marker Which one did I want, I wanted dotted one, so I wrote here oh okay now x access what i wanted was an axis what did i want you to write type it look at the restaurant then write it in x access What is the type of restaurant and its color I have written red here so now If its color is red then you need to do something else I want to keep this color blue so you can right here blue Now if you look, you will see the blue color here below If the type of restaurant is different then this is for you The top is whatever color you want to keep The size is given, how big can you write now you want me to know its size and If you want to make it bigger then look at what I have written here I just made the size zero Let’s suppose I make it 30, then now Look here the size has become bigger If so, let’s suppose we have kept the size here it is 20 now if you look here it is size 20 I came in just like that and in access too We had to put a heading or we did it in access Put a heading here with the name of bots and kept the color red and size is 20 so in this way this This is the graph that has come to us The answer to the second question is I had to find out what was its conclusion The conclusion here is that the dining It is a restaurant, what do they do? We have got more votes so we are here Let us write it in conclusion Dining Restaurants If he has received maximum votes then this is for us If it is cleared then now the company knows this It’s okay brother, the dining restaurant It is our best source of income because people like it the most Now we have to do the most kind of work Cafes need to do better buffet Working outside restaurants that have If needed, get insights from data in this way Only when a company comes out with a strategy makes it clear what is going well and what Things are going bad now, the third question is The third insight we have is that we need to extract is from this question it is here that what are the ratings that majority of the Customers have received a rating of 2.5 If someone gives a 5 rating, what will that rating be? Which restaurant has the majority? We have now received this type of rating If you want to remove people then rating is very important It is important for the customer to like your product Knowing how much each company is liking it If you want then what should we do now Here are the ratings, how many ratings are there If we want to work on this then we have If we look here then the rate will be here We already have a column named now we what will we do we have to see who has received the highest rating from so what are we We will do whatever the rating is here I have given it like 2.5 2.75 so we’ll plot the graph here and A graph of all the ratings is made in this manner Let’s see which has the highest rating The rating is given here distribution so here I am I want to make a histogram so I written here pat dot hist hist i.e. that histogram now this histogram This is a histogram, all the bars are stuck in it We go through all these things since school time I have been reading things and now I am trying to code it We learn this through data analysis are comma bins equal to fyi code which i am writing you I will also explain it, so don’t worry about it We will keep its title as the title of the graph Rating by the distribution name followed by P lt dot show now let’s make it y p LT Dot have to right palti dot distribution and then here plt dot show run this so here he is saying ok data There is no R in the frame, hence the spelling It was a mistake, now look at what we are making This was what he wanted, as a histogram It has arrived and here is the count given by you now we’re seeing that the most Which likes are the ratings you received? if the restaurant has got it then here we If people watch, we can observe from here looking at this graph that this 3.5 Such restaurants get 3.5 to 5 lakh marks. The highest rating of 4 was received here So people have given so many ratings Where does the maximum rating come from that between 3.5 to 4 so it means here and look here at the code a bit if If you understand then we had to make it here histogram so i wrote hist data Placed inside the frame the column containing the rates name bean equal to 5 it means if I’ll show you once and make it three So look at all these things, they are very sticky hey right now this is what happens every time basically this is what we call beans doing five I am here and I can understand the difference, this is the thing If I do 10 then look at this and go away It will go away and you will understand it better now It will be between 3.5 to 4 at the most People have given the rating that is there now equals two if I say do two then look and you can see it clearly here It seems that the maximum would be between 3.5 and 4.5 Meanwhile people have given rating in this way You can set the beans according to your requirement You can check it after making it Conclusion of our third problem Here comes the majority Restaurants Majority which are Restaurants what are they doing so I am writing here The Majority restaurant Receive ratings from 3.5 to 4 for majority of restaurants which Ratings are between 3.5 to 4 out of four It is getting a good rating There would be concern if two are getting one If it is then you would have to work more But right now the rating is between 3.5 ser Now next thing is what do you want to take out from inside Now Jomat has observed to me that brother There are many couples when two people live together So most and especially such cities if Let us talk about metro cities Bangalore Bangalore Hyderabad Pune where two people Couples are let’s assume working professionals Most of the people who are there are from Jomat let’s order this online let’s order this online This is an overview of Jomat, now know about Jomat wants you to accept when they spend That today let’s assume there are two people and we are going to have dinner What is the average size of what you are ordering It costs Rs. 500 and we order food worth Rs. 600 Let us ask what is its average size So he wants to know about Zomat, so now for this We will write the code for what we are doing If we extract the data then let’s solve this So now we have come to this conclusion Now the next thing we are going to solve is This is how much people spend on an average order Couples do so average order spending Bye couples and this we call the heading format Let me write it down now see what to do Once you have the data frame here Data frame data head is written to the data once I will show you, now you see what is written here that is the approximate cost for two people It is given right here, you can check it here If you want to get out then here you can see now here approximate 800 800 200 300 600 is given in this manner then what will we do We will visualize this thing by making a graph because when you go inside the company The insights you submit will either be sent to you via a report You will create a dashboard there explain through graphs so that we can With all this DA as well as the visualization which yes they are performing so what can we do How many people will do it here? how much money do you spend so here There will also be an amount, we will write it down and If you write the count here then we will know It will be counted on which people spend the most Let’s see what is its average pricing So let’s write the code here I created a like variable here couple This data is written in a data frame named data. what was the name of our column inside the frame The column was titled Approx Cost for Two People wrote the name of this column, now we are here If you want to make a count plot then I have done Here we write the count plot in X access People are passing this couple data here If we go here and run it now Look, it has come to us now. Let’s see which one is the biggest So we will see here what 00 is Most couples spend the most On one order, we had to take out this that The maximum amount that people spend on average If you do it then 00 is its count here Most of the people we see here are After this, if you look here, for ₹ 50 People give more orders then after that If you look, most of them are between 400 to 500. more people order and if you are here If you look at it, the lowest one is Rs 950 for two people Very few people spend on food This means the average order spend is the highest how much is done so the order of 00 Couples would be the most ordered of 00 they do it right so this is us from here had to draw a conclusion, now see when the company If this data is known then it will be on their page The updates that will be made on the menu will be such orders will suggest which is between 00 to 500 so that What is the order capacity of people If it is around 00 then the same thing should be visible Now assume that anyone can buy whatever they can could you say let’s suppose that his budget you don’t have the right to buy it so if you You will show him i, his budget is 10000 rupees phone won’t he buy the right to take it? In the same manner as the customer’s budget You should consider it as a company and accordingly When the items were shown the company understood that Brother, when two people order food then they People order food up to approx 00 The most we do is up to 00 If you start showing them the products Is this the conclusion that got us here? It turned out that the majority of couples who ordered ₹ you keep writing conclusions like this when you If you are learning to do a project, you will forget it In future, always write such conclusions. Majority of couples Prefer Restaurants With N Approximate Cost Off 00 So you got this insight from here Now you people are the true professionals Whose such insights are very useful to the company Now we help in big tasks Next, we have discussed these four things here We have solved it, now we have to find it Which mode is online mode or offline mode The one that gets the most likes rating People give more ratings in online mode What are we for this offline? For this we will enter the code here So, first of all, we write it here. Heading Ch Mode receive Maximum rating people order online then give good rating or off in line so for this we will code here Now what do we have to do? I am the first one to First you have to read the data frame here once I will show you by doing it so I am writing here data frame dot head now let’s look here People here we have a rating what will we do if column is box plot We will make it and see, I will tell you here I am making different plots so that When you are creating your project By now you have realized that how to make a graph how to make a histogram How to make a count plot? How to make a line? How to make a graph and this is the fifth type How to make a bar plot like a box plot I’m going to explain this as well, so the box plot There are some boxes like this and There is some plot like this, for this I have Here you can set the size of Palti Dr. figure want to make a box plot given here now But we have written that order online. I need things, I want to order one online and rating now online order means yes yes Where ‘y’ means online and where ‘no’ is written It means offline and access in Wi I have written the rating and here passed in data. Data frame is now If I run this and see, we have this Look at the graph that has come in X Access We have online orders, so many people If yes, it means it is written here then you can see it There are so many people who order online here Their ratings are higher, from 4.25 to 3.5 in between and those who are offline and those who are online No, their rating is 3.75 to 3.25 This means that when people panic online They give good ratings as compared to the We understood this thing offline on this So through the graph we can see it here Let me write the conclusion so I am writing yer conclusion and in conclusion we are will write offline order receive lower rating lower rating it gets a lower rating in Comparison to online orders which has good rating then here this thing I understood it when people ordered online so your ratings are good but You need to improve a little bit offline You will need it so that is the conclusion now The last question is also a very good one The question is, we will make this heat plot You guys news about heat plot You may have seen many channels etc. but We will now learn how to make it So look, so far we have found out that the There are four types of restaurants What kind of restaurants do we have near us? we have a restaurant we have other type of okay we have one dining type of restaurant we have cafe And we have a buffet which you can order those restaurants are ok i understand that Now the order is coming in every restaurant The way to do it is to order online or So that would be yes, people can order it online or no Now let’s go to a hotel When we eat, we also order Zomato from the same hotel Does it come to our house or do we come to that hotel? can you eat it also so yes or no means Now there is both online and offline in between I want to make a graph that looks like this so that you can understand which restaurant it is from How much food is ordered A color scale We will set it up here as The color will get darker here, it means More and more people are ordering food and colour If there is light, it means less people will be there If you are ordering then this is the heat plot For this we will write the code here. that’s how to write the code for this ok So for this we write the code here. You will find this code line by line here. I will explain so don’t worry about that First of all let us look here, if we Here we have the pivot table here Before I want to create one Let me show you the bars and the data frame So that you get an idea, so I am writing here data frame dot head and look at the frame here once Let’s take it, okay, so now what did we do here? So I’m creating a pivot table here. Because we want to create a table here. I have changed the variable name to the pivot table name placed the data frame and pivot table placed inside it Which are our useful columns like we Which type of restaurant is required here? Inside it’s like a buffet, all these things are here So inside these types listed here we have Enter the column type, now order online If we do it offline then we need these columns also So we have written here how to order it online Here we set the size of the function fill kept the value zero now here we create If you want a heat map, I wrote it. Insert this pivot table inside Enter Equal T true and C map means color here we I will set this color, I kept it yellow green And we have kept the blue format as D set. The title should be Heat Map x Online order should be written in access and y should be written in access as listed type this now if I go and run here If I try it then this is ours here This is a heat map, now it’s ready, one line at a time OF code is understood according to this What we needed first was listed in Type and order was required online order then If you go here and type in these listed If you check here, you will find all four types Restaurants have come to you Dining Cafe & Buffet Right Near You It has been written that we needed the second thing whether the food has been ordered online or not It has happened online means yes and it has happened offline Meaning no, so now we have put color here. I had set it up here which I will tell you here She was showing the color which we have written yellow Green and blue, as the color becomes darker look look this is the dining that we have There is a restaurant in which there are 77 people No means they don’t eat online but offline have eaten food basically obviously when we Many times we go to a restaurant for dining Ambience to get the feel of people dining to get the feel of that wipe what do we do sometimes Going to restaurants for dinner etc. so mostly here we are seeing that If people use the dining type of food offline If there is a restaurant then people are more offline there We eat 33 but people order from there But if I talk about cafes then cafes People are going out and eating less, and off-line The food which is most consumed is snacks If you are ordering from a cafe then these colors are like As it gets darker it means range The stronger we will become now if you look at others then 2 but here we are People are seeing that two out of every 10 people of others which is cafe not cafe but other type The restaurant is going offline to eat and most people online buffet Same thing is happening in the buffet too On an average your people are three here If you are going offline, then four people If you are coming online in this then this is the whole I got the complete analysis after looking at this one graph. I am getting to know all the insights from this If I am able to take it out then I will get its title I had to give a heat map so I wrote PAT Dr title so look here is the heat map x We had to give the label in the axis here and If I had to give it in y axis then it would be in y axis We have listed these types and We had to give it online for X access order so in x access if here I Let me see, my online order has arrived now Look, the color is getting darker As we move towards the color blue We are going to do this show with darkness and if your range is low then click here Look at the yellow and green type shade It is showing you here, so in this way This was our conclusion from now on looking at this, looking at this graph Let’s talk about what conclusion is coming out We will write this in heading format Let’s take this conclusion so here we can The conclusion here is that Dining restaurants which are mostly Offline orders come to him from cafes We get more online orders this time Suggest that clients prefer to place Orders in Person at Restaurants This means that more people like to go in person Let’s talk about the ambiance of the restaurant and to pick up the vibes but mostly Online ordering is also available from the cafe, joke Mainly items like your snacks etc. If it was then it was basically a Zomat We have the complete project And sometimes such questions are even asked in some Jomat When hiring happens, sometimes such things happen Questions also come up, so if you do this The project that we solved today If you solve it then definitely somewhere Somewhere this will boost our confidence our portfolio and our Understanding our skills is very important If it makes you strong then this was today’s project whose code you have in the data set everything Description box is available below you can check it and many more like this projects if you want to do That Python project you want to do Whether that project is of machine learning or Then you want to do that project in SQL. or Power BI, then there are more like this Your projects are complete If you want to watch video lectures on Python Want Machine Learning SQL or Power? If BI offers these complete things for free The I Scale is available in courses You can check out by going to the website You can check The Eye Scale website and app Where you can find free data science data If you will get analyst courses then there More projects in these courses by visiting You can learn more lectures and more concepts Can be used along with notes port files And everything and today’s project too I have written all the notes and port files If it has been provided then complete it and when this project is completed Don’t forget to tell us how you felt in the comments How confident are you about completing this project? If it increases or not then I am waiting for this thing If it stays then we will meet in some other time Keep Data Curious in Video learning and stay motivated along with the Data Science & Analytics on iScale’s Platform Premium courses in data analytics too are available which you can check out The Dai Scale website and mobile application But where you complete the detailed curriculum Lectures End to End Projects Live Doubt Classes Industry Recognised Certificate Off training as well as interview You will also get help for preparation the link is in the Description right, let’s start today in this class project where u We can do a complete project of data analysis inside which we must have been data How to do EDM cleaning All these things about how to do visualization But there is one more special thing about this About the project which I am going to tell you about here I am going to see, we will take on any project If we make it then the ultimate behind it The objective is that one day we go to any company for interview We have to crack the race with this mindset When you do a project, you go for interviews If you go for the company interview You will know what the rounds are Most of the data should be known analytics data science business analyst All positions that are being hired for In that generally like three rounds of There are three to four rounds of interviews and It depends on the company company but what happens mostly is round Number one, the company gives you an assignment Sends you some questions in which There are problems to be solved, they will solve them for you You have to submit it within 24 to 48 hours time is given if you win in this round If you get shortlisted then you will have to appear for the round A call comes for number two technical and If you are shortlisted in round two also If you go then your third round is HR round or the managerial level round which is called If you get a call for that then this project Through which our round number one is Where the company will give you a case study How to solve the assignments she sends how do we submit it should make projects like this that we prepare ourselves for this round Was able to create potential and for this round You should be so capable that we can take out the round So these things are also included in today’s project You will get to learn how to The company provides case studies, so here the most first what happens like when we have a Do any assignments come to be solved? For the interview, first of all the company tells you about yourself Sent documentation or PDF or something like that in which first of all you understand After that let’s know the details of the company You are given questions in this assignment I will solve it and submit it to you if it has to be done then it should be done in the same way as a You get an email from the company’s end just like that I am here to help you get this project done If I am going to start everything first, it’s on you Introduction given about the company u Technologies commonly known as u It is said that this is an American multinational Transportation company and more Courier Services Food Delivery Fry This is also true in sectors like transport The company works as the main head of this company The quarter is San Francisco There are more than 70 countries in California They operate in 10,500 cities. World Wide Inky Rights Available Services It is available along with it world wide It is a big platform with 15 crore Its monthly active users are 60 lakh In this the drivers are active and its If we look around every day then it is 2.8 There are more than a million trips that take place People do this using this platform. Now if we talk about the company’s revenue How much is Rs 37.2 billion for 2020-23 It has a revenue which is quite large the amount is because it is already very There is a big multinational company now about this You have got the information, now this You will get information about how it was made here you are getting that here whatever you are getting You can see the man on the screen here Its name is Camp Garrett which is basically u He is the founder and the idea of ​​making R is his The first thing that came to my mind was basically this It was 2008 that Paris was in a lot of trouble It was a snowy evening and a lot of snow was falling Not much public transport available So in that frustration he told them It started coming to my mind that such a There should be technology, there should be such an app so that I can book the rights and My driver is standing in the place where I am standing If you come to the place and pick me up then this He had a problem and he had to find a solution started and from here the idea of ​​u came to him it came to his mind, he started How did you not become a Kudmir in the beginning In the beginning it used to be that you said let’s suppose If there is a car let’s suppose that car You have booked, you want to go somewhere booked a car, now there are more such people who are suppose that your office is like this, I will take you by example Let me explain to you that your office is Let’s assume your office is near India Gate and you are the one who has to stay let’s suppose you Do you live in Chandni Chowk now? If you need to take LETS to go to India Gate If you want to book then what used to happen earlier that all the people who live in Chandni Chowk And for all those who want to go to India Gate what would have happened to them was that right sharing What used to happen in that was that whatever amount of money you It will seem like it will divide you people. If you can, Uber started in this way first but after that gradually what today is we are looking for u if i can find any place I am standing now, I will take out my p If I book the rights in that then The driver will come directly to my location It seems Uber was not like this in the beginning initially the rights sharing platform which was that we can share the like cost that I had an idea of ​​the method, but slowly This idea changed and the camps that followed He met Travis Canick, who Its cofounder is and this is Pra Cania King Ye was very good at technology He had already formed two companies of his own and time, after that they got Uber If he liked the idea then he also told u I joined and in this manner I got my job in March 2009. in u starts properly Now when u is launched then first of all this sa Its beta version launched in Francisco It was done and R’s first ride was in 2009 It had started in but its first The ride that was booked took place in June 2010. very soon very positive to u I started getting response from people so in 2012 In 2015 u started expanding internationally The first thing they did was their expansion He started expanding it in Paris and also in the global market u When it starts reaching then the right sharing of u is Those are the four types of right sharing u x i.e. that every day if you get an affordable right If you want then you can book u x if you want u pool You want other passengers with you and everybody has to go in the same direction So you can book a bridge if you need more More premium service than Joe Black For cars that run in foreign countries u can book u group and u can book single You have a very large group and you have a lot of If you want a big spacious vehicle then you You can book single as well as today u ts u Freight and u for Businesses like You can also sell your business on the platform moving forward now how do u earn money So whatever commissions we get for whatever rides The commissions that are there in the booking From that u earn money, there is subscription is that you let’s suppose I am one such I am an employee whom you book every day and give me your If I have to go to office then I I can buy a subscription and You earn your money from advertising he earns from here the main important thing is u The thing that is important for us is that you stand strong Data Science in the Domain of Analytics it works on domain why does it work if so then its first point is this TA estimation i.e. arrival time which it happens now let’s suppose that I am here I am standing, okay, one of my locations Suppose Chandni Chowk and where do I go if so then let’s suppose I have to go here Chandni Chowk to Rajiv Nagar now its B’s The distance is how much time will it take for the driver Which driver is available in coming then this For the estimation of all things we people You can also read Machine Learning Algorithms Tips here Lie learning algorithms are responsible for all these things. now as soon as it is needed I book u from one place to another If you know then it immediately starts telling the price that If it costs you 00 then this is the price Prediction is the data science behind it There is a big rule of data analysis Optimization moves from one place to another How much time will it take to estimate the Have to tell me the route for all these things Algorithms are also used in data science happens between the driver and the rider Now we can help you in making perfect match Even if you make payment many times, then someone There should be no fraud in that payment, There is a domain, it is mainly used here Because of data science data analyst You just made a pap of u but that pap Can’t tell from one place to another How much will it cost me to get to the place How long will the driver take to arrive? He will be in some This is why data science is useless All these things are possible with the help of analyst it comes where we can tell that How long will it take for the driver to get your location How much will it cost which one is good This is the route to go for all these things Data science is the use of data analysts and so u are also hiring for this position if it does then this is what started in you That was for data science data analyst It was launched in 2011 to 2010 But work on it has been going on since 2011-10. This is the person you see on the screen Look, his name is Kevin Novak He was basically the head of data science and used to lead The background of me and him None of them came from an economics background It seems that I am a master in data science There was nothing like that were supposed to come from economics domain but He had a very wide interest which he Data Science had developed the skill set He is very much into data analysis Now this person was helpful, he was seventh I had joined as an engineer in R A very small company was started very early It started out small, so when The new one has come to give you the interview Someone told him what kind of a startup is It is open, you can give interview if you want I got to know from a friend that when he took the interview When I reached to give it, I was wearing a coat, pat and tie We reached there but it was a company which She was walking from a very small place so she felt that you should join or not but He knew very well that here This company can definitely grow at some point It will create a very good revolution They then used computational algorithms to This company has captured the science here I started leading it and as a chief Data Scientist he worked as a new After this, in bringing the technologies Now, it is of use to us that the thing which the company has It is given here in the documentation This is that we have told you everything about the company Now let’s assume that you are about to that you are working for data analysis I have six questions for you, You have been sent an assignment for your You have to solve this in the interview what the first question is saying is which People are most interested in this category let me tell you this second question he is saying that most of the rights people For what purpose do you book to go to office I book, I book to go to a party where do we go and book restaurants Let’s book it, after this the third one is that At what time do people book the most R caps? Now let’s see what the company needs to know It is very important, let’s suppose it is 7:00 in the morning Most people come between 10 am and 15 am Let’s say I book a cap to go to office Unless the company knows this, the company How is the driver available at that time? You will be able to show all these in the app It is very important to know the information of the company and we brought out this information here Forth is telling which month The least number of rides are booked during the week Which is that day on which people celebrate the most? book more rides and how many Miles or how many kilometers people have to travel Prefer to book rides now All the information is given to us by S.A. Assignment Now answer all these questions We will remove the curse from us here It is provided, it will show you the data once This data is provided to us by the company There are many columns in this data that the right one starts with what is the start date what is the end date who It is C category how many miles do I have to travel What is the purpose of knowing all this information We have been given what to do now Insights have to be extracted from this data and this questions are given to us to solve There are six questions, we will answer these questions We will solve it one by one so let’s go now Direct move of Jupyter Notebook and let’s solve this assignment in this so first of all we give the project all right People will see that you have the notes How to get it Where will you get the data set You will get the code files for this What to do simply Inside googlegroups.com Lore Courses You have to go here and select the category Want to do free category now it is free here Our free data science category There is a course in it, not only you will the project’s data set but also Lectures on machine learning So you can login directly First I’m going to go to Register Now here and After this, your details are here We will mention the details then after Your email address is sent to us here You will have to enter whatever mail address you use. you can post it here so here on the island contact@gmail whatever is your mail id Basically you have to put it here After that whatever is your contact number, you can I will mention it here, after that you will see it here You will get the option to register You have to click on the register now here Your verification should be successful The mobile number you entered is already there In that you must have got the code here, so we We have registered by entering OTP, now we are here You can directly login to any You had entered your registered number here you will enter your email id after that whatever The password you must have created is simply We will enter the password here and then We will pay here as soon as you log in You will be successfully logged in here to see the interface something like this will get Now after this as soon as you go here If in the courses enrolled in your courses You can visit us for free courses here If you want to select pay then right now we have selected any This is why you did not enroll in the free course It is showing blank here so its what we will do for this explore here We will go inside the courses here again Selecting the free category will give you free data We will go to the science course and Here you will see the button ‘Enroll Now’ You can see the button of enroll now right here So by going here we simply have to tell people this Is this a free course within the course? There are no charges for this too so here we are Simply go here and enroll now You will now be inside this course by clicking You have been successfully enrolled, now you can return If you go to your free courses from now then We will show you your free course here If you open this free course, you will feel The first one you see here is Data Analytics Project You can find it here All the notes, code files, everything. You will get it here, you will get it here plus The symbol will appear as soon as you go and click on it If you do this then you will find it here No notes on the project and find it here will go to u project’s data set this is then You will get it as soon as you click on this If you click, you will get the notes Also, your data set is You will get it easily here, how to download If you want to do it then click here to download You will see that you will see the symbol of two arrows It is given here, simply go to it You have to click, it has been downloaded and this We have both things in proper manner If it has been downloaded then this is enough for you Along with this this pre course will be helpful Lectures on Machine Learning in Python Lectures, all these things are for you people. If available then download the notes in this way So let’s get started today Let’s go to class and start our We have to open the project now How to do Jupyter Notebook in your Your Anakanda will have to go to the system Navigator is installed, you need to open it I already have Anaconda Navigator here It is open here you can go to Jupyter Notebook As you go to the latch you will find the button here We have to click on this launch button If you want to do it then our Jupyter Notebook is our It will open on the browser as shown here I am done with it, now what are we here for? I will create a new folder Now, here we have given this data set There is a lot of data in this data set This data set is available here We will do it, we will upload it here Firstly I uploaded it here, see here The pay button is given to you here upload In this I will simply go here and upload it we will go to the button this is u data set this I opened it here, it has been uploaded It’s gone now here we’ll go to python3 IY Colonel here we are, We will create this file which is ours to open It’s over, first of all we have to rename it Let me name it here now. hmm u data analysis project is it ok We kept the name and have renamed it now If you go back to this folder now Here you will see u data analysis the one you renamed it here The rename has been done, now what is the first thing to do If you want to do it then the data set you uploaded Open it once and take a look here This way you will get the data set that you have entered it is available to see here now What we need to do now is to open this Jupyter Notebook In this data, we first tell people A lot of work has to be done in this If you want to clean the data then for this we People will need libraries here so first of all we should focus on libraries here We will start importing first There will be a library, Panda, which we will call We use it for data cleaning There is quite a useful library so I I will import it here Panda Edge PD then Next Library We do not do any mathematical operation here If you want to perform then import Napai S NP after this we here If you perform visualization also then import Matte plot lip this library is Matte Plated Lip: Who is this for? to perform our visualizations For this purpose import mat plot is available Plot S Pat and another one adjoining it Libraries are made of wood which also help Now she does visualizations for us We have listed all these libraries here I have imported it Now what do we have to do after this The data set needs to be brought here for reading that file is the csp file which we have I have uploaded it for what I am saying here I’ll do a variable called data By set name and within it data set is equal To read this file I should write this I am going to PD read CSP file and here But what is the name of our file? The name of our file is u data name If we have our file then we can write it here I give you some data dot csv okay then it will come here Let’s put double inverted commas in it Let’s close it here and run now I’m going to divide the data set here by this variable when I call then it is showing certain error let’s just check data csv ok here what is the name of our file data set It is CSV we just wrote the data hence the error If it has come then let’s set the data here And let’s run it again, now this run It’s done proper now here what our We have the data set that we have This is our data set file which has been read. Now let us focus on what The columns are given to us firstly The start date of your ride is given On which date did it start? The date has ended and look at the time here It is also given to us that on 1st date 2016 It started on 1st of November, 2016 The end date of the right is its timing The right started at 9:1 and someone probably If the junction is nearby then it is a 6 minute right It was 9:1 right end is where it is now I had to know what the category was for business purposes I had to go to the start here which is the fourth vice Here the pierce is given, stop is given It turns out that one of the locations is Fourth Pierce like here in west palm beach so this location It is given from which location it started And how many have ended at which location? How many miles of delay did you have right to? 5.1 It was meals and for what purpose did you go? The purpose of multi-shape sl entity attainment was So in this way here you will get all the This data is given in columns every day If we look at the set that is here then total There are 10056 roses in this and the total for us How many seven columns are given here Its shape is given here, if you Have to find out whether its shape is proper so what do we write for that data set dot shape we will write it here So here it will show you that the total in this 100 116 What is Rose and Seven Your Columns Now, after this, for this data set, information i want to know that this data What are the values ​​in the set? I need information from this so I Here I will write ta set if bracket close if we run this then now this data set We got all the information, now see This is the start date and the end date Data of object type is Object meaning The data is of string type, ok, after that Here we also know the categories Data of object type is given start end stop this is also object type data which miles how many miles have you walked So what is this floating point in this ride There is data and the columns that have the purpose I have data of object type now you are here If you look at all these values, If you figure it out correctly from this If you want then there is such a column here In which a lot of values ​​are missing Values ​​are given to us here right so now these are the missing values purpose this is how we deal with it I will have to do it because there are a lot of such things here There is a lot of value which is missing values Let’s see how to deal with missing values Let’s see this, let’s move on now The data we have is so good There is no data why it is not there because here But many of your columns are about purpose. There are missing values ​​in it, aren’t we now? What will people have to do before data pre The data will have to be processed How will you have to bring it in the correct format? It will come in the format, what will have to be done Let us understand that first of all now here first The thing is that what is the missing data, first step this is the missing data with this we People have to deal with this, how will they do it Now we will learn and the second thing is that The start date and end date are given in The format of both is a string the format is the object format so now we People will have to change it in a proper way proper date and time format we’ll have to change it here right so in Now we do pre-processing of things and in this both these problems will be solved If we solve it now, then the first thing which The problem here is that I first put the heading I will give you the data, we will do the pre-processing how should you give heading then you people To give heading, write hash in space You have to apply it and then write the data pre We’re going to do the processing here. go here you will find three code marks Selecting Down and Raw Mark Down and run it here as heading This will always come whenever the company When you submit a project, the heading should be proper I want the comment to be proper which step What should I use, please tell me by commenting properly If you want the right then click on the file given here I have put it in the notes for you the whole thing will turn out in a very good way Right now I’m just teaching you project so I comment a lot I am not wasting time by writing etc. But when you download it in Notes You will be shown how to make a deposit what is the right way so here we are now In data pre-processing, this data is Our set is inside it for the purpose The column has a lot of missing data in it The one you see here is written by the name NA If it is right then this data is not available Now what will we do with it here? you will figure out what I want Wherever YA AAA is written it is missing These are the values, write them here instead Not OK means data is not available there is no purpose in calling it not If you want to replace then this is here My data set is: inside this my column is what is its name so the name of my column This is the purpose, now this is my purpose column what to do inside it instead we People feel a function, we are here We will put it here and NA which you have just written in its place i want to write not come and tell this thing to us here If I want to do it permanently then I will write to you in place i equal to write right and its Later we run this so here for you to see something like a warning you will get it and after that if you come here again data set dat head call karo head means You have the first five data if it comes in then i will do y p data set dot head If I call you here then now you are here But you will see that earlier he was coming with NA written on it Now it is here in missing data in purpose It is coming written that it is not ok now we know no what is the purpose its place is empty we have put it here not ok now its What do we have to do next step after that So now in the next step we will tell you this The start and end date is its time which is why does it need to be changed in its format It has to be changed because the format it has right now is What is that object type, see it here It has been given to us in exchange for this what to do with it in the rush of date time how can i put it for best First we will set the data on this and its name will be given below: inside this is my start date and end I have column name by date name so I will write this as the start date ok Write the start date here and After Start date and then what we have to do I would have closed the start date and the bracket is equal to now we have to change this is in date time format then date time Pandas library for converting formats which is yours so i wrote pd dot Inside this there is date time frame then PD dot to date time this hum log yahan pe function will you find out what does this do for date and time if that helps to convert the format Here I have written the date, time and its date If you want to change the time then this is the data set Inside this, your start date There is a column that we need to change so I will click here Let me write start underscore date ok yes, we write it down here start date and then one thing here what does errors equals two mean Now let me just explain you each and every code The line I am going to explain to you is very There is no need to panic too much You will understand something now that I wrote here errors equals to course by doing this means What happens if there is an incorrect data date value it’s right if someone is wrong it means there is no date If there is value and it is wrong then what is it to him? what does it do to fix it Not a time nt in his place puts the value it means not a time Now let’s assume that we know that Like in the morning, whatever time it starts in the morning it is right 6:00 o’clock, it is 12 o’clock in the day It is 12 o’clock, it is 12 o’clock at night, so It does not happen that we tell a different time are you here or else if we like Indians If we look at the time according to the railways, then one It rings, after that 13 14 15 16 such timing It starts, it does not happen that 26 comes If it goes right then this wouldn’t be a fable There is no time like this if there is a wrong time if it is inside the data then we can do it If you do the course then they will remove such wrong data what will it do, it will put the NT value and it will help to remove it so I have put it here it is written like this now this thing which you have written in this We have done the same code with spurt date For whom do people want to do the end date also If I want to change then what should I do here? by typing this code control A control C I copied this and then Now I’m going to paste it by pressing Control-V. Wherever there is a start date, I will what will I do when I go and date Because I need to change both these columns. If you want to do it then I have written the end date here Even here where the start date was I I will go here and change it wrote and date right now after this finally If we go and run this code then it is showing get set not defined ok here Somewhere it is showing data set not defined We made a mistake here, the data set is There must have been a spelling mistake in this Because this is an error, now the spelling has been corrected The error has been removed from here, what about here now? You have to import date and time so now we have changed the format Now let’s see if it has changed or not format actually so I’m going to read here set it If I write this and we run it here now let’s see what it was before The object was showing in both of these now The date and time are correct here now we will do one more thing, what will we do here now But we will create two more new columns what would be a column here that would tell us people which date is proper which date is right It has started, tell us this and Look here, the time is written here right i want some time here separately Let these two columns come in the column what to do here is to prepare it Can you tell us the proper time here also? What will we do for him? Two more new ones If we create columns here then How to create the columns for your So here we will import from date time date time a module already it happens this library is built here inside it we will import date from inside it time then this is our data set Inside this we will create a column, its name is There will be a column named date and a We will create a column, its name will be time here What I am after is what is here till now I have even given the purpose, I am saying that and two we will create new columns of one column The name will be date and the name of the second column will be time so that the date is written separately and this It’s time, I should write it here separately. So we’re creating these two new columns. So let’s make the first column date For this I will write PD here by name Dot date time index ok this is the function we call people will call it date time index and its What will we put inside the data set we have We already have this data set in our there is a column its name is start date so i It is in caps here so I will write here I’ll give you the start date okay now this start date so after that here we People should close the brackets properly After this, we will put the date dot here. date is ok i am just doing this much running I am showing you so say data time here ok here data is not time but from date time must be due to spelling mistake There is an error, the date was misspelled So there is an error, we have cleared it now. Now I’m here to set my head if I call the bracket should be closed now Look, a new column has been created in it What have you given as your date which is different here This column is given to me by It became possible to create another column in the same manner I want whose name is what whose name is time to do what it takes for him I have to write the same thing as written in the code above I won’t be able to spend time writing and explaining I will copy this so that I can save you time I have been doing it but when you guys do it yourself typing the code as much as you can The more you practice the more you remember things it keeps happening now I want that my The name of the next column should be time and this is whatever for the start date and here I am now Who needs this in which format of the hour In the format, I have written our here Now after this run the data set here If I do it then look here, yours has arrived now A separate column has been created with the date name and a separate So a column was created with the name Time so this one He was with me, now he is separated here There is a separate date and it has been given separately It is right, I have understood this much now Now let’s move forward and do one more thing How will we divide it into categories now? Look, we don’t say that Mostly if there is a ride at 6:00 then it will be at 6:00 So the morning can be saved and the evening can also be saved if possible then what is the category that Morning was right, afternoon is also ok was it right or was it evening’s right or It was night time right so what are we going to do with it now? We will divide the morning in this category afternoon, evening and night so what am I I will create another new column here that What will happen in the column will be that we This is the time, this is the category we created If we are going to put it here then I will create a new column here for this Name Let’s say I’ll name it D Night So which right was here for how long For example, look at the time in the morning, This time is from morning till 10 am. what do we call morning time They say right now let’s suppose from 10 o’clock By 3:00 in the morning, this is yours Afternoon ride is fine now after that evening ride From 3:00 PM to 7:00 PM It’s time for your evening and then it’s 7:00 This time is from 1:00 am to 12:00 pm If your night is over then according to this we Now people are divided here by making categories how will we make the category so let’s do this Let’s see, I have another new column here I will create a data set inside this and name it I will celebrate the day of another new column here I’ll name it Knight okay So what am I doing with this data? Create a new column inside this data set I am writing this, what should I name the column Day Night so here I am My own column is called Day Night I am keeping it okay now for this I am here If I want to create a PD dot category, I will use We will install PDCT function Equal to data set: We will write P here. What time do we need in this data set Okay time this is the one that our column is made No, this column, this time, this column I am posting it here because of the time how can you categorize without that If it is morning or night then I wrote here times comma now bins we will write here that Do we want to give away what we have? What does right buns mean time frame that means from 10 a.m. onwards like I Here bins equals to square bracket I will apply it from morning till 12:00 in the night from after noon till 10 a.m. Morning time will be from 10 am Three till 3 p.m. is 15 okay that means till 15 means till 3:00 Our afternoon after that 19 means 7 in the evening Evening time till 12:00 PM and then 7:00 PM 24 after 00 o’clock means 12:00 in the night It’s been night till now I have not spent this time here I have given it here now what is the label of these beans If it happens then the first label will be ours, that is Meaning when it is 12 o’clock at night we say yes, if it starts the next day then from the next day onwards The time will be from 10 am to 15 am we will give it morning label so I here the labels should be equal to To the first label I will give here in the morning ok morning i wrote here The next label I give will be mine I write about the afternoon here Afternoon then the next label which I’m here I will pay, it’s morning, it’s afternoon Then in the evening I write it on here evening then what will be your next label Obviously night this label we put it on okay we have given the code here run so it is showing certain syntax error or syntax error because here We forgot to put inverted commas Let’s run this code and see what happens We wanted to create a column and the column is here whether it came or not then I will write here I give you the data set dot head after this we run it So here we can see yes, the column we are looking for He was here and has come and is now here It is 21 which means it is 9 o’clock in the night here The proper category has arrived night right so this By the way, you have everything here Now it has been arranged properly What do we have to do one more thing here in data pre processing that All the null values ​​here are right We call it the data in which there are missing values. what people want to do is delete If you want to drop him a line You will have to write the code key in data set dot A means remove any null value to do this and write it to us I will have to break it in equal place so that it gets removed If these values ​​are taken then I have written them here place equal tot let’s run it is showing certain invalid syntax latest check what sort of syntax error it is here It has been installed by mistake so there was an error here This has been removed, now we will come here once again See, by setting the data to a dot shape, here it is There were null values, they have been removed from here now Our shape has come to such a pass that now this There are 403 rows in the data and there are 10 columns. It is now available to us what do we need to do next step The first step is to work on data visualization what did the question tell us which one People are most interested in this category right book if we do then we will go here and see that We have a column called Category Category is showing business can be done and There should be some category in which category the people are If you book the most over rights then We will make a graph of it here. If you show me then the first question is We are solving it here and Our visualization starts here now. If yes then I should give proper heading here also. I will provide data visualization from here we will If you are starting then apply hash here We need to select mark down and this is our heading is speaking the first question For which category people are most interested If we book open rights then what do we do? We will make it in graph format A graph in which whichever category is the most If it is more then we people know easily here If I could walk, what would I do for it? People will build it here if we If I want to visualize it properly then I I will write here pat dot figure we here I want to make it and this figure is We will make two plots here I am collecting the questions here together what is the first question to solve He is telling that in which category people are Who books the most and for what purposes? If you book for right then we are here I can make two sub plots so I will make two Put questions together in one diagram I want to show you what I want to say I am showing you, you will stay on one side We will create people for the category and another These two will make a graph for the purpose The graphs that we will have are our bar graphs This is how we will make graphs And whichever graph is higher, we will know Most people will go for this category overwrites the book and for this purpose So we have two sub plots here I want to make it, that’s why I am writing here I’m going to pet dot figure and then we People here will judge the size of your figure If you mention it here then I will write it here Give me the size, this is the size, let’s suppose figure size equals two 20 5 i want to keep the figure size here hmm okay we have this figure The size is coming now I have to make all the plots in this i want to make two plots so I will write here palti dot sub plot because two graphs have to be made So here we will write two graphs I want to make x axis wa axis for this In a way, this is a representation of you right now. Now I will make this first I will use the Seaborn library so I wrote here SNS the first plot which we People are making it here which gives us the feeling of a bar The one that looks like this in which numbers also appear is I have selected our count plot here. why did you take the count plot ma’am so the reason for this is now just making a graph When it comes you will know how much it counts This is what we have to take out the most Which category do people like the most? If you book it means most of the people It means more than just some number There will be like 1500 people, let’s say 1000 What do people book rides for? There may be 500 people to go to the office If you overwrite the book for someone else then I need the number, that’s why I am here It is written count lock so that they can count us If you can tell me the numbers then I will write it here AA D Count Block Inside this, we have data I want to create a data set, I will write it here We found the category column here If you want then I have written it here category okay after this here I will I will close the bracket now after this If you try running it, then look at it this way Two categories have come to you here One category is your business and the other is Personally, now if I count this on you If I see the plot, you can tell me comfortably You can choose the right business category for your business. I use u’s personal name the most We use less for category, it is clear It is understandable up to this point Now let us move on, here’s something else If you want to write down things then this is an If I want to rotate the axis and write so here we can write PT D A6 You can also write here You have to write in small print Palti D xxx xxx and in this rotation we people You can write that if you rotate this, you get We have to write rotation equals to 90°. If you want rotation here then I have put it here It was written 90 so now look here, this is yours The category has been created and here is your You have already reached here by writing the count. is already at 90° so a graph This has become ours now, this is where the purpose lies I mean our first question was that we It is clear which category People book Uber Right the most for this If yes then people are most interested in business category This conclusion is why more overrights are booked We have made it this far, what is the purpose now? We do this for the people here If you want to remove it then I will make another sub plot here If you want to make it I am here I will write pat dot sub plot now all this Inside the plot I will write 1 2 2 ma’am this what are you writing tattoo in the first one Look, I had to make the first plot, so I had written twa which means two is our already access is already given here first I have to make a plot so I wrote forest here Now I want to make another plot. I am writing here 2 so 1 2 2 now who We have to make the graph here We will also tell you which graph to make We will create a count so that it can count automatically If you can tell me then I am writing this Dr count plot now inside this count plot is the data set now inside this data set which This is our column, this time the column we will take is We will take the purpose column because category We have already made it here ok so i put the purpose here now If I simply run this by going here So now look, two graphs have appeared first There was only one, now two graphs have been made here If we look at the category, then business People use u the most for category We do this not for personal category From here we understood in the second graph If we go and look here, then not that you know that n n values There were null values ​​so we named it NOT It was written in it that now if we go If you look, most people use it If you use u for meeting then it is obvious It is important if the category is most important for your business if there are too many then what is the purpose of using u I use it for meetings what else would it be used for after the meeting if yes then it is in second number mile or They had to go somewhere for entertainment if yes then book it Then use it again for customer visit Then use it for supplies To go to the temporary site and Office suit is for business For which category and for which purpose People book u the most u have the best time to go to meetings If there is more use then these were the two questions Whose answers we have to give properly here what was it used for we did it okay so answer these two questions We have just solved the previous answer here Business category came, second answer came meeting here now asking which one People book u the most for timing Let’s kiss you like a morning Now this is us here for the evening I have to take it out, how will I take this thing out Let us see here which one if people book u for timing then We will make it here too snssdk1233 it is booked at night for this Which column do we have for day and night there is a column so we have called this day night column I had made it so I will write it here This is our column this night We will plot the counts within the column with Now I will try to make it, simply if I run If I do it then we will have a simple one The graph has arrived, you can see in it who u are most booked for this time so u do it the most during afternoon time It is booked during evening time at night time and at morning time u People book the least and this is also Our third question was at what time If you have the highest count then you can see it here there are almost 140 of us who are 120 people book for afternoon who in the evening time we do approximation 70 people do it at night time and In the morning time there were around 50 people who yes they book r which means most Most of the bookings are done during afternoon time So here we were asked which one Uber is booked the most on time that’s the afternoon time three We have just asked the questions here We have understood, let’s move forward Let’s look at the next questions now The next question is what is he saying that here we have to analyze the month which is the month in which least number of people book u so now we If people want to see about it then month What will have to be done to analyze this This is our data set column, in this we People will now have to create another new column Let’s know how to make a month whose name is Let’s see, don’t ask me any questions I have been here wondering which month it is Where the least amount is booked and during the week Which is that day where there is maximum crowd? What will I do if I get booked? I’ll go and check my data set now so let me just right here data set dot head I’m here I write but let’s run then see now What do I have to do here? Give me more New columns will have to be created here, this We have a date, so I can know the month what should i do here for this you will have to create a new column in which month comes ok january february now Here you will see that just like January 1st here pe hai right so what day was 1st January because which day of the week do we If I want to find out then I need another column here I will have to make a new one where I Where can I find out about the day? Let me know what right means That particular day was Tuesday, Monday, Wednesday We’ll have to put him here first of all what will you do for these columns If we make it here then we will need two new ones for it If you want to create columns here then Let’s start our work in the first month if i want to analyse then what am i doing here I will do the data set inside this data set I will create another new column named will be by the name of month okay I am by the name of month And I am creating a new column for this Here we will write PD dot date time This is our index, which we will call here will do pd date time index function like we did it before, after that this There is a start date, right, inside it we will will take out Start underscore date and start date which is our There is a column inside it, what do we know If you want the month then I will write it here start date bracket proper close The start date bracket will be applied here square brackets so let me just write this here bracket properly closed dot We will put it inside, we need the month properly Now it is month that we will have to do labeling as to who what do you want to give to me this month so I Here another variable named month label I will create month underscore label inside it What if there is simply nothing inside it Maybe we can create a dictionary here There must have been 1.00 if it was written in this dictionary what does it mean one then dot 0 if If it is written like this anywhere then it means the first the month that is the month of january okay then after that write for February if so 2.0 so I will write here 2.0 somewhere if it is written then coolan should come here This is our key and the value inside it If you want to enter the value pair then we will enter it here so if we make it then this is January it’s okay in this way if 0 is there anywhere This means which month is that? The month of February this is exactly how we are all up to Till when will you write likes, fill it up till December if you want to check it out then I have already posted the code here It is written so I am taking a lot of time now so what am i doing simply I am copying you here so I am simply Just copying this, you guys should not copy yourself Writing Code from Habit to Practice Things to do it’s necessary okay so let me just do one thing here let me just copy this control a control c and here control b so here But now we will see what is written. If it has happened then let me explain it to you once PD. date time index data set start Our date was here, after this every month We have created a dictionary here. what have you made in the dictionary so that here It is 1.0 which means the month of January is 2.0 which means February is 3.0 meaning March is up to date Now we are back on the set till December what will you do with this month label also will map so I used map function used so that the month which is with it The measurements were done and then finally we came here Created a variable named Pay AO and entered the data put the month value counts inside the set And after counting this every month here, now I will give it to you, I will run it and here I’ll go and write data set dot Write head data set dot head here Let’s run so we can see that month in the name The column is ready and look here, January If the month is one then here you will find the month of January has started showing so the column which we I wanted to make it, now it has been made Now we have to figure out that monthly Right, in which month did it occur the lowest? yes that was the question so here it is What do we have to do for this graph? I will have to make this icch padi dot data frame ok pd dot data frame i am here But I will apply it and inside it, whatever is our month’s wish These are the values, we will put them in, I will also I am telling you not to be too worried that this what is happening suddenly it’s a very easy code But I’ll write it down first so I can To make it easier for you to understand, here we have One basically puts it in a dictionary format we want to post this so let me just use here the curly brace okay I need to put curly braces so I will put the code here I am pasting it and the data here The frame is curly braces we wrote months The values ​​of the month in me and the value We are applying group by in the count So that we can know how many likes we see which was that month in which people were most If you book less rights then how many are these He has walked many miles, if we come here Maximum amount will be calculated every month We will calculate the value of maximum biles and If we calculate the total for each month then we will know It won’t work, what month was it like In which the cabs that were least booked were yes right so that’s why how much you and I are If you book for any distance, then every At least a few miles have been covered in a month, it seems like Suppose in the month of January, Rs. 600 I have walked miles, maybe in the month of February you have travelled 700 miles so that’s us has to be taken out here and after that here I want to make a line plot of months And we will put the value count here Now we have a line plot here. Let me run the code once and show you Then you will understand this code more easily. now look at the months here As we will see here, these two lines You will get to see this blue color This is the line that it is telling us count of months and this thing here The line is yours, we can speak it You can see the dotted line here The dashed line is telling you this value count value count means that every How many miles does your car travel in a month? Whatever it is has gone here so if we Here we basically have to focus on months that in which month is there such month where u rights are booked less so if If we go and see here then you will see here You will see that this month of January and February You can see the downfall here and People are seeing November-December These are such down falls in time where you If you see this, you will analyze it Also if this count is your plot if you see this also then this is such a time It is the time of January and February and these are It’s the time of November-December where the graph We can see it falling like this here That means that this is cold weather It is a cold time, whatever rights you have in this time It is the least booked here and This data is basically US based data And it is already snow fall time there If it happens then we will consider it as a luck rough evaluation if you want to take it out and show it then we can say that it is a very irregular curve but the time we People can see here what time it is This is the time of November, December, January It’s cold time u rights these months I am booked here so this is our This was the next question so we answered this as well Here, we have solved the first and second Which was the third question and the fourth question? In a month, here our month is January The month of December was here in December november january november december this month Most of the rights are booked less We have taken this out, now take out the week also if so then what do we have to do for that one more You will have to create a new column, this column’s name it should be right means right now I will give you If by writing data set dot head here Let me show you, let me run, now look, we have done this for the month It was created in the same way with the name Weak here Another column will have to be created for the next question So how can we solve this here We will make a week, if we look at it then it will be like a month we will create week in the same way as we created data set Inside this we will create another new column, this column We name it day because it is week which day is inside monday tuesday wednesday We have to justify this here so this is the data set and here I wrote de Now inside this we will write data set dot This is what we have called Start Date there is a column, we will take this and for this start date Inside, we convert every data into what We will give DT dot on weekdays here dot DT D Week means now it is converted into week day Now we will have to give labels in the week So I’ll write here the data label. Like you gave labels for the month, January It’s the first month here somehow What will we create first? Dictionary now this dictionary we have Let me just write has been created here this year now we will create this dictionary if If there is zero written anywhere, it means Monday happens okay so here I took the p label Zero if you are written zero anywhere If you see any calculus then we will take it here We will consider Monday in the same way If it is written here ‘one’ it means that It is Tuesday so I would write it here Tuesday T ues ok next one is yours If it is written ‘to’ somewhere then put a colon And two means your Wednesday is just like this in this way if there is three then plan t means thursday so i write t h y r then Later on you will keep writing for everyone in the same manner If you keep making it four then it will mean Friday to fo colon Friday then comma f then kallan that is saturday then comma six kallan and we will write it here Sunday what has happened to us this is with us This label of the dictionary was given by us I have made it here, ok now what is the next thing to do Now the next task is to Inside we have to look at the column named Day So here I write the day and then equals to data set and within it we people We will put D inside this day column. the name of the pass column is na so let me just right this yer d let me just correct this and right this to d okay ya by name so We have to create a column, after that here p will map this to the label so dot map and here I will write day Why are you mapping underscore labels because Look here toy day we have to call people day We have to create a column from this now inside this is that means if it is written two then it must be wednesday If it is written five then it must be Saturday We need it to map both of them I have to do this so I wrote here dot loop And inside this we pass the day label to let’s run this and see after this it is showing certain error let’s just check what is the error here so I am saying it here if day label is not defined then We have defined here zero than monday then one comma tuesday th 4 saturday and Sunday right after that there’s the bracket here Let us take a look at it properly ok here is the data label there is a mistake in the name Here we wrote data label and here we wrote is labeled D so calling him a wrong person let’s do the work, let’s do the day label Now the error has been removed from here, the spelling is fine Due to mistake, let us write it here now. Let’s type data set dot head and see it running Now let’s see another new column 1 January 2016 has arrived to you which day was it on 2nd so it was Friday by the way So what can we do here in an easy way now we will be able to plot this I want to know which day of the week it is The day when the most Ober Rights books were available it happens on Monday it happens on Tuesday if it is thursday then which day is it so now what do we have to do for this I will have to code, I will write day here Underscore label equals to data set dot let me explain don’t be worried at all that this what are you just writing code to me Let me focus a little on writing after That’s the line of code I’m going to explain to you counts okay after that we will be here I will write SA D bar plot first I will take the code then I will explain each and every line to you guy x ictu day We will put the under label then dot here What do we need in index comma or axis People need day labels writing here day under the score label okay i’ll write once I take it and make a graph after that one line in Everything will be understood after that what we have to Right here then it will be then after it will be plt dt x label and x access us guys who are into label want to give in t is day and why access If I want to give you a label I will turn around and put it on my face I will write and we will write here ok once i run this code I am showing by doing so i am writing here count let’s just run this okay what I mean, I am just explaining it to you right now I am looking at each line of code, this day label We had created Monday Tuesday for this who wanted to count that now i like this I want to know how many rights are there on Monday The book is being booked right so now I am here If I go and see, I will know here Got approximately 60 rights every Monday If it is being booked then the graph here shows 60 He was able to come because we counted If you could find it, that’s why I posted it here Write the line ta set value count so that here But we should get the count in the axis Now after this here we are making beer So de label in x access and va access I also put the de label mess here In access you have de and wa in access If you wanted to write the count then you can see it here ho in bar graph in x access your It is written nearby ‘Day’ and it is written here here is the count now if you look here then here Friday is such a day where most What do people do on Fridays in most weeks? let’s book cabs because It is obvious that our Friday is over Office hours are over, after that weekend starts Saturday and Sunday go by, so here it’s Friday It is the day when people are most What do you do to book more cabs? So we got this data from here And this question was now the fifth question We have also answered which weekday is it? There is a day in which the most people book so that is the friday On this day people book u the most now In the same way, which is the least day on which Obviously people book less during the day Sunday is Saturday Sunday to us getting to see why Because here on Saturday and Sunday What happens when there are holidays, people Most of the users do not book due to lack of time on there home so Most of the questions have been solved First was which category so business category for which purpose so the purpose of the meeting what time is it used for people Most Ubers are booked during the afternoon In which month people are most excited for time? Book fewer Ubers during winters that is november december january fifth question At what time, in which week, such a day would come This is the day when there is maximum overbooking So now on Fridays we will get this I want to find out how many miles I would normally need to travel or how many kilometers do people have to travel and you use a car, let’s say We need to find out, here If I have to go 5 km then people will you overbook if you accept from here I have to go 1 km so will the people over Will you book it or will you go on foot? All this will be taken by some other transportation Now we have to figure out things Miles So now let’s discuss it we will start alright so now our miles This is the column with the last question, we will now If people explore then I will provide the data here Let me write set dot head once, now here But look at these miles, these are different miles yes right someone u have booked 5 miles to go For some it is 4.8, some it is 63.7, so different miles If there is any, what will we do here? People have booked here for miles We will make a box plot of this why make a plot because you have Like here you have boxes, how many? How far did maximum people go for miles yes we people need to know this that’s why we Here I will create a box plot once I will make a box plot and show you easily you will understand why i am here Pay attention while making the box plot so sns dot box plot and then we Here we will write the data set and the name of the data set Which columns are inside, the name of this column is We have miles right so miles this column I’m running this here and The anthesis is not closed, ok here The bracket is not closed properly, you should have run it Now see what is the benefit of making this The box plot of this ride of ours okay this is showing you approximation 25 from zero means it is approximately 20 from zero Most of the people here for kilometers You have booked it, after that gradually you You will see as many plots as you can see here This is yours for up to 175 miles The graph has been prepared, meaning from zero This entire 175 mile stretch was visible till here How many miles does maximum people travel in Uber? to go to that that is near about 175 Now what do I have to do to travel miles? This all happened very quickly and very quickly I just want to see this for 100 miles I want to see even 100 miles till when people how much do you want to book then what should i write here sna dot box plot and then here we go will write the data set from within this data set Which column do we need? the miles column from inside so here I am I will write to you Miles okay now here Every mile I want to see is 100 I want to see the inside of the mile from him There shouldn’t be much so I am here I will write 100 100 miles and here I am I will write it down Miles, okay I will write it here closes the brackets properly now let’s run it is showing certain What is the error saying int object is not true subscribe can you see it here issue is coming sns2 plot we wrote ok Here is our data set from within this So here we have to take data set data Inside the set there is another data set of ours. From inside we will take miles to be miles we should go less than 100 miles around I have to bring people here so let me just do here one thing close the bracket here Let’s give it to you and then here’s a 100 and it’s miles let’s just run the code now okay Now it is clear here, so the first one was Miles inside the data set data set here And how many miles are within 100 miles? We want to see P here under 100 here If you look, you can go up to about 80. It has been saying that from zero to 80 here we are you can see it here as well if you If you look, people from 20 to 30 miles around more are going after that you will see 50 It is blank from 60 to 70 miles then from 80 to 70 miles Booking a Pay Cap here to go Now this is the way I want to look at it Now, I can even analyse that Look here, this is the area number 40 here But I feel very much that people Miles here so many miles 40 miles Many people book a camp here to go there. so here we go what do we do now If we look separately for 40 miles I will write here sns dot box You can make a plot or dis-plot here I will also tell you what is dis plot I will make it and show it to you will show with density okay so right now I what i can do is this is the same our code I wrote to him, what am I doing here? I am copying let me just control A Control A Control C Go here control and do copy paste now this is you If you see, this was for 100, what I can do is i can do this for 40 so now you If you look here, look here for 40 If you look at it you will see a graph like this Getting to know that I mean that most people go 5-10 miles there Later here is the graph of this method here This is what I see in the density plot if If I make it then I can see it in an easy way How many miles will it go or what is the density What is a plot? I can plot it once I take it so that I can give you a better look How to understand what is a dist plot This works, the density will be visible there SS dot dist plot and inside it is our The data set is inside this data set Data set And we will take it within 40 miles to show up because people go till there If you are going too far then you should have written it here Are Miles end bracket close less than 40 miles for less than 40 and let’s write here miles okay We can write the bracket close here as well after miles put the proper bracket here let’s close it okay and then after this if you run then there is a problem with parents dist dot dist plot data set was data set data start here proper here There is a bracket then miles was after this Less than that, I think of the data set here. Our column is not closed properly It has happened from here, it has not been closed here right here after 40 we People let me just write this here 40 bracket Close, now it has come to us, so what is the meaning of this dis plot, look You can see the density here right now You can easily analyze it here and tell me It is possible that in this graph of ours To travel four to five miles, people use the most You can also get a slot worth Rs 10 from here If you want to see then go to see the peak hike If you are getting it then it means from zero to 10 miles to go up to 10 miles People book the most for u and after this if we look then from 0 to 20 People book U even to travel miles but if they have to travel more than 40 miles If it happens then they don’t show that much interest if you want to go on for too long then this We have to find out the miles here which we have figured out that from row to 10 Most miles are as far as People go and do this from Ro to 20 Miles is where people prefer to go are by booking the u caps so this means The point here was that if we answer this question Answer now why did we choose this dis plot If you made this then the reason for making this dis plot is it was here look do you know the density it’s going through a curve, you can tell ho where is it and how much is it like we are from here If you see, from November to November 20, this is a very peak. in our graph this means 0 to 20 People spend the most money to travel miles that is to get around for example 10 mile 20 mile or 10 20 km if we consider but more than that if If you have to go then people here see you so much Obviously I don’t use it much Why are people using ‘R’ the most? are using it for business now If you are having a business meeting then look around The meetings are from 10 miles to 20 miles If they have to take distance then for that Who do they use, they are caps here so that’s about the other The answer which we have derived here I found out that u are the answer to the first question What was it, which category, business category For which purpose, for office and meeting purpose Then which cab is most available at what time? If you book then it is afternoon time In which month do you book the cheapest fare? Cabs are booked during winter time because There is snowfall in Paris, that is why this The month which was ours was January, November and December I use it less in a month then which week I am the most on the right side here Which is the most popular day on Friday? I use it a lot and the most on Sundays How many people book less caps till the last As for miles, from ro to a 20 miles If they have to go then they will go to R Cap. If you use it more then how do you use it the most many miles to people usually book to 0 to 20 miles so everything that we have here The question was given, we answered it properly make a graph in this way and put all the details here We have extracted the answers and this is our data This was our insight from this, here we So in this way we have taken it out here This project is completed that We extracted insights from every question I took out the visualization and used it for that question So this was our project Where data cleaning is missing Dealing with Values ​​in DateTime Formats Changing the column keeps creating new columns as and when the need arises Doing EDM visualization is quite something we People have got to learn from this project through so there was a lot in this project to learn how to do projects You have to complete the answers and get the insights I have to take it out so I hope you do it well You must have understood and if there is any other problem It seems that you did not understand some things You are definitely open for comments Please tell us by going to the comment box How did you like the project? How much did you understand? If you are able to complete the project then I will tell you about it I will wait for your feedback comments With this in mind, our work here is just to We do not end with just making a project people about this project that you Learn about it You want someone to take your project also give an overview so when you How deep should be your knowledge If you have it then you should also know how to show your work because at last our objective is Cracking interviews is a good job If you want to take it then please send your profile for that make it strong I did not understand, please tell me, we will meet Till then we will stay in some other video Data Curious and Keep Learning But Its Make sure to download the notes first There are some more practice questions in this Includes problems that you can solve yourself yes you will find its answer there Download the notes, see you next time Stay Curious and Keep Data in the Video Learning with this scale Data Science & Data on the Platform Premium courses in Analytics too are available which you can check out The Dai Scale website and mobile application But where you complete the detailed curriculum Lectures End to End Projects Live Doubt Classes Industry Recognised Certificate Off training as well as interview You will also get help for preparation the link is in the Description all right so let’s start This is today’s class where we are complete do We will take data of 10000 movies here and The special thing about this project is that Performing Exploratory Data Analysis That means ED, we learn this very well You will find it in today’s project and its Also you can work in any profile Data Scientist or Data Analyst Company even after I went in, the most The challenging thing that remains for you Data pre-processing happens i.e. Cleaning the data into the correct format arranging the data and then Extracting insights is the focus of today’s project Data cleaning pre-processing is also a It would be a great thing for us to understand What you can learn from this project Besides visualization many times we Reports have to be prepared and submitted to the company There are several bar graphs for that inside the charts in If all things are needed then this one But at the same time, this project that we will complete it in pythonanywhere.com I am going to explain in very easy language So whether you are a beginner or an intermediate level Or it may be in advanced level or any level You are in this project. Please include it in your CV. It will definitely add some weightage to your resume and this project is very basic and simple I will explain you in easy language But to start this project First you will find more on this channel of Dai Skill You will get projects like e-commerce There is a complete project of u data of the analysis This project of late is in which the machine Learning and NLP has been used in i Sales Analysis I am live youtube2 You will sit for three-four hours and complete a You will get up after making the project if it Maximum people comment yes live If you comment on the class then you will get my You will definitely find a youtube video from that On this we will complete the whole project If we complete it, then as many people as I am interested I am expecting that even if 500 students go and say yes in the comments If you write then I really like such live classes is to take such live classes where pay & sat end to end and in three to four hours A great project, we finished it Right so please tell me in the comments those who Would you be interested? Please write yes and tell me like More than 500 people commented yes in this We will definitely arrange such a class where I will give you a complete I will get the project done right so let’s start For now let’s do this project of ours Is In total three rounds are conducted Right, now you have come here to learn a skill. The most important thing for you is You should know that when you are in the company If you go for the actual interview then how many There will be rounds and what will happen in that round So the round which is number one is mostly What do companies do even if you share your data? Analyst Data Scientist Business Analyst Go for any position in most companies What this does in round one is that you get a An assignment or a project that’s mailed to you It will be sent to you within 24 to 48 hours time is given if you complete it I will send you your first round If your assignment is completed If you are shortlisted then you will have to appear for technical The call comes for the round and then after that Round three is your HR round, so this The assignment we get in round one Not in the same way that I am today I am going to get the project done for you so that your First of all let there be a practice right what the company does in the assignment such as Will give you some documentation first Company history will be provided That It has two co-founders as you can see Reed Hastings is looking at the screen And Mark, these two are basically its co-founders is when it started what happened to netflix’s once that its The founders have experienced that if people are late the DVD They charge extra for returning the money starts becoming so then they thought that Why don’t we send this DVD through mail? If we start delivering to people’s homes Whoever wanted the DVD could go What did you do? The photo you are seeing is from 2007 This is the photo of when the website was created It is available in more than including India right now if we talk netflix.in is this it is profitable with this Its profit now is $2.4 billion So this has become a very big company. 283 Its paid membership is in millions It has 283 million subscribers and It is available for more than 190 rupees and in netflix’s which you can also see on the map here are in every corner of the world What kind of things do you like and according to that If it starts showing us the result here then In the world That we understand customer behavior and patterns now you have been given this situation In the assignment that let’s assume you have data Working within this company in a driven role You have a data set of 9000 Now you can find 5 of 10000 movies here questions are given, from that data set you will get this You have to solve five questions and then answer them First, I’ll tell you this data set I will show you what question is solved after that If you want to discuss this then please do it here The data set you are looking at is a heavy We have data from around 9 to 10000 there are movies here so if we If people watch then around 9000 to 10000 movies this something data is given to us now Which movie was released on which date? what was the name of that movie what was the overview How popular was that movie and how many votes did it get? The count was found of how many people gave their average vote Which genre was that movie and URL link of the movie poster I have given every if related to the movie Formation is given like for example any one movie is spiderman right so this is spiderman Which genre is it and how popular is it? People came to see this whole information is available to you in this given in the data now you can contact the company which what are you expecting If you have to solve the question then first of all You have to tell which genre of this The movie which is he has to tell us which seconds are these There are genres that most people have Liked the one with the most votes It can either be a comedy or an action thriller C is such a genre that people like it We have to tell you the third question, which one? This is a movie which is most popular What is Milli and its genera? Fourth question which is that movie which has the least Popular Milli and what is her generation Fifth The question is which is that movie which In which year were the most films made? most released so until this information By giving you the answer to the questions If we have to give the right then now we will answer this question We will solve it with the help of Python, so let us People directly open their Jupiter How to Notebook Jupyter Notebook Anaconda If you don’t know how to install it Link to it in the description box below I have placed it so that you can easily If you can install it then let’s go ahead and Let’s start this project alright So before starting the project there is one more important thing like this right now There are five questions in the project, we will solve it but apart from that The libraries that are in interviews Python like measured panda mat plot lip c Bun and Plotly so related to this 25 I have prepared a set of practice questions for you. has been prepared so that there You can go and practice more questions So where are these practice questions now? You will get The Eye Scale for this You have to go to the website and open it as soon as You will see the website here, after that You have the option to explore courses here it will be seen that we will go simply and whatever is ours There is an option to explore courses by going to this We will click here for the free category We will select the course which is this In this particular website, The Eye Skill On the platform, you will get free data science and Data analyst courses are also available From where you can start your studies Where you will find more video lectures like Python Machine Learning SQL Excel and more You will get all these things like projects etc. Will go with video lectures along with notes and also with the code files now like You can get this free data science course here I will go and click, after this you will see above The option to login and register will appear if you If you have visited the website before, you You can log in directly or if you If you are coming for the first time then you can simply come here What can I do? I can register. So whatever your mobile number is, you can enter it here You will enter the password that you have created yes they will put it after that you will do it successfully Like you will be logged in here in this way You will be able to see an interface dashboard. Here you will get the option to explore courses And here you will also get the option to enroll a course So we are already here in this free course Enrolled so we are here to Enroll Courses I will go and read the free course which is here We will go to the free course as soon as we After this, we will select the first one which this is yours right here you’ll find python By doing practice questions, you can Simply solve this Python practice questions You will click and then you will find it here will go to python’s practice questions and This arrow is visible here, from here you can You can download this then simply Here you have to click on this arrow, this is your It will be downloaded after downloading Here you will get a complete answer of 25 questions If you get the set then I am hospital If it is full then you must download it. Take it and you will find the link below find it in description box So first of all we will look at our system we will go and the anaconda navigator which is you will open it here if you open it If we do something like this then we will The interface will be visible and here we will The option for Jupyter Notebook Latch comes here You will see a launch button at the bottom like You will then go to this launch button and click on it Whichever browser you use, mola firefox.exe In the decryption box I once again Let me tell you that the notes and the data set It is available in the description box below Check it there, I have already done it for you. If it is kept put then as soon as You will go to the description box to download The option will come and you will find it there So now we had this CSP file here What have we done with our data here? I have uploaded it on my site, now we have people here You will need a new Jupyter Notebook of your own to make it we will go to new here python3 iva null we will click on it After that this Jupyter notebook is ours It has been opened now you can see it here You may see untitled in this, whatever you I want to keep a name I want to keep a name hoon movie data analysis In this, the data has to be cleaned first There will be a lot of steps so we Here we will import some libraries. Pi Libraries for Mathematical Operations Pandas library for cleaning data We have a lot of graphs here too. If you make it then we will use it here I will do Matte Plot Lip Library and Also we will use it here seaborn library right so this What should libraries do first? If you have to import then first of all Whatever work we have to do here, we will do it here If you want to import libraries then first First, we will import the libraries here. We will start importing Nam Pay Edge NP NP is its alias then in the same manner import panda as pd than import mat plot lip dot pi plot s p lt her Later we will import sea bun also Import se ban as SNS then this is as much as The first work was done on essential libraries Now we have the right to import this Here our next step is which We have to perform as much as we can with the data We have ma m dbcp2 CSV This is our CSV file, its The name is my movie so if you write my If you look at the keyboard, it is above the caps lock The tab button appears as soon as you press it If you do it, it will automatically show you the name of the file now what do I want here We will use the line terminator for this Meaning one row should come and then the next row should come second row then third row in the next line In this way we can understand systematic data If you want it here then we will write the line here we’ll use the terminator so that we have We provide data here in a clean and nice way If people see it, I will write it here am the line terminator so that after a row The line for the next row changes to equals two So for the next row we have to change the line then the method to change the line in the next row is If we want to go to a new row then we write so that we can go to the next new row So we have written this here, now this data the set has arrived now in this data we Which are the first five days? Five data If you want to see it then we have written here The number of columns you have is given here The first column is the release date On which date is the movie released? After that what is the title of that movie overview what was in that movie I have given a brief overview of that movie How popular was that movie and how many votes did it get? How many average votes did that movie get? Count and the original of the movie that was made what was the language, it is given which It was a Janar movie and the poster of that movie its url link is given so now If you look here, this is the first movie. like it is shown here It was released on 15 December 202021 spiderman nove home ye movie ka jo The overview is written here How much popularity did he get after that, how many votes did he get Count Mille the actual one who made this movie His original language was English and this Which genre was the movie based on, action adventure Science fiction and the poster of this movie If the URL is given then this is the complete Now, we have the data that is given to us. If we want to understand that every Here we have a column given below What data type was inside it? There is no missing value in the data All these things are not null values, now we People want to check it here. Check it We already have a function to do this which we call ff.in now d.in As soon as you apply it here you will You will get the basic information first The information you are getting is that this How many data sets do you have in total? There are movies 9827 movies are given here now you can watch look at every like column then release What is the data type of the date here? Data is of object type i.e. 2021 12 The month means December and the date is 22nd The data that we have is this date We have its data type is object That means if the data is of string type then The first thing we need to understand is that we need to change its format in which place should we use this instead of string will have to change because there is a date so we This will change to proper date-time format The first thing I saw here was that Now let’s see another one, we’ll have to do this thing The title here is like a movie, the kings man so its title is definitely Data must be of string type right in the overview also here the string then this is also object type data After this, this is numeric, that is Floating point data is followed by integer If the data is floating point data then this The language is English, it is obvious if it is string data then this is also correct Object data is now as many data as is inside it These are also genres, object types here there is data of and after this this is the URL of The link is given, this is also object type data then all other things are ok but one The first thing we need to do is Release date is its data type we will have to change this instead of the object Converting people to date-time format The first task now is to find out if there is any If there is any missing value then we can see here You can find that all the records are here Any value is available missing here There is no null means there is no null in this data We don’t have the right to value here This is clean data, there are no null values Whatever null values ​​we have now here We do not have it so whatever operations are going on in future We will continue to clean this data further To simplify, let us focus on that If it will happen right then till here we will tell you this People have become clear now see how much they used to be before This is our question, solve this question there are a lot of questions on the genre of acting so we If you look at the genre of movies here, there are many There are different types of genres such as Spiderman movie its genre is action adventure science fiction but here i am If I watch another movie, Kings Man then this The genre is action adventure thriller One To Aise I have given different genres here If you look at the first five generations here Which genres do I want in this movie? so here I will write df our data The name of the frame is our column inside it Its name is genre and within this genre I will put my head here, that is, my If you only want to see the first five you can click here You can see different types of genres given It is an animation action comedy family fantasy thriller if i get my head out of here I am here, no matter how many people are there here You will get to see all these things in different ways genres but i am just getting started I only wanted to see five so I posted here It is written head right so this is generas now genre One thing that I need to understand is this Look, the first genre is written action, some attention Give me the right here, what I am telling you yes this is a little important matter, this data pre The processing that you will learn now is very This is an important step for even the company Go inside to work, this thing will help you You have to watch this step very carefully Look right, what is this? Action is written here. Just after one birth what did you feel comma then there is a space given here After this I have written the second generation in a different colour I have made it, look what action is going on again comma then after that look here is a white The space that I am showing in blue is a There is white space and then it is written second here genre adventure then comma then watch There is space and then there is science fiction in everything this is the same thing look here it is an animation put a comma then the next one is a comedy before that If there is space then always look here to see space If you get it for me then I will understand this It is being said that this genre column is in this A genre is written, there is a comma and then this white If there is space then it is possible that in the future we will How can I remove this white space also? This data pre-processing will be removed Whatever happens I will teach you to move ahead right There are two works so far, I have seen the first one The work needs to be brought in date-time format We need the release date and this other thing too It is possible later if the requirement arises So whatever white space we have We may have to delete this right twice I understand this even now, this is for us too It is a must watch movie, let’s suppose take spiderman a column okay is this again Is Spiderman being repeated again? Is there any movie or data duplicate? There is a lot to check here It is important that if any data is repeated somewhere If it is not there or it is not a duplicate then now Let’s check if any value To check if it is a duplicate or not function with da dot duplicated name for It happens that we will apply it and put a dot sum whether there are any duplicate values ​​or not here comes zero if i don’t apply even So let’s see what happens if I don’t apply any balance look falls falls falls come This means that any value if Not a duplicate means false if the value If it was duplicated you would have seen it here But this data does not have any value It is not a duplicate, that means every movie which is here There are 9827 movies here and every movie that is What is that, any movie is unique it is not a duplicate so as soon as we put the dot If we apply the sum then the value here will be zero This means any movie here is duplicate now what is the next thing to do so next The work is to do that whatever data we have It is given first of all the basic Perform statistics and see why look because as you look here you vote If there is a count then what was the maximum vote received? What was the minimum vote and what was the average vote? Right so what was the maximum and the minimum is now here If it is popular then how much was its maximum popularity What was the minimum and what was the average popularity? 25 How popular was it at 75? How popular was it? If we have to take all the things to the basics if so then we have describe name for it function is given by but it can be Statistics in the data which is numerical now you For example, suppose here is the title of the movie Spiderman, here are some statistics Statistics will not be applied with numbers If it seems so then we have a description for it There is a function named, by using this one Here we see the basics that what things are given here so here I will write df9 27 is given here because The total number of records you have is 9827 total count right now let me talk If we talk about popularity then average popularity of any movie Is 4.32 minimum popular is 13 and maximum It is popular 5083 This is how the movie got vote count I have received the average vote, I have received the vote count 1392 Minimum 0 votes received and maximum Joe is he is 3177 This is how you have to see the average of the votes So here is the minimum, if you look at it I have got zero votes and you will see maximum So if we get 10 then it is an average on an average. We understand basic statistics here Now whatever points we have earned till here comes We have seen all those points in one place I would write down here and keep my jaw down. I have all these points so far What things did I get to know? The first thing I did right now was What we have to do is this, this release The second thing is to change the format of the date It is possible that in the future, whatever genre it was There was a white space in between, it had to be removed Now I can’t see it here, it’s so big data is given but in this data we Will we keep only those things which are useful to us? He is of no use, so I will remove him from here what is not useful here then here By title we mean overview We have nothing to do with that movie We have no objective yet after knowing about it If not, this column will be of no use to us The language of the movie is in English Made in Hindi Made in any other language We have nothing to do with that, why? No, because the question that was asked of us There is no question of language in it Otherwise, remove the question that is not there. Because you will only ever get so much data Even if you go inside the company, there will be a lot of You will get the data in bulk, it is up to you The company should only provide you with what it is asking of you. keep the data to yourself because you are If you move forward with all the data then you will make mistakes You will not get accurate results what will we do with this data It is a useful thing for us, we will keep it if we keep eliminating him then what is the use I understood two things that these two columns has nothing to do with it eliminate it another one here is poster url now movie what is the poster of it, we have nothing to do with it we don’t have to give it because the interview I have asked a question, there is no mention of it in it There is no poster URL or language So what do we say about these three columns of the overview? We can do these three columns So can I simply delete it here? We will remove these three columns will bring the release date in the correct format and There may be white space in the genre in the future Who has to remove this work, whose work do we have to do data processing inside pre processing Properly arranged data inside We have to arrange it from here Right, so all these tasks that I told you about Let’s note down all these tasks in one place ma’am, make a summary and note it down what is needed it is needed because In the future this file could be used by someone else Look at the recruit or any other person See, he should understand your work. Cleanliness should be visible, everybody should coat It takes but where should I give the comment? You have to write the summary in the correct way so that even if any recruiter is looking for you If he opens the code file and looks at it then he will find that yes it can be done in proper and systematic way It is not just an interview that comes from By going and telling that my strong The points are that I am very organized or I like very clean work, so to speak No one will believe you, you should know how to show it So we write down all the summaries on the second page This summary is also important because today You may be working on this project If you open this file again after 202 days then If you don’t get confused yourself, then the record will be there Then you will understand it automatically, so let’s do one thing Whatever work I have to do till now, I have to do it If you note down the place then the basic These are the summary points, we have jotted down in one place we have written that we have a data Frame having 9827 roses and nine columns That is done. In our data set, any There are no duplicate or non-existent values ​​now But the work that needs to be done is this release date This is a column, we need it in date time format I have to bring three things and remove them Overview Original language poster URL: All three Columns need to be dropped and then vote We want to make average a better category people and all this white space I want to remove it right so we have to simply Right down here, whatever things we find here Now we have to do the next step which is our first task They will do the same thing, I will do D.Ed once Let’s take a look at the data before writing the head This is the release date and the question I have what is that related to the date given to The question is given that in which year the most More movies have been filmed and released in date time but even in this i don’t what does ko just mean to me just ear it means 2021 22 21 it means so I have nothing to do with its month So I have nothing to do with its date Who has anything to do with the year, we what will you do in this release date only year If we don’t need the rest of the things then we will keep it We will remove that also then how will you clean it How will you pre-process the data Let us begin to understand this now So the first thing that we need to do here df is the name of our data frame Inside this our column name is Release Date So we write the release date here and After this we have to change its type date In time format, here we have PD i.e. that we will use the pandas library and this will change to date in time format So I wrote here date time df And the name of our column inside this is Write to us here If you have to, then the name of the column is Release so let me just write this here release date right this is for us here You have to use pay and then the bracket Simply close it and then click here will write print DF After this we will be released here date bracket close and now its data type which is D type, we will write data here Let’s type check if it has changed or not If you run the change then the earlier data type was what we had of object type Tha Here We Can See release date which was delayed the type was that of the object type now this what did we change it into we changed it Look at the date-time format now Data type: This is what we have printed DF release and its data type i.e. D I checked the data type by writing the types, so now It is in date time format But now what are we to do here? just want to keep year 2021 22 like this We don’t need any month so now we will do it If you convert then write for it will have to do d a our column name is release date right and inside it equals to da i type the whole thing in a single code first I will take it after that I will give you one by one I will explain it by doing this so you don’t need to green about what is happening right i I will explain this entire project to you line by line. you will understand that that is my hope for you right So I write it first and then after I will explain every line that is released date and dot d types what is written I’ll explain it to you a little bit Look, first of all we have released this Date is our column, what do we do inside it If you want only year then click here that’s why i wrote release date dt year now Whatever we will get, we will get only year We have the right and if I have the data type If you want to check then now there is only year i.e. If 2021, 2022, 2024 are this type of years, then this What type of data will it be now? We have data of integer type so now i am going to put this code here I am going for a run and it is showing certain error what is the error ok error it is given here here we have a comma double inverted comma which we put single inverted comma He did not close it, now look here what is the data type integer now i let me go here now I’ll type df2 and Let’s see whether what we wanted happened or not otherwise it would have been done by writing DED head We wanted to know how it was coming here earlier Look, it was coming with the full date written 1512 2021 But now, after what we’ve done, If it is coming with just the year written then this year is coming 2021, 22, 21 are now coming according to the year Now that this year is written, its data what is the type integer type data Once it was typed, this was the work we had to do We have done a lot of work on this with the release date well done right now what is the next step to do so this is the first Done and dusted with a working release date now this The overview column will be removed from the original We will remove the language column and the poster We will remove the column containing the URL, so let’s go right Let’s go ahead and perform this operation. it’s alright so let’s proceed with the Next step: Here we first write Dropping the columns like headings So we have already marked those columns which are not useful decided now we have to drop that columns right so I’m gonna mark this down Now let me write down our seconds here Step 1: Which columns do we want to delete? If you want then I can name the columns here I am making a list in which I will get everything I don’t need any columns, I will name those columns If I keep posting then this overview column I don’t need it so I’m here Removing the column with pay overview I want to so I am writing the name of the Columns which I don’t want in that data set On the right, first select the overview column We don’t need it so we are here for the overview Let us give you which column is not required We want the one in original language There is a column, we don’t need that either, so I think The name of this column is Original Language Write So here we write O must be in The Capital Original Original Underscore Language In right language the L is in capital this is the original language we don’t know this one should inside column is poster url right this We also don’t need it so I am Writing Here Poster End underscore u a l so this is as many columns as We don’t need to make this a list Now we are giving lakhs of rupees here, all these If we want to delete the columns then click on delete button Our data frame is named df After this we will use the drop function here. We will use and whatever columns I have written here Those who have already been kept in this list I have to remove that, what am I doing here I am passing so I will write here I am calling the number of columns I need to delete There is already a list of comma columns inside it one has access to the forest and it is as many There are columns here in all three columns I want this data set of mine permanently get out of here i have nothing to do with it If not, I will write it here in place Equal to 2 right now after this DED here columns, let’s run here now Look what the columns are showing us here We did not need these three Columns not wanted Overview Poster URL The original language is now removed but But once we get our data back Let’s see and see if it is really removed or not Otherwise I wrote back here D.Ed Head Now you can see that these three There were columns, they have been removed, now only release Date Title Popular Vote Count Vote Average and Only Janras is coming, rest these three are columns We took away those things which were of no use to us what did you do successfully, eliminated it Now here’s another thing for us It is very important to calculate the vote average Look, many times we have such scenarios that we are given a criteria Like for example when you were in school When we study then this used to happen that also from 90 to 90 marks Whoever comes will get A grade, whoever is above 80 I will bring it up to 90 and give it B grade Whatever you get, let’s assume it will bring 70 to 80 He will get C, such was the criteria There used to be such a grading system in school In the same manner as I vote here I will talk to you if there is any such movie who got more than seven average votes If yes then it might be a popular movie There is a movie which has very low rating If we have got it then we can tell him that the movie What it is is an average movie, it could be any movie it is below average and there could be any other movie which What if it has not become popular at all? Whatever numbers are there, we will do it instead of numbers The label will give you the right if a movie scores 8.3 This means this is a popular movie, 88.1 I mean it is a popular movie but if a movie It is not popular, like say 6.3 right so we can tag it as below It is an average movie so here we are going to tell you a We will make a criteria and according to that The numbers here will be replaced with because seeing the number 8.3 someone might I don’t understand, things get tough But if it is written here Spiderman If it is a popular movie then you can immediately understand the word popular If it comes in then here we have the criteria create and label this vote average From which the numbers will be removed and we will use labels in their place We will add a total of four labels Which four labels will be the first popular ones? Movie’s below average Movie’s below average of movies and then of not popular movies We will put this total which will have four labels We will give here this weight average column So how to do this work inside, let’s see this let’s see alright so first of all we here Let’s write down what we need to do Here I will select mark down and write I am saying that we have to category is the average Vote column right and for this we have four labels as we decided that we are going to Make four labels popular average below average And it’s not popular to do this job anymore What we will do first is create a function Now we will create category with column name what will happen in this function then I will be the first one I am writing every line of code here and after that I will tell you definitely each and I will explain every word here So here we create a user defined function whose name is category is column Right, in which category should we place this? If you want to keep it then we will name it here keeping category column name ok now This is a user defined function because we Here you can implement this function yourself Now what we are doing is this category is column name Creating user defined functions from this I need to put the data set inside so I Another variable named create here now what will happen in this age Here ultimately this is the complete data set We have no minimum value in it there will be some value which will be maximum right and 25 will come in between 50 and 75 of this Accordingly we will give labels that which is most The maximum he will get is the popular label You will get it then let’s suppose if he is at 50 Like on and around 50 to 75 then He will get the title of average if he scores on 50 And if it is around 25 then pay his average bill You will get the title of average and if you score more than 25 If it lies between the minimum then it should not be I will get the label of popular right so that’s why I Here I am creating a variable named edges now inside this da is our data frame Inside this we will pass the column dot We will put the describe function here and Describe what we need inside it Firstly, I am making it for the minimum First, we are making the minimum here so i am writing here minimum ok next For whom will we make DF here? In the same manner we will write DF call here. and now we will create dot describe and Why should we use describe function is 25 for then I have to write here 25 But this is a very new thing so far We did this work in projects It is not implemented that any text We give the data to some number, data to people what to do to convert to labels so this is a very new thing which you guys are watching right now Wright is learning inside the project In the same manner we will write DA here column then dot describe function For dot describe then after 50 per here we will write it so we have to right it here 50 but okay and then we have to close the bracket same manner same thing as here It is written, you should type it here I’m just simply coping with this thing so that I want to have to write this again and again and it will save my time so thatchi guys are practicing for the first time then you must have to write those things Right because the more you write and code the more you will the better things work in your mind I’ll fit in right so here we have to close the bracket okay so this is us guys what did our name Ages do here This was a user defined function, we called it made Now after this we have a variable named edges what do we have to do now after making this df is data frame inside it we have Out of the number of columns, which column If you want to do categorization then I am writing here df1 dot and here the cut function this cut What does a function do in categorization It helps us to define what we want to label no it helps to give it a label Because what was there before, all these numbers before Now if we want to label the numbers then its So this is the function we are writing here If this cut helps in categorization then Here we simply tell you what’s next This is the step, we will proceed here so here we have to write pd dot cut after that We will put the data frame and in the data frame Inside our column, we will We have kept it fixed in ages, right, at 25, 50 Now in this we will give the labels right so now Here I wrote about ages and ages Accordingly we will provide labels here equals to labels comma right here now The labels which we have made separately for this We will pay here and after that no one can if it has duplicate values ​​or This is not the case for us if any If the value is duplicate, it should be dropped We don’t want any duplicate values If I survive inside this then what am I doing here I am dropping the duplicates and After that what will you do to return DA Now look, this is just a function that has been created We have created a separate user defined function so that when we need this function We will call this function whenever needed. So what did I do differently here? The user has created a user defined function def keyword to create a defined function What is the name of this function used for? We have named this category as this Now inside this, in the argument, we have placed Three things pass df means our data frame Columns Whatever column we use this for According to that we would like to label it can it be possible let’s suppose further Let me label some other column So what did I do when I needed it I have created the function whenever needed I will use it when I need it, so right now I just placed the calls here right now we Next, we will specify the name of the column What is the name of the column you will insert, vote average and The labels that we have to give, we also give them separately You will create it but on which category you will label it If you want to give, we have made it for you If we have minimum age then give it to him should the label let’s suppose it’s not popular if that If it is between 25 and 50 then it means below Average if it is at 50 and around 75 Let’s assume that the average and maximum are Popular in this way let’s suppose we I have thought about it, right? Now after this, what we have done Write this line of code to create this data frame Whatever your column will be in cut function It will help in categorization and this we made the ages, we passed it and These are the labels that we will use in the future Look, we have not written it anywhere in our code We have not written anywhere that average popular is ahead I will go and write it now and if there is any duplicate if it is there then we have to drop it, after that we what will people do, they will return this right to the data frame so now we are here Now what label do we want to give here We will start making pay labels, the first label let’s suppose i am writing here not underscore popular this is my first label Right I am creating a second label called Let’s Suppose below average that is my second label third Here we are making the label of average We are making the fourth label of Popular Right, so we created this label here. Now if you guys order this label If you see, the orders here are completely systematic Look, we have kept it, it was minimum so it was not popular Then there was a round of 25 to 50, so below average and if it is from 50 to 75 then average and If it is maximum then popular right we have said this Accordingly, the label has been created here Now what to do is now this function that we have Here we have created the category is column This function by name, the number of steps it had You are seeing that block, right? In this, only We have created the function now I Now I want to call this function I want to use it whenever I If I need to use it then I will call this function just like we name a person after him If you want to call someone, then call his name call me right sambalpuri call me swati Right in the same manner we used this function It is named category is column by taking its name Now we’re going to call this so let’s name it Here we mention the function The name of our category is Category is under Score column now inside it look at the data frame here now when any If you call the function then here you Three arguments have been given now these three You have to pass your arguments here If you read it, the first argument will be our DA Let’s pass this one now, just here Earlier, when we created the function, we only I had left the call in writing but now which one? If you want to function in column then vote If you want to do it in average then now I have to Here you will have to give the name of the column and what is the name of the column vote average right so i have to write it here vote underscore Average is okay, now we have given it a name now what to do next what to do next Labels These are the labels we created here this one this one label right this one Create labels and pass these labels here If you are doing it then I will simply write it here I will give you the labels, write the labels here Ok so this is it, calling the function now Here when you call this function I am writing DF and then this vote This is our column with the average. Now I’m going to put I want to check if this thing has happened or not it hasn’t happened right so what do we have to do Vote average and how many unique votes are there in it These are the values, let us look at them now So here I have written unique now let’s just run this code it is showing certain error let’s say category is column is not defined So what did we do wrong here? Look here the spelling of category is different and when we are calling then the category is the spelling of is different here it is e and here it is s Pay has come because what is it giving us Because of this it is giving us error here let’s do one thing i will go and get it I will do it let’s suppose right now here I will run this but it is showing some error lets say Just check the minimum amount and tell me here it seems there is an error ok so ok look here It’s an error, it’s such a small mistake, it’s minimal it is written the bracket is closed after that we What are singles closing here inverted commas so this is our mistake Let’s correct this mistake brother, remove it Now let’s try running it, now anyone There was no error, now the proper result has come Now you will see that there are unique values ​​here. What has become popular below average Average and not popular now this is what I call I n the data frame we have the change I am writing to see if it is true or not. dead head run and here we are Getting the results we wanted to vote for Look at the average, what came first We’re getting the numbers here you can see Right first here the number comes in the vote average was 8.3 8.1 6.3 now we have changed this thing and here we are getting the labels Right, this was a very important aspect Anytime you do data cleaning before We are currently processing So whenever you do such pre-processing then In this way a numerical data is given labels. converting into this was a new thing Data Prep for Learning in Project inside the processing step so I hope that If it is clear to you then inside We did the important work that we had to do I have done it now I want to see that brother How many movies got the tag of popular? There is so much of a movie which is considered average How many movies have got the tag? It has got the tag of average and how many such movies is the one which has got the tag of not popular right So let’s check how many movies are there In this we will write it to check it df what is the name of our column what is the name of the column name is vote underscore average right vote Underscore Average now includes value counts If we apply it then whatever we If you want to do something, you can do it here Two value counts now look you have it Not popular among the total movies There are movies There are 2467 popular movies 2450 is the average 2412 and below average is 2398 so this is us People now know a rough estimate If you look here then this will be your Right now those movies which are not even popular There’s no use in showing it to people Why right because there is no movie The movies are of no use to him There is no use of showing in recommendations the movie which is not running at all and people are watching it Why are they not liking it and recommending it? will do you right so in the same manner to me now It was found out that the not popular movies which it is not that special but our data Most of the movies are of this type which It is not popular right now what should we do As many likes as people get here, if we have any of these now There are duplicate values ​​or any non-values Is it necessary to remove it in any particular row If so, we will write about it here. Ded drop A and after that we people I have to remove it permanently so what am I to have right in place equals to true Because I don’t want these things forever I just want to have this data remove this permanently now after this you You have to confirm whether it is removed or not then click on dot we will write is a and put a dot some So now you will know, now you will know from here that all the non-values ​​etc. that were there in it All that has become 0g here, that means now this It has been removed properly, right, this was it Basically this is an important step for us Click here to understand the process I hope that I am able to understand the project if you can understand this till here If you have completed it then please tell me in the comment Write and tell me that yes ours is on 50 The project is complete, now move forward We will grow and complete the project at 50 If you do it then those who have been able to reach here at least just let me know in the comments that yes mam ours is at 50 so that I know How many people walked halfway The project is also made minimally, let’s go right Let’s move ahead and proceed to the next step okay let’s do it our work is done now I will call the DED head once again and here now what was in the beginning I had said that whatever genre our column is there is a genre in it then a comma then in the middle There is white space right so what do we do now We will remove this white space and I want that across all the genres They are not written together, they are written separately Like action in one line then in the next line Adventure then science fiction next in line So, whatever these genres are, I want to break them I want like three generas one line here I have written what I want it to be Different, first comes the action, then comes the adventure Then science fiction came here in the same manner But first comes crime, then mystery If a thriller comes then whatever thing is written in one line We have multiples written here General, we want those different lines I write it and white space from here If it gets removed right then what should I do for it now For this we will have to do what Applications which are yet to be implemented We have to do this now, we are going to learn it So firstly I will put this in heading format I will write it down in mark down that V Wood split the genres into a list now here But we will treat this generosity like a list wanting and then after that we will do our will explore this data frame so that If a movie has three genres then it They came in different writings, like Spiderman First genre is action then spiderman is next in line Adventure in me then Spiderman next in line Science fiction i.e. Spiderman Spiderman Spiderman That Will Be Common Action will be written in front of it, then adventure And then science fiction so this work we People have to do this now, how to do it We will proceed further, let’s take this Basically we start watching now so The first thing that we notice here We have to do that for our data frame what do we have inside called genre The name of the column is So I’m Writing Here genre ok now in this genre column df And here we will write the genre again because The operation has to be performed in this genre Now we will dot ATR dot split here First of all we will know how to do splits People had seen it after every genre comma then white space so we can use comma and what is this regarding white space If we split then I am writing I pressed comma once again so that a A space is created and then an inverted comma I am closing it and then it’s done right so this I basically did it here now df2 df-41 Earlier this index was zero Index and in this index three things were written but now if you separate them then they will be in rows Action One will come, Adventure Two will come science fiction will come in so right now what we have We have data and it will become more data Right now the data we have is about 9000 a like let’s just see we up here was fired on 9827 data was right, so now as we We will break this genre into different things If it comes in line then the data will increase further It will come to us so I will write it here I am doing reset index reset index Setting Drop Equals to True after writing we have to because we want to do it permanently and call df dot head Alright, let’s run it and see And finally we are getting the result which we Wanted to see Spiderman here now? Nove Home This was the first movie with three things in it was an action adventure science fiction first one was coming with writing as we saw above It was both action, adventure and science fiction Now this is what it is, it’s split And different things are coming right now one more task What we have to do is that we have a column with the generator What do we have to do with this column? If you want to cast in category then here I am I will write it once in the comments Casting What column into Category right now for this to do this work df We have a column which is the genre column, this We are trying to make it right in the category genre equals two df3 right allright next here df now this The honour is this, we will write it here let’s take it and what is its data type now If you check this out, you are here now You will see that the one who was here earlier is now as many as We also have genres, they are already here They are together in a category and look here Our category data type is object Typewrite so we have already done this work now if you go and see first then only There were 9827 records, now we have split it I did it, now it has increased, so look now How much data do we have now? 9827 Earlier we had number of roses, now we have it passed 2552 right why because what we did The column containing the generator is written first in one line broke it apart now when you If you break it then definitely the data will increase from 9827 it is now done 2551 data you have ok now pay here The more unique data we have, the more If we see this in other columns also If you want I will write it here df1 yike right n unique let me call this so here we are getting the results that our As many likes as you have particular columns Release date is 100% unique data popular vote count general right so we have the total How many unique generas are there? There are 19 left The release date of the movie is repeated 100 is the right title which is 9415 in this way We have seen these things here What a unique value it is, now finally we move ahead Work on data visualization will increase We will answer the questions that have been asked to us in this One by one during the project you will If you solve the question, the more data you get We had to do pre-processing, we did it Now our data is absolutely accurate It has happened once, I even called and showed it to you I will give you the D.Ed. head now in the correct way Our data is organized, now we can use it All set to take out the insides We have prepared our data very well. Now there is no missing value in it there is no error or anything in this There is no problem here now, it is completely ready The data is done, now what to do There are five questions given to solve in this interview assignment Only four or five come in strict assignment I solved the question and sent it but its They don’t show that by making a visualization is the biggest mistake you ever make if someone If he is asking you to extract insight then he Please show me the visualization so that I can understand to the person in front that yes brother you If you know how to do visualization then first of all Setting up i for visualization I want style so I am aa d style I am writing SNS, what does SNS mean? means your c bon library so here we are The white grid has a very stylish look let’s set it up so that our The visualization should be made properly, now the first thing what was the question asked what is the most Frequency genre is this I will put it in heading format Now let me write it down by selecting mark down Which is that genre? So here now we have I have separated the genres and now I am asking on which frequency whose movies has been released netflix’s 2552 means total count of data is 19 unique generals which are the most in the genre in which movie has come that is the drama right and its what is the frequency 3715 now we know this I got the basic idea from here but now I wish that if I write this much If I give it to someone then no one will understand I want this first honor if it is the most There is the highest drama, then what comes after that I want to show this by making a graph So how will the graph be made using Seaborn library? we will use it here now i am here I will write SNS dot cat plot we are here We will make a CAT plot i.e. category estimation can show what we have and what we have What kind of graph do I want to create? Access will be created I will show you how to make it rough first so that you can understand that I want All the people should come here as if they were here Drama action is right for all these genres be written here and according to that Such a graph keeps getting smaller and bigger So you will know that this is the first genre and This is the minimum so you want a graph like this So what I want in the y axis is I’m writing in Axis genre comma data data in giving us people will have to do da comma kind equals to what kind of you want to keep count you want to keep count Means if you want to show the number then suppose Drama is our most category which is drama is the best genre His count will show that 3715 were deducted from his account. The count is right, we have written it here. Comma order comma order equals ToD A in which order should the genre be shown The value counts right whichever is largest comes first and then the smaller one comes Value Counts and Then After What We Setting up the index is a must will have a dot Index value counts then bracket close Then dot index right this is now color too If you want to give something of this then you Can’t mention the color as well so color equals to We can put the color code here so one if I want blue colour let’s suppose then its colour code is 428 7f f I will put the code here and show it to you I am getting the color right, this is our job Now we have to show it something about the graph There should be a title as well so the plot has turned into a twist We would name the title graph as title Are Genre column distribution ok now let’s show this If this graph comes out then for this I will have to write p lt dot show so I am writing here p lt dot show let’s just run this ok it is showing certain error error here because one side is double inverted comma and one side single inverted comma let’s fix this again it is showing certain error ok let’s just check what is the error it is y equal to genre here is speaking order equals two well look here here There is a mistake in writing the genre here but Close here with a single inverted comma I have not done this let’s do that and this is the graph v R getting so now we understand that as much as Now we are seeing people here differently of which we see the most in which Movies are based here, that is drama and the lowest one is western and we have the documentary right Just by looking at a graph you will know immediately Think from this point of view that Assume someone is working inside the company is he in your senior position or someone else He is from the department and he wants to see this From the creative department let’s suppose that Which genre of movie do you want to watch? The one who has been praised the most is us Here you can see that the drama is the most It has gone and the least that is there is Western Right, so we got to know about this general here. Through this our first question is solved happens to be asking in the first question Which genre has the most movies released? has happened netflix’s proceed for the next question so here the second question is which has highest votes in the vote average column right so here now we has to solve this second number question we have to properly apply hash to it and then back it down if we convert it into and write it then see now Here I will tell you once again that D.Ed. Head I am showing you this by writing and here we are seeing That’s our vote average column Of which what we have done is categorized is it has been labeled as popular not In the popular average it is saying that who The genres which are most popular are The highest average is below average now we have to show this so for this also We will make a graph and here our There will be labels, we have four labels on average popular below average and not popular then this We have to make a graph and show it here right so what we’re going to do is most Firstly, if you want to make a graph then use A D Cut Plot That will create a category plot on the y axis We will have a column with vote average in it So here we have to vote and average right vote and average than comma put it equal to df2 as written above in same manner right here But we will show its count also Next will be our order here So whatever I have written above is the same We have to show people things so I am here But what am I doing simply copying this I am pasting but please do not copy paste you have to write right if you are learning you want to learn as much as you can by writing code yourself say that will be great for you to pet d Look at the title, we are not here whatever If you want to give a title, they write the title Let’s write down the board distribution When you see a project If you make a bar, you will hear half the thing, half the thing I will be careful in making it but when you go to the second If you make a bar, you will be able to write the code yourself Right now I am showing by writing that this If you want to do this then watch the video first Make a project next time but do it yourself try once that I post other than video If I try to make it without seeing it, it will be much easier for you right so it is showing certain things like we are not getting the average vote count so let’s just Check OK, this is why a mistake is happening here Because I have put the genre here Right, you will have to change the vote average here It will come, we just did copy paste from above If you had not changed it then you have to enter it here If we look at the vote average now, this is what we get The vote average has been completed, we can see it here Are movies more popular than average ones below average and then not popular to second gonna show us what’s here We have the most movies here too I have already shown it on Let’s proceed with it What does question number three say? Movie got the most popular and what is it Its genre means which movie is liked the most Which movie has got more popularity and which is that movie Is the genre right? This is our question number three so I will give this also properly Select Mark Down in the Format heading I will do this and write it down, now we will write this down too If people want to solve this problem here then now The data frame we have is df. Inside DA and our columns are popular names There is a separate column from this I do one work, D.Ed Let me head over and show you just two first. If you look at the data, look here at the popular If you have one then which movie is the best? Popular is asking too much here and which generation does she belong to right so we Now we will write the code for this here, what is the code We will write it here first of all df our data frame We have inside a column named John Popular We have a column so I’m writing here Popular then after bracket closed double equals two In this we are assigning Popular because we create one by assigning it are here pay and then comma minimum Maximum is asking here first I will write maximum here and After this the bracket should have to be closed and then run a it is showing error popular i think The spelling of popular is wrong here, popular ok here p should be capital If the error is coming due to a spelling mistake then The most popular result is here The name of the movie in this data set is Spiderman and his popularity is 508.509 which is the movie which has lowest If it is popular, then we will post it here people will write hash then space properly run this code in mark down then same hum People need to do the same thing as we did above first up what we did was max Now I want to change this same thing I will pay here for minimum and here We are getting the result of such a movie There are two movies which are very popular The name of the first movie is The United States Vs Bali Holiday and other movies Threads are popular for both of them 133.5 what is its genre so the first movie the general of what is now the united states Music Drama History and the Threads Wali Mu threads movie is its generator If there is drama and science fiction then we When asked which movie is the lowest popular one The lowest popular movie is that it is the United States and Thread are two movies now The next question is asking which year is this which had the maximum number of movies It’s been a while now that we’ve been here If you want to show then we will solve it now finally its another question so i am just writing here question number a let se five here and we have to write this in a Format of a heading so select mark down run now here we are I have to find out which movie is that The film which has the most movies in the year If it is right then what should we do for it It would be better if you show it by making a graph We will all stay here in one access below What will people write? They will write the year like 1990 Its graph will appear here in 2000 such years It will be made like this and its value counts here If we stay right then this graph of ours will be like this We will create it here to make this graph. I will first write it in df df We have what we need, year to year In which year is the release date inside So we will write the release of this column here. name release underscore date okay and now we I will create a histogram here so I am Writing Her Hist And you will have to give some title to it too pat dot title what is the name of this graph If it happens, we will write it here The name of the graph will be the release date column Distribution okay alright a here somethings are in the caps and it’s not looking good so let’s just Write This Again column distribution alright done ok now show this we have to do it too so we are going to right here PAT dot show so that the graph is made and the runs are shown this code is ok it is showing error see error why is there a very small error here There is an error here, you put a double here inverted commas and here it is single so It is showing error because there is a mismatch so let’s make it very simple Now let’s put a single inverted comma here We have already got the release date column Now here we are looking at the best year The one in which the least number of movies have been released is of 1940 and 2020 is a year in which Most movies have been released in this Write down the answer so that when you complete this project Submit the file so that the recruiter can see that Well, he solved it using this method And the answers that came in the end were put in one place It is written with surprise that it shows your efficiency that the way you work, that is very neat and clean you in a neat and clean manner You work and explain each and every thing properly You write in a way that shows your systemic approach that shows your personality that you are having That skill set that will help you do every job systemwide that you have messed everything up by throwing garbage here and there If you give us the right, we will always do this work should anyone remember this any time Assignment comes, project comes, company comes If you want to submit then you can try it now right now you are just doing aspirant be kind but you can also go inside the company Whenever you write code, do it in the correct way It is very important to write by arranging it There is too much scolding from the other wise manager You need to get this thing right now If you want to get into a habit of this thing then let’s do it Surprisingly we finally got to the point Conclusion to surprise the points let me just write this in a mark down format and here are the things right so this is basically the first question We were asked which drama genre was the best the second highest was asked what will be the general on that so that will be drama and The most popular vote we saw was this The third question was that the most Which is the most adventurous and most popular movie? so if it is spiderman what was his genre we I wrote it and then after that I asked lowest The United was popular for two movies status and thread then asked him after that Which year had the most movies? If the film has happened in 22020 then we have all these Answer of question in a simplified manner here If it is written right then these things are very important happens whenever we work on a project we must have to write this things in a proper manner so guys I am The hospital is well understood This project will have to be done two to three times properly If you make it then the most special thing about this project what did you see that once we got the data has been pre-processed correctly After that we quickly asked five questions We had given them, we solved them quickly, but Where did the real hard work go in this project Pre-processing the data in creating it What to keep and what to remove in each column how to convert that column into labels I have to arrange this, this was the thing Worth learning in the project and always in the company If you go inside also then the data will be pre The most time consuming is processing and That is where the company is getting the most profits now. Why does it give this type of project? Because the company also wants to see that Do you know data pre-processing or not? Because if the data is clean then it Anyone will extract some insight and give you the right Even in platforms like GPT If you try putting it in, you will see something somewhere you will get insight but the most important thing is that Understanding each step is a hard task Because four GPTs cannot do this work. like what we did here is white space Right the numbers we gave It was changed to a label popular not popular so you have to think about it right so This thing is called data pre-processing any company judges this, whether you If you go to any company then such things will be yours This is checked during interviews This was our project in which this thing was very It was important and worth learning, now what is here Our work is over after making the project The work is not over yet, now what do you want You should go and do this project You can tag The Eye Scale The Eye Scale You can tag the page of Also if you want to connect with me Together and with the marks, both of us brother and sister So you can connect with us You will get detailed curriculum complete lecture and to & Projects Live Doubt Classes Industry Recognized Certificate of Interview preparation along with training You will also get help for this, the link is in the Description Hello everyone I am Swati And welcome to the class, in today’s class we Starting e-commerce sales Full project key for data analysis analyst and data science now this is This is an e-commerce project, so it is special Because the first reason for this is that e-commerce We all know about the companies first It was very difficult to imagine that in 10 minutes Inside our home we order something and come But now it is possible with e-commerce and This is possible because so much the data that is collected in the correct way Analyzing, building recommendation models, and the goods coming to our house, all these things The data science behind data analytics is a You have a huge hand in today’s project We will also complete it because E-commerce companies have a very efficient amount How to manage the data used It goes we will get to learn one thing and another The most important thing is that here we The project that we are doing is sales Whether you are a data analyst or not, you can do the analysis as well. Pay as a Data Scientist Business Analyst for any profile will go for giving interviews and whether they Any company, be it a health care company Do you work in a finance company or Any company working on AI Be it e-commerce or retail company, any company Sales is important in every company If there is a part then how to do sales analysis This is done in today’s project, you will You will also get to learn Python’s how to use libraries To do visualization, perform EDAC Today we will understand all these things we will do it through today’s project and this This project which is related to e-commerce is ours It would be very beneficial for the resume as well so definitely when you start this project You will be completing your confidence The level will increase and it will be good for your career too This could be a very important project Before making the project its complete The complete material is there, the source file is there, the code is there We have notes, all these things come to us for study There must be material for which all this Things I’ve already put down on git hapu It is placed below in the description box for you You will also get the link to git hub by going there Whatever number of files you have, the data is set You can easily download all these things So let’s start today’s class and let’s start this project all Right, let’s start today’s class where we will complete it completely The project that is your e-commerce data Now when we talk about e-commerce, the analysis If we do that then e-commerce will bring a revolution It came like this where we don’t know how many websites Know whether you use it for shopping or not flipkart-in jettar and many more like this E-commerce platforms have been built now When we talk about e-commerce, But somewhere or the other, every day millions of customers Now all these customers do shopping This data is stored in a proper manner Utilizing these to make company business decisions The company was able to improve its strategy so that the event venue can grow For this, we get so much data every day This data is released and produced It is very important to deal with And if I talk about the purpose of the job If we want to make ourselves job ready Do you want to appear in interviews That data analyst’s job is business If it is an analyst or a data scientist We have to find an e-commerce related The project must be done because there are many There are all such e-commerce companies where Hiring happens and the e-commerce tech segment there is a very large portion of Multiple requirements of multiple companies So if there are any such projects for you Portfolios include those in your resume If yes then this is also an add on advantage for you Maybe today we will be able to create such an e-commerce platform. We will take the data of here, much more than that data will try to extract all the insights So that you can understand the technical aspect as well. and along with it the business of the company This is your point of view, you should also understand this Now if I may talk about it here There are as many technical brands as Brands are like those people who like us We get rights online from where we people Do online shopping like let’s suppose Let’s say we went shopping flipkart-com is a platform but its Apart from this, there are many such offline stores Now these are the offline stores A lot of data is produced from them too. Like for example let me talk about Big Bazaar or should I earn or should I talk Lifestyle Key So all these are the brands of There is a digital presence, but its Along with this, they also have offline centers where we go and from their shop We do our own shopping, so now it’s so There are big franchise chains like If there is a D-Mark then their shops are there Customers also visit here and their There is data, we have to analyze it too It is important so today we are sharing such data We will take a set in which even if we go offline customers are coming online line customers are coming If so, then the analysis is done from their data All these things have to be taken out in today’s In today’s project in class, we Now people will discuss whatever the size You can look at the brands even if I talk about them Lens Cart’s Sugar Cosmetics Mama Earth Louis Vito Sunny or else I am shopping from my cart so I Visit Lenskart’s online website and app I can go shopping there or I Go shopping at any Lenskart store If I can do it then online data and offline The data which is of both these customers How to deal with today’s project We will keep all these things in mind as well And now we will learn all these things too The situation you have here is that you The data of an e-commerce company is here pay is given so first of all I will tell you Let me show you the data set here, something like this type of data set we have here Available now in this data set you have to order ID of which date the shipping was done what was the mode customer id was from which country The customer was City State, all of this The data is given to you here Product ID: How much quantity did that customer buy? How much did you buy and sell? How much was the discount? How much was the profit, all these things It is now given that every company has the most The more important thing would be That company has the sales right, no matter which company it is Also if I talk about the company then give me sales way it could be one of her online Sales can be done through online mode and an offline mode Sales from this event can be important What is it like to analyze his sales? How much profit did that company make? There was no loss so much expenses If the company does it, then every company, whether you are any also go to the company for every company its It is very important to analyze the sales If it is more important then today we Whenever there is a company, someone who sells How much profit is to be made from it How much revenue is all this coming monthly You will also learn these things in today’s project So here we actually have the data set Have a look at e-commerce here The data set is available now also here But we have some questions Whatever amount of data is there in this data set Looking at the record, here we You have to solve six to seven questions The first question here is being asked by you Need to calculate the monthly sales of the store End Identification has the highest sales and the Lowest sales means you have a company You have a record of every sale The sales of the month have to be analyzed and this Tell me which month is the most What is the sales and which month is the lowest? Sales have happened, after that you have to analyze it You need to analyze sales based on Product Category and Determine Which The category has the lowest sales and which judges the highest sale now every single company when we Let’s talk about different categories of e-commerce I sell things, for example, furniture Electronic became a category Appliances has become one category and different If there can be a category here then which one? In which category sales are more and in which less We have analyzed all these things Have to do it according to the company’s product Now let’s talk about sales analysis needs to Be done on the sub category now like for For example, let me talk about a category If I buy electronic appliances If I talk about category then mobile is your Laptop has become a separate category in this TV has become a different category, yours is different AC has become a category, AC has become a separate category We also get sales according to each sub category I have to do the analysis of this here fourth The question is telling you that you need to analyze the Monthly profit from the sales determined Which month has the highest profit which month The company made the highest profit in a month This has happened, we also have to remove it Together we all get profit In the category let’s suppose company if If you sell electronic goods then it is possible that he is facing loss in mobile and If you are getting benefit from laptop then it is more If you focus on your laptop then which one? In this category, he has an advantage and a disadvantage You also have to take it out and show it along with this You have to understand each customer segment which is different Some customers shop like this Those who do shopping, their monthly income is 5 There are some customers who are less than Rs 5,00,000 which will have more than 1 lakh different If the customer segment is right then According to them we also have sales here Data has to be analysed and finally speak This is also the sales to profit ratio You have to take it out now let’s suppose your sales Right of ₹ 1 lakh is done but in this The profit margin was very low so how much Was there profit or was the company in loss? was as mathematical as possible about all these things Operations are the profit from sales We will tell you all the things through today’s project. People will solve these seven questions which were given to us as an assignment We will answer these seven questions one by one People will find the answers here when you Even if you go into interviews, actually What happens is that in your first round you have it in the same way as I am now Telling you like a business case study I have given you a data set Questions are given and you have 24 to 48 hours to answer them time is given that all these By finding the answer to the questions you can solve your problem. This is the code file, I will mail everything If the company has your If you get shortlisted then you will get second chance When a call comes for the round then this Like assignments which are questions and special We should do it as far as e-commerce is concerned If he wants then he has to answer all these seven questions We will solve different concepts will clean the data Manipulation will be done by EDL visualization If we do then we will learn all these things You will meet me in today’s project So let us now move forward with the company’s case We have understood what we have to study Now we will start further work, how will we collect the data We will extract all this information one by one One, let’s start this today project to do all these things We need to analyze the data first. So for this we will have to use Jupiter Notebook for which we use Anaconda Navigator We must have Anaconda installed Navigator is already installed on my system so We’ll go to the start here, Anaconda Navigator will search Anacanda here As soon as the navigator appears, you can simply use it you can go and open it when you open it to see the interface something like this Here we will get an ID whose The name is Jupyter Notebook this Jupiter You can find the button to launch the notebook here. You will get it as soon as we launch it here If you do this, whichever browser you use You will automatically get this in your browser This will open for me here, it is already there It is open now after this this data set of ours I have been given this data set which is the most I will have to upload it first, I have made one here A new folder is created and its What will I do inside I will upload it here so let’s put this data set here Simply click on upload and you will store the sample name is the name of this data set so here We have uploaded it now the next step what will happen now the next step will be that as much as There is also data in it, we need Python Libraries will have to be installed and after that the data If you need to analyze it then click here for it But I will go to new and the first option which I have given your python3 iva kernel to this If I simply open it here then this will be our The pass will open here Python IY File Here we perform all the operations I will now name this file something no it is so it is untitled you are untitled You will go there and click on it and whatever name you we want to keep it’s name here Want to keep the e-commerce project right So here I am going to start with an e-commerce project called I renamed this file now The first thing we do is to take this data set that we have There are several things that have happened in this data set. People have to do analysis and visualization We have to do monthly sales here He said right, he told us here how much profit We would have plenty of these things We need to make graphs here so that we can Now you can create the correct report You will need some libraries for this First we have to see that the data is clean data Is there any error in the data this is something we need to figure out first So for this we need people here You will need some libraries here We are doing libraries one by one When I start importing the data When I talk about cleaning then the most Important libraries are located here Panda, this is the panda library which helps We can do data cleaning very well with this Now we can see many graphs Here you have to make it for visualization Many times you have used matte flat lip color You might have used Plotly but this In the project I would especially like to tell you about the new LATEST LIBRARY Matte Palette Lip Seaborn The libraries are quite old, from 2003 Its use has been going on for around but This Plotly library is from 2015 Since then work on it has started, so it The library is on Pallip Seaborn It is based but this library is a bit advanced it is like a new library and in it Many latest things have come which will help This makes the graph dynamic and it gets created quickly If yes then we will use this library A lot of people skip Plotly but if you have started this project If you complete it, it means your Plotly library is also somewhere in this Your concepts will also be covered in this During the project, we are here We will use the Plotly library because This is dynamic, it is a new library and You can try new things in the industry You will have to do new things that will come out of it You should know how to use it, we are just one source But if you can’t rely on it then go for a matte lip I have done other projects on Seeb too But I am doing this project on Plotly I am doing this so that you can learn Plotly as well Let’s go ahead and first of all here We import Plotly Write to the library for this here I will have to import plotly.com Express SP 1 now what does this mean This is about data visualization For this, the library which is here can be used Now we will have to use it here I also have to make a lot of graphs by doing this so I its another module import plotly dot graph underscore objects here I will write it down and we will name it as age here. People will write that what will this do to us To create advanced and customized graphs will help for now after this there is another one here We will use Plotly here module import plotly dcompare this what work should I do So these are the templates of the graph This would have been very helpful for him here After this there is another module of Plotly That is why I am telling you this in all the modules so that you can use Plotly from here Understanding can also be developed through import Plotly now I want colors here too If it is in my graph then I will write it here I am importing plotly dot colors edge Colours right, these are the colours I used now what I want here is that By default our template’s theme is I want it to be white so here it is But I will use PIO which I have mentioned above Plotly is already written here PO D template and here we have no default We will use template so I am using default here I am writing PO D Template D Default pad telete d default equals to now here I want yes this invalid syntax is coming so Because our code just ran by mistake Plotly underscore not completed white white here is the template If I want a theme, I wrote it here. White now let’s run this all of it Your libraries will be imported here it is showing some error here let’s just Check Plus Let’s Check Where the Mistake Is So here the P is capitalized. It needs to be made smaller, alright, so right now it is This is our mistake, by mistake we capitalize on it It was done, we made it smaller, now this The code has been run properly, error g Why is Panda being used Data is being used using Panda Plotly for cleaning up your visualizations Plotly does not judge graph objects for Go This will help you advance and customize For the graph this is Plotly D Ao P Ao This will help you to get a graph template For customizing colors we have I have already imported it here and The theme we want is white so I have written plotly white here so this what have we done here all the time Now we have imported this data It was uploaded here, now read this data also we have to do that then by data name i am going to do this one I will create a variable and I will put this data here But if I read it then it is equal to read Under We have our data in CSV and beyond Its name is sample now you just look at it like this

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog

  • FastAPI Analytics API with Python and TimeScale

    FastAPI Analytics API with Python and TimeScale

    This series of tutorials guides viewers through building a comprehensive analytics API from scratch using Python and FastAPI. It focuses on creating a robust backend service capable of ingesting and storing time-series data efficiently using a PostgreSQL database optimized for this purpose, called TimescaleDB. The process involves setting up a development environment with Docker, defining data models and API endpoints, implementing data validation with Pydantic, and performing CRUD operations using SQLModel. Furthermore, the tutorials cover aggregating time-series data with TimescaleDB’s hyperfunctions and deploying the application to production using Timescale Cloud and Railway. Finally, it explores making the deployed API a private networking service accessible by other applications, like a Jupyter notebook server, for data analysis and visualization.

    Analytics API Microservice Study Guide

    Quiz

    1. What is the primary goal of the course as stated by Justin Mitchell? The main objective is to build an analytics API microservice using FastAPI. This service will be designed to receive and analyze web traffic data from other services.
    2. Why is TimeScaleDB being used in this project, and what is its relationship to PostgreSQL? TimeScaleDB is being used because it is a PostgreSQL extension optimized for time-series data and analytics. It enhances PostgreSQL’s capabilities for bucketing and aggregating data based on time.
    3. Explain the concept of a private analytics API service as described in the source. A private analytics API service means that the API will not be directly accessible from the public internet. It will only be reachable from internal resources that are specifically granted access.
    4. What is the significance of the code being open source in this course? The open-source nature of the code allows anyone to access, grab, run, and deploy their own analytics API whenever they choose. This gives users complete control and flexibility over their analytics infrastructure.
    5. Describe the purpose of setting up a virtual environment for Python projects. Creating a virtual environment isolates Python projects and their dependencies from each other. This prevents conflicts between different project requirements and ensures that the correct Python version and packages are used for each project.
    6. What is Docker Desktop, and what are its key benefits for this project? Docker Desktop is an application that allows users to build, share, and run containerized applications. Its benefits for this project include creating a consistent development environment that mirrors production, easy setup of databases like TimeScaleDB, and simplified sharing and deployment of the API.
    7. Explain the function of a Dockerfile in the context of containerizing the FastAPI application. A Dockerfile contains a set of instructions that Docker uses to build a container image. These instructions typically include specifying the base operating system, installing necessary software (like Python and its dependencies), copying the application code, and defining how the application should be run.
    8. What is Docker Compose, and how does it simplify the management of multiple Docker containers? Docker Compose is a tool for defining and managing multi-container Docker applications. It uses a YAML file to configure all the application’s services (e.g., the FastAPI app and the database) and allows users to start, stop, and manage them together with single commands.
    9. Describe the purpose of API routing in a RESTful API like the one being built. API routing defines the specific URL paths (endpoints) that the API exposes. It dictates how the API responds to different requests based on the URL and the HTTP method (e.g., GET, POST) used to access those endpoints.
    10. What is data validation, and how is Pydantic (and later SQLModel) used for this purpose in the FastAPI application? Data validation is the process of ensuring that incoming and outgoing data conforms to expected formats and types. Pydantic (and subsequently SQLModel, which is built on Pydantic) is used to define data schemas, allowing FastAPI to automatically validate data against these schemas and raise errors for invalid data.

    Essay Format Questions

    1. Discuss the benefits of using a microservice architecture for an analytics API, referencing the technologies chosen in this course (FastAPI, TimeScaleDB, Docker).
    2. Explain the process of setting up a development environment that closely mirrors a production environment, as outlined in the initial sections of the course, and discuss the rationale behind each step.
    3. Compare and contrast the roles of virtual environments and Docker containers in isolating and managing application dependencies and runtime environments.
    4. Describe the transition from using Pydantic schemas for data validation to SQLModel for both validation and database interaction, highlighting the advantages of SQLModel in building a data-driven API.
    5. Explain the concept of Time Series data and how TimeScaleDB’s hypertable feature is designed to efficiently store and query this type of data, including the significance of chunking and retention policies.

    Glossary of Key Terms

    • API (Application Programming Interface): A set of rules and protocols that allows different software applications to communicate and exchange data with each other.
    • Microservice: An architectural style that structures an application as a collection of small, independent services, each focusing on a specific business capability.
    • Analytics API: An API specifically designed to provide access to and facilitate the analysis of data.
    • Open Source: Software with source code that is freely available and can be used, modified, and distributed by anyone.
    • FastAPI: A modern, high-performance web framework for building APIs with Python, based on standard Python type hints.
    • TimeScaleDB: An open-source time-series database built as a PostgreSQL extension, optimized for high-volume data ingestion and complex queries over time-series data.
    • PostgreSQL: A powerful, open-source relational database management system known for its reliability and extensibility.
    • Time Series Data: A sequence of data points indexed in time order.
    • Jupyter Notebook: An open-source web-based interactive development environment that allows users to create and share documents containing live code, equations, visualizations, and narrative text.
    • Virtual Environment: An isolated Python environment that allows dependencies for a specific project to be installed without affecting other Python projects or the system-wide Python installation.
    • Docker: A platform for building, shipping, and running applications in isolated environments called containers.
    • Container: A lightweight, standalone, and executable package of software that includes everything needed to run an application: code, runtime, system tools, libraries, and settings.
    • Docker Desktop: A user-friendly application for macOS and Windows that enables developers to build and share containerized applications and microservices.
    • Dockerfile: A text document that contains all the commands a user could call on the command line to assemble a Docker image.
    • Docker Compose: A tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application’s services.
    • API Endpoint: A specific URL that an API exposes and through which client applications can access its functionalities.
    • REST API (Representational State Transfer API): An architectural style for designing networked applications, relying on stateless communication and standard HTTP methods.
    • API Routing: The process of defining how the API responds to client requests to specific endpoints.
    • Data Validation: The process of ensuring that data meets certain criteria, such as type, format, and constraints, before it is processed or stored.
    • Pydantic: A Python data validation and settings management library that uses Python type hints.
    • SQLModel: A Python library for interacting with SQL databases, built on Pydantic and SQLAlchemy, providing data validation, serialization, and database integration.
    • SQLAlchemy: A popular Python SQL toolkit and Object Relational Mapper (ORM) that provides a flexible and powerful way to interact with databases.
    • Database Engine: A software component that manages databases, allowing users to create, read, update, and delete data. In SQLModel, it refers to the underlying SQLAlchemy engine used to connect to the database.
    • SQL Table: A structured collection of data held in a database, consisting of columns (attributes) and rows (records).
    • Environment Variables: Dynamic named values that can affect the way running processes will behave on a computer. They are often used to configure application settings, such as database URLs.
    • Hypertable: A core concept in TimeScaleDB, representing a virtual continuous table across all time and space, which is internally implemented as multiple “chunk” tables.
    • Chunking: The process by which TimeScaleDB automatically partitions hypertable data into smaller, more manageable tables based on time and optionally other partitioning keys.
    • Retention Policy: A set of rules that define how long data should be kept before it is automatically deleted or archived.
    • Time Bucket: A function in TimeScaleDB that aggregates data into specified time intervals.
    • Gap Filling: A technique used in time-series analysis to fill in missing data points in a time series, often by interpolation or using a default value.
    • Railway: A cloud platform that simplifies the deployment and management of web applications and databases.

    Briefing Document: Analytics API Microservice Development

    This briefing document summarizes the main themes and important ideas from the provided source material, which outlines the development of an open-source analytics API microservice using FastAPI, PostgreSQL with the TimescaleDB extension, and various other modern technologies.

    Main Themes:

    • Building a Private Analytics API: The primary goal is to create a microservice that can ingest and analyze web traffic data from other services within a private network.
    • Open Source and Customization: The entire codebase is open source, allowing users to freely access, modify, and deploy their own analytics API.
    • Modern Technology Stack: The project utilizes FastAPI (a modern, fast web framework for building APIs), PostgreSQL (a robust relational database), TimescaleDB (a PostgreSQL extension optimized for time-series data and analytics), Docker (for containerization), and other related Python packages.
    • Time-Series Data Analysis: A key focus is on leveraging TimescaleDB’s capabilities for efficient storage and aggregation of time-stamped data.
    • Production-Ready Deployment: The course aims to guide users through the process of deploying the analytics API into a production-like environment using Docker.
    • Local Development Environment Setup: A significant portion of the initial content focuses on setting up a consistent and reproducible local development environment using Python virtual environments and Docker.
    • API Design and Routing: The process includes designing API endpoints, implementing routing using FastAPI, and handling different HTTP methods (GET, POST).
    • Data Validation with Pydantic and SQLModel: The project utilizes Pydantic for data validation of incoming and outgoing requests and transitions to SQLModel for defining database models, which builds upon Pydantic and SQLAlchemy.
    • Database Integration with PostgreSQL and TimescaleDB: The briefing covers setting up a PostgreSQL database (initially standard, then upgraded to TimescaleDB) using Docker Compose and integrating it with the FastAPI application using SQLModel.
    • Querying and Aggregation of Time-Series Data: The document introduces the concept of time buckets in TimescaleDB for aggregating data over specific time intervals.
    • Cloud Deployment with Railway: The final section demonstrates deploying the analytics API and a Jupyter Notebook server (for accessing the API) to the Railway cloud platform.

    Most Important Ideas and Facts:

    • Open Source Nature: “thing about all of this is all the code is open source so you can always just grab it and run with it and deploy your own analytics API whenever you want to that’s kind of the point”. This emphasizes the freedom and customizability offered by the project.
    • FastAPI for API Development: The course uses FastAPI for its speed, ease of use, and integration with modern Python features. “The point of this course is to create an analytics API microservice we’re going to be using fast API so that we can take in a bunch of web traffic data on our other services and then we’ll be able to analyze them as we see fit.”
    • TimescaleDB for Time-Series Optimization: TimescaleDB is crucial for efficiently handling and analyzing time-based data. “we’ll be able to analyze them as we see fit and we’ll be able to do this with a lot of modern technology postgres and also specifically time scale so we can bucket data together and do aggregations together.” It enhances PostgreSQL specifically for time-series workloads. “Time scale is a postgres database so it’s still postres SQL but it’s optimized for time series.”
    • Private API Deployment: The deployed API is intended to be private and only accessible to authorized internal resources. “This is completely private as in nobody can access it from the outside world it’s just going to be accessible from the resources we deem should be accessible to it…”
    • Data Aggregation: The API will allow for aggregating raw web event data based on time intervals. “from this raw data we will be able to aggregate this data and put it into a bulk form that we can then analyze.”
    • Time-Based Queries: TimescaleDB facilitates querying data based on time series. “…it’s much more about how to actually get to the point where we can aggregate the data based off of time based off of Time series that is exactly what time scale does really well and enhances Post cres in that way.”
    • SQLModel for Database Interaction: The project utilizes SQLModel, which simplifies database interactions by combining Pydantic’s data validation with SQLAlchemy’s database ORM capabilities. “within that package there is something called SQL model which is a dependency of the other package…this one works well with fast API and you’ll notice that it’s powered by pantic and SQL Alchemy…”
    • Importance of Virtual Environments: Virtual environments are used to isolate Python project dependencies and avoid version conflicts. “we’re going to create a virtual environment for python projects so that they can be isolated from one another in other words python versions don’t conflict with each other on a per project basis.”
    • Docker for Environment Consistency: Docker is used to create consistent and reproducible environments for both development and production. “docker containers are that something what we end up doing is we package up this application into something called a container image…”
    • Docker Compose for Multi-Container Management: Docker Compose is used to define and manage multi-container Docker applications, such as the FastAPI app and the PostgreSQL/TimescaleDB database.
    • API Endpoints and Routing: FastAPI’s routing capabilities are used to define API endpoints (e.g., /healthz, /api/events) and associate them with specific functions and HTTP methods (GET, POST).
    • Data Validation with Pydantic: Pydantic models (Schemas) are used to define the structure and data types of request and response bodies, ensuring data validity.
    • Transition to SQLModel for Database Models: Pydantic schemas are transitioned to SQLModel models to map them to database tables. “we’re going to convert our pantic schemas into SQL models…”
    • Database Engine and Session Management: SQLAlchemy’s engine is used to connect to the database, and a session management function is implemented to handle database interactions within FastAPI routes.
    • Creating Database Tables with SQLModel: SQLModel’s create_all function is used to automatically create database tables based on the defined SQLModel models. “This is going to be our command to actually make sure that our database tables are created and they are created based off of our SQL model class to create everything so it’s SQL model. metadata. create all and it’s going to take in the engine as argument…”
    • TimescaleDB Hypertable Creation: To optimize for time-series data, standard PostgreSQL tables are converted into TimescaleDB hypertables. “it’s time to create hyper tables now if we did nothing else at this point and still use this time scale model it would not actually be optimized for time series data we need to do one more step to make that happen a hypertable is a postgress table that’s optimized for that time series data with the chunking and also the automatic retention policy where it’ll delete things later.”
    • Hypertable Configuration (Chunking and Retention): TimescaleDB hypertables can be configured with chunk time intervals and data retention policies (drop after). “the main three that I want to look at are these three first is the time column this is defaulting to the time field itself right so we probably don’t ever want to change the time colum itself although it is there it is supported if you need to for some reason in time scale in general what you’ll often see the time column that’s being used is going to be named time or the datetime object itself so that one’s I’m not going to change the ones that we’re going to look at are the chunk time interval and then drop after…”
    • Time Bucketing for Data Aggregation: TimescaleDB’s time_bucket function allows for aggregating data into time intervals. “we’re now going to take a look at time buckets time buckets allow us to aggregate data over a Time interval…”
    • Cloud Deployment on Railway: The project demonstrates deploying the API and a Jupyter Notebook to the Railway cloud platform for accessibility.

    Quotes:

    • “thing about all of this is all the code is open source so you can always just grab it and run with it and deploy your own analytics API whenever you want to that’s kind of the point”
    • “The point of this course is to create an analytics API microservice we’re going to be using fast API so that we can take in a bunch of web traffic data on our other services and then we’ll be able to analyze them as we see fit and we’ll be able to do this with a lot of modern technology postgres and also specifically time scale so we can bucket data together and do aggregations together”
    • “This is completely private as in nobody can access it from the outside world it’s just going to be accessible from the resources we deem should be accessible to it”
    • “from this raw data we will be able to aggregate this data and put it into a bulk form that we can then analyze”
    • “Time scale is a postgres database so it’s still postres SQL but it’s optimized for time series”
    • “within that package there is something called SQL model which is a dependency of the other package…this one works well with fast API and you’ll notice that it’s powered by pantic and SQL Alchemy…”
    • “we’re going to create a virtual environment for python projects so that they can be isolated from one another in other words python versions don’t conflict with each other on a per project basis”
    • “docker containers are that something what we end up doing is we package up this application into something called a container image…”
    • “it’s time to create hyper tables now if we did nothing else at this point and still use this time scale model it would not actually be optimized for time series data we need to do one more step to make that happen a hypertable is a postgress table that’s optimized for that time series data with the chunking and also the automatic retention policy where it’ll delete things later.”
    • “we’re now going to take a look at time buckets time buckets allow us to aggregate data over a Time interval…”

    This briefing provides a comprehensive overview of the source material, highlighting the key objectives, technologies, and processes involved in building the analytics API microservice. It emphasizes the open-source nature, the use of modern tools, and the focus on efficient time-series data analysis with TimescaleDB.

    Building a Private Analytics API with FastAPI and TimescaleDB

    Questions

    • What is the main goal of this course and what technologies will be used to achieve it? The main goal of this course is to guide users through the creation of a private analytics API microservice. This API will be designed to ingest and analyze web traffic data from other services. The course will primarily use FastAPI as the web framework, PostgreSQL as the database, and specifically TimescaleDB as a PostgreSQL extension optimized for time-series data. Other tools mentioned include Docker for containerization, Cursor as a code editor, and a custom Python package called timescaledb-python.
    • Why is the created analytics API intended to be private, and how will it be accessed and tested? The analytics API is designed to be completely private, meaning it will not be directly accessible from the public internet. It will only be reachable from internal resources that are specifically granted access. To test the API after deployment, a Jupyter Notebook server will be used. This server will reside within the private network and will send simulated web event data to the API for analysis and to verify its functionality.
    • What is TimescaleDB, and why is it being used in this project instead of standard PostgreSQL? TimescaleDB is a PostgreSQL extension that optimizes the database for handling time-series data. While it is built on top of PostgreSQL and retains all of its features, TimescaleDB enhances its capabilities for data bucketing, aggregations based on time, and efficient storage and querying of large volumes of timestamped data. It is being used in this project because the core of an analytics API is dealing with data that changes over time, making TimescaleDB a more suitable and performant choice for this specific use case compared to standard PostgreSQL.
    • Why is the course emphasizing open-source tools and providing the code on GitHub? A key aspect of this course is the commitment to open-source technology. All the code developed throughout the course will be open source, allowing users to freely access, use, modify, and distribute it. The code will be hosted on GitHub, providing a collaborative platform for users to follow along, contribute, and deploy their own analytics API based on the course materials. This also ensures transparency and allows users to have full control over their analytics solution.
    • What are virtual environments, and why is setting one up considered an important first step in the course? Virtual environments are isolated Python environments that allow users to manage dependencies for specific projects without interfering with the global Python installation or other projects. Setting up a virtual environment is crucial because it ensures that the project uses the exact Python version and required packages (like FastAPI, Uvicorn, TimescaleDB Python) without conflicts. This leads to more reproducible and stable development and deployment processes, preventing issues caused by differing package versions across projects.
    • What is Docker, and how will it be used in this course for development and potential production deployment? Docker is a platform that enables the creation and management of containerized applications. Containers package an application along with all its dependencies, ensuring consistency across different environments. In this course, Docker will be used to set up a local development environment that closely mirrors a production environment. This includes containerizing the FastAPI application and the TimescaleDB database. Docker Compose will be used to manage these multi-container setups locally. While the course starts with the open-source version of TimescaleDB in Docker, it suggests that for production, more robust services directly from Timescale might be considered.
    • How will FastAPI be used to define API endpoints and handle data? FastAPI will serve as the web framework for building the analytics API. It will be used to define API endpoints (URL paths) that clients can interact with to send data and retrieve analytics. FastAPI’s features include automatic data validation, serialization, and API documentation based on Python type hints. The course will demonstrate how to define routes for different HTTP methods (like GET and POST) and how to use Pydantic (and later SQLModel) for defining data models for both incoming requests and outgoing responses, ensuring data consistency and validity.
    • How will SQLModel be integrated into the project, and what benefits does it offer for interacting with the database? SQLModel is a Python library that combines the functionalities of Pydantic (for data validation and serialization) and SQLAlchemy (for database interaction). It allows developers to define database models using Python classes with type hints, which are then automatically translated into SQL database schemas. In this course, SQLModel will be used to define the structure of the event data that the API will collect and store in the TimescaleDB database. It will simplify database interactions by providing an object-relational mapping (ORM) layer, allowing developers to work with Python objects instead of writing raw SQL queries for common database operations like creating, reading, updating, and deleting data.

    Analytics API for Time Series Data with FastAPI and TimescaleDB

    Based on the source “01.pdf”, an Analytics API service is being built to ingest, store, and refine data, specifically time series data, allowing users to control and analyze it. The goal is to create a microservice that can take in a lot of data, such as web traffic data from other services, store it in a database optimized for time series, and then enable analysis of this data.

    Here are some key aspects of the Analytics API service as described in the source:

    • Technology Stack:
    • FastAPI: This is used as the microservice framework and the API endpoint, built with Python. FastAPI is described as a straightforward and popular tool that allows for writing simple Python functions to handle API endpoints. It’s noted for being minimal and flexible.
    • Python: The service is built using Python, which is a dependency for FastAPI. The tutorial mentions using Python 3.
    • TimescaleDB: This is a PostgreSQL database optimized for time series data and is used to store the ingested data. TimescaleDB enhances PostgreSQL for time series operations, making it suitable for bucketing data and performing aggregations based on time. The course partnered with Timescale.
    • Docker: Docker is used to containerize the application, making it portable and able to be deployed anywhere with a Docker runtime, and also to easily run database services like TimescaleDB locally. Containerization ensures the application is production-ready.
    • Railway: This is a containerized cloud platform used for deployment, allowing for a Jupyter Notebook server to connect to the private analytics API endpoint.
    • Functionality:
    • Data Ingestion: The API is designed to ingest a lot of data, particularly web traffic data, which can change over time (time series data).
    • Data Storage: The ingested data is stored in a Timescale database, which is optimized for efficient storage and querying of time-based data.
    • Data Modeling: Data modeling is a key aspect, especially when dealing with querying the data. The models are based on Pydantic, which is also the foundation for SQLModel, making them relatively easy to work with, especially for those familiar with Django models. A custom Python package, timescale-db-python, was created for this series to facilitate the use of FastAPI and SQLModel with TimescaleDB.
    • Querying and Aggregation: The service allows for querying and aggregating data based on time and other parameters. TimescaleDB is particularly useful for time-based aggregations. More advanced aggregations can be performed to gain complex insights from the data.
    • API Endpoints: FastAPI is used to create API endpoints that handle data ingestion and querying. The tutorial covers creating endpoints for health checks, reading events (with list and detail views), creating events (using POST), and updating events (using PUT).
    • Data Validation: Pydantic is used for both incoming and outgoing data validation, ensuring the data conforms to defined schemas. This helps in hardening the API and ensuring data integrity.
    • Development and Deployment:
    • Local Development: The tutorial emphasizes setting up a local development environment that closely mirrors a production environment, using Python virtual environments and Docker.
    • Production Deployment: The containerized application is deployed to Railway, a containerized cloud service. The process involves containerizing the FastAPI application with Docker and configuring it for deployment on Railway.
    • Private API: The deployment on Railway includes the option to create a private analytics API service, accessible only from within designated resources, enhancing security.
    • Example Use Case: The primary use case demonstrated is building an analytics API to track and analyze web traffic data over time. This involves ingesting data about page visits, user agents, and durations, and then aggregating this data based on time intervals and other dimensions.

    The tutorial progresses from setting up the environment to building the API with data models, handling different HTTP methods, implementing data validation, integrating with a PostgreSQL database using SQLModel, optimizing for time series data with TimescaleDB (including converting tables to hypertables and using time bucket functions for aggregation), and finally deploying the application to a cloud platform. The source code for the project is open source and available on GitHub.

    FastAPI for Analytics Microservice API Development

    Based on the source “01.pdf” and our previous discussion, FastAPI is used as the microservice framework for building an analytics API service from scratch. This involves creating an API endpoint that primarily ingests a single data model.

    Here’s a breakdown of FastAPI’s role in the context of this microservice:

    • API Endpoint Development: FastAPI is the core technology for creating the API endpoints. The source mentions that the service will have “well mostly just one data model that we’re going to be ingesting” through these endpoints. It emphasizes that FastAPI allows for writing “fairly straightforward python functions that will handle all of the API inp points”.
    • Simplicity and Ease of Use: FastAPI is described as a “really really popular tool to build all of this out” and a “really great framework to incrementally add things that you need when you need them”. It’s considered “very straightforward” and “minimal”, meaning it doesn’t include a lot of built-in features, offering flexibility. The models built with FastAPI (using SQLModel, which is based on Pydantic) are described as “a little bit easier to work with than Django models” with “less to remember” because they are based on Pydantic.
    • URL Routing: Similar to other web frameworks, FastAPI facilitates easy URL routing, which is essential for defining the different endpoints of the API service.
    • Data Validation and Serialization: FastAPI leverages Pydantic for data modeling. This allows for defining data structures with type hints, which FastAPI uses for automatic data validation and serialization (converting Python objects to JSON and vice versa). We saw examples of this with the Event schema and how incoming and outgoing data were validated.
    • HTTP Method Handling: FastAPI makes it straightforward to define functions that handle different HTTP methods (e.g., GET for retrieving data, POST for creating data, PUT for updating data) for specific API endpoints.
    • Middleware Integration: FastAPI allows the integration of middleware, such as CORS (Cross-Origin Resource Sharing) middleware, which was added to control which websites can access the API.
    • Integration with Other Tools: FastAPI works well with other tools in the described architecture, including:
    • SQLModel: Built on top of Pydantic and SQLAlchemy, SQLModel simplifies database interactions and is used within the FastAPI application to interact with the PostgreSQL database (TimescaleDB).
    • Uvicorn and Gunicorn: Uvicorn is used as an ASGI server to run the FastAPI application, particularly during development. Gunicorn can be used in conjunction with Uvicorn for production deployments.
    • Docker: FastAPI applications can be easily containerized using Docker, making them portable and scalable.
    • Production Readiness: The tutorial emphasizes building applications that are production-ready from the start, and FastAPI, along with Docker and Railway, facilitates this. The deployment process to Railway involves containerizing the FastAPI application.

    In essence, FastAPI serves as the foundation for building the API interface of the analytics microservice, handling requests, processing data according to defined models, and interacting with the underlying data storage (TimescaleDB) through SQLModel. Its design principles of being high-performance, easy to use, and robust make it well-suited for building modern microservices.

    TimescaleDB: PostgreSQL for Time Series Data

    Based on the source “01.pdf” and our conversation history, TimescaleDB is a PostgreSQL database extension that is optimized for time series data. While it is still fundamentally PostgreSQL, it includes specific features and optimizations that make it particularly well-suited for handling and analyzing data that changes over time, such as the web traffic data for the analytics API service being built.

    Here’s a more detailed discussion of PostgreSQL with the Timescale extension:

    • Based on PostgreSQL: TimescaleDB is not a separate database but rather an extension that runs within PostgreSQL. This means that it retains all the reliability, features, and the extensive ecosystem of PostgreSQL, while adding time series capabilities. The source explicitly states, “time scale is a postgres database so it’s still postres SQL”. This also implies that tools and libraries that work with standard PostgreSQL, like SQLModel, can also interact with a TimescaleDB instance.
    • Optimization for Time Series Data: The core reason for using TimescaleDB is its optimization for time series data. Standard PostgreSQL is not inherently designed for the high volume of writes and complex time-based queries that are common in time series applications. TimescaleDB addresses this by introducing concepts like hypertables.
    • Hypertables: A hypertable is a virtual table that is partitioned into many smaller tables called chunks, based on time and optionally other criteria. This partitioning improves query performance, especially for time-based filtering and aggregations, as queries can be directed to only the relevant chunks. We saw the conversion of the EventModel to a hypertable in the tutorial.
    • Time-Based Operations: TimescaleDB provides hyperfunctions that are specifically designed for time series analysis, such as time_bucket for aggregating data over specific time intervals. This allows for efficient calculation of metrics like counts, averages, and other aggregations over time.
    • Data Retention Policies: TimescaleDB allows for the definition of automatic data retention policies, where older, less relevant data can be automatically dropped based on time. This helps in managing storage costs and maintaining performance by keeping the database size manageable.
    • Use in the Analytics API Service: The analytics API service leverages TimescaleDB as its primary data store because it is designed to ingest and analyze time series data (web traffic events).
    • Data Ingestion and Storage: The API receives web traffic data and stores it in the Timescale database. The EventModel was configured to be a hypertable, optimized for this kind of continuous data ingestion.
    • Time-Based Analysis: TimescaleDB’s time_bucket function is used to perform aggregations on the data based on time intervals (e.g., per minute, per hour, per day, per week). This enables the API to provide insights into how web applications are performing over time.
    • Efficient Querying: By using hypertables and time-based indexing, TimescaleDB allows for efficient querying of the large volumes of time series data that the API is expected to handle.
    • Deployment: TimescaleDB can be deployed in different ways, as seen in the context of the tutorial:
    • Local Development with Docker: The tutorial uses Docker to run a containerized version of open-source TimescaleDB for local development. This provides a consistent and isolated environment for testing the API’s database interactions.
    • Cloud Deployment with Timescale Cloud: For production deployment, the tutorial utilizes Timescale Cloud, a managed version of TimescaleDB. This service handles the operational aspects of running TimescaleDB, such as maintenance, backups, and scaling, allowing the developers to focus on building the API. The connection string for Timescale Cloud was configured in the deployment environment on Railway.
    • Integration with FastAPI and SQLModel: Despite its specialized time series features, TimescaleDB remains compatible with PostgreSQL standards, allowing for seamless integration with tools like SQLModel used within the FastAPI application. SQLModel, being built on Pydantic and SQLAlchemy, can define data models that map to tables (including hypertables) in TimescaleDB. A custom Python package, timescale-db-python, was even created to further streamline the interaction between FastAPI, SQLModel, and TimescaleDB, especially for defining hypertables.

    In summary, TimescaleDB is a powerful extension to PostgreSQL that provides the necessary optimizations and functions for efficiently storing, managing, and analyzing time series data, making it a crucial component for building the described analytics API service. Its compatibility with the PostgreSQL ecosystem and various deployment options offer flexibility for both development and production environments.

    Docker Containerization for Application Deployment

    Based on the source “01.pdf” and our conversation history, Docker containerization is a technology used to package an application and all its dependencies (such as libraries, system tools, runtime, and code) into a single, portable unit called a container image. This image can then be used to run identical containers across various environments, ensuring consistency from development to production.

    Here’s a breakdown of Docker containerization as discussed in the source:

    • Purpose and Benefits:
    • Emulating Production Environment: Docker allows developers to locally emulate a deployed production environment very closely. This helps in identifying and resolving environment-specific issues early in the development process.
    • Production Readiness: Building applications with Docker from the beginning makes them production-ready sooner. The container image can be deployed to any system that has a Docker runtime.
    • Database Services: Docker simplifies the process of running database services, such as PostgreSQL and TimescaleDB, on a per-project basis. Instead of installing and configuring databases directly on the local machine, developers can spin up isolated database containers.
    • Isolation: Docker provides another layer of isolation beyond virtual environments for Python packages. Containers encapsulate the entire application environment, including the operating system dependencies. This prevents conflicts between different projects and ensures that each application has the exact environment it needs.
    • Portability: Once an application is containerized, it can be deployed anywhere that has a Docker runtime, whether it’s a developer’s laptop, a cloud server, or a container orchestration platform.
    • Reproducibility: Docker ensures that the application runs in a consistent environment, making deployments more reliable and reproducible.
    • Key Components:
    • Docker Desktop: This is a user interface application that provides tools for working with Docker on local machines (Windows and Mac). It includes the Docker Engine and allows for managing containers and images through a graphical interface.
    • Docker Engine: This is the core of Docker, responsible for building, running, and managing Docker containers.
    • Docker Hub: This is a cloud-based registry service where container images can be stored and shared publicly or privately. The tutorial mentions pulling Python runtime images and TimescaleDB images from Docker Hub. Images are identified by a name and a tag, which typically specifies a version.
    • Dockerfile: This is a text file containing instructions that Docker uses to build a container image. The Dockerfile specifies the base image, commands to install dependencies, the application code to include, and how the application should be run. The tutorial details creating a Dockerfile.web for the FastAPI application, which includes steps like downloading Python, creating a virtual environment, installing requirements, and running the FastAPI application using a boot script.
    • Docker Compose: This is a tool for defining and managing multi-container Docker applications. It uses a compose.yaml file to configure the different services (e.g., the FastAPI application, the database) that make up the application, along with their dependencies, networks, and volumes. The tutorial uses Docker Compose to run both the FastAPI application and the TimescaleDB database during local development. Docker Compose allows for defining build parameters (context and Dockerfile), image names, environment variables, port mappings, and volumes. The –watch flag can be used with Docker Compose to automatically rebuild the container when code changes are detected during development.
    • Container Image: A lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and configuration files.
    • Container: A running instance of a Docker image.
    • Usage in the Analytics API Project:
    • Containerizing the FastAPI Application: The tutorial focuses on creating a Dockerfile.web to containerize the FastAPI microservice. This ensures that the API can be deployed consistently across different environments.
    • Running TimescaleDB: Docker Compose is used to spin up a containerized instance of the open-source TimescaleDB for local development. This simplifies setting up and managing the database dependency.
    • Local Development Environment: Docker Compose, along with features like volume mounting, facilitates a stable local development environment that closely mirrors production. Volume mounting allows for code changes on the host machine to be reflected within the running container without needing to rebuild the image every time.
    • Production Deployment: The containerized FastAPI application is deployed to Railway, a containerized cloud platform. Railway uses the Dockerfile.web to build and run the application.

    In essence, Docker containerization provides a robust and efficient way to develop, package, and deploy the analytics API service and its dependencies, ensuring consistency and portability across different stages of its lifecycle. The source emphasizes using Docker early in the development process to prepare for production deployment.

    Railway Deployment of Analytics API

    Based on the source “01.pdf” and our conversation history, Railway is used as a containerized cloud platform to deploy the analytics API into production. The process involves several key steps to take the Dockerized application and run it in the cloud.

    Here’s a discussion of deployment on Railway based on the provided information:

    • Containerized Deployment: Railway is designed to deploy containerized applications. Since the analytics API is built using Docker, it is well-suited for deployment on Railway. The process leverages the Dockerfile.web created for the application, which contains all the instructions to build the Docker image.
    • railway.json Configuration: A railway.json file is used to configure the deployment on Railway. This file specifies important settings for the build and deployment process, including:
    • build command: This command tells Railway how to build the application. In this case, it points to the Dockerfile.web in the project directory.
    • watchPaths: These specify the directories and files that Railway should monitor for changes. When changes are detected (e.g., in the src directory or requirements.txt), Railway can automatically trigger a redeployment.
    • deploy configuration: This section includes settings related to the running application, such as the health check endpoint (/healthz). Railway uses this endpoint to verify if the application is running correctly.
    • GitHub Integration: The deployment process on Railway starts by linking a GitHub repository containing the application code. The source mentions forking the analytics-api repository from cfsh GitHub into a personal GitHub account. Once the repository is linked to a Railway project, Railway can access the code and use the railway.json and Dockerfile.web to build and deploy the application.
    • Environment Variables: Railway allows for configuring environment variables that the deployed application can access. This is crucial for settings like the database URL to connect to the production Timescale Cloud instance. Instead of hardcoding sensitive information, environment variables provide a secure and configurable way to manage application settings. The tutorial demonstrates adding the DATABASE_URL obtained from Timescale Cloud as a Railway environment variable. It also shows setting the PORT environment variable, which the application uses to listen for incoming requests.
    • Public Endpoints: Railway can automatically generate a public URL for the deployed service, making the API accessible over the internet. This allows external applications or users to interact with the API.
    • Private Networking: Railway also supports private networking, allowing services within the same Railway project to communicate internally without being exposed to the public internet. The tutorial demonstrates how to make the analytics API a private service by deleting its public endpoint and then accessing it from another service (a Jupyter container) within the same Railway project using its private network address. This enhances the security of the analytics API by restricting access.
    • Health Checks: Railway periodically checks the health check endpoint configured in railway.json to ensure the application is running and healthy. If the health check fails, Railway might automatically attempt to restart the application.
    • Automatic Deployments: Railway offers automatic deployments triggered by changes in the linked GitHub repository or updates to environment variables. This streamlines the deployment process, as new versions of the application can be rolled out automatically.

    In summary, deploying the analytics API on Railway involves containerizing the application with Docker, configuring the deployment using railway.json, linking a GitHub repository, setting up environment variables (including the production database URL), and leveraging Railway’s features for public or private networking and health checks. Railway simplifies the process of taking a containerized application and running it in a production-like environment in the cloud.

    FastAPI Python Tutorial: Build an Analytics API from Scratch

    The Original Text

    so apparently data is the new oil and if that’s the case let’s learn how we can control that data and store it oursel and then refine it ourself now what I really mean here is we’re going to be building out an analytics API service so that we can ingest a lot of data we can store a lot of data into our database that changes over time or time series data the way we’re going to be doing this is with fast API as our micros service this is going to be our API inpoint that has well mostly just one data model that we’re going to be ingesting from there we’re going to be using of course python to make that happen that’s what fast API is built in but we’re going to be storing this data into a postgres database that’s optimized for time series called time scale I did partner with them on this course so thanks for that time scale but the idea here is we want to be able to keep track of time series data time scale is optimized postgres for Time series data it’s really great I think you’re going to like it now we’re also going to be using Docker in here to make sure that our application is containerized so we can deploy it anywhere we want and we can use the open-source version of time scale to really hone in exactly what it is that we’re trying to build out before we go into production which absolutely we will be now the idea here is once we have it all containerized we’ll then go ahead and deploy it onto a containerized cloud called Railway which will allow us to have a jupyter notebook server that can connect to our private API inpoint our private analytics server altogether now all of this code is open source and I did say it’s done in fast API which really means that we’re going to be writing some fairly straightforward python functions that will handle all of the API inp points and all that the thing that will start to get a little bit more advanced is when we do the data modeling and specifically when we do the querying on the data modeling now these things I think are really really fascinating but I ease you into them so I go step by step to make sure that all of this is working now I do want to show you a demo as to what we end up building at the end of the day we got a quick glance at it right here but I want to go into it a little bit more in depth now I do hope that you jump around especially if you know this stuff already and if you come from the D Jango world the models that we’re going to be building I think are a little bit easier to work with than Jango models there’s less to remember because they’re based in pantic which is what SQL models is based in as well which allows you to write just really simple models it’s really cool it’s really nice to do so those of you who are from D Jango this part will be very straightforward fast API itself is also very straightforward and a really really popular tool to build all of this out now the nice thing about all of this is all the code is open source so you can always just grab it and run with it and deploy your own analytics API whenever you want to that’s kind of the point so if you have any questions let me know my name is Justin Mitchell I’m going to be taking you through this one step by step and I really encourage you to bounce around if you already know some of these things and if you don’t take your time re-watch sections if you have to some of us have to and that’s totally okay I know I had to repeat these sections many time to get it right so hopefully it’s a good one for you and thanks for watching look forward to seeing you in the course the point of this course is to create an analytics API microservice we’re going to be using fast API so that we can take in a bunch of web traffic data on our other services and then we’ll be able to analyze them as we see fit and we’ll be able to do this with a lot of modern technology postgres and also specifically time scale so we can bucket data together and do aggregations together let’s take a look at what that means now first and foremost at the very end we are going to deploy this into production and then we will have a jupyter notebook server that will actually access our private analytics API service this is completely private as in nobody can access it from the outside world it’s just going to be accessible from the resources we deem should be accessible to it which is what you’re seeing right here once we actually have it deployed internally and private we can send a bunch of fake data which is what’s happening this is pretending to be real web events that will then be sent back to our API as you see here so this is the raw data so from this raw data we will be able to aggregate this data and put it into a bulk form that we can then analyze now of course this is just about doing the API part we’re not going to actually visualize any of this just yet but we will see that if I do a call like duration 2 hours I can see all of the aggregated data for those two hours now this becomes a lot more obvious if we were to do something like a entire month and we can actually see month over month data that’s changing but in our case we don’t have that much data it’s not really about how much data we have it’s much more about how to actually get to the point where we can aggregate the data based off of time based off of Time series that is exactly what time scale does really well and enhances Post cres in that way so if we actually take a look at the code the code itself of course is open source feel free to use it right now you can actually come into the SRC here into the API into events into models you can see the data model that we’re using if you’ve used SQL model before this will actually give you a sense as to what’s going on it’s just slightly different because it’s optimized for time series and time scale which is what time scale model is doing now if you’ve used D Jango before this is a lot like a d Jango model it’s just slightly different by using SQL model and something called pantic don’t don’t worry I go through all of that and you can skip around if you already know these things but the point here is that is the model we are going to be building to ingest a lot of data after we have that model we’re going to be able to aggregate data based off of all of that which is what’s happening here now this is definitely a little bit more advanced of an aggregation than you might do out of the gates but once you actually learn how to do it or once you have the data to do it you will be able to have much more complex aggregations that will allow you to do really complex you know averaging or counting or grouping of your data all of it is going to be done in time series which is I think pretty cool now when we actually get this going you will have a fully production ready API endpoint that you can then start ingesting data from your web services I think it’s pretty exciting let’s go ahead and take a look at all of the different tools we will use to get to this point let’s talk about some of the tools we’re going to use to build out our analytics API first and foremost we’re going to be using the fast API web framework which is written in Python now if you’ve never seen fast API before it’s a really great framework to incrementally add things that you need when you need them so it’s really minimal is the point so there’s not a whole lot of batteries included which gives us all kinds of flexibility and of course makes it fast so if you take a look at the example here you can see how quick we can spin up our own API that’s this right here this right here is not a very powerful API yet but this is a fully functioning one and of course if you know python you can look at these things and say hey those are just a few functions and of course I could do a lot more with that that’s kind of the key here that we’re going to be building on top of now if you have built out websites before or you’ve worked with API Services before you can see how easy it is to also do the URL routing of course we’re going to go into all of this stuff in the very close future now of course we’re going to be using Python and specifically Python 3 the latest version is 3.13 you can use that 3.13 3.14 you could probably even use 3.10 Maybe even 3.8 I’m going to most likely be using 3.12 or 13 but the idea here is we want to use Python because that’s a dependency of fast API no surprises there probably now we’re also going to be using Docker now I use Docker on almost all of my projects for a couple of reasons one Docker locally emulates a deployed production environment it does it really really well and you can actually see this really soon but also we want to build our applications to be production ready as soon as we possibly can Docker really helps make that happen now another reason to use Docker is for database services so if you just want a database to run on a per project basis Docker makes this process really easy and so of course we want to use a production ready database and we also want to use one that works with fast API and then is geared towards analytics lo and behold we’re going to be using time scale time scale is a postgres database so it’s still postres SQL but it’s optimized for time series now yeah I partnered with them on this series but the point here is we’re going to be building out really rich robust time series data and fullon analytics right that’s the point here we want to actually be able to have our own analytics and control everything so initially we’re going to be using Docker or the dockerized open- source version of time scale so that we can get this going then as we start to go into production we’ll probably use more robust service is from time scale directly now we’re also going to be using the cursor code editor this of course is a lot like VSS code I am not going to be using the AI stuff in this one but I use cursor all of the time now it is my daily driver for all of my code so I am going to use that one in this as well now I also created a package for this series the time scale DB python package so that we can actually really easily use fast API and something called SQL model inside of our fast API application with the time series optimized time scale DB that’s kind of the point so that’s another thing that I just created for this series as well as anything in the future that of course is on my private GitHub as far as the coding for entrepreneurs GitHub all of the code for this series everything you’re going to be learning from is going to be on my GitHub as well which you can see both of those things Linked In the description so that’s some of the fundamentals of the tooling that we’re going to be using let’s go ahead and actually start the process of setting up our environment in this section we’re going to set up our local development environment and we wanted to match a production or a deployed environment as closely as possible so to do this we’re going to download install Python 3 we’re going to create a virtual environment for python projects so that they can be isolated from one another in other words python versions don’t conflict with each other on a per project basis then we’re going to go ahead and do our fast API hello world by installing python packages and then of course we’re going to implement Docker desktop and Docker compose which will allow for us to spin up our own database that postgres database that we talked about previously and of course it’s going to be the time scale version The Open Source version of that once we have our database ready then we’ll go ahead and take a look at a Docker file for just our fast API web application so that that can also be added into Docker compose and again being really ready to go into production then we’ll take a look at the docker based fast API hello world now all of this you could skip absolutely but setting up an environment that you can then repeat on other systems whether it’s for development or for production I think is a critical step to make sure that you can actually build something real that you deploy for real and get a lot of value out of it so this section I think is optional for those of you who have gone through all of this before but if you haven’t I’m going to take you through each step and really give you some of the fundamentals of how all of this works just to make sure that we’re all on the same page before we move to a little bit more advanced stuff and advanced features so let’s go ahead and jump in to downloading installing Python 3 right now we’re now going to download install Python and specifically python 3.13 or 3.13 now the way we’re going to do this is from python.org you’re going to go under downloads and you’re going to select the download that shows up for your platform now there are also the platforms shown on the side here so you can always select one of those platforms and look for the exact version that we’re using which in my case it’s Python 3.13 and you can see there’s other versions of that already available that you can download with now this is true on Windows as well but the idea is you want to grab the universal installer for whatever platform you’re using that’s probably going to be the best one for you this isn’t always true for older versions of python but for newer ones this is probably great so we’re going to go ahead and download that one right there and if course if you are on Windows you go into windows and you can see there’s a bunch of different options here so pick the one that’s best for your system now if you want a lot more details for those of you who are windows users consider checking out my course on crossplatform python setup because I go into a lot more detail there now the process though of installing python is is really straightforward you download it like we just did this is true for Windows or Mac and then you open up the installer that you end up downloading and then you just go through the install process now one of the things that is important that it does say a number of times is use the install certificates we’ll do that as well just to make sure that all of the security stuff is in there in as well so I’m going to go ahead and agree to this installation I’m going to go ahead and run it I’m going to put my password in I’m going to do all of those things as you will with installing any sort of software from the internet now I will say there is one other aspect of this that I will do sort of again in the sense that I will install python again using Docker and specifically in Docker Hub so yeah there’s a lot of different ways on how you can install Python and use it but both of these ways are fairly straightforward okay so it actually finished installing as we see here and it also opened up the finder window for me for that version if you’re on Windows it may open this folder it might not I haven’t done it in a little while but the idea is you want to make sure that you do run this installation command here which if you look at it is really just running this pip install command we’ll see pip installing in just a moment but that’s actually pretty cool so we’ve got pip install uh you know the certificate you know command basically to make sure all that security is in there now once you install it we just want to verify that it’s installed by opening up another terminal window here and run something like Python 3 – capital V now if you see a different version of python here there’s a good chance that you have another version already installed so for example I have python 3.12 installed as well you can use many different versions of python itself all of these different versions are exactly why we use something called a virtual environment to isolate python packages again if you want a lot more detail on this one go through that course it goes into a lot more detail on all different kinds of platforms so consider that if you like otherwise let’s go ahead and start the process of creating a virtual environment on our local machine with what we’ve got right here part of the reason I showed you the different versions of python was really to highlight the fact that versions matter and they might make a big issue for you if you don’t isolate them correctly so in the case of version 3.12 versus 3.13 of python there’s not going to be that much changes in terms of your code but what might have a major change is the thirdparty packages that go in there like what if fast API decides to drop support for python 3.12 and then you can’t use that anymore that’s kind of the idea here and so we need to create a virtual environment to make that happen which is exactly what we talked about before so back into cursor we’re going to go ahead and open up a new window inside of this window we’re going to go ahead and open up a new project so I want to store my projects in here so I open up a new project I find a place on my local machine as to where I’m going to store it and we’re going to call this the analytics API just like that and I’ll go ahead and open up this folder okay so normally with cursor it’s going to open you up to the agent I’m not going to use the agent right now I’m just going to be using just standard code and we’re going to go ahead and first off save the workspace as the analytics API we’ll save it just like that then I’m going to go ahead and open up the terminal window which you can do by going to the drop down here or if you learn the shortcut which I recommend you do it can toggle it just like I’m doing okay so the idea here is we want to just of course verify that we have got Python 3 in there you can probably even see where that version of Python 3 is stored this actually shouldn’t be that different than what you may have saw when we installed called the certificates here but the idea of course is we’re going to use this to activate our virtual environment so I’m going to go ahead and do python 3.12 or rather python 3.3 13 and then do m venv v EnV so this is the Mac command for it this also might work on Linux if you’re on Windows it’s going to be slightly different which is going to be basically the absolute path to where your python executable is so it’s going to be something like that then- MV andv V andv okay so this is another place where if you don’t know this one super well then definitely check out the course that I have on it uh which was this one right here so just go ahead and do that okay so the idea is now that we’ve got this virtual environment all we need to do is activate it now the nice thing about modern text editors or modern code editors is usually when you open them up they might actually activate the virtual environment for you in my case it’s not activated so the way you do it is just by doing Source VMV and then b/ activate this is going to be true on Linux as well Windows is going to be slightly different unless you’re using WSL or you’re using Powershell uh those things might have a little different but more than likely Windows is going to be something like this where it’s uh VMV scripts activate like that where you put a period at the beginning that will help activate that virtual environment and so now what we do is we can actually do python DV notice the three is not on there and here it is and of course if I do which python it will now show me where it’s located which of course is my virtual environment and so this is the time where we can do something like python DM pip or just simply pip both of those are the python package installer and we can do pip install pip D- upgrade this is going to happen just on my local virtual environment it does not affect pip on my local machine which we can check by doing pip again if I do that notice that it’s not working right so it works in here where I do pip but it does not work in my terminal window nonactivated terminal window if I do python 3-m pip um then I’ll get it or rather just pip that will give me that actual thing and it’s showing me where it’s being used just like that same thing if I came back in to my virtual environment scrolled up a little bit it will show me something very similar here’s that usage if I do the python version it will show me the same sort of thing that we just saw but it’s going to be based off of the virtual environment just like that so that’s the absolute path to it of course it’s going to be a little bit different on your machine unless you have the same username as I do and you stored it in the same location but overall we are now in a place to use this virtual environment so let’s see how we can install some packages and kind of the best approach to do that now let’s install some pyth packages it’s really simple it’s a matter of pip install and then the package name that is how the python package index works if you’re familiar with something like mpm these are very similar tools but the idea here is if you go to p.org you can search all of the different P published python packages and pip install can install them directly from there so if we did a quick search for fast API for example we can see here is the current version of fast API that’s available and this is how we can install it now the key thing about these installations is just like many things there’s many different ways on how you can do this there are other tools out there like poetry is another tool that can do something like poetry ad I believe that’s the command for it but the idea here is there’s a lot of different ways on how you might use these different package names I stick with the built-in modules cuz they are the most reliable for the vast majority of us now once you get a little bit more advanced you might change how you do virtual environments and you also might change how you install python packages but the actual python you know like official repository for all of these different packages is piie and they still say pip install so it’s still very much uh a big part of what we do okay so the idea here now is we need to install some packages so how we’re going to do this is we’re going to go ahead and once again I’m going to open up my project and in my case I actually closed it out mostly so I can show you the correct way to install things here’s my recent project here with that workspace all I have is a virtual environment and that code workspace in there now if I were to toggle open the terminal it may activate the virtual environment it may not so the wrong way to do this is to just start trying to do pip install fast API in this case it says commands not found the reason this is the wrong way is cuz I don’t have the virtual environment activated so I have to activate that virtual environment just like that if I need to manually do it if for some reason the terminal is not automatically doing it then we can do the installation so we can go ahead and do pip install fast API and hit enter and just go off of that and that’s actually well and good except for the fact that I don’t have any reference inside of my project to the fast API package itself so I have no way to like if I were to accidentally delete the virtual environment I have no way to like recoup what I did so what we need to do then is we create something called requirements.txt once again this is another file that could be done in different ways and there are other official ways to do it as well but one of the things that’s nice about this is inside of this file we can write something like Fast API and then I can do pip install d r requirements.txt which will then take the reference from this file assuming that everything’s installed and saved and so once I save it I can see that that’s the case it’s now doing it this comes in with the versions then so what we do then is we can actually grab the version and say it’s equal to that version right there in which case I can run the installations now this is actually really nice because it kind of locks that version in place now if you go into the release history you could probably go back in time and grab a different version like 011 uh 3.0 if we do that so 0113 and then 0.0 I save that now I run that installation it’s going to take that old one and it’s going to install the things that are relative to that uh inside of my entire environment now in my case I actually want to use this version right here and a lot of times I would actually do another tool to make sure that this version is correct that other tool is called pip tools which I’m not going to cover right now but the idea here is we want to keep track of our requirements and fast API of course is one of them now within fast API we have something else called uvicorn that we will want to use as well this is often hand inand with using fast API so once again we see pip install uvicorn and you can just do something like that now there is something else with uvicorn that we might want to use and that’s called gunicorn so G unicorn and we do a quick search for that this is for when we want to go into production G unicorn and uvicorn can work together so once again I’ll go ahead and bring that in uvicorn and gunicorn they are um great tools for running the application in production but we usually don’t have to lock down their version I think once you go into production you need regular things then yeah you’ll probably want to lock down the version the more important one is probably fast API but even that we might not need to lock down our version at this stage like if you’re getting something from what I’m telling you right now then you probably don’t need to lock down the version yet if you already know oh I need to lock down the version then you’ll probably just do it anyway that’s kind of the point that’s why I’m telling you that um but the idea here is of course we’ve got all these different packages and then of course my package that is definitely a lot newer you do a quick search for it and it’s time scale DB it is not a very like there’s not that many versions of this package so this is another one that I’m going to go ahead and not lock down because I definitely don’t want that so within that package there is something called SQL model which is a dependency of the other package so it’s this one right here this one also doesn’t have that many versions itself uh but it’s pretty stable as it is and this one works well with fast API and you’ll notice that it’s powered by pantic and SQL Alchemy which means that we probably want to have those in there as well just so we have some reference to it so I’m going to go ahead and bring that in here with pantic and SQL Alchemy there we go so now that we’ve got all of the different versions here I can once again do pip install R requirements.txt and then I’ll be able to install all of the packages as need be now here’s the kicker this is why I’m showing you is because what you want to think of your virtual environment as just for the local environment you are not going to be using it in the production environment you’ll be doing a completely different one what’s interesting is my terminal now opens up that virtual environment and attempts to activate it which is pretty funny but I don’t actually have one yet so I’m going to go ahead and re bring it back now I’ve got that virtual environment back of course if you’re on Windows you might have to use something different here but I’m going to go ahead and reactivate it with bin activate and then we’ll go ahead and do PIV install r requirements. txt hit enter and now I’m getting all of those requirements back for this entire project this is going to come up again we will see it for sure because it’s really important okay so now we’ve got our requirements we’ve got our virtual environment it’s time to actually do our fast API hello world before we go much further I will say if there are file changes within the code it will be on the official GitHub repo for this entire series as to what we’re doing going forward so in the in other words in branches of start this is the branch we’re going to finish off with of course this one doesn’t have everything in it like we just did but it will and so that’s the start Branch that’s where we’re leaving off but as you see there’s license and read me those things I’m not going to show you how to do or the get ignore file uh they’re just going to show up in just a moment but now that we’ve got this let’s go ahead and actually create our first fast API application it is very straightforward we now want to do our fast API hello world so the way we’re going to do this is by creating a python module with some fast API code and just run it the key part of this is just to really verify that our package runs that it can actually go so to do this we’re going to go ahead and go back into our project here and of course I want to open up my terminal window if the virtual environment activates that’s great if it does not activate like that then we want to activate it manually every once in a while when you create a virtual environment you might see this where it automatically activates we hope that it will start to automatically activate I’ll mention as to when it might happen but the idea here is we want to activate that virtual environment so Source VMV bin activate or of course whatever you have on your machine then I’m going to go ahead and do pip install R requirements.txt it’s not going to hurt to run that over and over and over again right it never will because if it’s already installed it’s not going to do anything cool other than tell you that it’s already installed so the idea here is we want to make sure that it’s installed so we can actually run it the next question of course how do we run it well if we go into the fast API documentation and we go to the example we see here some example python code so it says create a file main.py with all this stuff so the question then is where do we actually put this and that’s going to be as simple as you could do it right here in main.py you can copy this code paste in here save it with command s which is exactly what I do a lot I won’t actually show you that I’m saving it I’ll just save it I just saved it like 10 times just there so now that we’ve got that we actually want to run this code right so how do we actually run it well going back into the docs again you scroll down a little bit you see that it says run it right here and so I’m going to go ahead and attempt to run it with fast API Dev main.py hit enter I get an error so there’s a couple things that I want to fix before this goes any further but the idea here is this may might make you think think that oh we didn’t install fast API correctly and in a way we didn’t we didn’t install it to use this particular command line tool it is a pce ma command line tool it’s not necessarily going to be there by default in this case it’s simply not there now in my case you could I could consider using this as my development server for fast API what I actually want to use is uvicorn because it’s closer to what I would use in production and it’s actually what fast API is using anyway as you look in the documentation you can scroll down a little bit it says we’ll watch for changes uvicorn running and all that so it’s basically uvicorn anyway so that’s what we want to do is we want to have uvicorn run our our actual main app so you use uvicorn then this is the command if you hit enter you should be able to see this now if you’re on Windows you might actually have to use waitress at this point waitress is just like uvicorn but that might be the one that you end up using we’ll talk about solving that problem in just a little bit when we go into dock but for now um I’m going to be using uvicorn and of course if you are on Windows go ahead and try that fast API standard that will probably work for you as well okay so the idea here is now we want to actually run this application with uvicorn and then the way we do that is we grab main.py here and then we use colon looking back into main.py we look for the app declaration for fast API which is just simply app and then we can do something like D- reload hdden enter oh that trailing slash should not be there so I’ll try that again we hit enter and now once again it says we’ll watch for these changes and it’s running on this particular Port just like what we see right in here great it just doesn’t show us this serving at and docs at and stuff like that as well is there’s not a production version of this just yet that we’ll use we will see it though okay great so now I can open this up with command click or just copy this URL you can always just control click and then open it up like that but the idea is hello world it’s running congratulations maybe this is the first time you’ve ever run a fast API application and it is up and running and it’s working on your local machine now don’t get too excited because you can’t exactly send this to anybody right so if I were to close out this server which we have a few options to do so one is just doing contrl C which will do that it just literally shuts it down another option is just to you know kill the terminal and then you can open up the terminal again in which case ours right now we need to then reactivate it and then we can run that command Again by just pressing up a few times we’ll be able to find it and there it is okay great so now we have it running what can we do we well we need to actually put it in a location that makes more sense than where it is right now main.py is in the root folder here this is basically something that no one ever does when you start building you know professionally you often put it inside of SRC main. P like that notice that I put that that slash in there that’s important because as soon as I hit enter it actually creates a folder for me with that new module which is where I’m going to go ahead and actually paste all of that code again and then I’ll just go ahead and delete this one with command backspace that allows me to delete it of course there’s other ways to delete it but there we go okay so now we can verify that fast API is running it’s working it’s ready to go so if you’re on Windows and you have Docker installed or if you just have Docker installed the next part is going to allow for us to build on top of this or the next half of this section is going to help us build on top of this so that we can actually use this code a little bit more reliably than we currently do let’s say you want to share this application with one of your friends and you want to help them set it up to run it what you’d probably tell them is hey download python from python.org make sure that you create a virtual environment install these requirements and then use uvicorn to run main.py and then you remember oh wait you also have to activate the virtual environment then install the requirements then run with u viacor then you might remember oh make sure you download python 3.13 because that’s the version we’re going to be using so there is some Nuance in just the setup process to make sure that this is working correctly that of course you and your friend could figure out by talking but it would be better if you could just give them something and it just worked docker containers are that something what we end up doing is we package up this application into something called a container image and we do it in a way very similar to what we’ve been doing which is download install the runtime that you want to use like Python 3 create a virtual environment activate that virtual environment install the requirements and then run your P your you know your application your fast API application so we will get to the point where we actually build out these things very soon but right now I want to just show you how you can run them how you can just use them by downloading Docker desktop so if you go to dock. and hit download Docker desktop for your platform you’ll be able to get the docker desktop user interface just like this but you will also more importantly get Docker engine to be actually able to run Docker containers to be able to create them and to be able to share them the key parts of using Docker now Docker itself can be really complex so I want to keep things as straightforward as possible so it’s very similar to what we did with python when we downloaded and installed it but we’re going to do that the docker way right now so at the point that you download it you’re going to go ahead and open it up on your machine and it’s going to look something like this UI right here now if for some reason this isn’t popping up and it’s just in your menu bar you might actually see that as soon as you exit out it might be something like this in which case you’ll just go to the dashboard and that will actually open it back up that’s just off of the recording screen for me right now which is why you’re seeing it this way but the idea here is we now have the docker desktop on our local machine now I also want to verify that this is working correctly because if I don’t verify it then it’s going to be hard to run in general so opening up the terminal I should be able to do Docker PS Docker PS really just shows me all of the things that are running on my machine we’ll come back to this in just a moment but the idea is if this runs an error you don’t have it done correctly the error will look something like that so just type in gibberish that’s the error right command not found in this case so I want to make sure that that’s there and I also want to make sure that Docker compose is there we’ll come back to Docker compose really soon but for now we’re just going to have those two commands now I’m going to be giving you a very minimal version of using Docker and learning how to use Docker over the next few videos or next few parts of this section now the reason for that has to do with the fact that we need it for a very stable local development environment but also so we can put push this into production so let’s actually take a look at how we can actually use a Docker container image right now so on Docker desktop there’s this Docker Hub right here there’s also Docker Hub if you go to hub. deer.com both of these things are just simply Docker Hub and you can search for other container images it’s not surprising that if you look at the docker Hub on Docker desktop versus the website they look basically the same that’s of course makes a whole lot of sense now in our case we’re going to go ahead and do a quick search for python we want to do the python runtime so when we went to python.org we went to downloads and you could have gone to one of your platforms here and you could actually grab a specific version of python so this is true on dockerhub as well but instead of saying download it’s just a tag so you just go into tags here and you can find different versions of python itself you can get the latest version which often is a good idea but just like what we talked about with the actual versions of you know packages or python software that we’re using a lot of times you want to use a very specific version to make sure that all of these things work as well so going back into uh you know the dockerhub we’ll do a quick search for Python 3.13 and so what we see in here is python 3.13 the one we’ve been using here’s 3.1 3.2 and of course if we go back into you know actual python releases we can see there’s 3.12 right there and and it’s the same one exact same one is through Docker one is directly on your local machine so the one through Docker is going to work on Linux Windows Mac it’s going to work crossplatform it’s really great that it does that but I actually don’t want to use the version that I already have on my machine I want to use an a really old version so Python 3.16 and we’ve got relevant and it has vulnerabilities there’s a lot of security concerns with using this old one for our production systems but for this example there’s no con concerns at all we can delete it it’s really simple to do so the way we actually get this thing working is by copying one of these pole commands so we can scroll on down I’m going to go ahead and just grab the one that is 3.6.1 I’m going to go ahead and copy this command here go into my terminal and I’ll go ahead and paste that in with that pole command so this is going to go ahead and download python 3.1 or 3.6.1 5 directly from dockerhub it downloads it in the image or the container form so we can now run this with Docker run python Colin 3. 6.15 and hit enter and it immediately stops so the reason it immediately stops is because Docker itself has a lot of features it can do a lot of things one of those things is we can do an interactive terminal for this which is- it with that same Docker container image and we can hit enter there now it actually opens up python for me it’s quite literally in the python shell much like if we were to open up a new terminal window here and type out Python 3 in there that is also a python shell right the difference here is if we actually clear this out I can just run it at any time and I can change the version there’s also another difference that says Linux up here here and Darwin down here that’s because on my local machine I’m on on a Mac but in the docker container I’m on a Linux so Docker containers are basically Linux with a bunch of things already set up now the other cool thing about this is I can exit out of here of course and then I can run python 3.7 and now it says it can’t find it so it’s going to go ahead and download it for me all the while on my local machine if I try to do python 3.7 it’s not available python 3.6 not available very similar to using a virtual environment but it’s another layer of isolation and it allows us to have these packaged run times that we can use at any time which is fantastic so the other part about this of course is that inside of Docker desktop we can delete these old ones that we don’t want to use and you’re going to want to get in this habit sometimes to make sure that you don’t have these old versions of python on your machine because they’re rather large that’s why there’s other versions or other tags of it right so if you go back into Docker Hub you’ll see that there’s this slim Buster look how much smaller of a an actual python project it is it’s way smaller so you can just grab that version and then you can come in here and do the same thing Docker run-it that version right there this is going to download it it’s going to be much smaller which means that it probably doesn’t have nearly as many features out of the gates it’s still on Linux but we can do you know all sorts of python things in here as you would and then you can then see inside of Docker desktop all of the things that are downloaded in here and then the slim Buster is quite a bit smaller than these other ones and so another thing about Docker desktop that’s really nice is you can come in here you can search for Python and you could do a quick search which gives us all of these ones that are using the python image all of these are running versions of the application which you can stop and delete which you would want to do and then you go into your images you can also delete these as well just like that which gives you back 2 gigs of space which you’re definitely going to want to get in the habit of doing in which case then when you want to run it again in the future it will just go ahead and redownload that image right so it’s really meant to be ephemeral like this you’re meant to think of Docker as something that is temporary so you can add it remove it you know run it when you need to and then delete it when you don’t right and so every once in a while you’ll see hey you can’t delete it because it’s running so then that means you just go into the containers here these are ones that are running you just go ahead and delete that you go back into the images and then you can delete it worst case scenario you just literally shut down Docker desktop open it back up and you can see what’s running with Docker containers in here or you can use Docker PS to see if anything’s running right now I don’t have anything running which is very clear in the desktop as well as my terminal okay so we’ll continue to use this I don’t expect you to know all of Docker at this point you shouldn’t know all of Docker at this point instead you should just be able to benefit from using Docker which will allow for us to do do all sorts of Rapid iteration and isolate our projects from each other on a whole another level well beyond what virtual environments can do and it will allow us to have as many database instances as we might want which is what we actually want to do very soon by using time scale itself so that is the docker desktop and some things about Docker compos now I actually want to get fast API ready we want to actually build our own container for fast API or at least the makings of it then we’ll go ahead and take a look at how we can develop with Docker itself we are now going to bundle our fast API application as a container the way we do this is by writing some instructions in a specific syntax so that Docker can build the container from those instructions and our code so this is called a Docker file so we’ve got a production Docker file for fast API the reason we’re doing it now is because the sooner we can have our project optimized for production the sooner we can actually go into production and share this with the world or anyone who has a Docker runtime those things are the key part of this so the actual Docker file we’re going to be using is on my blog so it’s here this is a production version that you can just go ahead grab and run I’m going to make a modification to it for this project but that’s the general idea here here so I’m going to open up my project now I’m actually going to copy these steps you don’t have to copy them but I just want to lay them out so I know where I want to go with this instruction this Docker file so if I go into Docker file like this no extension I’m just going to go ahead and paste this out and these are the instructions I wanted to do first off is download Python 3 create a virtual environment install my packages and then run the fast API application those four steps are really what I want to have happen now the way this works is very similar to like when we ran our python application itself right when we ran it just with Docker itself we can see that it is in Linux and here is our docka application so it really starts at this tag here the container and its tag so the way we find this of course is by going into Docker Hub looking for a container image like we did looking for the version that we want to use and then also the tag that we want to use which of course is going to be 3 uh 13. 2 which we had locally that’s the one we downloaded in the first place we’re going to use that same one for our Docker container so the big difference here though is we don’t want to use the big one because it’s massive it’s massive all across the board we want to use a small one which is called slim Bullseye so that’s the one we actually want to use and so the idea here is very similar to what we have with the slim Buster from the original python download in the docker container we’re going to do something very similar to this the way it works is we say from this is the docker um you know Syntax for it the actual image that we want to use which we could use latest this might be 3.13 this might be 3.14 this might be 3.15 we want to be very specific about the version we’re going to use which again 3.13 and then I think it was2 is the Baseline one if you do slim Bullseye slim blls eye like that it will be a much smaller image as soon as you do a much smaller image we lose some of the default things that might come within Linux itself when we lose that that means we need to also install our Linux environment so the next step might be you know set up Linux OS packages right so if you were going to deploy this directly on a Linux virtual machine you would need to do that same idea that same concept here now I could go through all all of these steps with you or we can just jump into the blog post and copy it because this is not a course about Docker so I’m going to go ahead and copy what’s in this blog post and we’ll go ahead and bring it right underneath those comments and I’m going to go ahead and modify this a little bit to make it work for our project the first thing is notice that I’ve got this argument in here we actually don’t need that argument we’re just going to go and stick with that single one right there and then I’ll just go line by line and kind of explain what’s going on first off we create that virtual environment no big deal this time it’s going to be in a specific location this is like as if we were setting up a remote server we would want our virtual environment in one location because that one server is probably going to have only one virtual environment for this particular application which is what we’re doing with our container this one container is going to only have one python project but we still want to use the virtual environments to isolate any python that might be on the operating system itself so now that we’ve got this virtual environment here this little Command right here makes it easy so that we don’t have to activate it each time it’s just activated and so we can run the PIP install command and upgrade pip we do python related stuff here’s the OS dependencies for our mini virtual machine or our Mini Server here we can then install things like for postres or if you’re using something like numpy you would have other installations in here as well or if you wanted to have git in here you could install that as well so a lot of different things that you can do on the OS level and that’s it right there so that’s one of the cool things about using Docker containers themselves is you also control the operating system not just the code so what we see now is we are making a directory in this operating system this mini one called code we have a working directory in here also called code in other words we are just going to be working inside of there then we copy our requirements file hey what do you know requirements.txt into an absolute location this doesn’t matter we could actually not have it in an absolute location but it is nice that it is in one because later when we need to install install it that’s what we do and so what we see here is it copies the code into the container’s working directory in other words it’s copying main.py into a folder called code no big deal now we’ve got a few other things that we probably don’t need in here um and then the final one is actually running a runtime script A bash script to actually run this application and then removing some of the old files to reduce image size and then finally running the actual script itself so what I want to do before I say this is done is I actually want to create something called boot slocker run. SH now the reason I’m doing this is because all of us are going to need to know what it is that’s going on with our application at any given time and this is what it’s going to be so we first off make this a sh file or bin bash file so that the Linux virtual machine will be able to run it the Linux container will be able to run it then we want to CD into the code folder we also want to activate the virtual envir M then we have runtime and variables to actually run our application which is going to be an SRC main app and that is a little bit different than what we’ve got here so I’m going to go ahead and just keep it in as main app just for now we might change this in the future but this is going to be our script to actually run our application and so to use that script what we do then is we are going to go ahead and copy theboot slocker run. sh and we’re going to go ahead and copy that that into opt runsh so instead of this script here we’ve got a new one and that’s going to be the name of it then we want to do the Cho mod to make it executable then that’s what we’re going to end up running is that script right there and make sure it has a.sh and that’s it so of course I still need to test and make sure that this is working it’s not necessarily working already so I will have to make some changes in here just to make sure that that’s the case that’s not something we’re going to worry about yet the main thing here is that we have a Docker file and that we’re going to be able to build it really really soon this Docker file most likely won’t change that much if anything the blog post will give the updates to the change or the actual code itself will have this Docker file in here that’s the key of Docker files they don’t need to change that much the only thing that will change most likely would be the python version that you end up using over time and then the code that’s going in but everything else related to this is probably going to remain pretty static that’s also why in the blog post the way we run the actual application itself is written right in line it’s actually a script that’s created right in line but having it external makes it go a little bit faster than what we have right here now I realize some of you aren’t fully ready to learn the inss and outs of Docker but we want to be as close to production as possible which is why that blog post is exists and it’s why you can also just copy this stuff personally I think looking at this it’s hopefully very clear as to the steps that need to happen to recreate our environment many many many times over so that it’s a lot easier to share it whether it’s with somebody else who has a Docker runtime or with a production system over the next few parts we’re going to be implementing the docker compose based fast API hello world but before we get there we need to still see some things about Docker just so you have some Foundation as to what’s happening in Docker compose so jumping back into our project here we’ve got this Docker file these are the instructions to set up the tiny little virtual machine or the tiny little server to run our application right it bundles everything up on our code so we need to actually be able to build out our application so there’s really two commands for this I’m going to put them in our read me here so we’ll go ahead and do Docker in here and we’ll go ahead and do the two commands and it’s Docker build and then it’s going to be Docker Run Okay so build is what it sounds like it’s going to build our bundled container image and the way we do it is we tag it something hey these tags what does that remind you of hopefully it reminds you of a few things a tag like this and in our terminal a tag like this right so we need to tag it in the same way it’s going to be tagged on our account if we were pushing it into dockerhub which we’re not but we still need to tag it so either way we’re going to build it and tag it then we want to say hey where are we building this file well we’re going to build it in the local folder which is just period there now we also want to specify the docker file we’re going to use which is how we do it like that now if you don’t specify the docker file it’s just going to go based off of that as in no other Docker file the reason being is you can have multiple Docker files like docker file. web in which case you would want to specify something like Docker file. web we aren’t doing that we’re just using the one single Docker file but it’s important to know about if you were going that direction on how you go about building it once you build it then you run it hey what do you know build it then run now we’ve already seen that command a little bit as well too which was Docker run and then that python command like this now the thing about this run command is there are a bunch of arguments that can go in here there can be the- it argument like we saw with python we ar going to spend any time with the arguments in here in fact this is all we want to see for our arguments at this point because we actually want to use Docker compose because it will do all of this for us as we’ll come to see so the idea here is we want to build out the container let’s go ahead and do that in the root of our project right next to Docker the docker file itself I’m going to go ahead and run this command and it’s going to go ahead and build this out for me now in my case it actually went really fast cuz there’s this cache in here I actually did test this and built it already so if I go into Docker desktop I can see the image that was built in here which will be my image my actual Docker analytics app image in here so I have got a few of them in here and the reason that I have a few is because I was testing this out but of course you can delete these just like we’ve seen before in this case we’ve got one that I can’t actually delete yet so go back into my containers in here and let’s go ahead and get rid of the search bar and I’m going to go ahead and stop all of my containers and delete all of them and then I’ll go back into my image here and I’m going to delete this one as well just so I can see it being built out I also wanted to show you that’s what you do if you want to get rid of it whether it’s your app or someone else’s and so once again it still has cash in here so it’s going really really fast which is super nice but sometimes it will take a little bit longer than that now there’s this other Legacy warning that we’ve got in here CU one of the docker files needs to be changed a little bit to having equals instead of that space bar there so I’m going to go ahead and do that and I’m going to try and build it again cool so it builds really fast great so now I’ve got a container image that I can run it’s not one that’s public it’s only on my local machine it goes public when I push it into dockerhub which like I said we’re not going to do at this point so to run it I just go ahead and do Docker run and then whatever that tag is in our case that tag is anal analytics API which we can also verify inside of our images in here there’s the the tag itself well actually it’s the name of the container with latest in here so it actually def defaults to latest that’s the tag that’s how you actually can do it it’s going to default to that if you don’t specify so if you were to specify one it would just be like that again we’ll do some of this stuff with Docker compose in just a little bit so now all I really want to do is verify that I can run this application by doing Docker run and there it is it’s now running the application itself if I try to open this application it’s not going to work we will make it work in a little bit the reason it’s not working is because of how Docker Works itself everything needs to be explicit to make it work so in order for it to run on a specific Port we also have to let Docker know about that the application itself doesn’t have to let Docker know about that we’ve got a lot of control over how all of that works so what I want to see now is how to do these two things inside of Docker compost off the video I went ahead and stopped that container and deleted the built image so that I could then run the command Docker run analytics API which of course failed it’s not locally and it’s also not on Docker Hub so it just can’t run it so that’s a little bit of an issue that actually is overcome by Docker compose so if we go in here and do compose diamel we can actually start specifying the various Services we might need so the very first key in this yaml is going to be services in inside of there are going to be all of the container images we might want to use like a database or our app in this case we’ll go ahead and just work off of our app the nice thing about modern tooling is a lot of times you can just run Individual Services right inside of the ammo file it also might depend on an extension that I have installed but the point here is we want to make sure that we have these nested key value pairs Here app is just what I’m calling it I could call it SRC I could call it web app I could call it a whole lot of things what we call it is going to make more sense or it’s going to matter more later when we use something a little bit different than just app okay the idea here in then is we’ve got our app and now we want to specify the image name so what do we want to call it well we could call it the same image name as in analytics API and this time we can say something like V1 okay great next up what we can do is Define the build parameters here and that is going to be our context which is going to be the local folder this should remind you of this dot right here so that’s the context that we’re looking at and then the next part is specifying the docker file we’re going to use relative to the composed file so we’ll set Docker file and it’s going to be Docker file just like that now if we had this as Docker file. web which you might do at some point then you would just change this to web as well let’s actually keep it like that so it’s a little bit more clear as to what’s going on in here now if we do change it one note I will change is inside of my little command here I would want to change that one as well just in case I wanted to build it individually okay so the idea now is I’ve got my Docker image and some build parameters now you can actually add additional build parameters in here this is all we’re going to leave though is the context and the docker file in part because it actually matches what we did to build it in the first place but the other part is well we probably don’t need a whole lot of context just to build it because that’s what the docker file is for the docker file has a lot of those context things that you might need to build it now to run it it’s a whole another story so to run it what we did was well actually if I just leave it like this and try to run it we’ll see something interesting so I’m going to go ahead and clear this out and then do Docker compose up hit enter what this will do is it starts to run it but it actually ends up building the application itself and then it goes to run it which it is now running and of course if I were to go to this actual place it will still not work right okay so we’ll get to that in a second but it built and ran it basically the same way but now we specify the image here and so the command I need to remember is just simply Docker compose up and if I want to take it down I can open up another terminal window and do Docker compose down that will stop that container application from running which I think is pretty nice okay and then we could also go into Docker desktop we can take a look at the images in here and look for our analytics API and what do you know there’s that V1 tag in there as well uh which makes things a little bit nicer and easier to see what’s going on so the image was built with that tag is kind of the point okay great so now what we want to do though is we want to be able to actually access this inpoint this URL here so the way it works with Docker this is true whether it’s Docker compos or just straight Docker itself as in this run command we actually need a specify ports so part of the reason that I actually had ports in the first place like inside of my Docker run is so that I can specify a different port right so this port value this environment variable we want to play around with this in just a moment so the way we play around with this is going to happen uh next to Ports so before I actually do the ports let’s go ahead and change the environment variable so we’re going to go ahead and come in here and not to use entry point but

    yeah the rather use environment and set key value pairs so the port value you want to use so let’s go ahead and use port value 8,000 and2 I’m not going to do the ports just yet we’ll just change the port and then I’ll go ahead and do Docker compose up notice that the environment variables have changed if I try to open it once again it still is not accessible okay so this is in part because what I changed was runtime arguments this little thing changed a pretty big change inside of the application because of this environment variable or more specifically this one right here so inside of our composed. gaml we’ve got this port value in here now the reason I’m mentioning this is because at some point we will have a database URL in here and we’ll be able to pass in what that argument is another thing that we can do inside of using you know just Docker compose is do an EnV file and you can do something more like EnV and which case you could just come in here and Dov and this being Port like 801 so right now I have conflicting environment variable values so let’s actually see what that looks like I’m going to go ahead and call Docker compose down and then we’ll go ahead and bring it back up in just a second okay took a few seconds for that to finish but now if I do Docker compose up it’s still Port 802 in other words these hardcoded environment varibles override the environment variable file so if I were to get rid of that Port it would still be uh you know whatever the environment variable value is uh so for now I’ll just leave them the same so there’s not any confusion but the idea here is we have the ability to have environment variable files now we can add more of them by just using another line here you can do something like em. sample or any other kind of environment variable files and of course it has to be in this format of key equals some value okay great so the final step here is really just to expose this port so I’m going to go ahead and bring this down and the way this works is we declare ports in here and our this is super cool so what it shows us is our host port and our container Port so if I click on that we’ve got host Port of 8080 goes to container Port of 80 if you’re not familiar with what the container Port is that is going to be this number right so Port 802 and then the host Port is our system what port do we want to access Port 802 on this so this sometimes doesn’t work as intended so we’re going to go ahead and try it out we’ve got Docker composed up here’s 802 and then we exposed Port 8000 so inside of my Local Host here I’m going to try to do port 8080 and see if I can connect so depending on how your system ends up being designed this might work and it also might not work uh so what you want to typically do is default to your local host port to that Port itself the reason for this has everything to do with how our port and host and all this sort of stuff is being mapped it sometimes works very seamlessly sometimes does not basically the general rule of thumb is if you have the ability to use the same port you should but the reason I wanted to show you all these different ports here is because this is kind of confusing it doesn’t really show you what’s going on you just need to remember the first Port is going to be our Systems Port the second Port is going to be the port that the docker container app is running on great so now we can go ahead and bring that back down and then we’ll go ahead and bring it up in just a second okay let’s bring it back up and here we go so we got Port 802 we open this up and now we’ve got the hello world in there so congratulations you have Docker compose working now this stuff I realize might be a little complicated if you’ve never done this before but really these are just a bunch of arguments that we are telling docker to use for this particular container image that’s being built locally you don’t always build things locally as we saw before when we actually had a python project but when we do build things locally we have a whole another set of stuff that we can do so for example we can change the docker file command here the one that starts the application the runtime script we can change it to something different something like uvicorn main app and then the host port and reload the nice thing about this then is it’s actually going to be built off of that command it’s not going to be using the gunicorn script at all so if you made any mistakes with that you could just use this command to run it and of course you would take it down and make it all work just like that um as it need be so the next thing about the docker compost stuff is you can do something like this where you can actually watch for changes on files and it will rebuild the entire container we’ll see that in just a moment now if you wanted to rebuild the container or if you want to be able to do your own development you’re going to do one more thing which is attaching a volume to this container so this volume here is going to go ahead and grab this folder of source and it’s going to go ahead and put it where we want it to right now this says slash apppp which may or may not be the correct location we want to mount source so this means that we go back into the docker compost file and we scroll down and we see where we copy our SRC folder which actually goes into slash code so we just make a minor modification to this volume here and that means that we are going to go ahead and basically copy our code into the Container constantly and then we’ll be able to rebuild it so this might be confusing so let’s go ahead and see what what I mean by all of this we do Docker compose up d-at so what’s happening here is we see that watch is enabled if I were to change something to requirements.txt let’s go ahead and say bring in requests here like python requests and I just go ahead and save it this rebuilds the container image it’s going to happen right away now this might take a few moments because that’s exactly what does happen when it comes to building out containers but there it is it rebuilt it and then it restarted the application altogether so the next thing is actually changing the code but before we do that let’s take a look in here we see Hello World at Port 802 now inside of main.py if I change this to hello world Earth or something like that we should be able to refresh in here and it automatically changes which is super nice so really the command we want to use from now on is d-at in here so that we can actually just change our code we now have a Docker based development environment all you’ll need to run now going forward is Docker compose up and watch and then if you do need to get into a command line of this Docker image you can do so with simply Docker compose r and then the service name which is in the composed damel in our case it’s simply app so if we come back into our read me here we can just go ahead and say app and then you can put in something like /bin/bash which will allow you to go directly into the command line shell now you could also see something like or uh you could do something just like that but instead of b/ bash you could just go ahead and say something like python right that should actually work as well which will give you the virtual environment version of python so let’s go ahead and try either one of these out so you can take a look at what we’re doing here and then we’ll be able to see that I have this runtime ability in here as well so every once in a while you’ll see this removed orphin you might want to just add that in to a command as well uh whenever you run it but we’re going to leave it like this here is that version if I import OS and then I print out os. get current workking directory I think that’s the command we hit enter and we see that we’re in code if I go to exit it it exit the container altogether so what I typically do is not run python directly but I want to go into the command line kind of like sshing into this container to then run off of that and then in here I will be able to list things out and there’s main. Pi which is quite literally this code right here and so we can actually see that as well by doing cat main.py this will show that code in there if I were to change main up high in here to something different to like Hello World and then let’s clear this out again and then run cat main.py it changes and shows that actual code right there as well so now we have a mostly complete development environment that we can use through Docker um there are other things that we might want to expand on this but if you’ve ever thought about hey I want to use a more even more isolated environment inside of my project this would be the way to go but the main thing about this is mostly to be prepared for production which I think we are well prepared for production now in the next section what we want to do is actually start using another service in Docker compose to actually start building out the data schema that we want to use the key takeaway from this section is that we now have a production ready development environment of course this is thanks to Docker containers and this Docker file right here now the composed file helps us with the development environment the docker file file will help us with the production environment but both of them together give us that production ready development environment now this is more General right it’s not necessarily about our python application which of course means that we also need to make sure that our python application is ready for a local development environment as well which is why we started it there some of you may or may not use the docker composed stuff during development and that’s totally okay the key thing is that it’s there it’s ready and we can test it when we’re ready in other words if you need to just run your uvicorn and activate your virtual environment and run that on your local machine like that that is totally okay and it’s more than acceptable when it comes to the actual Docker compos though we can also just have that running if we need to because the way the actual Docker composed file is developed is it will react to changes that you make with your code or at least sync those changes and a lot of that has to do with all of the different commands we put in here so like we saw when we changed our requirements.txt the entire Docker container was rebuilt and then started to run again thanks to Docker compose watch so this really gives us this Foundation that we can build off of now does this mean that we’re not going to change anything related to Docker or Docker compose no as our application develops we might need to change how it actually runs whether that’s in Docker Docker compos or whether that’s locally just through Python and virtual environments but the key thing here is this Foundation can be used on other python applications if you want and with slight modification to the docker file you can also use it in node.js applications now node.js applications will also follow this same sort of pattern that’s the cool thing about the docker file itself is that it has well very stepbystep instructions that are going on here and of course you could use this to deploy things manually as well so if you don’t want to use containers when you go into production this Docker file will at least help you with that as well so we’re really in a good place to start building on top of our application and really just flush out all of the features we want to use now we still will use Docker compose for other things we still will use the docker file for other things but the point here is we have the foundation ready and that was the goal of this section let’s take a look at how we can start building new features on our application to really make it the true analytics API in this section we’re going to be building out API routing and data validation we are creating something called a rest API which has very static endp points that is predetermined URL paths that you can use throughout your entire project and so fast API is really good at building these things which is the reason we’re using it but the idea here of course is you’re going to need to have something the code going and I’m going to be using my Docker compose version of this with watch running so if you don’t have Docker compose up and going you can use just straight python of course what you’d want to do is clone the repo itself and if you are going to use straight python you’ll create a virtual environment in there and then you’ll navigate into SRC in which case you will do the docker compose command which is this one right here and you would just maybe change the port or you could use the same Port so in my case I have both of these things running and they are allowed to access so either one works the point here is being able to develop on top of what we’ve already established previously and so we can actually build out the API service now there’s one other thing I’m going to be doing here is I’m going to be using something called Jupiter notebooks and I’m going to go ahead and create a folder called NBS here and it’s going to be simply hello-world doip python notebook or iynb and so in here I’m going to go ahead and create some code and print out just simply hello world and then I’m going to go ahead and run this with shift enter this will then prompt me to select my python environment which is going to be my local virtual environment right here this of course means that I’m not using Docker for this the reason I don’t want to use Docker for this has everything to do with how we’re going to test this stuff out and so I’m going to go ahead and install this this uh you know it’s asking me to install the iPod python kernel in here and we want to do that obviously you can add this to requirements.txt but cursor VSS code wind Surfer all really good at actually just running Jupiter notebooks right in line which is why we’re going to use it right here so this is pretty much it for the intro Now using these jupyter notebooks we will then test out all of the API routes we put in and make sure that they’re working as intended let’s jump in the purpose of rest API are really API Services is so that software can communicate with software you’re probably already well aware of this but since we’re having software automatically communicate with each other we want to make sure that we Implement a health check almost first and foremost so that if the software has a problem reaching it it could just go to the health check to see if the API is down or not so that’s where we’re going to start and this is a really just a lwh hanging fruit way of seeing how we can build all this stuff out now inside of Main Pi I’m going to go ahead and copy my read root here and I’m going to paste it underneath everything and I’m going to just change the path to Simply Health with a z health check API endpoints are usually just like that it’s not usually Health but it has a z in there not really sure why but basically what we want to do here is rename the function to something different and we’ll go ahead and say read API Health something along those lines and we’ll go ahead and say status being simply okay in other words we can tell the designers that are going to be using this API hey if you need to just go ahead and run this health check this is also going to be really important for us when we go into production so now what we want to do is actually test out this health check so inside of my NBS here I’m going to keep this hello world going and I’m going to import a package called requests so python requests is a really nice way to do API calls this might be what you end up using in the future there’s another one called htpx both of them are really good and have a very similar way of doing HTP requests now what I see here is I actually don’t have the module called requests or python requests itself so jupyter notebooks one of the other nice things about these sort of interactive environment is I can run pip install requests right here and what it’s going to do is it’s going to use the environment that you set up at the beginning of this one right it’s going to use that virtual environment to install the packages you might need in which case you can just run something like that every once in a while you might need to restart the kernel which you just hit restart and there it goes once it restarts you have to run the various things and at this point python request is installed on my local virtual environment okay so I’ll leave that out for now I don’t actually need it any longer so feel free to comment these things out and realize I’m doing shift enter a lot to actually run each cell itself okay so how do we actually call this health check itself well the way I think about it is my endpoint the endpoint I want to use which is going to be Health Z and then I’m going to go ahead and do like my API base URL which I often do something like this base URL and it’s going to be HTP col Local Host and then the port we’re going to be using in my case I’m using Port 802 now if you’re not aware Local Host is also the same thing as doing on 7. 0.0.1 those two are interchangeable with in most cases okay so endpoint maybe actually is not what I want to do I want to say path here and I’ll go ahead and say the endpoint is the combination of these two things which I’ll just use some string substitution to make that happen just like that so now we’ve got our endpoint here and I can print that out and that’s pretty straightforward now if I could do a command click or control click I can actually open up the web browser and see this right here now of course we want to build towards automation since we’re using our our uh you know our API service we’re going to go ahead and say the response equals to request.get this endpoint right here and then all I want to do is print out response. okay okay so I’m going to go ahead and run this and what it does is it gives me a True Value great if I change the path to something like ABC on it and run those two cells again I get false fantastic okay so I’m going to keep it in as just simply health and this is what we’re going to be doing we’re going to be building on top of this with different paths and different data points and also different HTTP methods now let’s go ahead and create a module for our actual API events so inside of our SRC here I’m going to create a folder called simply API inside of that folder I’m going to go ahead and create another one called events and then inside of there we’re going to go ahead and do routing. so what I also want to do is make sure that each one of these things has an init file in here to turn events into its own python module so we can use dot notation appropriately as we’ll see in just a moment the idea here is we want to have very specific routes for our events resource so if you think of main.py this is kind of generic routes what we really want to have is something like SL API events and then that being all of the events stuff that’s going on in here so very similar to main.py but just slightly different in the way it’s termed so let’s see what that looks like so we’re going to go ahead and do from Fast API we’re going to go ahead and import API router and then I’m going to go ahead and declare router equaling to the API router now this router right here here is basically the same thing as the app but it’s not an app it’s just for this one small portion of the larger app and so in here then I’m going to go ahead and do router and we’ll go ahead and do.get the HTTP method of get which we’ll see in just a moment and I’ll just put a slash here and then we’re going to Define and this is going to be something like you know get or let’s say read events eventually in here that we will have some sort of more robust response and I want to return back just let’s go ahead and return back in a dictionary of items and I’ll just go ahead and say one uh two and three now the data that’s coming back we’ll definitely look at and change over time but for now we’ll just say read events like that so in order for this to work what we need to do is we need to bring it into main.py because right now the actual fast API application doesn’t know about this routing module because we have nowhere it’s not orted anywhere right this right here has all of the definition that we have in place at this point so the way it works then is we need to import it from API so we’ll go ahead and do from API and then the fact that it is a python module because of this in it we can do do events like that and then I actually can also then go one more do notation and import routing and then we can import the router that comes in and then we can change it as something like as event router now the reason that I do as event router is so that I can just riable name this if you will um so that I can have a lot of different ones with the same sort of organization or the same sort of setup that we might have had before now that we’ve got that we can come in to our app itself and we can use do include router and we can just pass in that event router but as you recall the event router itself has a slash here but we really want this to be SL API SL events like that so this is the prefix we want to use for this route now of course the way I can actually use this path is I could set it right here and that would generally be okay it would technically work but what I actually want to do is go back into main.py and set the prefix itself to that actual prefix without the trailing slash okay great so that is now an API endpoint route that we can use of course we still need to test these things out and we will but before I go much further I will say that this routing is something I actually want to change the way we change that is by jumping into init.py and in here I can do from routing import router and then I can just go ahead and exported by putting it into this all list and we just put in router just like that notice it then gets highlighted and now I can just come back into Main and I can get rid of routing and just imported that way it’s just a subtle way to do that it’s a nice thing that those init methods are able to do now the init method in here I could probably also import that router in this init method as well I don’t want to do that I just want to keep it like somewhat isolated from each other um or somewhat packaged in this way but now that we’ve got this router what we can do of course is jump into our API endpoint and just check it out by going API and events and there we go items 1 2 3 since we have this API Ino let’s go ahead and create a notebook to verify it so I’m going to go ahead and do one- verify router or let’s say API Event Route something like that IPython notebook will bring this in here now I’m going to go ahead and continue to number the notebooks themselves so you can always reference them later but the idea here is we want to do something very similar to this hello world where we’re going to go ahead and import the requests here so let’s go ahead and open up new code then we’re going to go ahead and bring in all of these inpoint stuff in here so we can do some various tests itself now I want to run this with shift enter it’s going to tell me what environment I want to select again we’re going to use our local virtual environment and then our endpoint our actual path is going to be a little bit different than this and that of course is going to be API events and we can have a trailing slash in here and then I can do my response equals to requests.get and then that path itself now the.get method here is correlated directly to this git method right here we’ll talk about that more in a little bit but for now I’m going to go ahead and run these two things and what we should get is the actual API endpoint but of course I did path not endpoint so that’s a little slight little mistake so we need to change it and then that gives us back a okay response which we can now print out with response. okay and there we go we got a True Value there so what we can do then is say something like if response. okay like that we can actually print out the data that’s coming from that response by doing response. Json like that and then we can print out that data okay so we hit enter and there we go we got items 1 2 3 now the reason we’re doing response. Json is because this is now a dictionary so if I actually do type of that data we should see that it is a dictionary itself right oh this is not dot but rather comma we can see that the class is a dictionary which in other words means that I should be able to do something like data. git items and do the dictionary value stuff that you might want to do now of course if we change our API endpoint this might have some issues in here as in it items might be none so if we actually did change our API end point let’s change it real quick to being you know uh results something like that slight change made it happen real fast I refresh in here this is still the same data because I didn’t actually do the res the request again so I didn’t actually hit the API endpoint again now when I do hit it it gives me something a little bit different in terms of the actual data that’s coming back so this is one of the main reasons why it’s really important to think through all of those API end points in the beginning you want to make sure that these stay basically the same going forward that’s why we’re going to be testing them out first to make sure we’ve got exactly what we want then we can always of course improve it later but something is simple as just returning back results here could have a drastic effect on somebody who might want to use your API itself even if that somebody is just you let’s keep going we want to take a look at the impact of data types on our rest API now what we saw in the last part was when I changed the key value of this data from items to results the actual notebook itself had a different result altogether and so I actually copied that notebook and just made it into one or two cells here what we run now is we can see that result still so what I want to do though is I want to take a look at how this impacts a single API result itself so in other words I’m going to go ahead and copy this and we’re going to call this instead of read events we’ll call this get event like a single event itself and then we’re going to return back what that single entry might be so if you think of this in terms of a single row and this being a you know list or a bunch of items in a table more on that soon but the idea is we want to look at this single row here now the way this works is we put in something like event ID so this is going to be a you know a variable that’s going to be passed after that endpoint here that variable will then come into the function itself so whatever you name that you put it in here and of course if you declare it as a data type like int then it’s going to look for a specific number we’ll see that in a second and then of course we can return back the ID being that event ID okay so back into this new notebook here I’m going to go ahead and pass in let’s say 12 in here for example and I’m going to go ahead and run this and what I should get back is exactly this data it’s still a dictionary but it’s now returning back ID of 12 if I change it to something like a and then try to run this I get nothing back in other words I can print out the like you know okay and then do response okay and I see that it’s not okay and I can also open up the URL itself and we see that it says un able to parse string as an integer because the input was a the URL everything like that we’re seeing that the input is a so there’s a couple ways on how we can solve this number one we could change this back to being a actual number because it’s an invalid request number two if we did want to support an a we would change this from a integer right here a very specific data type to string which is a little bit more generic in the sense that we can then you know now support that API inpoint for that string now in my case I want to keep it back as an integer because that’s how we’re going to approach this inpoint itself which it requires it to be an integer all across the board which means then our notebook will also require that as well but we’re not quite done yet that’s the input that’s the data coming through what about the data going back like what if I change this to being ABC ID and run it that way what we end up seeing here is we will actually still see the data come back and in some cases oops let’s make sure we save that and then run it again and there it is now this is now the output data it is now incorrect so we need to change that to something different that something different is going to be called a schema which is basically just designing the way the data should flow in this case we’re going to be using pantic so we’ll go ahead and create schemas dopy and I’m going to go ahead and do from pantic we’re going to import the base model and this base model is going to help us a lot so if we look back into our requirements.txt we did add pantic in here pantic itself most likely will be in with fast API as well so we’re really just using the basics of pantic here and we’re going to go ahead and say the event itself is going to take in that base model and this is where we’re going to design how we want it to be returned in this case we’re going to do an ID which has an integer so that’s the schema that is going to be used this is also a lot like data classes but it’s going to end up turning into something along these lines where it’s a dictionary that’s coming back so that’s kind of what’s expected by this schema now the way this ends up working then is back into our API routes we can import that schema so from schemas import the event this is probably better than calling it event we would call it something more like event schema so let’s call it that you could call it model but we’re not going to and you’ll see why when we start using the database stuff database models is how I typically call models schemas like the design that might go into a database model or come from a database I’ll call schemas okay nevertheless we now have this event schema in here and it’s in here as well now what we can do is we can actually just return that data type so if the colon and then the int is how we declare the incoming data type this arrow and then the data type is how we declare the outgoing data type the response data type itself and so as soon as I do this what I should probably see inside of my fast API application or at least very soon I should see some error with that data type okay so let’s go ahead and do a request here and I’m going to go ahead and run it in two places so I’ve got my notebook here and then my fast API application running below as soon as I run that I see that it’s missing the response ID field required the input again is ABC ID of 12 so the input’s incorrect so all we need to do then is change our route itself and that is going to be of course ID or basically to match that schema from before and now we can run everything again and this time it actually ends up working great so now we’ve actually made our system a little bit more robust it’s a little bit more hardened because it’s harder to make a mistake now or that mistake will be a glaring problem here so if I put in ID that is possible you make that mistake on accident but as soon as you go to try it out you’ll get that same error like what is ID don’t know got to fix that and we’ll go back okay great so then we bring it back just like that Co cool so of course solving errors also comes back to using something like git so tracking the changes over time will become important if you were using something like this if you’re building out a rest API of course you’re going to want to use get I’m actually not covering that because it’s outside the scope of this series but it is something important to note right now because git would help identify that problem because then you could just do something like get diff and of any sort of thing if it was already in your database so let’s take a look at that right now or rather in your data itself so I’m going to go ahe and add it and we’ll go ahead and do something along the lines of the actual name itself which I believe I called it this will be something like uh five and we’ll do basic data types there we go and so now I’m going to go ahead and do that slight little change here save it and now if I do get status I can see that there was a change and now I can take a look at that difference and what I should be able to see uh oops not schemas but rather the routing itself so get diff of that I now see exactly the problem that’s coming through so this would be another way to catch it and hopefully catch it early on of course you can also use automated testing using something like Pi test to actually catch that as well which would be even better to harden the system but the point here is we now have a way to validate a single piece of data how about a list of data let’s take a look now that we can return reliable data for a single item let’s go ahead and look at how we can do it with a bunch of items so there’s a couple different ways and how we could think about doing this one of the ways would be to do something like this where we just bring back a list you can just use the list element just like that and then put brackets there and put in that event schema that is a way to return back a list but that actually changes the result schema from a dictionary to a list so you can’t exactly do a dictionary like this that’s not really how it works so what we would end up doing is we would actually bring in a whole new schema for the result results themselves so what I would do then in here is inside of schemas dopy I would do something like maybe the event list schema in which case this is now going to be results of the data type itself which will be a list of schema right and we could also bring in the other class from typing so we can do from typing we can import the list class and we can use that instead of the list element so this is a better data type decoration itself instead of doing the python built-in list both work but this one is more verbose okay so now that we’ve got this we’re going to go ahead and bring it back into our routing and we’ll go ahead and use that list event schema here and we’re going to go ahead and return back that a list event schema and just like that we don’t need to change it at all because inside of this schema we’ve got this results here great so the actual result itself well let’s actually take a look at that one as well so if we save this I’m going to go ahead and duplicate this basic one and now we’ll do list data types or something like that we’ll go ahead and come in and change this to Simply events and we will update this and let’s go ahead and run it now and we get false once again okay so if I open this up I get an internal server error and what’s likely happening is the actual input is incorrect so each one of those items has invalid input data itself and that’s because the actual data that’s coming through in here is not an event schema so we would actually want it to be at least of that type of event schema in other words it needs to look closer to this for each instance in here the way we do that then is actually turning these into dictionaries themselves so ID equals to that ID and then we would just repeat this process for each element which I’m just going to go ahead and do really quickly and of course I probably could have just copied pasted but there we go so now the results are dictionary values and we can see if that solves the problem for us it looks like the application rebooted and now we can go back in here and we can run it again now the results are coming back so this is actually super nice so that means that our routes themselves are now hardened to what we want for our results and of course if we wanted to add something to this list scheme I say something like count and we’ll go ahead and do count being something like three three right then we would go back into our event list schema come in here and say count and this is going to be an integer H here uh we could also have it as an optional value but we’ll just leave it in there just like that and then we’ll come in and run this again and now we’re getting false once again so let’s see for instance uh we don’t have the results coming back correctly so let’s go back in the route we’ve got our count in here uh this actually should work just fine let’s make sure that everything’s all saved up and let’s try that one once again with it all saved and there we go so now we’ve got the data coming back here it is with that count in there so the point of this of course is to make sure that how we’re designing our API when it comes to communicating the data is going to be very hardened it’s not going to change a whole lot altogether so of course this is going to be even better when we start entering this data into the database uh and also ex and actually grabbing the data from the database but before we even start grabbing the data from the database we need to learn how to send data Beyond something like the URL route in other words we need to take a look at the postp put and Patch methods to see how we can send data to our API let’s take a look up until this point we’ve been using the HTTP git method this becomes even more obvious when you look at something like requests.get and then inside of main.py we’ve got app.get and then we have a URL path and then in routing. we have router. git so the G method is very very common so we’ve got git like this we’ve got get down here for like some ID those methods are what you’ll use to grab data from the database or from your API itself and so our notebook is really just a small example of that this notebook is really meant to be like as if you were building out another app to work with the API we’re also building at the same time but now what we need to do is we need to be able to send data back to Any Given uh you know server or to our API so what we want to do is we want to use the post method that’s what we’re going to now is we’re going to use that post method in here and so the idea then is we’ve got our endpoint all of that stuff can be the same to change the response here all we need to do is change this to not get but rather post so now this is going to attempt to send data but in this case it’s actually not sending any sort of data at all because we well we didn’t Define any data to send so if we actually look at the response itself so I’ll go ahead and do response. text and we can see detail method is not allowed as in the HTTP method is not allowed if we do get and we see that as well we can see that it actually unpacks two different things so we’ll look at this as well in just a moment but the idea here is going back into the post method we get a detail not allowed that not allowed error is because of our routing here so we don’t actually have a route to handle the HTP method the only routes we have right now are to handle git Methods at these particular endpoints if we want to handle a post method as well we can just duplicate our function here and then just change it from router. git to router. poost which is now going to be this as an send data here type of thing and then this instead of read event it will be something like create event now the important part to note is the endpoint is the same as each other but the method is different so inside of fast API we create a different function to handle the different HTTP method as well as the different HTTP route sometimes what you’ll see on AP apis is something more like this where it’s create and it’s quite literally a different route as well to handle the different method now that is not nearly as common in the case of creating data so we’re going to go ahead and leave it in as this right here so we’re going to get data this is going to be more of a list view this is going to be more like a send data here or a create view right and so let’s go ahead and get data here type of thing right so the idea then is in this function when we go to create an event the response should be very similar to the detail view because when we’re going to add data into the database what we could could return back is hey we added that data oh yeah and here is the data we added because if we modify things inside of this function which we might then we want to send back whatever that modification would end up being so in my case I’m going to return back the original event schema and we’ll just go ahead and say we’ll just do an arbitrary ID here because we don’t actually have a database that will help us with that ID just yet okay so we’re almost there now that I’ve got the actual inpoint let’s go ahead and try it again because now the method should be allowed so let’s go ahead and run it again and now I get the method being allowed and then the response is just echoed back in here so the post method is basically the same as G method at this point it’s just echoing back some data but that’s not what we want to do we want to actually send data here we want to actually see some data so I’m going to go ahead and say data and I’m going to put this equal to an empty dictionary in here and I want to actually print out that EMP empty dictionary or rather data dict equals to empty dictionary rather so I’m basically declaring the data type and then an empty dictionary in here so we want to see what that data is and so this is actually very similar to what we saw down here where we passed in an argument into the route a wild card if you will into the route it’s very similar to that but slightly different okay so let’s go ahead and take a look at that and we’ll do this by sending that event again and if we look in here we see that there is no data coming through so there’s that dictionary okay so if I actually want to send data though I need to change things a little bit before I do though I’m going to go ahead and jump into the response and grab the headers that are coming in here and so what we see is a bunch of headers but the important part is the content type here so what’s happening on the server side in our fast API application is is expecting a certain data type to be sent especially with sending out data in other words the data itself is going to be a Json data type so I’m going to go ahead and say something like our page is equal to you know Test Plus or something like that so that’s the data we’re going to send now and we’re going to go ahead and say data equals to data but there’s a caveat here so if I actually run this now I will get an error right so it says the error there and of course I can say something in here something like else print out the response. text so we can see what’s going on with that response and what it shows me is input should be a valid dictionary and here is the actual input that was sent so that is not Json data we can know what Json data is in a couple of different ways so one of the ways we do this is by bringing in the Json package that’s built in to python itself and then we can do json. dumps and put in data there hit enter and now we see this string in here this is actually quite literally a string which you can check by just putting type around it and you can see that as a string it’s no longer a dictionary like what we have up here so so in other words this is string data but it’s in a very specific format that format is you guessed it Json data and so we can send this data now to our back end so let’s go ahead and try that so I’m going to go ahead and use instead of just pure data here I’m going to go ahead and grab this Json dumps pass that in run it now this time I didn’t get that error it actually did send that data end and we can actually take a look at that data with this right here so what ended up happening is this data was treated as Json still even though I didn’t tell it to treat it as Json so one of the other things that you do every once in a while is you create headers and you declare what the data is that’s coming through in this case it’s application Json this is not the only kind of data so for example if you were sending a file like an image file you would not use application Json you’d use something different but the idea here is you can pass in headers in there as well and send that same data these headers are going to send to our back end our actual API will look for those headers and be like okay cool I see you headers you say application Json so I can treat this data as if it were Json but in in the case of fast API it actually unpacks that Json data for us and turns this into an actual an actual dictionary which we can verify by coming in here and saying type of what that data is and then we can come through in here send that and there we go we’ve got a dictionary in here so in other words what’s happening here under the hood this is all happening for us but we still need to understand what’s going on when it comes for apis communicating with each other it’s usually through Json data it’s not always there’s other data types that you can pass but it’s usually through Json so fast API was designed in a way that said hey when we skit post data we’re just going to expect it to be Json so then we can just infer that the data that’s coming in is going to be a dictionary in other words I should be able to respond with this data assuming I did it correctly but of course the schema that I have set up won’t allow for this data to respond as an echo so I’m just going to get rid of this schema for just a moment so we can see the Echo come through now so with data test I’m going to go ahead and run this all again and now we should see that Echo coming back as soon as I bring that schema back it is going to run an error as we’ll see in just a second so if we run that back everything’s saved run it again what I get is now an error because the schema is invalid so we need one more step with our data and that is going to be an incoming schema in other words this can’t be generic like this it needs to be something else so for now I’ll leave it like that and we’ll look at that incoming schema in just a moment now I will say one of the cool things about this as well is we can go even further I can copy this router here down here and we can grab something like put as in we are going to go ahead and update this data now I’m not actually going to cover patch but put is very similar to this where we have the event ID coming through and then we also have the actual data that would be coming through from the back end this actually would be not called Data but rather payload that’s what we would end up calling it for the put events so we’ll see this one as well but in this case we are just returning back this here and this would be something more like update event now in the case of an Analytics tool I don’t know why you would be updating the event to directly instead of just entering a new event but this is kind of what we need to understand is these different methods now there are other methods as well that I’m just not probably going to implement for a bit but one of them being like delete where you would delete a specific event itself that would be another one that you might end up using once again we’re not going to be implementing that just yet but now we want to actually validate the incoming data to our API we need to ensure the data that’s being sent to our API is correct or at least in the correct format so very similar to when we send data back needs to be in the correct format in this case as the event schema we need to make sure that the data that’s coming in is in the correct format not just in the correct data type so not just a dictionary for example so what we want to do here is create a new schema that will help us with this validation now we already saw some elements of this validation with the git lookup where we said event ID is an integer we want to make sure that it’s only a number of some kind you could obviously change it to being an Str Str in which case that actually sort of breaks this endpoint where it’s now a wild card it’s no longer an actual number so we want to keep that one as an integer the same sort of concept exists for our create event as well so what we want to do then is we want to jump into our schemas here and I’m going to go ahead and create a brand new schema this one is going to be called our event create schema this one will no longer have an ID in there but rather it’s going to have a path in here which for now will just leave as a string value and so this schema itself is now going to be brought into the router in here so we bring it in just like this I’ll go ahead and pass it in here and we’ll do something along these lines right here there we go and so now this is going to be our schema so this is the data in here all I can do is bring it in as create event schema or the event create schema and then I would want to actually rename this to Simply payload you could keep it as da data you could keep it as a whole lot of things but I actually want to keep it in as payload I find using the term payload to be a little bit better than data data is a little bit too generic for what this kind of data is payload is what’s being sent to us right it could be correct data but realistically it’s payload just like what we started talking about down here now the other thing is with the put method we could could do the same schema if we wanted to or we could expand it if we wanted to add something different so let’s say for instance we came in here and did the event update schema and instead of updating the path maybe we just update the description and that’s literally the only field that we allow to be updated in an event update schema and so these are required and the only fields that are changing so what that means then is I’ve got three Fields here that the event could potentially save that is going to be these three Fields right here right so we would have ID we would have path and we would have description so this is what would actually be stored on the database where the actual endpoints are only supporting these items in here but the actual event schema the final one might have all of them as well so we’ll talk about that when we get to the database stuff uh but now that I’ve got both of these I’m going to go ahead and bring that one in as well and we’ll bring it in to the update portion also so let’s go go ahead and do that just like that great and this time I’ll just go ahead and print out that payload also so I want to see these two events and see what they look like going back into send data to the API this notebook here I’m going to go ahead and run this and what we should get back is we’ve got an issue here for our input right so we’ve got page in here and the input saying Page Field required being path not page so that means that I need to change this to being path or I need to change the other other schema being page so I’m actually going to change the schema because maybe I don’t want it to be called path maybe I want it to actually be called page and so in which case I would save it like that and then we go back into the API call in the notebook and now it actually sends it back to us which is great so of course we could always Echo the data back as well if we wanted to by sending or updating the event schema payload altogether we’ll look at that in just a second but what I actually also want to see is this update view on how that might work so going back and here we’ll go ahead and copy some of this data here paste it below this time I’m going to go ahead and grab the endpoint stuff and it’s going to change slightly uh which will be this down here and then we’ll say this is 12 now with a trailing slash because of how our endpoint is set up and so there we go and I’ll call this my detail inpoint so detail endpoint and then detail path and we probably don’t need this base URL anymore and then we’ll go ahead and put this down here now you might be wondering what method do we use hopefully you remember it’s simply the put method that’s it okay so now that we’ve got this there’s one other thing that I can do in here that’s actually really nice so instead of doing data equals to Json dumps data and then the headers I can literally just use Json itself python requests will then Implement all of those other things for me and what we said was description is the value that we need here in which case I’ll just say hello world and then now this of course is an HTTP put I now can run this and I should be able to see that it is okay and if I look in the terminal of our application running I see there’s the description Hello World and there’s the page and of course back into the route this is not a dictionary now but it’s rather a schema so if I wanted to get this stuff I could go ahead and do payload page and then in here it would be payload uh what did we call it maybe that one was page yeah so that one’s page the other one’s description so this is payload do page this is going to be payload do description giving us the full advantage of using pantic in our API and so back into the notebook itself if I run that again I see that it says hello world for that description and then the other one should probably say something like uh the path itself so let’s go ahead and run that again and there it is cool so we now have the routing and the schemas for incoming validation as well as outgoing validation to ensure all of that stuff’s working with pantic we can have optional fields at this point we don’t have any optional Fields we’ve got only required Fields so let’s see how we can actually create these optional Fields now before I do I want to actually update my notebook here a little bit so that the create endpoint and the update endpoint are basically one sell by themselves so that there’s no issues with it and so the create one is going to be here I’m going to just change this to create endpoint and we’ll go ahead and put that there the data will actually turn this back into Json data and we’ll pass in the dictionary with the page of you know slash Test Plus or whatever uh this is mostly so we don’t have any conflicts with some of the variables that might come through from the different responses and then also we only have to run uh each one of these one time right so get update great so now that we’ve got this let’s go ahead and add in the optional data here so jumping into our schema what’s the optional data I want is in this event schema I probably want to have page and description optional I’ll start with just simply page and so the way this works is if we put it in as page colon string that is required data if we want to make it optional we bring in the optional class from typing and then we actually bring in the square brackets like this now it’s optional or at least seemingly optional so let’s go ahead and save everything jump back into our notebook and then run it I get an internal server error so if we actually take a look at that server error what we’ll end up seeing is the field is required right so it’s now still assuming that that field is required so it’s not quite optional yet uh so let’s go ahead and add in a default value for it so it’s almost optional just not quite there my default is going to be just an empty string here just like that so let’s make sure it’s saved and once again back into our notebook we’ll go ahead and run that post and this time it seems to have worked and there we go okay so what I want to do is actually Echo back that response I want to see that data I don’t have an error anymore with that field but it doesn’t seem to be showing up much right so if we look in here we’ve got the response coming back let’s actually take a look the response coming back is Page being an empty dictionary so that’s actually kind of nice in the sense that yeah cool the page is there if we look at the other one same thing the page is there it’s empty great so what I want to do then is I actually want to return back the page data at least from the first one and the way we do that is by jumping into routing and then putting in the page data in here and we can do that with the page value being added to the response value so payload dopage we save everything like that we go back into our notebook we run it again and now we can see that page values coming through and now it’s technically an optional value for this field right for that data and we can do that same thing again with with the description itself and we can come in here just like that and now we’ve got some optional values in here and if I look at my requests there we go and we can come in and we see all that great so of course I want to Echo back the description back here as well so back into our rounding here we’ve got our payload down here which has our description and then I can do something along these lines where it’s pay. description great so this will help us with that echoing and of course we can verify that again and there’s that description coming through of course it’s just echoing what’s ever here and that’s that okay so in the long run of course these schemas would be tied into what’s happening in the database so in the middle of all this stuff would be sent to the database and then receiving stuff from the database but the other thing about this is the actual payload itself we can actually return it to be a little bit easier on ourselves so now what we can do is do data being payload do model dump and what this does is it takes the payload and turns it into a dictionary which is basically from pantic itself has that model dump right and it used to be something different I think it was like two dick maybe uh like this if I remember correctly but now it’s just simply model dump in which case this is now going to be a dictionary in here which I can unpack just like that and I would do it also down here as well so we would unpack it like that and then the data just like that great probably don’t need those print statements anymore uh but now we’ve got the data coming through as we probably want okay so once again I can verify these in my notebook and I should be able to see the echo data coming through in here and one of the questions of course would be oh well could I actually send the description now if I wanted to so if I come in here and do description and set something like you know abc123 will that respond that data back and the answer right now is no hopefully you understand why at this point but I’ll let’s go ahead and go through it first off the data that’s coming through is just this payload right here it’s going to ignore that other data if you pass in more data in here it’ll just ignore it there’s probably a way around that as in it will then say Hey you have too much data there’s probably a way to do that but for us we really just want to make sure that we’re grabbing the correct data and so I can actually come up here and give that same optional description in here and also on the update view if I wanted to be able update the page I can do that as optional as well but there we go so now that we’ve got the description coming through I should be able to Echo that back as well and there it is it’s now being echoed and now I have a way to change the responses really quickly and it all has to do with these optional values if you wanted to add in optional values now every once in a while you might want to add in a default value this default would be then using a field so you’d come in here and say something like field default being an empty string or my description something like that in which case you would come back into the API let’s go ahead and get rid of the description I passed in and now we’ll take a look at it if I run it again it now gives me that default value if you wanted to put one in there um and of course there’s more verbose ways and more robust ways we can continue to modify that stuff uh but overall this is actually really nice because I have the ability to change all sorts of things in here for for my schemas and now once again adding to that stability to how we end up using pantic inside of fast API our API now has the ability to handle different HTTP methods like getting post at different API endpoints like our list view or our detail view now once we actually can do that we also see that we can validate incoming and outgoing data now a big part of the reason I showed you schemas in this way like with multiple Parts is because the way you use different data pieces might change depending on your needs for your API service when it comes to an analytics API do we really want to ever update it maybe maybe not this is going to be something you’ll decide as you build this thing out more and more the point here though is we have a foundation to move to the next level that next level is going to be using SQL databases to actually store the data that’s coming through and be able to handle it in a very similar way that we’ve been doing and the way we’re going to do that is by using SQL model if you look at SQL models documentation and you actually take a look at how it defines a database table it looks like this this hopefully looks very familiar to what we just did and that’s because SQL model itself is based off of pantic it’s also powered by pantic and something called SQL Alchemy SQL model is definitely one of the latest Cutting Edge ways to work with python and SQL SQL of of course is designed for storing data it designed much better than python would be python itself is not a database SQL is and so we’re going to use SQL model to actually integrate with a postgres database called time scale DB and another few packages to make sure that all works really really well let’s take a look at how to do that in the next section in this section we’re going to be implementing SQL model this really just means that we’re going to be taking that piden schema validation for validating that incoming or outgoing data and we’re going to convert that so that it actually stores into a SQL database SQL model will do all of the crud operations that’s create retrieve update or delete of that data into that SQL database now the SQL database we’re going to be using is postgres postgres is one of the most popular databases in the world for good reason one of the reasons is the fact that it has a lot of third party extensions to really pick up where postres Falls flat one of the things that postes doesn’t do very well is realtime analytics or just a lot of data ingestion postris doesn’t do this natively by itself in which case time scale picks up the slack in a big way so we’re going to be using time scale because we are building a analytics API and this is designed for real-time analytics and it is still postgres so at the underlying technology how we’re using SQL model is going to be the same regardless of which version

    of postres you use especially in this section where it’s really just about using SQL model itself we will use the advanced features that time scale gives in the next section this section is really just about understanding how to use SQL model and actually storing data into a postc database instead of just using pantic to validate data because that doesn’t really provide that much value let’s go ahead and dive in on our local we are going to be using Docker compose to spin up our postgres database we’ll start with the configuration for Pure Old postgres then we’ll upgrade it to time scale and all of this is going to be done using Docker compose in just a moment now if you skipped Docker compose you can always log in to timescale decom and get a database URL and just use the production ready version right in there that process we will go through in the next section but for now I want to just do the post one on my local machine now a big part of this is looking for the postres user and all of the different environment variables that are available so we’ve got postgres password user and DB those are the main ones we want to look at to get our local version working inside of Docker compose so I’m going to open up compose diamel here and then we’re going to go ahead and tab all the way back make sure it’s on the same level as app one of the ways you can figure this out is by just you know you know breaking it down like that and then you can do something like DB we’ll call it DB service in here and then I can go ahead and create those things out with this still broken down having it broken down makes it a little bit easier to know exactly what to do now the first thing is we can come in and decide our image so if I were to use postgres itself I would come into postgres official and then I would want to look for the tag that I’m going to end up using in here and be very deliberate about the tag especially with post cres as a database so maybe you use 17.4 Bookworm or just 17.4 the size of them looks about the same so it really probably doesn’t matter that much between the two of those so you could do something like this postes 17.4 next up you would want to set the environment which will be all of those environment variables so once again searching for that postcg user in the documentation on the read me let’s go backup page here and search for that and you can actually see the different environment variables so you’d come in here and do something like postc user and then the next tab would be postres password and then something like this and then the actual database itself the core values here are basically this right so those are the key ones that we’re going to want to have next up what we’re going to want to do and this is one we will definitely need is something called a volume this volume is going to be managed by Docker compos we don’t manage it oursel it’s not quite like what we did with our app where we actually mounted the folder here into the Container instead we’re going to let Docker or actual Docker compose manage the volume itself so before I even declare that I’ll go ahead and do volumes and this one I’m going to call this my time scale DB data and just like that that’s all you need to put in and then you can use whatever you name this and you can bring it up to your volumes here and then have it go to a specific location which in the case of the postgres location the postes database it’s going to be VAR lib postgres SQL and then data and so that allows your postgres instance on Docker compose to persist data because you definitely want to have that so if you take down this or run Docker compose down all of this will still be up and running so similar to like our app itself we also want to declare the ports in here and I’m going to go ahead and use the default ports which is 5432 and then 5432 and then in some cases especially with what we’re going to be doing you might want to expose the database which is going to be 5432 I’ll explain that when we start to integrate this into our local project here okay so the idea with this is this is using postes of course right so if we actually scroll back into postgres there’s the name of it all that it’s all good we could totally go that route but I actually don’t want to use postes I want to use time scale so time scale is not a whole lot different but the thing is we grab this right here so now it’s going to be time scale that’s going to be the repo name or the image name and then we grab the actual image tag which we come in two tags here and we can actually scroll down actually in the overview here we also see the tags the notable ones latest pg17 so in the case of postgres itself you got 17.4 with time scale we can just do latest pg1 17 and everything else is the same which is really nice but of course I’m going to change this these values in here to something just a little bit different and I’m change the user to time- user and then we’ll go ahead and say time PD uh PW and then I’ll go ahead and do my time scale DB in here as the actual database name that I’ll end up using so all of those values are going to allow me to do something like this in myv which is going to be my postgres SQL plus pyop G which we’ll go over again once we implement it but it’s going to be our username so it’s going to be just like that and then colon our password like that and then at some host value which will come back to then the port value which is going to be right here and then slash the actual database itself so all of this is what we’ll need in our environment variables for our app itself so the app we can add it into up here we could say something along the lines of database URL equaling to basically that data now in development this is generally okay in production you’re not going to want to have your actual environment variables exposing something like this and so in which case you would use an EnV file as we discussed before but that’s when you would come in here and go into EnV and save it kind of like that now as we saw before as well when we were doing it with our app whatever you hardcode in here that’s going to take precedence over the EnV files themselves which is kind of nice but there’s our database URL and of course it’s based off of all of this stuff but there is one key component that we still need to change in here and that is this host value here this host value is going to be the name of the service itself when you are using it inside of Docker compose I’m going to show you how to use it in both places but I want to leave it like this for now we’ll save this and then inside of my Docker compose all I have right now is my app running so I am going to need to take this down and we’re going to go ahead and restart our app itself looks like I’m having a few issues with our app running so I’m not sure if maybe it’s related to this Docker compose rebuild it seems that it is now with the app down I’m going to go ahead and run Docker compose up and I really just want to see that our postgrad is a database is up and working our DB service is being created and downloaded and all that no surprise there it’s actually grabbing it from time scale the docker Hub version of the time scale DB of course it’s open source so that’s really nice and I’ll let that finish running okay so it finished running and it looks like everything is working in here we might need to test this out with postgres directly but in our case we’re just going to leave it as is and we’ll test it directly with python but if you are familiar with postgres you will want to probably test this the other thing I just want to make sure that my app is still running it looks like it is so I think our Docker compost stuff is probably good and we’re now ready to start the integration process within our app itself self I’m going to be integrating it in two different ways one is by using the docker compos version which is basically this right here another one is just considering our database as Docker compos so that our app itself will be able to run with things as well so in other words having basically two environments that can still work with this Docker compos instance of our database itself now one of the things that’s also important to note that we will be doing from time to time is we’ll go into the root of our project and we will run Docker compos down- v-v will delete the volume as well which means that it will delete all of the database stuff as well too the reason we run this especially in development is to get things right to get it working correctly then once it is done deleting we can bring it back up and everything is back basically from scratch when it comes to development this is a key part of it is making sure you can bring things down and bring bring it back up and it happens that quickly which is another reason to really like using something like Docker compose of course one of the bad things about it is you might delete a bunch of test data that you didn’t intend to delete which of course is something you want to think about if you are going to run Docker compos down one of the first steps in integrating our database is to be able to load in environment variables now when it comes to Docker compose directly the environment variables are going to be injected in other words we can come into SRC here and let’s go into one of our apis into one of our routes I should be able to go in here and go import OS and then just come on read events and I’ll just go ahead and print out os. Environ dogit of the database URL itself and so in which case I should be able to go into uh you know slash API SL events hit enter and I should see that print statement come through and there it is right there okay great so that’s being injected into our application because of this right here but unfortunately or fortunately this won’t necessarily work if we bring in our application directly from our virtual environment so let’s go ahead and do Source VV bin activate and then I’ll go ahead and navigate into the SRC here and then I’ll go ahead and run this same thing right here but this time I’m going to go ahead and not specify the port because it’ll give me most likely Port 8,000 so so now I can open that one up and I can go to API and events and hit enter the print statement is going to be none so we need a way to kind of handle both scenarios some of you might want to use Docker compos when you’re developing I know I do a lot of times but I also know that I also don’t do it a lot of times sometimes I don’t set up my apps this way so our actual fast API application needs to be able to be ready for both of those things so inside of requirements.txt this this is where we need to change something I’m going to add in something called python decouple now there are other ways to load in environment variables one I think is called python. EnV but I like using python to couple for a reason as you’ll see in just a moment so the idea here is now inside of my API I’m going to go ahead and create another folder in here we’re going to call this DB inside of DB here I’m going to go ahead and create that init method and then I’m going to also going to create config.py and what I want to do of course is using pip install python decouple I’m going to go ahead and do from decouple import config and then we’ll call this as decouple config mostly because this module I just created is also called config we don’t want to have any weird Imports in here now as you notice cursor is saying hey decouple is not installed so we need to make sure that we do have that installed in our virtual environment which I’ll do with that pip install there I already added it into requirements.txt so I don’t necessarily need to install it again through that but with this running now um I can reload in here and now I’ve got this decouple config in here so what I like to do is I like to put in our database and URL and this is going to be equal to decouple config and that’s going to be that same database URL and the default is going to just be equal to an empty string here so this is why I like python decouples we’ve got this empty string here the other thing about decouple config is it can actually load in a EnV file as in this right here so we’ve got this host value here so now hopefully if we did it correctly we should be able to use this now the way we use it is going to be from our events we now instead of using OS we’re going to go ahead and do from. db. config we’re going to go ahead and import the database URL or better yet I think we should be able to do from api. DB config Maybe grabbing database URL that way but we’ll see if that ends up working so let’s go ahead and take a look with that print statement now and going back in here we refresh and now we’ve got none from os. Environ but database URL is actually working inside of here which is giving me a different host value but that host value is based off of the EnV file so this actually brings me back to what I would actually do with Docker compose I would not put it hardcoded in here but instead what I would do is I bring in another EnV file like env. compose and use those same parameters in there and I’m going to just go ahead and put them in directly like that and then we would use maybe that same port value um it’s completely up to us in that term but we’ve got this database URL here and then in compose yaml the EnV file itself would be do compose and then I would just go ahead and completely get rid of this environment itself the only reason I’m commenting it out is so that well we can actually bring it in as need be okay so what we see here is we got this you know no module found error I think maybe I didn’t save something or whatever but let’s go ahead and bring it back up which should actually build everything uh but it’s not actually building the python decouple so what I’m going to do then is I’m going to go ahead and stop it um and then whenn in doubt when something like this happens you do uh d d build and that should actually build the application itself for the ones that are going to to be built uh which will install those requirements now for some reason I actually know why this rebuild did not happen and it’s because I did not do Docker compose watch so that’s something else I need to remember to do and that is Docker compose and up– watch so that it does rebuild it when requirements.txt changes so that error goes away but the key part about this is that now I have another place for my environment variables if I refresh on the docker composed version or my local version I should should be able to see the environment variables coming through for either one and now I have support for both places now the reason I’m doing this as well is so you get very familiar with the process of loading environment variables especially as you work towards going into production because you’re going to want to help isolate these things going forward now notice that this EMV do composed is underlined or it’s ready to be going into um you know git so if we actually look inside of our git step is here we can see that env. compose is in there if we look in the GitHub repo the actual sent code we can see thatv is not in here this is an example of I’m okay with sending this environment variables because composed yl is still using them and in this case it’s really just for a local test environment one that I consider disposable as then I can delete it any time obviously the usernames and passwords are not great here this also should indicate to you that the actual time scale service if you were to want to use compose in production this these environment variables should also be in an EnV file and be done differently inside of there if you were using compos and production which we are not going to be using at all we will be using Docker files but not Docker composed okay so that’s loading in the environment variables in both places as you can see Docker compose is far easier than what you would do what I just did but the other thing about this is beyond just having environment variables if you have any other static variables you want to set like other settings for this entire project you could also put those inside of this location as well now we’re going to convert our pantic schemas into SQL models this is before we actually store it into the SQL database I really just want to see how the feature of SQL model work in relation to the actual pantic models so what I want to do here is grab this schemas and I’m going to go ahead and copy it and make a new file called models so realistically what we’re doing here is just updating the previous ones to be based in SQL model and it’s going to be incredibly straightforward to do I’m actually going to name them the same thing for now we might change them later but for now we’ll leave them as event schema and so on mostly for the ports when we go to test this so the idea here is we want to not use pantic but rather use from SQL model we’re going to go ahead and import the SQL model and then also the field okay so base model is now going to be SQL model so we go ahead and replace all of these in here like so and I shouldn’t have to change the field but overall this is like the main change that I’ll need to do with the actual ID we will probably have to change that later which you can actually see in the documentation it’s going to look like this at some point so we might as well put that in there uh at least as a comment for now and then we’ll come back to that in a little bit uh but what we’ve got here is basically the same thing as we had before so inside of routing instead of schemas we’re going to go ahead and change this to simply just do models then we’ll go ahead and jump into our notebook and we really just want to test to see if all of the notebook stuff is still working let’s make sure our app is running I have it running in two places I think it looks like it is so uh inside of Docker of course and then in my local machine let’s go ahead and run this all together and everything works as it did prior um so we should have the actual models all set up as we had before okay so the big change from here would then be to change this into a table as in this is what we’re going to actually end up storing of course we’re not going to do that just yet the point of this was really to see that SQL model and pantic are the same and of course if you actually look in the documentation you will see that at the same time it’s also a pantic model so it’s quite literally a drop in replacement for this as we’ll see very soon it will also help us with storing this data now we need to create our first SQL table our database table the way we’re going to do this is a three-step process the first step is going to be connecting to the database itself using a database engine when I say connecting I mean having python being able to call the SQL table right that’s it that’s the connection that’s what database engines allow for us to do after we actually can connect to the actual database we need to decide which of our schemas in here is going to be an actual database table that might be all of them it might be one of them it might be none of them but the idea is we need to decide which one’s going to be a table and then make sure that that it’s set up the correct way to be a table and then finally inside of fast API we need to make sure that fast API is connected to all that so those tables are actually being created and they’re be being created correctly okay so the first thing that I want to do then is actually decide which of these are going to be database tables now you may have never worked with databases before or you have and you just didn’t realize how these tables work to make a decision like this so if you think of a database table very similar to like a spreadsheet you’re on the right track spreadsheet have all of those columns and you can keep adding columns as you see fit but then when you actually want to store data those columns describe what data you want to store in that particular column this is not that surprising you might have a column for ID you might have a column for page you might have a column for description all of those columns in a database need to have data types so in the case of an ID the entire column is only integers they’re only numbers in the case of a page those can all be strings and every once in a while you might have a datetime object or you might have float numbers or all sorts of different other data types there’s a lot of them out there the point is SQL tables especially inside of the structure of SQL have the ability to do very complex operations on all this data now when you think of a spreadsheet you might only have a few thousand rows in there at any given time maybe you have up to a million but if you got a million rows in a spreadsheet there’s a really low likelihood you’re going to be opening up in in Excel or something like that you probably need to use something different altogether but the idea here is a spreadsheet has limits to the number of rows SQL tables on the other hand basically have no limits or at least in many cases they’re designed to have massive amounts of data in there and so having these data types make a huge difference in how we can use those tables so for example if you want to get IDs that are greater than 100 or a, the actual SQL table itself will be able to do that very very efficiently versus trying to go one by one and looking for that specific ID there’s a lot of advantages of using a database itself to store data I’m not going to go through those advantages the point that we need to know right now is that our columns need to have specific data types now this is the cool thing about SQL model is we’ve already declared those data types integer string string right so we already have three data types in here which are going to be actual fields that will be in the database itself otherwise known as columns columns Fields you can think of them as the same thing in the case of how we use our SQL model so the idea here then is we want to decide which one of these is going to be a table now this is actually fairly straightforward if we look at how we’re using it by going into routing. PI right you could already have some intuition about which to pick already but let’s go ahead and go through each view to see how we’re using it and how this might affect what we decide to store in our database so if we look at the very first one we’ve got an event list schema this is a git we are only extracting data from the database that’s it so there’s a really good chance when you extract data you’re going to not store the extraction that you just did so that kind of is like a circular Loop if you were going to store every time you extracted something that doesn’t make sense now you might store the fact that it was extracted but you probably won’t exore the actual data that was extracted so this one I think the event list schema we can say yeah it’s probably not going to be a database table the next one create event now the incoming data is what we end up storing but do we always want to store all of that incoming data or do we want to make sure that the incoming data is flexible in other words this event create schema here if we were to change it let’s say for instance we didn’t want to store a description anymore on that crit event we want to remove that do we want to remove this field from the entire database itself the answer in that case would be no we would want to keep that field in there it’s just when we first get that data we aren’t going to want to um you know maybe have that field anymore okay so in that case this one we’re probably going to rule out but notice that it actually turns it into an event schema we return back the event schema itself now this is the one we’re definitely going to want to store so we get a payload coming in of data that’s all validated and all that then we want to turn it into a different data type which means that we’re going to end up storing it in inside of the event schema table which will change very soon now another thing about this is we notice that the event schema itself is used multiple times for database like things in other words if we are looking up that database table we see event schemas in there so that means it’s probably going to grab data from there and then also when we go to update data um we’ve got some data coming in that we might want to change and again we can decide which fields we want to actually allow for updating to happen without removing those fields from the database then we can actually return back that event schema so yeah event schema is the only one we really need to St save in here um and of course the event list view is doing a result of the event schema as well so that’s the one that’s going to be our database table it’s going to be this one I actually do not want to call it event schema anymore I now want to call it event model the reason is hopefully straightforward but let’s go ahead and actually modify this a couple times we’ll call it event model and then I want to go ahead and add in here table being true and then of course if I change this event model I’m also going to delete schemas dopy we’re just going to use models.py in this to keep it simple and then inside of routing we’re going to go ahead and do some changes in here because it’s no longer event schema it is now just event model so all the places that had event schema I’m going to change to event model now the reason I’m calling event model is you might as well think of it as event table or event database table and that’s kind of the point here so that’s why I’m going through the process of changing all of the places where it said event schema and now also when I look at the schema stuff I can kind of think of those in terms of not being a database table or not being a database model um and that’s kind of the point now these could be pantic models but in terms of the documentation from SQL model they recommend you use SQL model itself there’s probably advantages to it that we could obviously look up but for now we’ll just leave it as is now we need to connect python to SQL by actually creating a database engine instance and then actually create the tables themselves so inside of the DB module here we’re going to go ahead and create session. piy this will handle something else later which is why it’s called session but the idea here is we need to create the engine itself so engine equals to something and then we need to actually Define something that we will initialize the database itself which should be something like creating database okay so before I actually even create the engine itself we need to know when we’re going to run this init DB stuff now the idea here is in main.py this is where we will run it in what’s called a lifespan method for fast API but before I show you the lifespan method I wanted to show you the old way of doing it which is an event so if you actually came in here and did app.on event and something called startup you could then Define on startup and then this is where you could actually run the init method for DB this is still a valid method it’s still works but it is deprecated meaning it’s not going to be supported in the long run so we do something a little bit different than this now if you have these onevent stuff you’re going to want to either do all on event or none of them before you do the the context method or the context manager method which is what we’re going to do right now so what I want to come up at the very top we’re going to go ahead and bring in from context lib we’re going to import the async context manager this right here and then up here we’re going to go ahead and use that async context manager it is a decorator so we’ll go ahead and grab it and then we’re going to define the lifespan but it needs to be async Define lifespan and it takes in the app which is a instance of the fast API app so you could absolutely use the fast API app in here if you wanted to this is we’re going to where we’re going to go ahead and call in it TB and then we need to yield something and then this last part would be uh any sort of cleanup that you might want to do so basically like uh we’ve got here let’s remove this we’ll go ahead and say before app startup then it yields to actually run it and then we actually can clean up anything in here now I think the reason they brought this in was because of AI models and with AI models you might want to run this lifespan thing in here so it kind of gives us before and after inside of our app but the idea here is we can pass this in to fast API so that’s what we’re going to be doing now we want to actually bring in that in method here so we’ll go ahead and do from api. db. session we’re going to go ahead and bring in initdb now of course we actually need the database to work in here so we’ll go back into our session and create this stuff out so the first thing is going to be our database engine we’re going to import the SQL model uh itself not SQL Alchemy but SQL model and we’ll create the engine by doing SQL model. create engine and then passing in the database URL string as we see here so that also means that of course I need to go ahead and do from. config I’m going to import the database URL and we’ll go ahead and pass that in here now the important part about this database URL is it does have a default in here of an empty string so I want to actually make sure that that’s not the case so we’ll go ahead and say if database URL equals to that empty string then will raise a not implemented error saying the database URL needs to be set now the reason I’m defining the engine here altogether is because later we’re going to do something called get session which will handle or use that database engine as well so now that we’ve got that we need to actually initialize the database itself which is going to be from SQL model once again we’re going to now import the SQL model class and we’ll use this class to create everything so it’s SQL model. metadata. create all and it’s going to take in the engine as argument so now what this is going to do is obviously make the database connect to the database and make sure that that database has all of the tables that we’ve created in the case of table being true now one of the things that it will not do is it will not search your python modules for any sort of instance of this class right here that’s not how SQL model works it only actually creates those tables these tables right here when those models are actually being used in our case they are being used and routing. we actually have this the event model in here it’s being imported this routing is also being imported into our main application from event router in other words the actual SQL model metadata engine thing should actually work now and so let’s go ahead and save everything if you do save everything what you might get is an error like this where pyop G is not installed and then in you also might get it in both places actually so what we want to do is make sure that pyop g is installed right now I don’t actually have it installed in here so of course we’re going to do that in just a moment now if you remember back to environment variables we actually did this plus pyop G in here that’s part of the reason that it’s looking for it that’s why um you know we’ve got this error in altogether is it’s looking for that specifically so what we need to do then is inside of requirements.txt is we need use that I’m going to be using the binary version which is why I put that bracket in there as binary this is scop G3 or version three there is another one called pyop 2 and you would see that and it would be something like this I believe that’s the old version that you would end up using so now that we’ve got this requirements.txt of course if you’re using Docker which going to end up happening is this should actually rebuild everything looks like we’ve got a little bit of an issue going on there but if you’re not using Docker which I have both of them up for a reason you would then just go pip install d r requirements.txt and hit enter this should install all of it using pyop G binary I think is the way to go uh it makes it just a little bit easier to run the integration itself there are other ways to connect it we’ll talk about that in a second but I just want to make sure that this loads and it runs it looks like it’s loading and running but I get this error in here this is another error that we definitely need to fix and we’ll talk about in just a moment but of course on the Docker side it actually rebuilt everything and Docker looks like it is working just fine so we’ll come back to our local python in just a moment the key here though is really this lifespan in here and that it is creating these database tables now we won’t know if these are created just yet we need to solve that one error so let’s talk about what that error is and how it relates to Docker and also using Docker locally in that last part we saw an error where psychop G was not available it was not found and the reason that we saw that error in the first place has to do with how we were mapping in our database engine so if we look into our session here we see that we’ve got this create engine for this database URL the pyop G error happened because of our environment variables with this plus pyop G and we can actually see that in both places whether it’s in our local python project or in our Docker composed version of it so both of these showed us that specific error now the thing about the SQL model and more specifically the underlying technology which is SQL Alchemy is it can use more than just postgrad it can use MySQL it can use Maria DB and there’s probably others that it’s supports so this create engine here from a database URL is very very flexible which is why in ourv we have to specify postgress SQL plus the actual package we’re going to use from python to connect which is why it raised that error because we never actually installed it once we installed it that error went away but it brought us a new error which is related to connecting to that database now this is not really that clear as to this right here of course if you do a search or if you look at this it will probably show you that there’s something with a connection that’s failing and of course if we look at our Docker composed one that one is actually not failing it is working correctly or at least it seems like it’s working correctly so the reason that the python one is failing is because of where our database is coming from it’s coming from Docker compose this DB service right here so inside of the docker composed Network which has both of these apps they can communicate with one one another that’s one of the great things about using Docker compose is there’s internal networking inside of Docker compose which is why we were able to specify our database URL in this way so I could say DB service which is literally the name of the service and it would be able to connect it’s a little bit different than like going on our local browser right so when we go into our browser and look at our app it doesn’t say app here it says 00 Z right so if I tried to do app here I would get a connection error because that is not how my local machine can connect to this app it has to be on 0000 or my Local Host basically I should be able to connect with simply local host or at least I could try to connect with Local Host and that also seems to be working working just fine so the way we connect to our actual database service though is a little bit different than the way we would connect to our app itself now the reason it’s a little different is the app itself is connecting slightly different than our browser so if we actually look in the EnV here we see that it says host value if I change this to Local Host I can try that one and see if that ends up working as soon as I save it let me just restart the python application itself and see if that does it that seems to do the trick right and so if for some reason that was not doing the trick I want to show you one other thing we could try as well so let me close this out and inside of Docker compose I’m going to go ahead and get rid of this expose here for a second we’ll save that and we’ll run this again and it’s still connecting so we’re doing okay in terms of our ports are concerned but every once in a while you might need to go into EnV and change it from Local Host to being something based off of Docker itself which is host. doer. internal so that might not be another environment variable you would need to run in order for your app to be able to run as well but right now this is still also giving us an issue so that is not one that you’re going to want to use you’ll want to stick with Local Host just like our application has been using before so the reason that I wanted to show you the docker host internal one so I’ll go ahead and leave this commented out right above here uh the reason that that even exists is because you might want to try it from time to time depending on your host machine itself and how that ends up working so it’s host. doer. internal will often be mapped as well so this is of course one of the challenges with trying to use Docker compose without putting your app in there as well but to me I think developing this way is fairly straightforward but of course every once in a while you might need to use a database service that’s not inside of your internal Network itself uh so there’s a lot of different things that we could talk about there but the point here is we need to solve that single connection error now this is also more easily solved by using a actual you know database that is definitely going to work like if you went into time scale and created a database you should be able to actually bring in that connection string it might also still need to use this stuff right here because of how we need to connect but the actual host and all that stuff all of these things would then be based off of like time scale an actual production ready one that we could then go off of as well of course we will see that very soon but at this point now that we’ve solved some of the database issues we can move to the next part which is actually using our notebooks to see if we can actually even store this database stuff which actually takes a little bit more than just using those notebooks let’s take a look let’s store some data into our database the way this is going to work is first off we need to create our session function inside of the DB session module we’re going to go ahead and create something called get session here and then what it’s going to return back is with session session the session class itself which we’ll bring in from SQL model we’re going to go ahead and pass in the engine in here and then as session this is going to yield the session itself now the reason we have this is because then we can use it inside of our routes that need a database session so the idea then is grabbing that get session method so we’ll do from the api. db. session import get session we’re also going to go ahead and bring in depends from Fast API and then we’ll go ahead and bring in from SQL model not SQL Alchemy but from SQL model we’ll import the session as well so all of this allows us to then come into an event like this and say something like session and this is of the data type session and then it equals to depends and get session that’s it so this function right there will yield out the database session so we can do database session stuff down here now the thing about this function now is it’s getting a little bit bigger so I’m going to go ahead and separate things out a little bit and I’m also going to put up response model up here just like that so we don’t have to use that Arrow method anymore you could do that on all of them both methods are supported and work just fine okay great so now we’ve got this database session I want to actually use my model I actually want to store things in my model so the first thing that I want to do is I’m going to go ahead and say object object and that is going to be our event model then we’re going to go ahead and do model validate of the data that’s coming through so this data right here we’re going to pass into validate so we’re basically validating it again but more specifically to the model we’re storing it with then we’re going to go ahead and do session. add that data so when I say obj I think of this as an instance of this model or basically a row of that class but I like calling it obj it probably is an old habit you can call it what you’d like just make sure that it’s consistent throughout your entire project whenever you’re doing something like this and then you’re adding it into the database so this is preparing to add it then session. commit is actually adding it into the database then if we want to do something with this object as in get the ID we would do session. refresh of that object and then we would just return back that object itself because it’s going to be an instance of this model class itself and that’s how we do it that’s how we can add data into our database it’s really just that simple now of course it we have to remember that if we want to use a session this is the argument that has to come in there we use the session variable through here if I called this sesh that would be fine you would just want to update it down here that’s not common so definitely consider that when you change these names but the idea is of course it’s a session class and it depends on that function which will yield out a session based off of our database engine which is of course why I have that database engine up there in the first place so now that we’ve got this we’ve got a way to actually add in data which we can verify by going into our notebook and trying this out so I’m going to restart in my notebook here and I’ll just go ahead and run it and what we should see is there’s some data in here with an ID now do we recall whether or not that ID was valid before I don’t know let’s do it again there’s another ID and we do it again there’s another one and we do it again and another one so we’ll Contin continuously increment this ID even though we didn’t specify it directly inside of here all we are specifying on that API request is the data that’s coming through and so it’s automatically incrementing it for us thanks to the features of a SQL database in this case it’s a postcg database but we’re automatically incrementing with this being a primary key as I mentioned before and we can see that result right here when we go through and actually add in more data now of course we need to fill out the other API routes as well which we’ll do in just a moment but before we do that I will say every once in a while as soon as you start filling up the database with some of this test data you’re going to want to come in here and do Docker compose down with that- v to take down the database itself this will delete that database and that Network it alog together and then you would just bring it back up with that watch command just to make sure that everything’s changing as need be now the database is completely fresh which we can then test again by jumping back into that notebook and running the same commands now we get a connection error with the notebook which doesn’t make that it makes a whole lot of sense with how these sessions work within a notebook so I’m just going to restart the notebook and then I’ll go ahead and try that again and I’m still getting a connection error so that’s probably not good this is probably a little bit of an issue with one of these things so let’s go ahead and restart this application which Port are we using up here let’s see here we’ve got a 802 so everything should be back up and running again let’s see looks like the database is ready let’s go back into the notebook here and maybe I just went a little too fast for it and there we go so now it’s back into being connected with that one let’s try this one here and there we go so we should be good to go in terms of that data and there it is auto incrementing okay so every once in a while you might need to let things refresh and actually rebuild as well so saving data and doing all that will help flush out that process because you’re probably not going to bring the database down that often especially not while you’re talking about it we’re now going to ask our database for a list of items a certain number of items that we want to come back this is going to be known as a database query that will return multiple items so the first thing that we want to do is look into read events this is where we’re going to be changing things because well that’s our API inpoint to list things out now the first thing I want to do is bring in the session because I’m using the database so I definitely want to have this session in there to be able to use that as well now once again we can use that same response model idea up here as well but this time instead of the event model it’s going to be the event list schema which we of course already have format it out right down here so the idea is we want to replace this list right here with the actual results so the way we do this is by doing something called a query so we set it equal to a query now if you’re familiar with SQL you might be familiar with something like select all um and then from some sort of database table itself which in our case is the event model we’re basically doing that as well but we’re going to be doing it from the actual event model class in other words the actual SQL model om or uh so which means that we need to bring in this select call here which we will go ahead and do select from this event model and so that is a basic query that we can do then from there we can actually get the results by executing this in the database itself which is our database session. execute and then the query itself and then the final thing is we would do all this would allow us to get a bunch of items in here and which case I should be able to return them just like that the reason I should be able to return them is because of this model itself is inside of the list schema also right so going back into that schema itself we see that the results are looking for instances of that model which is exactly what we’re doing here we’re returning those instances in this request that’s what’s happening with that so that’s at the the Baseline what is going to happen and then we can also grab the length of those results and then there we go so this should give us some results in here now if we actually go back into our notebook for the list data types here I added this print statement in here so we can actually see it a little bit better which allows us to see hey there’s the ordering that’s going on there’s all of that data of course if we were to add in some more data we can see this again so I’m going to go ahead and add in a few more items in here and just run this cell several times to just make sure that I have maybe over 10 items in here as we can see by this ID or maybe even over 12 so later we can use that 12 number again so now that I’ve got 12 in here let’s go back into my list data types and run this again and we can see there’s my 12 in here so this becomes a little bit of an issue is our results here have 12 items but maybe we actually only want to return back 10 items in which case we would come back in a routing and we would need to update our query here by having something called liit and then we can pass in the amount that we might want to limit in terms of the result itself in which case I should be able to go in here and then I can run that limit itself and then there’s 10 as a result and we see that there’s only 10 items in here now I can also update this so the ordering or the display order is a little bit different as well so back into our routing here we’ve got this query still we can come in and then do something like order bu and we can add in an ordering in here now how does this ordering work well it’s going to be based off of the actual table itself which of course that table is our event model inside of that table we have different fields in there in my case I can use ID that field is this one right here we could use page your description as well but those are the only fields we can really order this by and then we can do descending which just changes the order it flips the order in here which we could verify by going back into our API and doing another request and we see now it’s flipped and ID of 12 is coming up first and two and one are no longer in the results altogether and of course if I wanted to flip it back I can change it to ascending and of course that will flip back that order with different values in here okay so this would be like okay the next level of this would be doing pagination or allowing to have only 10 results coming back but then multiple pages of 10 so you can you can do this request several times to get all of the data cuz realistically you would not get all of the data once you know you would probably never do this command that’s probably going to end up being too many things so you would often use a limit you might have a bigger limit like 100 or 500 or a th000 it really is going to be depending on your API capabilities but the idea here though is we do want to have a limit of some kind and by default you may have noticed that the actual ordering was based off of the ID it was actually going up like this it was doing that ascending from the ID um which is also very nice it’s a very easy thing to work with but that’s our list view now that we’ve got that we might want to make it even more specific as in getting a detail view a very specific item that we can work with and then maybe eventually update as well let’s take a look using SQL model we will do a g or 404 error for any given event now what we’re going to do here is very similar to what we’ve done before first off I’m going to use the response model again and this time it is just going to be the event model like that and then now we want to build out the session query itself so I’ll go ahead and grab the session data and we’ll bring this in as well now what we want to do of course based off of this model we want to look it up for this event here so we need to define the query first which is going to be our select call and it’s going to be based off of this model here and then we use something called where and we can grab that model do a field name and we can set it equal to this event ID that’s being passed through and that’s going to be our query now so then from that query we get a result which would be something like the actual data itself which of course is going to be our session. execute and then we’re going to grab that query and instead of using all we’re going to use first the value here would then be returned and that’s pretty much it for our detail view or is it it’s Clos close it’s not quite the whole story jumping into our list data types here we’re going to go ahead and copy this git event down here and I want to just change it ever so slightly so that our path is going to be updated to be more appropriate for a detail view so I’ll call this detail path and that’s going to take in you know something like that 12 and then we’ll use the detail endpoint and I probably don’t need to put that base view in here anymore okay so now we’ll go ahead and grab that detail uh endpoint here for our request and I’ll just go ahead and set this to R and then we will do R all across the board and once again PR print this time instead of status R okay we’ll do our status code in here and see what that looks like okay so if we look at our list view we should still see uh that there is probably 12 in here right so let’s go ahead and just change our list view real quick to descending just to make sure that we have some data in here that is accurate to what’re looking up okay so back in here and list view there we go so we got an ID of 12 I’m going to go ahead and run this and there we go so we get that data of 12 if I go one Higher I get a 500 error that is a server error this should not raise a server error it should raise a different kind of error so this is not quite done and it has or rather this is not quite done and it has to do with this right here this query res returned back a none value and we can actually see the error in here that we get the input is simply none right because we’re trying to return back the event model but we’re actually returning back none so really what we need to do is say if not result then we need to raise something called the HTTP exception and it’s Capital HTP exception and we want to add in a status code in here of 404 and then some sort of detail saying that this item was not found or event not found right and so we need to bring in this HTTP exception from Fast API we’ll bring this in like that save it and now we’ll go ahead and try out this same sort of lookup and we’ll do it again now we get a 404 the actual error that you should have for a item that’s not found not found in the database in this case and so the cool thing about this is the same sort of idea if I made a mistake on this lookup and turned this into let’s say a string value here and and still did the lookup let’s take a look at what happens there 500 error this time so let’s go ahead and see what that error is right so it’s looking for the parameter with the ID of 13 so in this case it’s actually not looking up the ID uh based off of the string value right it’s not going to do that it’s going to give us another kind of error which shows us yet another time that hey you want to make sure that you’re using the correct data types when you do this lookup now the important part about the actual lookup itself the request that I’m making this is a string it’s just fast API parsing it into a number so that’s also another nice thing about this um but overall we now have our git lookup uh it’s fairly straightforward the next part is using this same sort of concept to actually update the data which kind of puts it all together the delete method I will leave to you it’s actually fairly straightforward and there is plenty of documentation on how to delete events which is something I’m not really concerned about at this point but updating one is a good thing to know because it kind of combines everything with we’ve done so far let’s take a look let’s take a look at how we can update the event based off of the payload that’s coming through keep in mind the payload is only going to have a certain number of fields the way we have it is literally the description that’s the only thing we’re going to be able to change in this update event which is nice because it limits the scope as to what you might potentially change so the idea here is we’re going to go ahead and grab the query and all of this stuff just like we did in the G event I want this to happen before I go to grab the payload data CU I don’t need it if I don’t have the correct ID now of course we still need the session in here as well and then of course I also want to bring in my event model as the response model to make it a little bit easier as well so I’ll go ahead and copy that and paste in here there we go and then I will update the arguments here just a little bit so it’s also easier to review as we go forward okay so let’s let’s go ahead and get rid of this now now I’ve got my session I can still look everything up as we saw before and this is now going to be our object this is really what it is I had it as result up here but it’s the same thing it’s still an object just like when we created that data so in other words we still need to do basically this same method here just like that and then when it’s all said done we will go ahead and return back that object so the key thing here though is the data itself how do we actually update the object based off of this data itself the way you do that is by saying for key value in data. items this of course is unpacking those things we can do set attribute and we can set attribute for that object of that key value pair that’s in there and again the actual incoming payload is only going to have a certain number of keys if for some reason you didn’t want to support those keys you could set that here so say for instance if k equals to ID then you could go ahead say something like continue because you’re not going to want to change that field but again you shouldn’t have to do this at all it’s a bit redundant based off of the actual schema itself but if you were to make a mistake then yeah of course you might want to do it then okay so with this in mind I should be able to run this and get it going so the idea here then is just checking this out or testing it out so if we look into the send data to API we have the API endpoint for this which is basically what we’ve done before so now we’ve got the event data here’s the description that we want to bring in this time I’m going to go ahead and say inline test and we’ll go ahead and run that now we’ve got inline test in here if I go back and list out that data let’s go ahead and do that and see what the description is now and there is that inline test so we were able to actually update that data as we saw uh just like that okay so this process is very straightforward of course if we tried to update data for an item that does not exist we will get a 404 right so going back into the send data here I’m just going to change it to 120 now we get a you know event not found and if of course if I change this response to status code we should see a 404 in here coming through great so yeah the actual API endpoint is now working in terms of updating that data again this one we probably won’t use that often but it’s still important to know how to update data in the case of an API not just with what our project is doing right and of course the challenge I want to leave you with is really just doing the delete method yourself self which is actually pretty straightforward given some of this data in here as well so at this point we now have update we have create and we also have list so we’re in a really good spot in terms of our API the last part of this section what we want to do is adding a new field in this case I’m going to be adding a timestamp to our model to the event itself having an ID is nice because it will autoincrement but actually having a timestamp will give us a little bit more insight as to when something actually happens now what we’re doing here is we are actually going to delete our previous models and then go ahead and bring things up in other words I’m going to come into my Docker compose down and then- V this will of course delete the database so it’s a lot easier to make changes to my database table so what you would want to do in the long run is use something like a limic which will allow you to do database migrations it will like quite literally allow you to alter your database table but in our case we’re not going to be doing that because once we have our model all well organized we should be good to go and not need to change it very often and if you do running this down command is not really that big of a deal altogether now before I start this up again and this is going to be true for any version of this app I want to build out the actual field that I’m going to do right now so the idea here is we’re going to go ahead and do something like created at and this is going to be a date time object so we’ll go ahead and bring in from date time we’re going to import date time okay and this is going to be equal to a field here now this field takes a few other arguments there’s something called a default Factory this default Factory has to do with SQL alchemy that will allow me to do something like Define get UTC now and then I can return back the date time. now and then we can actually bring in the time zone in here and do time zone do and UTC and then I can just go ahead and say um that this value should be replaced for the TZ info of time zone. UTC okay so just making sure that we are getting UTC now this will be our default Factory so when it’s created it’s going to hopefully go off of that function and work correctly now when it comes to the actual database table there’s another thing that we need to add in this is going to be our SQL Alchemy type the actual database type itself which is going to take in from SQL model which we need to import SQL model as well and oops that’s not what we wanted so we’ll do SQL model here and then I’ll go ahead and bring in SQL model. dat time and then this is going to be time zone being true so when it comes to SQL Alchemy you need to Define things for datetime objects it’s definitely a little bit different than what we have here but the SQL Alchemy stuff does creep in a little bit with SQL model when you’re doing a little bit more advanced fields datetime is one of such field and then we’re also going to go ahead and say nullable being false okay so doing nullable being false is yet another reason to take down the database and then bring it back up so at this point we should have this field automatically being created for us when we go forward okay so now I’m going to go ahead and run the Ducker compos up again and this of course should create the database tables for me and I’ll also go ahead and run my local version of the Python app as well which is attempting to create those database tables as well so the only real way I can test to make sure this is working at least how we have it right now is to create new data so we’ll go back into this notebook that will allow for it and hopefully everything’s up and running as it was looks like I’ve got a connection aborted so maybe I need to do a quick save on one of my models just to make sure that that’s up and running looks like it is now so let’s go ahead and try that one more time we definitely saw this before so I’ll go ahead and run it again and it looks like that time it worked okay great and so now if I go through this process I can see there is a time zone being created in here and of course I could do this as many times as necessary to update future Fields if I wanted to which in my case I’ll just go and go to five and notice that created at is in there as well now one of the other things that you might consider using this same idea of creat at is updated at so you could add a whole another field that’s very similar to this but it won’t have a default Factory uh necessarily you could still leave a default Factory but if you wanted to create it at field or an updated at field in your update you know command here you would then go ahead and set it again so you would say something like updated at and this is where you would go ahead and be able to set that field itself for that time stamp when it may have been updated that’s outside the scope of what we wanted to do here but the point is I wanted to make sure that I have a bunch of different fields in here that make a whole lot of sense in terms of actually storing data especially when it comes

    to a datetime field like this we need to see how we can actually store individual objects itself um at any given time and also have a field that is automatically generating inside of our database which is exactly the point of this whole thing let’s actually take a look at how we can do that updated field I’m going to go ahead and come into our event model here we’re going to paste this and change it to updated at just like that there shouldn’t be a whole lot of changes we would need to that but of course this also means that I need to go ahead and do Docker compose down of that actual entire thing so we can bring it down now back into our routing what I want to do is I’m going to bring in the git UTC now method here so it’s using the same functionality as when it’s being created so that it grabs what that value would end up being based off of all that data so then in our update view then we come in here and I’m going to go ahead and bring in object. updated at and then this is going to be the G UTC now and then that should actually set it and get it ready for when we go and store it okay great so with this in mind I’m going to go ahead and run this again we’ll bring it back up now one of the things the reason that we have it with the same default Factory is because when it’s initially created we might might as well also set the updated at field at the same time so they’re going to be the same value when they’re initially created but then in the future they might change if you then update them and let’s just make sure that everything is running looks like it’s all running we’re good to go so let’s go ahead and give this a shot so first and foremost we want to just send some data in which I will do with that post method right there and there we go so we’ve got an ID of one in here I’m going to change this to an ID of one as well and we should see that updated at and created at are the same time um it might be slightly different because of the microsc but overall the actual seconds are the same as we can see right here and right here great so now I’m going to go ahead and update it and we should now see a change the created at should be the same the updated AD should be slightly different and it’s not really that much different but it is different it’s different enough to see that that actually ends up working so that’s how you end up going about doing that now of course you could also have a field that is completely blank you don’t necessarily have to automatically update it uh in the future but that’s just a simple way to make that change so you can really see those time differences uh if you wanted to now the other nice thing about this too is then in our list view what we could do is we can come in here and do updated at and go based off of that value instead of we could also do created at of course but now we can go off of a different value alog together in which case we can come back in here and we can see the different data items that are coming through in here which we only have one right now so let’s go ahead and send a couple more and of course uh we’ll update the first one again just to make these things a little bit out of order and so let’s go ahead and try that again and what we see in here is now the different data is coming through on how it ends up looking the updated at is the first one because of course that’s how we defined it but I can always re flip it if I wanted to based off of the routing itself so now ascending so the oldest one being first we can go ahead and take a look again and let’s list that stuff out and now we’ve got the oldest one being first the most the the oldest updated one not the oldest created one uh being first and then the most recent created one being last or updated one being last again okay so cool um very straightforward uh I think that would have been really easy for you to do off the video but I just wanted to show you some techniques to do it in case you were curious about it and most importantly to show you this ordering stuff as well as to you can use updated and created app we now know how to store data into a SQL database thanks to SQL model it makes it really straightforward to use this structured data that is validated through pantic and then actually stored in a very similar way as it’s validated which I think is super nice and then extracting that data really looks a lot closer to what the actual SQL is under the hood which I think will actually help you learn SQL as well if you are trying to learn it because the way this query ends up being designed designed is it’s very much like SQL itself so SQL model I think is a really really nice addition to the python ecosystem and of course I’m not alone in that it’s a very popular tool but the idea here is we have the foundation to read and write data into our database now we need to elevate it so we can read and write a lot more data and do so by leveraging time series when it comes to analytics and events specifically we definitely want to turn it into time series data now the difference between it are going to be mostly minor based off of all of the tooling that’s out there so actually turning this into time series data is very straightforward so let’s go ahead and jump into a section dedicated directly to that we’re building an analytics API so that we can see how any of our web applications are performing over time in other words our other web applications will be able to send data to this analytics API so we’ll be able to analyze it and see how it’s doing over time the key word is overtime there now a big part of the reason that our event model didn’t have any time Fields until later was really to highlight the fact that postgres is not optimized for time series data so we need to change we need to do something different and of course that change is going to be time scale and specifically the hyper taes that time scale provides now we have been using a time scale database this whole time but we’ve been only using the postgress portion of it so now we’re going to convert it into a hypertable because that revolves around Time and Time series data it’s optimized for it now a big part of that optimization is also removing old data that’s just not relevant anymore now it removes it automatically and of course you can build out tooling to ensure that you’re grabbing that data if you wanted to keep it in some sort of Cold Storage long term but the point of this is that we want to optimize analytics API around time series data so we can ingest a lot of data and then we’ll be able to query it in a way that we’re hopefully kind of familiar with at this point by using postc so at this time we are now going to be implementing time scale and specifically a package I created called time scale python we’ll look at this in just a moment but as you can see here this is exactly like our SQL model with a few extra items a few extra steps to really optimize it for time scale DB now these extra steps are not really that big of a deal as you’ll come to see and a big part of the reason they’re not a big deal is because the time scale extension is the one that’s doing the heavy lifting this is just some configuration to make sure that it’s working let’s go ahead and get it started right now we’re now going to convert our event model which is based in SQL model into a hyper table or at least start that process so we can start leveraging some of the things that time scale does super well so the way we’re going to do this is we are going to go ahead and bring in from time scale DB we’re going to import the time scale model and then we’re going to swap out SQL model for that time scale model at this point it is still mostly the same so if we actually look at the definition of the time scale model by hitting control click or right click you can see the definition of this model it is still a squel model it is based off of that and then we’ve got ID and time in here so two default fields that are coming through and then some configuration for time scale specifically but let’s take a look at these two Fields we’ve got an ID field hey that looks really really familiar to what we were just doing with our ID field it has another argument in here that is related to SQL Alchemy on SQL model if you ever see sa like that that is referring to SQL Alchemy which is a another part or dependency of SQL model um it’s an older version of SQL model if you want to think of it that way now the other thing here is is we’ve got a date time field which hey what do you know we’ve got a default Factory of get UC UTC now it is a datetime object with the time zone and it’s a primary key as well so we actually have two primary keys so the hyper tables which is what we’re building is going to rely on a Time Field absolutely which is part of the reason we declare the time field altogether the other thing is we have a composite primary key which actually uses both of these fields as the primary key so the idea here then is we want to actually use this time field instead of the one we have so if we look back in our event model we had this created at field in here which we can now get rid of as well as this ID field now the other thing you’ll notice is we’ve got this get UTC now it’s in both places and so we can actually use the time scale DB version of get UTC now instead of having our own now of course you could still have your own but it’s not necessary at this point since we have this third party package that we are relying on we can use other fields to rely on it as well so the last real thing about this model is deciding what is it that we’re actually tracking here now in my case what I’m trying to track or the time series data that I’m looking for is Page visits so how would we think about page visits well that’s going to count up the number of visits at any given time right that’s the thing that’s what we’re counting up this is a little bit different than like sensor data for example so if this was sensor data you would have a sensor ID instead of a page which would be an integer and then you would have a value which might be a float right and so you’re going to then do something like the um you know value or maybe let’s say the average value of a sensor at any given time right so that would combine those two Fields but in our case we want page visits we want to count up the number of rows that have a specific page on it so the reason I’m bringing this up is mostly so that when we think about designing this model we want to rethink What fields are required in the case of page that one is absolutely required we we definitely need page otherwise it’s not an event so we’re going to go ahead and say that it is a string and this time we’re going to go ahead and give it a field with an index being true now making an index makes it a little bit more efficient to query but the idea here of course is going to be it’s going to be our about page or our you know contact page or so on and of course inside of time scale or postc in general we can aggregate this data we’ll be able to count the pages we’ll be able to do all sorts of queries like that which we’ll see very soon but the rest of the actual event model we could either get rid of or we can still use the updated at I’m probably never going to update this data but it is nice to have that in there and then a description in there as well so we’re really close to having this as a full on hypertable it’s not quite done yet and we still have to modify a couple things those are related to how we are using our session so we’re going to change our default engine in here and then we need to run one more command related to initializing all of the hyper tables it’s time to create hyper tables now if we did nothing else at this point and still use this time scale model it would not actually be optimized for time series data we need to do one more step to make that happen a hypertable is a postgress table that’s optimized for that time series data with the chunking and also the automatic retention policy where it’ll delete things later this is a little bit different than a standard postgres model but again if we don’t actually change anything it will just be a standard postgres model now we want to actually change things we want to commit those changes now the way we’re going to do this is inside of session. high we’re going to go ahead and import the time scale scale DB package in here and underneath in a DB I’m going to go ahead and print this out and say creating something like hyper taes just like that and it’s as simple as doing timescale db. metadata. create all engine this function here is going to look for all of these time scale model classes and it will go ahead and create the hyper tabls from them if they don’t already exist it’s really just a conversion process it’s still a postc model under the hood and then it actually turns into a hypertable now there are issues that could come up with migrating your tables so I’m not really going to talk about that right now it’s kind of outside the scope of this series if you’re going to be changing your tables very often but the point is that this right here automatically creates hyper tables the time scale DB package has a way to manually create them as well especially when it comes to doing migrations and making those changes that a little bit more advanced now the big reason that I’m talking about that at all is because we want to make sure our model is really good Ready Set because we probably don’t want to be changing our time scale model very often or really our hypertable very often unlike your standard postgres table you might change that more often than something like a hypertable now the idea here then is after we’ve got our creating hyper tables we have one more step that we need to do and that’s changing our create engine because we actually want a time zone in here as well so there is an argument that you can pass in time zone whichever time zone you are using in our case we’ve been using the UTC time zone which of course we see in our model itself where it’s get UTC now that’s going to be the default time zone we use and it’s the default one for the time scale model anyway now I recommend just sticking with that time zone because you can always change a time zone later using UTC makes it really easy to remember hey my entire Project’s in UTC we can convert it if we need to okay so the idea also with this time zone is we might want to put it into our config.py here which in which case I would go ahead and do something like DB time zone and then just paste that in here and then have the default being UTC once again in which case I would then import it into my session. and do something like that great so we’re not quite ready to run this command and when I say run this command I mean that we need to take down our old database and then bring it back back up so we can make sure that everything’s working correctly including any changes that I made to this model but there are a few other things that we need to discuss before we finalize this model and then has to do with some of the configuration that we want to use inside of our hypertable itself there’s a couple configuration items we want to think about before we finalize our hyper tables so let’s take a look at the time scale model itself and if you scroll down there’s going to be configuration items in here maybe some aren’t even showing yet the main three that I want to look at are these three first is the time column this is defaulting to the time field itself right so we probably don’t ever want to change the time colum itself although it is there it is supported if you need to for some reason in time scale in general what you’ll often see the time column that’s being used is going to be named time or the datetime object itself so that one’s I’m not going to change the ones that we’re going to look at are the chunk time interval and then drop after so let’s go ahead and copy these two items and bring them into our event model and I want to just go ahead and set these as a default of empty string here so what happens with a hypertable is it needs to efficiently store a lot of data which means that it’s going to store it in a chunk each chunk is like its own table itself now you’re not going to have to worry about those things time scale will worry about them for us the things we need to worry about are the interval we are going to create chunks for and then how long we want to store those chunks for in this drop after so this is actually very well Illustrated inside of the time scale docks by taking a look at a normal table and then taking a look at a hypertable a hypertable is a table and then it has chunks within that so as we see here we’ve got chunk one 2 and three and then each chunk corresponds to the interval that we set so for example this chunk is for January 2nd this chunk is for January 3rd and so on because the time interval is for one day the entire chunk is a day at a time that’s kind of the point here hopefully that makes a little bit of sense but the point of this also with respect to chunks is if you have a lot of data how you handle your chunks is going to be different if you have a little data right if there’s very little data your chunks could probably be a lot longer because you’re not taking up that much room in any given chunk and also you’re probably not going to be analyzing that data much for any given chunk so for example if this is your very first website your chunk time interval might be 30 days and it actually is going to be listed out as interval 30 days like that and so that’s how you write it you could say something like 30 days now one of the downsides of having an interval of 30 days or too long of an interval is you will have to wait after that interval passes to make a change so say for instance right now we say hey it’s going to be interval of 30 days that means all of the data coming in right now has a chunk time of 30 days if in a day or a week I want to change it down to one day then we have to wait that full 30 days before this new interval would end up changing now these automatic changes right now are not currently supported by time scale DB’s python package or the one that I created at least maybe at some point it will be but the point of this is we want to decide this early on I actually think interval of one day is pretty good for what we’re going to end up doing if we start end up having a lot of data then you might want to change this interval to like 12 hours but more than likely I would imagine one day is going to be plenty for us so the next question comes when do we want to drop this data now time scale will automatically drop this data based off of what we put in this drop after so in the case of our interval here we could absolutely say drop after 3 days but that actually doesn’t give us much for time for analyzing our data and what do I mean by drop after I mean quite literally after 3 days this thing would be deleted this would be completely removed from our table it’s not going to be there anymore this helps make things more uh optimized and also improve our performance altogether while also removing like storage that we just don’t need anymore we might not need that anymore so we could always take that old data and put it into something like cold storage like S3 storage but what we want to think about here though is when realistically would we want to drop some of this old data again it’s going to be a function of how much data we’re actually storing so this interval can also change as well so for example if I did two months then I would only have at most the trailing two months data if I wanted to have a little bit longer than that like 6 months then again I would only have at most 6 months of data at least in terms of querying this specific time scale model again we can always take out those chunks and store them later if we wanted to in my case I’m going to go ahead and just leave it in as three months I think that’s more than enough you might also change it to one month and so on um and you can change these things later but I wanted to make sure that they’re there right now so that we have them and we understand kind of what’s going on with our hyper tables or hopefully a lot more as to what’s going on with our hyper tables so the way we’re we’re going to be chunking our data is going to be with one day and we’re going to keep the trailing one day for 3 months so we’ll have at least that much data going forward let’s go ahead and verify that our hyper tables are being created first and foremost I’m going to go into session. here and I’m going to go ahead and comment out the process of creating those hyper tabls because we’re going to do it in just a moment then I’m going to go ahead and run Docker compose down just like that and then we’re going to go ahead and bring it back up so that we can quite literally have this working once again okay so we should see creating database regardless of how you end up doing it but the idea here is we now have our database up and running so to verify this what we’re going to be using is something called popsql or popsql decom and just sign up for an account it’s free especially what we’re doing and you’re going to want to download it which is going to be available on this desktop version right here and you can go ahead and download it for it your machine once you do you’re going to open up and you’re going to log in you’re going to probably see something like this so the idea here is you’re going to select your user and click on connections and then you’re going to create a new connection so we’ve got a lot of different connection types in here that we can use to really just run different queries that we might want to in our case right now we’re just going to go ahead and verify that our database is a hypertable database or has a hypertable in it so that means that we’re going to be using time scale here now the idea here is we could also use post credits both of them are basically the same right the connection type is basically the same it’s just a little bit different so we want to use time scale to verify that it is a hypertable when we look at it and then the next part is going to be our connection type we’re going to go ahead and use directly from our computer we want to make sure we do that and of course this is going to be on Local Host and the port here is 5432 the standard postest port and it’s also the same port we defined in composed. yl this port right here the first one of the two great so now we’ve got that we now need to fill out the rest of it the database name which of course is our time scale DB the username which is going to be our time scale user and then also the password time PW and that’s it then I’m going to go ahead and save that and we can run connect and now what we should be able to do is grab this data so I’m going to refresh here and we can see there’s our event model and I should be able to go to the event model itself and we can see that it’s not a hypertable at least not yet so back into our session here I’m going to go ahead and change it to see if it will create those hyper tables from here so as soon as I save this I should see it says creating hyper tables and hopefully I don’t see any other errors this is true regardless of what version we’re using here uh but ideally speaking we have all of our hyper tables so if I refresh in here this might actually show me my hyper taes it might not uh so the reason that it might not be showing our hyper tables is maybe we need to just start over a little bit in which case I’m going to go ahead and go Docker compose down again and then I’m going to go ahead and do Docker compose up just to see if that ends up solving it it might be a caching thing as well so we would also look at that but there we go creating hyper tables and all that let’s go ahead and refresh in here right now it’s still saying no which it shouldn’t so let’s go ahead and close out popsql for a moment and see if we can just reopen this up again I think it’s a caching thing which is not that big of a deal at all we just need to refresh pop SQL itself so that we’ll be able to run with our previous connection now there’s a chance that we might need to reenter our credentials I don’t think that’s going to happen uh but here we go and now it shows sure enough it shows a hypertable right away and notice that compression is disabled currently but it does show us the number of chunks we have so what if we actually start making some data the way we’re going to do this is by jumping into our notebook here I went ahead and had this one single event right so we had this post event that we did before off the video I went ahead and did this event count where it sort of randomizes stuff but it creates 10,000 events for us so I’m going to go ahead and run that and what we should see in our t uh table at some point it will just be one chunk uh so it’s going to take a moment maybe for that to be flushed out completely or maybe we need to do a query alt together I’m not actually sure um in terms of why that’s not showing up just yet but it is bringing in all sorts of data for us which is pretty cool so we will’ll test this out a little bit more the key of this was just to verify that it does say hypertable notice that compression is disabled that goes back into our time scale model where the default for compression is false so you can absolutely add in compression to make the data even more optimized for storage uh but for now we’re just going to leave it as is and then we’ll go through the process of actually seeing all of this data we will use uh popsql a little bit more to run some SQL queries or really to verify them but we’re not going to run that many SQL queries instead we will do the related queries right inside of our notebooks using um you know SQL model as well as time scale DB the python packages now what would be best is if we actually ran this a number of times over a number of days to simulate the you know actual process of visits but of course I can still run this a lot and we get you know 10,000 visits per time or 10,000 events per time and we’d actually be able to see a lot of data coming through and here uh at any given moment right so 10,000 events and of course this is happening synchronously so it’s not quite like what a real web application would be like uh but it is still at least giving us a bunch of data that we’ll be able to play around with let’s take a look at how we can do some of the querying with the SQL model now that we have a hyper table and we are able to successfully send data to it I want to actually be able to query this table and I want to do so in a way that is still leveraging the event model itself before I create the API endpoints now we have already seen how we can query it in fast API but I want to actually design the queries outside of fast API before I make it an API endpoint itself just to make sure I’m doing it correctly now the way we’re going to do this is inside of a Jupiter notebook so we’ll go ahead and do NBS I’ll go ahe and do five and we’ll go ahead and do SQL model queries. IPython notebook and then in here I’m going to go ahead and import a few things the first thing is going to be S the next thing is going to be from pathlib we’re going to go ahe and import our path here so when it comes to being able to import something from a model like models.py there’s no real clean way to do this I can’t just go from source. api. events. Models import event model right this will most likely not work because there’s no module named SRC now this is in part because we’re inside of the NBS folder here and so there’s a number of different ways on how we would be able to import this or grab it but the way I’m going to do it is I’m going to say my SRC path equals to path of dot dot slsrc and then we’ll just go ahead and resolve that right and so if we resolve that and take a look at what it is that is that SRC folder right there and so what I want to do is I want to pin this to my system path so all I can do is sy. path. append will take that string of that SRC path here just like that okay so that will append it to our system path which means then I can actually just go ahead and do instead of SRC I can just use api. events. models and I should now be able to grab that event model and have some no errors related to it and I mean I could also just go through and manually add all of this stuff in as well into the notebook itself but having this event model allows for me to do a few other things as well like I can go ahead and do from api. db. session we can import the engine as well which of course is going to give us this engine right here which has the database URL already on it or at least in theory it does so that’s another part of that as well let’s go ah a and run that one sure enough it ends up working so next up what I want to do is I’m just going to do a very basic lookup a very basic query for this first part and then we’ll come back and start doing more and more advanced queries so what we want to do here I’m going to actually put all of the Imports in the same sort of area we’ll go ahead and bring in from SQL model we’re going to bring in the session itself and then run that cell and then to use this session we’ll do with session and then we’ll pass in the engine in here and then we’ll go ahead and say as session and then I’ll be able to do my different session things that we might want to this of course is going to be basically the same as what we’re doing in our rep it just fast API handles it for us with that session stuff and all that but we can actually do the same exact query in there so let’s go ahead and grab that and just verify this in here with our session and I should be able to print out the results in here as well um we also need to import the select so let’s go and do that as well and now we’ll run this and we should get the same sort of data coming back okay so that gives us a result but what I really want to see right now is before we build out the rest of the queries I want to actually see the raw SQL query here now what we do is we bring in compiled query equals to query. compile and then we want to go ahead and bring in something called compile uh keyword RS here which I’ll just go ahead and copy and paste but the idea is compile keyword ARS the literal binds being true I’ll show you why right now so if we print out the compiled query and then if we print out the string of the actual query we see a couple different things so I’m going to go ahead and print this one little line to separate a little bit so the compiled query query is this one right here the other query has a parameter in here that does not have the literal binds in here at all so the nice thing about this is we would be able to change the limit with that parameter uh this is not something we’re going to do right now the point is I want to be able to use this compiled query by going into popsql I open up a new tab in here and I can paste that query in and just run that and so what it’ll do is it’ll run everything that we have in here with all of that data so we’ve got the pricing data and all the pages and all that so notice that the pricing data looks like we when we import put it in it didn’t have a initial slash in there which is kind of interesting uh but overall the data is in there and there’s a lot of data going on and this is how we’ll be able to verify that our queries are good and so we can continue to refine what kind of queries we’ll be end up doing inside of our database now when you’re working with database technology it’s a really good idea to do some of these queries just to verify that it’s working before you were to actually put it somewhere else so now that we’ve got that I want to jump over into doing time buckets we’re now going to take a look at time buckets time buckets allow us to aggregate data over a Time interval so it’s actually a really nice way to select certain data that falls into an interval in our case we’re going to end up counting the correct data for the correct pages but we want to start with just the basics of the actual time bucket itself so one of the things I want to do is grab this session here because we will be using the session itself and we’re going to be working based off of roughly this and so the nice thing about these queries here is we can put it into a parenthesis and of course this is just python related but then we can sep create them out line by line now I don’t really care about the limit right now so I’m going to go ahead and get rid of that and I’m not actually going to run this query we are going to chunk it down a little bit by using the time buckets so to do this we’re going to go ahead and do from time scale db. hyperfunctions we’re going to go a and import the time bucket idea here and then we’re going to go ahead and add in bucket equals to time bucket and then we’ll add in some amount of time I’ll leave it in as one single day now we want to do it on a specific field that we’re going to end up using which in our case is that event model. time now this is where keeping the actual field the time field consistent makes it a little bit easier so if I need to change to a different model time is still that time so that’s kind of the idea here so we can actually select it based off of that bucket itself we can come in here and just select that bucket we can also add in our page in there as well so if I do event model and then page I can bring that one as well that of course is an arbitrary field at this point but now what we can do is actually see this so I will leave that compile query because I might look at it in a little bit but for now I’ll go ahead and do results equals to session and then execute of the original query and then we’ll go ahead and do fetch all to see what that looks like and I’ll use prettyprint to actually do this so let’s go ahead and import prettyprint so from prettyprint or PR print import PR print okay so I run that and now I can see all of this data coming through based off of time and all that so there’s a lot of data I’m actually not sure how much data is in there it’s not quite what I want though right so I want to change this a little bit and of course we don’t order it by updated at that’s not even being selected we’ve got our time bucket and then our page those are the only things that are being selected and if we scroll down we will see that there is all instances of that so like it’s going to have duplicates in here but it actually removed the original time to just being based off of the date right it’s not quite the same as what you would see up here which of course we could verify by running the results up there as well so if I came up here and ran those results I should be able to see that there are uh differences in the time itself so the datetime object in here is certainly different than this so already showing the aggregations at work is kind of the point I’m going to go ahead and move these import statements up a little bit uh but now that we’ve got that out of the way we’ve got a way to gate this data based off of buckets we want to do one more thing and that is I want to actually count the number of items in that time bucket or based off of that time bucket so the way we do that is we bring in from SQL Alchemy we’re going to bring in a function in here and so this function right next to the page is just going to be function. count and that’s it we can give it a label as well if we wanted to we can say something like event count in here and then we could reuse that label later but I’m just going to leave it as simple as possible by using function count if I go ahead and run this we get this other issue now so this is now not necessarily giving us all of the right data so I’m going to go ahead and get rid of this order by here altogether we’ll just use the select for now still not giving us exactly what we want now this is happening in part because we need to group the data somehow so how is this count going to work what is it actually counting so the way we do that then is we come in here and do group by and the way we want to group this is with the time and the page so those two things together will give us the grouping that we’re trying to count so I can go ahead and run this again now it’s showing me the grouping that we’re trying to count this is now looking pretty good I think so it’s giving us all of this data in here and what if we change this to 1 hour we should be able to see something very similar to that maybe we didn’t change very much in an hour let’s change it to one minute now we can see those chunkin coming through uh because of all that data that’s been coming through so if I were to run and send more data in it’s been more than a minute um I should be able to see a bunch of data coming through with these chunks as well we could always take this a little bit further as well but overall what we’ve got is now a Time bucket chunking and we probably also want to maybe order it so we’ll do order bu and in this case I’ll go ahead and say bucket and then we will maybe also do the page itself so the page model is being ordered and then uh or maybe not even in that ordering we can play around with these things a little bit oh we forgot a comma let’s run that again there we go and so now it’s in order right and we see and of course it will skip minutes right so if there’s no data in there it’s not going to have any chunks so it’s going to be skipping things which is what we’re seeing here now there is a way to do something called a gap fill which is definitely a function that the time scale DB package has uh so instead of using time bucket there’s time bucket Gap fill which allows you to fill in empty spots that are missing that might be missing uh by aggregating the data in my case I don’t actually want any empty data coming in here I’m just going to leave it as is because these are the actual data points and the actual counts I’m not going to guess what the counts might end up being now of course the actual analytics for a page would be a little bit different than this if we were actually grabbing web traffic right it’s not going to be consistently skipping minutes based off of the ingested data for all of the data it’ll just be for some of it okay so another thing about this is we can then narrow this down to the pages we want in here so for example if we go back into when we created this data I’m going to scroll up here I’ve got a few pages in here so I can go ahead and grab that and we can grab those pages and put them right in here as to like narrowing down the results that I want to have and the way we do that then is by adding a wear clause in here so we’ll do uh Dot where and inside of here we can then say that this page and then do in with an underscore one of these Pages all right so if we run that now it’s going to go based off of those pages and so if I were to do let’s just say the about page here those are what’s happening on that about page over that time over that time bucket so if we do one day not going to be that big of a deal right now but of course over time hopefully we’ll see those days come through and it’ll end up working based off of this data so it’s aggregating I think really well and it’s doing so with all the chunks that are available and all that so it’s really really nice and we have the ability to do all sorts of advanced querying in here the other part about this is our wear claw we could do it based off of date time as well so if we were to bring in additional Imports like from date time we can import date time and time Delta and then time zone right and so what we can say something like this where we say start equals to date time. now. time zone. UTC and then we can subtract time Delta and we could do something like hours equals to one and then we could do finish or end being basically the same thing and then we can just go ahead and add in another time Delta in here uh which would allow you to come in and say that the time so event model. time is greater than start comma and then event model. time is less than or equal to finish now that actually filters it down a little bit more but the time bucket is already doing some of that for us so if we were to go into the day and let’s say for instance it’s doing a Time bucket of the day but we actually narrow it down a little bit it’s probably going to give us different results so let’s see here we’ve got day the about page is 7500 but that’s happening within the last hour for today basically if I were to get rid of those and run it again it’s quite a bit more right so it is a the ability for us to filter down if we wanted to this I think is maybe potentially redundant to doing the time bucket because the time bucket already does that really effectively but you will see this from time to time especially when you go into the time bucket Gap fill then you might want to fill in the gaps of a specific interval of time now the thing about this bucket here is you could still do a lot of this stuff in standard uh postgres itself so these SQL queries can get a lot more complex of course um and then what we have here it just so happens that you know hypertable and more specifically time scale has some of these functions built into it that allow for it to happen efficiently so this kind of aggregation is very efficient and then we can take that compiled query let’s go ahead and get rid of the printed results here let’s take that compiled query from the bottom which would be pretty big so I’m going to go ahead and actually start from the top and hopefully grab that whole thing and we will bring it into popsql here and we’ll go ahead and run that new query which let’s make sure that it does finish with page at the end there it looks like it does then we’ll go ahead and run this and now it does basically that exact same query but we can just test it right inside of SQL itself uh which is exactly why we have that compiled query there in the first place um but of course you might not actually run through the SQL stuff very often you might just be running it with python but the underlying SQL is definitely still there and it works essentially the same way as SQL model does here so that’s pretty cool it’s a very straightforward way to do these aggregations and be able to start analyzing our data so all we really need to do is turn this into an API route that will allow for us to do basically this stuff so let’s take a look at how we do that now it’s time to bring in that time bucket querying and aggregating into fast API the way I’m going to do this is I’m going to actually remove our old list view our old events list view to being specifically just aggregations now when we start getting a lot of data it’s really unlikely that I’ll use the list view very often if at all to see individual items I could always bring it back later if I wanted to but realistically I want to see aggregations here this is going to be that lookup so the way this is going to work is by going into our model here we can grab all of the things related to the query I don’t need the compiled query and I will use results in here in just a little bit but the idea is bringing in this query and getting rid of what we had before so I’ll go ahead and paste this in this of course means that I’ll need to import several things which of course I already have some of the Imports on here so I’ll go ahead and bring those in as well as well and then we need the time scale DB import as well so let’s go ahead and do that too okay great so that should give us our new query in here and then the result here I’m going to go ahead and do fetch all and that is actually what I want to return now is just simply those results so that of course means that I need to update my response model so I’m going to leave some of the default arguments in here to start but I do want to change the response model now the response model itself is going to be very uh much based off of what we already saw in here so if we were to run this again we can see that it’s a datetime object the page and then some sort of count in here so that’s what I want to have as well but I want to be a little bit more specific about that before I create the event list schema or the re response model together so that means inside a select I want to add a label to each one of these this label we can go ahead and call it bucket this one is going to just simply going to be page and then this one right here is going to be labeled as count what do you know basically the same thing as what they are but just adding this label on here which is a characteristic of SQL model uh will make it really easy to then have a response model of some kind now let’s remember what’s happening here this query is going to be searching more than one it’s always going to return back a list which means that I also want to bring in from typing we’re going to import list here and we’re going to go ahead and use a list of some kind so back into models.py let’s go ahead and actually create our new schema which is going to be Loosely based off of this event schema not quite the same but count will be the same then we want to have page in here which is a string itself and then we’re going to have a bucket in here which is a datetime object right and so we already have that imported in here because of updated but the idea is this is now going to be our event uh we can call this our event bucket schema something like that and then we’ll go ahead and bring this back into our route we’ll use that as our import now and that’s what we’re going to list back that’s what’s going to be our new response to this request so not a huge difference now one of the things we could think about too is maybe we change this query to being in its own model or its own field here like maybe something like services. that’s not something I’m going to do right now because I really just want to work on what we got here now the other things I’m going to get rid of are this start and finish I don’t necessarily need that I’m just going to go based off of the bucket itself um you totally could do the start and finish in there but using just the bucket will allow for me to do these as query parameters which we’ll do very soon but now that we’ve got this API endpoint let’s just make sure that our server is running uh so we’ll go up here and it looks like we need to maybe save models up pi and there’s our oh we got a little little error there let me just resave that there we go so it looks like everything’s running both in python as well as Docker great so now I’m going to go ahead and make an API call which of course we already have that endpoint in there with list data types I can go ahead and run each one of these I might need to restart the server here or restart the notebook itself um but there’s my results right there right and of course these are off of one day so I want to be able to change this I want to be able to come into my request and do pams and say something like duration and we’ll go ahead and change that to 1 minute right I want that to be a URL parameter that I can change and maybe in here I also want to go ahead and say pages and add in something like slab and let’s see another one maybe contact right and so there we go great so those are the parameters I want to bring in now we can do this fairly straightforwardly inside of fast API which uses a query parameter so the query parameter we can bring in as page and this is going to be an st Str or rather not page but we called it duration and this is going to be a string of some kind which basically will be a default of one day okay so that query we need to bring in to from Fast API we’ll go ahead and import query in here and so having a default in there is nice because then I can use that as my parameter here let’s make sure we put a comma great okay so the next one is going to be our Pages this is going to be a list um but the query itself we’ll go ahead and have a default of none or maybe just a default of an empty list let’s just try with none for now um and we’ll leave it as is okay so this is going to be our default Pages now the pages itself I’m going to go ahead and call these lookup pages and I’ll have some defaults already in here we could always say something along the lines of our default lookup pages and set that up here so we can kind of readjust that as we see fit but we’ll have some default lookup pages in here that will basically be this right here and we’ll go ahead and use those inside of our query here for that lookup great so the idea then is inside of here is if there are Pages coming with the query we would say uh Pages basically and we would then want to check if is instance of a list and pages is greater or the length of pages is greater than than zero else we will go ahead and use this so a nice oneliner for the condition basically if all of these conditions are met it’s going to use those pages otherwise it’s going to use the default ones and then that’s now our lookup and now our API endpoint with that time bucket and all of those aggregations let’s just verify that this is working by going back into our notebook and there we go so we’ve got it at 1 minute here’s that one minute we could also verify this by taking a look at the time that is happening obviously if you had a lot more data it’d be even more clear uh then we can also say something like one week right so we can change it as we see fit and it shows only those items in here if I were to use an empty list here it’s going to then go ahead and use all of them that I have available in my default ones now in my default ones I’m actually missing one which is when I was playing around myself I didn’t actually put a uh leading slash on pricing as well so I did it in two ways so when you actually end up doing this you might want to have some default pages that are in there uh but overall once I actually add that other one and we can see that that one has a lot of data as well now this distribution is probably unlikely I doubt about page would you know be far greater than anything else uh but the idea here or even the contact page being that high right so the analytics here is the data points are actually only because when we created it it just did some pseudo random creation the actual data once you put this into action would look quite a bit different okay so pretty cool now we’ve got a actual API point to get the aggregations from our analytics we have a way to create the data we have a way to aggregate the data both things are what’s critical here and what we will be using once we actually put this thing into a deployed production environment we now want to augment our event model so it’s a a little bit closer to extracting real web data so like for example you want to know what web browser people are using to access these Pages or you want to know how long they spent on those pages now that is more realistic web analytic data now the point of this is to show you how you can add additional fields and what you would need to do to be able to use the time bucket equation to make sure that you can also include those fields and see how those might play out so the idea here then is inside of our event model we’re going to change it now before I make any changes here I’m going to go ahead and run the docker compos down- V to make sure that all of the data has been removed now the reason for this is because I don’t have database migrations in place to make these changes you could have database migrations in place I don’t so we’re going to go ahead and remove some of the fields that are in here by default I’m going to go ahead and get rid of the comments even for them because we no longer need those but I want to add in additional Fields the ones I’m going to add in are these right here we’ve got our user agent otherwise known as like the browser the web browser that’s being used their IP address to identify their machine itself um the refer like you know if it’s coming from Google or from its own website the session ID which you might have in there as well and then how long they spend now of course you could always have more data than just this as well so what we’re not going to do though is change this data we’re just going to ingest this raw data and we will be able to change it later that’s more specific to the user agent so that you can see the specific like operating system that’s being used as we’ll see in just a moment so now that we’ve got this event model I also want to get rid of some of the schemas that are going to be a little bit different or at least change them so the create schema is no longer going to have this description in here it’s just going to have these things the update schema I’m not going to allow updates any longer so I’ll go back into my routing and I will get rid of all of the things related to updating any of these events seeing how to update things was important but now we no longer need them so go ahead and get rid of that as well and of course you could always review the old commits to see all of that old data if for some reason you wanted to go back and see it so at this time we don’t actually have that much different in terms of our data itself so let’s go ahead and bring back Docker I’m going to go ahead and do Docker compose up watch and then I want to make sure that my models everything’s saved and I should see something along the lines of we’ve got a database in here great so these actual Fields themselves maybe you want to change them in the future maybe these aren’t actually the ones you’ll end up using that’s not really the point here the point here is how do we add fields to the data we want to collect and then how do we look at that in terms of our events themselves so this actually means that we need to modify the data we’re going to send so inside of our data here I’m going to go ahead and do a pip install Faker Faker is a python package that allows for us to have some fake data and I’m going to go ahead and grab some of the data that I have which is going to be just this import here I have additional pages in here now I have 10,000 events that I want to bring in Faker can allow for you know fake session IDs in here we can have however many that we want in this case I’m just going to use 20 but if you want to use 200 feel free to do it we still have the same AP in points I added a few refers in here and so we can actually start the process of building out this data so what we are going to do here is very similar to what we have up here where we’re going through a range of events and we’re going to get a random page which we have right there then we’re also going to go ahead and get a random user agent which we can use Faker for so Faker has all of these user agents in here that you can go off of right so just make sure that you have all of that fake stuff implemented then we’re going to go ahead and just create a payload based off of this data so the payload is going to be well simply these key value pairs in here it’s really just a dictionary but I like to looking at it this way uh just to make sure that it’s all working great Okay so we’ve got a duration maybe we do 1 to 300 there’s a lot of things that we could do in that realm but the point here is we now have a new payload that I can send back into my database or into my actual API service with all of that data so I’m going to go ahead and run it and there it goes so we can see all of the different data items that are coming through in here uh we see the user agent and all that okay so while that’s running I’m going to jump into my queries from the SQL model now actually what I want to see is the list data I’m going to go into the API request themselves and we’ll just go ahead and run this within that API request there we go we actually see the same sort of data coming back so if I say something like five minutes we should see a lot of that same data coming back as well now the pages are going to be a little bit different because of my original routing how I had it set up so let’s go ahead and grab these new pages in here as our new default pages so go ahead and copy this and then we’ll go jump into our routing and our new defaults are going to be those right there great now of course you don’t necessarily have to even narrow down the defaults you could remove that Al together which would allow for all of the pages to show up okay so with that in mind now that we’ve got that there we’re going to go ahead and run this once again in that list data types let’s go ahead and run it and there we go so now we have a bunch of different pages in here and we can see the contact and all that stuff okay so now what we want to do is we want to see this a little bit different so going back into the routing we want to add a new field in here and we want to count the number of instances for that field in other words most of this is basically the same so let’s go ahead and do that with let’s say for instance our user agent so if we came in here and we did event. user agent and then we added a label as something like UA like user agent we could do something along these lines where we’re now selecting that data if we go to group that data we totally can which would just be adding in that user agent and then we can order it by that data as well depending on how we see fit our result results now will be slightly different than before so we have to remember the way we are returning our data is going to play in right now so event bucket schema we need to make sure that we are using the label of UA make sure that that’s in there as well so the event schema here we’ll go ahead and bring it in as simply UA this might have a or not UI but UA this might have an optional value in here as well uh which you just set to an empty string maybe uh but we’ll go ahead and save that we’ll save this and then we’ll go back into our list data here and we’ll run it again and now what we see is these things being sort of collected together and right now it’s only showing one count for some of these different user agents so realistically this isn’t that great because it’s not really parsing the data the way I want to because there’s different versions and stuff well if you want to get super granular this is going to be great but we don’t want to get that granular that’s maybe too granular for us so what we want to do is take it a little little further and we’re going to change how we actually return back this data so back into our routing here what we can do is we can import another SQL Alchemy function here so I’ll come in and bring in something called case and we can add in something called a Operating System case so inside of my read events now I can come in here and say something like this where we’ve got our event model user agent and then it basically says hey if it’s one of these things it’s going to set it to something else and then that’s what it’s going to end up being otherwise it’s going to just mark it as other this now I can use as my user agent in here the label I’ll leave as operating system so we’ll go ahead and paste this in here then we will paste it here and here so basically taking place of our original user agent although it’s still the user agent once again I made some changes to the query which will affect the results which means that I need to update the schema itself in which case I’ll go ahead and copy the UA this time I’m going to go ahead and add in the operating system in here same sort of idea still optional let’s go ahead and leave that as is operating system is the label which is why I named it that way we have all these other labels for that same exact reason so there we go we save it let’s go back into our list view here do a quick search and now we’ve got a much better look at what this data could be we’ve got Android at the homepage Android at the about page how many times when all of that and we can keep going through and really see very very robust data so this concept here we could take even further right so we could use something like OS case to unpack different IP addresses so we can see the different parts of the world we can do all sorts of that within the query now I’m not going to go through those Advanced queries in this one but that is absolutely something You’ be able to do at this point let’s go ahead and take a look at how we might do the average duration so I’m going to go ahead and copy the operating system thing here and go ahead and do AVG duration this is going to be optional float and then we’ll go ahead and add in 0.00 or something like that where it’s an average duration value now back into our routing here what we can do to add that average duration is just put it into our select what you do that is funk. AVG then you grab the average of what that value would end up being which is the event model duration and then you set a label to it and it’s average duration and then there we go so now we have enriched this data with the average duration everything else doesn’t need a change because how we’re grouping it together is going to be based off of that operating system and that page and the time not the average duration that wouldn’t actually make any sense to group it by the Aver duration in this place okay so now that we’ve got that let’s go ahead and take a look inside of our list here if I do a quick search here um what I see is that average duration is coming through on each one of these and what we should be able to do is actually modify that to let’s just modify how we were sending this data in the first place by going back into send data here we’re going to change the average duration up by a lot I’ll go ahead and say 50 to uh 5,000 seconds which is definitely a much different look at the durations themselves which should give us a different response back to the actual data that we’re getting depending on our our you know duration parameter here so if I said every 15 minutes for our duration we run that our average durations will hopefully change at least somewhat now of course there would be a bunch of ways on how we can modify this but here’s another average duration that just popped up a lot that’s because of that change that we just did um there was a chance that that wouldn’t have happened but it’s overall really nice that we’re able to completely change how our analytics works and we can do it in a very short amount of time now this is where spending a bunch of time to build out more robust queries might be really useful we now have the foundation in place for a very powerful analytics API where we control all of the data that’s coming in and ignore the data we don’t want then we can also analyze this data with our very own queries now this data itself the actual SQL data that’s coming through the table design probably could be done in postres by itself but the real question here is how efficient or how effective will it be as your data set grows a lot and the answer is it won’t be the nice thing about time scale is you can just add it whenever you need to so if you want to start with just a SQL model you totally can do that now of course the package that we went through won’t necessarily automatically do that just yet maybe at some point it will but right now it doesn’t do that you kind of have to decide this from the get-go in terms of this particular package but in the long run in terms of time scale you can add this at any time and there’s a lot of options for that which is really nice and it gives us a lot of flexibility and the fact that it’s open source means that we can just activate it in our postgress database and We’re Off to the Races but what we want to see now is we want to take this to the next level which is actually deploying it so we can see it in production what it might actually function like on our own systems let’s take a look at how to do that in the next section we’re now going to deploy our application into production using time scale cloud and Railway now time scale Cloud itself will allow for us to use time scale but not worry about running time scale it’s a managed version so all of the performance gains the new releases the bug fixes all of that stuff is going to be in the managed version so we don’t have to worry about it at all now do keep in mind that time scale is based on postgres so it’s still just a postgres database so yeah we could still use the open source version of time scale if we want but it will be a lot more simple if we just go and use time scale Cloud directly so go to this link right here so I get credit for for it and they know that we should make some more videos covering all of these things then we’re going to be deploying our containerized application into Railway now we’ve already seen how to build out the containerized application so this process is going to be fairly straightforward as well so we’ll be able to integrate the two of them and just have it run and we’ll test all of that in this section let’s jump in before we go into production I’m going to go ahead and add in some Kors middleware this is cross origin resource sharing Kors allows us to prevent certain websites from accessing our API this also means certain HTTP methods as well now we’re actually going to open up the floodgates on it mostly because we can actually turn this app into a private networking resource in other words the other apps that would access it need to be inside of that same network that is deployed similar to like you can’t access my version on my computer because it’s in its own private Network that you are not a part of that’s kind of the same idea when we go into production so for that reason I’m going to go ahead and open up my course here so we’ll go ahead and do from fast api. middleware does we’re going to import the cores middleware and then underneath the app here I’m going to go ahead and do app. add middleware and it’s going to be our cores middleware like this and of course you can go into the fast API documentation and get all of the different arguments you might put in here in our casee we’re going to allow all Origins all methods all headers we’re letting everything come through but you know if you were going to expose this to the outside world you might want to lock down the origins to like your actual website domain you might only want to allow get and post methods in here maybe not even delete right so that’s something else that you can think about going forward but this is one part that I wanted to make sure we had before we went into full production so now what I’m going to do is I’m going to push this into GitHub so what we’ve got here is in my terminal I’ve got git status I can actually see all of the files that I’ve changed now I’m actually not going to show you this process mostly because I also have git remote- V I actually have the entire code that you’ll be able to use in the next part we’ll actually use this code directly make a few changes so we can do the deployment directly I won’t do anything else inside of this project at this time other than just adding those cores and that will be on GitHub just a moment now what we’ll do is take it from GitHub to deploy it at this time go ahead and log into GitHub or create an account if you don’t already have one this account that I’m using is really just for these tutorials then you’re going to want to go to cfsh GitHub this will take you to the coding for entrepreneurs profile in which case you’ll go into the repositories here and you’ll look for the analytics API repository of course do a quick search for it if you need to it’s it’s going to be this one right here now the point of this is really to just go ahead and Fork this into your own project here so you go ahead and create that fork and it’s going to bring the code on over now what we need to do is we need to add some additional configuration to our now forked project now that additional configuration has everything to do with deploying fast API so if you actually go to fastapi container.com it’s going to bring you to this in which case will allow you to scroll on down and you can see the code directly that’s going to be used here now this code is just boilerplate code to deploy fast API into Railway that’s it so in our case we really just want this railway. Json because it’s something I’ve worked out to make sure it works really well for you now I’m going to go ahead and copy the contents of this which you can you know select all of it or just press this button right here then I’m going to go back into my repo the one we just forked from the coding for entrepreneurs profile then we’re going to go ahead and hit add file we’re going to create a new file here and I’m going to call this railway. Json and I’m going to paste this on in here now before I commit the changes I will say that it would have been or it will be a good idea to have it on my local project as well I’m just sort of assuming that you are not using git or you haven’t been this whole time maybe you don’t know it yet but of course if you do know it you know what to do from here but the idea here is we need to adjust our Railway settings to ensure that this thing actually gets deployed now what we see is this build command this build command is looking for a Docker file that does not have a period it just says Docker file so we need to use Docker file. web because that’s our actual Docker file path which of course is going to

    be the same for our watch patterns so we actually seen something like these watch patterns already composed igl has watch patterns what do you know now we actually didn’t update our composed yl very well this one probably should say web as well but the point here is we want to actually rebuild this application based off of these patterns here mostly for the SRC and requirements those are the main ones of course but Docker file is another one that’s important as well then we also have this deploy stuff this deploy stuff is looking for that health check do you remember when I said we will have a health check well if we actually look in the main.py code and scroll on down here’s that health check right there notice there’s not a traing slash here but this one is looking for a trailing slash let’s get rid of that TR trailing slash we want to make it the same as what’s in our code so now that we’ve got this I can go ahead and commit these changes and we’ll go ahead and say create railway. Json great our code is now ready for Railway there’s not really much else we need to do to deploy it so let’s go ahead and actually do that going to railway.com feel free to sign up for a free account jump into the dashboard notice that I’m on the hobby account this is a key part of this we’re going to go ahead and jump on over into our account settings and we want to make sure that our account Integrations are connected to GitHub so go through that process if you haven’t what you’ll end up seeing is something similar to this when you go through it and then it’s going to say hey what repositories do you want you could say all of them or you could select the one that you just forked which is what I’m going to do obviously I have two in here but I definitely want to have the one I just forked so I’ll go ahead and save that in here so that now I can go back into Railway and I can deploy this so let’s go ahead and do new now and notice that the analytics API is there it’s ready to go it’s ready for me to deploy it which is really cool it’s a very straightforward process in terms of the deployment but the key here is we want to look at our settings inside of these settings we should be able to scroll on down and we should see the Builder that it says build with a Docker file using buildkit mostly that it says Docker file. web in there if your does not say that that most likely means that your railway. Json in here is done incorrectly which might mean that that isn’t correct that’s kind of the idea beyond that we actually probably won’t need to change anything much notice that it has watch paths in there as well we should not have a start custom start command that’s not necessary one of the things we might need to change is the actual region but I’ll stick with this region for right now and then if you scroll on down notice the health checks in there the timeouts in there all of this stuff is in here things are looking pretty good okay great so what it actually does is it automatically starts deploying this might deploy it might not I actually think it would fail mostly because we don’t actually have our database yet so let’s go ahead and start spinning up our database and let’s combine the two in here assuming that you’ve already signed up for time scale Cloud you’ll log in and you’ll see something like this we’re going to go ahead and create our first database service right now so the idea of course is we’re using postgres so we can just go ahead and hit save and continue now we can select what region we want to use now the region you end up using will likely be close to you physically or close to where the most of your users are so if you’re doing this for somewhere else you’re going to want to put it in that region itself now Us West Oregon is the closest to me physically but it’s also the closest to where I actually have my app being deployed on Railway so if I come in here into my settings I can see the deployment is in California now there is one for organ in here as well so I could always change that too now that deployment is going to fail as well but I’m going to go ahead and leave it in as organ because inside of time scale I have the ability to use organ as well so now I’m going to go ahead and hit save and continue how much data I’m going to be using is practically none because we’re still early days we’re going to use devel velopment here and then we’re going to go ahead and hit save and continue and then we’re going to create this service and I’m just going to call the service name analytics API and then we’re going to go ahead and hit create service now we get a free trial here for 30 days which after that free trial it’s still very affordable in terms of the ability what we can do in here but notice we get a new connection string so I’m going to go ahead and copy this connection string and I’m going to bring it locally first so in myv down here I’m going to go ahead and say CRA DB URL and I’m going to paste that in now the reason I did this is twofold number one if I ever want to test my production URL locally I totally can now this also means that if you skipped using Docker compose this is the route you could do but then you would also use something like this where you actually change the connection string just slightly so realistically you would use something like that you would actually comment out this old one and then use this new one now my case I’m going to leave it as is and I’m going to go ahead and copy the entire new string with it commented out now I’ll go ahead and jump back into Railway into variables here and I can do this raw editor and just paste in those key value pairs right that’s it and then I’m also going to go ahead and update those variables as soon as I do that it’s going to attempt to deploy which I’ll go ahead and let it do now one other thing that’s important to note is how this is being deployed how it’s going to be run so if we go back into our Pro project a long time ago we created this Docker run file which has a run port and a host so this run Port is going to either be set by us or by Railway so we could set this port in here so inside of our variables here we could come in and do a new variable and we can set it to whatever we want I’m going to use 880 which I believe is the default on Railway but I’m going to go ahead and set that port in there as well and then we’ll go ahead and deploy that one as well so one of the nice things about railway is the ability to really quickly change your environment variables and then it will go ahead and build those containers for us and then run them for us very very similar to what we were doing with Docker compose in that sense now we also have this run host here now at some point in the future we might end up using this a little bit differently so at some point if you want a private access one you might need to put your run host the actual host itself inside of a variable as this value right here so you would do something along the lines of run host equ to that in which case I will go ahead and put this into my sample. EMV U you know compos file here just so we have it for later but that might be something if you don’t want to expose this to the outside world okay so after I did all those changes in a matter of minutes it was able to deploy the application itself and we can view the logs in deploy logs this will show us that it is running and notice the port in there says 8080 we also have our build l logs in here we could go through this this is building the container for us it happened really quickly and then it was did the health check which also happen really quickly and then of course it finally deployed so now what we can do is since it is fully deployed we need to of course access this you know API itself so inside of our settings here we go into networking we generate a custom domain now sometimes it might ask you what port you want to use sometimes it might not in this case it just defaulted to the environment variable Port that we have we might even be able to edit that looks like we can right in there which is really nice as well so the one thing that I want to check though is before I go any further is making sure that the deployment is in that region that I wanted and sure enough it is great so going back into our networking this is our API now this is the actual URL that we can use and here’s that hello world and of course if I go into SL API events the actual inpoint itself we should have no events which is very very straightforward so if if you are a casual viewer you will notice now this is a production deployment it’s fully production and it’s also using of course time scale if we go back into our time scale service here and go to the service overview we should see that it has been created right so we’ve got service information in here um we can see all of the things related to it by doing our Explorer in here we can see that there is a hypertable and what do you know there’s that event model all of that is working really well now of course we could always open up popsql and bring in that cloud-based version as well which of course is one of the things it’s built for and you can always test things out there you could also do these all of this stuff locally as well like we discussed okay so what we really need to do though is we need to test this endpoint the actual production endpoint and see if it’s working and how well it’s working let’s take a look let’s go ahead and send some test data to our production endpoint so what I’m going to do here is I’m going to copy the actual URL that I got which of course came directly from Railway it’s this URL here of course you could always have a custom domain as well but I’m going to go ahead and use the one that they gave us and then I’m going to jump into my local notebook here where it says send data to the API and I’m just going to go ahead and change our base URL to that exact value now I don’t actually want the trailing stuff in here I just want to make sure that I’m using htps and then the actual endpoint that it gave us always use htps if you can so now that I’ve got this I should be able to send out a bunch of data this one right here is not actually valid anymore we want to use the new data that we have uh based off of the new schemas that we set up and all the new models so here it is right here I’m going to go ahead and run it it is 10,000 events so in theory it should be able to send all of that data just fine it looks like it’s working fine now if I go into the production inpoint itself refresh an the air there’s some of that data it is now working and it’s working in a way that hopefully you have expected now this of course is now a production endpoint it is fully ready you are ready to deploy this all over the world if you want to and have it in all of your applications and just go wild with analytics but there’s still one more thing that I want to show you and that is how to use this privately now of course we could always sit here and wait for all of this to finish and have all of that data to come through I’ll let you play around with that at this point but let’s actually take a look at how we could deploy the analytics API as a private service so we can still use it this way but just not expose it to the outside world now it probably comes as no surprise that you don’t want to have your analytics API open to the outside world because then you might get events that aren’t accurate they’re not real so of course we need to change that we need to turn our analytics API into a private networking service only so you can have private networking in a lot of cloud services what we’re doing here is we’re really just removing the ability to have public networking which in the case of Railway we can just delete the actual endpoint itself and now there’s no public networking whatsoever but there is private networking and this is something that’s done by default in the case of Railway you have to use IPv6 which means that we need to update how we access this now we could spend a lot of time on Fast API itself to harden the system add security layers to allow only certain connections but as soon as we turn it into a private networking thing it just adds a whole layer of security right there so we don’t have to necessarily add all this additional stuff into our application so what we need to do though is we need to modify our Docker run command here and that’s going to be by changing the host I think I mentioned before that it was the Run host but it’s actually the host in here that we need to change and we need to change it to being this right here so that’s what we’re going to do now is we’re going to do host equals to that as our environment variable and it has to be these two Brack here this is how gunicorn is able to bind to IPv6 so IPv6 is that by default it’s ipv4 which is what this is right here so let’s go ahead and grab this and we’re going to go ahead and bring it into our analytics API as a variable we’re going to go ahead and bring it in here just like this go ahead and update it and deploy it now there’s a chance that this won’t work the reason that it might not work has to do with how this string is here it’s possible that this needed string substitution which we’ll see in just a little bit it’s also possible that it work just fine so once again we will see that in a little bit as well now it’s one thing to make it private and it’s another thing to make it private and still being able to access it so the thing about railway that makes it a little bit simpler is if we go into the settings on our application we can scroll on down to private networking and here is the new API endpoint that we can use inside of our Railway deployments the big question is going to be how do we access that without building out a whole another application I’m going to show you that in a moment so what we see on our API though is that it looks like it’s being deployed and it looks like we’re in good shape let’s look at our logs here more specifically our deploy logs it looks like it’s listening at that actual location so that’s actually really good everything else isn’t arrowing out we do see those print statements that’s a good sign so now we actually want to deploy something that will allow for us to test out these communications so I actually created a tool called Jupiter container.com which will take you to this right here which allows you to have a jupyter notebook in a server right inside of Railway in your own environment so the way we deploy this is by going back into Railway hit create into our project here so the important part of this is you are in the exact same project that you’ve been working on notice that I have a couple deleted in here this one I want to go in and I want to use it right next to the analytics API I do not want to deploy a new project which may happen if you just go to Jupiter container and deploy it directly from there so what we want to do then is come back into our application here we’re going to go ahead and create go into template look for Jupiter that’s with a py and Jupiter container you could probably use jupyter lab as well jupyter container works because I know it works that’s pretty much it um okay so the next thing is we want to add in a password here I’m just going to do abc123 that’s going to be a password this is going to be publicly accessible as we’ll see in a moment I’m going to go ahead and deploy this which might take a moment as well so it’s going to also have a volume on here for persistent storage which is actually kind of nice when it comes to wanting to use things okay so the next thing here is though going to be our variables now what I can do is I can add a variable to my Jupiter container that references this analytics API variable so let’s go ahead and do that I’m going to go ahead and hit new variable here and I’ll go ahead and say um let’s go ahead and call it analytics endpoint something like that and then this one I’m going to use dollar sign two curly brackets and I’ll type out analytics API and these are going to be the objects that we have the op and that’s going to be specifically the rail Ray private domain here so that’s the one that I want to have access to inside of this container which kind of connects them together so I’m going to go ahead and add that and we’ll go ahead and deploy it now these deployments do take a little bit of time because it is building a container and then it’s deploying the image based off of what whatever is going on but one of the main things here is as soon as I create that variable a line is created here showing that they are connected in terms of Railway this is important because the actual Jupiter container itself should have a public inpoint as we see right here so it already has one as far as the template is concerned without us doing anything else so that’s really my goal with this analytics API is to have it so easy that we can deploy it pretty much anywhere we need to as long as we have the necessary configuration across the board which of course would include our time scale DB in there as well so now what we want to do is just wait for this to finish so that we can log in to our service once it’s up and ready which we’ll come back to okay so the Jupiter container finished after a couple minutes let’s go ahead and jump into the actual URL for it there it is I did abc123 to log in of course if you forgot what that value was you can always go into your variable here and just look for this Jupiter password this container is meant to be destroyed almost as soon as you open it up so you can delete it at any time as the point so going back into that jupyter notebook here what we can do is we can jump into the volume and I can create a brand new notebook in here and we’re going to call this you know connect to analytics or something like that I’ll just call it connect to API now I’m going to go ahead and import OS here if you’re not familiar with jupyter notebooks well you probably are now all of these NBS here these are all Jupiter notebooks they’re just running inside of our cursor application so what I can do is I can use os. Environ doget and I should be able to print out the environment variable that we used for our analytics API endpoint so I’m going go ahead and do that and there it is right there notice there’s no htttp on there so that’s an important part of this as well so now what I want to do is import something called httpx httpx is basically like python request but it allows us to call the IP V6 endpoints themselves so our actual inpoint our base URL is going to be equal to http col slash this value right here which I’ll go ahead and set up here like this and just do a little magic here not really magic but you know we’ll go ahead and do some string substitution there’s our base URL let’s jump back into our send data to the API thing in here which I’m going to go ahead and copy this whole thing and we’re going to go ahead and bring it into our Jupiter notebook paste it in like that now I don’t have Faker in here I don’t think so I’m going to go ahead and install it so that installation is going to be a matter of pip install Faker and that should actually install it onto the jupyter notebook itself um and then we should be able to run this now of course the base URL here is now this one so I can go ahead and get rid of these two right here and I also want to use htps X not um using python requests so there’s going to be htbx and I think the API otherwise is basically the same let’s go ahead and give it a quick little run and we get a error in here for that one no service known in here so that’s a little bit of an issue that we will have to address before I do though I want to just take a look at the base URL and what do you know I didn’t do some string substitution so let’s go ahead and make sure we do the string substitution as well one would so that we have the correct base URL now I’m going to go ahead and run this and it still might fail now the reason it still might fail has to do with the port value we’ve got connection refused so when we actually deployed the public endpoint in here when we went through that process it asked for that Port value so this is why I actually mentioned it in the first place is so that we can understand that we do need to know the port value now you could go through the same process as the analytics API endpoint I already know what the port value is off top of my head and it’s 8080 so that’s the one we are going to go ahead and use and we want to use that Port right here this would be the same thing as if we were doing this locally as well and so now we should be able to see the same base URL I’m hoping this actually solves the problem it’s certainly possible that it still won’t solve the problem uh but of course if we look into our deployment for this analytics API we should see that it is running at that specific point this is the endpoint right here so instead of using whatever this is right here right we are using the actual private IP address name basically which is this right here okay or a DNS name rather okay so now that we’ve got that let’s go ahead and see if it starts to work it looks like we’ve got no response of okay so httpx doesn’t do the same thing let’s go ahead and actually just print out the data I’m going to go ahead and come back here and get rid of that and we’ll just go ahead and run this there’s that data coming back and it’s quite literally working in a private networking setting pretty awesome if you ask me so the other part about this of course is to verify this data I’m going to go ahead and stop this we don’t need to add in 10,000 things we can just do 10 for example we could also just G use the git command to grab that data as well so I’m going to go ahead and grab this response similar thing it’s going to be the create endpoint still because it’s the same endpoint to do.get and then we can get that data back and we can see what it looks like by just like that we’ll get rid of some of these comments run that and there you go we now have that data coming through as well now of course if we were to change the params let’s go ahead and change that in the case of the duration I think it was we’ll go ahead and say one day and that’s all all change and now the data is going to be a bit different right and we can also change the pages let’s go ahead and do that I’ll come in here and say pages and we go ahead and put in maybe just the uh let’s go ahead and do just the pricing page and we’ll see what that looks like and now it’s only doing the pricing data in there which is not nearly as many because well it’s going through one day which is you’re grabbing different operating systems over that day right so we saw that as well but the point here is we now have a private analytics API that is well deployed and leveraging a lot of cool technology to wrap up this section we are going to want to unload everything that we just did as in delete all of the deployed resources unless of course you intend to keep them in the case of time scale we’re going to come in here and just grab delete service and then we’re going to go ahead and write out delete go ahead and delete that service that of course will delete all of the data that went with it which of course is no big deal because we did a bunch of fake data we also want to jump into rail way itself and delete the entire project which we can do by coming in here whatever that project is you go into settings you go into danger and then you can remove each one of these items by typing out their names this of course is just to make sure that you clean up all the resources you might not be using in the future so I’m going to go ahead and delete both of these and there we go and then I’ll go ahead and delete the project which I think would also delete those other services but this process has changed a little bit since the last time I did it the idea here is we’ve got all of these things being deleted including the services we were just working on so at this time I won’t be able to access that Jupiter notebook at all any longer or our deployed production API endpoint but you now have the skills to be able to deploy it again and again and again because well the repo is now of course open source on github.com so feel free to go ahead and deploy your own analytics API and if you do change it and make it more robust than than what we have here I would love to check it out let me know hey thanks so much for watching hopefully you got a lot out of this now I will say that data analytics I think is going to get only more valuable as it’s going to be harder and harder to compete so it’s one of those things that I think I’m going to spend a lot more time in what I want to do next is really just kind of visualize the data we built I want to build out a dashboard and I encourage you to attempt to do the same thing after I have that Dash dashboard I really want to see how I can integrate it with an AI agent to see if I can communicate or have some chats about the analytics that’s going on but before I can even make use of chats or a dashboard I probably want to use this somewhere for real so those things are actually better and will help me make decisions either way it’s going to be an interesting technical problem and we’ll probably learn a lot and I hope to show you some of this stuff in the near future so thanks to time scale for partnering with me on this one and thanks again for watching I hope to see you again take care

    By Amjad Izhar
    Contact: amjad.izhar@gmail.com
    https://amjadizhar.blog