Script Setup & Statements

Give Feedback

This lesson covers the best practices to follow when setting up a Python script. You saw that it is good idea to add a shebang statement at the top of your Python scripts. A shebang is useful when working with virtual environments that may contain third party applications.

You also learned how to specify the encoding of your script by adding this line to the top of your script:

# -*- coding: utf-8 -*-

This information is used by the Python parser to interpret the script using the given encoding. If no encoding is specified, ASCII is assumed. It is considered best practice to use UTF-8 since it has the added benefit of character support beyond ASCII-characters.

The last section of the lesson talks about the number of statements you should have on each line. Python supports writing multiple statements on a single line but this is considered bad practice. The pythonic way to write statements is to have each statement on its own line:

Bad

print 'Hello' ; print 'World'

if x == 1: print 'one'

Good

print 'Hello' 
print 'World' 

if x == 1:
    print 'one' 

Comments & Discussion

Become a Member to join the conversation.