ubuntu2004
Hello, World!
'Hello, World!' is a traditional introductory exercise in programming with origins dating back as far as the mid 1970s. It has become an almost universal first step taught in all programming languages.
The task is to write a program to display the text 'Hello, World!' on the computer screen, and by presenting such an easy task ensures you are ready to begin to learn to code.
To print Hello, World! in Python, type print( "Hello, World!" )
into the empty code cell below, then press ctrl+enter to run the cell.
Making mistakes and Python errors
All coders, even the best, make mistakes all of the time. Coding is often a process of trial and error until the code works; and even then there may be missed errors (called bugs) that do not surface until later. So do not become disheartened when your code does not work, even after many tries.
Here are a couple of examples of typical errors.
All Python commands, such as print
, are in lowercase. If you type in a command with any uppercase characters and run it you will cause Python to raise an error.
Try using Print
or PRINT
instead of print
in the code cell above to see what happens.
You should get an error message. The last line of the error message should saying something like NameError: name 'Print' is not defined
. This is telling you that Python does not know what Print
is and so cannot run the code.
Here's another common error: Try missing off the closing double quotes like this print( "Hello, World! )
This time you should get SyntaxError: EOL while scanning string literal
. A syntax error means that Python does not understand how to run your code because you have missed or mis-typed a special character (like a speech mark or a parenthesis).
One of the hardest aspects of learning a programming language is to know which aspects of the language are inflexible, such as lowercase commands, and which aspects are not important. For example, you can add spaces before and after parentheses, and Python ignores these. You can also use single quotes as well as double quotes. So
all work and give the same output. Unfortunately, the only way to really learn these rules is by trial and error. Which can make learning any new language frustrating.
Comments
Python allows you to add comments within the program to describe what a part of the code does or how it works to anyone trying to read and understand the program. Such comments are also often extremely useful as a reminder to yourself for what a piece of code does.
Python comments always start with a #
symbol and extend to the end of the line. They can stand on their own line or can appear after a line of code. Python ignores all text after the #
to the end of the line, so the comments have no effect on how the program runs.
There's a quick way to comment or un-comment a line. Place your cursor on the line you wish to (un)-comment and press Ctrl-/.
You can (un)-comment several lines at the same time by selecting the lines of code either with your mouse with click-drag or using Shift-down arrow or Shift-up arrow.
Using Google to help you code
In this course, if you are stuck trying to answer an exercise, you can and should ask one of the teaching staff or post a question on the Learn Discussion board.
But sometimes, it is just faster to Google it. All of the questions you may have have an answer on the web. A common website for finding answers to coding questions is called stackoverflow. Coders from complete beginners to gurus use stackoverflow, so you are likely to find the answer to your question there. However, there are many other websites and some specifically for beginners. Try a few to see which you prefer.
One problem you will encounter when starting out is how to ask the right question. It is useful to begin a google query with the word "python" so that google knows the question is about Python and not some other programming language. Also using the technical terms you will come across in this course will help to direct your question to the right website.
During this course we will get you to try googling questions to help you complete some of the exercises.