Hello World! First Python Script

Updated 13 April 2022

Python is a powerhouse programming language capable of supporting you throughout all your programming adventures. Making exciting and unique code is just flashes of comprehension away. For those just starting, looking at the Python Program Window the potentials can seem immense. It can leave you wondering just how exactly do I begin my Python journey? The answer to this, my dear reader, is by writing a 'Hello World' script. So let us dive right in and start learning Python! 

By the end of this guide, we will have created and executed a Python program. This will instruct our computing device to announce in five different ways ‘Hello World!’. This will demonstrate some of the capacities of the print command. The print command is typed in Python as | Print() |. This guide will be a dive into what these programming files are saved as and also give an analysis of the code. Furthermore, it will introduce the concept of variables, strings and commands. To highlight differences we will run this program in Python and through an online Python emulator. The contents of this guide can be seen below.

There are several related tutorials on our website to help you become a true Pythonista. A great place to start would be Python Rundown and Main Windows and Python and MicroPython - Compare and Access. These will also tell you where to download Python and access online Python emulators. Python is an excellent language to utilise with Raspberry Pi Boards such as Raspberry Pi 4 Model B 8GB. These are computing devices that can easily fit on the palm of your hand.

As always if you have any questions, queries or things to add please let us know your thoughts!


The Code

The code written below demonstrates a number of ways to use the print function in Python. A function is a block of organized, reusable code that is used to perform a single, related action that only runs when called upon. 

#Hello World! First Python Script

#Print Function is being used below to display Hello World
#What you want displayed needs to be in ' ' or " " and they need be a matching
#pair. Quotations Marks are not required if printing a number.


# (1) Simplest example of print function.
print('Hello World!')


# (2) Print function method of pulling two strings variables and printing them
#     together.
print("Hello Again "   "World!")


# (3) Print function allowing you to separate what is printed with a paragraph
#     using \n to denote a new paragraph.
print("Hello\nOnce\nAgain\nBig\nWorld!")


# (4) Printing function method with all info displayed on one line but allows
#     a delay to be coded in between the first part and second part 
print('Guess what...', end='')
#     Right here, between these two prints you can code in a pause (perfect
#     for loading screens) 
print(' Hello World!')


# (5) Final method of printing Hello World. This uses a variable referred to
#     as x. Below we tell the computing device what the value of x is. 
x = ('Final ''Hello ''World')
#     Now the print function is used to display the variable x with an
#     Exclamation mark at the end
print(x, end='!')


The Script in Python and Online Python Emulator

So without further ado lets execute your first Python script, Hello World! Below left you can see the code from above written out in the Python Program Window. You too can write out the code or copy the above into your own Python window or download the file at the bottom of the page. To run a program in the Python Program Window press F5 or go up to the drop-down menus at the top, click on Run then click on Run Module. Below right you can see the Python IDLE Shell displaying the results of the script after it has run. You can see in the results of the code stacked on top of each other in blue. This code produces five unique hello world variations making use of different capabilities of the print function. Above the results is the exact location saved to the computer of the code. Above that is the exact version of Python being used as well as the date and time. The bottom right of each window tells you exactly the location that the text cursor has been placed on the code, by line number and column number. The text cursor can be altered by clicking elsewhere in the code. The location to download Python is seen here - Download Python.
 

Python Program Window on Left and Python IDLE with results on Right

 

Below you can see the script and results of the script running in the Online Python Compiler. It is as if the Pythom Program Window was situated on the left and the Python IDLE shell was situated on the right. The name automatically given to the code is main.py however you will not be able to save it to your own computer. Details about the time and date are not given. Each new line of code is labelled with a number. The location to access this emulator is seen here - Access Python Online Compiler

Emulator running the code!


Overview and Code Analysis

A print statement is the easiest way to get your Python software to communicate with you. Being able to effectively control this communication will be one of the most valuable tools in your programming toolbox. The main purpose of this code is to introduce syntax, learn about comments and to establish a very useful function, print. To use this command in Python type | Print() |. This is one of the simplest directives within Python, it prints out a line and includes a newline. The print directive is both a function and a command word. A function in Python is a block of code which only runs when it is called. 

 

Python Code File Saved on Computer

 

 

The code, as you can see above, has been saved as a Python File (.py) and then run by the Python IDLE Shell and also run using an online Python emulator. The code demonstrates many variations on how to use the print function. It is also interesting to see how small the file is (this one takes up only two kilobytes).

A Python Wizard!The print function requires parentheses to be invoked | ( ) |, cannot be used with curly brackets | {  } |. This basically means all the information you want to be printed needs to be inside parentheses. To print characters you must encase the data in single or double quotation marks | ' ' | or | " " |. This is to ensure the characters inside these quotations marks do not get mistaken for a command. Quotation marks are not required if printing a number. To separate what is written inside the function with a paragraph employ | \n |. This is shown in the third hello world example.

Comments are very important while writing a program. In Python, any written text after a | # | will be a comment. These are seen as red in Python and light green in the Online Python Compiler. It will extend until a newline character is used (the enter key for example). Python interpreter will completely ignore comments. Therefore any text written as a comment will have no impact on the program.  Correctly used they will describe what is going on inside a program so that another person (or you in the distant future) will be able to look at the code and quickly understand what is happening. Key details can easily be forgotten in a couple of weeks time and you’ll definitely save time as a programmer by employing good commenting habits.

In the fifth hello world example, line 32 in the code to be exact, states | x = ('Final ' 'Hello ' 'World') |. Now, in Python lingo, this is referred to as a string variable. A variable is a named location used to store data into the computing devices memory. Imagine variables as a box filled with books and these books can be changed, replaced or altered. Every variable in Python must have a name which can consist of letters, numbers, and/or the underscore character. The data of a variable can be a number, a string, a Boolean (true/false, yes/no statements), a list or some other data type.  A string is a sequence of characters (letters, numbers, whitespace or punctuation) enclosed by quotation marks. It can be enclosed using either the double quotation mark or the single quotation mark.

The equal sign | = | is used to assign a value to a variable. Python is a case-sensitive language. This means if you type | x | and | X | as variables they will be recorded as two unique variables. It is best practice to give your identifiers (the title you call the variable) a name that makes sense. Don’t be afraid to use longhand and I would recommend avoiding abbreviation as it will be easier to figure out what is going on in your code after a long nap. When written longhand you can even use underscores to separate words in identifiers.


The Next Step

A great next step on your coding adventures would be the guide Python Variables and Types.


Download the Code

Below is a link which will let you download the code directly into your computing device. Open it up with Python IDLE shell.  

Attachment - First_Python_Script.zip

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.