Difference between revisions of "Physics 212, 2018: Lecture 2"
nemenman>Ilya (→Introduction to Python) |
m (1 revision imported) |
(No difference)
|
Latest revision as of 11:28, 4 July 2018
Back to the main Teaching page.
Back to Physics 212, 2018: Computational Modeling.
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, +, -, *, /, **.
- Parentheses
- Importing modules and functions from modules
- numpy as np and matplotlib.pyplot as plt
- Python Modules
- Importing and reloading
- pyplot and bumpy
- Exercise
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
- Exercise
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.