Python Programming Course: Quickly Master the Basics in 1.5 Hours

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another course in this course we will talk about the fundamentals of Python programming language at the end of the course you are going to be ready to write your python programs with the fundamentals you learned in this course this course is going to be completely beginner friendly we are going to start by installing the python and our code editor and we are going to finish the course with the classes and the objectoriented programming so if you're just starting to learn programming or python this course is a great place to start here I am at the python.org you can download the python from here from just visiting download section you can just select the package you want like you can just press download Python and then you're ready to go don't forget to get the download button for your operating system from here for example if you are using Windows you need to select the one with Windows and this is our second place our code editor if you're just learning python I recommend you to use this code editor but you can try others too but I'm going to be using the visual studio code in the course so if you download this and use this in the course it's going to be really really good for covering the everything in the course from here code. visual studio.com you can just press download and download this code editor and after that you are going to be ready to write your python programs and we are going to jump on the code editor right now so here we are at our screen that that when we install the code editor we downloaded so from here you can just visit here extensions and then from here you can install Jupiter Jupiter keymap Jupiter cellex they are going to be installed automatically if you just install the Jupiter we are going to use a jupyter notebook on this python course also you can download the python and pyland from here and after that you are going to be ready to go we can just close this extensions page after that what we are going to do is is we are going to open this page in here like I'm just going to click on it on the Explorer and after that here is our file base like from here by right clicking or using the upper buttons I can create files from here like I'm going to say right click and then I'm going to choose new file and I'm going to say first file. py so here is a python file you can create a python script from here like you can just write code you can say blah blah blah blah and after that you can just run and run the everything in the file but in the course we are not going to use that we have a special file format which is called Jupiter notebook which enables us to run sales instead of the full program like what I meant by sales I'm just going to show you we are going to create another file like notebook. IB and after creating this you can see a automatic sale came up and we can just close the other two like I'm saying going to say don't save I will close the welcome one and I'm just going to delete this since we are not going to use that and let's talk about the Jupiter notebook in Jupiter notebooks you can just add code sales like this and write programs separately and you can just run them sell by sell so you can do aot of things by not running the entire program which is going to be quite useful in data science and Python Programming this is going to be a course for python development and also it's going to be a base for a data science road map so whether you are trying to learn python or you are trying to learn data science this is a great point to start after learning the python you can just learn other things like you can just learn web development data science and extend your knowledge in that areas you can do game development you can do cyber security Etc you have a lot of options great now I'm going to delete this code sales and from the right side I will select a kernel from here I will say python environments and I'm going to select the python the most upto-date version like this and we are going to write our first code right now it's going to be our first program we will say print and we are going to say welcome to the course and I'm just going to run that if you're using Windows you can just press shift and enter for running the sale and getting a new one if you're using Mac you can just press command and enter and you can do the same operation and in here we just said print and we use the print function for printing something to here we display the welcome to the course in the screen the another thing we are going to cover is going to be the comments you can just use the hash character and add like this is a comment and you can just run this a comment doesn't affect your program and it just help other developers or the ones that you read your code to understand the code better like after this print statement I can just add a comment like here I added a comment and after that when I run this you're going to see it's not going to display this line here because it can be seen only in the development site okay I'm going to show you a feature of the Jupiter notebooks I'm just going to come in here and I will select the markdown and after that I'm going to say this is my first markdown and I'm just going to run this one and here you can see that I displayed a text in the place that I want and I can use the markdowns like this for adding comments on the Jupiter notebooks by the way following the course by coding like writing the codes with me is going to be really really helpful on your learning process and I highly recommend it if you just see on the screen and don't write them on your code editor or a notebook and anything if you don't write them you're going to forget them but if you write them and do practices by yourself you're going to learn better great now we will talk about variables and data types let's start with the basics in Python a variable is like a container that holds data we can think of it as a label attacked to a value variables allow us to store and M plate data in our programs with variables also I will show you the type function just like the print function which is going to help us on seeing the data type of a variable let's say name and after that for defining a variable I will use a q sign and after that I'm going to say my name like owner in codes we are going to cover the codes after this and I'm going to say type name and we are going to see that we will get a string in here so for creating a variable I will use a name and after that I'm going to say a co sign and after that it can be like integer like I will say h and after that I'm going to enter my age after that and when I do that when I call the age here I'm going to get the data type of integer right now the important thing in variable defining is entering a name and after that using a a q sign so let's do more examples on this I'm going to say height and I'm going to say 185 and I'm just going to run this and I will call the type of this and it's going to be a float in here let's do one more like is student and it's going to be no so when I Define this it's not going to give error because we don't have something like no but we can just say false instead of no we are just going to cover the B booleans after this I'm going to say is student type in here and it's going to be Boolean instead of yes or no we have true and false in Python in this examples we have declared four variables name age height and is student each variable has a specific data type string integer float and bulling now we are going to talk about them we will start with string the string data type is used to represent text strings in Python are in closed in single or double quotes so let's do a example with strings I will say like name again I'm just going to use single code and after that I will say honor and we are going to see that it's going to be string again and after defining this I can use this string in a function like I will say name like this and it's going to print honor so what it does is it stores this string in name variable and after that when I code a name in anywhere in my program it will return honor for that also you can Define strings with double quotes like let's say message and after that I will say if you like the course please like the video and subscribe and I'm just going to say print message I will just use it in a function and it's going to print the message in this example we have created a string variable named message and we just printed it strings are verse style and can represent anything from simple words to end typ paragraphs let's talk about integers integers are used to represent whole numbers without any decimal points they can be positive negative or zero so let's say number is going to be 25 and here it is when I call the type number number you will get to 25 and when we print the number here it is so when I call the type of the number it's going to return integer when I print the number it's going to return 25 because I just defined a variable as number with the value 25 also you can just do minus 25 and after that you will get a negative number printed in here you can just change the variables like this like I just wrote minus 25 in number and it up dated to minus 25 and after that if I say number equals to zero and if I print the number again we will see that it's going to print zero instead of minus 25 because we just updated it integers are commonly used to represent quantities counts or any situation where only whole numbers are relevant great let's talk about floats the float data type is used to represent numbers with decimal points floats are suitable for situations where Precision matters like let's say height again and let's say 63 64 doesn't matter and after that I will say type height and we will get the float in here and I'm just going to print the height like this and after that I'm going to see that 6.4 is displayed on the screen again you can use like minus like this for getting negative floats and you can just use 0.0 for getting something like zero here it is I'm just updating that and running the print statement again so let's just change this to 64 again and I'm just going to show you a thing like we said print height and it's 64 but what we can do with print functions is we can use multiple variables or multiple things together in print functions like I will say print and I will say hide after that I'm going to add a comma to here and after that I will say height so it's going to print height and 6.4 together so what else you can do is you can just say height string and you can just say height you can define a string like this and after that you can just say print height string and after that you can use comma then you can say height and you're are going to see that it's going to run perfectly okay let's keep with the flots flots are crucial when dealing with measurements calculations involving fractions or any scenario requiring decimal Precision great let's talk about bullings bullings are used to represent truth values either true or false you can think them like yes or no but in Python we have true and false not yes or no they are fundamental for making decisions in your code like let let's say is python great and I'm just going to say true after that I will just type the is python grade and here we get the bulling so let's print this like let's print this with the feature we just learned like is python great and I will say is python great after that and we will see that is Pyon grade true in here also you can delete this and PR print this by itself it will print through like this bullings are essential for creating conditional statements and controlling the flow of your program great we just learned variables and data types we have some rules and common use Types on variable naming so let's talk about them one of the golden rules of variable naming is to use names that are meaningful and descriptive this makes your code self-explanatory reducing the need for comments so let's do an example like name and let's set this like Clark and let's say age is 35 and let's say is adult and it's going to be bulling true so these variable names clearly convey the purpose of each variable making the code more understandable okay let's come to the pon rules on variable naming we can't start with a number or symbol when we are defining variables like I can say three variable and it can be like 40 it's going to raise an error one of the golden rules of variable naming is to use names that are meaningful and descriptive this makes your code self-explanatory reducing the need for comments so let's do an example like name and let's set this like Clark and let's say age is 35 and let's say is adult and it's going to be bullying true so these variable names clearly convey the purpose of each variable making the code more understandable okay let's come to the python rules on variable naming we can't start with a number or symbol when we are defining variables like I can say three variable and it can be like 40 it's going to raise an error but if I just delete this it's going to work smoothly so we can't add numbers it's same for the function ation like minus variable is going to return error also underscore variable is going to raise an error we can't use any symbols in variable naming like I can say event name and I can just say like basketball match it's going to raise an error but with with one exception that you can use underscore in variable naming like you can say event and and name with underscore and you can just say basketball match and if you do like this you are not going to get an error you are going to create your variable successfully this is only for the underscore also we have another rule like be cous not to use reserved keywords as variable names like print was a reserved keyword it's a functions name in Python doing this can lead to unexpected behavior and errors in your code like if I try to print and if I just Define a variable like print and I say basketball match and if I just run this it's going to work smoothly but when I call the print function I can have some problems like here I can't use the print function like this I will say python is great in print function but what happened is we can't use the print function because we defined a variable like this so try not to do that but how you going to change this from here you can just say restart but before that you are just going to delete this sales like I will delete this one and when I press the restart in here and I will say restart and after that I'm going to say run all and you will see that it's going to be stuck on the errors like we have errors in here and run the others it's going to run our runtime sell and it's going to stop at the first error at this place so I'm just going to pass them and I will come to here we can see that our print functions name is changed like the color of it changed and now I can use the print function safely if you did something like we just did you can just press restart and you can say restart for restarting the kernel and after the restart you can just do things again without the problems like you can just use the print function again so again we have a function named print in Python so not using it as variable name will be a good practice let's talk about common naming practices when naming variables there's two common methods and they are called snake case and camo case this convention en change consistency across your code base snake case gets used like lowercase letters and underscore bit in the words like this and the camel case is like upper case letters for the first letter of the words like camel case and we can just say 40 for example from here we can see that we use underscore and Lowa case letters on snake case and we capitalize every first letter for word in cam case great let's talk about arithmetic operators arithmetic operators allows us to perform mathematical operations in Python these include addition subtraction multiplication division and more let's define two variables like Number One S 10 and number two s five and let's start with addition let's say addition is going to be equal to the number one plus number two so if we print the additions value we will see that it's going to be 15 also in a print function you can just do 10 plus five and after that you will see that it's going to print 15 let's do subtraction we will say subtraction is going to be 10us 5 which you can also do with variables like number one minus number two and if you print its value it's going to be five great also I want to show you that in normal Python scripts like thep extension files you can just print something with writing print function but in a Jupiter notebook if you just write the variable's name like this you'll get its value like you write the print you don't need to use print every time great so let's get the multiplication right now you'll say multiplication and it's going to be equal to number one multiplied by number two and let's see its value from now on I'm not going to use print in the most of the topics and I will just write the variable name that's all Al do division we will say result Division and it's going to be like number one divided by number two and let's get the result division also you can do an operation like 10/ by 5 like strictly like this and you will get the same result since we are in Jupiter notebook we don't need to write the prints in the most of the scenarios great so let's talk about exponentiation we will say exponentiation and it's going to be number one power number two and let's see what's what's its value like as you can see 10 with power five is going to be 10,000 great so in these examples we have covered various arithmetic operations using two variables number one and number two so let's talk about the order of the operations it's crucial to understand order of operations often remembered by the acronym pamas p e m d a s and it's like parentheses exponents multiplication and division from left to right and addition and subtraction I want to write this for Clear demonstration like parentheses exponents multiplication and division left to right and also addition subtraction from left to right also it's need to be like division all of these are left or right let's do an example like p e m d a s I just read it like pmas but I'm not sure how it's R and I will say number one + three and let's multiply this with number two and let's divide this like with two and give the power of two and let's see our result in this example we have used parentheses to enforce a specific order of operations the result is calculated following the P MD rule ensuring that multiplication and division take precedence over addition and subtraction python also provides augmented assignment operators which offer for a concise way to perform arithmetic operations and update the value of a variable let's see how they work so let's go like let's set zero to total and then let's use total plus and aute sign together and let's say five this one is going to make our total five it's PR same like I'm going to add here as comments I will say total equals to Total + 5 these both are same like this one and this one is completely same let's do like total minus a cosine 2 and let's see the value of total in here it's going to be three and it's same as total total minus 2 these are same so let's do a multiplication like this total multiplied a sign three also it's accur total total multipli by three and let's see the value of this like nine great in this examples we have used the plus a sign minus a sign and multiplication symbol a sign operators for augmented assignments these operators offer a shorthand way to update the value of a variable based on its current value okay let's talk about comparison operators these operators allow us to compare values and make decision based on the results let's explore the different comparison operators available in Python let's firstly talk about equality operators it's going to be double AC sign and exclamation mark with AC sign the acity operators allow us to check if two values are a or not so let's say like X is going to be a to the five and let's say Y is going to be a to 10 so let's check like if x is equ to Y so let's say like Result One and it's going to be equ to the x equal to Y also we can add parentheses to this it will both work I will just show you both when I run this when I get the value of this we are going to see it's false it returns a booing we can also check it's type like type result one and we are going to see it's a bullan so let's just delete this type thing and I'm going to show you if I remove the parentheses is it's going to be the same so I will just update this one and update this one this checks if x is a to Y so great so at the another step we will see that result two is going to be X exclamation mark a sign and we will say y also we can't add space to there and I'm just going to run this and I will say result two in here so it's true this is like if X is not equal to Y also you can add parentheses if you want it's not necessary but you can do that and it will work in the same way in this example we use the equality operator to check if the value of x is equal to the value of y we also use the inequality operator to check if X is not equ to Y okay let's talk about relational operators like greater less than less or equal to greater or equ to relation operators helps us compare the magnitude of values whether one value is less than greater than less than or equ to or greater than or to another so let's say like a and let's say 15 for this B let's say 24 this and let's check if a is less than b like let's say result one again and I'm going to say a less than b so let's print the result one true 15 is less than 20 great let's check if a is greater than b so for that we will say result two and I will say a and then I will say B and let's print the result too it's going to be false like this and now we are going to check if a is less than or equal to be so for that I will say result three and I'm going to say like a is less than or equal to B so I will just get the result three in here it's going to be true also you can directly do like less than or accute to b instead of assigning to a variable but I'm just assigning them to a variable for this to be more informative so let's check if a is greater than or equ to B so for that we will say result for and it's going to be like a greater than or equal to B and it's going to be false great in this examples we use relational operators to compare the values of A and B in various ways checking if one is less than greater than less than or a to or greater than or a to the other we can chain multiple comparison operators together to create more complex conditions like let's say number and let's set it like 25 and then we can say result again and it's it can be like 10 less than or equal to number less than or equal to 30 so we have two conditions in here and let's get the result in this example we use a chaining of comparison operators to check if the value of number is between 10 and 30 this is a concas way to EXP Express multiple conditions so let's see object comparison the is and is not operators are used to check if two objects reference the same memory location let's say variable one and let's set 10 to this and let's say variable two and let's set 10 to this two so we can say like result one can be variable one is variable two and let's get the result of this like result one it is true since they have the same value and we can just use is not same like this result two and it's going to be variable one is not variable two and let's get the result two in here like we have false in this example we create two variables variable one and two the is operator checks if both variables reference to same object while is not checks if they don't great now we are going to turn back into Strings and we will talk about the string Methods let's redefine strings like strings in Python are sequence of characters and python provides a rich set of tools for working with them so let's create a string variable like greting and it's going to be like hello and after that what we can do is also let's say like hello python and what we can do now is let's see some basic operations like we can get the length of this like length function we have in Python it's a build in function and we can say length greeting and it's 14 this 14 here is the character count like five in hello six space also included 7 8 9 10 11 12 13 and 14 we have 14 in here we can access to individual elements like if you want to access to the first character we need to say greeting and we need to say zero because in Python indexes start from zero like if you want to reach the first one in all of the scenarios you need to write zero so the first index is going to be zero and the second one is going to be one like if H in here is index zero and this e in here is the index one so if you want to reach to the last character here we need to say Greetings variable name then we need to Open brackets and we need to say 15 see I'm sorry 13 since we started with zero so we are going to have this Mark In Here Also how we can do that is we can just say greeting and say minus one inside of this bracket and if you do that we will reach the same result it will just go from here this age to left and take the first one if we say minus one if if we say minus two it's going to give us the N like this great so it's like the same for the others if you want to access to element of a I'm not sure if I talked about it yet but for a list tle Etc you're just using the variable name and you will add brackets then you will give the index number if I didn't talked about the list topple Etc that kind of things it's just coming in the I'm I'm thinking like five or six minutes of this course and you will see that you can also slice the strings like let's call our string again like the great thing let's say we want to get a subring like seventh character to 13th and here it is python let's say if you want to get the exclamation mark to we can say 14 in here great we can add strings together like we we can just say greeting and we can say plus arithmetic operator and we can say welcome and it's going to work smoothly like it's going to be like hello python welcome you can do that in a print function like greting plus let's give it an empty string for a space and we can say welcome and here it is our result but one important thing in here you can't add numbers to here if you're adding strings all the types is needs to be string like it's going to raise an error but if I give this 25 like in a string format not integer it's going to work smoothly like this one in here is not an integer it's a string you can also just check the type of this from here for making sure it's string if it's written inside the code in this examples we have covered various basic string of operations we have calculated the length of the string accessed individual characters using indexing slice the string to create a subst string and concatenated strings together python provides a variety of buil in string Methods that Mak string manipulation more efficient and convenient let's explore some of them so let's create a string again like sentence is going to be like python is an amazing programming language and what we can do is we can change the case like sentence. upper method in here we can see that all the letters are uppercase and we can do like sentence that lower for getting them or in lower case we can find substrings like sentence. find method in here amazing and we can see its index it's going to be the starting point of its index like 6 7 8 9 10 11 12 13 and from 13 here is amazing right also see that after the dot I'm I can use the methods like from the variable name and using dot I can use the upper method in here you can do that too you can try different methods like sentence Dot and after putting that you are going to see the list of methods in here and you can just try different things from here here let's see replacing so we can say replace and after replace we need to give like let's say we want to change amazing with powerful and when we run this you are going to see that python is a powerful programming language also this sentence is not modifying the original variable like if I call the sentence again we are going to see that it's going to be a python is an amazing programming language so what we need to do in here is if we want this to be our original sentence we just need to do an assignment like we need to say sentence and we need to reassign the variable like sentence. replace amazing with powerful and if you do that you are going to see if we call the original sentence variable it's going to be changed to powerful so what we did in here is we just reassign the sentence with this acute sign also we can count to occurrences like we can say sentence and we can add Dot and we can use the count method and we can just give the i in here like this and it says two let's check them one in programming and one in is in this examples we have used various string Methods like upper lower find replace and count to mplate the string sentence these methods offer powerful tools for working with text Data let's talk about string formatting string formatting is a powerful feature in Python that allows us to create Dynamic and readable strings let's explore different ways to format strings let's say name and it can be like Jack and let's say age and it can be 30 so we can use f strings what are they we can say formatted string and for creating F string we just need to add F before the code so I'm just going to create a normal string like this and I will say say hello my name is and after that if I use color braces I can insert variables like I can say name and I will say I am K brac's age years old and I'm just going to set this and call format string it's going to return hello my name is Jack and I'm 30 years old what if we don't put this F or Cur braces it's not going to work like that and and we can get this in that scenario so if you want to use F string with color braces it's a really nice method for string formatting also we have another one the format method like we can say formatted string two and it's going to be hello my name is empty C braces and I am empty C braces again and years old but after this string and we will say do format and we will pass the name and age with the order we want to create so if I call the format string again it's going to say hello my name is Jack and I'm 30 years old but this order is really important if you just say age and name it's going to mix them up and it's going to give H something different so we don't want this so we need to take care of this order in here name age now it's cool go great in this examples we have demonstrated two common ways of formatting strings F strings and format method this techniques allows us to insert variables into Strings for dynamic content great let's talk about conditional statements conditional statements are a fundamental building block of programming they allow our programs to make decisions based on certain conditions let's start with basic if statement so our keyword is if in here let's set age to 25 and we will say if AG is greater than or equal to 180 print you are an adult and when I'm just making this work it's going to print that you're an adult but it's working because this condition is satisfied this is true like I'm just going to run this in here and it will return true but what if it not so I can just change the agent here to 15 and it's not going to work in this scenario it didn't print anything so okay in this example we have used an if statement to check if the variable AG is greater than or equal to 80 if the condition is true the indented block of code is executed printing you are an adult what if that condition is not satisfied we have the keyword else for this scenario so let's say like age is going to be 15 this time and we will say if age is greater than or equal to 18 print you are an adult and we will say else right now we don't need to give any condition to here we can say print you are not an adult so what this does is it checks this condition it passes it since it it is not satisfied in here after that it comes to here we don't need to specify any condition in here and when I run this you will see that we are going to have this lse block worked sometimes we need to check multiple conditions this is where the L if which is the combination of as and if statement comes in play so let's say like temperature is 28 and we can say that if temperature greater than 30 then print it is a hot day and we will say no else if LF 20 less than or equal to temperature and less than or equal to again 30 print the weather is pleasant and we will say else and it's going to be like print it is a cold day and here we can see that it's catched by this else if block right now it checks for the first if then it passes it check it gets called in the second if so it doesn't print it's a cold day if you just delete this you will see that it's going to print else plug we have but if you just insert this it's not going to because it's going to be catched from this in this example we have used LF to check multiple conditions depending on the temperature the program will Print Different messages conditional statements can also be nested allowing us to create more complex decision- making structures like we can say is raining and it can be like true and we can say is cold and it can be like false so let's say if is raining print bring an umbrella and we can say if is Cod then we can say print where a jacket to so it's only going to work if this if condition is satisfied it means that it is true like if you assign True to this like in here it's going to work and what we can do is we can say else and we can say print enjoy the day so it only printed bring an umbrella because because this one worked this one didn't work because this condition is false and it didn't get C from else because it get taken by this if like the this else is connected to this if statement in this example we have nestive statements if it's raining the program advises to bring an umbrella if it's cold as well it suggest wearing a jacket if it's not raining it encourages enjoying the day for simple conditions python offers a conc terer operator let's explore how it works it's going to be a one liner like we can say age 24 and after that we can say you are allowed if age is greater than or equal to 18 else you are not allowed and it's going to give your are allowed and we change this to 10 it will say you are not allowed this time and if you change this to 50 for example it's going to say you are allowed so in this example we have used the tary operator to assign a value to the variable message based on the condition it provides a concu way to express conditional statements let's talk about four and while Loops we will start with while loop no let's start with for Loop I'm going to start with for Loop not while the for Loop is used when we want to iterate over seconds a specific number of times let's start with a basic example I'm going to create a list like fruits and it's going to be apple banana and I will say cherry so after that I'm going to create my for Loop like for fruit in fruits print fruit so how this work is for every element in fruits list like for apple banana and Cherry it's going to iterate three times it's going to print the element like you can just change this fruit as index whatever you want but you want to use the you you need to use the same thing inside the for Loop and it's going to work in the same way in this example we have used the for Loop to iterate over the fruits list the loop variable fruit takes on each value in the list and we print each for it the range function is often used with for Loops to generate a sequence of numbers so let's explore how that works we will say for number in range one to four print let's use an F string in here let's say number and we can say number this one in here the element indexer and I'm just going to make it work and we can see that number one number two and number three in this example we have used the range function to generate a SE of numbers from one to three the for Loop iterates over this range and we print each number okay now we can talk about the Y Loop the Y Loop is used when we want to repeatedly execute a block of code as long as certain conditions true let's explore a basic example let's say that count equal to zero and I'm going to say while count less than three I will say print F string again count and I will give the count in here so what I'm going to do in here is I will say count plus a sign one so when I run this you are going to say 0 1 and two so it works as long as count is less than three and count is zero at the starting point like in here and we print that value but after that we add one to count so it's increasing in every Loop and we get the two and we can't get three because now there is no equ sign if you do that it will also print the three and we need to reassign this also but let's delete this and make them rerun it it works only three time because we are increasing it by one every time in this example we have used used a y Loop to repeatedly print the value of count as long as it's less than three we increment count within the loop sometimes we want to control the flow of our Loops using break and continue statements let's see how they work by the way we are using lists like numbers 1 2 3 4 5 we are going to cover the lists and other data types in our forward video processes but I'm just using them for giving examples this means that we have brackets for creating a list and we have five elements in it like one two three and four and five they are all our elements and I'm just using them because it's going to be clearer to cover these concepts with lists and we will cover what are they in the further processes of our course knowing only the list is created by the brackets and we have five elements on it it can be also strings and multiple random things like Apple 2 three Cherry five this is also a way to create a list and we will cover that okay let's give the same example like create the list like numbers and we can say 1 2 3 4 and five we have five elements in and we if you want to reach to them we can say numbers and brackets let's say we want to reach to first one we can just say zero and we can get the one because our first index is one great so we will cover the lists later on and we have the numbers list like you can check the type of numbers and you will get list okay let's talk about the break and continue let's say for number in numbers if number equals to three break and then print F string number and number so what it does is it exits for Loop when number is three and it goes over one firsty then goes in second firsty and when it's comes to the third element the second index it quits to Loop because number accus three this if statement work on the scenario like we inserted an if statement in a for Loop in here so if you want to give an example to the continue we can say for number in numbers again we are going to look through this list we can say if number ID by two equals to one continue there's typo and otherwise we will say print F number number it just kept this two in here because 2 / 2 is 1 it gets called by this condition and we just skip that with continue keyword so break for exiting the loop continue for skipping that and keep going on let's do an example with conditions and Loops like let's Loop through a string so let's say word is going to be equal to the python and if you write a for loop on this it will iterate through p y t h o n n so we can say like for character invert we can say character. lower and we can print this here we get the python in correct order and we can also say upper after character and we will get all uppercase letters in this scenario so if we use for loops on strings it iterates over the every character let's talk about indentation we can think it like spaces before codes like this print statement have four spaces and I give the space with tab you can both use tab or space you can press tab for giving four spaces python relies on indentation let's dive into the details of this unique feature indentation is not just aesthetic in Python it's a fundamental point of syntax it's used to define blocks of code such as those within Loops conditional statements and functions and we already saw examples like inside the for Loops we give inent for spaces in here like in the both loops and conditions like I'm going to if statements you can see that we are indenting here we are nesting this if statement and we we use indentation it's a really crucial thing in Python and let's do examples with this let's say if true print this line is indented and also so print so is this one and we can say outside of this condition this line is in not indented so I'm just going to make this run and if I say false we will see that this two is not going to work because our condition is false right now so it will only print this one since since it's not indented but if I say True to this you'll see that all of them is going to work mixing spaces and tabs or having inconsistent indentation levels can lead to have syntax error so we don't need to do something like if true and we can say like print this line is indented with spaces and we can say like print this line uses a tab which is not consistent they need to be at the same level like if I press tab in here it's going to work but I used space eight spaces in here after this if enter I used four extra we generally don't do that so we need to check the indention levels let's Let's do an example for Loops like for index in range three let's also firstly print this for remembering it print index and we get 012 and let's say print this line is inside the loop and let's say print so is this so this is two and we can say print this line is outside the loop Also let's make this run and we see that it repeats three times this because it's inside the loop it's going to Loop through this three times and it prints the outside think at the end let's talk about lists tle sets and dictionaries I showed you lists like with brackets and 1 2 3 four example but we are going to talk about it more right now we will be using them a lot when we are coding in Python understanding these St structures is crucial for Effective and verse style let's start with lists a list is a worse style and mutable data structure in Python it allows you to store and mplate collection of items let's create a list like fruits and it can have values like apple I'm going to store strings in it banana and Cherry so we have three elements for defining each every different element you can say this comma in here and then create a new element like you can insert the variables to inside the lists like let's say apple is going to be apple and you can just chuse the variable it won't raise an error in this scenario it can store variables integers floats strings any type type of data and here is our fruits list so let's access to its elements like let's say first fruit and let's save it like fruits and we will get the first one the Apple so in Python indexes start from zero so we will say zero and let's print this first fruit and it's apple as we can see Also let's get the last one like last fruit and we need to use fruits two or minus one and we can just access to this with last fruit remember that if you want to go back to the last element and go from right to left we can use minus let's do slicing in the list like fruits can be like one to three remember this bracket and the operations from the strings they are same we can check the type of our list like type first it's going to be list and let's get the list again so let's see some more methods like we can say fruits. append and we can say grape and it's going to add the grape to the list like this and if you want to remove an element we can say fruits the pp and let's say we want to remove the element with the index number two so it's cherry and let's call the fruits again you will see we have three we just added grp in here with aend and dropped Cherry in here with pop method and in this examples we have created a list of fruits and demonstrated various operations such as accessing elements slicing the list modifying elements adding new elements and removing elements okay great let's talk about topples a topple is similar to list but unlike lists topples are immutable which means that we can edit the lists and we can't edit the topples once a tle created their elements cannot be changed like in lists we just added grape in here and we dropped Cherry in here but we can't do that in tles so let's create a tle so what's the difference on creation is let's say dimensions and we need to use normal parentheses after this instead of instead of brackets so if I just say 10 20 and 15 and I can change its elements right now but we can still reach to them like we can say dimensions and let's say we want to reach to the first element I can say zero and I can get 10 and we can get the like blast element like this there's no problem on accessing the elements and we can just check the type of Dimensions like this and it's topple topples are useful for St ations where you want to ensure that the data remains constant throughout the program okay great let's talk about the dictionaries a dictionary is collection of key value payers providing a flexible way to store and retrieve data okay let's talk about them we can create dictionaries with Cur braces like let's say students and we can say Cur braces then we can say name and we need to use this after that and we can say let's say Alice for the name and after the comma we can say h and we can say 22 then we can Define like major and actually it says student not students for the major we can say computer science and we can say is student and it can be true so when I call student again you will see see that we will get a dictionary like name and its value Lis age and 22 major computer science and each student true so let's check the type of this it's going to be a dictionary it enables us to store keys and values together and we use dictionaries a lot when we're programming in Python so how we can access its values like we can say student and we can after that we can say say with brackets name so we will get Alice and we can do like student age and it will return return 22 if you want to modify a value like you can say student age and let's make it 30 so when we call the student again you will see that our age is going to be 30 and let's say we want to add another thing to here we can say like student and we can enter the new data point we want to create and we can say like a and when we call the student again we will see the grade two and if you want to remove that we can just say remov major and we can say student. pop major and let's call the remote major we can see that computer size is removed and we can call student we won't see the major right now great in this example we have created a dictionary representing a student we have demonstrated how to access values modify values add new key value payers and remove K value payers so let's talk about sets a set is an unordered and mutable collection of unique elements it's useful for tasks that involve testing membership and eliminating duplicate entries for defining sets we will say like colors set and we will open Cur Braes again but this this time we won't give key value type of data we are just going to create data like tles and lists I will directly say the value and then I will use comma like green also you can give integers like this twoo I will say red and I will say yellow we can call like type color set and it's a set as you can see we can get the length of this also we can get the length of the lists and Tes for checking a membership like we can say is green here and we can see like green in color set so we we have green in here so it's going to return true and let's get the value of the is green here this needs to be true great now we are going to talk about a really good feature of python which is named as list comprehensions list comprehensions provide a compact way to create lists making your code more expressive and readable list conventions allow us to create lists using a single line of code they are conage and often more readable than traditional for Loops so let's start with a basic example we can say squares and we can create an empty list then we will say four number in range five squares append number and power of two we need to use that after this and I will just make it run also let me show you the output of this for Loop normally I will say print number and you will see that we will have 0 1 2 3 4 5 if I do this we will see that 149 like this great so we are just adding them into our list like in here with aen method like in every iteration with the append method we add a value like the we add 0 the power of two 1 2 3 4 5 and we add them into scur list we created an empty list and by using the epen method we add the number each iteration in each iteration to this list so in this example we have used the traditional for Loop to create a list of squares now let's do the same using a list comprension so I will say that squares list comprehension and it's going to be like number and this for number in range five it's its syntax so if I run this and call this it's going to give the same result just like the for Loop but in a single line and and it looks really readable right when you get used to this it's a really nice feature and it's commonly used on Python Programming okay let's talk about nested data structures nested structures such as lists of lists or dictionaries of dictionaries allow us to represent complex relationships and hierarchies in our programs one common form of Nesta data structures is a list of lists this structure is particularly useful for representing matrices or grids so let's say Matrix and we can say like we are going to open a list with brackets and then for the first element we are going to open a new list and we will say one two and three so in this side we have an element for the the main list and our element is have three elements inside so we have two lists in here and I'm going to create a new one like 456 and I'm going to create a new one let me open up a space for that for a Clear Vision 7 8 n so we have four lists in here one main list and three in the elements so I will just make it run and I will just call the Matrix you will see that we have three different lists inside the main list if I get the length of this it's going to give me three since it has three elements like this great now we can access to its elements like Matrix Also let's just delete this if you want to reach to the first element the second element I'm sorry this one and I will just run this here and if I want to reach to this five I'm going to say one again I'm just going to add another bracket and here we have five we firstly reach this after that we reach this this in this example we have created a list of lists representing a matrix we can access individual elements using double indexing where the first index refers to the outer list and the second index to the inner list working with nested lists also provides modifying and adding elements so let's say we want to change this one in here and we want to make this 99 so we can say Matrix the first index and the first index of it so we will say zero again remember indexes starts from zero in Python and let's first reach to that here it is and let's set this to 99 and when I call the Matrix again you will see that that value is going to be changed and we can add a NE R like let's say n row and we will say 10 11 12 and we can say matrix. append just like in list we can say Nal and we can call the Matrix again and you will see that 10 11 12 is going to be added up in this example we have modified an existing element in the Matrix and added a new row to the list of lists this showcase the versatility of Nesta structures another powerful form of Nesta data structures is a dictionary of dictionaries this allows us to create a hierarchical structure with key value pairs so we can say like students and then we can use Cur braces for creating a dictionary and let's say like Alice and we're going to open a dictionary again and we can say like H = 22 and major is mathematics and we can just add a new one like Bob and we can say age = 25 and major equals to economics and let's call this students dictionary and if you want to access to the Alice's age what we can do is we can say students Alys and after that we can say h like this in this example we have created a dictionary of dictionaries representing student information we can access individual elements using multiple key indexing modifying and adding elements in a dictionary of dictionaries follows a similar pattern let's see it con action like let's say students Alys and age and we can say it like 25 and after that when we reach to this element we will see that it's 25 and let's also update this one and let's add a new student like new student and it's going to be age let's say 20 major physics and we will say student let's say Charlie is going to be equal to the value new student so when I run this and call the students again we will see Charlie too in this example we have modified an existing element in the dictionary just like in here and then we added a new student to the structure python allows us to combine various n structure es to represent even more complex relationships so we can just do like copy this entire thing and after that we can say people and we can open a list then we can paste this dictionary In Here Also let's fix this great so when I run this we will see that we will have people a list and we can check type of this it's a list but we can take the first element of the people and check the type of this and we can get the first element of this dictionary like zero and zero now you will see that we will get an error because this one was a dictionary and we need to write it like Alice and after that we will get age and major in here in this example we have created a list of dictionaries representing people we can access individual elements using both list indexing and dictionary key indexing like this one in here great let's talk about functions functions allow us to encapsulate and reuse code making our programs more modular and readable defining a function involves specifying a block of code that performs a specific task so let's start with a basic example we have the keyword Define DF and after that we will give the function name and we are going to open parentheses and let's say like when we call this function we want it to print hello python lover lovers okay something like that or we can just write hello so after defining this we can call this function in our program like this just like the type of print function we can use that in this example we have defined a simple function named GRE that prints a greting message the function is defined using the defined keyword followed by the function name and parentheses we then call the function using its name and parentheses functions can accept parameters allowing them to receive input values so let's explore how this works let's say Define grid person and let's add name in here and then we will say print F hello and we will say name so let's talk about this here we give a name in function and then we use this give name in here it's not a variable we just give it when we call function and we use it in here like let me show you create person and I'm going to write my name and it's the name is going to be my name and we can also change this to Bob and you will see that the name is going to change it's not a variable it's the input of the function in this example we have defined a function named grd person that takes a parameter name and prints a personalized greting when calling the function we provide an argument for the parameter such as GD person Bob functions can have default parameter values which are used when an argument is not provided this provides flexibility on function CS like we can say Define grid with default and we can say name is going to be equal to the charie and we can say print hello name again and here if I don't give a name to function it's going to give like also we need to use it with parentheses if you do that we won't get this print message we will see that hello Charlie but we can give it name inside like Bob and it will print the Bob this time but if we don't give anything to function it will just print this default thing Charlie like this in this example we have defined a function name grid with default with a default parameter value for name if no argument is provided when calling the function it uses the default value functions can return values too allowing them to produce a result that can be used used elsewhere in the program so let's define a function like square and give number inside and let's return we have the return keyword for returning values number squared version of the number so when we call this it's going to give an error because we need to give an input and when we say five it will return 25 great so we can just store this like result SC five and then we can use this in our programs like result we will get the 25 again so in this example we have defined a function name square that takes a parameter number and Returns the square of that number we call the function and use the returned value in the program functions can have multiple parameters and return multiple values like we can say Define calculate rectangle area and let's say we want to give length and width and after that we what we can do is also I was writing I wasn't going to make it run so I can just say clear cell outputs from here and I will say area is going to be length multiplied by width Also let's add something like per meter is going to be two times length plus width actually I made a typo in here I'm just going to change that and we will return area and parameter so also now we are going to see a different variable creation like we can say area and parameter and we can say calculate rectangle area four and six so when we run this we will get values for both area the first one and we will get a value for both per meter the second one you can create variables like this but if you mix the order of them it's going to change so the order is really important on this two like if I change their position you will see that the values are going to change like this we don't want that okay let's see a function with variable number of arguments so we can say Define calculate sum and regardless of the number entry like user can enter 20 numbers we will say arguments in here we will say total and we have a function like sum for getting the sum we will say arguments and we will say return total so here we can say calculate some after creating this and give 1 2 3 4 5 6 7 8 and a lot more and it will return the sum of this you can just delete some of them and it will work the same like this in this example we are defining a function named calculate sum that accepts a variable number of arguments using the argument syntax this allows the function to handle any number of input values I'm just repeating everything after each operation for this course to be more informative let's talk about Lambda functions Lambda functions are a concise way to create Anonymous functions in Python they are often used for short onetime operations we can Define them in one line like we can say square and we can say Lambda x x and two like this and after that we can just use like result is going to be square 4 just like the regular function here it is it works exactly same as the function we defined let's talk about modules in Python a module is a file containing python definitions and statements these files can Define functions classes and variables that can be reused in other Python scripts we have a keyword import for that and we are going to let's say import the random module and after that we can use its attributes functions like I will say random and I will use random integer and I will give it the range of 1 to 10 also you can see at the upper side it return random integer in range including both end points and it shows inputs of the and I will say enter and it just returns a random number it will change every time when you run the sale and it's a nice module that you can use a lot in your programs after the import of the module you can use its attributes like this in this example we have imported the random module using the import keyword the random module provides functions for generating random numbers we use the random integer function from the random module to generate a random integer between one and 20 you can also import specific functions or classes from a module reducing the amount of memory your program uses like you can say from random module import choice and Shuffle methods and after that after this import you can use this two but you can't use others if you import it like this like let's say my list is going to be 1 2 2 three 4 and five and if you want to make a random Choice from this you can just say random. choice or directly Choice since we imported directly we can say my list and it will give it the random Choice from this if you do like random. Choice random. random integer you will be using this if you directly right choice you will be using this because you are importing this one directly and you can use Shuffle like this and it's going to be like it will change its order you can just print this for seeing it will just update the original list like when you do Shuffle it won't work with print because it will return none if you use shuffle my list it's going to be shuffled and you can just call this again make this run again call this again it's just going to change the order of your list like this okay so in this example we have imported specific functions from the random module we use a choice function to pick a random element from a list and a shuffle function to randomly rearrange the elements in the list sometimes module names can be lengthy you can use alysis to create shorter names for modules making your code more concise so what I'm talking about is I will say import random we have a keyword s and we can say R&D and after that I can just use random as like R&D I can say R&D and then I can use its features like random integer one five so here it is if I import it like this with s keyword I can use R&D after that in this example we have alas the random module as R&D this allows us to use the shorter name R&D by referencing functions from the random module python comes with a variety of buil-in modules that provide essential functionalities let's explore one of them the math module like we can say import math and after that we can say math and we can use its functions like you can just scroll in here and see so I will say square root and I will say 25 also we can do like math. factorial 5 and we will get 120 great in in this example we have imported the buildin math module and used functions like square root to calculate the square root and factorial to compute the factorial of a number great let's talk about classes and objects in Python it's going to be our last topic for this course in Python a class is a blueprint for creating objects an object is an instance of a class and it can have attributes and methods attributes are the characteristics and methods are the functions we will also talk about objectoriented programming object oriented programming is a style of programming characterized by the in identification of classes of objects closely linked with the methods with which they are associated so you will understand better it's just a definition and we can start to learn about them right now I will create a class we have a class keyword for creating classes I will create a class as Dog and Its syntax is like Define then we need to use in it in here it's a common thing for literally every class we will say self name and age we need to use this self too and after that we can just use like self. name is going to be name and self. AG is going to be age and this is a characteristic of this and we can just Define a function like Define bark and we can say self and we can just use print F self. name size wo here it is so we have a characteristic in here and method in here so great let's talk about this in this example we have defined a class name dog the class has an init method which is a special method called a Constructor The Constructor is used to initialize the attributes of the class we have attributes name and age additionally there's a method named bark that prints a bark message once a class is defined we can create objects from it objects are instances of the class and they can have unique values for their attributes so we can do like dog one and we can use dog class let's say body and let's say three the name is going to be body like in here we need to give it in the same order we don't care about the self in here name is going to be buddy and age is going to be three and also let's say dog two and we can say dog Max and we will give five in here actually I need to add parentheses not brackets great it looks better now we can call this characteristics like dog one that name is body dog do one AG is Three Dog two name is Max and dog two AG is five now we can use this bark method with two of this like we can say dog one. bark and after that we will see the method B method do. bark and if you add parentheses after this we will see that body says move this function like let's delete them let's see it layer and if we say dog 2. bark we will see that Max says wol so this is how we create classes and use them we can talk about inheritance and other features of the class but I'm not going to include them in this tutorial for this to be simple and not more complex if you want to just see them you can just visit my YouTube channel and I have a full video of classes it was like a 7 minute and I show everything about classes on there so I'm not going to include in this tral because it's going to be L literally complex if I add them so with the class definition we can finish our course it was all from me for this video and thanks for watching if you watch this far because it was a really long video I think it's more than an hour and thanks for watching you can like the video share the video or subscribe to the channel for supporting me I'm creating content about Python Programming and data science and I'm trying my best on providing valuable content and you can just check the videos if you want I have a full data science learning playlist with the name learning data science in 2024 it has like 20 courses inside of it you can just check the playlist from the channel playlist page because I have a full playlist of python videos that are python projects and python tutorials I have a full playlist of data science projects data science projects I teach on the channel you can just check them for seeing more informative videos if you are learning programming I'm pretty sure that you can find valuable content in the channel playlists page so you can just visit that thanks for watching again thanks for spending your time in this video and have a great day see you in another course
Info
Channel: Onur Baltacı
Views: 3,646
Rating: undefined out of 5
Keywords: Data Science, Data Analysis, Data Engineering, Machine Learning, Deep Learning, SQL, Python, Data
Id: VOdPQmm298o
Channel Id: undefined
Length: 89min 12sec (5352 seconds)
Published: Sat Dec 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.