01: Python Basics Notebook#

Introduction#

This notebook is designed to introduce the basics of Python programming and the concept of Jupyter Notebooks. It covers fundamental concepts such as variables, data types, mathematical operations, and the use of external libraries like NumPy. The goal is to provide a hands-on learning experience for beginners to become familiar with Python syntax, essential programming practices, and how to use interactive notebooks for coding and documentation.

Topics covered in this notebook:

  • What is a Jupyter Notebook?

  • Variables and naming conventions

  • Data types (integers, floats, strings)

  • Comments and best practices

  • Mathematical operations (addition, subtraction, multiplication, division, exponentiation, floor division, modulo)

  • Scientific notation

  • String handling

  • Case sensitivity in variable names

  • Using external libraries (NumPy)

  • Printing and displaying output in Jupyter Notebook

What is a Jupyter Notebook?#

This document is a Jupyter Notebook. A Jupyter Notebook is an interactive computing environment that allows you to write and run code in small sections called “cells.” You can mix code, text, equations, and visualizations in a single document. This makes it a powerful tool for learning, experimenting, and sharing code, especially in data science, scientific computing, and education.

  • Code cells let you write and execute code (like Python).

  • Markdown cells let you write formatted text, explanations, and notes.

  • You can run cells in any order and see the output immediately below each cell.

Jupyter Notebooks are widely used because they make it easy to document your workflow, explain your thought process, and share your work with others.

# Comments in Python:
# Any text following a '#' symbol is ignored by Python and is not executed.
# Comments are used to explain code, make notes, or temporarily disable code.

# You can use a single '#' or multiple '###' for emphasis or sectioning, but only one is needed.

# Best Practice: Write clear, concise comments to explain your code.
# Good comments help others (and your future self) understand your logic.

# Example of a comment:
# This line adds two numbers:
sum_result = 2 + 3  # sum_result will be 5
print(sum_result)  # Output the result
5
# Best Practice: Place all import statements for external modules (like numpy) at the very beginning of your notebook or script.
# This makes it easier to see which libraries your code depends on and avoids repeated imports.

# Importing external modules:
import numpy as np  # 'np' is a common alias for numpy

# Now you can use numpy functions with the 'np.' prefix, e.g., np.sum([1, 2, 3])
## An example of how I might use numpy:
np.sum([2,3,4])
9

Let’s talk about variable names, what’s valid and invalid.

Let’s make our first variable

myfirstvariable2022 = 2022

Note: the above did not print 2022 to the screen, because we were just setting a variable.

If I want to print the value of that variable to the screen, I can do that two ways in jupyter:

  1. type variable name

  2. use print function in python

myfirstvariable2022
2022
print(myfirstvariable2022)
2022

To convince ourselves that python is case sensitive:

Myfirstvariable2022 = 3
print(Myfirstvariable2022)
3
2022myfirstvariable = 3
  Cell In[40], line 1
    2022myfirstvariable = 3
       ^
SyntaxError: invalid decimal literal

So far, we’ve been setting our variables equal to integers.

However, we can set them equal to many different things.

Some examples:

sciexp = 4e6
print(sciexp)
4000000.0
sciexp2 = 1e-3
print(sciexp2)
0.001

Scientific notation is one example, and we use “e” to represent x10

## Strings:
## We can set variables equal to a string - words/letters
stringvariable = 'redapple'
print(stringvariable)
redapple
##double quotes work as well to define a string:
stringvariable2 = "redapple"
print(stringvariable2)
redapple
stringvariable3 = redapple
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[13], line 1
----> 1 stringvariable3 = redapple

NameError: name 'redapple' is not defined

above there are no quotes, which we need to define a string in this case because we are setting the variable equal to letters, it thinks those letters are another variable which isn’t defined.

## if we had defined redapple, it would set stringvariable3 equal to the value of the variable redapple
redapple = 3
stringvariable3 = redapple
print(stringvariable3)
3

Other python Basics#

## How do we add?
2+2
4
## Subtraction:
2-2
0
## Multiply and divide, * and / respectively:
2*2
4
2/2
1.0
## To the power of?
2**3
8
## We need numpy for things like logs and exponentials:
np.log10(9) ## this is using numpy's function log10, which log base 10
0.9542425094393249
## natural log is np.log (they're different here)
np.log(9)
2.1972245773362196
## Exponentials, e
np.exp(1)
2.718281828459045
## We can also combine these:

np.log(np.exp(1))   # gives me ln(e**1)
1.0
## Floor division:
## Rounds down the division:

## We will use this later on for peasant multiplication:
np.floor(26/5)
5.0
## the second way doesn't need numpy, //
26//5
5

Python follows PEMDAS!#

Test it out..

2+3**2*2
20
(3+7)**2-8*2
84
## Just because it follows PEMDAS, doesn't mean it's easy to read code.
pemdas_test1 = 2+3**2-(4+2)*2

## This should be -1: 2+3**2 - (6)*2 = 2+9-12
print(pemdas_test1)
-1
## More readable sometimes to add more parentheses
pemdas_test2 = 2+(3**2)-((4+2)*2)
print(pemdas_test2)
-1

One more operation, mod:

## modulo, or mod is %

## so 5 mod 2: 5%2
5%2
1
## Can also use "mod" in numpy:
np.mod(5,2)
1

Summary of Concepts Learned#

In this notebook, we covered the following Python basics:

  • Introduction to Jupyter Notebooks:

    • What a Jupyter Notebook is and how it allows you to write and run code in interactive cells, mix code with formatted text, and document your workflow.

  • Variables and Naming Conventions:

    • How to create variables with valid names (e.g., myfirstvariable2022, Myfirstvariable2022).

    • Python is case sensitive (myfirstvariable2022Myfirstvariable2022).

    • Variable names cannot start with a number.

  • Data Types:

    • Integers (e.g., 2022, 3)

    • Floats and scientific notation (e.g., 4e6, 1e-3)

    • Strings (e.g., 'redapple', "redapple")

  • Comments:

    • Using # to add comments and explain code.

  • Printing Output:

    • Displaying variable values by typing the variable name or using the print() function.

  • Mathematical Operations:

    • Addition (+), subtraction (-), multiplication (*), division (/)

    • Exponentiation (**)

    • Floor division (//)

    • Modulo (%)

  • Scientific Notation:

    • Using e to represent powers of 10 (e.g., 4e6 for 4,000,000).

  • Using External Libraries:

    • Importing and using NumPy for advanced math functions (e.g., np.sum, np.log, np.exp, np.mod).

  • Order of Operations:

    • Python follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).

  • Error Examples:

    • Assigning a variable to an undefined name (e.g., stringvariable3 = redapple before redapple is defined).

This notebook provided hands-on examples and explanations to help you understand and practice these foundational Python programming concepts.