Transcript for:
Python Basics Overview

what is up guys in this video i'm going to be showing you how you can get started with python in less than 10 minutes and i'm going to be using python 3.8 for this along with pycharm as my code editor you can just search these on google and they will pop up immediately they are free and open source so definitely do check those out otherwise let's get started immediately with the first thing you should know in python and that is how to assign values to variables so to create a variable all you have to do is decide on a name such as item and then you can just assign it a value this is a string and a string is just any form of text for the program to read and you also need to keep in mind that python is a case sensitive language which means item with a small i and item with a big i are going to mean absolutely different things and also a naming convention in python is to add an underscore every time you have more than one word so if we say item name you want to add this underscore over here and we can just say orange so now we have a few strings assigned to three different variables let's go ahead and print first the difference between item and item just to show you that there is a difference so we'll go ahead and click on run and you'll notice it will say banana and apple now let's go ahead and print another string which this time is going to be hello and we're going to add a plus and the item name so as you can see we can actually combine text and when we print that it's going to say hello orange next we have data types so the first one is the integer which is just any number and you're going to notice it being written in blue then we have strings which as i showed you earlier is just any text inside quotation marks then we have booleans which is just a simple true or false so as you can see we can just type in false here or true here and that's all that it is it just says true or false and the final one you should know for getting started is list which all you have to do is create a pair of angle brackets and insert the values you want inside here and it can be a mixture between numbers booleans and so on but for this example we'll just keep it simple and write a few strings then if we go ahead and print these you're going to notice that we will get 2021 text false and the list but let's move on to an example that we want to print these values combined so let's say name is mario the string number is going to equal 22. so you're going to notice 2021 is a blue number and this one is green because this is considered text while this is considered an integer so what we need to do is actually convert them into the correct types before combining them so to do that we can just go ahead and type in print name plus the string value of integer so it's going to convert the integer to a string so that we can combine these two texts together if we remove that and only write integer you're going to notice we are going to get an exception that says we cannot do that and the same thing goes for the string number pretend we want to add the value of 10 to the string number the editor will be nice enough to tell you you shouldn't do that and we should just put some parentheses there and type in in ahead of that so we can convert it to an integer and add it easily next let's move on to math so we will create two variables one will be 10 and one will be five and to do math in python it's very self-explanatory as you can see to do addition you just need to add a plus symbol for the difference you just add a minus symbol to multiply you add an asterisk to divide you add a slash and if you want the exponential power just add two asterisks and you'll notice that if we print all of these you're going to get a very quick calculator so of course 10 plus 5 is 15 a minus b is going to be 5 and so on you will get the values that correspond to those operators okay next let's go ahead and create a logic statement so we're going to use these two values so is happy is false age is 28. let's pretend we want something to happen if the age is more than 21 we'll say print you are old and if they are not old we can also type in l if the age is equal to 18 we will tell them you are getting old and if it's neither of those we can go ahead and say you are still young and the same thing goes with the is happy statement so we type in if the person is happy we can write you are happy else they are probably not happy so we will just write you are not happy and when we play it it's going to say they are not happy because is happy is false and if we set this to true it's going to change to you are happy so the if statement just takes into account whether a statement is true or false and depending on that it's going to give the outcome of the values and the if else statement is going to probably be your most used logic next let's move on to the for loop so for i in range of three which means it's going to loop over three values we want to print hello and we can add a comma here and the value of i so if we print this you're going to notice it's going to say hello0 hello1 hello2 because in programming the index always starts at 0. so if you actually want that to start at one you're going to have to manually add one to i so now you get hello one hello to hello three and also just so you know what range is we can go down here and we can go ahead and type in print range of three so you're going to notice it's going to create a range object which is going to have the values from 0 to 3 so this can iterate three times over that but you should also know that this works for lists so we can create a name list and we can type in for name in name list we can print the name next we have a while loop so we're just gonna type in i equals zero and while i is less than five we're going to increment the value of i by one each time it goes in this loop and finally we want to print the value of i so what you should get are five values printed out just like this because as soon as this statement becomes false it's going to exit out of this loop and just terminate the program but a reason to use this is because you can create an infinite loop such as while true and that's never going to be false and inside here we're going to type in user input and that's going to equal an input that says enter something and if the user input is equal to the string of zero then we're going to print we are done here and we are also going to break and what break does is just cancel the loop which means if we type in zero this loop is no longer going to be valid and it's going to exit out of the program otherwise it's going to loop forever and if we actually play the program you're going to notice that finally we can add some inputs so we can type in 10 we can do two we can type in whatever that is but as soon as we type in zero it's going to exit the loop because it will reach this break statement and the program will finish moving on to functions to create a function all you have to do is use the def keyword and just name the function however you like of course i'm going to use the underscore naming convention and this function is only going to be used to say hello to the user so here we can go ahead and type in print hey there and we're going to insert the name so you might be wondering what are functions and what do we use functions for well functions are just used to reuse your code in many different instances because the main point of programming is to reuse your code as much as possible you want to maximize how often you can reuse the same line of code without having to rewrite it so for example now we created a function that says hello we can type in mario and we can also use it again and we can type in luigi and when we run the program it will say hey there mario and hey there luigi instead of writing this statement twice we were able to just use one function to do it multiple times and one thing we should cover also on functions is pretend you have a lot of functions you need to create such as get internet and run game something such as that and you don't really have the logic for it yet so you're going to notice that these are empty if you run the program you're going to get an error but python has created this keyword that is called pass which essentially just says there's nothing in here we accept that and your program is still going to be able to run regardless of having no content inside the function now when we tap on run you're going to notice there are going to be no errors because the program's going to understand that we provided the keyword pass which just tells it that there's nothing there and it accepts it but this is very good for creating the blueprints later in case you need to write everything your program should do but don't really know how to do that yet just insert the word pass and it will save you a lot of trouble and finally we're going to go over how we can use the try and accept block so for here we're going to create an input and it's going to say please provide a number so now we're going to go ahead and type in try and we're going to try to print the value plus the integer of the number that we get from the user and if that doesn't work we're going to print that that is not a valid number so the way it works is it tries to execute the code that's inside here and if anything goes wrong if an exception occurs it's going to go to the accept block and it's going to execute whatever is inside that of course if you write something that throws an exception inside here you need to try with another try and accept block but if we go ahead and run the program now we can write something like 10 and it's going to add 10 to the value but of course what if we write something such as mario try adding mario to 10 and the program is not going to be happy because you cannot convert mario to an integer and then add it to 10. so it's going to say you shouldn't do that but don't worry because we have the exception covered and our program will say that is not a valid number and not throw an exception but anyway i really hope that helped you to get started with python of course there's a lot more to cover this was just meant as a very quick guide for helping you to get started and with that being said have a wonderful day and i'll see you guys in the next video