Python Programming Overview 4
Python Programming Overview 4
(1) Running The Interpreter
The Python Interpreter
- Python ships with an interactive interpreter
- This tool will be your best friend as you learn the language
- Provides instant feedback and introspection capabilities
Running the Interpreter
- On a Linux (or Mac OS X) machine, it’s as simple as:
- Launch your favorite terminal application
- Type python at the command prompt
- Press Enter
- To exit the interpreter, either type exit() and press Enter, or just press Ctrl+d
Gotchas
- Depending on your $PATH environment variable, you may need to specify the absolute path to the python binary
- Or, add the directory that the python interpreter lives in to your $PATH
- You can modify your shell’s startup file to have this happen every time you log in if needed
Exercises
- On your system, determine where the Python interpreter lives
- Execute the Python interpreter
- Type “help()” and press Enter
- Type “keywords” and press Enter
- Read the help for three keywords of your choosing
- When finished, press Ctrl+D twice to leave the interpreter
(2) Running code inside the Interpreter
It Really Is Interactive!
- Running code in the interpreter is only slightly different than typing it in a text editor before running it
- The interpreter feedback is immediate – if you make a mistake, it will tell you what you did wrong
Prompts
- The interpreter has two prompts:
- >>>
- Primary prompt
- …
- Secondary prompt, is used for continuation lines, namely blocks of code; use of the Tab key is required here as well
- >>>
Instant Feedback
- Statements and expressions are executed or evaluated as soon as the Enter key is pressed
- And again, if there are any errors, you will know about them, and usually why they happened
Exercises
- Run the Python interpreter
- Type the following and hit Enter: “import sys”
- Type sys.ps1 = ‘C:\> ‘ and hit Enter
- Assign the string ‘foo’ to the variable ‘bar’
- Press Ctrl+D to leave the interpreter
(3) Learning With The Interpreter
Introspection
- In Python, everything is an object
- Python can introspect and look at other modules, functions, etc. in memory as objects
- This translates into using the interpreter to get information about how a library works!
Some Useful Introspection Functions
- help()
- Displays help on built-ion Python keywords
- dir()
- Displays a list of object attributes and methods
- The .__doc__ method
- Displays doc strings for functions or methods (use with print to get the formatting right)
- Type()
- Displays the type of the object queried
- id()
- Displays the unique indentifier of the object
Using Introspection
- Introspection functions like “dir()” and “callable()” allow you to “feel out” how a library works without necessarily needing to read the documentation
- Combined with the immediate feedback of the interpreter, this is a very powerful advantage of Python
Methods and Attributes
- Method: something that a thing does. Conceptually it is no different with function.
- You call it, you can pass parameter, and it return you something
- Attributes: Something that a thing has (a.k.a variable). It can be string, integer, etc.
- Generally you want to ignore methods and attributes that are in this form ‘__xx__’
- Except for ‘__doc__’
0 Comments