Hello everyone! Syntax is like the rules of a game that you can't change, it's like playing monopoly and trying to make up your own rules. It's just not going to work unless of course you are the game designer and you are the one making the rules. In Python, Syntax refers to the rules and the structure of the language that govern how you write code, it's the set of guidelines and standards that dictate how you must format your code in order for it to be valid and functional. Syntax includes things like the correct use of keywords, punctuations and indentation. For example, using a column at the end of an if statement and indenting the lines below it is part of the syntax for creating a conditional statement in Python. If you don't follow the correct syntax when writing Python code, you'll get syntax error. This means that the code you have written doesn't conform to the rules of the language and Python interpreted can't execute it. It's important to pay attention to the syntax when writing Python code. As even small errors can cause big problems, fortunately many modern text readers and integrated development environments or IDs have built-in features to help you identify and fix syntax errors as you write code which can make the process of coding in Python much easier. In Python, the syntax is relatively simple and easy to learn with a focus on readability and simplicity. There are some of the basic syntax rules in Python. Adhering to these syntax rules will help you write clean, readable and maintainable code in Python, like how to add comments, notes and explanations to your code without affecting its functionality, why capitalization matters in Python and how to use it correctly, write lines for naming variables in Python, how to create strings in Python using codes, the importance of indentation or white space in Python and how to use it to create blocks of code, how to indicate the start of code block in Python, how to break a long line of code into multiple lines for better readability, how to indicate the end of a statement in Python. So let's get started. You can add comments to your code using the hash symbol. Everything following the hash symbol on a line is ignored by the interpreter. Here you can see the example and everything following the hash symbol on a line is ignored by the interpreter. So this is a comment and there is another comment here you can see starting from the hash. Here you can see the variable definition that is not the part of the comment because comment is after this declaration. In Python you can use triple quotes either single quotes or double quotes to define a multi-line comment. So here is the example you can see in the start you can put the triple quotes and you can write your explanation or comment in between the triple quotes and you end the comment with triple quotes as well. So in case of multi-line comment, you need to define the start and end of the comments using triple quotes. Python is a case sensitive programming language. For example, hello and hello are considered two different variables. In the first hello you can see h is capital and in the second it's small. So here you can see the example hello is equal to 10 with all small letters and hello is equal to 20 with h is capital. So if you output the result you will get the corresponding result that you have assigned to these variables. Variables names can only contain letters, numbers and underscores. But remember variable names cannot start with a number. Variable names are also case sensitive. For example, my variable with v is capital and my variable with all small letters are two different variables. Variables names should be descriptive and cannot be the same as Python keywords such as far, while, if, etc. You will cover the Python keywords later. So here you can see you can start a variable with the underscore. Name is equal to John Doe. And also you can combine the numbers with the name of the variable. For example, name one, but you cannot start a variable with a number. So here is another variable age is equal to 30. You can define a string with single quotes, double quotes or triple quotes where triple quotes are used for multi-line strings output. For example, here you can see the example with single quotes, a is equal to hello are with double quotes, b is equal to hello are and with the triple quotes for multi-line strings output. And you can see after the hello word is in the next line. So you can define a string with triple quotes for multi-line strings output. Intentation in Python is a way of indicating blocks of code that are associated with the control flow statement such as far loop or if statement. In Python, indentation is used instead of curly braces or other keywords to define the scope of a block code. So Python uses indentation to define blocks of code such as loops, function and conditional statements. Python uses indentation to indicate the scope of a block rather than braces or keywords. Each line of code that is part of the code block must be indented the same number of spaces. The exact number of spaces doesn't matter as long as each line in the code block is indented the same amount. However, it is widely accepted convention to use the four spaces for Python indentation. Here you can see the example and after the if statement you will define the code block for if statement that will execute in case of if statement. So here you can see there are two lines of code for the if code block. Here you can see the first line and the second line. And you can note the same number of spaces for two lines since it's common to use the four spaces for indentation. So here you can see these are basically representing the spaces. So here you can see the four spaces are available before each line of code in the if code block. The start of a code block is indicated by a column at the end of a control flow statement. The statement that belongs to the code block must be indented to the right of the control flow statement. The indentation level must be consistent throughout the code block. For example, here you can see the if statement and at the end of the if statement you can see the column that basically indicate the start of the if code block. So here you can see the first line of code that prints x is positive and the second line of code that prints this is the part of the if block as well. So these two lines are the part of the if code block with the same indentation level. And the last line you can see that is outside of the if block due to its indentation level. In part on line breaks are used to separate statement using backslash symbol. A statement can span multiple lines, but it is generally recommended to limit a statement to one line. For example, here you can see you are going to add three numbers and assign the final value to x that you can do in single line. But you can use the line break with the backslash. Here you can see the line break. And if you print the result in the x, so you will got the 60 because the sum of these three numbers is 60. You can terminate a statement by placing a new line after the statement. For example, here you can see, then you terminated this statement with a new line. And the next statement is on the new line. However, in some cases, you might want to write multiple statement in a single line. In such cases, you can separate the statement using a column. For example, here you can see in this example, you write the same code in one line that you have written in the above example in four lines. In this code, you are going to add two numbers and then output that number. So it's your choice. Either you are going to do it in four lines or either you want to do it in one line. So here you can see x is equal to 10. And then you have used the semicolon to separate the statement. And then the next code statement y is equal to 20. And then you have also used the semicolon. And then the third statement and finally the fourth statement. And you will get the same result. That is the summation of the x and y. And that will be equal to 30. Think of Python as a language that you use to communicate with the computer. Just like any other language, Python has its own set of words and grammar rules. In Python, some words have specific meanings and purpose that are reserved for use by the language itself. Reserved words in Python are keywords that have special meanings and purpose within the language and cannot be used as an identifier. For example, variable names are function names. So now let's cover the reserved words in Python and is reserved in Python. That is a logical operator that evaluates to true if both operands are true. Or is a logical operator that evaluates to true if either operand is true. Not is also a logical operator that negates the value of its operand. True is a Boolean value representing true. False is a Boolean value that representing false. If is used to start an if statement. Elif is used in an if statement as a shorthand for else if else is used in an if statement to specify a block of code to run if the if statement evaluates to false. Far is used to start a far loop. While is used to start a while loop. Break is used to exit a far or while loop prematurely. Continue is used to skip the current iteration of a far or while loop and move to the next one. Def is used to define a new function. Return is used to return a value from a function. Lambda is used to create anonymous function. That is basically a function without a name. Async is used to define asynchronous functions. Yield is used in generator functions to return a value and pause execution allowing the function to be resumed later. Try is used to start a try accept block. Accept is used to handle exceptions and errors. Finally is used in a try accept block to specify a block of a code that should always run. Whether an exception is raised or not. Raise is used to raise an exception. Width is used to simplify the use of try finally blocks when acquiring and releasing resources. Assert is used for debugging. Checks if a condition is met and raises an error if it's not. Clause is used to define a clause. Dell is used to delete a variable list item or dictionary item. From is used to import a specific part of a module or import a module under a different name. Global is used to specify that a variable is global variable rather than a local variable. Import is used to import a module into your program. Ass is used as a keyword in import statements to give a module different alias. In is used to check if an element is present in a list. Topol set or dictionary is basically used to check if two variables are equal. None is a special constant representing the absence of a value or null value. None local is used to specify that a variable is non-local like it's defined in an enclosing function but not in the global scope. Pass is used as a placeholder in cases where statement is required syntactically but you don't want any code to be executed. So that was the list of the reserve words in Python. So you don't need to memorize it but before going deeper into the Python code you must need to have an overview of the reserve words but you will get used to it with the practice and with the demos. So you are gonna use all these reserve words while writing the code. See you in the next lecture. Now you will cover variables and their operations. Variables in Python are like boxes. You can put stuff in them, take stuff out but just like boxes you have to make sure you label them properly and remember what's inside or else you will end up with a mess. In Python a variable is a named location in memory where you can store values, you can assign values to variables and then use the variables in your program to store and manipulate data. A variable name can contain letters, digits and underscores but it must start with a letter or underscore but not with a number. In Python you can assign a value to a variable using the assignment operator. It is a good idea to choose descriptive and meaningful variable names to make your code easier to read and understand and this step is very important. Python is a dynamically typed language which means that a type of a variable is determined at runtime rather than being fixed when the variable is defined. This allows for greater flexibility. Variables do not have a fixed type and can store values of different types at different points in the program. You can define different types of variables in Python. You can assign values to variables and then use the variables in your program to store and manipulate data. So for example here we go, you can see the integer type variable x is equal to 42 and remember we didn't mention the data type of this variable because Python is a dynamically typed language which means that the type of a variable is determined at runtime according to the data that is assigned to that variable. Similarly, you can define float variable, you can define string variable, you can define boolean variable, you can define list, you can define topple, you can define dictionary with key values and you can define set as well. But here you notice we didn't mention any data type with any variable but we define different types of data types, variables. Variable assignment is the process of giving a value to a variable. This allows you to store data in a named location which you can later access and manipulate. In Python, you can assign a value to a variable by using the assignment operator. For example, here you can see the single variable assignment, then you can see the multiple assignment, here you can see x, y, z that are separated with the comma is equal to one, two and three. It means x value is one, y value is two and z value is three and you can also swap values for example, here you can see x, y is equal to y, x. It means value of y will be assigned to x and value of x will be assigned to y and you can also use the compound assignment. Here you can see the example x plus equal to one that is basically equivalent to x equal to x plus one. In terms of operations, Python sports a wide range of operations that can be performed on variables including arithmetic operations, assignment operations, comparison operations, logical operations, membership operations and identity operations. So let's see the arithmetic operation that you can perform. You can perform addition with the plus operator. For example, x is equal to one plus two and the output will be three. Similarly, you can perform the subtraction. You can perform the multiplication using the static operator. You can perform the division using the slash operator. You can perform the floor division using the double slash. You can perform the modulus and you can also perform the exponentiation using the double static operator that is actually to take the power. For example, x is equal to two double static three. It means two to the power three that will be equal to eight and here is the code to perform the arithmetic operations. So first of all, you perform the addition and then print the result of the x variable. Then you perform the subtraction. Then you perform the multiplication. Then you perform the division. Then you perform the floor division. Then you perform the modulus and the final code is to take the power. Now let's see the assignment operations with compound assignment. For example, you can perform the compound assignment with addition. You can use the plus equal to operator x plus equal to one that is basically equivalent to x is equal to x plus one. Similarly, you can perform subtraction with assignment using the minus equal to operator. You can perform the multiplication with assignment using static equal to operator. You can perform the division with assignment using slash equal to operator. You can perform the floor division with assignment using this operator. You can perform the modulus with assignment using this operator. You can perform the exponentiation with assignment using this operator. So, that's how you can perform the compound assignment with different arithmetic operations. In Python, a compound assignment operation combines an arithmetic with an assignment. This means that you can perform an operation on a variable and then immediately assign the result back to the same variable all in one line of code. Arithmetic operations are basic mathematical operations in Python such as addition, subtraction, multiplication, division, flow division, modulus, and exponentiation. These operations can be performed on numeric data types such as integers and floating point numbers. So, here is the Python code to perform the arithmetic operations using different compound assignment operations. For example, you define x is equal to 2 and then you use the compound addition assignment operator. That will basically add 1 to the x value. So, when you print this value, the output will be 3. Similarly, here you can see the code for other compound assignment operations. In Python, comparison operations are used to compare two values and determine if they are equal, not equal, greater than, less than, greater than, or equal to. The result of a comparison operation is a Boolean value either true or false. So, here is the example of equal to comparison operator. You define variable x is equal to 1, then you define variable y is equal to 2 and then you compare both variables using equal to operator. Since x value is not equal to y, so the output will be false. Similarly, the example of comparison not equal to operator, example of less than operator that you can use if x is less than y, less than or equal to operator that you can use if x is less than equal to y. Similarly, greater than operator that you can use if x is greater than y. Similarly, greater than equal to operator that you can use if x is greater than equal to y. and here is the python code to compare value of x and y using different comparison operators for example print x greater than y so the output will be false because x is less than y in python logical operators are used to combine or manipulate the values of boolean expressions there are three logical operators in python and or not the and operator returns true if both boolean expressions are true and false otherwise the our logical operator returns true if either of the boolean expressions are true and false otherwise the not logical operator returns the opposite of a boolean expression for example if the value is true it will return false so here is the code to use the logical operators so first of all here you can see we have defined x and y so in the first example we use the and operator if x greater than 2 and y greater than 2 then it will return true otherwise it will return false because both these condition must be true so here you can see the output the result is true because both conditions are true and the second example is the use of our operator x greater than 7 y greater than 7 output is false because both conditions are false and third example is use of not operator x greater than 2 and output is false because x greater than 2 is true but the not operator will negate this result so the result will be false in python the membership operation checks if an element is a member of a sequence such as string list triple set or dictionary there are two membership operators in python the first operator is in operator the in operator returns true if an element is found in the sequence and falls otherwise for example a is in apple so it's true because apple contain a and similarly x in apple apple didn't contain the character x so it will be false and the second operator is not in the not in operator returns true if an element is not found in the sequence and falls otherwise for example a not in apple so it will be false and similarly x not in apple it will be true so here is a demo code to use and understand the membership operations for example you have defined word is equal to apple then you use the in operator here you can see if a in word then print a found in word so output will be a found in apple similarly the example of not in operator another example with the list fruits is equal to apple banana and cherry so if apple in fruits then print apple found in the list of fruits if it's true and similarly the use of not in operator at this stage don't worry about the if and advance concepts we will cover all this concept later but this example is to make you understand the membership operations in python identity operation checks if two variables refer to the same object in memory there are two identity operations in python the first one is is not but this operator returns true if two variables refer to the same object in memory and falls otherwise the is not operator returns true if two variables do not refer to the same object in memory and false otherwise for example a is equal to this list and b is equal to this list and then you assign a to c so if you print a is b then the result will be false because these are two different objects in the memory and if you print a is c so here you can see you have assigned c is equal to a so it means c and a are referring to the same memory location so the result will be true in this case similarly here is the example of is not operator that will be the opposite of the is and here is the Python code first you define the objects and then use the is operator and you can see the output is false because a is not b because memory location of a is different from b and then you use the is not operator a is not b so the result is true because a is not b because the memory location of a and b is different in Python our data type refers to the category of value that a variable can hold for example the most common data types in Python include integers, floats, strings and booleans each data type has its onset of operations that can be performed on its values and Python provides built-in functions and operations that can manipulate and convert between different data types as needed understanding data types is important for writing correct and efficient programs as well as for working with external data sources and APIs that may require specific data types data types are classified into two categories the first one is immutable data types and immutable data type is one whose values cannot be modified once it is created example of immutable data types in Python include strings integers floats and tuples for example here you can see the string variable integer variable and tuple variables these are the immutable variables in each of these examples the value of variables cannot be changed once it is created for instance you cannot change the value of string variable to word without creating a new string the second class is mutable data types a mutable data type is one whose values can be modified after it is created examples of mutable data types in Python include lists and dictionaries so here is the example so you can see two lists are defined list one and list two in each of these examples the value of the variable can be modified after it is created for instance you can append a new item to the list variable or add a new key value pair to a dictionary variable here you can see you can modify the list one list one dot append four and if you print the list two you can see the four is in the list because you have assigned list two is equal to list one so another important thing is when you assign a mutable object to a new variable both variables refer to the same object in memory if you modify the object using one variable the changes will be visible when you access the object using the other variable so you can see we will append the four in list one and we printed out the list two and it reflect in the list two as well because we assigned the list one to list two in terms of data types Python has number of built-in data types. Python has the intЗr data type that represent the integer integer is mutable integer can be used for numeric computation storing whole numbers Python has float data type that represent floating point numbers float data type is immutable load data type can be used for numeric computations with decimal places python has complex data type that represents complex numbers complex is immutable data type as you know the complex number contain the real part and the imaginary part. So the complex can be used for mathematical computations involving imaginary numbers. Python has bool data type that represent the boolean value bool data type is immutable bool value can be true false bool can represent truth values in conditions and control flows. Python has none data type that represent a null value. None data type is also immutable and is used to represent the absence of a value. Python has a string data type str that represent the string string data type is immutable string can be used to represent the textual data processing and manipulation of strings. Python has list data type that represent list of values list is mutable. So here you can see the example of list of values. So list can be used to store and manipulate collection of values that can change dynamically. Python has tuple data type that represent immutable ordered list of values tuple is immutable data type. There is the example of the tuple. Tuple can be used to store collection of values that shouldn't change often used as records. Python has set data type that represent unordered collection of unique values. Set data type is mutable. Here you can see the example of set. Set can be used to store collection of unique values for quick membership testing and removing duplicates. Python has frozen set data type. Frozen data set are same as set but immutable set. You can define a frozen set like this. For example frozen sets can be used as keys in dictionaries. Python has dictionary data type that represents unordered collection of key value pairs. Dictionary data type is mutable. So here you can see the example of dictionary that contain key and value pair. Python dictionary can be used to store and accessing values by keys often used as a lookup table. So here is the demo code for different data types. So as you know the Python is dynamically typed language. So we don't mention the data type when assigning the value to the variable. Python determines the data type of the value that is assigned to the variable automatically at runtime. For example here you can see we have assigned x is equal to 42 that is integer type and you can output the data type of a variable using a keyword. So if you print type x and in the output you can see the data type is in. Similarly here you can see x is equal to floating number and in the output you can see the float data type. Here you define the string variable and when you print it out the data type its string then you define Boolean data type x is equal to true and you can see the Boolean data type then you define x is equal to list of values and here you can see in the output list data type then you define the tuple and here you can see the data type is tuple then you define a dictionary and here you can see the data type is dictionary and finally you define a set and if you print out the data type it is set. So you can find out the data type of variables as in Python you don't explicitly mention the data type when defining the variables. Now let's cover the data types conversion our type casting in Python. Python has a built-in functions for type casting to convert one data type to another data type. So this function converts x to an integer. Here you can see the example. This function converts x to a floating point number. This function converts object x to a string representation. This function converts x to a Boolean value true or false. This function converts x to a list. This function converts x to a tuple and this function converts x to a set and finally this function converts x to a dictionary and you can see the corresponding examples. It is important to note that not all data types conversion are possible and attempting to convert a value to a type that is not compatible with it will result in an error. For example trying to convert the string hello to an integer using integer function will raise the value error. So be careful while converting one data type to another data type. So for example you cannot convert a string that contains corrected to a integer data type. So here is the Python code to convert different data types. So first of all int function here you here you can see x is equal to a string and then you have converted x to an integer and output will be an integer. Here is the example with the float function x is equal to a string and then you have converted this string to a float and in the output you can see the output is in float but remember in these two example x is string but it does not contain any character in the string because it is not possible to convert if there is a character in that string. Then here is the example with the string function here you can see x is integer and then you have converted x to a string then there is the example with bool function x is integer and you have converted x to bool and here you can see the output is true then here is the example with the list function x is string and you have converted this string to a list using the list function and here you can see in the output you can see the string in the form of list then here is the example with tuple function x is equal to a list and you have converted this list to a tuple and here you can see the tuple here is the example with the set function x is a list and you have converted this list to a set and here you can see the set in the output and here is the example with dictionary function and here you can see x contain the list of tuple values and you have converted this to a dictionary and here you can see in the output and here Here you can see in the output you have converted x into a dictionary. So that's how you can convert one data type to another data type using Python built-in functions for typecasting. A control structure in Python is a block of code that determines how the program flows. It controls the flow of execution based on certain conditions such as if a certain condition is true or false. There are several types of control structures in Python. These control structures include if else statements, for loops and while loops. If else statement are used to execute a block of code if a certain condition is true and execute a different block of code if the condition is false. For loops are used to iterate over a sequence of items such as a list or a string. While loops are used to repeatedly execute a block of code as long as a certain condition is true. So these control structures allow a program to make decisions and perform actions based on the input or state of the program. So first let's cover the if else statement. So first consider an example to understand the if else control structure. If else control structure is like deciding whether or not to wear a raincoat based on the weather condition outside. Then you woke up in the morning and see that it's pouring rain outside. You could use an if statement to check if it's raining. So if raining wear raincoat. But what if it's not raining? You don't want to wear a raincoat if it's not necessary, right? So that's where the else statement comes in. So if raining wear a raincoat, else wear sunglasses. Now you are covered no matter what the weather is like. And if you are feeling particularly adventurous you could even use alif to check for different weather conditions. So if raining wear raincoat, alif sunny wear sunglasses, else bring umbrella. So you can check for different weather conditions with if alif and else statement. So an if else statement in python is a control structure that allows the program to make a decision and execute different code based on whether a certain condition is true or false. So here is the basic syntax of if else statement. So if then specify the condition and here you will write the code to execute if condition is true and then else statement and then you will write the code to execute if the condition is false. Here is an example of an if else statement in python that checks if a variable x is greater than 5. First of all declare a variable x is equal to 10 then use the if statement if x greater than 5 then you will print x is greater than 5 else print x is not greater than 5. So what will be the output in this case definitely x is greater than 5. So this program will print x is greater than 5. Now let's consider an other example you can also use alif statement in python which is short for alif to check multiple conditions. So here is the example this code check whether x is greater than 5 if not it check whether it is less than 0 and if not it execute the last block of code. So define x variable is equal to 3. If x greater than 5 then print x is greater than 5 else if x less than 0 then print x is less than 0 else print x is between 0 and 5. So what will be the output in this case since x is equal to 3 the first condition is false because x is greater than 5. The second condition will also be false because it will be executed if x is less than 0 so it will not be executed in this case because x is equal to 3. So first two conditions are false so in this case the else statement will be executed. So the program will print x is between 0 and 5 because x is equal to 3. Now let's switch to the Google colab to run few examples with if else statement. So here is the simple example with if else statement in this example the condition x greater than 5 is true so the code block under the if statement is executed and the output is x is greater than 5 because here you can see x is equal to 10 so if x greater than 5 then print x is greater than 5 so let's run this code and see the output so here you can see the output x is greater than 5 because the condition with if statement is true. Now consider another example using if and elif with multiple conditions using if elif else statements this is a way to check multiple condition by using the elif keyword which stands for else if this allows you to check multiple conditions and execute different code blocks based on the first true condition so let me create an other code section and here is our code so as you can see the multiple condition with if elif and else statements so if x is greater than a 5 then the program will print x is greater than 5 since the x is equal to 3 so this condition will be false let's go to the second condition that is elif if x is less than 0 then print x is less than 0 but x is equal to 3 program will not execute this code block in the elif statement so both first conditions are false so in this case program will execute the code block in the else statement that is x is between 0 and 5 so let's run this and see the output so here you can see the output x is between 0 and 5 now let's see another example of if and else statement with logical operators such as and or so let me create an other code section and here is our code so in this code variable x is equal to 3 variable y is equal to 5 so in the first condition you can see if x greater than 2 then we have used the and logical operator y less than 10 so if both these condition will be true then this condition will be considered as true because we have used the and logical operator so then it will print x is greater than 2 and y is less than 10 and here you can see x is greater than 2 and y is less than 10 so this condition will be true in this case and the program will print x is greater than 2 and y is less than 10 and in case if the first condition is false then program will print this statement x is less than or equal to 2 or y is greater than or equal to 10 so let's run this example and see the output so here you can see x is greater than 2 and y is less than 10 because the first condition is true and program will execute the code block in the if statement in python you can also use the if else statement in a single line so let me create another code section and here is the code so in this code example you can see you can use the if else statement in a single line here x is equal to 3 and then print x is greater than 5 if x greater than 5 so if this condition is true then this code block will be executed else print x is less than or equal to 5 since in this example x is less than 5 so first condition will be false and the code block in the else statement will be executed so let's run this example and see the output so here you can see x is less than or equal to 5 so you can use the if else statement in a single line as well see you in the next for loops are like a buffet line at a party you know exactly how many dishes there are and you can go through the line one dish at a time trying each one until you reach the end just like a buffet once you reach the end you are done and you can go back for a second if you had like a far loop in python is used to iterate over a sequence of items such as a list topple dictionary or a string here is the basic syntax for the far loop here you can see far and then variable in the sequence sequence can be a list topple dictionary or a string and then you specify the code to execute for example here you can see the list of numbers and then you iterate over this list of numbers using for loop here you can see far num in numbers and then you print each number in this list you can use for loop to iterate over a list this is the most basic way of using a far loop where you iterate over a list of items and execute a block of code for each item here you can see the code example in this example the far loop iterates over the items of a list called colors and for each item it executes the block of code which is printing the color here you can see the list of colors and then you use the far loop to iterate over the list of colors and in the far loop you are going to print these colors so here you can see the output in the output you can see the red green and blue color you can use the far loop to iterate over a range of numbers you can use the range function to generate a sequence of numbers and then use a far loop to iterate over them so here is the example to iterate over a range of numbers in this example the far loop iterates over the number generated by the range of five which returns a sequence of numbers from 0 to 4. For each number it executes the block of code which is printing the numbers. As you can see far i in range of 5 then print i and here is the output. Here you can see the numbers from 0 to 4 because range of numbers is 5 so it will start from 0 and will end at 4. You can use a fire loop to iterate over the characters of a string and execute a block of code for each character. Here you can see the example. In this example the fire loop iterates over the characters of a string called name and for each character it executes the block of code which is printing the character. So here you can see the string variable name is equal to python and then you print each character in the name variable using a fire loop and here you can see the output. In the output you can see the each character in the name variable. You can use fire loop to iterate over multiple list or sequence. You can use the zip function to iterate over multiple sequence at the same time by combining the items of each sequence with the corresponding items of the other sequence using the zip function. So here you can see the example. In this example the fire loop iterates over the items of two lists names and edges simultaneously by using the zip function. The zip function combines the items of each list with the corresponding items of the other sequence. For each pair of the item the loop executes a block of code which is printing the name and the age. Here you can see the first list of names and then you can see the second list of edges. These edges are corresponding to each name in the names variable. Then in the fire loop you use the zip function to combine the items of each list with the corresponding items of the other sequence. So zip function combines the names and edges and then you print the name and age for each list. So here you can see in the output. The first output you can see allies and 25. Here you can see allies and 25 and the second output is bob 30 and here you can see the corresponding items at the second place in the two lists bob 30 and the third list is Charlie and 35 and here you can see at the third place in the two list Charlie and 35. So in this way you can combine the corresponding items of the multiple list using the zip function and you can use the fire loop to iterate over multiple lists. You can use the fire loop with the enumerate function. This function returns the index and the value of each item in a sequence or list. So you can use it to iterate over the items of a sequence or list and also access their index. For example, here you can see the example. In this example, the fire loop iterates over the items of a list called fruits and for each item it executes the block of code which is printing the index and the value of the item by using the enumerate function.  Here you can see in the print function I is the index of the corresponding fruit and in the output you will get the index and its corresponding fruit in the list. You can use the fire loop with the reverse function. This function returns the items of a sequence or list in a reverse order. So you can use it to iterate over the items of a sequence or list in reverse order. Here is the code example. In this example the fire loop iterates over the items of sequence generated by the reverse range five function which returns a sequence of numbers from four to zero in reverse order. For each number the loops execute the block of code which is printing the numbers. So in this fire loop the range function will generate the numbers from zero to four and then you have used the reverse to function that will reverse these numbers. Here is the output. You can see the numbers are in the reverse order in the output. Now let's switch to the Google Collab to execute demo examples. So here you can see the simple code to print the list of colors. Here you can see the list of colors. Then you have used the fire loop to print these colors. So let's run this code. So in the output you can see all the colors in the list. Now let's create another code section and here is the code to print the range of numbers. Here you can see we have passed the five to the range function that will generate the numbers from zero to four and after running here you can see the numbers from zero to four. Let's create another code section to print the characters of a string. So as you can see the string variable name is equal to Python and you are going to print each character in the name variable using the for loop. So let's hit the run button and here you can see all the characters in the name variable. Now let's create another code section. In this code you are going to print the multiple list with the zip function using the for loop. So first list is names and the second list is ages and you have zipped the corresponding items of two lists using the zip function and you have combined the corresponding items of each list using the zip function and then you have used the for loop to print each list items. So let's hit run and see the output here you can see as you can see the first name in the list one is allies and here you can see the allies and the first age in the list ages is 25. So in this way you have combined the corresponding items of each list using the zip function and then printed it out using the for loop. Now let's create an other code section and here is our code. So if you want to print the list with the index number you can use the enumerate function. So here you can see the fruits list and you have used the enumerate function in the for loop. Now you can also print the index of each item in the list. So I is the index of each fruit. So let's hit the run button and here you can see April at the 0th index banana at the first index and orange at the second index in the fruit list. Let's create another code section. So here is our code to reverse the range of numbers. So you have used the range function to generate the number from zero to four and then you have used the reverse function to reverse these numbers and then follow to print the reverse number from four to zero. So let's hit the run and here you can see in the output the number are rewards from four to zero. So in this way you can use for loop in different applications according to your requirements while loops are like being lost in a maze. You don't know how many turns you will have to make or how long it will take to find the exit but you keep going making turns until you finally find your way out.  A while loop in Python is used to repeatedly execute a block of code as long as a certain condition is true. So here is the basics in text for the while loop while and then you specify the condition and the while loop will repeat until this condition will be true and then you specify the code that you want to execute in this while loop while loop can be used as a count control loop. This is a way to use a while loop to repeat a block of code a certain number of times you can use a variable as a counter and increment or decrement in each iteration. Here is the code example. In this example the while loop uses a variable called I as a counter and start with the value zero. The condition of the loop is I less than five, which means the loop will continue to execute as long as I is less than five. In each iteration the loop increments the value of I by one using the I plus equal to one statement and also prints the value of I. As a result, the loop will execute five times and print the number from zero to four. As you can see I start from zero and while I less than five and then print I and as you can see you increment I and the loop will iterate five times and will print the numbers from zero to one. You can use a while loop as an event control loop. This is a way to use a while loop to repeat a block of code until a certain event occurs. You can use a variable to store the state of the event and check it in each iteration. Here is the code example. In this example the while loop uses a variable called user input to store the input entered by the user. The condition of the loop is user input not equal to q, which means the loop will continue to execute as long as user input is not equal to the letter q. In each iteration the loop prompts the user to enter a command and assigns the input to the user input variable. Then it prints the value of user input. The loop will execute indefinitely until the user enter the letter q which will make the condition false and the loop will exit. You can use a while loop as a infinite loop. This is a way to use a while loop to repeat a block of code indefinitely. You can use a true constant as a condition for the loop but you have to make sure to include a way to exit the loop otherwise the program will not stop executing. So here is the code example. In this example the while loop uses the true constant as a condition which means the loop will continue to execute indefinitely but the loop includes an if statement that checks if the user input is letter q and if so the break statement is executed which will exit the loop. This way the user can exit the loop and the program will not run infinitely. You can use continue and break statements to control the flow of a loop in a python either it's a far loop or while loop. You can control the flow of both the loops using continue and break statements. So let's see what is continue and break statements do with the loops. Continuous statement is used to skip the current iteration of a loop and move on to the next one. Break statement is used to exit the loop prematurely. The continuous statement is used to skip the current iteration of a loop and move on to the next one. It is typically used inside the body of the loop and when it is executed the rest of the code in the current iteration is skipped and the next iteration starts. Here is the example code with the far loop. This example shows the use of continuous statement in a far loop. The loop iterates over the numbers generated by the range of 10 which returns a sequence of number from 0 to 9. Inside the loop the code checks whether the current number is even or not using the if i modulus 2 equal equal to zero statement. So if the number is even the continuous statement is executed which skips the rest of the code in the current iteration and move on to the next one. So as a result the loop only prints the odd numbers between 1 and 10. So here you can see the output. This loop will only output the odd numbers between 0 to 10 because in case of even number you are skipping the iteration using the continuous statement and when even number occurs the continuous statement skip the print statement and it will go again back to the loop. So in this way it will only print the odd numbers between 0 to 10. Break statement is used to exit a loop prematurely. It is typically used inside the body of the loop and when it is executed the loop is terminated and the program continues with the next statement after the loop. So here is the code example with the while loop. This code example shows the use of break statement in a while loop. The loop uses the true constant as a condition which means the loop will continue to execute infinitely. Inside the loop the code increments the value of i by 1 using the i plus equal to 1 statement and checks if the value of i is greater than 5 using the if greater than 5 statement. If the value is greater than 5 the break statement is executed which exits the while loop and the program continues with the next statement after the loop. As a result the loop only prints the numbers from 1 to 5 and then program exits the loop. So here you can see the output. Only numbers from 1 to 5 are printed because you are exiting the loop when i greater than 5. So when i becomes greater than 5 it will execute the break statement that will exit the loop. So you can control the flow of the while and the far loop by using the continue and break statement according to your requirements. Now let's switch to the google colab to run the code examples. So let me create a code section and here is our first example with the while loop. So i start from 0 and while loop will iterate until i less than 5 and inside the loop you are printing the value of i and then you increment the value of i. So it will print the numbers from 0 to 4. So let's run this code and see the output. So here you can see the numbers from 0 to 4. Now let's see the example of the while loop as an even controlled loop. So create another code section and here is the code example. So this while loop will repeat until you entered letter q and inside the while loop you are taking the input from the user and then you are printing that input and if user enter letter q the while loop will end. So let's run this code. So enter a command for example i enter you can see the hello is printed and then it's asking again because while loop will repeat until you enter the letter q. So let me enter letter f for example let me enter letter r. So it will continue whatever you input until you enter q. So let's enter q now and this will end the loop. So here you can see the execution has been terminated. Now let's see the while loop example as an infinite loop with the break statement. So let's create another code section and here is our code example. So in this code you can see the while loop is always true. It means it will run infinitely and it will only terminated when the break statement will be executed. This break statement will be executed when i greater than 5. So i starts from 0 and then you increment the i inside the loop and when the if condition met the loop will be terminated. So this program will print the numbers from 1 to 5. So let's hit the run and see the output. So here you can see the output. So the program prints number from 1 to 5. So if you don't use the break statement in this loop, this loop will run infinitely. So be careful while using the infinite loop. Similarly you can also use the break statement with the far loop. Now let's see an example of the continue statement with the far loop. So here is the example of the continue statement with the far loop. You can also use the continue statement with the while loop. So this far loop will generate the number from 0 to 9 and if the number is even then you skip the iteration using the continue statement. Otherwise far loop will print the number. So it means if the number is odd this far loop will print the number and if the number is even the continue statement will be executed and it will skip the print statement and it will again go to the next iteration. So let's run this code and see the output. So as you can see in the output it will only print the odd numbers between 0 to 10. So in this way you can use the continue and the break statements to break and skip the iterations of the loops and you can control the flow of the loops according to your requirements. See you in the next lecture. What is list data structure in python? A python list is a collection of items stored in a single variable. It is built-in data type in python and is used to store a collection of items which can be of different types such as integers, strings, etc. List can be defined using the square brackets. Lists are order and mutable which means that items can be added, removed or changed at any position in the list. You should use list when you need to store a collection of items that will be iterated over or when you need to maintain the order of items. They are also useful when you need to perform operations such as slicing, indexing or using the built-in methods such as append, insert, remove and sort. So here is the list data structure operations. So you can create a list using the square brackets. Here you can see list is equal to 1, 2, 3, 4 and if you print this list here you can see print list and here is the output. List contains four numbers from 1 to 4. You can access the element of a list using the index. Index of a list start from 0. For example, the first element is at index 0, the second element is at index 1, the third element is at index 2 and fourth element is at index 3. So here you can see you can access the element 1 using list index 0 and if you print this you will get the output 1 because 1 is at index 0. You can slice a list here you can see you can access a range of elements of the list and access the elements from index 1, 2, 3. So if you print this and in the output you get the element 2 and 3 from the list here you can see 2 and 3. You can print the length of the list using the length function. If you print this you will get the output 4 as there are four number of elements in the list. You can append at the end of the list list dot append number 5 at the end of the list. So if you print this list after the append you will get the final list with the appended number 5 at the end of the list. You can insert the elements in the list using the insert method. For example, list dot insert element 0 at position 1 in the list. So if you print here you can see the element 0 at the position 1 in the list. You can remove the element from the list for example list dot remove element 2 and here you can see the element 2 is removed from the list. You can remove the last element from the list using the list dot pop method and if you print list dot pop you will get the last element from the list and if you print the list the element from the list will be removed. you can concatenate two lists for example list one and you can use the plus sign to concatenate the two lists and then you can see the list two so list one plus list two here you can see the list one and then the list two and if you print list one plus list two and here is the output with the concatenated lists list one contain one two three four and list two contain five six you can repeat a list using the steric symbol list into two and if you print this print list into steric two so the list will be repeated and appended at the end here you can see the list one two three four and then again one two three four you can check the membership of an element in a list for example two in list and if the two is in the list it will print the true otherwise it will print the false so now let's see the common applications of the list list can be used for storing and manipulating data lists are commonly used to store and manipulate data in python they can be used to store collection of items such as names numbers or even other lists list can be used to iterate over data list can be used to iterate over data in a far loop this is a common technique used in python for data manipulation and processing list can be used to create powerful and concise list comprehensions that can be used to filter transform and extract data from large data sets list can be used to store and manipulate metrics as a list of lists list can be used to store and manipulate graphs as a list of edges and verses lists can be used to store and manipulate trees as a list of nodes and edges list can be used to implement queues and stack data structure list can be used to store and manipulate ordered data and sorting data using list now let's switch to the google colab to see the demo examples so let me create a code section and here is the code to create lists so you can create an empty list here you can see my list is equal to empty square braces and you can create a list with elements for example my list is equal to one two three four five you can also create a list with mixed data types in this list you can see my list is equal to one that is integer and then hello that is string and then 3.14 that is float and then you can see true that is boolean data type so let's give this list name as a mix list and if we print this list so let me print this list print mix list so let's run this code and see the output so as you can see in the output the mix list that we have printed so you can create a list with the elements of different data type now let's create an other code section and here is the code for list comprehension list comprehension is a concise way to create lists in python it allows you to create a new list by applying an expression to each item in an existing list or any other i-travel for example here you can see you can use a fire loop to create a list of squares squares is equal to empty list par x in range 10 squares dot append x raise to power 2 and you can perform the same operation using the list comprehension in a single line of code so squares comprehension is equal to x raise to power 2 for x in range 10 that is the list comprehension to create a list of squares for number 0 to 9 similarly in the last example you can use the list comprehension to create a list of even numbers x for x in range 10 if x modulus 2 is equal to 0 that will use the list comprehension to create a list of even numbers now let's print the squares comprehension squares comprehension and even numbers even numbers now let's run this code and see the output so in the first list you can see the squares of the numbers from 0 to 9 this list is created using the list comprehension and in the second list you can see the even numbers in the range of 10 let's create another code section to see how to access the elements of a list so here is our list you can access the first element of a list first element is equal to my list and then index 0 because the first element is at index 0 in the list similarly you can access the last element of a list using the minus 1 index that will retrieve the last element in the list that is true similarly you can access a range of elements in a list for example sub list is equal to my list access the elements 1 2 3 that will include the element 1 that is hello and element 2 that is 3.14 this range is from 1 2 3 it means it will retrieve the elements at the index 1 and 2 and the third index will not be included in this result so it will retrieve the hello and 3.14 so let's print all of these print first element and then print last element and finally print sub list sub list now let's run this code to see the output so as you can see first we have retrieved the first element from the list and then the last element that is the true in the list and then the range of elements from 1 2 3 and here you can see the result remember the list start from the index 0 as you can see the first element is at index 0 and the last element is at index minus 1 and the last one is also called as a slicing of a list to access a range of elements in a list so let's say this is a slicing of a list in which you can access a range of elements in a list you can change the value of an element in a list for example here is our list so you can change the value of an element for example my list change the value of element at index 2 and replace it with word so let me print this print my list now let's run this code and see the output so as you can see the value of an element at index 2 has been changed to word you can also remove an element by index you can use the del keyword to remove an element by index so del my list remove the element at index 2 so from the above list the word will be removed so let me print again print my list so let's run this and here you can see the output the element at the index 2 has been removed now let's see the details about the truffle data structure in python truffle data structure can be defined using the round brackets or parentheses a python truffle is a collection of ordered and immutable objects it is similar to a list but the items in a truffle cannot be modified after they are created because truffle is immutable truffles are often used to store multiple related pieces of data such as person's name and age they can also be used to store items that have a specific order such as the days of week or the months of the year truffles are generally faster and use less memory than list so they are a good choice when performance is a concern and the items in the collection will not need to be modified so here is a list of some common operations using truffle you can create a truffle using round brackets here you can see t is equal to the elements you can see in between the round brackets or parentheses you can access the truffle elements using the index for example you can access the element one using index zero you can access the element two of the truffle using index one truffle indexing is similar to list you can slice a truffle to access range of elements just like the list here you can see you can access multiple items in the truffle you can print the truffle length using the length function here you can see length and then you pass the truffle to this length function so as you can see the truffle length is four you can concatenate two truffle using the plus operator just like the list so here you can see the truffle one and truffle two and if you print the concatenation of the truffle and here you can see the output with the concatenated truffle you can repeat the truffle just like list using the steric operator t steric two and it will repeat the truffle you can check the membership of an element if it is in the truffle or not so here you can see two in t you can use a in operator to check the membership of an element in a truffle so if you print two in truffle so output will be true if the element exists in the truffle otherwise false now let's see the applications of truffle truffles can represent data as a single entity truffles can be used to group related data together and represent them as a single entity for example you can use a truffle to store the information of a student such as their name age and address truffles can be used to store multiple values in a single variable which can be useful for situations where you want to return multiple values from a function or store multiple items in a single data structure truffles are immutable meaning that their elements cannot be changed once they are created this can be useful in situations where you want to ensure that the data stored in a truffle remains constant truffles can be used to swap the values of two variables in a single line of code truffles can be used as a keys in dictionaries which can be useful for situations where you want to store data in a key value format truffles can be passed as an argument in a function and can be used to store multiple input parameters truffles are often used in unpacking which allows you to assign multiple values to multiple variables in a single line of code what is set data structure in python set can be defined using the curly brackets a set is a collection of unique elements it is unordered used to store multiple items in a single variable they are useful for removing duplicates from a list or for performing mathematical set operation such as union intersection and difference sets are defined by using curly braces or set function they are commonly used in programming to check if an item is present in a list or to remove duplicates from a list here is the list of common set operations so you can create a set using curly braces s is equal to curly braces and then the number of elements of the set you can retrieve the length of the set using the length function you need to pass the set to this length function and it will return the length of the set here you can see the set contain four elements you can add an element to a set using the add function s dot add five and it will add five into the set you can remove element from a set here you can see s dot remove two and you can see two is removed from the set similarly you can discard an element from the set for example s dot discard three and it will be discarded from the set you can clear the elements using the s dot clear function and it will empty the set you can take the union of two sets for example s one dot union s two here you can see the set one that is s one is equal to one two three and s two is equal to three four five and when you print the union of s one dot union s two it will contain all the unique elements in two sets you can take the intersection of two sets s one dot intersection s two using the intersection function here you can see the set one and set two and when you print the s one dot intersection s two it will output the common elements in two sets you can take the difference of two sets using difference function set one dot difference set two here you can see the set one and set two and when you print the difference of these two sets it will give the difference of two sets you can check the membership of an element in a a set using the in operator to in S. If two is in the set, it will return the true otherwise false. Here are the common application of the set data structure. You can use set to remove the duplicates from a list sets only store unique elements. So you can use the set function to remove duplicates from a list by converting it to a set and then back to a list. You can use set for membership testing. Sets are optimized for fast membership testing. So you can use the in operator to check if an element is present in a set. You can use set data structure to perform the set operations. You can perform set operations like union, intersection and different on sets in Python. You can map keys in a dictionary using sets data structure. Sets can be used to quickly check if a key is present in a dictionary. As keys of dictionary are stored as a set, you can use set operations to find common elements between two multiple lists. You can build a spell checker using the set data structure. A set can be used to store a list of valid words, allowing the program to quickly check if a word is spelled correctly. Sets can be used to store unique elements without worrying about duplicates. Dictionaries are like a set of keys and locks. You have a key to unlock specific values. Previously, you have covered different data structures including list, tuple and set. Now you will deep dive into the dictionary data structure. As you have seen list data structure is like a shopping list. You can add and remove items as needed. Tuple data structure is like an encyclopedia set where you can't add or remove books but you can look up any book by its volume number. Set data structure is like a group of friends. You can add and remove people but everyone is unique. Dictionary data structure is like a set of keys and locks. You have a key to unlock specific values. You can define a dictionary using curly brackets with key and its value. A dictionary in python is a data structure that stores key value pairs. It is also known as a map or hash table. Dictionaries are used to store data that is organized by key and can be used to quickly look up values based on their corresponding keys. Dictionary data structure is useful when you need to store data in a way that allows for quick retrieval such as in web development to store configuration settings or to store data from a database, etc. Dictionaries can also be used to store data that can be used for mathematical or statistical operations. They are generally implemented such as hash tables which makes them very fast and efficient for lookups. Now let's cover the common dictionary operations. So you can create a dictionary using key value pair. Here you can see D is a dictionary, D is equal to key one and its value, key two and its value. And if you print this dictionary you will get the output in the form of key value pair. You can retrieve the dictionary length using the length function. Here you can see the length of the dictionary D is two because key and its value is considered as a one element. You can access dictionary by its key. Here you can see keys use as an index to its value. So if you print key one of the dictionary you will get the output one because here you can see key one value is one because key one value is one in the dictionary D. You can add element in the dictionary. Here you can see dictionary D and then you use the key three is equal to three and if you print the dictionary D now here you can see the key one key two and now you have added the key three with its value three. You can delete a key value pair by using its key. Here you can see del key one of the dictionary and if you print the dictionary now here you can see the element one from the dictionary is removed. You can clear the dictionary using the D dot clear function. You can print the dictionary keys using keys function here you can see. So if you print here you can see the list of the keys in the dictionary. You can also access the only dictionary values using the values function. Here you can see D dot values and if you print here you can see the list of values in the dictionary. You can also access the the dictionary elements in the form of list of tuples for that you can use the items function here you can see d dot items and if you print here you can see key value pair in the form of list of tuples here you can see element one is tuple one and element two is tuple two and then you can see the square brackets for the list so you can retrieve the dictionary items in the form of list of tuples now let's see the common application of the dictionary data structure you can use dictionary data set to represent a json object dictionaries are often used to represent json objects as they have a similar key value structure you can use dictionary data structure to store configuration options dictionary can be used to store configuration options for a program such as the size of a window or the number of iterations to run dictionaries can be used to count the number of occurrence of items in a list or other data structure such as counting the frequency of the words in a text dictionary data structure can be used to group items together based on a key such as grouping all items with a certain tag together dictionary can be used to represent a graph with the key representing the nodes and the values representing the edges dictionary can be used as a cache memory for storing recently accessed data for example for web scrapping etc now let's switch to the google colab to run the code examples so here is the code to create dictionary in different ways so here you can see you can create an empty dictionary then you can use the empty curly braces to create an empty dictionary you can create a dictionary with key value pairs so my dictionary is equal to name is the key and then zone is value age is the key and then 30 is the value gender is the key and male is the value you can also create a dictionary with the dictionary method so here you can see the dictionary is equal to dictionary method and then you pass the key value pair to this method you can also create a dictionary using comprehension so here you can see the dictionary of the squares in the range of one two six so first output is the dictionary with the use of dictionary method that is this one we have printed this and you can see the dictionary with its key value pairs and the second output is for the squares of the numbers that has been created using the dictionary comprehension so as you can see value key one and its square key two and its square key three and its square key four and its square key five and its square now let's see how you can access the dictionary values so here is our code so you can see the my dictionary so you have created the my dictionary and you can access an element by its key here you can see my dictionary and then you have accessed the name key and here you can see in the dictionary name key value is johan and you can also use the get method to access an element by key so as you can see my dictionary dot get and then you have used the key and here you can see the age key in the dictionary and its value is 30 so let's run this code and see the output so here you can see the first output in which name is printed and the second output you have used the get method and in which age is printed now let's see how you can update the dictionary so here is the code section and here we go so this is the original dictionary and you can add an element to a dictionary and here you can see my dictionary and then you have used the key city is equal to new york so this will be added at the end of the dictionary and you can also use the update method to add multiple elements in the dictionary so my dictionary dot update so the first key is country and its value is usa and second key is job and its key is developer so let's print this dictionary so print my dictionary so let's run this code and see the output so as you can see this is the original dictionary items and then you have added the city new york and then you have added the multiple items using the update method country and job you can update the dictionary value by using its key for example here is the code so here is the dictionary person and you are going to update the age in the original dictionary the age is 30 and you can use the age key with the dictionary is equal to the updated age value to update the age so let's run this and print the updated dictionary so here you can see the age is updated so you can update any value by using its key so here is the code so here you can see the original dictionary and you can remove an item from a dictionary using the del keyword so here you can see you have used the del keyword and you are going to remove the gender from the dictionary so let's print the dictionary after removing the gender from the dictionary so my dictionary and then you can also remove an element by a key using the pop method and here you can see my dictionary dot pop and you are going to remove the age from the dictionary here you can see the age so let's print the dictionary after removing the edge so print my dictionary pop method will also return the value of the age for example you can save the age value in a variable let's say age is equal to my dictionary dot pop and then you can print this variable age so let's say age you can also use a clear method to remove all elements from the dictionary so here you can see the mydictionary dot clear so let's run this code and see the output so here you can see first you have removed the gender so here you can see del mydictionary you have removed the gender from the original dictionary gender with its value male has been removed from the dictionary here you can see then you have used the pop method to remove the age and pop method will also return the age that you have saved in the age variable and then you have printed the age and the dictionary so here you can see age is 30 and then you have printed the dictionary age has also been removed from the dictionary so now the dictionary contains only the name and its value and finally you have used the clear method so let's print this as well so let's say print my dictionary now let's run it again and here you can see now the dictionary is empty you can get all the keys and the values from the dictionary so let's create a code section and here is our code so here is the dictionary and here you can see you have used the person dot keys method to get all the keys and you have used the values method to get all the values from the dictionary and then you have printed the keys and values so let's run this to see the output so as you can see the dictionary keys name age city as you can see the dictionary values name is john age is 30 and city is new york you can use a loop to iterate over the dictionary so here is the code so here is the dictionary so in the first loop you are going to print the keys only so far key in my dictionary and then print the key you can also loop through the key value pair using a far loop as you can see far key value in my dictionary dot items and then print key and its value so let's run this code and see the output so here is the output of the first far loop where you have printed only the keys of the dictionary and then the output for the second far loop you have printed the keys and its value here you can see name john age 30 gender male so you can iterate over both keys and values using the far loop you can also merge two dictionaries so let's see here is the code so here is the dictionary one person one and person two and you want to merge the person two into person one so you can use the update method so person one dot update person two so pass the person two to the update method and if you print the person one elements of the person two will be added at the end of the person one so let's print this and see the output so here you can see the data for the person one and then you can see the data for the person two so in this way you can merge two dictionaries into one you can also concatenate two dictionaries so let's see here is the code so you can use the double stereo operator to concatenate the two dictionaries and here you can see you are going to concatenate the dictionary one and dictionary two and save it into the dictionary three so use the double stereo operator for both the dictionaries in order to concatenate so let's run this and see the output so here you can see the two dictionaries are concatenated now let's see some problem statement and try to solve them using the dictionary data structure so you are given a list of names and ages of people and you need to store them in a dictionary where the name is the key and the age is the value so let's create a code section and here is the input data in the form of list of tuples so the first element contains name and age first tuple of the list and the second tuple also contain the name and age and so on so you are required to save this into a dictionary so first of all you will create an empty dictionary let's say pupil dictionary and then you will use the far loop in which you will unpack each tuple name and its value so here is the codes and here you can see far loop and then name age in pupil and then you are going to save this into the pupil dictionary here you can see name is the key and age is the value now let's print the dictionary pupil dictionary let's hit the run and see the output so as you can see you have saved the data in the dictionary in the form of key value pair now let's solve another problem using the dictionary data structure so given a list of employees where each employee is represented as a tuple containing their name and salary so implement a hash table that allows for efficient lookups of employees by their name so let's create a code section so here is the employees data in the form of list of tuples each tuple contain employees and its salary now let's create an empty hash table or dictionary so let's say employee table that will act as a hash table now you will use the far loop to store the data in a hash table that allows for an efficient lookups of employees by their name so here is the far loop so far name salary in employees so then you will add the employees to the dictionary using their name as a key and then you will store both name and salary corresponding to its key now let's test the hash table for the efficient lookups of the employees by their name so let's print employee table so you have used the name as the key so let's access the bob and similarly access an other let's say david now let's run this and see the output so as you can see against the bob you have saved the bob and its salary here you can see bob and its salary in the original data and here you can see in the original data david and its salary and in the output you can also see so in this way you can store the data in the dictionary and this dictionary will act as a hash table and you can access the data faster for efficient lookups now let's say the bit complex problem to solve using the dictionary data set so given a list of student names and their corresponding grades so create a dictionary that stores the student names as a key and grades as a values then calculate the average grade for all the students and return the names of the students who scored above the average grade so let's create a code section and here is the data student grades with student and its grade now you can create a student dictionary using the dictionary method so here is the code create a dictionary from a student grade list so student dictionary is equal to you have used the dictionary method and you pass list of tuples to the dictionary method and it will convert into the student dictionary now you can access the values from the student dictionary and calculate the average so grade sum is equal to sum of student dictionary values so it will access all the grades and then take the sum and then grade average is equal to grade sum divided by length of student dictionary so it will take the average of the student grades now let's initialize a list to store the students names above the average grade so here is the empty list above average students now let's use a far loop to iterate through the dictionary to find the students above average so for student grade in student dictionary dot items if grade greater than grade average then append that student to the above average students and then finally you will print above above average students now let's run the code and see the output so here you can see in the output lies charlie and eve are the students who have the grade above average so in these examples you have seen dictionary is very useful data structure with the key value pair to solve the complex problems see you in the next lecture functions in python are like a superhero team each function has its own unique powers and you can call upon them to save their day and solve specific problems for example you have the sort function with the power to organize data in a neat and tidy way just like how superman organizes his close at are the filter function with the power to shift through data and pick out the important bits just like how spiderman uses his spider sense you can also give functions different arguments just like how each superhero has their own set of gadgets and weapons and just like how each superhero has their own secret identity you can also have anonymous functions are lambda functions that can do their job without revealing their true identity and just like how each superhero team has a leader you have the main function that calls upon the other functions to work together and accomplish the task so in python a function is a block of organized reusable code that is used to perform a specific task functions help to break down complex problems into smaller manageable chunks of code making it easier to understand and maintain functions also allow you to reuse code making it more efficient and reducing the risk of errors there are several types of functions in python user defined functions these are the functions that are created by the user to perform specific tasks they can be called multiple times throughout the program just like other functions anonymous are lambda functions lambda functions these are also known as anonymous are lambda functions as they don't have any name these functions are defined using the lambda keyword they are mostly used with the filter and map functions python has a wide range of built-in functions built-in functions are included with the python and are available for use without importing any additional libraries examples of built-in functions include print length and string functions etc so let's see the details about the user defined functions in python user defined functions are created by the user to perform specific tasks the syntax of defining a function is as follow the keyword def is used to define a function followed by the name of the function and a set of parentheses that may contain parameters the code block that defines the function behavior is indented beneath the function definition so here you can see the def keyword and then function name and then you pass the number of parameters a function may have the parameters or not it depends upon the requirement and then you put the code what function will do and a function can also return a value it's also optional it depends upon the requirement if you want to return a value or not here is the example of a user defined function the function add numbers takes two parameters x and y and returns their sum by adding them inside the function and returning the result the function is called by passing the two arguments two and three which gets added inside the function and returned as five so here you can see the function definition so here you can see the two parameters x and y and inside the function you are adding these two parameters and then you are returning the result and then you have called the function add numbers by passing the two parameters two and three and the result will be saving the result variable and then you have printed the result and definitely the result will be five because you are adding these two parameters let's see the types of user defined functions in python recursive function that is defined by the user recursive function is a function that call itself they are useful for solving problems that can be broken down into smaller sub problems of the same type generator functions that can be defined by the user these are the functions that use the yield statement to return a generator object they are used for creating iterators allowing the user to iterate over a sequence of values they can only be called once as they keep track of their on state and cannot be re-initialized so let's see the example of the recursive function recursive function is a function that calls itself so here is the example of our recursive function this recursive function calculates the factorial of a number n using recursion the function factorial n takes an integer n as input parameter if n is zero the function immediately returns one this is the base case of the recursion when n is zero the function immediate returns one in the base case if n is not zero the function returns n multiplied by the result of calling factorial n minus one this is the recursive case of the function so remember the recursive function must have the base case and the recursive case when the function is called with the nonzero n it calls itself with the value of n minus one this process continues recursively until the base case is reached and the base case is reached when n becomes zero at which point the recursion stops and the function returns the final result so the base case is the essential part of the recursive function at which the recursion stops so so in the function factorial here you can see the base case if n is equal to zero return one and then recursion stops else the recursive case here you can see return n into factorial n minus one for example calling factorial five would result in the following recursive calls recursive calls where function calls itself here you can see in the recursive case function calls itself inside the same function so let's see the recursive calls so you call the factorial function with the parameter five so this function will take the factorial of five so five is greater than zero so the function will go to the recursive call five into factorial four as you can see return n and will be five first time into factorial n minus one so five into factorial four and again function will call itself with the value four and here is the recursive call in that case then function will call with the value three And here is the recursive call when function call itself with value three, and then function will call with value two. And here is the recursive call. In that case, and then function will call with the value one and here is the recursive call. And finally, the function will call itself with a value zero when it is equal to zero. Then the function returns one. And that is the base condition when function stop recursion. Then function start turning to a recursive call its value, for example return 1 to this recursive call then return a value to this recursive call and then return to this recursive call and then return a value to this recursive call. And then finally return value to the first recursive call and here you can see the final value is 120 that is the factorial of five. So that's how the recursive function works. So generator function uses the yield statement to return a generator object as they are used for creating iterators allowing the user to iterate over a sequence of values. So let's see an example of a generator function. So here you can see the code example. This generator function generates an infinite sequence of Fibonacci numbers. The function Fibonacci uses the yield keyword to generate a sequence of values. The yield keyword allows the function to return a value and then resume execution from where it left off the next time it is called. The function initializes two variable a and b to zero and one respectively. The function enters an infinite loop using the while true statement. Inside the loop the function first yields the value of a using yield a. Then the function updates a and b to the next two numbers in the Fibonacci sequence using a b is equal to b a plus b. The loop repeats yielding the next value in the sequence and updating a and b accordingly. So you define the Fibonacci function, define the value of a and b. Inside the infinite while loop you are going to yield the value a and then you are updating the values of a and b to generate the Fibonacci series. So here is the example to call the generator function. To use the generator function, you can create a generator object by calling the function. So you have called the Fibonacci function that is a generator function and created its object Fib is equal to Fibonacci function. Then you can use this generator object to generate values from the sequence as needed. So here is the code to generate different values. Each call to next Fib generates the next value in the sequence starting from zero. The generator function doesn't generate all values at once, but instead generates them one at a time as they are requested. This can be useful for generating large sequences of values without consuming a lot of memory. Note that because this generator function generates an infinite sequence, you need to be careful not to iterate over the entire sequence at once. Instead, you should generate values from the sequence as needed using next or a loop with a break condition. So as you can see when you call the next Fib first time, it will return the value zero. When you call second time, it will return the value one. So each time you call the Fibonacci generator function, it will return the updated value of because you have used the yield statement for creating iterator as they keep the track of their on state and resume the execution from where it left. So there is a wide range of Python built in functions. So this function can be used for mathematical operations, string operations, type conversions and much more. So here is a list of some other built in functions in Python. I will provide you this list. So here you can see different built in functions, minimum, min, max, some sorted, some of these we have already as you can see some other functions, any char, dire, and we have already seen some functions. And you can also use function like hash, callable, and that's a lot of mathematical functions like math dot seal, math dot power, math dot degrees. And you can also have date and time functions like daytime now date time dot date, and random function, etc. So I will attach this list in the resources. What are the anonymous functions in Python? Anonymous functions don't have any name. These functions are defined using the lambda keyword. They are mostly used with the sort, filter and map functions. Here you can see the example of an anonymous function. So this example defines an anonymous function that takes two arguments x and y and returns there some the lambda keyword is used to define the function without giving it a name. The arguments are specified after the lambda keyword. After the arguments, there is a column that separates the argument list from the body of the function. The body of an anonymous function is a single expression that is evaluated and returned when the function is called. And then you specify the parameters x and y separated by comma and after the column, you have used the expression x plus y that is adding the two numbers x and y. And then you have assigned this anonymous function to the add variable. So you can call the anonymous function using the variable that it's assigned to. So in this example, you will call the anonymous function add with arguments three and five, the function adds these two numbers together and return the result, which is then assigned to the variable result. So you will call the function with the same name that is assigned to the anonymous function. So let's see another code example. Here you can see the code example. Anonymous function are often used in Python for simple one of calculations where it's not necessary to define a named function. They can be especially useful in cases where you need to pass a function as an argument to another function. For instance, in this example, you will use an anonymous function to sort a list of dictionaries by a particular key. So you will sort a list of dictionary by the age key, you will use an anonymous function to specify the key to sort by which is the age value of each dictionary. The sort method sorts the list in the place based on the result of the anonymous function. The result is the sorted list of the dictionaries based on their age values. So here you can see the pupil dictionary. And then you have used the pupil dot sort method. And inside the sort method, you have used the anonymous or lambda function, you are passing one argument, and you are going to sort the result by the age key. So this dictionary will be sorted with respect to the age of the people. So if you print the dictionary pupil, here you can see the result. The dictionary is sorted with respect to the age of the people. So we have already mounted the Google Drive to read the data from the Google Drive to Colab and also to write the data from Colab to Google Drive. Now you will read the data from the text file and then write back data to the text file. So here is our problem statement. So let me paste our problem statement over here. So here is your project problem statement. Statement is a company wants to keep track of the sales data of its products. The data is stored in a text file sales dot txt in the following format product name quantity sold and price. The company wants to calculate the total revenue generated from each product and store the result in a new text file revenue dot txt in the following format. So the revenue dot txt will contain product name and total revenue calculated. So you will write a program that reads the data from the sales dot txt file calculates the total revenue for each product and stores the result in the revenue dot txt. So first let's have a look at the text file as our text file will contain the product name quantity sold and price. So let's go to the Google Drive where we have put the text file that is sales dot txt. So here is our Google Drive and here you can see the sales dot txt file. So let me open the sales dot txt file. So here you can see the data. So let me zoom it. So here you can see in the first line the product name and then the quantity of the product and then the price. So here you can see the different products apple banana orange grapes watermelon strawberry mango pineapple peach and lemon and for each product there is a quantity and its price. Now you will write a python program to read this data and perform the calculation and then write back to another text file. So let's go to the Google Collab to write the code to read the data and then perform the calculations as in our problem statement. So let's start addressing this problem with the python code. So we will generate another code section to write the code. So first of all we will access the sales dot txt input file. So you will open the sales dot txt file in the read mode. So here you can see with open and here is the file path file is located in the Google Collab. At this part you can see the sales dot txt that is in the Google Drive my Drive Collab Notebooks and Python data and then there is a sales dot txt. So if we go to the Google Drive and we see the path. So here you can see the path of the sales dot txt my Drive Collab Notebooks and then Python data and here you can see the same path to access the sales dot txt file and we have opened this file in the read mode. So here we will pass the R parameter as file. So now we can access sales dot txt with the file object. So in this line we have just opened the sales dot txt to access the data from this file. Now we will read the data from the file. To read the data from the file you will use the read method in order to read the data from the file. So let's print this data for example if we print this data print and then we will print this data for a moment. So let's run this code. We will see what is the output. So let's run this code. So here you can see we have all the data that is in the sales dot txt file but in this case we have read all the data but we need to traverse through every product so that we can multiply the quantity and the price calculate the revenue for each product. So now what we can do first we will split this data into lines and further we will split each line to divide it into three values. So let's first divide this data into list of lines. So here we go. So in this line of code we have split the data into list of lines. Lines is equal to data dot split and here you can see we have split the data into list of lines. So data dot split we have used the split method to split the data into lines and here you can see we have passed the end of line parameter. So to the split method we have passed the end of line parameter. So it will split all the data into lines where each line ends and And now you will create an empty dictionary to store the revenue data where the keys are the product names and the values are the total revenue. So here we have created an empty dictionary to store the revenue data. So here is our dictionary. Now you will use the far loop so that for each line in the file the product name quantity sold and price are extracted from the line and the total revenue is calculated by multiplying the quantity sold by the price. The total revenue for each product is then added to the dictionary. So here is our code with the far loop to iterate over the lines and to calculate the revenue. So here you can see our loop so we will iterate over the lines. So here you can see we have split the data into the lines so far line in lines. So we will iterate over each line in the list of lines. So then first of all we will split the each line into a list of values because in each line we have to access the three values product name quantity sold and price. So here we will split each line so line dot split method to split each line into values where comma occurs. So here you can see for example in the first line we have three values and after first value there is a comma and after second value there is also comma. So it will split this line by comma into three values. So now we have split the each line into three values and we have the access to individual value to perform the calculations. So here is just a check check if the line is not empty if the values length is equal equal to three then the line is not empty because each line must contain three values. So after that we will get the product name quantity sold and price from the values because this values is a list of values that contain three values so we can access each value. So here you can see the product name is at position zero in the list and quantity sold at position one in the list and price is at position two in the list. So remember one thing each value that will be read from the text file is a string value. So here you can see the product name is equal to value zero so we didn't convert it into any other data type because product name is also a string. So in the quantity sold here you can see we have converted the value one that is the quantity of the product into int because this data in the integer format and the price is in the float format so that is why we have converted this data into float because each value is a string value when you read from the file by default then calculate the total revenue as you know that to calculate the total revenue you need to multiply the quantity sold into price so that you have the total revenue for each product. So here is the total revenue. So this total revenue is for a single product so we will put this revenue into a dictionary against each product. First we will check if the product is already in the dictionary as we have already created the dictionary here you can see the revenue data. So now we will check if the product name in revenue data that is our dictionary if it is already in the dictionary then we will update the total revenue for the product. So here you can see the our dictionary revenue data and then we will use the product name as a key of the dictionary and then we have plus the total revenue and if the product is not already in the dictionary then add the product and total revenue to the dictionary. So revenue data that is the dictionary and then use the product name as a key and then add the total revenue to the dictionary respective to the product. So if the product already exist in the dictionary then plus the revenue to the existing revenue otherwise add that revenue to the dictionary for that product so until here we have read the data from the file and we have calculated the revenue for each product from the text file now let's print this dictionary so that we can see the total revenue for each product so let's go here and print the dictionary of the total revenue data so revenue data so here also we will write the dictionary revenue data so let's run this code and we will see the output so here is our input data that is in the file that we have read and we have then printed that data and here is the dictionary revenue data that we have calculated so as you can see in the dictionary against apple there is a 35 value because here you can see we have multiplied the quantity of the apple and then its price so if you multiply this it will be 35 and then similarly against banana orange grapes so here you can see that we have calculated the revenue against each product so now you will write this dictionary data into an other text file so let's go and open the revenue.txt file so first of all you will open the revenue.txt file in the right mode so here you can see we have opened the revenue.txt file in the right mode here you can see we have passed the w parameter and we are writing this revenue.txt file at the same path from where we have read the data so this file will be saved at the same path so after writing the data we will see this file so now we will iterate over the dictionary keys and values to store the data into revenue.txt so here we go so now we will iterate over the revenue data dictionary so you will use the far loop to iterate over the dictionary data so far product name that is the key in the dictionary and total revenue that is the value against each product in revenue data.items so here you will write the product name and the total revenue to the file so file.write product name and total revenue so when you write the data into the text file you have to convert the data into the string because text file read all the data as a string and then we have used the end line operator so that each product must be saved in a single line so in a single line there must be a one product so that's very simple to write the data into the file we have just used the far loop to write the data into the file and also remember one thing we didn't created the revenue.txt file manually so when you will run this code the program will automatically create this revenue.txt file at this path in the google drive so let's run this code so that we can see the revenue.txt and the data so here we go so all of our code has been executed so that's all our code where we have read the data and then we have write the data into the text file so let's go to the google drive to see the revenue.txt file so let me refresh it so that we have the revenue.txt file so here you can see the revenue.txt file so let me open this file so file is going to be open so here you can see the data so let me zoom so here you can see the apple and its revenue total revenue that we have calculated by multiplying the apple quantity at its price and similarly revenue for each product is listed over here so that's all we have covered how to read the data from the input file and then we perform some calculations and then we write data back to the next file see you next