0% found this document useful (0 votes)
98 views23 pages

Seng 265: - Attendance - Introduction To Python

it talks about basics of pythonwhich was taught in software development methods course in fall 2014 at uvic

Uploaded by

SaadMalik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views23 pages

Seng 265: - Attendance - Introduction To Python

it talks about basics of pythonwhich was taught in software development methods course in fall 2014 at uvic

Uploaded by

SaadMalik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Seng 265

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

NOTE: The definition interpreted is muddied


since the use of VMs to convert code to byte-
code (like Java and the JVM!).

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:

>>> print (start)


>>> greet = hello
>>> whom = world
>>> print (greet, whom)
>>> print(type(greet))
>>> print(len(greet))

8
Python:
Note: The terminal program lets us quickly
test Python constructs. When the values
were assigned, they are remembered
until we exit

A batch program is the same when stored


in a file name <progname>.py and
executed with $python3 progname.py)

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:

>>> list1 = [name, Testy


McTesterson, age, 25]
10
Python: Lists
To print the list, we just have to call the
print command:

>>> print (list1)


>>> print(len(list1))
>>> print(list1[2])
>>> list1[2] = A
>>> print(list1)

11
Python: Lists
Iterating through the list.
Python has many built-in functions

>>> for item in myList:


print item

NOTE: The colon marks an enclosing


structure. At each level of enclosure you
must indent. Indentation marks blocks

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()

>>> print (list1)

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 []

>>> t = (test, 1, 2, 3, liftoff)


>>> print t
('test', 1, 2, 3, 'liftoff')

16
Python: Tuples
Tuples are immutable (they cant be
changed) but we can combine tuples
(Continuing from previous statements):

>>> t2 = ('more', 9, 8, 7, 'less')


>>> t3 = t + t2
>>> print t3
('test', 1, 2, 3, 'liftoff', 'more', 9, 8, 7, 'less')

17
Python: Tuples
Why would we want to use something that
works like restricted lists?

Because you can use tuples for operations


such as returning multiple variables from a
function and they are very light-weight
compared to lists

18
Python: Tuples
You can make tuples (or lists) quite easily
using the following:

>>> test = Testy


>>> list(test)
['T', 'e', 's', 't', 'y']
>>> tuple(test)
('T', 'e', 's', 't', 'y')

19
Python: Tuples
One more thing
You can do this in Python:

>>> var1, var2 = (1, 2)


>>> var1
1
>>> var2
2
* So you can do this var1, var2 = foo()
If foo() returns 2 variables in a tuple

20
Python: Strings
Strings are nice in Python

>>> name = Testy

There are lots of functions to use (Google


python strings)

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

You might also like