Skip to main content

Day 4 : Comments, Escape Sequences & Print Statement | Master Python in 100 Days

Day 4 : Comments, Escape Sequences & Print Statement


Welcome back to Day 4 of our Python journey! Today, we will dive deeper into three important topics: comments, escape sequences, and the print statement. Understanding these concepts is essential for writing clear and effective code. Let’s explore them in detail!


What are Comments?

Definition :

  • Comments are notes that you write in your code. They help explain what the code does. Python ignores these comments, so they don't affect how your program runs. Comments are helpful for anyone who reads your code, including you!

Comments are very useful for:

  • Explaining complex logic
  • Making the code easier to read
  • Providing context for future reference

Types of Comments

1. Single-Line Comments : These comments are written on one line and start with the # symbol. Python will ignore everything after the # on that line.

Example:

  • In this example, # This is a single-line comment explains that the line below it prints "Hello, World!". The comment helps anyone reading the code understand its purpose.

2. Multi-Line Comments : For comments that need more than one line, you can use triple quotes (""" or '''). This type of comment is helpful when you want to explain something in detail or when you want to temporarily stop a piece of code from running.


Example:

  • In this example, the comment starts and ends with triple quotes. It explains that the next line of code will print "Welcome to Python!" on the screen. This way, anyone reading the code can understand what the print statement is doing.



  • What are Escape Sequences?


    Definition :
    • Escape sequences are special characters in Python strings that begin with a backslash (\). They allow you to represent certain characters that are hard to include directly in strings, such as new lines, tabs, or quotation marks.


    Common Escape Sequences : 

    • \n - New Line: Moves the cursor to the next line in the output.
    • \t - Tab: Inserts a horizontal tab space, creating indentation in the output.
    • \\ - Backslash: Inserts a single backslash character.
    • \' - Single Quote: Allows you to include a single quote in a string surrounded by single quotes.
    • \" - Double Quote: Allows you to include a double quote in a string surrounded by double quotes.


      Using Escape Sequences:

      • Escape sequences help format strings in a more readable way. Here are some examples:


      • In these examples, we use escape sequences to format our output nicely.


      The Print Statement

      The print() function is used to display information on the screen. You can print strings, numbers, or even the results of calculations.

      Basic Usage:


      Printing Multiple Items:

      You can print multiple items by separating them with commas. Python will automatically add a space between each item.

      • This will output: My name is Samanta and I am 25 years old. Here, the comma separates the different items, allowing us to print variables and strings in one line.


      Formatting Output

      • For more complex output, Python provides several ways to format strings. One of the easiest methods is using f-strings (available in Python 3.6 and later), which allows you to embed expressions inside string literals.


      Example of f-Strings :
      • In this example, {name} and {age} are placeholders that will be replaced with the values of the variables. This makes the code cleaner and easier to read.

      Key Points We Have Learned

      • Comments are used to explain your code and are ignored by Python.
      • Escape sequences allow you to include special characters in strings, like new lines and tabs.
      • The print() function displays information on the screen and can print multiple items or formatted strings.

      Thank you for joining me on Day 4 of the "Master Python in 100 Days" challenge! Today, we explored comments, escape sequences, and the print statement in depth. These tools are essential for writing clear and effective Python code.

      Remember, practice is key to mastering these concepts, so take some time to experiment with what you’ve learned today. Tomorrow, we will dive into more exciting features of Python, and I can’t wait to continue this journey with you!

      Let’s keep growing, learning, and coding together!

      Comments

      Popular posts from this blog

      Day 5: Understanding Variables and Data Types in Python | Master Python in 100 Days

      Day 5: Data Types, Variables, and Type Conversion in Python W elcome to Day 5 of our "Master Python in 100 Days" journey! Today, we will explore some of the most important building blocks in Python: data types , variables , and type conversion . Understanding these concepts is essential as they form the basis of how we store and manipulate data in our programs. What are Data Types? Data types tell Python what kind of data we are working with. Python has several built-in data types, and the most common ones are:   1.  Integers (int) : These are whole numbers without decimal points, like 10 , -5 , 0 , etc .   2.  Floats (float) : These are numbers with decimals, like 3.14 , 0.001 , -2.5 .  3. Strings (str) : A string is a collection of characters (letters, numbers, symbols) enclosed in quotes ( " " or ' ' ) . These are used to represent text.  4.  Booleans (bool) :  These are either True or False values. It’s used for logical conditions. Chec...

      Day 3: Writing Our First Python Program in Detail | Master Python in 100 Days

      Day 3: Writing Our First Python Program in Detail  W elcome back to Master Python in 100 Days ! Today, on Day 3, we are going to write our very first Python program from scratch, step-by-step. This isn’t just about writing code; it’s about understanding every line and making sure we know exactly what’s happening in our program. Let's get started! A Quick Review: What We Did on Day 1 If you remember, on the very first day of the   Master Python in 100 Days , we touched upon how to use the print() function to display basic output on the screen. That was a warm-up , where we gave a quick look at the print() function. But starting from today, we will dive deeper into the actual use of print() . By the end of this session, you’ll not only understand how to use print() properly, but also learn how to avoid common errors and use it in your own Python scripts. So, let’s take our Python learning journey one step further!   Introduction to the print() Function : The ...

      Day 1: Introduction to Python | Master Python in 100 Days

      Day 1: Introduction to Python Hello everyone! Welcome to Day 1 of our 100 Days to Learn Python journey! Today, we will start by understanding what Python is, its importance, and how to get started. What is Python? Python is a popular, high-level programming language that was created by Guido van Rossum and first released in 1991 . It’s designed to be easy to read and simple to write, making it perfect for both beginners and advanced programmers. Python allows developers to focus on solving problems rather than worrying about the complex syntax often found in other programming languages. Why Learn Python? 1. High demand: Many companies need Python programmers. 2. Versatile: You can use Python for web development, data analysis, machine learning, game   development, automation, and more. 3.  Great community: There are many resources, forums, and libraries available online.    Key Features of Python: 1. Simple and Easy to Learn : Python’s syntax is similar to the...