Comments, Escape Sequences & Print Statement | Python Tutorial - Day #5

Comments, Escape Sequences & Print Statement | Python Tutorial - Day #5

Understanding Comments, Escape Sequences, and Print Statements in Python

Introduction to Comments

  • The video begins with an overview of the importance of comments in programming, particularly in Python. Comments are used to include text that does not execute but serves as documentation for future reference.
  • The speaker emphasizes the need for comments by sharing a personal anecdote about forgetting the purpose of code after several months. This highlights how comments can aid understanding when revisiting code later.

Using Print Statements

  • The tutorial transitions to practical coding by opening a "main.py" file and demonstrating how print statements work in Python. For example, print("Hey I am a good boy") outputs the string within double quotes.
  • An error occurs when attempting to add a new line without using proper syntax, leading to an "EOL while scanning string literal" error message. This illustrates common pitfalls beginners may encounter.

Escape Sequence Characters

  • To resolve the previous error, the speaker introduces escape sequence characters, specifically n, which allows for creating new lines within strings without causing errors.
  • By using n, users can format their output more effectively; for instance, it separates two sentences into different lines when printed.

Importance of Comments in Code

  • The discussion shifts back to comments as essential tools for collaboration and communication among developers working on shared projects.
  • A specific example is given where a comment might indicate design changes or reminders not to alter certain parts of the codebase.

Syntax Highlighting and Commenting Techniques

  • The speaker explains that adding comments incorrectly leads to syntax errors; however, Python has built-in functionality that allows programmers to write comments without execution issues.
  • By prefixing lines with a specific character (e.g., #), those lines become comments and are ignored during execution. This feature enhances readability through syntax highlighting in IDEs.

Efficient Commenting Practices

  • Users can quickly comment or uncomment multiple lines using keyboard shortcuts (Ctrl + / on Windows or Command + / on Mac), streamlining workflow during coding sessions.

Understanding Comments and Escape Sequences in Python

The Purpose of Comments

  • Comments are used by programmers to explain blocks of code or to prevent specific parts from executing during testing.
  • For example, a programmer can comment out a line like print("hello world") to remove it temporarily without deleting the code.

Commenting and Uncommenting Code

  • Programmers can easily comment or uncomment lines using shortcuts (e.g., Ctrl + /), allowing for quick modifications without losing code.
  • If a pound symbol (#) is added before text, the Python interpreter ignores that text, preventing syntax errors that would occur if left uncommented.

Multi-line Comments

  • Multi-line comments can be created using triple single quotes (''' ''') or triple double quotes (""" """), which allow for commenting multiple lines at once.
  • Modern IDEs provide shortcuts (like Ctrl + / in Replit and VSCode) for easy commenting and uncommenting of selected lines.

Escape Sequence Characters

  • Escape sequence characters are used to insert special characters into strings. For instance, n represents a new line character.
  • Directly adding a new line within a string will cause an error; hence escape sequences are necessary for proper parsing by Python.

Handling Quotes in Strings

Understanding Escape Sequences in Python

The Confusion of String Termination

  • The speaker discusses how the Python interpreter can misinterpret string termination due to misplaced double quotes, leading to an "Invalid Syntax" error.
  • To avoid confusion with double quotes, the use of the escape sequence character " is introduced, allowing for proper inclusion of double quotes within strings.

Utilizing Escape Sequence Characters

  • An escape sequence character consists of a backslash followed by a character (e.g., ") that allows special characters to be included in strings without causing syntax errors.
  • The speaker emphasizes that single quotes cannot be used directly within single-quoted strings and must instead be escaped using '.

Print Statement Parameters

  • The print statement can accept multiple values, which will all be printed together. This includes various data types like strings and numbers.
  • A separator parameter (sep) can be added to customize how multiple values are separated when printed; for example, using a tilde (~).

Understanding 'end' Parameter in Print Statements

  • The end parameter specifies what should follow after the print statement concludes. By default, it adds a new line unless specified otherwise.
  • If you set end="009n", this will append "009" before any subsequent print statements rather than starting on a new line.

Optional Parameters in Print Function

  • Both the separator (sep) and file parameters are optional; thus, they do not need to be included for the print function to operate correctly.
  • It is reiterated that even if only single values are provided without additional parameters, the print function will still execute successfully.
Video description

Python Udemy Course: https://goharry.in/python Get this course at 90% Discount if you use this link Python is one of the most demanded programming languages in the job market. Surprisingly, it is equally easy to learn and master Python. This python tutorial for absolute beginners in Hindi series will focus on teaching you python concepts from the ground up. Access the Playlist: https://www.youtube.com/playlist?list=PLu0W_9lII9agwh1XjRt242xIpHhPT2llg Link to the Repl: https://replit.com/@codewithharry/05-Day5-Comments-and-Print#main.py Join Replit the browser-based IDE used in this course - https://join.replit.com/code-with-harry-100-doc ►Checkout my English channel here: https://www.youtube.com/channel/UC7btqG2Ww0_2LwuQxpvo2HQ ►Instagram: www.instagram.com/codewithharry python, C, C++, Java, JavaScript and Other Cheetsheets [++]: Playlist: https://www.youtube.com/playlist?list=PLu0W_9lII9agrsRZjFECeFuWY5ev2pQlk ►Learn in One Video[++]: Python[15 Hr]: https://www.youtube.com/watch?v=gfDE2a7MKjA&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python Advance[3.5 Hr]: https://www.youtube.com/watch?v=61a7UkDO50s&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python[1 Hr]: https://www.youtube.com/watch?v=qHJjMvHLJdg&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python[2 Hr]: https://www.youtube.com/watch?v=ihk_Xglr164&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Python[15 Min]:https://www.youtube.com/watch?v=fr1f84rg4Nw&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 JavaScript[1 Hr]: https://www.youtube.com/watch?v=onbBV0uFVpo&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 C[1.3 Hr]-https://www.youtube.com/watch?v=YXcgD8hRHYY&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 php[1 Hr]: https://www.youtube.com/watch?v=xW7ro3lwaCI&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 php[2.3 Hr]:https://www.youtube.com/watch?v=1SnPKhCdlsU&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 php[Project]- https://www.youtube.com/watch?v=-al2bECumKg&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 HTML[30 Min]:https://www.youtube.com/watch?v=E3ByCRqE7Lo&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 CSS[8.5 Hr]:https://www.youtube.com/watch?v=Edsxf_NBFrw&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 CSS[1.4 Hr]:https://www.youtube.com/watch?v=u5-K_ua9sOw&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Wordpress[3.2 Hr]:https://www.youtube.com/watch?v=GlLRYml8mCY&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Angular[2 Hr]:https://www.youtube.com/watch?v=0LhBvp8qpro&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Java[2.3 Hr]:https://www.youtube.com/watch?v=rV_3Lewxx6o&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Web Scraping[1 Hr]:https://www.youtube.com/watch?v=uufDGjTuq34&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 MongoDB[2 Hr]:https://www.youtube.com/watch?v=oSIv-E60NiU&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Numpy[1 Hr]:https://www.youtube.com/watch?v=Rbh1rieb3zc&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Android Dev[12 Hr]- https://www.youtube.com/watch?v=mXjZQX3UzOs Linux[1 Hr]:https://www.youtube.com/watch?v=_tCY-c-sPZc&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 JQuery[1.1 Hr]:https://www.youtube.com/watch?v=YFlx1C8XwR0&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 Git and GitHub[1.1 Hr]:https://www.youtube.com/watch?v=gwWKnnCMQ5c&list=PLu0W_9lII9ahKZ42vg2w9ERPmShYbYAB7 ►Complete course [playlist]: React: https://www.youtube.com/playlist?list=PLu0W_9lII9agx66oZnT6IyhcMIbUMNMdt Python-https://www.youtube.com/playlist?list=PLu0W_9lII9agICnT8t4iYVSZ3eykIAOME OOP Python-https://www.youtube.com/playlist?list=PLu0W_9lII9ahfRrhFcoB-4lpp9YaBmdCP Java:https://www.youtube.com/playlist?list=PLu0W_9lII9agS67Uits0UnJyrYiXhDS6q JavaScript- https://www.youtube.com/playlist?list=PLu0W_9lII9ajyk081To1Cbt2eI5913SsL PHP-https://www.youtube.com/playlist?list=PLu0W_9lII9aikXkRE0WxDt1vozo3hnmtR C-https://www.youtube.com/playlist?list=PLu0W_9lII9aiXlHcLx-mDH1Qul38wD3aR C++-https://www.youtube.com/playlist?list=PLu0W_9lII9agpFUAlPFe_VNSlXW5uE0YL Git & GitHub-https://www.youtube.com/playlist?list=PLu0W_9lII9ahVQekD7ePHmnirTePXwIln Android Dev- https://www.youtube.com/playlist?list=PLu0W_9lII9aiL0kysYlfSOUgY5rNlOhUd Python GUI- https://www.youtube.com/playlist?list=PLu0W_9lII9ajLcqRcj4PoEihkukF_OTzA Web Development- https://www.youtube.com/playlist?list=PLu0W_9lII9agiCUZYRsvtGTXdxkzPyItg Python Django:https://www.youtube.com/playlist?list=PLu0W_9lII9ah7DDtYtflgwMwpT3xmjXY9 Projects Using HTML, CSS & Javascript- https://www.youtube.com/playlist?list=PLu0W_9lII9aiQiOwthuSvinxoflmhRxM3 Data Structure and Algo:https://www.youtube.com/playlist?list=PLu0W_9lII9ahIappRPN0MCAgtOu3lQjQi Follow Me On Social Media ►Website (created using Django Rest & Angular): https://www.codewithharry.com ►Facebook: https://www.facebook.com/CodeWithHarry ►Instagram: https://www.instagram.com/codewithharry/ Twitter: https://twitter.com/CodeWithHarry Comment "#HarryBhai" if you read this 😉😉