1.1. Your First Python Script#
So, it is time for your first Python script. It is located beneath.
# My First Python Script
message = 'Hello world!'
print(message)
Hello world!
Let’s break it down
First line: # My First Python Script
is a comment, which is used just to explain and/or put useful information about the code next to it. If you need to create a comment — just type a #
symbol and write your text after it. The interpreter does nothing with it — the comments are there just as useful information for you or another reader.
Second line: message = ‘Hello world!’
creates a variable called message and assigns the text literal (string) Hello world! to it, by using the operator = (equal sign). Variables are used to store all data you use in your code.
Third line: print(message)
calls the functionprint()
and passes the variable message to it. A function is just a set of encapsulated code, which is tailored to perform a certain action. This specific function outputs the content of everything you pass to it. Since the variable message had a small line of text in it, the function print()
outputs that message.
This script is quite primitive and simple but it represents the main idea of programming in Python. You have data (which is stored in variables) and you perform an operation on it: by using the inbuilt print()
function. In addition, you could create your own functions.