Attendance
Introduction to Python
1
This Week
Python
Command Line mode and Batch mode
Python is:
Interpreted
Dynamically Typed?
2
Python: Interpreted
Interpreted means that the programming
language is not really compiled like it is in C
but is interpreted by some other medium.
Think scripting language
3
Python: Typed?
Typed dynamically AND strongly?
But how??
Python is strongly-typed as the interpreter
keeps track of all variable types. Its also
dynamic as a variable type can be
changed on the fly by assignment, (use
care)
4
Python: Powerful
Very good at string parsing
Good glue language
(Used to glue modules and systems
together)
Object-oriented! (Classes! etc)
Portable!
Easy to use!
5
Python: In The Terminal
Open up a terminal
Create a new test directory called week06
and cd into it.
6
Python In The Terminal
You can open the Python interpreter in the
terminal by typing python3
$ python3
Some text will appear and then you will
see the prompt:
>>>
7
Python: Practice
You are now using the interpreter.
Type:
8
Python:
Note: The terminal program lets us quickly
test Python constructs. When the values
were assigned, they are remembered
until we exit
9
Python: Lists
Lists in Python are arguably the most
useful structure.
A Python list is simply a list of comma-
separated items
They DO NOT have to be the same type!
Type:
11
Python: Lists
Iterating through the list.
Python has many built-in functions
12
Python: Side Note
Tabs are fine although you will find that
many Python examples use spaces. 4
spaces per indent seems to be common.
Functions, selection statements, loops and
all enclosing structures will use a colon
and require an indent.
Since end of line is a semicolon and indent
marks a block errors of indent are
common.
13
Python: Lists Continued
We have seen that Lists can be accessed
using indices and insertion can use an
index.
Experiment with sub lists
>>> print (list1[1:])
>>> print (list1[:2])
>>> print (list1[2:3])
>>> print (list1.pop(2))
>>> print (list1)
14
Python: Lists
There are a variety of other functions you
can call on lists
Here are a few:
>>> list1.append(newstring)
>>> list1.sort()
>>> list1.reverse()
15
Python: Tuples
Tuples are the immutable equivalent of lists
Immutable means that they CANNOT be
changed
Tuples use the round brackets () instead of
square ones []
16
Python: Tuples
Tuples are immutable (they cant be
changed) but we can combine tuples
(Continuing from previous statements):
17
Python: Tuples
Why would we want to use something that
works like restricted lists?
18
Python: Tuples
You can make tuples (or lists) quite easily
using the following:
19
Python: Tuples
One more thing
You can do this in Python:
20
Python: Strings
Strings are nice in Python
21
Python: Strings
You can use the str() function to convert a
variable to a string:
>>> a = 100
>>> print(type(a))
>>> str(a)
>>> print(type(a))
22
A Batch Program
See batch code example in this weeks
folder: makehtml.py uses testfile.csv
Take batch program examine test and run.
EX 1. Make input a tabbed delimited file.
EX 2. Put in table header row and take out
the alternating color.
EX 3. Create a method that is able to add
rows before output.
23