python slide
DESCRIPTION
Teaching Basic Python at CPSU Sep 10, 2013 Thanks for all attendees :)TRANSCRIPT
![Page 1: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/1.jpg)
![Page 2: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/2.jpg)
![Page 3: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/3.jpg)
![Page 4: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/4.jpg)
Keep Calm and Code Python
Let’s Python Department of Computing!Faculty of Science!Silpakorn University
![Page 5: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/5.jpg)
Keep Calm and Code Python
Let’s Python Department of Computing!Faculty of Science!Silpakorn University
![Page 6: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/6.jpg)
Keep Calm and Code Python
Let’s Python Department of Computing!Faculty of Science!Silpakorn University
![Page 7: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/7.jpg)
Keep Calm and Code Python
Who am I ?
Kiattisak Anoochitarom!Graduated from Computer Science at SU, 2013!Software Developer at Charged Concept Co, LTD.!!Skill Sets: iOS, Rails, Node.js, Ruby, Python, Javascript, !C++, C, Java, Badminton!!Contacts:! [email protected]! Twitter: @iMacbaszii! http://www.facebook.com/baszii! http://www.macbaszii.com!
![Page 8: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/8.jpg)
Who Invented ?
❖ Guido van Rossum!❖ Dutch!❖ 2005 - 2012 at Google inc.!❖ 2013 at Dropbox inc.
![Page 9: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/9.jpg)
Langauge Characteristic
![Page 10: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/10.jpg)
Langauge Characteristic
❖ Open Source (Python Software Foundation)
![Page 11: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/11.jpg)
Langauge Characteristic
❖ Open Source (Python Software Foundation)
❖ Rapid Development
![Page 12: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/12.jpg)
Langauge Characteristic
❖ Open Source (Python Software Foundation)
❖ Rapid Development
❖ Short and Readable Code
![Page 13: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/13.jpg)
Langauge Characteristic
❖ Open Source (Python Software Foundation)
❖ Rapid Development
❖ Short and Readable Code
❖ Indentation!
![Page 14: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/14.jpg)
Langauge Characteristic
❖ Open Source (Python Software Foundation)
❖ Rapid Development
❖ Short and Readable Code
❖ Indentation!
❖ Strong and Dynamic Typing
![Page 15: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/15.jpg)
Langauge Characteristic
❖ Open Source (Python Software Foundation)
❖ Rapid Development
❖ Short and Readable Code
❖ Indentation!
❖ Strong and Dynamic Typing
❖ Interpreter Style
![Page 16: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/16.jpg)
PEP - 8 Python Coding Style Guide
http://www.python.org/dev/peps/pep-0008/
![Page 17: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/17.jpg)
Input & Output
![Page 18: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/18.jpg)
Input & Output
name = raw_input()
print "Hello, %s" % (name)print "Hello, " + name
x = input()y = input()
print x * y
![Page 19: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/19.jpg)
Input & Output
name = raw_input()
print "Hello, %s" % (name)print "Hello, " + name
x = input()y = input()
print x * yHands - on!!
![Page 20: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/20.jpg)
Data Type
DictionaryString
Integer
Floating Point
None
List
Boolean
Class Instance
Set
![Page 21: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/21.jpg)
Data Type
Dictionary
Integer
Floating Point
None
List
Boolean
Class Instance
Set
sentence = 'this is a cat'
![Page 22: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/22.jpg)
Data Type
Dictionary
Floating Point
None
List
Boolean
Class Instance
Set
sentence = 'this is a cat'
x = 20
![Page 23: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/23.jpg)
Data Type
Dictionary
Floating Point
None
List
Class Instance
Set
sentence = 'this is a cat'
x = 20
you_love_me = True
![Page 24: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/24.jpg)
Data Type
Dictionary
None
List
Class Instance
Set
sentence = 'this is a cat'
x = 20
pi = 3.1415927
you_love_me = True
![Page 25: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/25.jpg)
Data Type
Dictionary
List
Class Instance
Set
sentence = 'this is a cat'
x = 20
pi = 3.1415927
you_love_me = True
nothing = None
![Page 26: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/26.jpg)
Data Type
Dictionary
Class Instance
Set
sentence = 'this is a cat'
x = 20
pi = 3.1415927
you_love_me = True
nothing = None
even_numbers = [2, 4, 6, 8, 10]
![Page 27: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/27.jpg)
Data Type
Dictionary
Class Instance
sentence = 'this is a cat'
x = 20
pi = 3.1415927
you_love_me = True
nothing = None
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
![Page 28: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/28.jpg)
Data Type
Class Instance
sentence = 'this is a cat'
x = 20
pi = 3.1415927
you_love_me = True
nothing = None
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
profile = { 'name': 'Bas',\ 'email': '[email protected]'}
![Page 29: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/29.jpg)
Data Type
sentence = 'this is a cat'
x = 20
pi = 3.1415927
you_love_me = True
nothing = None
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
profile = { 'name': 'Bas',\ 'email': '[email protected]'}
my_car = Car('Lamborghini',\ 'Aventador LP 700-4', 'White')
![Page 30: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/30.jpg)
Data Type
sentence = 'this is a cat'
x = 20
pi = 3.1415927
you_love_me = True
nothing = None
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
profile = { 'name': 'Bas',\ 'email': '[email protected]'}
my_car = Car('Lamborghini',\ 'Aventador LP 700-4', 'White')
![Page 31: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/31.jpg)
Operator
![Page 32: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/32.jpg)
Math Operator
![Page 33: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/33.jpg)
Math Operator
x = 10y = 20
x + yx - yx * yx / yx % yx**yx += yy *= x
![Page 34: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/34.jpg)
Math Operator
x = 10y = 20
x + yx - yx * yx / yx % yx**yx += yy *= x
## Tips ##
int + int = intint**(-int) = floatint / float = floatstring + string = concat_stringstring * int = multiply_stringlist + list = list
![Page 35: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/35.jpg)
Comparison Operator
![Page 36: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/36.jpg)
Comparison Operator
x = 10y = 20
x > yx >= yx < yx <= yx == yx != yx is y
![Page 37: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/37.jpg)
Comparison Operator
x = 10y = 20
x > yx >= yx < yx <= yx == yx != yx is y
# Chain Comparison5 < x < y1 < y < 100
# Contains Operatorprime = [2, 3, 5, 7, 11]9 in prime # False
sentence = 'this is a cat''cat' in sentence # True
![Page 38: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/38.jpg)
Logical Operator
![Page 39: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/39.jpg)
Logical Operator Although Python have & and | (pipe) to do logical operation !but it isn’t readable and it will confuse with other symbol. !But Python have special symbols to do logical operation that is …
![Page 40: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/40.jpg)
Logical Operator Although Python have & and | (pipe) to do logical operation !but it isn’t readable and it will confuse with other symbol. !But Python have special symbols to do logical operation that is …
and, or
![Page 41: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/41.jpg)
Range and Lazy Generator
![Page 42: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/42.jpg)
Range and Lazy Generator
range(10)# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]range(3, 10)# [3, 4, 5, 6, 7, 8, 9]range(1, 20, 3)# [1, 4, 7, 10, 13, 16, 19]
xrange(10)xrange(3, 10)xrange(1, 20, 3)
![Page 43: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/43.jpg)
Control Statement
![Page 44: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/44.jpg)
Conditional Statementif
![Page 45: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/45.jpg)
Conditional Statement
number = input("Enter Number: ")
if number > 0:print "Number is Positive"
elif number < 0:print "Number is Negative"
else:print "Number is Zero"
if
![Page 46: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/46.jpg)
Conditional Statement
number = input("Enter Number: ")
if number > 0:print "Number is Positive"
elif number < 0:print "Number is Negative"
else:print "Number is Zero"
if
result = (number % 2 == 0) ? 'Even' : 'Odd'
![Page 47: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/47.jpg)
Conditional Statement
number = input("Enter Number: ")
if number > 0:print "Number is Positive"
elif number < 0:print "Number is Negative"
else:print "Number is Zero"
if
result = 'Even' if number % 2 is 0 else 'Odd'
![Page 48: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/48.jpg)
Conditional Statement
number = input("Enter Number: ")
if number > 0:print "Number is Positive"
elif number < 0:print "Number is Negative"
else:print "Number is Zero"
if
result = 'Even' if number % 2 is 0 else 'Odd'
![Page 49: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/49.jpg)
Iteration Statementfor
![Page 50: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/50.jpg)
Iteration Statementfor
for i in xrange(1, 11):print i**2
![Page 51: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/51.jpg)
Iteration Statementfor
for i in xrange(1, 11):print i**2
socials = ['Facebook', 'Twitter']for social in socials:
print 'I played %s' % social
![Page 52: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/52.jpg)
Iteration Statementfor
for i in xrange(1, 11):print i**2
socials = ['Facebook', 'Twitter']for social in socials:
print 'I played %s' % social
message = 'Hello, CPSU'for letter in message:
print letter
![Page 53: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/53.jpg)
Iteration Statementfor
for i in xrange(1, 11):print i**2
socials = ['Facebook', 'Twitter']for social in socials:
print 'I played %s' % social
# Endless Loop #while True:
# Do something
message = 'Hello, CPSU'for letter in message:
print letter
![Page 54: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/54.jpg)
Problem#1: Most Letter CountHint: You can get list of alphabets with this code
import string !alphabets = list(string.lowercase)
![Page 55: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/55.jpg)
Function
![Page 56: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/56.jpg)
Functiondef function_name(params):
# Do something# Do anything
![Page 57: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/57.jpg)
Functiondef function_name(params):
# Do something# Do anything
def fibonacci(n):fibo = 0for k in xrange(0, int(math.floor((n - 1) / 2)) + 1):
fibo += math.factorial(n - k - 1) /\(math.factorial(k) * math.factorial(n - k - 1 - k))
return fibo
![Page 58: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/58.jpg)
Functiondef function_name(params):
# Do something# Do anything
def fibonacci(n):fibo = 0for k in xrange(0, int(math.floor((n - 1) / 2)) + 1):
fibo += math.factorial(n - k - 1) /\(math.factorial(k) * math.factorial(n - k - 1 - k))
return fibo
def colorMultiply(r, g=0, b=0):return [ r * 3.14159,\
g * 1.414,\b * 3.27 ]
![Page 59: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/59.jpg)
Built-in Functionshttp://docs.python.org/2/library/functions.html
![Page 60: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/60.jpg)
Problem#2: Make a Palindrome StringgnirtS emordnilaP
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.!
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.!
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn't contain any leading zeroes.!
Thus, "localization" will be spelt as "l10n", and "internationalization will be spelt as “i18n".!
and make Palindrome from that word after that :)
http://codeforces.com/problemset/problem/71/A
Way Too Long Words
![Page 61: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/61.jpg)
Indexing and Slice
![Page 62: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/62.jpg)
Indexing and Slicemessage = 'Hello, world'message[0] # Hmessage[len(message) - 1] # dmessage[-1] # d
![Page 63: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/63.jpg)
Indexing and Slicemessage = 'Hello, world'message[0] # Hmessage[len(message) - 1] # dmessage[-1] # d
fibo = [1, 1, 2, 3, 5, 8, 13, 21, 34]fibo[:5] # [1, 1, 2, 3, 5]fibo[2:] # [2, 3, 5, 8, 13, 21, 34]fibo[3:6] # [3, 5, 8, 13]fibo[::2] # [1, 2, 5, 13, 34]
![Page 64: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/64.jpg)
Indexing and Slicemessage = 'Hello, world'message[0] # Hmessage[len(message) - 1] # dmessage[-1] # d
fibo[::-1] # ???message[::-1] # ???
fibo = [1, 1, 2, 3, 5, 8, 13, 21, 34]fibo[:5] # [1, 1, 2, 3, 5]fibo[2:] # [2, 3, 5, 8, 13, 21, 34]fibo[3:6] # [3, 5, 8, 13]fibo[::2] # [1, 2, 5, 13, 34]
![Page 65: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/65.jpg)
String and Collections methods
Demo!
![Page 66: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/66.jpg)
List ComprehensiveThe Other way to create and manipulation Python’s List
![Page 67: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/67.jpg)
List Comprehensive
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
The Other way to create and manipulation Python’s List
![Page 68: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/68.jpg)
List Comprehensive
squares = [] for x in range(10): squares.append(x**2)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
The Other way to create and manipulation Python’s List
![Page 69: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/69.jpg)
List Comprehensive
squares = [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
The Other way to create and manipulation Python’s List
![Page 70: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/70.jpg)
List Comprehensive
squares = [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
set1 = [1, 2, 3] set2 = [3, 1, 4]
The Other way to create and manipulation Python’s List
![Page 71: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/71.jpg)
List Comprehensive
squares = [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
set1 = [1, 2, 3] set2 = [3, 1, 4]
combs = [] for x in set1: for y in set2: if x != y: comb.append((x, y))
The Other way to create and manipulation Python’s List
![Page 72: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/72.jpg)
List Comprehensive
squares = [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
set1 = [1, 2, 3] set2 = [3, 1, 4]
combs = [(x, y) for x in set1 for y in set2 if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
The Other way to create and manipulation Python’s List
![Page 73: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/73.jpg)
List Comprehensive
squares = [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
set1 = [1, 2, 3] set2 = [3, 1, 4]
combs = [(x, y) for x in set1 for y in set2 if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
The Other way to create and manipulation Python’s List
![Page 74: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/74.jpg)
List Comprehensive
squares = [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
set1 = [1, 2, 3] set2 = [3, 1, 4]
combs = [(x, y) for x in set1 for y in set2 if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
The Other way to create and manipulation Python’s List
![Page 75: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/75.jpg)
List Comprehensive
squares = [x**2 for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
set1 = [1, 2, 3] set2 = [3, 1, 4]
combs = [(x, y) for x in set1 for y in set2 if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
The Other way to create and manipulation Python’s List
![Page 76: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/76.jpg)
Working with File
![Page 77: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/77.jpg)
Working with Fileopen('filename', mode)
# r: open file for read# w: open file for write# a: open file for append
![Page 78: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/78.jpg)
Working with Fileopen('filename', mode)
# r: open file for read# w: open file for write# a: open file for append
f = open('data.txt', r)f.read() # read file to stringf.readline() # read file for one linef.readlines() # read file to lines
![Page 79: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/79.jpg)
Working with Fileopen('filename', mode)
# r: open file for read# w: open file for write# a: open file for append
f = open('data.txt', r)f.read() # read file to stringf.readline() # read file for one linef.readlines() # read file to lines
f.write('this is a cat') # write string to filef.writelines([list of line]) # write lines to file
![Page 80: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/80.jpg)
Object Oriented Programming
![Page 81: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/81.jpg)
Object Oriented ProgrammingClass
![Page 82: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/82.jpg)
Object Oriented ProgrammingClass
Instance
![Page 83: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/83.jpg)
Object Oriented Programmingclass Book: def __init__(self, name, size): self.name = name self.size = size class BookStack: def __init__(self): self.books = [] self.top = 0 def push(self, book): self.books.append(book) self.top += 1 def pop(self): self.top -= 1 return self.books.pop() !
![Page 84: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/84.jpg)
Test Driven Development
![Page 85: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/85.jpg)
![Page 86: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/86.jpg)
![Page 87: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/87.jpg)
Unit Testimport unittest !class TestBookStack(unittest.TestCase): # when book is created, it can return name, size def test_book_created(self): hunger_book = Book('The Hunger Games', 4) self.assertEqual(hunger_book.name, 'The Hunger Games') self.assertEqual(hunger_book.size, 4) # when Book Stack is created / top is 0 def test_book_stack_created(self): book_stack = BookStack() self.assertEqual(book_stack.top, 0) self.assertEqual(book_stack.books, []) # when push book / top increase by one def test_book_stack_push(self): hunger_book = Book('The Hunger Games', 4) book_stack = BookStack() book_stack.push(hunger_book) self.assertEqual(book_stack.top, 1) self.assertEqual(book_stack.books[0].name, 'The Hunger Games') book_stack.push(hunger_book) self.assertEqual(book_stack.top, 2) # when pop book / top decrease by one def test_book_stack_pop(self): hunger_book = Book('The Hunger Games', 4) harry_book = Book('Harry Potter', 3) book_stack = BookStack() book_stack.push(hunger_book) book_stack.push(harry_book) present_size = book_stack.top poped_book = book_stack.pop() self.assertEqual(book_stack.top, present_size - 1) self.assertEqual(poped_book.name, 'Harry Potter') if __name__ == '__main__': unittest.main()
![Page 88: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/88.jpg)
Keep Calm and Code Python
Problem#3: Books
Instruction:!! - Using TDD!!! - Each Book have a name, size!! - Books have size and all of book!!Test case:!! - paste the new book on top!! - pick a book at top of books!! - when paste new book you must take a larger book to below!! - program can tell a present Books size
![Page 89: Python slide](https://reader033.vdocuments.net/reader033/viewer/2022052508/559ea64c1a28abcc2e8b45a7/html5/thumbnails/89.jpg)
Migrate to Python 3
❖ print is function instead of statement!
❖ input()!❖ range instead of xrange!❖ more usage on lazy generator