Python Hello, World#
Python is easy to learn and code and can be execute with python interpreter. We can also use Python interactive shell to test python code immediately. A simple hello world example is given below. Write below code in a file and save with .py extension. Python source file has .py extension.
“Hello, World!” Program#
Let’s create a very simple program called Hello World
. A “Hello, World!” is a simple program that outputs Hello, World!
on the screen.
print("Hello, World!")
Let’s break down the different components of the code.#
print()
is a function that tells Python to display or output whatever we put in the parentheses.Some functions, like the print() function, are built-in functions included in Python by default.
Inside the parentheses of the
print()
function is a sequence of characters — Hello, World! — that is enclosed in quotation marks'
or"
.Once we are done writing our program, save the file and we can exit notepad.
Running the “Hello, World!” Program#
The hello.py program produce the following output:
Hello, World!
print("Hello, World!")
Hello, World!
Print blank lines#
Sometimes you need to print one blank line in your Python program. Following are an example to perform this task.
Example:#
Let us print 8 blank lines. You can type:
>>>print (9 * "\n")
or
>>>print ("\n\n\n\n\n\n\n\n\n")
print ("Hello World!")
print (9 * "\n")
print ("Hello World!")
Hello World!
Hello World!
print ("Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug\nSep\nOct\nNov\nDec")
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
print ("I want \\n to be printed.")
print("I'm very *happy*")
I want \n to be printed.
I'm very *happy*
print ("Hello\tWorld!") # \t is equal to 4 spaces
Hello World!
print ("""
Routine:
\t- Eat
\t- Study
\t- Sleep\n\t- Repeat
""")
Routine:
- Eat
- Study
- Sleep
- Repeat