Monday, May 27, 2019

Python First Program - Hello World

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 World
If 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

  1. Functions in Python
  2. Class And Object in Python
  3. self in Python
  4. Interface in Python
  5. Python Conditional Statement - if, elif, else Statements

You may also like-

  1. String Slicing in Python
  2. Magic Methods in Python With Examples
  3. Operator Overloading in Python
  4. List in Python With Examples
  5. Statement Interface in Java-JDBC
  6. Conditional Operators in Java With Examples
  7. Insertion Sort Program in Java
  8. Spring MVC File Upload (Multipart Request) Example

No comments:

Post a Comment