#2: Python print() function and variables | Python for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys i'm punit from programmers and welcome back to this series on python in this video we'll be learning about data types more specifically we will learn about textual data and numbers we will also learn how to store these data so that you can use them later in the program let's get started if you haven't watched the previous video from this series i highly recommend you watch that first click on the info button if you would like to watch the previous lesson on my screen i have the program from the last video i'll go ahead and run it first as you can see hello world is printed this program is using the print function to print whatever is in between the parentheses in this case hello world is a string a string is simply a sequence of characters either one or many and is always wrapped inside quotation marks now let me take this upper notch and print another string this time i'll use double quotes instead of single quotes so i'll say print oops print python 3 is awesome and this time when i press run then as you can see both hello world and python 3 is awesome are printed now you can use either single or double quotes for strings they're basically the same however you cannot do something like this so i'll change this to single quote and when i press run this time python shows me an error you cannot mix and match single and double quotes so i'll change this back and make this double quote when i press run then you can see the same output as before another data type that's heavily used in python is the numeric data and there are two commonly used numeric data in python integers and floats the difference between integer and float is that integer is a whole number and float is a decimal we can use the print function to print integers and floats as well let me give you an example quickly so i'll say i'll remove this old code and i'll say print 5 and i'll also write print 34.5 when you run the code the integer 5 and 34.5 and the floating point number 34.5 are printed one thing to remember is that if you use a number with quotations it's technically a string so if i surround five by quotes five here is a string and not a number i'll go back and remove this quotation marks for now till now we learned about strings and numeric data and how to print them in the real world scenarios we may need to store and use this data in our program and not just print them before moving to the next section of the video i'd like to mention that the program is team has created an app that allows you to learn python from your phone the app contains bit size lessons that are easy to understand a built-in interpreter so that you can run python on your phone quizzes and many more features the app is available on both ios and android the links are in the video description below in order to store data and use them later in our program we use something called variables before explaining more about variables let me give you an example so i'll remove the old code and i'll say city equals kathmandu here i've created the variable city and this variable stores a string data kathmandu to store the data in a variable we are using the equals operator now instead of printing the string directly i'll print the city variable and see what we'll get so i'll say print city and this time when i press run then kathmandu is printed to the screen again this is because the city variable stores the string data kathmandu now technically speaking the city variable does not hold the kathmandu string when we write city equals kathmandu city is actually referring to the string kathmandu now storing data and referring to the data may seem like similar concepts but they are very different in python we will learn more about what referring actually means in detail in the future videos for now we'll continue to say that city stores kathmandu for simplicity now let me modify this program and instead of city i'll print city inside quotation marks and see what we get so i'll say city and this time when i press run then instead of kathmandu the string city is printed if i replace the quotes again and press run then this time again kathmandu is printed we can also change the data of variable holds after we print the city let me change the city to new york and print it again so i'll say city equals new york and i'll say print city now this time when i press run then it prints both kathmandu and new york let's try to understand what's happening here on line 1 when i say city equals kathmandu then city holds the string kathmandu on line 2 when i say print city then since the current value of city is kathmandu that's what gets printed on line 4 i change the value that the variable city is holding to new york on line 5 when i print city then since the current value of city is new york that's what got printed on the screen as you can see we can change the value of a variable which is why they are called variables i hope that makes sense it is also possible to assign a value of one variable to another let me give you an example so i'll remove this code and i'll add another variable called destination underscore city equals new york now let me try something different i'll say city equals destination underscore city and when i print city then you must have guessed by now when i press run then it prints new york not kathmandu this is because the value of now city is now the value of destination city which was new york we can also assign numeric data to variables in a similar way we assign strings to variables let's take a look at it so i'll remove this code and i'll say my favorite number equals 5. now let me print this quickly so i'll say my favorite number i'll print that now let me create another variable called pi and i'll say pi equals 3.14 and let me print pi and now let me do the same thing i did before with strings so i'll change my favorite number to pi and then i'll print my favorite number now this time when i press run then i get 5 3.14 and 3.14 again this is because on line 2 the value of 5 my favorite number was 5 on line 5 the value of pi was 3.14 and on line 8 when i print my favorite number its value has been changed to the value of pi which is 3.14 if you need to print more than one variable and data in a single print statement we can separate them by commas let's take an example so i can say print city and i can put a comma here and i can say kathmandu here we are printing two strings city and kathmandu in a single statement one thing to notice here is that when we print objects separate by commas python adds a space in between them by default so when i press run then as you can see it prints city and kathmandu but you can also see that there's a space in between which was not there here and here we can also print variables and strings in a single statement i'll show you an example so i'll say city equals kathmandu and i can say something like print city and instead of string kathmandu i can do the variable city when i press run then i get the same output as before because the value of the variable city which was kathmandu is replaced in the print let's try one last example so i'll add another variable called kfc underscore locations the value of which is 3 and then i can say print city the variable city and then kfc locations comma kfc underscore locations here the city string the city variable the kfc location string and the kfc underscore locations variable are printed in one print statement i'll press run and as you can see all four are printed one by one and separated by space that means the comma operator can be used to separate not just 2 but any number of objects and python will print them all at this point we have covered all the basics of variables before ending this video let's talk about how to choose a good variable name if you have noticed in our programs we have used simple and descriptive variable names like city kfc underscore locations and destination underscore city we can give variables like c instead of city it works just fine however it is hard to understand what c means just by looking at the code so i can replace city by c here and here as well and i can run the program and i'll get the same output but here you can see for yourself that it is very confusing to know what c really means when we use good descriptive variable names it becomes easier to understand the code to make variable names descriptive we may need to use names having more than one word in that case you can separate the variable name by an underscore like destination underscore city in our program by the way there are some rules you need to know while creating a variable rule number one you cannot create a variable name with space in between like this you also cannot start a variable name with a number rule number three you cannot use certain words as a variable name you cannot use else as a variable name because else is a keyword these keywords have special meaning in python and are part of the python syntax we will learn more about these keywords as we progress through the course before we end this video here's a recap of what we learned we learned about three most common data types in python strings integers and floats a string is a textual data surrounded by codes integers and floats are numeric data integers are whole numbers and floats are decimals we also learned about variables variables allow us to store data so that we can use them later in the program it's possible to change the value a variable can hold and last but not the least we learned to give good descriptive names to variables that's it for this video i hope you learned something if you are just watching the video without writing any code i highly encourage you to try the programs in this video on your own the only way you can be a good programmer is by trying by the way you can find all the programs from this video on github i've provided the link in the description below feel free to copy the programs and edit them as you please and if you have any questions and feedback use the comment section below join me in this video series and let's explore the exciting world of programming together in the next video we will learn how to take data input from the user and how to print them if you like this video hit the like button now and also don't forget to subscribe to our channel and ring that bell icon so that you don't miss the next video thanks for watching and i'll see you in the next one happy programming
Info
Channel: Programiz
Views: 62,657
Rating: 4.973918 out of 5
Keywords: python, learn python, learn programming, programiz, python print, python variables
Id: i83VkP0LHPI
Channel Id: undefined
Length: 11min 48sec (708 seconds)
Published: Wed Aug 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.