Loading video player…

Initial Script Setup and Statements

Here’s a code snippet showing the example code used in this lesson:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# One statement per line


00:00 In this lesson, I want to talk to you about statements and about setting up your scripts in Python. Let’s start with the quicker one, which is that you’re supposed to put one statement per line in Python.

00:11 Here you see two calls to the print() function, and you see that they are in two separate lines. This is the best way of doing it in Python, but it’s also possible to put two statements in one line, so you could separate them with a semicolon (;) like this, and just say, <first statement>; <second statement>, and that works as well.

00:31 Let me run this to show you. So it prints out Hello World just like it does when you have them in two separate lines. But the more idiomatic way of writing Python code is to put each statement on its own line.

00:46 Now let’s look at script setup. So, these two lines here at the top—you might see them around when you see Python scripts, and you might be wondering what this is about.

00:56 So, the good news is that you probably are not going to have to deal with them very much, but it’s still interesting to know how they work and what this is about.

01:04 The first one is called a shebang statement. This instructs the operating system on how to run this script that you’re writing—specifically, which program to use.

01:15 The program that you’re defining here is the python3 program. If you add this shebang to the beginning of your script, you can do things like not needing to specify the program when you’re actually calling it. So here I say, “Use the python program to run this Python file,” but if you’re adding the shebang at the top, you don’t actually need to do this necessarily. In some newer versions of Windows, it’s already enough to just add this shebang at the top, and then you can just say, “Run this file,”

01:47 like that. In most Unix systems, you’re going to run into a permission error, which is what happened here. But you can still make it executable as well by changing the mode by saying chmod (change mode). Add the executable flag, and then give the name of the script.

02:05 After doing this, if you have the shebang at the top, you can execute it and again get the expected output without needing to specify that you want to run this with the python program because you have it specified up top here in the shebang.

02:22 The second line of code is a little outdated, and you’re probably not going to need it anymore, but this defines the encoding for using UTF-8. This is the default for Python 3, but if you are dealing with some legacy Python scripts, you might want to also declare the encoding up top, just to make sure that the interpreter knows how to encode the text that is in the script. Now you might… If you have a specific different encoding, you could also define it here.

02:50 You could say, “This file is encoded in ascii.” But generally, you’re going to want to have your scripts in UTF-8—oops—

02:59 and you won’t need to specify this if you’re running Python 3. But as The Zen of Python says, sometimes explicit is better than implicit, so now you know what these two setup code comments in Python mean and how you can use them.

03:16 That’s all for setting up your scripts and how to write statements in Python. In the next lesson, you’re going to learn some more about idiomatic Python code.

Become a Member to join the conversation.