Previous Index Next

Creating First Program

The shell window is useful because as you enter commands, Python will execute it and display it. But if you want to create a program that can be saved and run, again and again, go to the menu and click File -> New File. You can also do this by pressing Ctrl + N.

Program (intro.py)

print("Deepak Singh")
print("Indirapuram, Ghaziabad")
print("Pin - 201014")

To run the program, click on Run -> Run Module. You can also press F5. The following output will be displayed.

Output

Deepak Singh
Indirapuram, Ghaziabad
Pin - 201014

print() Function

To display the message on the screen print() function is used as we used in the above example. Let's learn more about the print function.

Syntax: print(object(s), sep=separator, end=end)

You can print more than one object by separating them by a comma. In interactive mode (shell), if you type statement and press the Enter key, the following output will display:

>>> print("Orange", "Mango", "Apple")
Orange Mango Apple

sep parameter specifies how to separate more than one object. It is an optional parameter default is ' '.

>>> print("Orange", "Mango", "Apple",sep="#")
Orange#Mango#Apple

Line break The '\n' character is used to insert a line break in a string. Look at the following example.

>>> print("Everybody\nHas a name\nSome are different\nSome the same")
Everybody
Has a name
Some are different
Some the same

end parameter specifies what character you want at the end of a print statement. It is an optional parameter default is newline. Look at the next program to see an example of this:

Program (print-single-line.py)

print("apple", end = " ")
print("banana", end = " ")
print("mango")

Output

apple banana mango

Comments in Python

Comments are important for making your code more readable and for explaining what your code is doing. Comments are not executed by Python interpreter, but they help you and others to understand your code. In Python, there are two types of comments: single-line comments and multi-line comments.

Single-Line Comments

Single-line comments are used to explain a single line of code. They start with the hash symbol # and continue until the end of the line. Here's an example:

# This program displays an address.
print("Deepak Singh") #Displays Person Name
print("Indirapuram, Ghaziabad")
print("Pin - 201014")

The print() statements will execute, but the comments will be ignored.

Multi-Line Comments

Multi-line comments are used to explain a block of code. They start and end with three double quotes """ or three single quotes '''. Here's an example:

"""
This is a multi-line comment
that explains a block of code.
"""
print("Hello, world!")
    

The print() statement will execute, but the comment will be ignored.

Comments are an essential part of writing clean and readable code in Python. Whether you use single-line comments or multi-line comments, it's important to document your code and explain what it's doing.


Previous Index Next