Introduction to Python
davidbpython.com
Your New Partner: the Python Interpreter
what do computers do?
Computers can do many different things for us.
Think about what our computers do for us:
- perform numeric calculations
- analyze, gather, compose, edit text
- move files around, search files
- download web pages
- send email
- process image or sound files
- play videos and music
- display images
- operate storage devices to save files
- operate hardware like printers, light switches, automobiles, drones, etc.
what do computers really do?
At base, computers really only do three things.
- store data in memory and perform calculations
- send messages over a network
- operate devices
Python can do many things, but we will focus on the first item -- working with data.
The main purpose of any programming language is to allow us to store data in memory and then process that data according to our needs.
programming languages
A programming language like Python is designed to allow us to give instructions to our computer.
- your computer understands "machine language", which is a "lower level" programming language
- however, machine code is challenging to write
- "high level" programming languages were devised to make it easier to communicate with a machine
- Python, Java, C, C++, JavaScript, php, Ruby, and C# are all "general purpose" languages
- languages like SQL, HTML, CSS, etc. are "domain specific" languages, designed for a specific purpose
the Python Interpreter
The Interpreter is Python Itself.
- the Interpreter is the program that processes our Python code
- it is what we mean when we use action words: "Python runs the program"; "Python prints to the screen"; "Python raises an error" -- all of these refer to the Interpreter
- when we run a Python program, the Interpreter reads our Python code and translates it into machine instructions
- as it is translating "Python" into "Machine", this is why we call it an Interpreter
- we should think of the Interpreter as our new coding partner. It will execute our code, and tell us when and how we have made an error
- the true purpose of any study of Python is to understand the Interpreter
evaluate - compile - run
When we run a python program, the Interpeter takes these three steps.
- first, the Interpreter reads the Python code stored in a file
- it validates the code, checking for errors in syntax
- (a SyntaxError occurs when our code is missing or misplacing elements, like a missing period at the end of a sentence.)
- code syntax must be perfect for the code to run
- after validating the code, the Interpreter converts it into bytecode
- it then executes ("runs") the bytecode, statement by statement until complete
what the interpreter can do
Python is very smart in some ways.
- execute code very quickly, sometimes instantly
- execute any valid instruction
- allocate as much memory as we need for a task
- tell us immediately when something goes wrong
what the interpreter can't do
Python is not smart in some ways, too!
- understand what we're trying to do or what our program means
- tell us when we're doing something inefficiently or incorrectly
- explain every error to us or explain exactly what went wrong
- tell us what we need to do to fix errors
how to respond to exceptions (errors)
We should seek to understand what the Interpreter is telling us.
- most of us think of errors only as problems
- when something goes wrong, our first instinct is to just fix the error any way we can and move on
- however, exceptions are learning opportunities
This learning is not just about making programs work -- it's about understanding the interpreter -- what it can and can't do.
[pr]