2015년 3월 18일 수요일

파이썬 프로그래밍 - Chapter 03

string.py


fred = "Why do gorillas have big nostrils? Big fingers!!"
print (fred)

fred = 'Why do gorillas have big nostrils? Big fingers!!'
print (fred)

fred = '''How do dinosaurs pay their bills?
With tyrannosaurus checks!'''
print (fred)

silly_string = '''He said, "Aren't can't shouldn't wouldn't."'''
print (silly_string)

#escape
single_quote_str = 'He said, "Aren\'t can\'t shouldn\'t wouldn\'t."'
print (single_quote_str)

double_quote_str = "He said, \"Aren't can't shouldn't wouldn't.\""
print (double_quote_str)

myscore = 1000
message = 'I scored %s points'
print (message % myscore)

#placeholder
nums = 'What did the number %s say to the number %s? Nice belt!!'
print (nums % (0, 8))

myletter.py


spaces = ' ' * 25
print('%s 12 Butts Whnd' % spaces)
print('%s Twinklebottom Heath' % spaces)
print('%s West Snoring' % spaces)
print('')
print('')
print('Dear Sir')
print('')
print('I wish to report that tiles are missing from the')
print('outside toilet roof.')
print('I think it was bad wind the other night that blew them away.')
print('')
print('Regards')
print('Malcolm Dithering')

List.py


wizard_list = 'spider legs, toe of frog, eye of newt, bat wing, slug butter, snake dandruff'
print(wizard_list)

wizard_list = ['spider legs', 'toe of frog', 'eye of newt', 'bat wing', 'slug butter', 'snake dandruff']
print(wizard_list)
print(wizard_list[2])

wizard_list[2] = 'snail tongue'
print(wizard_list)
print(wizard_list[2:5])

some_numbers = [1, 2, 5, 10, 20]
print(some_numbers)

some_things = ['Which', 'Witch', 'Is', 'Which']
print(some_things)

number_and_strings = ['Why', 'was', 6, 'afraid', 'of', 7, 'because', 7, 8, 9]
print(number_and_strings)

numbers = [1, 2, 3, 4]
strings = ['I', 'kicked', 'my', 'toe', 'and', 'it', 'is', 'sore']
mylist= [numbers, strings]
print(mylist)

wizard_list.append('bear burp')
print(wizard_list)

wizard_list.append('mandrake')
wizard_list.append('hemlock')
wizard_list.append('swamp gas')

print(wizard_list)

del wizard_list[5]
print(wizard_list)

del wizard_list[8]
del wizard_list[7]
del wizard_list[6]
print(wizard_list)

list1 = [1, 2, 3, 4]
list2 = ['I', 'tripped', 'over', 'and', 'hit', 'the', 'floor']
print(list1 + list2)
list3 = list1 + list2
print(list3)

list1 = [1, 2]
print(list1 * 5)

#tuple
fibs = (0, 1, 1, 2, 3)
fibs2 = (4, 5, 6, 7, 8)
print(fibs[3])
print(fibs * 2)
print(fibs + fibs2)

#map
favorite_sports = {'Ralph Williams' : 'Football',
                   'Michael Tippett' : 'Basketball',
                   'Edward Elgar' : 'Baseball',
                   'Rebecca Clarke' : 'Netball',
                   'Ethel Smyth' : 'Badminton',
                   'Frank Bridge' : 'Rugby'}
print(favorite_sports['Rebecca Clarke'])

del favorite_sports['Ethel Smyth']
print(favorite_sports)

favorite_sports['Ralph Williams'] = 'Ice Hockey'
print(favorite_sports)

파이썬 프로그래밍 - Chapter 02

variable.py


found_coins = 20
magic_coins = 10
stolen_coins = 3
msg = "found_coins + magic_coins * 365 - stolen_coins * 52 = %s"
sum = found_coins + magic_coins * 365 - stolen_coins * 52
print (msg % sum)

stolen_coins = 2
sum = found_coins + magic_coins * 365 - stolen_coins * 52
print (msg % sum)

magic_coins = 13
sum = found_coins + magic_coins * 365 - stolen_coins * 52
print (msg % sum)