14. Langauge Characteristic
❖
Open Source (Python Software Foundation)
❖
Rapid Development
❖
Short and Readable Code
❖
Indentation!
❖
Strong and Dynamic Typing
15. Langauge Characteristic
❖
Open Source (Python Software Foundation)
❖
Rapid Development
❖
Short and Readable Code
❖
Indentation!
❖
Strong and Dynamic Typing
❖
Interpreter Style
16. PEP - 8
Python Coding Style Guide
http://www.python.org/dev/peps/pep-0008/
25. Data Type
Dictionary
sentence = 'this is a cat'
x = 20
you_love_me = True
pi = 3.1415927
List
Set
nothing = None
Class Instance
26. Data Type
Dictionary
sentence = 'this is a cat'
x = 20
you_love_me = True
pi = 3.1415927
even_numbers = [2, 4, 6, 8, 10]
Set
nothing = None
Class Instance
27. Data Type
Dictionary
sentence = 'this is a cat'
x = 20
you_love_me = True
pi = 3.1415927
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
nothing = None
Class Instance
28. Data Type
profile = { 'name': 'Bas',
'email': 'macbaszii@gmail.com'}
sentence = 'this is a cat'
x = 20
you_love_me = True
pi = 3.1415927
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
nothing = None
Class Instance
29. Data Type
profile = { 'name': 'Bas',
'email': 'macbaszii@gmail.com'}
sentence = 'this is a cat'
x = 20
you_love_me = True
pi = 3.1415927
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
nothing = None
my_car = Car('Lamborghini',
'Aventador LP 700-4', 'White')
30. Data Type
profile = { 'name': 'Bas',
'email': 'macbaszii@gmail.com'}
sentence = 'this is a cat'
x = 20
you_love_me = True
pi = 3.1415927
even_numbers = [2, 4, 6, 8, 10]
odd_numbers = {1, 3, 5, 7, 9}
nothing = None
my_car = Car('Lamborghini',
'Aventador LP 700-4', 'White')
33. Math Operator
x = 10
y = 20
x + y
x - y
x * y
x / y
x % y
x**y
x += y
y *= x
34. Math Operator
x = 10
y = 20
x + y
x - y
x * y
x / y
x % y
x**y
x += y
y *= x
## Tips ##
int + int = int
int**(-int) = float
int / float = float
string + string = concat_string
string * int = multiply_string
list + list = list
37. Comparison Operator
x = 10
y = 20
x
x
x
x
x
x
x
> y
>= y
< y
<= y
== y
!= y
is y
# Chain Comparison
5 < x < y
1 < y < 100
# Contains Operator
prime = [2, 3, 5, 7, 11]
9 in prime # False
sentence = 'this is a cat'
'cat' in sentence # True
39. 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 …
40. 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
45. Conditional Statement
if
number = input("Enter Number: ")
if number > 0:
print "Number is Positive"
elif number < 0:
print "Number is Negative"
else:
print "Number is Zero"
46. Conditional Statement
if
number = input("Enter Number: ")
if number > 0:
print "Number is Positive"
elif number < 0:
print "Number is Negative"
else:
print "Number is Zero"
result = (number % 2 == 0) ? 'Even' : 'Odd'
47. Conditional Statement
if
number = input("Enter Number: ")
if number > 0:
print "Number is Positive"
elif number < 0:
print "Number is Negative"
else:
print "Number is Zero"
result = 'Even' if number % 2 is 0 else 'Odd'
48. Conditional Statement
if
number = input("Enter Number: ")
if number > 0:
print "Number is Positive"
elif number < 0:
print "Number is Negative"
else:
print "Number is Zero"
result = 'Even' if number % 2 is 0 else 'Odd'
51. Iteration Statement
for
for i in xrange(1, 11):
print i**2
socials = ['Facebook', 'Twitter']
for social in socials:
print 'I played %s' % social
52. Iteration Statement
for
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
53. Iteration Statement
for
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
# Endless Loop #
while True:
# Do something
54. Problem#1: Most Letter Count
Hint: You can get list of alphabets with this code
import string
!
alphabets = list(string.lowercase)
60. Problem#2: Make a
Palindrome StringgnirtS emordnilaP
Way Too Long Words
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
68. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = []
for x in range(10):
squares.append(x**2)
69. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
70. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
set1 = [1, 2, 3]
set2 = [3, 1, 4]
71. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
set1 = [1, 2, 3]
set2 = [3, 1, 4]
combs = []
for x in set1:
for y in set2:
if x != y:
comb.append((x, y))
72. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
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)]
73. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
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)]
74. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
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)]
75. List Comprehensive
The Other way to create and manipulation Python’s List
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
squares = [x**2 for x in range(10)]
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)]
78. Working with File
open('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 string
f.readline() # read file for one line
f.readlines() # read file to lines
79. Working with File
open('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 string
f.readline() # read file for one line
f.readlines() # read file to lines
f.write('this is a cat') # write string to file
f.writelines([list of line]) # write lines to file
87. Unit Test
import unittest
! TestBookStack(unittest.TestCase):
class
# 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()
88. 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
89. Migrate to Python 3
❖
print is function instead of
statement!
❖
input()!
❖
range instead of xrange!
❖
more usage on lazy generator