"Hello, World!" Tutorial on Python

01
of 06

Introducing "Hello, World!"

The simplest program in Python consists of a line that tells the computer a command. Traditionally, the first program of every programmer in every new language prints "Hello, World!" Start up your favorite text editor and save the following in a file:

 print "Hello, World!" 

To execute this program, save it with a suffix of .py—HelloWorld.py—and type "python" and the filename in a shell like this:

 > python HelloWorld.py 

The output is predictable:

Hello, World!

If you prefer to execute it by its name, instead of as an argument to the Python interpreter, put a bang line at the top. Include the following on the first line of the program, substituting the absolute path to the Python interpreter for /path/to/python:

 #!/path/to/python 

Be sure to change the permission on the file to allow execution if necessary for your operating system.

Now, take this program and embellish it a bit.

02
of 06

Importing Modules and Assigning Values

First, import a module or two:

 import re, string, sys 

Then let's define the addressee and the punctuation for the output. These are taken from the first two command line arguments:

 greeting = sys.argv[1]
addressee = sys.argv[2]
punctuation = sys.argv[3] 

Here, we give "greeting" the value of the first command-line argument ​to the program. The first word that comes after the program's name when the program is executed is assigned using the sys module. The second word (addressee) is sys.argv[2] and so on.The program's name itself is sys.argv[0].

03
of 06

A Class Called Felicitations

From this, create a class called Felicitations:

 class Felicitations(object):
def __init__(self):
self.felicitations = [ ]
def addon(self, word):
self.felicitations.append(word)
def printme(self):
greeting = string.join(self.felicitations[0:], "")
print greeting 

The class is based on another type of object called "object." The first method is mandatory if you want the object to know anything about itself. Instead of being a brainless mass of functions and variables, the class must have a way of referring to itself. The second method simply adds the value of "word" to the Felicitations object. Finally, the class has the ability to print itself via a method called "printme."

Note: In Python, indentation is important. Every nested block of commands must be indented the same amount. Python has no other way to differentiate between nested and non-nested blocks of commands.

04
of 06

Defining Functions

Now, make a function that calls the last method of the class:

 def prints(string):
string.printme()
return 

Next, define two more functions. These illustrate how to pass arguments to and how to receive output from functions. The strings in parentheses are arguments on which the function depends. The value returned is signified in the "return" statement at the end.

 def hello(i):
string = "hell" + i
return string
def caps(word):
value = string.capitalize(word)
return value 

The first of these functions take an argument "i" which is later concatenated to the base "hell" and returned as a variable named "string." As you see in the main() function, this variable is hardwired in the program as "o," but you could easily make it user-defined by using sys.argv[3] or similar.

The second function is used to capitalize the parts of the output. It takes one argument, the phrase to be capitalized, and returns it as a value "value."

05
of 06

The Main() Thing

Next, define a main() function:

 def main():
salut = Felicitations()
if greeting != "Hello":
cap_greeting = caps(greeting)
else:
cap_greeting = greeting
salut.addon(cap_greeting)
salut.addon(", ")
cap_addressee = caps(addressee)
lastpart = cap_addressee + punctuation
salut.addon(lastpart)
prints(salut) 

Several things happen in this function:

  1. The code creates an instance of the Felicitations class and call it "salut," which allows access to the parts of Felicitations as they exist in salut.
  2. Next, if "greeting" does not equate to the string "Hello," then, using function caps(), we capitalize the value of "greeting" and assign it to "cap_greeting." Otherwise, "cap_greeting" is assigned the value of "greeting." If this seems tautological, it is, but it is also illustrative of conditional statements in Python.
  3. Whatever the outcome of the if...else statements, the value of "cap_greeting" is added onto the value of "salut," using class object's append method.
  4. Next, we append a comma and a space to salut in preparation for the addressee.
  5. The value of "addressee" is capitalized and assigned to "cap_addressee."
  6. The values of "cap_addressee" and "punctuation" are then concatenated and assigned to "lastpart."
  7. The value of "lastpart" is then appended to the content of "salut."
  8. Finally, the object '"salut" is sent to the "prints" function to be printed to the screen.
06
of 06

Tying It Up With a Bow

Alas, we are not done yet. If the program is executed now, it would end with no output whatsoever. This is because the function main() is never called. Here is how to call main() when the program is executed:

 if __name__ == '__main__':
main() 

Save the program as "hello.py" (without the quotes). Now, you can start the program. Assuming the Python interpreter is in your execution path, you can type:

python hello.py hello world !

and you will be rewarded with the familiar output:

Hello, World!
Format
mla apa chicago
Your Citation
Lukaszewski, Al. ""Hello, World!" Tutorial on Python." ThoughtCo, Feb. 16, 2021, thoughtco.com/quick-tutorial-on-python-2813561. Lukaszewski, Al. (2021, February 16). "Hello, World!" Tutorial on Python. Retrieved from https://www.thoughtco.com/quick-tutorial-on-python-2813561 Lukaszewski, Al. ""Hello, World!" Tutorial on Python." ThoughtCo. https://www.thoughtco.com/quick-tutorial-on-python-2813561 (accessed March 28, 2024).