Introduction to python - Part 2

ยท

4 min read

In the previous article, we learned about what is python, how to install it, and learned about some IDE where python can be written. If you haven't seen the previous article please visit it. In this article, we are going to write our first python code and know some concepts of comments in python, and escape characters in python. Let's get started...

Writing first python code

In this blog post, we will explore how to use the "print" statement, escape characters, and comments in Python code.

Print Statement

The "print" statement in Python is used to output text and values to the console. The 'console' is nothing but a space where we get the output of our python code. A simple print statement goes like this.

print("Hello, how are you")
print('Hello, how are you')

In the code above, we used the "print" statement to output the text "Hello, how are you" to the console. The text to be printed is enclosed in parentheses and quotes. In python, single and double quotes mean the same. In the above code, both of them will print "Hello, how are you". This was about how to print anything in python. We are going to print variables in the coming sections.

Comments in Python

Comments are used in Python code to add explanations and notes for other programmers or for future reference. In Python, comments start with the "#" symbol and continue until the end of the line. For example:

# This is a comment

# below is the print statement to print hello.
print("hello")  # This is also a comment

The '#' symbol is used when we want to comment on a single line. When we want to comment on multiple lines we use triple quotes. You can write multi-line comments to provide detailed explanations for your code. Multi-line comments are also called block comments, and they can span multiple lines of code.

To write a multi-line comment in Python, you can enclose the comment text in triple quotes (either single or double quotes), like this:

"""
This is a multi-line comment.
It can span multiple lines of code.
Use it to provide detailed explanations for your code.
"""

In the code above, we used triple quotes to enclose the comment text. The comment starts with three quotes on one line and ends with three quotes on another line. Any text between the triple quotes will be treated as a comment and ignored by the Python interpreter.

Escape Characters in Python

In Python, we can use escape characters to insert special characters in strings. Escape characters are denoted by a backslash "\" followed by a character. Some common escape characters are:

  • \n --> inserts a new line character in a string.

  • \t --> inserts a tab character in a string.

  • \" --> insert a double quote character in a string.

  • \' --> insert a single quote character in a string.

Here's an example of how to use escape characters in Python:

# Printing a string with a new line using the "\n" escape character
print("Hello,\nWorld!")

# Printing a string with a tab using the "\t" escape character
print("Hello,\tWorld!")

# Printing a string with a double quote using the "\" escape character
print("She said, \"Hello, World!\"")

# Printing a string with a single quote using the "\'" escape character
print('She said, \'Hello, World!\'')

# Printing a string with a backslash using the "\\" escape character
print("C:\\Users\\John")

The outputs of the following codes are as follows.

Hello,
World! 
Hello,    World!
She said, "Hello, World!"
She said, 'Hello, World!'
C:\Users\John

In conclusion, the "print" statement, escape characters, and comments are all important concepts in Python programming. Understanding how to use these features allows you to create more effective and readable Python code.

I hope that you guys have understood what I tried to explain. Now that's it for this blog, we will be learning some more things in the upcoming blogs. Till then keep learning.

Please do like, share, and follow for more content like this ๐Ÿ˜€.

You can connect with me on Linkedin |GitHub
if you have any suggestions please comment below, Thank you :)

ย