Download Quick Intro to Python

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

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

Document related concepts
no text concepts found
Transcript
Quick Intro to Python
January 10, 2017
1
Quick Introduction to Python
CS 1656 – Introduction to Data Science
Alexandros Labrinidis
http://cs1656.org
In [1]: # ADAPTED FROM: Scipy Lecture Notes
# http://www.scipy-lectures.org/index.html
# Section 1.2 -- The Python Language
# http://www.scipy-lectures.org/intro/language/python_language.html
print("Hello, world!")
Hello, world!
In [2]: a = 3
b = 2*a
type(b)
Out[2]: int
In [3]: print(b)
a*b
6
Out[3]: 18
In [4]: b = 'hello'
type(b)
Out[4]: str
In [5]: b + b
1
Out[5]: 'hellohello'
In [6]: 2*b
Out[6]: 'hellohello'
Note: above was example of dynamically typed variables
In [7]: 1 + 1
Out[7]: 2
In [8]: a = 4
type(a)
Out[8]: int
In [9]: c = 2.1
type(c)
Out[9]: float
In [10]: 3 > 4
test = (3 > 4)
test
Out[10]: False
In [11]: type(test)
Out[11]: bool
In [12]: # integer division - python 2
3 / 2
# = 1
Out[12]: 1
In [13]: # integer division - python 3
3 / 2
# = 1.5
Out[13]: 1
In [14]: 3 / 2.
Out[14]: 1.5
In [15]: a = 3
b = 2
a / b # In Python 2
2
Out[15]: 1
In [16]: a / float(b)
Out[16]: 1.5
In [17]: from __future__ import division
3 / 2
Out[17]: 1.5
1.1
Lists
In [18]: ####################### LISTS #######################
l = ['red', 'white', 'blue', 'gold', 'black', 'white']
type(l)
Out[18]: list
In [19]: print l
print l[2]
print l[-1]
print l[-2]
#print l[15] # causes syntax error
['red', 'white', 'blue', 'gold', 'black', 'white']
blue
white
black
In [20]: # l[start:stop] contains the elements with index=i, such that start<= i <
# (i.e., i ranges from start to stop-1)
print l[0:3]
print l[:3]
print l[2:4]
print l[2:]
['red', 'white', 'blue']
['red', 'white', 'blue']
['blue', 'gold']
['blue', 'gold', 'black', 'white']
In [21]: # lists are mutable
l[3] = 'yellow'
print l
l[2:4] = ['light blue', 'gold']
print l
3
['red', 'white', 'blue', 'yellow', 'black', 'white']
['red', 'white', 'light blue', 'gold', 'black', 'white']
In [22]: l[1] = '42'
print l
l.append('green')
print l
['red', '42', 'light blue', 'gold', 'black', 'white']
['red', '42', 'light blue', 'gold', 'black', 'white', 'green']
In [23]: l.pop()
print l
['red', '42', 'light blue', 'gold', 'black', 'white']
In [24]: l.extend(['one', 'two'])
print l
['red', '42', 'light blue', 'gold', 'black', 'white', 'one', 'two']
In [25]: l = l[:-2]
print l
['red', '42', 'light blue', 'gold', 'black', 'white']
In [26]: m = ['alice', 'bob', 'cathy', 'dre']
print m
n = l + m
print n
['alice', 'bob', 'cathy', 'dre']
['red', '42', 'light blue', 'gold', 'black', 'white', 'alice', 'bob', 'cathy', 'dre
1.2
Strings
In [27]: ####################### STRINGS #######################
s = 'Hello, how are you?'
s = "Hi, what's up"
# tripling the quotes allows the string to span more than one line
s = '''Hello,
how are you'''
4
print s
astr = 'abcdefghij.def'
print astr
print astr[3:6]
print astr[3:]
# strings are not immutable
# astr[2] = 'z' ## uncomment to see syntax error
print astr
Hello,
how are you
abcdefghij.def
def
defghij.def
abcdefghij.def
In [28]: print astr
print astr.replace('d', '#', 1)
print astr
abcdefghij.def
abc#efghij.def
abcdefghij.def
In [29]: astr.replace('d', '#')
Out[29]: 'abc#efghij.#ef'
In [30]: fs = 'An integer: %i; a float: %f; another string: %s' % (42, 0.01, 'alice
fs
Out[30]: 'An integer: 42; a float: 0.010000; another string: alice'
In [31]: i = 102
filename = 'processing_of_dataset_%d.txt' % i
filename
Out[31]: 'processing_of_dataset_102.txt'
1.3
Dictionaries
In [32]: ####################### DICTIONARIES #######################
courses = {'cs1555':'intro to database systems',
'cs1520':'web programming',
'cs1501':'algorithm implementation'}
5
courses['cs1656'] = 'intro to data science'
print courses
{'cs1520': 'web programming', 'cs1656': 'intro to data science', 'cs1555': 'intro t
In [33]: courses['cs1555']
Out[33]: 'intro to database systems'
In [34]: print courses.keys()
print courses.values()
['cs1520', 'cs1656', 'cs1555', 'cs1501']
['web programming', 'intro to data science', 'intro to database systems', 'algorith
In [35]: 'cs1656' in courses
Out[35]: True
In [36]: 'cs1655' in courses
Out[36]: False
In [37]: mixed = {'a':1, 'b':2, 3:'hello'}
mixed
Out[37]: {3: 'hello', 'a': 1, 'b': 2}
In [38]: tweet = {
'user' : 'alex',
'text' : 'cs1656 today features an intro to python',
'retweet_count' : 3,
'hashtags' : ["#datascience", "#python", "#pitt"]
}
print tweet
print tweet['text']
print tweet['hashtags']
print tweet['hashtags'][1]
{'text': 'cs1656 today features an intro to python', 'retweet_count': 3, 'hashtags'
cs1656 today features an intro to python
['#datascience', '#python', '#pitt']
#python
6
1.4
Tuples
In [39]: ####################### TUPLES (IMMUTABLE LISTS) #######################
t = 12345, 54321, 'hello', "there!"
print t[0]
print t
u = (0, 2)
print u
# u[1] = 6 ## uncomment to see syntax error
print u
12345
(12345, 54321, 'hello', 'there!')
(0, 2)
(0, 2)
In [40]: my_list = [1,2]
my_tuple = (3, 4)
also_tup = 5, 6
my_list[1] = 12
try:
my_tuple[1] = 13
except TypeError:
print "cannot modify a tuple"
cannot modify a tuple
1.5
Sets
In [41]: ####################### SETS (UNORDERED, UNIQUE ITEMS) ###################
s = set(('a', 'b', 'c', 'a'))
print s
print s.difference(('a', 'b'))
set(['a', 'c', 'b'])
set(['c'])
1.6
Assignments
In [42]: ####################### ASSIGNMENT #######################
a = 'banana'
b = 'banana'
a is b
Out[42]: True
7
In [43]: c = [1, 2, 3]
print c
d = c
print d
print c is d
d[0] = 'z'
print d
print c
d = ['a', 'b', 'c', 'd']
print d
print c is d
[1, 2, 3]
[1, 2, 3]
True
['z', 2, 3]
['z', 2, 3]
['a', 'b', 'c', 'd']
False
In [44]: d[:] = [1, 2, 3, 4, 5]
d
print id(c)
print id(d)
4383941840
4384442416
1.7
Control Flow
In [45]: ####################### CONTROL FLOW #######################
if 2**2 == 4:
print "TADA!"
a = 10
if a==1:
print (1)
elif a==2:
print (2)
else:
print ('more than 2')
print range(6)
for idx in range(4):
8
print (idx)
for word in ['alice', 'bob', 'cathy', 'dolores', 'emily']:
print (word)
for word in ('alice', 'bob', 'cathy', 'dolores', 'emily'):
print (word)
#
#
#
#
#
1
2
3
4
The following evaluate to False:
any number equal to zero
an empty container
False, None
Everything else evaluates to True
== 1
is 2
in [1, 2, 3]
in [1, 2, 3]
vowels = 'aeiouy'
for i in 'powerful':
if i in vowels:
print(i)
ar = {'a': 1, 'b':2.2, 'c':'third'}
for key, val in ar.items():
print('Key: %s has value: %s' % (key, val))
for key, val in sorted(ar.items()):
print('Key: %s has value: %s' % (key, val))
[i**2 for i in range(5)]
[x for x in range(25) if x%2 == 0]
TADA!
more than 2
[0, 1, 2, 3, 4, 5]
0
1
2
3
alice
bob
cathy
dolores
emily
alice
bob
9
cathy
dolores
emily
o
e
u
Key: a has
Key: c has
Key: b has
Key: a has
Key: b has
Key: c has
value:
value:
value:
value:
value:
value:
1
third
2.2
1
2.2
third
Out[45]: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
1.8
Five Word-Counting Examples
In [46]: ####################### FIVE WORD COUNTING EXAMPLES ######################
doc_string = """Alma Mater, wise and glorious,
Child of Light and Bride of Truth,
Over fate and foe victorious,
Dowered with eternal youth,
Crowned with love of son and daughter,
Thou shalt conquer as of yore,
Dear old Pittsburgh, Alma Mater,
God preserve Thee evermore!"""
doc_words = doc_string.split()
In [47]: # Info from: https://docs.python.org/2/library/pprint.html
import pprint
pp = pprint.PrettyPrinter(indent=4)
In [48]: # version 1
word_counts = {} # empty dict
for word in doc_words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
pp.pprint(word_counts)
{
'Alma': 2,
'Bride': 1,
'Child': 1,
'Crowned': 1,
'Dear': 1,
'Dowered': 1,
10
'God': 1,
'Light': 1,
'Mater,': 2,
'Over': 1,
'Pittsburgh,': 1,
'Thee': 1,
'Thou': 1,
'Truth,': 1,
'and': 4,
'as': 1,
'conquer': 1,
'daughter,': 1,
'eternal': 1,
'evermore!': 1,
'fate': 1,
'foe': 1,
'glorious,': 1,
'love': 1,
'of': 4,
'old': 1,
'preserve': 1,
'shalt': 1,
'son': 1,
'victorious,': 1,
'wise': 1,
'with': 2,
'yore,': 1,
'youth,': 1}
In [49]: # version 2
word_counts = {} # empty dict
for word in doc_words:
try:
word_counts[word] += 1
except KeyError:
word_counts[word] = 1
In [50]: # version 3
word_counts = {} # empty dict
for word in doc_words:
previous_count = word_counts.get(word, 0)
word_counts[word] = previous_count + 1
In [51]: # version 4
from collections import defaultdict
word_counts = defaultdict(int)
for word in doc_words:
word_counts[word] += 1
11
In [52]: # version 5
from collections import Counter
word_counts = Counter(doc_words)
1.9
Functions
In [53]: ####################### FUNCTIONS #######################
def squared(x=5):
'''returns the square of the input parameter, with a default value of
NOTE: Default values are evaluated when function is defined, NOT when
return x*x
print squared(12)
print squared()
144
25
In [54]: # using tuples to return multiple results
def sum_and_product(x, y):
return (x+y), (x*y)
sp = sum_and_product(1, 10)
print (sp)
s, p = sum_and_product(1, 10)
print s
print p
print (s, p)
(11, 10)
11
10
(11, 10)
In [55]: # pass by value
# https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyref
def try_to_modify(x, y, z):
x = 23
y.append(42)
z = [992] # new reference
print("x is", x)
print("y is", y)
print("z is", z)
a = 77
b = [99]
c = [28]
# immutable variable
# mutable variable
# mutable variable
12
try_to_modify(a, b, c)
print("a is", a)
print("b is", b)
print("c is", c)
('x
('y
('z
('a
('b
('c
is',
is',
is',
is',
is',
is',
23)
[99, 42])
[992])
77)
[99, 42])
[28])
In [56]: ### Global Variables ###
# SCOPE RULES: http://sebastianraschka.com/Articles/2014_python_scope_and_
x = 5
def addx(y):
return x+y #look, but not touch
print addx(10)
#
x = 5
def setx(y):
x = y
print ('x is %d' % x)
setx(12)
print x
15
x is 12
5
In [ ]:
In [ ]:
13