# Python Programming II

Λογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 4 χρόνια και 6 μήνες)

118 εμφανίσεις

Introduction to

Computational Linguistics

Programming II

Resum
é

calculator mode

arithmetic operators, simple and
complex arithmetic expressions

saving, checking and running programs

Exercise 2.1

Write a program that

gets 2 string variables and 2 integer
variables from the user,

concatenates (joins them together with
no spaces) and displays the strings,

then multiplies the two numbers on a
new line.

s1 = raw_input("give me string 1 ")

s2 = raw_input("give me string 2 ")

n1 = input("give me number 1 ")

n2 = input("give me number 2 ")

print s1+ " " + s2, n1+n2

while loop

while <condition> : <statements>

i=0

while (i<10) :

print i

i = i+1

Notice that indentation is used to group
items together

Exercise 2.2

Modify the last program so that it prints
the sum of all the numbers.

Exercise 2.2

sum=0

i=0

while (i<10) :

print i

i = i+1

sum = sum+i

print sum

range
([
start,
]

stop
[
, step
])

Creates lists containing arithmetic
progressions

most often used in for loops.

If the
step

argument is omitted, it defaults to
1.

If the
start

argument is omitted, it defaults to
0.

Observe the behaviour of the range function

Use of range function

>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> range(0, 30, 5) [0, 5, 10, 15, 20, 25]

>>> range(0, 10, 3) [0, 3, 6, 9]

>>> range(0,
-
10,
-
1)

[0,
-
1,
-
2,
-
3,
-
4,
-
5,
-
6,
-
7,
-
8,
-
9]

>>> range(0) []

>>> range(1, 0) []

for loop

Basic shape of for statement is this

for <variable> in <list>: do something

Examples

for i in [1,2,3] : print i

for i in range(1,4): print i

for i in ["comp", "ling"]: print i

for i in ['comp', 'ling']: print i

Exercise 2.3

calculate and print the sum of numbers
in range(5,20).

Strings

Besides numbers, Python can also manipulate strings,
which can be expressed in several ways.

They can be enclosed in single quotes or double
quotes:

>>> 'spam eggs‘

'spam eggs'

>>> 'doesn
\
't‘

"doesn't"

>>> "doesn't“

"doesn't"

>>> ‘ "Yes," he said.'

‘ "Yes," he said.'

Strings

We can get at individual characters of a
string using subscript notation

>>> s = 'dog'

>>>s[0]

'd'

>>>s[1]

'o'

>>>s[2]

'g'

Strings

Strings can be concatenated (glued
together) with the + operator, and
repeated with *:

>>> word = 'Help' + 'A'

>>> word 'HelpA'

>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'

Slice notation I

Slice notation is two indices separated by a
colon, and selects that part of a string which
begins with the first index and which finishes
just before the second

>>> s = 'dog'

>>> s[0:1]

'd'

>>> s[0:2]

'do'

Slice notation II

Slice indices have useful defaults

an omitted first index defaults to zero

an omitted second index defaults to the
size of the string being sliced.

>>> s[:2] 'do'

>>> word[2:] 'g'

>>> word[0:] 'dog'

More Data Types

We have seen numbers and strings.

These are different types of data.

Each kind of data has characteristic
operations.

Now we look at lists.

A list is a compound data type which is used
to group other data types together.

example: range(3) = [0,1,2]

Lists

The
list
can

be written as a list of comma
-
separated values (items) between square
brackets.

List items need not all have the same type.

>>> a = ['spam', 'eggs', 100, 1234]

>>> a ['spam', 'eggs', 100, 1234]

Like string indices, list indices start at 0, and
lists can be sliced, concatenated and so on:

Lists

Lists can be sliced, concatenated and so on:

>>> a[0] 'spam'

>>> a[3] 1234

>>> a[
-
2] 100

>>> a[1:
-
1] ['eggs', 100]

>>> a[:2] + ['bacon', 2*2]

['spam', 'eggs', 'bacon', 4]

>>> 3*a[:3] + ['Boe!']

['spam', 'eggs', 100, 'spam', 'eggs', 100,
'spam', 'eggs', 100, 'Boe!']

Lists can be changed

Unlike strings, which are
immutable
, it
is possible to change individual
elements of a list

>>> a[2] = a[2] + 23

Assignment to slices is also possible,
and this can even change the size of
the list

Changing Lists

# Replace some items:

>>>

a[0:2] = [1, 12]

>>> a

[1, 12, 123, 1234]

# Remove some:

>>>a[0:2] = []

>>> a

[123, 1234]

# Insert some:

>>>a[1:1] = ['bletch', 'xyzzy']

>>> a [123, 'bletch', 'xyzzy', 1234]

The built
-
in function len( )

This function computes the number of
elements in a list

>>> a = [1,2,3]

>>> len(a)

8

>>>len([1,1,1,1,1,1])

6

Nesting Lists

It is possible to nest lists (create lists
containing other lists), for example:
>>> q = [2, 3]

>>> p = [1, q, 4]

>>> p

[1,[2,3],4]