Installation
There are pre-compiled packages made for easy installation on multiple operating
systems such as Windows, Linux, UNIX and Mac OS to name but a few.
If you head over to https://www.python.org/downloads/ you can select the right
installation method for your operating system. Most Linux systems come with Python preinstalled.
Version 3 of Python is used in this book.
In Windows, once you have installed Python, select Python IDLE or Python command
line/console from the Start menu or screen.
The difference between the IDLE and the console is that the IDLE has a graphical
interface, which looks similar to a text editor. In Linux, UNIX, and OS X, you can launch
Python emulator from command line by typing Python.
To choose a specific version of Python type PythonX where X is the version number (e.g.
“Python3” for version 3).
Hello World!
Most of the time that you write Python code, you will be writing the script in the IDLE or
any other IDE like Eclipse or rich text editor like Sublime text or Notepad++. However,
you can use the Python interpreter to write code interactively as it acts like a UNIX shell,
Even though it is possible to create programs using just the interpreter it is strongly
recommended not to do so since it is hard to save the code as a script for further reuse.
Rather consider the interpreter as an “on the fly” testing tool for your coding ideas.
Now lets make the interpreter output “Hello World!” string on the screen. Lets do it via
the interpreter.
Terminal way
Launch the interpreter from command line using Python command as it was described in
the previous chapter.
Type the following: print(“Hello world!”) and hit “Enter”.
The terminal shall look the following way:
In the code snippet above you can see that the first line starts with >>>.
This symbol indicates the line where you provide dynamic input to the interpreter. Notice,
as soon as the result of your first command was printed on the screen the interpreter
printed >>> again. Giving instructions to the interpreter is sequential. You give one
instruction - you wait till execution finishes, you enter another command and so on.
Note, further on in this book if you spot >>> symbol in a code snippet it shall mean that
you are expected to test the code in the interpreter yourself.
On the first line you entered a statement. The statement is nothing else but an
instruction for a computer to perform some task. The task could be anything from
make_a_coffee to launch_a_rocket. In your case it is a simple print function though.
Functions will be covered further in this book.
Scripting Way
One of the main principles of software engineering is reusability.
Let’s make the code that was just tested in the interpreter reusable.
To do so, create a file called “hello_world.py” and type the following text inside:
1. print(“Hello World!”)
What you placed into the file is exactly the line that was previously executed via the
interpreter.
Now lets execute the script that you wrote.
Open the command line, navigate to the location where the script resides and type the
following: python hello_world.py. After you hit enter you shall see that a “Hello World!”
text was placed on the screen.
Congratulations!
You’ve managed to write your first Python program and successfully launch it.

systems such as Windows, Linux, UNIX and Mac OS to name but a few.
If you head over to https://www.python.org/downloads/ you can select the right
installation method for your operating system. Most Linux systems come with Python preinstalled.
Version 3 of Python is used in this book.
In Windows, once you have installed Python, select Python IDLE or Python command
line/console from the Start menu or screen.
The difference between the IDLE and the console is that the IDLE has a graphical
interface, which looks similar to a text editor. In Linux, UNIX, and OS X, you can launch
Python emulator from command line by typing Python.
To choose a specific version of Python type PythonX where X is the version number (e.g.
“Python3” for version 3).
Hello World!
Most of the time that you write Python code, you will be writing the script in the IDLE or
any other IDE like Eclipse or rich text editor like Sublime text or Notepad++. However,
you can use the Python interpreter to write code interactively as it acts like a UNIX shell,
Even though it is possible to create programs using just the interpreter it is strongly
recommended not to do so since it is hard to save the code as a script for further reuse.
Rather consider the interpreter as an “on the fly” testing tool for your coding ideas.
Now lets make the interpreter output “Hello World!” string on the screen. Lets do it via
the interpreter.
Terminal way
Launch the interpreter from command line using Python command as it was described in
the previous chapter.
Type the following: print(“Hello world!”) and hit “Enter”.
The terminal shall look the following way:
In the code snippet above you can see that the first line starts with >>>.
This symbol indicates the line where you provide dynamic input to the interpreter. Notice,
as soon as the result of your first command was printed on the screen the interpreter
printed >>> again. Giving instructions to the interpreter is sequential. You give one
instruction - you wait till execution finishes, you enter another command and so on.
Note, further on in this book if you spot >>> symbol in a code snippet it shall mean that
you are expected to test the code in the interpreter yourself.
On the first line you entered a statement. The statement is nothing else but an
instruction for a computer to perform some task. The task could be anything from
make_a_coffee to launch_a_rocket. In your case it is a simple print function though.
Functions will be covered further in this book.
Scripting Way
One of the main principles of software engineering is reusability.
Let’s make the code that was just tested in the interpreter reusable.
To do so, create a file called “hello_world.py” and type the following text inside:
1. print(“Hello World!”)
What you placed into the file is exactly the line that was previously executed via the
interpreter.
Now lets execute the script that you wrote.
Open the command line, navigate to the location where the script resides and type the
following: python hello_world.py. After you hit enter you shall see that a “Hello World!”
text was placed on the screen.
Congratulations!
You’ve managed to write your first Python program and successfully launch it.
Comments
Post a Comment