First program: Hello, World!¶
Work with the system console¶
Due to the fact that Python is interpreted language, it provides the command console that allow to execute the single command without the whole program. It's a great benefit that, in case of doubt, you can test a Python's command before you use it in a code.
To execute the Python's command line, go to the system console (cmd, Powershell, bash) and write a command:
python
python3
Execution example:
$ python
Python 3.8.0 (...)
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
it's prompt that means you can use the Python's commands.
Python interpreter is a great calculator. You can check it using arithmetic operations:
>>> 4 + 5
9
>>> 2 + 5 - 3 * 2
1
>>> 10 / 5
2.0
Hello World!¶
Usually the first program is to write "Hello World!" on the screen.
In Python it's trivially easy. It's only a one line:
print("Hello, World!")
Program execution¶
To execute above instruction and check if "Hello World!" will show up on the screen:
- go to command shell and write command
print
with "Hello, World!" in brackets or - save code line in file with .py extension and write command: this will execute the program and show the sentence on the screen.
python file-name.py
How program works¶
- Command show the sentence Hello, World! on the screen.
- Build-in function
print
was used. - This function uses the parameter in brackets - sentence, that we want to display on the screen.
At this moment we will treat this function as a behaviour: something needs to be done depends on the parameter. Coffee machine is good example of the function. Specific coffee is prepared (function) based on the user choice (parameter).
One of Python advantage is that we need a small code to get the result. In this case we want to display "Hello World!" on the screen and it's done only by one line of the text. For example in Java we need 3 to 5 lines.