Once you are done with Python installation first thing you want to do is to write hello world Python program to check if every thing is working as required or not.
Python command line
You can enter the python command line by typing python on command prompt and write your first hello word program there itself to display “Hello World”.
print() function is used here to print "Hello World" on the console.
Python hello world program
By using command line though we are successful in printing “Hello World” and that does verify installation is done without any problem but this way of writing hello world program doesn’t satiate the programmer in you so you can open an editor and write the same line- print(“Hello World”) there and save it as HelloWorld.py where py extension indicates Python file.
Then you can run this program from command line-
F:\NETJS>Python HelloWorld.py Hello WorldIf you want to make HelloWorld Python program procedural then you can also write program in HelloWorld.py as follows.
def display(): print("Hello World") # call function display()
Here display() is a function that prints hello world. You can run this program from command line the same way as above.
Python hello world program – Object oriented way
Well if you are looking for a proper object oriented way to write your first python program then you can write a class and method with in that class to display hello world. Then use an instance of the class to call the method.
class HelloWorld: # method def display(self): print("Hello World") #creating object obj = HelloWorld() #calling method obj.display()
Output
Hello World
You can also pass an argument to method.
class HelloWorld: # method def display(self, name): print("Hello World", name) #creating object obj = HelloWorld() #calling method obj.display("netjs")
Output
Hello World netjs
That's all for this topic Python Hello World program. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Python Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment