Physics 212, 2017: Lecture 2
Back to the main Teaching page.
Most important thing to note about these lectures:
- You won't learn Python by reading. You have to try coding. If you have a question how a certain expression works, type it in Python console, and test it!
Introduction to Python
- Algorithmic thinking
- Example: opening a door
- Different levels of algorithms
- Basic parts needed to design an algorithm
- State (or memory)
- Rules for transforming states (operations, functions, procedures)
- Rules for making decisions
- Assignment operation (vs. testing for equality)
- Variable is a pointer to a container (object) in memory where a state is stored. (This is not quite correct; we will return to this later).
- Anaconda distribution / Spyder development environment
- How to launch it
- Console vs. editor plus additional tools
- Syntax highlighting / code analyzer
- Basic syntaxes
- Resetting the state of the system
- Asking for help with ? and with Google
- Built-in functions, like print
- Numbers; note that j is the imaginary unit, not i
- Are numbers real or integer?
- Arithmetic operations, +, -, *, /, **.
- Importing modules and functions from modules
- numpy as np and matplotlib.pyplot as plt
We will solve a quadratic equation a*x**2+b*x+c=0 for a=1, b=2, c=-3. Do this by
- Resetting the environment
- Importing sqrt function from numpy
- Assigning values to a, b, c
- Evaluating both solutions using the standard formula you learned in middle school
- Printing both solutions
- More syntaxes
- Functions have arguments; keyword arguments
- Functions return values
- Functions can otherwise change Python state
Evaluate a binary log (log base 2) of a number 42 using at least three different sets of commands. Verify your result by taking 2 to the appropriate power and seeing if you get 42 back.
Don't forget to submit your work at the end of the class.