First Chapter

1) Python資料結構 (List, Tuple, Set, Dictionary) 2) 第三方函式庫 (Numpy, Scipy, cv2) 3) 外部資料的處理 (json, csv, image)

List

# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']

print('I have', len(shoplist), 'items to purchase.')

print('These items are:', end=' ')
for item in shoplist:
    print(item, end=' ')

print('\nI also have to buy rice.')
shoplist.append('rice')
print('My shopping list is now', shoplist)

Tuple

zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is', len(zoo))

Dictionary

ab = {
    'Swaroop': '[email protected]',
    'Larry': '[email protected]',
    'Matsumoto': '[email protected]',
    'Spammer': '[email protected]'
}

print("Swaroop's address is", ab['Swaroop'])

# Deleting a key-value pair
del ab['Spammer']

print('\nThere are {} contacts in the address-book\n'.format(len(ab)))

for name, address in ab.items():
    print('Contact {} at {}'.format(name, address))

# Adding a key-value pair
ab['Guido'] = '[email protected]'

if 'Guido' in ab:
    print("\nGuido's address is", ab['Guido'])

Set

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
if 'orange' in basket:                 # fast membership testing
    print(' Found ')
else:
    print('Orange Not Found')

results matching ""

    No results matching ""