Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python Basics

Here are some quick examples to refresh your understanding of Python code.

This is not meant to be a comprehensive tutorial.

We encourage you to explore ProjectPythia for additional Python resources and tutorials.

Please run these examples demonstrated today yourself!

  • Press Shift+Enter to execute each code cell!

  • Feel free to make changes and explore!

Starting off - Hello World!

Loops

  • for loops are a staple of scientifc programming.

Things to Note:

  • Python defaults to counting starting from 0 rather than 1.

  • Function calls (such as range) always use parentheses.

  • for loops require colons as punctuation to start.

  • Code blocks are indicated through indentations.

    • NOTE indentations need to be similar for each block of code.

Program Control - Conditional Statements

For logical decisions, Python has an if statement

QUESTION Why is i greater than 2? What value is i? If you do not know, how could you determine the value?

Python also defines True and False logical conditions

Python also includes compound statements and and or

Things to Note:

  • if statements can also use less than or equal to notation <=

  • The and statement uses formatted output

Basic Python Data Types

A key strength of Python are the flexible data types, the core of which are noted below

QUESTION What data type is i?

Floating Point Numbers float

Things to Note:

  • print statements can take multiple inputs

  • float numbers are include scientific notation

Character strings str

As we have been doing, you can use either single quotes '' or double quotes "" to denote strings

Lists

  • A ordered container of objects denoted by square brackets

  • Note that objects can be any data type

Things to Note:

  • you can also iterate through lists

  • lists do not need to contain the same type of data!

  • Since Lists are ordered we can access items by index

What Now?

  • Now that the Python basics have been reintroduced, let’s take a look a specific libraries that are useful in scientific applications

Matplotlib - Scientific Data Visualization

From the Matplotlib Documentation:

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.

  • Create publication quality plots.

  • Make interactive figures that can zoom, pan, update.

  • Cus tomize visual style and layout.

  • Export to many file formats.

  • Embed in JupyterLab and Graphical User Interfaces.

  • Use a rich array of third-party packages built on Matplotlib.

Additional Tutorials: