Skip to content

Python multiline comment

in this tutorial we’ll be talking about howwe can include comments in our scriptfiles and a few more interesting andimportant things that we need to knowbefore we dive deep into thefundamentals of Python programminglanguage right so what I’m going to dois I’ll fire up my Python ideally thatis the GUI application that we’re goingto use and I just open the file that Icreated in the last tutorial and if youguys remember I named it firstunderscore script and this file just hasone executable statement at the momentand I just have a print function callingyou know within parentheses and doublequotes I have you know some text and ifwe run this file the way it is then wesee the text and I’ll just run it torefresh your memory so when I press thef5 button I see the text that’s theredouble quotes and that’s hello worldright now let’s say you want to add someexponent read text into this file thatyou don’t want the interpreter to youknow mess with like you know you don’twant the interval do anything with thattext or you know that information wejust want an information to be there sothat if anyone has a look at the codethat’s there in the file or if yourevisit your file let’s say six monthslater then you know you know what’s thepurpose of you know having that programthere right so to include a comment in ascript file in Python what you have todo is you have to start your commentwith the hash symbol or the pound symbolright so you type in that and then youcan type in anything that you want totype so I just type in this is a commentand if you think that your comment isgoing to span over multiple lines it’snot going to end in one line then at thebeginning of each new line you stillhave to type in the hash symbol right sothis is different from language like Cin which you can have a slash and anAsterix symbol at the beginning of yourcomment and it’s slash Astrix ember atthe end of your comment you can spanyour comment over as many lines as youwant you know you can’t do that inPython at the beginning of each new lineyou have to put in the hash symbol soyou know this is line two you know Imean you know these comments don’treally make much sense but then when youmake bigprograms and you know this is somethingthat I would suggest you guys to do youknow get in the habit of commenting yourcode as much as possible because lateron as I said when you visit your code itjust makes you no sense to see what yousee right so I hope you guys get thepoint save the file and when I run it Isee that I still get my messagehelloworld but then the comments havenot been displayed so Pythian reallyhasn’t done anything with the commentsis the university has just ignored thecomments right the next thing that I’mgoing to show you guys is I’m going tocreate a couple of string variables andyou know show you a few things that youneed to know about strings right so I’mgoing to talk about variables later onin the course in lot of detail and alsoabout strings later on but then in thistutorial I just want you guys know acouple of things so the way you create avariable in Python is you give it a nameso I’ll call my first string variablestrname Bowl and then since I’m creatinga string variable I’ll have to usedouble quotes and I’ll put in some textin my string so I’ll you know just typein my string and if you guys don’t knowwhat a variable is you can think of itfor the moment you know as a containerin the memory and you can put some stuffin it right so we’re just storing somecharacters in it at the at the momentand you can store like lots of differentkinds of things in variables in Pythonbut for this tutorial I just wanted towork with the string variable so that’swhy I’ve created one and when I pressthe Enter key I don’t see any message oranything but my variable has beencreated and I can confirm that by justtyping in the name of the variable atthe prompt so if I type in STR 1 andpress the Enter key I see that I get thetext that’s then variable and I also seea pair of double a pair of single quotesyou know one at the beginning and one atthe end and that’s to indicate that thisvariable is a string variable and I canalso use the print function to see thecontent of my variable so you know sinceI have already used quotations to createmy variable I don’t have to usequotations within the parentheses here Ican simply type in the name of myvariable which is STR 1 then when Ipress the Enter key I simply see thetext and I don’t see you know thequotations around thisbecause you know I’ve used a printfunction here right so what if you wantto have a new line between you knowseveral words that you have in yourstring so let’s say if I want a new lineinstead of a space character I’m on anew line between the words my and stringso if I create another variable STR totoward the assignment operator you knowgetting my first double quotation markand write the first word which is my andthen press the Enter key you know I’llget an error so that’s not allowed inPython but then there is a way around itif I want to have a new line betweenseveral words or sentences in my stringthen what I can do is I can use triplequotes instead of using the you knowdouble quotes or single quotes right soI’ll type in STR two as my variable nameI’ll put in the assignment operator andinstead of using you know the singlequote twice or double quote once I’llget in you know the double quote symbolthrice and now when I type in mine pressthe Enter key I see that I don’t get anerror message instead the cursor startsblinking on another line a new line andI can type in my stretch second wordstring and I’ll have to end my stringwith the same quotations sequence in thesame variation symbols that I use at thebeginning of the string right so if I’veused three double quotes and I have toput in three double quotes here and whenI press the Enter key now I get myprompt back and if I type in the name ofmy variable STR 2 and press the Enterkey I see that I get the string and Ialso see the newline escape sequencebetween the two words which is anindication of the fact that there is anewline character between the two wordsand if I use the print function to youknow see the text in the string I’lltype in STR 2 here and I press the Enterkey now I see that the string isformatted the way I created it and I seethe two words on two different lines thelast thing that I want to show in thistutorial is you know what you have to doto have two executable statements on thesame line in a script file in Python soI have just one executable statementwhich is the print statement here let mehave another one you know I’ll haveanother print function call rinsingdouble quotes I’ll like an somethingelse like what’s upand save the file and when I run aid Isee that I get the two strings on theoutput screen right now what if I wantedto have these two print statements onthe same line not the output statementsbut you know just the executablestatements what if I don’t want myprogram to be too lengthy in the senseyou know I don’t have to scroll down tosee the code so I want to have like tosee statements on one line so if I put abackspace and have these two you knowfunctions on the same line and executethe program I get an error messagePython tells me that that syntax isinvalid so what I can do to get aroundthis is I can have a semicolon betweenthe two print functions I’ll save thefile and now when I execute it I seethat I don’t get an error messageinstead you know the values aredisplayed these string values aredisplayed right so this is what you haveto do if you want to have two executablestatements on one line or you know evenmore than two you just have to separatethe statements with the semicoloncharacter and if you create a variablein Python and you restart your shellthen that variable is lost so let mecreate another variable STR 3 and I’llgive it you know a value this is astring and I press the Enter key tocreate it and you know if I type in thename of the variable and press the Enterkey I see that the text is displayed nowI’ll go to shell and I restart the shelland you can do this by also pressingctrl + f6 together and when I do thatthe shell is restarted and now when Itype STR 3 and press the Enter key I getan error message and Python tells methat the variable does not exist so thisis a very important thing and you shouldmake a note of it that if you havecreated variables and if you restartyour shell then those variables are lostso you know you should be careful aboutthis and that’s it for now in the nexttutorial we are going to talk more aboutvariables and you know we see how we canassign values to variables and you knowwe check out some more interestingfeatures so thank you so much forwatching this tutorial and you maysubscribe to my channel if you haven’talready and I see you soon take careEnglish (auto-generated)

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)