Hello World
This lesson will introduce learners to Python where they will write their first program.
We'll cover the following
In the previous chapter, we saw why we should learn Python if we are interested in data science. Let’s get straight to writing our first program in Python.
Syntax of a language
When we speak or write something in English, we follow certain rules and regulations of the English language. In the same way, there are certain rules that we must follow to write programs in a programming language. The spelling and grammar rules and regulations of a programming language are called syntax. Python has a very simple syntax that is human-readable which makes it easy for us to make the computer accomplish what we want it to.
print
statement
We will start by learning how to display text and numbers on our screen. Every language has a different way of displaying or showing things to the user. Python does this with a print
statement. The syntax is simple; you write the keyword print
, followed by a parenthesis. In the parenthesis, you write whatever you want it to display.
print(data)
We can write any data we want to be displayed inside the parenthesis. Let’s see an example below. If you press the RUN
button below, you will see Hello World written on the screen.
print('Hello World')
In the above example, we wrote our Hello World inside single quotes because it is text. Any text that we want to be printed should be written in quotes. You can use both single quotes and double quotes in Python. But what about numbers?
print(77)
If you run the code now, you will see printed on the screen. For numbers, we do not have to enclose them in quotes. This was easy. But what if we want to print more numbers and text?
print('Hello World')print(77)print("This was easy")
Now when we run the code, we can see the three different statements printed on 3 lines. Each time we use print
, the output moves to the next line. We can also print multiple numbers and texts in the same line by separating them with commas.
print("I am writing this in",2019,'at Educative')
When we run the code, we can see that everything is printed on the same line.
Comments
A very handy feature that all programming languages provide is commenting. We can add comments that explain things alongside our code so if anyone sees our code later, they know what the code is trying to accomplish. In Python, we can add a comment with the #
character. Everything following the #
character is treated as a comment instead of code.
print("Hello World") # This line will display Hello World# The code below will display the yearprint(2019)
As we see above, the comments did not affect our code whatsoever. But they are of great help when writing code.
This brings us to the end of this lesson. We have written our first program in Python. In the next lesson, we will cover two fundamental concepts, variables and data types.