Python first program: print & comment

Okay guys, as you all know, Python is one of the simplest programming languages to learn. This is your first chapter where you will actually write and run your first Python code. But before that, I want to tell you that every language has its own way of writing—just like English has its own grammar and Hindi has its own. Similarly, Python also has its own way of writing, which is called syntax. Technically, syntax refers to a set of rules or instructions that you need to follow when writing a program.

As we know, Python provides two different ways to execute code:

  1. Shell mode (executes code line by line and provides a command-line interface).
  2. Script mode (write your code in files, and Python executes it all at once).

You can use either of these modes to run your Python code.

# First program
print("Hello World")
Python

In the above code, first, we have written print, which is a function. To call a function, we need to use parentheses. Hence, print() is the calling of the print function. But you might not know what a function is. Functions are nothing but a set of instructions, and print is one of the predefined (or built-in) functions provided by Python itself for direct use. This function is used to display content on the console (terminal) that is written inside it, either directly, using a variable, or in any other way.

As we know, Python has multiple data types. Here, we are using a string ("Hello World"). However, we can use any other valid Python data type to print.

The above code prints "Hello World" on the console. But what if you want to print a string and an integer simultaneously? The print() function can print any number of values that you want. Just separate them using a , (comma).

# Print multiple values
print("Name:", "Inkita\n", "Age:", 22)
Python

The above code produces the following output:

Name: Inkita
Age: 22
Bash

Separate multiple values

As you can see, after "Name:", a space is used to separate it from "Inkita". The "\n" then changes the line. Similarly, "Age: 22" follows. But wait—how does this space appear? Actually, Python provides an argument to the print() function called sep, which has a default value of a single white space (). You can change it as shown below:

# Modify separator in print
print("Name:", "Inkita\n", "Age:", 22, sep="#")
Python

Here’s the output:

Name:#Inkita#Age:#22
Bash

Boom! Now the space is replaced with the string "#". Similarly, the print() function provides another argument called end, which determines what should be used at the end after printing everything. By default, it is "\n" (newline). This is why, when you use two different print() statements, they each end with a new line.

print("SHRAY")
print("SALVI")
Python

Output:

SHRAY
SALVI
Bash

You can change this behavior as well.

# Changing the end character of print
print("SHRAY", end="-")
print("SALVI")
Python

Output:

SHRAY-SALVI
Bash

Comments

Throughout this conversation and the examples, did you notice that I wrote lines starting with # that didn’t do anything? Those are comments. Python provides two different ways to add comments in a program. Comments don’t affect the program during runtime; instead, they are used to explain what you have written and why.

  • Single-line comment: It can be written by putting a # at the beginning, followed by your comment.
  • Multi-line comment: It is essentially a multi-line string that does nothing if it is not assigned to a variable.
# This is a single-line comment

"""
This is a multi-line comment.
It spans multiple lines.
"""
Python

Conclusion

So you have learned how to write and execute your first Python program using the print() function, understood how the print() function can display different types of data on the console, and how to customize the behavior of the print() function using the sep and end arguments. The importance of comments, which help explain your code without affecting its execution. So welcome to the journey of learning Python. Be consistent and have a never-give-up attitude.

Python first program

New line insertion

1 / 8

What effect does the escape sequence \n have in a string?

Use sep parameter

2 / 8

How can you change the separator between printed values in Python?

Same line with space

3 / 8

What is the output of the following code?

print("Hello", end=" ")
print("World")

Parentheses are required

4 / 8

How do you call a function in Python?

Explain code purpose

5 / 8

Why should you include comments in your Python code?

Basic print function

6 / 8

What is the output of the following code?

print("Hello World")

sep changes separator

7 / 8

What will be the output of this code?

print("Name:", "Inkita\n", "Age:", 22, sep="#")

Hyphen separator, exclamation end

8 / 8

What is the output of print("A", "B", sep="-", end="!")?

Your score is

The average score is 0%

0%

Leave a Reply

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