Comments, Escape Sequences & Print Statement | Python Tutorial - Day #5
Summary
TLDRThis video script from the '100 Days Of Code' series focuses on Python's Comments, Escape Sequence Characters, and Print Statements. It explains how to use comments to add non-executable text for clarity or reminders. The script also covers escape sequences for including special characters like new lines and quotes in strings. Additionally, it explores the print statement's functionality, including how to print multiple values with customizable separators and end characters, and mentions the optional 'file' parameter for writing to files.
Takeaways
- đĄ Comments are used in code to explain functionality or to avoid executing certain parts of the code during testing.
- đ Escape Sequence Characters like '\n' and '\"' allow programmers to include characters in strings that would otherwise cause errors.
- đ Print statements in Python can print text enclosed in double quotes by using the escape character '\"'.
- đ« The error 'EOL while scanning string literal' occurs when a new line is introduced in a string without using an escape sequence.
- â The 'print' function can print multiple values separated by a specified separator, defaulting to a space.
- đ The 'end' parameter in 'print' allows customization of what is printed at the end of a statement, defaulting to a new line '\n'.
- đ Comments can be added to code using the '#' symbol for single-line comments or triple quotes for multi-line comments.
- đ ïž IDEs like Replit provide shortcuts like 'Ctrl + /' or 'Command + /' for commenting and uncommenting lines of code.
- đ The 'file' parameter in 'print' allows writing directly to a file object, with 'sys.stdout' as the default for console output.
- đ The video emphasizes the importance of understanding comments and escape sequences for effective Python programming.
Q & A
What is the purpose of comments in programming?
-Comments are used in programming to add text that the interpreter does not execute. They are helpful for explaining blocks of code, reminding the programmer or other developers about the purpose and functionality of the code, and for avoiding the execution of specific parts of code during testing.
How do you create a single-line comment in Python?
-In Python, a single-line comment is created by prefixing the comment text with a hash symbol (#). Anything following the hash symbol on the same line is ignored by the Python interpreter.
What is an Escape Sequence Character and why is it used?
-An Escape Sequence Character is a sequence introduced by a backslash (\) that represents a special character or instruction. It is used to include characters in a string that cannot be directly used, such as a new line (\n) or a double quote (\").
How do you print a new line in a string using an Escape Sequence Character?
-To print a new line in a string, you use the Escape Sequence Character '\n'. When this sequence is included in a string, Python will interpret it as a command to start a new line when the string is printed.
What is the default behavior of the 'end' parameter in the print statement?
-The default behavior of the 'end' parameter in the print statement is to print a new line (\n) at the end of the output. This means that by default, when a print statement is executed, the cursor moves to the next line.
How can you change the default separator in the print statement?
-You can change the default separator in the print statement by using the 'sep' parameter followed by the separator character you want to use. By default, the separator is a space.
What is the purpose of the 'file' parameter in the print statement?
-The 'file' parameter in the print statement specifies a file object to which the output should be directed. By default, it is set to 'sys.stdout', which means the output is directed to the standard output, typically the console.
How do you print multiple values in a single print statement?
-You can print multiple values in a single print statement by separating them with commas. The 'sep' parameter can be used to specify the separator between these values.
What is the difference between 'sep' and 'end' parameters in the print statement?
-The 'sep' parameter specifies the separator between multiple values in a single print statement, while the 'end' parameter specifies what to print at the end of the print statement. The default separator is a space, and the default end character is a new line (\n).
How can you prevent the automatic new line after a print statement?
-You can prevent the automatic new line after a print statement by setting the 'end' parameter to an empty string ('end=""'). This will cause the next print statement to start on the same line.
What is the significance of the shortcut (Ctrl + /) mentioned in the script?
-The shortcut (Ctrl + /) is used to quickly comment or uncomment lines of code in many Integrated Development Environments (IDEs). This is a time-saving feature that allows developers to toggle comments on or off for selected lines of code.
Outlines
đ» Introduction to Comments and Print Statements
The script begins with a discussion on the importance of comments in programming, particularly in Python. Comments are lines of code that the interpreter ignores, allowing programmers to leave notes or explanations within the code without affecting its execution. The video introduces the concept of escape sequence characters, which are used to include special characters within strings that would otherwise cause errors, such as a new line. The presenter demonstrates how to use the print statement to output text to the console, and how to use escape sequences to include new lines within the printed text. The example of a syntax error due to an unclosed string literal is also discussed, along with the solution of using the escape sequence ' ' to indicate a new line.
đ Understanding Comments and Syntax Highlighting
This section delves deeper into the concept of comments, explaining how they serve as a reminder or explanation within the code. The presenter illustrates how to write comments in Python using the pound (#) symbol and how to use the IDE's functionality to comment or uncomment multiple lines of code quickly using the shortcut Ctrl + /. The script also mentions the visual aid of syntax highlighting in IDEs, which helps programmers distinguish different elements of the code, such as comments, strings, and errors, by displaying them in different colors.
đ Exploring Multi-line Comments and Escape Sequences
The script continues with a discussion on multi-line comments, which allow programmers to comment out multiple lines of code at once. It explains how to use triple single-quotes (''' ''') or triple double-quotes (""" """) to create multi-line comments. The presenter also touches upon the use of escape sequences to include characters like double-quotes or single-quotes within strings that would otherwise cause syntax errors. The concept of an escape sequence character, which is a backslash followed by the character to be inserted, is clarified with examples.
đšïž Advanced Print Statement Features
In this part of the script, the presenter explores advanced features of the print statement. It covers how to print multiple values at once and how to use the 'sep' parameter to define a custom separator between values. The 'end' parameter is introduced, which allows the programmer to specify what should be printed at the end of the print statement, overriding the default behavior of adding a new line. The script also mentions the optional 'file' parameter, which can be used to write the output to a file instead of the console, and notes that parameters 2 and 4 ('sep' and 'end') are optional and can be omitted if not needed.
Mindmap
Keywords
đĄComments
đĄEscape Sequence Character
đĄPrint Statement
đĄSyntax Highlighting
đĄEOL while scanning string literal
đĄIDE
đĄSyntax Error
đĄMulti-line Comments
đĄSeparator
đĄEnd
đĄFile
Highlights
Introduction to Comments, Escape Sequence Character & Print Statement in Python
Explanation of why comments are necessary in code
How to use comments to explain code to future self or colleagues
Demonstration of the print statement and its output
Error explanation: EOL while scanning string literal
Introduction to Escape Sequence Characters
How to use the newline Escape Sequence Character (back-slash)n
Definition and explanation of comments in programming
How to use comments to leave notes within the codebase
Error handling when attempting to write comments without proper syntax
How to use the comment function in Python
Syntax highlighting in IDEs and its benefits
Shortcut (Ctrl + /) for commenting and uncommenting in code
Explanation of single-line and multi-line comments
How to use triple single-quotes for multi-line comments
Demonstration of the print statement with multiple values
Introduction to the 'sep' parameter in the print statement
Introduction to the 'end' parameter in the print statement
How to use the 'file' parameter in the print statement
Clarification that parameters 2 and 4 in the print statement are optional
Invitation to bookmark the playlist for future videos in the series
Transcripts
Welcome back to the 100 Days Of Code series
We'll have a close look on Comments, Escape Sequence Character & Print Statement
Whenever we code in Python or any other programming language
Sometimes we want to have text in the program that does not execute.
Whatever we write in our program, Python interpreter tries to execute it
But sometimes we would like to write some lines in our program which should not be executed.
For eg. If I'm writing a program
So it is possible that after 6 months I myself forget what was that program for
So in this situation it would be natural for me to write few lines in my program
I'd like to add some lines in it to tell future Harry what program I had written.
Let's suppose I'm working with a friend of mine Shivam and the other friend Rohan
So I have to tell both of them what am I doing in this program
And how this program is functioning
And for this I'll use 'Comments'
And we'll also learn Escape Sequence Character & Print Statement in this video
Let's move to the computer screen And let's get started
[STARTING THEME]
So what I'll do is... I'm going to open my "main.py" file here
So let me open the 'Files' and as always I'll open "main.py" here
And here you'll be able to see...
...The output of "main.py"
So if I write here "print("Hey I am a good boy")
So there's no doubt that I'm a good boy
So "Hey I am a good" will be printed
In fact in print statement whatever you add in double-quotes(" ") will be printed
So "Hey I am a good boy" has been printed
And look if I write here "Hey I am a good boy"
And as I pressed 'Enter' here
And let me write "and this viewer is also a good boy/girl"
Now if I run this, so will the red line be displayed?
We are skating on thin ice, What's this error "EOL while scanning string Literal"
Let me tell you what does it mean.
Basically Python is saying "You know what,
You wrote 'print' here and started with double-quotes(" ")
But when you added a new line by pressing 'Enter'. I didn't like it"
So you'll ask Python what to do "I need a new line after the word 'boy' "
Python will say "OK! If you need a new line...
...So you must use Escape Sequence Character
And you've to use " (back-slash)n " here like this
And you don't literally need to add this new line
Just write " (back-slash)n " and Python will take care of it
So you'll say OK, I wrote "I am a good boy (back-slash)n "
And when you'll run this code
So look " I am a good boy"
And then "this viewer is also a good boy/girl" appeared in a new line
So what was this?
What was " (back-slash)n "?
It was an Escape Sequence Character
I'll talk about this today
And I'll also tell you how many different kind of Escape Sequence Character are there
And what are the different Escape Sequence Character
But first of all I wanna tell you what are comments
Let's suppose I'm working on a code base
Along with you I'm creating "codewithharry.com"
And together we are working on that with a tough grind
Now let's suppose I want to tell you that the Home Page of 'codewithharry.com'
That home page will have a new design and I'll release that after 2 days
And I want to add a comment here "Please don't change this design or remove this line from here"
I want to write here...
"Hey Harry please don't remove this line"
Let's suppose I want to add this
"I'm working on it"
So I just want to keep this in code base for a reminder
But can you see the the Python is in a black mood
His Highness is saying "What's this. From where did this Invalid Syntax came, I know 'Print' and other syntaxes"
But Python is saying "Hey Harry, Please don't remove this line" I didnât like it
So when we'll run this obviously we'll see an error in it
There's no doubt. And you might also know what error is this
But I still want to keep this line
I'd say "Please" to Python and in reply it'll say "OK! Stop crying like a baby"
"OK! Stop crying like a baby. Let me tell what to do"
Pythons says "I've a comment function in me"
Comments means "You can add whatever text you want and I don't have any business with that"
You'll say "WOW! Python you're so great"
And Python will say "Yes! For sure"
What to do now?
Python says "Just add a character like this and this text will become a comment"
What will it become? A comment
And if you mark a line as a comment...
Look, Replit is doing syntax highlighting first, for you
When you add this character in any IDE so it'll be syntax highlighting
In this case this comment started to be displayed in green color
And look this syntax highlighting in being continued
I wrote print, it is displayed in color 'yellow'
And String literal are displayed in orangish color
And these brackets are also visible to me in a different color
And 'error' is being displayed in 'Red' Color so this is called syntax highlighting
Which any IDE does for you
"Hey Harry, Please donât remove this line"
Let's try to run this
And this time our program worked
Which means I can add some text in my program or I can also add some information
For eg. I want to mark that the author of this program is 'Harry'
And let's suppose I want to mark that this course is the 100 Days Of Code
Let's suppose I want to write this so I can do this
Now let me spill the beans
And what that secret is? Actually it is not a secret anymore because I've told you
You can select multiple lines
With ( Ctrl + / ) Or (Command + / ) if you're using Mac
When you'll press these keys
You can comment or uncomment to any code base
So if you ever see me commenting some lines faster, so don't ask me how I did that
I would have done that with ( Ctrl + / )
So you can use ( Ctrl + / ) for commenting & uncommenting...
...Single or multiple lines
Man what happened to Siri! Siri I'm not talking to you
So I was telling you that you can mark these multiple lines....
As comments or you can also uncomment them with the same keys
OK! That's great
So this is what I've written here "A comment is a part of the coding file that the programmer does not want to execute
Rather the programmer uses it to either explain a block of code or to avoid the execution of a specific part of code while testing."
Are you understanding this? This means
Let's suppose that if I write a comment here
Or let's suppose I write a code here
Suppose I want to write "print("hello world")" here
And now I want to show this program to my boss
And I ask him how the code is
So he'll say that this is great but...
I don't like this. So please remove "Hello World"
I'll say OK
So I'll select it and I'll press ( Ctrl + / )
And now I can remove this code
And now the boss of my boss will ask him "Why have you removed "Hello World" "
So I can uncomment it
And it'll work again
So this is how you can do comment or uncomment
So I've written the same thing here with example You don't even need to write the code
Just copy this from here, paste it in your computer and run this
And when you'll do this so look
You won't see the error "this is the single line comment" and you'll be able to see "this is the print statement"
Now I've given an example here you can also write a comment after a line of code
For eg. If you've written something like this
I'm pasting it....
And I'll tweak it like this
Look I've written "print"
And then what I can do is... actually I deleted " a "
I should have added this bracket
Now look as I'll write "print" here
And then I'll write "Hello World"
And then I can write any comment if I want to
Which means my code will be executed
And as it'll get the pound(#) symbols It'll say "OK I understood, I won't execute this text"
"I'll ignore it"
So Python interpreter will ignore " print("Hello world") "
What should have happened if this symbol was not added?
Obviously the error would have occurred You already know that
What type error that should be? A syntax error
It'll ask what's going on. What is this "Printing Hello World"
"Print hello world" this was alright His Highness Python will say
It'll say "What have you added this "Printing hello world" I didn't like it
So you can see as I'll add the pound(#) symbol here
And then if I run so it'll work
As you can see it works
Now look what I can do is.. I can comment this code out I already have told you this
But I want to tell you one more thing
That how you can add multi-line comments
Now let me tell you the story of multi-line comments
Let's suppose someone has said to me...
That...
For now let's take it's example let me uncomment it
Suppose someone ask me to uncomment these 3 lines
I have to 2 ways to do that
Now because I'm living in a modern world
I have IDE I have Replit I have modern IDEs
So I'll select these lines & I'll press ( Ctrl + / )
This is the shortcut provided by Replit & VScode and other modern IDEs
Which helps you to do Commenting & Uncommenting easily
But if I'm unable to do this so what would have I do?
So for my convenience I'd have added triple single-quote(''' ''') and whatever I should have written in it
I'm using ( Alt + â ) to move the line you should also learn this shortcut
In Replit all the shortcuts of VScode works so there's no need for you to be worries
And if you want to learn the shortcut of VScode so you can do the Google search
I also have uploaded a video regarding VScode you watch that too
But for now let's just focus on Python
So if I enclose anything in Triple single-quote(''' ''')
So it'll become a multi-line comment
"But the comment we do earlier wasn't the multi-line comment? "
Obviously it was a type of multi-line comment
So that's what I've written here "that you can use pound(#) symbol for multi-line comment"
But this method in which we use Triple single-quote(''' ''')
Or you can also use Triple double-quote(""" """) Let me tell you this is also valid
Program with Triple single-quote(''' ''') worked
But program with Triple double-quote(""" """) will also work. I've added Triple double-quote(""" """)
OK I'll do one thing I'll press ( shift + end ) key and then ( Ctrl + X ) and then ( Ctrl + V )..
...After selecting it
Now if I run this...
Even then it'll work
Can you see, It worked?
Quite great! Man!
So this is how we can comment & uncomment any line
So I can use this shortcut ( Ctrl + / ) or in Mac ( Command + /)...
...For commenting & uncommenting of line/s of code
So I hope you've understood this
Now let's talk about Escape Sequence Character
If you want to insert characters in a string
For eg. Here "Hey I am a good boy". And let's suppose I want to insert a new line character here
So how will I do that?
For now I'll comment it out
So that you'll only see the output of this print statement
You understood why I did this, don't you?
"Hey I am a good boy and this viewer is also a good boy/girl"
Now look the tutorial that I've written here What have I written in it?
I've written " To insert characters that cannot directly be used in a string, we use an escape sequence character."
Essence of everything
"A character that cannot directly be used in string, so we use Escape Sequence Character"
For eg.
If I press enter and literally add a new line character so an error will occur
Because Python won't be able to parse it
If you literally add a new line in a string so it won't be able to parse it
That's why we add new line character
Now '\' + 'n' they may seem different character
But they are one
This is called Escape Sequence Character
'(back-slash)n' is a new line character an Escape Sequence Character
Whenever you add this in a string so you'll be able to see a new line like this
So if I run it now so "Hey I am a good" and the next sentence will appear in new line
What about if I want to enclose "Hey I am a good boy" in double-quotes(" ")
This should work, what's the problem in this?
You'll think that and if you do so, Can you see this red line
Replit already has given the sign for danger It rang the bell for danger
So IDE always ring the bell for danger and ask you "don't you dare to run this code"
And if you dare so this will happen
You'll see the error "Invalid Syntax"
You'll ask "What's the problem in this? "
The problem in this is "when you started the print statement and then you started your string with double-quotes(" ") "
So confusion has been created here that this string is being started from here, but is it ending here?
You know this, That it is not ending here
But here
But our Python interpreter get's confused
It says that you've added double-quotes(" ") here and then here, so it must be ending here
OK! It is ending here Then what it is?
This is an Invalid Syntax for Python
Because if this double-quotes(" ") is creating a string from here till here
Then What this "good boy" is
So you'll see an error here
So to avoid this we don't use double-quote character
We use ' \" ' which is a double-quote Escape Sequence Character
This one is a character
This one is a character
So now if I run this so it'll only display double-quote(" ")
"Hey I'm good boy" is in double-quote(" ")
Did you see " \ " No.
But I've used " \ "
Why?
Because I want to use ' \" '
Which is a single character
They may seem 2 but it is 1 character
' \" ' which produce a single Double-quote(") in output
Which is an Escape Sequence Character
" An escape sequence character is a backslash \ followed by the character you want to insert. "
"An example of a character that cannot be directly used in a string" I've written here
If your string of print function is in a...
... single-quote(' ')
You cannot even use single-quote(' ')
Do you get it?
We'll dive deep in this in chapter 'Strings'
But if I've inserted a single quote in single-quoted string So I cannot do that
I've to add " \' "
Look the error has occurred
But if I add " \' " so single-quote will appear
A single-quote has appeared after "Hey I am"
So this is how I can add single-quotes in strings
So this was the Escape Sequence Character I hope you all have got it
Now I want to spill some more beans about print statement
The first thing I want to tell you is that...
...You can also add multiple values in print statement
Just like I can add "Hey" "6" & "7" all three of these
And when I'll run this so all three of them will be printed
"Hey" "6" & "7" as you all can see in your computer screen
And now its other parameter is called separator
Now let's suppose If I add "sep = Tilde(~)" character
And now if I run it
So you'll be able see, when I add "sep = (~)" so this line will be separated by (~) character
As you can see a Tilde(~) character has been appeared after "Hey"
And then another Tilde(~) character
So if you ever want to join any 3 words so you can do this just as I did
So you can do this...
And after this there's another parameter "end="
Let's suppose if I write "009"....
...In place of "end="
So what does it mean?
It means if I write next 'print' statement
And in that statement suppose I write "print("Harry")
And if I run it...
So look "Hey" "6" "7" will be printed
And then "009" will be printed before the next print statement
Ideally we use "end="....
Let's suppose if we write "009(back-slash)n"
We usually use "(back-slash)n" and we can also add "end=null"
By default the function is "(back-slash)n"
We also can add "end=null"
You should know this too
Now I've added "009(back-slash)n" here
So whenever I run the next print statement so "009(back-slash)n" will be appended in this print statement
What's the difference between "sep" & "end"?
"sep" means that how the multiple values of a single print statement will be separated
But the default separator is 'Space' as I've written here
Now what does "end" means? It means 'specify what to print at end'
"What to print when this print statement will end"
You can write anything here and it'll be printed
'By-default' is a new line
If you want to over-write this behaviour that when you enter a new print so a new line won't be added
Look as I ran this print statement
A new line after this, has been printed automatically
And a new line will be printed after this too if I haven't added "end=009(back-slash)n"
But because I've added "end=009(back-slash)n"
That's why this line will be printed after this print statement
And "sep" means "separator"
"What should come between multiple values in a single print statement"
Great!
Now there's another argument name "file"
And keep this in mind
And I've written this here because I want you to know that you can write on 'file' using print statement
"An object with a write method. Default is sys.stdout"
Which means it'll write on 'stdout' wherever you're running it'll be shown on console
We'll talk about the 4th argument later I'll discuss this in the upcoming videos
As we'll progress in this course
Now I've written another thing here "Parameters 2 and 4 are optional"
2nd & 4th parameter are optional
Which means you don't need to add 'separator' or 'end'
And there's no need for you to write 'file' without these your statement will work
As we have already seen we haven't used it in beginning
Neither we used "end" nor we added multiple values
You can also add single values
But if you add multiple values even then it'll work
I hope you're enjoying it and if you haven't accessed this code so make sure to give it a hit
And also bookmark this playlist because all videos are going to be add there
Bookmark this playlist by clicking here
And click here to save this play-list
That's it for this video guys
Thank you so much guys for watching this video:)
And I'll see you next time
Voir Plus de Vidéos Connexes
5.0 / 5 (0 votes)