Theory
In this tutorial we’re going to use the terminal in order to interact with our program.
You should be already familiar with Graphical User Interface (GUI) as you see it everywhere in the modern world:
- It’s the graphical interface of your OS whether it’s Windows or Mac
- In a browser
- In a website
- etc
Terminal is also an interface to interact with your computer, but it’s text based. It allows us to input data or output data in text format.
Let’s now learn how to output some data with Python and write our first program.
In Python it’s very simple, for that we use the print()
statement.
As you might have guessed, the print command prints something. But don’t worry, it doesn’t require a physical printer to do its job, instead it’s going to print text in your Terminal.
Example #1
Let’s write the first program:
- Open the VS Code.
- Navigate to File -> Open Folder.
- In your documents, create a new folder with name
code
and open it. - On the right hand side click to Explorer
- Right click on the empty space and select “New File”
- Give the file a name, for example “test.py”
- In the editor type the text below
- Click to the Run Python File button on the right top corner.
- The result will be in the bottom part of VS Code - this is Terminal.
1
print('hello world')
Python Print function example visualized
Note, that the text data in Python is called string and it’s always surrounded by quotes.
Congratulations, you wrote your first Python program
Example #2
More about print()
:
- The
print
statement we used is in fact a function - You can execute a function by adding a pair of parenthesis
()
to it like thisprint()
- Functions can take input values which are called arguments
- The arguments are always inserted inside the parenthesis
- We’re going to learn more about the functions later in this tutorial
- The
print
function can be used to print almost any type of data in Python: text, numbers, objects and so on - If you just want to print a new line with no text simply use it with no arguments like this
print()
- The print function in fact can take multiple arguments or values, you just need to separate them by
,
Example of print function with two arguments:
1
print('hello world', "hello universe")
Python Print function example #2 visualized
Note that we used single quotes to surround 'hello world'
and double quotes in "hello universe"
.
In Python you can use either single or double quotes, but remember to put the same type of quotes at the beginning and the end of a string.
Practice
Since we’re going to create a calculator, we need to print results on the screen. Let’s now pretend we have already calculated them.
- The results are the numbers 77, 11 and 17
- For each number print ‘the result is: ‘ and then the number
- Make sure to supply two arguments to the
print
function: first is text and the second is the number
Solution
1
2
3
print('the result is: ', 77)
print('the result is: ', 11)
print('the result is: ', 17)