Getting Started with Python

Writing your first program in Python, or any language, can be a bit intimidating because you are usually starting from a blank canvas. In this topic, we will cover some basic Python programming, including how to set up your files, creating and assigning data to variables, printing information to the console, and some basic mathematical operators.

Programming Environment

Python is an interpreter, meaning that it will execute lines code from top to bottom, though this doesn’t mean your code will run in order from top to bottom. For instance, you may define a function near the top of your code to be used later on near the bottom of your code. But that does mean if you want to use a function, module, etc., Python needs to know about it before it is called. Some programming languages read through all of the code before it is executed, so where things are placed matters less, but in Python it matters!

Code that we write in this class will generally follow this outline:

  • Author block – Some general information about the file goes here, such as the author, date created, and a short description of the file/project
  • Import section – Code from other libraries or files can be imported so that you can have access to a wider array of functions and tools. Most, if not all, of the imports that we will use in this class are prepackaged with Python. You may be wondering why aren’t all of these additional functions available to us if they come with Python. Programmers generally want just the bare necessities to keep their programs lightweight. Including lots of functions and tools that aren’t needed for a particular project only slows down your code.
  • User-defined functions and classes – Throughout this course, you will write your functions to solve problems and challenges. This is where the bulk of your work will take place. Classes are collections of functions (also known as methods) and attributes that are related. We will explore these much later in the semester.
  • Main function – The main controller of your program. It will call your user defined functions and handle the flow of your program.
  • The call to the main function – A.K.A. the start button

Here is an example:

# Author: Prof. Jason Grant, Villanova University
# Date created: 8/23/2022
# Getting Started with Python

# This program does not use any imported modules, but 
# any were needed, they would go under this line

# This is a user-defined function
def foo():
  This is called a doc-string. It provides
  information about the function I am writing.
  Notice that is is enclosed in triple quotes.

  print("Let's get started with Python!")

def main():
  # Here, I am calling the sample function I created above.

# This calls the main function. We'll spend a little more
# time understanding this in another section.
if __name__ == "__main__":
Program Output

One of the most useful tools in any programming language is learning how to output or display information. The print function in Python allows us to display information to the console. Here is an example:

print("Hello, world!")

Print statements can be used to display text, the data stored in variables, or the combination of both!

Spacing and Style Guides

Python uses spacing to denote scope and membership between elements in code. Take a look at the sample code provided earlier. Notice how all of the elements inside the function foo are spaced the same from the left side of the page. This spacing lets the interpreter know that all of these elements should be contained within the function. If your spacing doesn’t line up correctly, you’ll likely get an error that looks like this:

IndentationError: unexpected indent

While correct spacing is a requirement, there are several stylistic guidelines that programmers try to follow to help make their code readable to other users. Those guidelines can be found under the Python Enhancement Proposals, otherwise known as PEP. Throughout the semester, I will encourage you to write code following the rules of the PEP-8 style guide.