1.1 First Python Script

1.1 First Python Script#

So, it is time for your first Python script. It is located beneath. Don’t forget to hover over the rocket icon () at the top right and click on Live Code. Now go ahead, run it: select the cell by clicking on it, then click the ’run cell’ button or press shift + enter.

# My First Python Script

message = 'Hello world!'
print(message)

Now, let’s analyze it.

First line: # My First Python Script is a comment, which is used 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 string Hello world! to it, by using the operator = (equal sign). Variables store all data you use in your code. When you assign text to a variable, enclose the text in single ’ ‘ or double ” “ quotes.

Third line: print(message) calls the function print() 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 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.