Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 862

Python First Program - Hello World

$
0
0

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!


Related Topics

  1. Constructor in Python - __init__() function
  2. self in Python
  3. Class And Object in Python
  4. Encapsulation in Python
  5. Interface in Python

You may also like -

  1. Non-Blocking Algorithms
  2. Statement Interface in Java-JDBC
  3. BigDecimal in Java
  4. Conditional Operators in Java
  5. Insertion Sort Program in Java
  6. Spring MVC File Upload (Multipart Request) Example
  7. Bean Definition Inheritance in Spring
  8. Introduction to Hadoop Framework

Viewing all articles
Browse latest Browse all 862

Trending Articles