Intro to Python
-
- Python is an interpreted language.
- It
- is object oriented, or not
- is dynamically typed
- has a huge support library
- runs on many platforms.
- is open source
- is fairly popular
- is named after Monty Python
- has a c/c++ api, so you can extend it.
- Python
- has dynamic garbage collection, no pointers or memory management.
- has nice built in types.
- looks very much like c/c++ on many instances.
- With python, and especially the libraries, it is possible to write
hundreds of lines of assembly code in a single line of python.
- Caution: I am a c/c++ programmer
- My code tends to look like it.
- You will find python people who code differently.
- Versions
- Python 2 is a legacy version of python.
- It had a strong usage base.
- It had many libraries, some of which have not been ported to python 3
- Python 3 was released in 2008
- Represents a major overhaul to the language.
- Is not completely backwardly compatible with python 2
- Most libraries have bee moved.
- This is the modern version.
- On mirkwood
- python2 and python3 exist
- python is a symbolic link to python2
- This is because most of the linux install stuff was in python 2
- I will let you know in a month what fedora 30 uses.
- I am a python2 person, but I am going to try to do this with python33.
- What do I do with python.
- I do a bunch of xml file processing to generate maps and statistics here
- I frequency do conversion stuff like the database example I gave you.
- I built a set of GUI interface control/data viz tools for my dissertation.
- I am currently building a Database Interface for my Miniature collection.
- Interact with file systems.
- Interact with database
- Display pictures
- Let's play a bit.
- Interacting with the interpreter.
- Just run python3
- CTRL-D will exit.
- This will do for now.
- Later we will move to files.
- I use the Library Reference constantly.
- I code in python with a book in my hands.
- Variables and basic types.
- True, False and None are defined.
- bool
- True/False as above.
- print(True and True)
- x = 3 > 4
- print(x)
- and, or not
- <, <=, >, >=, ==, !=
- is and is not test to see if two things are the same object.
- l = [1,2,3]
- m = l this is a shallow copy.
- n = l[:] this is a deep copy.
- l is m
- l is n
- l is not n
- type, id
- Type identifies the type of an object.
- ID returns the id of an object (an address I bet)
- try type (l), type(x), id(l), id(m), id(n), id(x)
- By the way, in the interpreter, you don't need print(xxx)
- Just the variable name will print the object.
- Numeric types: int, float, complex
- type(3)
- type(2.1)
- type (3+2j)
- int(4.999)
- float(3)
- +,-,*,/, //, %
- abs, **
- divmod(a,b) returns a pair (a//b, a%b)
- casts as above.
- strings
- in single or double quotes, either.
- '"Hello World"' is what you normally print.
- Can be indexed a[4], with 0 based indexing
- But can be indexed with negative numbers as well
- Ranges can be extracted a[1:3]
- Anything left empty means from the beginning
- a[3:] or a[:4]
- A third argument to this is a stride
- There are MANY string methods
- This is one of my reasons for reaching for python.
- strip, split, upper, lower, are among my favorites
- But I would look there before I did anything else.
- + is stringcat.