Transcript for:
Python Crash Course

hi there and welcome to my python crash course on the new boston channel in this tutorial we'll be looking at the different types of syntax assigning variables looking at the different data types using numbers strings different boolean values operators different kinds of loops functions classes and even objects now for the stuff that i've just said made no sense to you then i challenge you to watch till the end of the tutorial and i assure you that by the end you'll have a great understanding of all of these topics [Music] also as you can probably imagine i put a lot of work and effort into making this tutorial it mean the world to me if you go ahead and subscribe to my channel it should be the first link in the description thanks so much for that and let's get right into it so let's start off with looking at comments now comments can be used to explain python code and they can also be used to make code more readable now comment always starts with a hash and python will ignore them whenever you try to run the script comments can be placed at either the tsar or the end of the line a comment does not have to be text to explain the code it can also be used to prevent python from just executing that bit of code now maybe you don't just want one line for a comment and you want multiple so instead of using this you could use triple quotes in your code as long as the string is not assigned to a variable python will read the code but then ignore it let's now have a look at how you can create a variable a variable is created the moment that you first assign a value to it for example here i'm going to say x equals to 10. so x is going to be the variable and 10 is going to be the value now if you go ahead and print out x it should respond with 10. now as i said earlier we can use variables to assign any type of data whether that be a number or even a word so let's go ahead and make a new variable and we're going to assign it to the string coding now if you go ahead and print out y this is the result that we get now that you know how to create a variable let's have a look at how you can change the type even after the type has been set now changing a type is as simple as making something from an integer to string or string to an integer if you want to specify the data type of a variable this can be done with casting so if you go ahead and say x equals a string and in brackets 10 then x will be 10 but in quotations and hence it will print out as a string now we can also go ahead and say y equals to the integer 3. now y will be 3 without any quotation in addition to that we can also say the z equals to flow in practice three all this does is it adds a decimal place to the end of three and hence the result three point zero let's now have a look at single or double quotes string variables can be declared either by using a single quote or a double let's have a look at what the difference is so if you go ahead and say x equals coding in double quotes and then we also go ahead and say y equals the coding in single quotations the output that we get here is actually going to be the exact same another key thing to take into consideration is the variable names are case sensitive for this one we're going to have a capital a and we're going to assign this value to python and now we're going to have a small a right below it and we're going to assign the value to variables let's now go ahead and print both of these out to see what we get as you can see here we get two different results this is because the capital a will not override the other let's now go ahead and look at data types now there are a lot of different types of built-in data types in programming data type is a very important concept variables can sort data of different types hence data types some of the data types are built into python are scr or string for text int or integer and flow for numeric types list tuple and range for sequence types as well as bool for brain types we'll be having a look at most of these in a bit more detail in just a second let's have a look at how you can get the data type so if you don't remember getting a data type can be done by using the type function here i'm going to go ahead and say x equals five i'm gonna go ahead and print out type x as you can see it tells us that x is an integer let's now have a look at numbers so there are two main numeric types in python int or integer and float variables of numeric types are created when you first assign a value to variables of numeric types are created when you assign a value to them now let's go ahead and say x equals 1 and y equals to 2.8 if you go ahead and print out the type for both x and y it tells us that x is an integer and y is a float int or integer is basically a whole number it can be positive or negative but it cannot have any decimals float on the other hand is a floating point number and it can be either positive or negative but it has to contain either one or more decimals let's now have a look at strings now strings in python are surrounded by angle quotations or double quotation marks hello in single quotation is the same as hello in double and you can display a string literal with the print function you can also assign a multi-aligned string to a variable by using three quotes as i showed you earlier we can also use three codes for making multi-line comments however when it's connected to a variable it's a value let's go ahead and just say a equals triple quote and some sample text in multi-lines as you can see when we print this out it prints out more than one line now again it doesn't matter if you use three single quotes or three double quotes they both still do output the same result like many other programming languages strings in python are arrays of bytes representing unicode characters however python doesn't actually have a character data type it's simply a string with a length of one square brackets can be used to access the elements of a string so we're going to go ahead and say a equals to hello world and we're going to go ahead and print out a then after the a we're gonna add in square brackets and say one in the middle now before i print this out try and have a go yourself to see what you get as you can see here it prints out the e that's because the first character actually has a position of zero and not one so if we did the same thing with zero instead of one then it would print out h instead you can also get the length of a string by using the len function to try this out we're gonna carry on with our example with a being hello world what we're gonna do is go ahead and say print then and then a in the middle this should go ahead and print out the length of the string in this case that's going to be 13. in let's now have a look at booleans in python number lines represent one or two values true or false in programming you often need to know if an expression is true or false now evaluating any expression in python will get you one of these two answers let's go ahead and compare two values python will return the boolean answer so let's go ahead and say print 10 is greater than 9 10 equals to 9 and 10 is smaller than 9 and the result that we get should be in this order 2 false and false because 10 is greater than 9 however the next two after that are false when you try and run a condition in an if statement python will again return either true or false we're going to go ahead and say a equals to 100 and b equals to 50. let's also say that if b is greater than a then print b is greater than a else print out b is not greater than a now if you don't really know how if or else statements work don't worry because we'll get onto that in just a second now the ball function actually allows you to evaluate any value it also gives you true or false in return for example let's go ahead and say print bull hello and we'll also say print bool 15. as you can see we got two outputs and they're both true because they're both valid statements now almost any value is evaluated to true it has some sort of content any string is true except for empty strings and any number is true except for zero any list tuple or even sets or all through except for the ones that have nothing in them so again we're going to go ahead and say bool and then we're going to say abc bool123 and bull a list of apple cherry and banana now we haven't really looked the list yet but we'll be looking at that in a second anyways the output here should all be true even though a lot of the values are true some are still false in fact there's really not that many values they value to false except for empty values the number zero and even the value of none and of course the value false also evaluates to false to have a look at all of this let's say ball false ball none bull zero ball quotations with nothing inside will brackets when nothing inside both square brackets but nothing inside and bull curly brackets with nothing died and yes you guessed it they're all false now functions can also return a boolean value but we're not going to dive into it right now just because we haven't done functions yet let's now have a look at python operators now operators are usable for operations on variables and values for example let's use the plus operator to add together two values we said print 10 plus 10. now addition isn't the only operator that you can use you could also have subtraction which is just minus multiplication which is an asterisk modulus which is going to be a percentage symbol python also has a lot of assignment operators how these work is x plus equal to 3 is going to be the same as x equals to x plus 3. x minus equals to 3 is the same as x equals x minus 3 and so on and so forth there's also comparison operators which give us boolean values as we said earlier these include equal 2 which is gonna be two equal signs next to each other greater than or equal to and even less than or equal to greater than or equal to which is going to be a greater than time with an equal to sign right next to it and less than or equal to which also follows the same principle as greater than let's now go ahead and have a look at python lists now lists are used to store multiple items in just a single variable list of one of the four built-in data types in python they're used to store collections of data the other three are tuples sets and dictionaries all of these have different qualities and usages list equates using square brackets let's go ahead and say list equals to square brackets apple banana oranges in the next line we can go ahead and print out our list and it gives us a pre-expected output now list items are ordered they can be changed and they also allow duplicate values items in list are also indexed the first item has an index of zero and the second item has an index of one when we say that the lists are in order it means that the items will have to have a defined order and that order will not change unless if you do it manually if you add new items to the list then your items will be placed at the end of the list since the lists are indexed you get multiple items with the same values we can go back to our list and we're going to add in apple and orange again at the end and when we print this out we get five different values to check how many items a list has use the lend function this is really helpful to help you print the number of the items in the list out if you go back to our list on the top we can go ahead and say print then and then list as you can see here prints out five now inside list you can have any data type whether that be a string an integer or even a boolean value let's now have a look at tuples now tuples are used to store multiple items in just a single variable and once again this is one of the four built-in data types that's used to store collections of data a tubal is a collection which is ordered however it's unchangeable which is why it differs from list and tubals are also written with round brackets instead of square let's go ahead and say tuple equals to again apple banana and orange when go ahead and print this out we get the expected output now as you know tuple items are ordered unchangeable but they do still allow duplicate values and again they are indexed so the first item has the index of zero the second is one the third is two and so on to work out the tuple length we can again use the link function and for this example we'll get three you can also create a tuple with only one item to do this you can go ahead and say this tuple equals apple but then we need a comma at the end otherwise python will not recognize it as a tuple and again just as list we can have any data type that we want let's now have a look at sets this is a third built in data type that can be used to store collections of data the sets are again used to store multiple items in just a single variable however a set is a collection which is unordered as well as on indexed and sets are written with curly brackets let's go ahead and say set equals to apple banana and orange now if you go ahead and print this out the result will be unordered this is because that's just don't have a defined order in addition to that they also can't have two items with the same value duplicate values here will be ignored so we go ahead and change our set variable to add an example at the end as you can see it simply just ignores it however again if you want to get the length of the set you can again use the len command and just like list and tuples you can have any data type now let's have a look at dictionaries this is the last function for drawing sets of data dictionaries are used to store data values and key to value pairs a dictionary is a collection which is ordered from python 3.7 if you're using a version from 3.6 earlier the dictionaries are on order but they are still changeable and they still don't allow any duplicates dryer dictionary you have to use curly brackets have keys they also lead into their values let's go ahead and create our first dictionary so we're going to say dictionary equals to brand it's going to be forward the model is going to be focused and the year is going to be 2010. when you go ahead and print this out it prints out exactly what we wrote but in just a single line now the items in this dictionary are ordered from python 3.7 changeable and they also do not allow any duplicates and as i said earlier dictionary item is also presented in key to value pairs and can be referred to by using the key name to print the brand value of the dictionary we go ahead and say print dictionary and in square brackets we have to give the exact name of the key which is going to be brown as you can see here it prints out forward we can also do this for the model as well as the year now just like says dictionaries also don't allow any duplicates so if we go ahead and try and say yeah it's 2021 then simply going to ignore the value have a look at the dictionary length again we can use the link function and for this example here we're going to get 3. let's now have a look at some conditional statements so we're going to go ahead and again say a equals to 100 and b equals to 200. now if b is greater than a then print out b is greater than a else printout a is greater than b now if you go ahead and run this it tells us that b is greater than a now python relies a lot of indentation now indentation is a white space in the beginning of the line so for example we get rid of the indentation in front of print we get an indentation error now in addition to this we can also use the alif keyword the lf keyword is python's way of saving if the previous conditions were not true then try this one so let's go ahead and change our value of b to 100 as well and we'll say l if a equals b then print a and b are both equal in this example a is equal to b so the first condition isn't true and hence python goes to the alif statement which is true and hence we get the result a and b are equal let's now have a look in while loops now python has two primitive loop commands one loops and four loops with a while loop we can execute a set of statements for as long a condition is true we're going to go ahead and say i equals 1 and we're going to say while y equals 1 print i as you can see this is going to go on forever because i is always going to be 1. let's now go ahead and change this loop up a bit and we're going to say that while i is less than six print i and also i plus equals to one if you don't remember what this does i equals i plus one when we run this we get one two three four and five before the loop stops we can also add in a break statement so we can go ahead and say if i equals to five then break as you can see here we only just go up to four because the loop breaks as soon as i is equal to five now opposite is a break statement it's a continue one so we're going to say if i equals to five then just continue we again get up to five we can also use the else statement with while so if we go ahead and get rid of the if i equals five statement then we can change that to say else print i is no longer less than six so still going to count all the way up to five let's have a look at some for loops now a for loop is used for iterating over a sequence this can either be a list a tuple a dictionary a set or even a string with the follow we can execute a set of statements one for each item in the list tuple dictionary or even a set so let's go ahead and make our first list we're going to say fruits equals apple banana and orange then let's say for x in fruits we're gonna go ahead and print out all the different foods in a different line even strings are iterable objects they all contain a sequence of characters to loop the letters through the word banana we go ahead and say for x in banana in quotations print x what it says is just prints banana but with all of his characters separated in six different lines we can also use break statements in for loops so if we go back to our statement in the first place we can say for x in fruits blueprint our x however if x equals the banana then break so what this is going to do is just going to print out apple banana and then the code is going to break if you move the if x equals banana pop above print x then it's only going to print out apple and then break now again just like the while loops we can also use the continue statement with for loops we can also use else in for loop so let's go ahead and say for x in fruits print x l sprint i'm done and as you can see it's gonna print out all the fruits in a different line and then it's going to say i'm done now follow-ups cannot actually be empty however if you put in a pass statement you can avoid getting an error now this can be used if you don't really have anything to put into the loop right now however you know that you're going to need it for later let's have a look now it functions a function is a block of code which only just runs when it's called you can pass data known as parameters into a function a function can then return data as a result now creating a function is really easy in python it's defined by using the def keyword we're going to go ahead and define function and inside the function we're going to say print hello i'm a function let's call the function we have to use the function name followed by parentheses so if we go ahead and say function and then the parentheses then it prints out hello i'm a function information can be passed into functions as arguments arguments are specified after this function name inside the parentheses you can add as many arguments as you want just try and separate them with a comma let's go ahead and make a new function we're going to define this function and call it name we're going to pass in the argument of name and when the function is called we're going to print out name now to actually give the function what it needs to print out we have to call the function and in the parentheses we have to put in the argument so here i'm going to call in the function and put in my name as you can see for the output it prints on my name now when you are calling function you need to call it with the correct number of arguments no more no less or else you will get a type error now python is an object oriented programming language almost everything in python is an object with its own properties and methods a class is like a blueprint for creating objects to create a class we use the keyword class let's go ahead and make our own class so let's say class my class and in there we're going to put in the variable x and the value of that is going to be 5. now let's go ahead and create an object we're going to say new equals my class and remember the parentheses let's go ahead and now print out new dot x and this shouldn't print out the value of the property that's named x but it's inside the class so that's how you can make a really basic class in python but now you can go a lot more advanced in that however for the sake of this tutorial i won't be doing that as they can get quite advanced so that's gonna be it for this video thank you so much for watching if you did enjoy don't forget to subscribe to my channel as well as the new boston's channel and also comment down below if you'd like to see more longer and comprehensive tutorials on python again thanks so much for watching i'll see you in the next one you