We will start our Python journey by creating a programming classic - the "Hello world!" program which simply displays Helo world!
on the screen.
Run idle and type the following command and then press Enter:
print("Hello world");
We are using a built-in function print
whose job is to display the desired text (in our example Hello world) in the console (a.k.a. shell - a program that understands and carries out Python commands).
You can see that Python shell (the language interpreter) highlights different words in different colors:
Let's create a program that will ask the user for their name and then remember it somewhere, so it can display it afterwards:
name=input("What is your name?");
print(name);
In the first command, we created a variable called 'name
' and asked the computer to save in it whatever the user types in response to the "What is your name?" prompt. We prompt the user to type something by using the input function:
input('question to ask')
;
If I only did this, the computer would ask the user for their name, but wouldn't save the user's response anywhere. Therefore, we need to save it in some variable, e.g.
name=input('What is your name?');
Let's improve our program and ask the user for their name. For this, we will need to prompt the user to input their name and then print the greeting.
name=input("What is your name?");
print("Welcome "+name);
The output will now look like this:
You can notice that the console printed "Welcome Matej
".
It combined two different bits of information and displayed it together as one message:
name
variable (color-coded black) which in our program contained the string "Matej
".A variable is a place allocated in the computer memory that can hold some data - a number (integer, binary, double), text, a boolean value (true/false) and many other data types.
We used the plus sign +
to join the two parts of the message together; this is called concatenation in the world of programming.
So far, we have been using the shell where you can input one command at a time and then press Enter, after which the line of code gets executed right away. For really simple programs, this is OK, but for longer programs, this may be very unpractical. Therefore, most programmers use a text editor, which allows them to write the program, save it and then run it in its entirety.
To start editing your first Python file, press CTRL+N
in the Python shell. This will launch the text editor where you can type multiple commands, one after another, and only when finished with the program in its entirety save it (CTRL+S
) and then run it (F5
). After I do so, the editor will call the shell and pass on the entire program to it to execute. The output will look something like this:
This is what the entire program looks like in the IDLE text editor:
A shell is a text-based command interpreter that listens to the user's input and then executes the command input by the user. The command can come with parameters. e.g. ipconfig /renew
in the Windows PowerShell renews the IP configuration for the machine. ipconfig
without any parameters only displays the current IP configuration.
Common shells include:
Experienced programmers and IT technicians typically work in a shell as opposed to a GUI (graphical user interface).
Here are some interesting Windows Command Prompt commands that work in PowerShell as well:
tracert URL
traces the route it takes to reach a given websitenetstat
shows the active TCP connections ping URL
lets you test the connection to a given URLsysteminfo
displays information about the computernet use
shows a list of network drives connected to this computerdriverquery
shows all the drivers installedAll commands can be found here.