Download Python basics

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Scripting Languages
Python basics
–
lists & tuples
–
range
–
for-in loop
–
functions
–
exercise
Data type → list
Ordered collection of items
●
●
●
●
●
items can be referenced by [index] (starting from 0)
items can be of any type (including other lists)
can contain items of various types
can be modified (grow, shrink , item changes)
can be joined, sliced, compared
x = [item, item, item, …]
aa == [1,
[1, 2,
2, 3]
3]
bb == ['apple','orange']
['apple','orange']
cc == [[3,'red'],[5,'blue']]
[[3,'red'],[5,'blue']]
print
print a[0],
a[0], b[1],
b[1], c[1],
c[1], c[1][0]
c[1][0]
list cont.
Some operation on lists:
–
–
–
–
–
–
–
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
[1,
[1,
x[index] = newitem → change existing item
x.insert(index, item) → insert item at index position
x.append(item) →add item at the end
del x[index] → remove item at index position
x.remove(item) → remove item (first occurrence)
len(x) → return list length
item in x → reports existence of item in list (True of False)
aa == [1,
[1, 2,
2, 3]
3]
a.append(5)
a.append(5)
a.insert(3,4)
a.insert(3,4)
print
print aa
2,
2, 3,
3, 4,
4, 5]
5]
>>>
>>> del
del a[1]
a[1]
>>>
>>> a.remove(4)
a.remove(4)
>>>
>>> 11 in
in aa
True
True
>>>
>>> print
print aa
[1,
[1, 3,
3, 5]
5]
Data type →tuple
Tuple → a fixed list
●
●
●
tuples can story any types of objects
access to elements is identical to lists
no modifications are allowed
x = (item, item, item, …)
>>>
>>> aa == (1,
(1, [2])
[2])
>>>
>>> print
print a[0]
a[0]
11
>>>
>>> a[0]=0
a[0]=0
TypeError:
TypeError: 'tuple'
'tuple' does
does not
not support
support item
item assignment
assignment
>>>
>>> a[1][0]=0
a[1][0]=0
>>>
>>> print
print aa
(1,
(1, [0])
[0])
List of integers → range()
List of integers with arithmetic progression
range(stop)
–
–
range(3) → [0, 1, 2]
range(10) → [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(start, stop [,step])
–
–
–
range(1, 3) → [1, 2]
range(-1, 2) → [-1, 0, 1]
range(1, 11, 2) → [1, 3, 5, 7, 9]
>>>
>>> aa == range(1000)
range(1000)
>>>
>>> len(a)
len(a)
1000
1000
>>>
>>> aa == [range(1000)]
[range(1000)]
>>>
>>> >>>
>>> print
print len(a),
len(a), len(a[0])
len(a[0])
11 1000
1000
Definite loop → for-in
Definite loop
●
●
●
repeat the body defined number of times
iterate over elements of list or tuple (also non-numerical)
break and continue are applicable
for item in list:
instr
for
for nn in
in range(10):
range(10):
print
print n**2
n**2
for
for nn in
in [4,
[4, 2,-1]:
2,-1]:
print
print n**2
n**2
I=('pretty','smart','modest')
I=('pretty','smart','modest')
for
for ss in
in I:
I:
print
print "I'm
"I'm "+s
"+s
Functions
Function – sequence of instructions executed
as single instruction
–
–
–
function can take parameters and return value(s)
names created inside function are local
names existing in outer scope are accessible
def function( parameters ):
instructions
return [value(s)]
def
def fib(n):
fib(n):
if
if n<=0:
n<=0: return
return 00
a,
a, bb == 0,
0, 11
for
for ii in
in range(n-1):
range(n-1):
a,
a, bb == b,
b, a+b
a+b
return
return bb
...
...
print
print fib(3)
fib(3)
def
def fibseq(n):
fibseq(n):
seq
seq == []
[]
for
for ii in
in range(n):
range(n):
seq.append(fib(i))
seq.append(fib(i))
return
return seq
seq
...
...
cc == fibseq(10)
fibseq(10) #c
#c is
is aa list
list
This is implementation of Fibonacci sequence has poor
performance and is here just for demonstration purpose
Function documentation
Describes functionality, parameters
and return value
–
used for built-in documentation system
def
def fib(n):
fib(n):
"""
""" Fibonacci
Fibonacci number"""
number"""
if
if n<=0:
n<=0: return
return 00
a,
a, bb == 0,
0, 11
for
for ii in
in range(n-1):
range(n-1):
a,
a, bb == b,
b, a+b
a+b
return
return bb
...
...
print
print fib(3)
fib(3)
def
def fibseq(n):
fibseq(n):
"""
"""
Returns
Returns the
the sequence
sequence
of
of n-Fibonacci
n-Fibonacci
numbers
numbers
"""
"""
seq
seq == []
[]
for
for ii in
in range(n):
range(n):
seq.append(fib(i))
seq.append(fib(i))
return
return seq
seq
print
print fibseq(10)
fibseq(10)
Exercise
Problem: Hailstone numbers sequence
–
cn+1 = ½∙cn
if cn is even
–
cn+1 = 3∙cn+1
if cn is odd
–
stop when reaching 1
–
(polish: liczby gradowe)
example: c=3 → 10, 5, 16, 8, 4, 2, 1
def
def next_hsn(c):
next_hsn(c):
if
if cc %% 22 ==
== 0:
0:
return
return c//2
c//2
else:
else:
return
return 3*c+1
3*c+1
def
def hsn(c):
hsn(c):
sequence
sequence == [c]
[c]
while
while c>1:
c>1:
cc == next_hsn(c)
next_hsn(c)
sequence.append(c)
sequence.append(c)
return
return sequence
sequence
print
print hsn(27)
hsn(27)
Exercise
Investigation:
1) compute and store all hsn sequences (up to 100)
2) find the longest sequence
3) find the highest value reached
● optionally: find the sequence with biggest sum of elements
● optionally: find the most common sequence length
● optionally: find the longest common subsequence
## solution
solution to
to 11
all_hsn
all_hsn == []
[]
for
for cc in
in range(101):
range(101):
all_hsn.append(hsn(c))
all_hsn.append(hsn(c))
print
print all_hsn
all_hsn
hsn(0) →[0]
hsn(1) →[1]
hsn(2) →[2, 1]
...
Related documents