Python - An Introduction

121
Python - An Introduction Eueung Mulyana | http://eueung.github.io/EL6240/py based on the material @Codecademy Attribution-ShareAlike CC BY-SA 1 / 121

Transcript of Python - An Introduction

Page 1: Python - An Introduction

Python - An IntroductionEueung Mulyana | http://eueung.github.io/EL6240/py

based on the material @Codecademy

Attribution-ShareAlike CC BY-SA

1 / 121

Page 2: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

2 / 121

Page 3: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

3 / 121

Page 4: Python - An Introduction

Python SyntaxBasic Types: string, int, float, boolFunction: print (python 2)Comment: #

print "Welcome to Python!" my_variable = 10

my_int = 7 my_float = 1.23 my_bool = True

# change value my_int = 7 my_int = 3 print my_int

4 / 121

Page 5: Python - An Introduction

Python SyntaxNote: IndentationError: expected an indented block

# firstdef spam():eggs = 12return eggs

print spam()

# seconddef spam(): eggs = 12 return eggs

print spam()

5 / 121

Page 6: Python - An Introduction

Python SyntaxComment: #, """Operators: +,-,*,/,**,%

# single line comment"""testmulti lines comment"""

addition = 72 + 23subtraction = 108 - 204multiplication = 108 * 0.5division = 108 / 9

# additioncount_to = 5000 + 6000.6print count_to

# square, exponentiation, to the power ofeggs = 10**2print eggs

# modulospam = 5 % 4print spam# 1, modulo

6 / 121

Page 7: Python - An Introduction

Python SyntaxLearned:

Variables, which store values for later useData types, such as numbers and booleansWhitespace, which separates statementsComments, which make your code easier to readArithmetic operations, including +, -, , /, *, and %

# commentmonty = Truepython = 1.234monty_python = python ** 2

7 / 121

Page 8: Python - An Introduction

Python SyntaxTip Calculator

meal = 44.50

# 6.75%tax = 0.0675

# 15%tip = 0.15

meal = meal + meal * taxtotal = meal + meal * tip

print("%.2f" % total)

8 / 121

Page 9: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

9 / 121

Page 10: Python - An Introduction

Strings and Console OutputAssignment, Escaping Chars"MONTY"[4]

caesar = "Graham"print caesar

# Escaping charactersabc = 'This isn\'t flying, this is falling with style!'print abc

"""The string "PYTHON" has six characters,numbered 0 to 5, as shown below:

+---+---+---+---+---+---+| P | Y | T | H | O | N |+---+---+---+---+---+---+ 0 1 2 3 4 5

So if you wanted "Y", you could just type"PYTHON"[1] (always start counting from 0!)"""

fifth_letter = "MONTY"[4]print fifth_letter# Y

10 / 121

Page 11: Python - An Introduction

Strings and Console OutputFunctions: len(), lower(), upper(), str()

parrot = "Norwegian Blue"print len(parrot) #14

# String methods: len(), lower(), upper(), str()

parrot = "Norwegian Blue"print parrot.lower() print parrot.upper()

# to stringpi=3.14print str(pi)

# reviewministry = "The Ministry of Silly Walks"print len(ministry) print ministry.upper()

11 / 121

Page 12: Python - An Introduction

Strings and Console OutputPrinting String VariablesString ConcatenationFunctions: raw_input()

# to string, printing variables, string concatenationprint "Spam " + "and " + "eggs"print "The value of pi is around " + 3.14 # errorprint "The value of pi is around " + str(3.14)

string_1 = "Camelot"string_2 = "place"print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)

# string formattingname = "Mike"print "Hello %s" % (name)

# string formatting, tanda %s dan %# fungsi raw_input (input dari keyboard pas runtime)name = raw_input("What is your name?")quest = raw_input("What is your quest?")color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \"and your favorite color is %s." % (name, quest, color)

12 / 121

Page 13: Python - An Introduction

Strings and Console OutputDate and Time

from datetime import datetimenow = datetime.now()print now

# member, bukan methodprint now.yearprint now.monthprint now.day

print '%s-%s-%s' % (now.year, now.month, now.day)print '%s/%s/%s' % (now.day, now.month, now.year)

print now.hourprint now.minuteprint now.second

print '%s:%s:%s' % (now.hour, now.minute, now.second)print '%s/%s/%s %s:%s:%s' % (now.day, now.month, now.year,now.hour, now.minute, now.second)

13 / 121

Page 14: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

14 / 121

Page 15: Python - An Introduction

Conditionals and Control Flow# raw_inputdef clinic(): print "You've just entered the clinic!" print "Do you take the door on the left or the right?" answer = raw_input("Type left or right and hit 'Enter'.").lower() if answer == "left" or answer == "l": print "This is the Verbal Abuse Room, you heap of parrot droppings!" elif answer == "right" or answer == "r": print "Of course this is the Argument Room, I've told you that already!" else: print "You didn't pick left or right! Try again." clinic()

clinic()

15 / 121

Page 16: Python - An Introduction

Conditionals and Control FlowBoolean Comparisons

bool_one = True # 17 < 328 bool_two = True # 100 == (2 * 50)bool_three = True # 19 <= 19 bool_four = False # -22 >= -18 bool_five = False # 99 != (98 + 1)

bool_one = False # (20 - 10) > 15bool_two = False # (10 + 17) == 3**16bool_three = False # 1**2 <= -1bool_four = True # 40 * 4 >= -4bool_five = False # 100 != 10**2

# -----

bool_one = 3 < 5 # Truebool_two = 3 > 5 # Falsebool_three = 5 == 5 # Truebool_four = 3 != 3 # Falsebool_five = 3 <= 3 # True

16 / 121

Page 17: Python - An Introduction

Conditionals and Control FlowOperators: and, or

bool_one = False and Falsebool_two = -(-(-(-2))) == -2 and 4 >= 16**0.5bool_three = 19 % 4 != 300 / 10 / 10 and Falsebool_four = -(1**2) < 2**0 and 10 % 10 <= 20 - 10 * 2bool_five = True and True

# -----bool_one = 2**3 == 108 % 100 or 'Cleese' == 'King Arthur'bool_two = True or Falsebool_three = 100**0.5 >= 50 or Falsebool_four = True or Truebool_five = 1**100 == 100**1 or 3 * 2 * 1 != 3 + 2 + 1

17 / 121

Page 18: Python - An Introduction

Conditionals and Control FlowOperators: not

bool_one = not Truebool_two = not 3**4 < 4**3bool_three = not 10 % 3 <= 10 % 2bool_four = not 3**2 + 4**2 != 5**2bool_five = not not False

#-----bool_one = False or not True and Truebool_two = False and not True or Truebool_three = True and not (False or False)bool_four = not not True or False and not Truebool_five = False or not (True and True)

#-----# not first, berikutnya and,or -- sama prioritas, kl not not sama dengan not (not )

bool_one = (2 <= 2) and "Alpha" == "Bravo"

18 / 121

Page 19: Python - An Introduction

Conditionals and Control FlowConditional Statement

answer = "Left"if answer == "Left": print "This is the Verbal Abuse Room, you heap of parrot droppings!"

# Will the above print statement print to the console?

# ---def using_control_once(): if True: return "Success #1"

def using_control_again(): if True: return "Success #2"

print using_control_once()print using_control_again()

19 / 121

Page 20: Python - An Introduction

Conditionals and Control FlowConditional Statement

# conditionanswer = "'Tis but a scratch!"

def black_knight(): if answer == "'Tis but a scratch!": return True else: return False # Make sure this returns False

def french_soldier(): if answer == "Go away, or I shall taunt you a second time!": return True else: return False # Make sure this returns False

20 / 121

Page 21: Python - An Introduction

Conditionals and Control FlowConditional Statement

# conditiondef greater_less_equal_5(answer): if answer > 5: return 1 elif answer < 5: return -1 else: return 0

print greater_less_equal_5(4)print greater_less_equal_5(5)print greater_less_equal_5(6)

# reviewdef the_flying_circus(): if 5 == 5: # Start coding here! # Don't forget to indent # the code inside this block! return True elif True and True : # Keep going here. # You'll want to add the else statement, too! return False else : return False

21 / 121

Page 22: Python - An Introduction

Conditionals and Control FlowPygLatin (Python Pig Latin)

Ask the user to input a word in English.Make sure the user entered a valid word.Convert the word from English to Pig Latin.Display the translation result.

print 'Welcome to the Pig Latin Translator!'

original = raw_input("Enter a word:")

if len(original) > 0 and original.isalpha(): print originalelse: print "empty"

22 / 121

Page 23: Python - An Introduction

Conditionals and Control FlowPygLatin (Python Pig Latin)You move the first letter of the word to the end and then append the suffix'ay'. Example: python -> ythonpay

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha(): word = original.lower() first = word[0] new_word = word + first + pyg new_word = new_word[1:len(new_word)] print new_wordelse: print 'empty'

23 / 121

Page 24: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

24 / 121

Page 25: Python - An Introduction

Functionsdef tax(bill): """Adds 8% tax to a restaurant bill.""" bill *= 1.08 print "With tax: %f" % bill return bill

def tip(bill): """Adds 15% tip to a restaurant bill.""" bill *= 1.15 print "With tip: %f" % bill return bill

meal_cost = 100meal_with_tax = tax(meal_cost)meal_with_tip = tip(meal_with_tax)

25 / 121

Page 26: Python - An Introduction

Functionsdef square(n): squared = n**2 print "%d squared is %d." % (n, squared) return squared

square(10)

# ---

def power(base, exponent): # Add your parameters here! result = base**exponent print "%d to the power of %d is %d." % (base, exponent, result)

power(37,4) # Add your arguments here!

26 / 121

Page 27: Python - An Introduction

Functions# Functions call functionsdef one_good_turn(n): return n + 1

def deserves_another(n): return one_good_turn(n) + 2

# ---

def shout(phrase): if phrase == phrase.upper(): return "YOU'RE SHOUTING!" else: return "Can you speak up?"

shout("I'M INTERESTED IN SHOUTING")

# ---def cube(number): return number**3

def by_three(number): if number % 3 == 0: return cube(number) else: return False

27 / 121

Page 28: Python - An Introduction

FunctionsImporting a Module: a module is a file that contains definitions—including variables and functions—that you can use once it is importedMath Standard Library

# 1import mathprint math.sqrt(25)

# 2from math import sqrtprint sqrt(25)

from math import *print sqrt(25)

# 3import math everything = dir(math) # Sets everything to a list of things from mathprint everything # Prints 'em all!

"""['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']"""

28 / 121

Page 29: Python - An Introduction

FunctionsMath*args : one or more argumentsFunctions: max, min, abs

def biggest_number(*args): print max(args) return max(args)

def smallest_number(*args): print min(args) return min(args)

def distance_from_zero(arg): print abs(arg) return abs(arg)

biggest_number(-10, -5, 5, 10)smallest_number(-10, -5, 5, 10)distance_from_zero(-10)# ---maximum = max(2.3,4.1,6)minimum = min(2.3,4.1,6)absolute = abs(-42)

print maximum

29 / 121

Page 30: Python - An Introduction

FunctionsFunction: type

print type(42) #<type 'int'>print type(4.2) #<type 'float'>print type('spam') #<type 'str'>

# ---def speak(message): return message

if happy(): speak("I'm happy!")elif sad(): speak("I'm sad.")else: speak("I don't know what I'm feeling.")# ---def shut_down(s): if s=="yes": return "Shutting down" elif s=="no": return "Shutdown aborted" else: return "Sorry"

30 / 121

Page 31: Python - An Introduction

Functionsdef is_numeric(num): return type(num) == int or type(num) == float:

# ---def distance_from_zero(a): if type(a)==int or type(a)==float: return abs(a) else: return "Nope"

31 / 121

Page 32: Python - An Introduction

FunctionsTaking a Vacation

def hotel_cost(nights): return 140*nights

def plane_ride_cost(city): if city=="Charlotte": return 183 elif city=="Tampa": return 220 elif city=="Pittsburgh": return 222 elif city=="Los Angeles": return 475

def rental_car_cost(days): costperday = 40 if days >= 7: total = days * costperday - 50 elif days >=3: total = days * costperday - 20 else: total = days * costperday return total

def trip_cost(city,days,spending_money): return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)+spending_money

print trip_cost("Los Angeles",5,600)

32 / 121

Page 33: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

33 / 121

Page 34: Python - An Introduction

Lists and DictionariesList : ArrayDictionary : Asc. Array (notation {} -> object in JS)

List

# list_name = [item_1, item_2], empty []zoo_animals = ["pangolin", "cassowary", "sloth", "gajah"];

if len(zoo_animals) > 3: print "The first animal at the zoo is the " + zoo_animals[0] print "The second animal at the zoo is the " + zoo_animals[1] print "The third animal at the zoo is the " + zoo_animals[2] print "The fourth animal at the zoo is the " + zoo_animals[3]

34 / 121

Page 35: Python - An Introduction

Lists and DictionariesList

Methods: append, len

suitcase = [] suitcase.append("sunglasses")

# Your code here!suitcase.append('a')suitcase.append('b')suitcase.append('c')

list_length = len(suitcase) # Set this to the length of suitcase

print "There are %d items in the suitcase." % (list_length)print suitcase"""There are 4 items in the suitcase.['sunglasses', 'a', 'b', 'c']"""

35 / 121

Page 36: Python - An Introduction

Lists and DictionariesList

Slicing: [index_inclusive:index_exclusive]

suitcase = ["sunglasses", "hat", "passport", "laptop", "suit", "shoes"]

first = suitcase[0:2] # The first and second items (index zero and one)middle = suitcase[2:4] # Third and fourth items (index two and three)last = suitcase[4:6] # The last two items (index four and five)# < 6 bukan <= 6# We start at the index before the colon and continue up to but not including the index after the colon.# ---

animals = "catdogfrog"cat = animals[:3] # The first three characters of animalsdog = animals[3:6] # The fourth through sixth charactersfrog = animals[6:] # From the seventh character to the end# tanpa start/end

36 / 121

Page 37: Python - An Introduction

Lists and DictionariesList

Methods: index, insert

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]duck_index = animals.index("duck") # Use index() to find "duck"

# Your code here!animals.insert(duck_index,"cobra")

print animals # Observe what prints after the insert operation# ['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fennec fox']

37 / 121

Page 38: Python - An Introduction

Lists and DictionariesListfor element in the_list.sort()

# 1my_list = [1,9,3,8,5,7]

for number in my_list: print 2*number

# 2 # .sortstart_list = [5, 3, 1, 2, 4]square_list = []

for num in start_list: square_list.append(num**2)

square_list.sort()print square_list

38 / 121

Page 39: Python - An Introduction

Lists and DictionariesDict

Key : Value Pair

# dictionary key value notasi object {}residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}

print residents['Puffin'] # Prints Puffin's room number

# Your code here!print residents['Sloth']print residents['Burmese Python']

39 / 121

Page 40: Python - An Introduction

Lists and DictionariesDict

# ---menu = {} # Empty dictionarymenu['Chicken Alfredo'] = 14.50 # Adding new key-value pairprint menu['Chicken Alfredo']

# appendmenu['karedok'] = 2.2menu['lotek'] = 2.3menu['rujak'] = 3.3

print "There are " + str(len(menu)) + " items on the menu."print menu"""14.5There are 4 items on the menu.{'lotek': 2.3, 'Chicken Alfredo': 14.5, 'karedok': 2.2, 'rujak': 3.3}"""

40 / 121

Page 41: Python - An Introduction

Lists and DictionariesDict

A dictionary (or list) declaration may break across multiple linesdel

# key - animal_name : value - location zoo_animals = { 'Unicorn' : 'Cotton Candy House', 'Sloth' : 'Rainforest Exhibit', 'Bengal Tiger' : 'Jungle House', 'Atlantic Puffin' : 'Arctic Exhibit', 'Rockhopper Penguin' : 'Arctic Exhibit'}

# Removing the 'Unicorn' entry. (Unicorns are incredibly expensive.)del zoo_animals['Unicorn']

del zoo_animals['Sloth']del zoo_animals['Bengal Tiger']zoo_animals['Rockhopper Penguin']='Arctic Exhibit Mod'

print zoo_animals

41 / 121

Page 42: Python - An Introduction

Lists and DictionariesList & DictremoveComplex Dict

# removebackpack = ['xylophone', 'dagger', 'tent', 'bread loaf']backpack.remove("dagger")

# ---inventory = { 'gold' : 500, 'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key 'backpack' : ['xylophone','dagger', 'bedroll','bread loaf']}

# Adding a key 'burlap bag' and assigning a list to itinventory['burlap bag'] = ['apple', 'small ruby', 'three-toed sloth']

# Sorting the list found under the key 'pouch'inventory['pouch'].sort()

inventory['pocket']=['seashell','strange berry','lint']inventory['backpack'].sort()inventory['backpack'].remove('dagger')inventory['gold'] += 50

42 / 121

Page 43: Python - An Introduction

Lists and DictionariesA Day at the Supermarket

names = ["Adam","Alex","Mariah","Martine","Columbus"]for item in names: print item

# ---# indent first element dalam dict dalam {}, spt di bhs lainwebster = { "Aardvark" : "A star of a popular children's cartoon show.", "Baa" : "The sound a goat makes.", "Carpet": "Goes on the floor.", "Dab": "A small amount."}

for key in webster: print webster[key]

Note that dictionaries are unordered, meaning that any time you loopthrough a dictionary, you will go through every key, but you are notguaranteed to get them in any particular order.

43 / 121

Page 44: Python - An Introduction

Lists and DictionariesA Day at the Supermarket

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

for number in a: if number % 2 == 0: print number

# ---def count_small(numbers): total = 0 for n in numbers: if n < 10: total = total + 1 return total

lost = [4, 8, 15, 16, 23, 42]small = count_small(lost)print small# ---def fizz_count(x): count = 0 for item in x: if item == 'fizz': count += 1 return countfizz_count(["fizz","cat","fizz"])

44 / 121

Page 45: Python - An Introduction

Lists and DictionariesA Day at the SupermarketAs we've mentioned, strings are like lists with characters as elements. Youcan loop through strings the same way you loop through lists!

for letter in "Codecademy": print letter

# Empty lines to make the output prettyprintprint

word = "Programming is fun!"

for letter in word: # Only print out the letter i if letter == "i": print letter

45 / 121

Page 46: Python - An Introduction

Lists and DictionariesA Day at the Supermarket

prices = { "banana": 4,"apple": 2, "orange": 1.5,"pear": 3 }

stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 }

for key in stock: print key print "price: %s" % prices[key] print "stock: %s" % stock[key]

46 / 121

Page 47: Python - An Introduction

Lists and DictionariesA Day at the Supermarket

# take 2prices = { "banana" : 4, "apple" : 2, "orange" : 1.5, "pear" : 3,}stock = { "banana" : 6, "apple" : 0, "orange" : 32, "pear" : 15,}

for key in prices: print key print "price: %s" % prices[key] print "stock: %s" % stock[key]

total = 0for key in prices: print prices[key]*stock[key] total += prices[key]*stock[key]print total

47 / 121

Page 48: Python - An Introduction

Lists and DictionariesA Day at the Supermarket

# take 3shopping_list = ["banana", "orange", "apple"]

stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15}

prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3}

def compute_bill(food): total = 0 for item in food: if stock[item] > 0: total += prices[item] stock[item] -= 1 return total

48 / 121

Page 49: Python - An Introduction

Student Becomes the TeacherList of Dicts

lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0]}alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0]}tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0]}

students = [lloyd,alice,tyler]for student in students: print student['name'] print student['homework'] print student['quizzes'] print student['tests']

49 / 121

Page 50: Python - An Introduction

Student Becomes the TeacherArithmetics Notes

5 / 2# 2

5.0 / 2# 2.5

float(5) / 2# 2.5 biar jadi float karena int/int

50 / 121

Page 51: Python - An Introduction

Student Becomes the Teacherlloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0] }alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0] }tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0] }# ---def average(numbers): total = sum(numbers) total = float(total) return total/len(numbers)def get_average(student): homework = average(student['homework']) quizzes = average(student['quizzes']) tests = average(student['tests']) return 0.1*homework + 0.3*quizzes + 0.6*testsdef get_letter_grade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" elif score >=60: return "D" else: return "F"def get_class_average(students): results = [] for student in students: results.append(get_average(student)) return average(results)

51 / 121

Page 52: Python - An Introduction

Student Becomes the Teacheraverage(A-List) returns floatget_average(A-Dict) returns float --> better get_numeric_grade()get_letter_grade(num) returns stringget_class_average(List-of-Dicts) returns float

# ---students=[lloyd,alice,tyler]print get_class_average(students)print get_letter_grade(get_class_average(students))# ---#classaverage = get_class_average([lloyd,alice,tyler]) # print get_letter_grade(get_average(lloyd))# ---

52 / 121

Page 53: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

53 / 121

Page 54: Python - An Introduction

Lists and FunctionsAccess to ListMethod: .append(), .pop(), .remove()Function: del

n = [1, 3, 5]# Do your multiplication heren[1] *= 5n.append(4)print n# [1, 15, 5, 4]

n = [1, 3, 5]# Remove the first item in the list heren.pop(0) # atau del(n[0]) # n.remove(1) value 1, bukan index# pop nge-return value, del voidprint n

54 / 121

Page 55: Python - An Introduction

Lists and Functions# 1n = "Hello"def string_function(s): return s+' world'

print string_function(n)

# 2def list_function(x): x[1]+=3 return x

n = [3, 5, 7]print list_function(n)

# 3n = [3, 5, 7]

def list_extender(lst): lst.append(9); return lst

print list_extender(n)

55 / 121

Page 56: Python - An Introduction

Lists and Functions# 1 rangen = [3, 5, 7]

def print_list(x): for i in range(0, len(x)): print x[i]

print_list(n)# each line: 3 5 7

# 2n = [3, 5, 7]

def double_list(x): for i in range(0, len(x)): x[i] = x[i] * 2 return x

print double_list(n)# [6, 10, 14]

56 / 121

Page 57: Python - An Introduction

Lists and Functions# 3range(6) # => [0,1,2,3,4,5]range(1,6) # => [1,2,3,4,5]range(1,6,3) # => [1,4]# range(stop)# range(start, stop)# range(start, stop, step)

def my_function(x): for i in range(0, len(x)): x[i] = x[i] * 2 return x

print my_function(range(3)) # [0, 1, 2]# [0, 2, 4]

57 / 121

Page 58: Python - An Introduction

Lists and Functions# 1n = [3, 5, 7]

def total(numbers): result = 0 for num in numbers: #for i in range(len(numbers)): result += num #result += numbers[i] return result

# 2 n = ["Michael", "Lieberman"]

def join_strings(words): result = "" for item in words: result += item return result

print join_strings(n)

58 / 121

Page 59: Python - An Introduction

Lists and Functions# 3 operator + agak beda di python buat listm = [1, 2, 3]n = [4, 5, 6]

def join_lists(x,y): return x + y

print join_lists(m, n)# [1, 2, 3, 4, 5, 6]

# 4 List of Listsn = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

def flatten(lists): results=[] for numbers in lists: for num in numbers: results.append(num) return results

print flatten(n)# [1, 2, 3, 4, 5, 6, 7, 8, 9]

59 / 121

Page 60: Python - An Introduction

Lists and FunctionsBattleship!In this project you will build a simplified, one-player version of the classicboard game Battleship! In this version of the game, there will be a single shiphidden in a random location on a 5x5 grid. The player will have 10 guesses totry to sink the ship.

# tambah elemen berulang pd listboard = []

for i in range(5): board.append(["O"]*5) #["O","O","O","O","O"]print board #[[],..,[]]

# print 1 row elemen, 1 barisboard = []

for i in range(5): board.append(["O"]*5) #["O","O","O","O","O"]

def print_board(board): for row in board: print row

print_board(board)

60 / 121

Page 61: Python - An Introduction

Lists and FunctionsBattleship! (1)

# .join method uses the string to combine the items in the list.from random import randint

board = []for x in range(5): board.append(["O"] * 5)

def print_board(board): for row in board: print " ".join(row)

print "Let's play Battleship!"print_board(board)# ---def random_row(board): return randint(0, len(board) - 1)def random_col(board): return randint(0, len(board[0]) - 1)

ship_row = random_row(board)ship_col = random_col(board)#print ship_row#print ship_col

61 / 121

Page 62: Python - An Introduction

Lists and FunctionsBattleship! (2)

# Multi line if condition, guess_row not in range(5)for turn in range(4): print "Turn", turn + 1 guess_row = int(raw_input("Guess Row:")) guess_col = int(raw_input("Guess Col:"))

if guess_row == ship_row and guess_col == ship_col: print "Congratulations! You sank my battleship!" break else: if (guess_row < 0 or guess_row > 4) or \ (guess_col < 0 or guess_col > 4): print "Oops, that's not even in the ocean." elif(board[guess_row][guess_col] == "X"): print "You guessed that one already." else: print "You missed my battleship!" board[guess_row][guess_col] = "X" print_board(board) if turn == 3: print "Game Over"

62 / 121

Page 63: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

63 / 121

Page 64: Python - An Introduction

Loopswhile

# 1 count = 0

if count < 5: print "Hello, I am an if statement and count is", count

while count <= 9: print "Hello, I am a while and count is", count count += 1# ---# 2 loop_condition = True

while loop_condition: print "I am a loop" loop_condition = False# ---# 3 num = 1

while num <= 10: # Fill in the condition print num ** 2 num += 1

64 / 121

Page 65: Python - An Introduction

Loopswhile, break

# ---# 4 choice = raw_input('Enjoying the course? (y/n)')

while choice != 'y' and choice != 'n': choice = raw_input("Sorry, I didn't catch that. Enter again: ")# ---# 5 breakcount = 0

while True: print count count += 1 if count >= 10: break# ---

65 / 121

Page 66: Python - An Introduction

Loopswhile, else

# 6 while elseimport random

print "Lucky Numbers! 3 numbers will be generated."print "If one of them is a '5', you lose!"

count = 0while count < 3: num = random.randint(1, 6) print num if num == 5: print "Sorry, you lose!" break count += 1else: print "You win!"

#else dieksekusi setelah kondisi false; tdk pernah dieksekusi setelah break# ---

66 / 121

Page 67: Python - An Introduction

Loopswhile, else

# 7from random import randint

# Generates a number from 1 through 10 inclusiverandom_number = randint(1, 10)

guesses_left = 3while guesses_left > 0: guess = int(raw_input("Your guess: ")) if guess == random_number: print 'You win!' break guesses_left -=1else: print 'You lose.'

67 / 121

Page 68: Python - An Introduction

Loopsfor in

# 1hobbies = []

# Add your code below!for i in range(3): hobby = str(raw_input("Your hobby: ")) hobbies.append(hobby);

# ---# 2thing = "spam!"

for c in thing: print c

word = "eggs!"

for c in word: print c

68 / 121

Page 69: Python - An Introduction

Loopsfor inLooping over a Dictionary

# 3phrase = "A bird in the hand..."

for char in phrase: if char == "A" or char =="a": print "X", else: print char,print

# The , character after our print statement means that # our next print statement keeps printing on the same line. # plus 1 space# ---# 4 Looping over a dictionaryd = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d: print key + " " + d[key]

69 / 121

Page 70: Python - An Introduction

Loopsfor index,item in enumerate(list)for a, b in zip(list_a, list_b)

# 5 choices = ['pizza', 'pasta', 'salad', 'nachos']

print 'Your choices are:'for index, item in enumerate(choices): print index+1, item# ---# 6 list_a = [3, 9, 17, 15, 19]list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]

for a, b in zip(list_a, list_b): # Add your code here! if a > b: print a else: print b# zip will create pairs of elements when passed two lists, # and will stop at the end of the shorter list.# zip can handle three or more lists as well!

70 / 121

Page 71: Python - An Introduction

Loopsfor, else

# 7 fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']

print 'You have...'for f in fruits: if f == 'tomato': print 'A tomato is not a fruit!' # (It actually is.) break print 'A', felse: print 'A fine selection of fruits!'# spt else while, kl break tidak dirun, kl exit normal yes# ---

71 / 121

Page 72: Python - An Introduction

Loopsfor, else

# 8fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape']

print 'You have...'for f in fruits: if f == 'tomato1': # kl match, executed; else never print 'A tomato is not a fruit!' break print 'A', felse: print 'A fine selection of fruits!'# ---# 9thelist = ['a','b','c']for i in thelist: print ielse: print 'completed!'

72 / 121

Page 73: Python - An Introduction

LoopsPractice Makes Perfect

# 1def is_even(x): if x % 2 == 0 : return True else: return False# ---

# 2 def is_int(x): delta = x - int(x) if delta == 0.0: return True else: return False

print is_int(7.0)print is_int(7.9)print is_int(-1.2)

73 / 121

Page 74: Python - An Introduction

LoopsPractice Makes Perfect

# ---# 3 cast ke str, cast ke int

def digit_sum(n): dsum = 0 n_str = str(n) for c in n_str: dsum += int(c) return dsum

print digit_sum(1234)# ---

# 4 rekursifdef factorial(x): if x==1: return 1 else: return x*factorial(x-1)

print factorial(4)

74 / 121

Page 75: Python - An Introduction

LoopsPractice Makes Perfect

# 5def is_prime(x): if x < 2 : return False for n in range(x-2): if x % (n+2) == 0: return False return True

print is_prime(9)print is_prime(11)# ---

# 6def reverse(text): varlength = len(text) textrev = "" for i in range(varlength): textrev += text[varlength-1-i] return textrev

print reverse('abc@def')

# You may not use reversed or [::-1] to help you with this.# ---

75 / 121

Page 76: Python - An Introduction

LoopsPractice Makes Perfect

# 7def anti_vowel(text): rettext = "" for c in text: clower=c.lower() if clower != 'a' and clower != 'e' and clower != 'i' and clower != 'o' and \ clower != 'u': rettext += c return rettextprint anti_vowel("Hey You!")# ---

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l"

def scrabble_score(word): wordlower = word.lower() retscore = 0 for c in wordlower: for key in score: if key == c: retscore += score[key] break return retscoreprint scrabble_score("Helix")

76 / 121

Page 77: Python - An Introduction

LoopsPractice Makes Perfect

# 8def censor(text,word): textlist = text.split() textlist_new = [] for item in textlist: if item != word: textlist_new.append(item) else: textlist_new.append("*" * len(item)) return " ".join(textlist_new)

print censor("this hack is wack hack", "hack")# ---

# 9def count(sequence,item): varcount = 0 for i in sequence: if i == item: varcount += 1 return varcount

print count([1,2,1,1], 1)

77 / 121

Page 78: Python - An Introduction

LoopsPractice Makes Perfect

# 10def purify(numbers): retnumbers = [] for num in numbers: if num % 2 == 0: retnumbers.append(num) return retnumbers

print purify([1,2,3])# ---

# 11def product(numlist): res = 1 for num in numlist: res *= num return res

print product([4, 5, 5])

78 / 121

Page 79: Python - An Introduction

LoopsPractice Makes Perfect

# 12def remove_duplicates(varlist): newlist = [] for var in varlist: if var not in newlist: newlist.append(var) return newlist

print remove_duplicates([1,1,2,2])# ---

# 13 sorted()# The median is the middle number in a sorted sequence of numbers. If you are given a sequence with # an even number of elements, you must average the two elements surrounding the middle.# sorted([5, 2, 3, 1, 4]) --> [1, 2, 3, 4, 5]

def median(varlist): sortedlist = sorted(varlist) length = len(varlist) if length % 2 == 1: return sortedlist[ ((length+1) / 2 - 1) ] else: return (sortedlist[length/2 - 1] + sortedlist[length/2])/2.0

print median([1,1,2]) # 1print median([7,3,1,4]) # 3.5

79 / 121

Page 80: Python - An Introduction

Exam Statistics(1)

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades): for grade in grades: print grade

def grades_sum(scores): total = 0 for score in scores: total += score return total

def grades_average(grades): return grades_sum(grades)/float(len(grades))

print_grades(grades)print grades_sum(grades)print grades_average(grades)

80 / 121

Page 81: Python - An Introduction

Exam Statistics(2)

def grades_average(grades): sum_of_grades = grades_sum(grades) average = sum_of_grades / float(len(grades)) return average

def grades_variance(scores): average = grades_average(scores) variance = 0 for score in scores: variance += (average - score) ** 2 variance /= float(len(scores)) return variance

def grades_std_deviation(variance): return variance ** 0.5

variance = grades_variance(grades)

print print_grades(grades)print grades_sum(grades)print grades_average(grades)print grades_variance(grades)print grades_std_deviation(variance)

81 / 121

Page 82: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

82 / 121

Page 83: Python - An Introduction

Advanced Topics in PythonList Comprehensions

my_dict = { "Name": "Otong", "Age": 23, "Title": "Dr"}print my_dict.items() # [('Age', 23), ('Name', 'Otong'), ('Title', 'Dr')]print my_dict.keys() # ['Age', 'Name', 'Title']print my_dict.values() # [23, 'Otong', 'Dr']

for key in my_dict: print key, my_dict[key]

# Age 23# Name Otong# Title Dr# You should use print a, b rather than print a + " " + b

What if we wanted to generate a list according to some logic—for example, alist of all the even numbers from 0 to 50? List comprehensions are apowerful way to generate lists using the for/in and if keywords

83 / 121

Page 84: Python - An Introduction

Advanced Topics in PythonList Comprehensions

# 1evens_to_50 = [i for i in range(51) if i % 2 == 0]print evens_to_50

# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]

# ---------------------------------------------------------# 2new_list = [x for x in range(1,6)] # => [1, 2, 3, 4, 5]doubles = [x*2 for x in range(1,6)] # => [2, 4, 6, 8, 10]doubles_by_3 = [x*2 for x in range(1,6) if (x*2)%3 == 0] # => [6]

# ---------------------------------------------------------# 3doubles_by_3 = [x*2 for x in range(1,6) if (x*2) % 3 == 0]

even_squares = [x**2 for x in range(1,12) if x%2 == 0]

print even_squares # [4, 16, 36, 64, 100]# ---------------------------------------------------------

84 / 121

Page 85: Python - An Introduction

Advanced Topics in PythonList Comprehensions

# 4c = ['C' for x in range(5) if x < 3]print c # ['C', 'C', 'C']

# ---------------------------------------------------------# 5cubes_by_four = [x**3 for x in range(1,11) if (x**3)%4 == 0]print cubes_by_four # [8, 64, 216, 512, 1000]

# ---------------------------------------------------------# 6l = [i ** 2 for i in range(1, 11)] # Should be [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

print l[2:9:2] # [9, 25, 49, 81] -- 2 belakang, 1 diloncat

# list slicing# [start:end:stride]# Where start describes where the slice starts (inclusive), end is where it ends (exclusive), # and stride describes the space between items in the sliced list. For example, a stride of 2 # would select every other item from the original list to place in the sliced list.# start & end --> index

85 / 121

Page 86: Python - An Introduction

Advanced Topics in PythonList Comprehensions

# 7to_five = ['A', 'B', 'C', 'D', 'E']

print to_five[3:] #['D', 'E'] print to_five[:2] #['A', 'B']print to_five[::2] #['A', 'C', 'E']

"""The default starting index is 0.The default ending index is the end of the list.The default stride is 1."""# ---------------------------------------------------------# 8my_list = range(1, 11) # List of numbers 1 - 10

print my_list[::2] #[1, 3, 5, 7, 9]# ---------------------------------------------------------

86 / 121

Page 87: Python - An Introduction

Advanced Topics in PythonList Comprehensions

# 9letters = ['A', 'B', 'C', 'D', 'E']print letters[::-1] #['E', 'D', 'C', 'B', 'A'] right to left

# ---------------------------------------------------------# 10my_list = range(1, 11)

backwards = my_list[::-1]

# ---------------------------------------------------------# 11to_one_hundred = range(101)

backwards_by_tens = to_one_hundred[::-10]print backwards_by_tens #[100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]

# ---------------------------------------------------------# 12to_21 = range(1,22)

odds = to_21[::2]middle_third = to_21[7:14]

87 / 121

Page 88: Python - An Introduction

Advanced Topics in PythonLambda - Anonymous FunctionFunctional programming : means that you're allowed to pass functions aroundjust as if they were variables or values. The function the lambda creates is ananonymous function. Lambdas are useful when you need a quick function todo some work for you.

When we pass the lambda to filter, filter uses the lambda to determine whatto filter, and the second argument (my_list, which is just the numbers 0 – 15) isthe list it does the filtering on.

# ekivalenlambda x: x % 3 == 0def by_three(x): return x % 3 == 0

# ---------------------------------------------------------# filtermy_list = range(16)

print filter(lambda x: x % 3 == 0, my_list)# yg bs di bagi 3 -> [0, 3, 6, 9, 12, 15]# ---------------------------------------------------------

88 / 121

Page 89: Python - An Introduction

Advanced Topics in PythonLambda - Anonymous Function

languages = ["HTML", "JavaScript", "Python", "Ruby"]print filter(lambda x: x=="Python", languages) # ['Python']# ---------------------------------------------------------

cubes = [x**3 for x in range(1, 11)]print filter(lambda x: x % 3 == 0, cubes)# ---------------------------------------------------------

squares = [x**2 for x in range(1,11)]print filter(lambda x: x > 30 and x <=70, squares) # [36, 49, 64]# ---------------------------------------------------------

89 / 121

Page 90: Python - An Introduction

Advanced Topics in PythonRecap

# Dictionarymovies = { "Monty Python and the Holy Grail": "Great", "Monty Python's Life of Brian": "Good", "Monty Python's Meaning of Life": "Okay"}print movies.items()#for key in movies: print key, my_dict[key]# ---------------------------------------------------------

# Comprehending Comprehensionssquares = [x**2 for x in range(5)]

threes_and_fives = [x for x in range(1,16) if x%3 == 0 or x%5 ==0]

90 / 121

Page 91: Python - An Introduction

Advanced Topics in PythonRecap

# List Slicingstr = "ABCDEFGHIJ"# str[start:end:stride] -> start, end, stride = 1, 6, 2# ---------------------------------------------------------

garbled = "!XeXgXaXsXsXeXmX XtXeXrXcXeXsX XeXhXtX XmXaX XI"rev_garbled = garbled[::-1]message = rev_garbled[::2]print message# ---------------------------------------------------------

# Lambdamy_list = range(16)print filter(lambda x: x % 3 == 0, my_list)

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"message = filter(lambda x: x != 'X',garbled)print message

91 / 121

Page 92: Python - An Introduction

Advanced Topics in PythonBitwise Operators

# bitwise operatorprint 5 >> 4 # Right Shift 101 -> 0print 5 << 1 # Left Shift 101 -> 1010print 8 & 5 # Bitwise AND 1000 & 0101 -> 0print 9 | 4 # Bitwise ORprint 12 ̂ 42 # Bitwise XORprint ~88 # Bitwise NOT

# 0 | 10 | 0 | 13 | 38 | -89# ---------------------------------------------------------

# hasil operasi print -> desimalprint 0b1, #1print 0b10, #2print 0b11, #3print 0b100, #4print 0b101, #5print 0b110, #6print 0b111 #7#1 2 3 4 5 6 7

print 0b1 + 0b11 #4print 0b11 * 0b11 #9

92 / 121

Page 93: Python - An Introduction

Advanced Topics in PythonBitwise Operators

one = 0b1; two = 0b10; three = 0b11; four = 0b100; five = 0b101; six = 0b110; seven = 0b111; eight = 0b1000; nine = 0b1001; ten = 0b1010; eleven = 0b1011; twelve = 0b1100

print eight# ---------------------------------------------------------

print bin(1) #0b1print bin(2) #0b10print bin(3) #0b11print bin(4) #0b100print bin(5) #0b101# ---------------------------------------------------------

print int("1",2) # 1print int("10",2) # 2print int("111",2) # 7print int("0b100",2) # 4print int(bin(5),2) # 5# Print out the decimal equivalent of the binary 11001001.print int("0b11001001",2) # 201# base 2 semua, pakei 0b atau gak, sama

93 / 121

Page 94: Python - An Introduction

Advanced Topics in PythonBitwise Operators

shift_right = 0b1100shift_left = 0b1

shift_right = shift_right >> 2shift_left = shift_left << 2

print bin(shift_right) # 0b11print bin(shift_left) # 0b100# ---------------------------------------------------------

print bin(0b1110 & 0b101) # 0b100print bin(0b1110 | 0b101) # 0b1111print bin(0b1110 ̂ 0b101) # xor 0b1011# ---------------------------------------------------------

print ~1 #-2print ~2 #-3print ~3 #-4print ~42 #-43print ~123 #-124# this is equivalent to adding one to the number and then making it negative.

94 / 121

Page 95: Python - An Introduction

Advanced Topics in PythonBitwise Operators

# maskdef check_bit4(input): # input reserved? mask = 0b1000 desired = input & mask if desired > 0: return "on" else: return "off"# ---------------------------------------------------------

a = 0b10111011mask = 0b100print bin(a | mask) # 0b10111111print bin(a ̂ mask) # 0b10111111# ---------------------------------------------------------

a = 0b11101110mask = 0b11111111 #flip *semua* bit aprint bin(a ̂ mask) # 0b10001

# ---------------------------------------------------------def flip_bit(number,n): mask = (0b1 << (n-1)) result = number ̂ mask return bin(result)

#xor kalau mask 1 nge-flip; kalo mask 0 tetap

95 / 121

Page 96: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

96 / 121

Page 97: Python - An Introduction

Introduction to ClassesPython is an object-oriented programming language, which means itmanipulates programming constructs called objects. You can think of anobject as a single data structure that contains data as well as functions;functions of objects are called methods.

class Fruit(object): """A class that makes various tasty fruits.""" def __init__(self, name, color, flavor, poisonous): self.name = name self.color = color self.flavor = flavor self.poisonous = poisonous

def description(self): print "I'm a %s %s and I taste %s." % (self.color, self.name, self.flavor)

def is_edible(self): if not self.poisonous: print "Yep! I'm edible." else: print "Don't eat me! I am super poisonous."

# ---------------------------------------------------------lemon = Fruit("lemon", "yellow", "sour", False)

lemon.description()lemon.is_edible()

97 / 121

Page 98: Python - An Introduction

Introduction to Classespass doesn't do anything, but it's useful as a placeholder in areas of yourcode where Python expects an expression__init__() function is required for classes, and it's used to initialize theobjects it creates. __init__() always takes at least one argument, self, thatrefers to the object being created. You can think of __init__() as thefunction that "boots up" each object the class creates.We can access attributes of our objects using dot notation

class Animal(object): pass

class Animal(object): def __init__(self): pass

class Animal(object): def __init__(self,name): self.name = name

zebra = Animal("Jeffrey")print zebra.name# ---------------------------------------------------------

class Square(object): def __init__(self): self.sides = 4

my_shape = Square()print my_shape.sides

98 / 121

Page 99: Python - An Introduction

Introduction to Classesclass Animal(object): """Makes cute animals.""" def __init__(self, name, age, is_hungry): self.name = name self.age = age self.is_hungry = is_hungry

# Note that self is only used in the __init__() function *definition*; # we don't need to pass it to our instance objects.

zebra = Animal("Jeffrey", 2, True)giraffe = Animal("Bruce", 1, False)panda = Animal("Chad", 7, True)

print zebra.name, zebra.age, zebra.is_hungry # Jeffrey 2 Trueprint giraffe.name, giraffe.age, giraffe.is_hungry # Bruce 1 Falseprint panda.name, panda.age, panda.is_hungry # Chad 7 True

99 / 121

Page 100: Python - An Introduction

Introduction to ClassesClass ScopeAnother important aspect of Python classes is scope. The scope of a variable isthe context in which it's visible to the program.

It may surprise you to learn that not all variables are accessible to all parts ofa Python program at all times. When dealing with classes, you can havevariables that are available everywhere (global variables), variables that areonly available to members of a certain class (member variables), and variablesthat are only available to particular instances of a class (instance variables).

100 / 121

Page 101: Python - An Introduction

Introduction to ClassesClass Scope

variables: global, member, instance

class Animal(object): """Makes cute animals.""" is_alive = True def __init__(self, name, age): self.name = name self.age = age

zebra = Animal("Jeffrey", 2)giraffe = Animal("Bruce", 1)panda = Animal("Chad", 7)

print zebra.name, zebra.age, zebra.is_alive #Jeffrey 2 Trueprint giraffe.name, giraffe.age, giraffe.is_alive #Bruce 1 Trueprint panda.name, panda.age, panda.is_alive #Chad 7 True

#is_alive member variable; name,age instance variables

101 / 121

Page 102: Python - An Introduction

Introduction to ClassesClass Scope

class Animal(object): is_alive = True def __init__(self, name, age): self.name = name self.age = age

def description(self): print self.name print self.age

hippo = Animal("Nil",5)hippo.description()# ---------------------------------------------------------hippo = Animal("Jake", 12)cat = Animal("Boots", 3)print hippo.is_alive # True

hippo.is_alive = Falseprint hippo.is_alive # Falseprint cat.is_alive # True

102 / 121

Page 103: Python - An Introduction

Introduction to ClassesClass Scope

class Animal(object): is_alive = True health = "good" def __init__(self, name, age): self.name = name self.age = age def description(self): print self.name print self.age

hippo = Animal("Nil",5)hippo.description()sloth = Animal("slothName", 6)ocelot = Animal("ocelotName",7)

print hippo.health # goodprint sloth.health # goodprint ocelot.health # good

#member variable = class variable (bukan instance, available to all inst)

103 / 121

Page 104: Python - An Introduction

Introduction to ClassesClass Scope

class ShoppingCart(object): """Creates shopping cart objects for users of our fine website.""" items_in_cart = {}

def __init__(self, customer_name): self.customer_name = customer_name

def add_item(self, product, price): """Add product to the cart.""" if not product in self.items_in_cart: self.items_in_cart[product] = price print product + " added." else: print product + " is already in the cart."

def remove_item(self, product): """Remove product from the cart.""" if product in self.items_in_cart: del self.items_in_cart[product] print product + " removed." else: print product + " is not in the cart."

104 / 121

Page 105: Python - An Introduction

Introduction to ClassesClass Scope

my_cart1 = ShoppingCart("Otong")my_cart2 = ShoppingCart("Udjang")

my_cart1.add_item("Obeng",2000) # Obeng added.my_cart1.add_item("Obeng",1000) # Obeng is already in the cart.print my_cart1.items_in_cart # {'Obeng': 2000}

my_cart2.add_item("Sapu",2000) # Sapu added.my_cart2.add_item("Sapu",1000) # Sapu is already in the cart.print my_cart2.items_in_cart # {'Obeng': 2000, 'Sapu': 2000}

my_cart1.add_item("Sapu",5000) # Sapu is already in the cart.print my_cart1.items_in_cart # {'Obeng': 2000, 'Sapu': 2000}

my_cart2.items_in_cart = {'Buku': 3000}print my_cart2.items_in_cart # {'Buku': 3000}print my_cart1.items_in_cart # {'Obeng': 2000, 'Sapu': 2000}

my_cart2.add_item("Sapu",1000) # Sapu added.print my_cart2.items_in_cart # {'Sapu': 1000, 'Buku': 3000}

my_cart1.add_item("Dodol",10000) # Dodol added.print my_cart1.items_in_cart # {'Obeng': 2000, 'Sapu': 2000, 'Dodol': 10000}print my_cart2.items_in_cart # {'Sapu': 1000, 'Buku': 3000}

105 / 121

Page 106: Python - An Introduction

Introduction to ClassesClasses can be very useful for storing complicated objects with their ownmethods and variables. Defining a class is much like defining a function, butwe use the class keyword instead.

We also use the word object in parentheses because we want our classes toinherit the object class. This means that our class has all the properties of anobject, which is the simplest, most basic class. Later we'll see that classes caninherit other, more complicated classes.

Notes for Variables:

To create instance variables, initialize them in the init functionWhen Python sees self.X (object.X) it looks if there's a property X in yourobject, and if there is none, it looks at its class.

106 / 121

Page 107: Python - An Introduction

Introduction to ClassesInheritanceInheritance is the process by which one class takes on the attributes andmethods of another, and it's used to express an is-a relationship.

For example, a Panda is a bear, so a Panda class could inherit from a Bearclass.

class Customer(object): """Produces objects that represent customers.""" def __init__(self, customer_id): self.customer_id = customer_id

def display_cart(self): print "I'm a string that stands in for the contents of your shopping cart!"

class ReturningCustomer(Customer): """For customers of the repeat variety.""" def display_order_history(self): print "I'm a string that stands in for your order history!"

monty_python = ReturningCustomer("ID: 12345")

monty_python.display_cart() # Customer.display_cartmonty_python.display_order_history() # ReturningCustomer

107 / 121

Page 108: Python - An Introduction

Introduction to ClassesInheritance

class Shape(object): """Makes shapes!""" def __init__(self, number_of_sides): self.number_of_sides = number_of_sides

class Triangle(Shape): def __init__(self,side1,side2,side3): self.side1 = side1 self.side2 = side2 self.side3 = side3

# Override __init__segitiga2 = Triangle(1,2,3)print segitiga2.side1

#print segitiga2.number_of_sides #super?

108 / 121

Page 109: Python - An Introduction

Introduction to ClassesInheritance

class Employee(object): def __init__(self, name): self.name = name def greet(self, other): print "Hello, %s" % other.name

class CEO(Employee): def greet(self, other): print "Get back to work, %s!" % other.name

ceo = CEO("Emily")emp = Employee("Steve")

emp.greet(ceo) # Hello, Emilyceo.greet(emp) # Get back to work, Steve!

109 / 121

Page 110: Python - An Introduction

Introduction to ClassesInheritance

Sometimes you'll want one class that inherits from another to not onlytake on the methods and attributes of its parent, but to override one ormore of them.On the flip side, sometimes you'll be working with a derived class (orsubclass) and realize that you've overwritten a method or attributedefined in that class' base class (also called a parent or superclass) thatyou actually need. Have no fear! You can directly access the attributes ormethods of a superclass with Python's built-in super call.

110 / 121

Page 111: Python - An Introduction

Introduction to ClassesInheritance

class Employee(object): """Models real-life employees!""" def __init__(self, employee_name): self.employee_name = employee_name

def calculate_wage(self, hours): self.hours = hours return hours * 20.00

class PartTimeEmployee(Employee): def calculate_wage(self,hours): self.hours = hours return hours * 12.00 def full_time_wage(self,hours): return super(PartTimeEmployee,self).calculate_wage(hours)

milton = PartTimeEmployee("Milton")print milton.full_time_wage(10)

111 / 121

Page 112: Python - An Introduction

Introduction to Classesclass Triangle(object): number_of_sides = 3 def __init__(self, angle1, angle2, angle3): self.angle1 = angle1 self.angle2 = angle2 self.angle3 = angle3 def check_angles(self): sum_angle = self.angle1 + self.angle2 + self.angle3 if sum_angle == 180: return True else: return False

class Equilateral(Triangle): angle = 60 def __init__(self): self.angle1 = self.angle self.angle2 = self.angle self.angle3 = self.angle

my_triangle = Triangle(90,30,60)print my_triangle.number_of_sides # 3 inheritedprint my_triangle.check_angles() # True

112 / 121

Page 113: Python - An Introduction

Introduction to Classesclass Car(object): condition = "new" def __init__(self, model, color, mpg): self.model = model; self.color = color; self.mpg = mpg def display_car(self): return "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg)) def drive_car(self): self.condition = "used"

class ElectricCar(Car): def __init__(self,model,color,mpg,battery_type): super(ElectricCar,self).__init__(model,color,mpg) self.battery_type = battery_type def drive_car(self): self.condition="like new"

my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")

print my_car.condition # new my_car.drive_car() # print my_car.condition # like new

my_car1 = Car("DeLorean", "silver", 88)print my_car1.condition # newprint my_car1.model # DeLorean print my_car1.display_car() # This is a silver DeLorean with 88 MPG.

my_car1.drive_car()print my_car1.condition # used

113 / 121

Page 114: Python - An Introduction

Introduction to ClassesUsually, classes are most useful for holding and accessing abstractcollections of data.One useful class method to override is the built-in __repr__() method,which is short for representation; by providing a return value in thismethod, we can tell Python how to represent an object of our class (forinstance, when using a print statement).

class Point3D(object): def __init__(self,x,y,z): self.x = x self.y = y self.z = z def __repr__(self): return "(%d, %d, %d)" % (self.x, self.y, self.z)

my_point = Point3D(1,2,3)print my_point # (1, 2, 3)

114 / 121

Page 115: Python - An Introduction

Agenda1. Python Syntax2. Strings and Console Output3. Conditionals and Control Flow4. Functions5. Lists and Dictionaries6. Lists and Functions7. Loops8. Advanced Topics in Python9. Introduction to Classes

10. File Input and Output

115 / 121

Page 116: Python - An Introduction

File Input and OutputBuilt-in io functionsWrite

# Generates a list of squares of the numbers 1 - 10my_list = [i**2 for i in range(1,11)]

f = open("output.txt", "w")

for item in my_list: f.write(str(item) + "\n")

f.close()

# This told Python to open output.txt in "w" mode ("w" stands for "write"). # We stored the result of this operation in a file object, f.

116 / 121

Page 117: Python - An Introduction

File Input and OutputWrite

my_list = [i**2 for i in range(1,11)]

my_file = open("output.txt", "r+")

for i in my_list: my_file.write(str(i) + "\n")

my_file.close()

# "r+" as a second argument to the function so the # file will allow you to read and write

117 / 121

Page 118: Python - An Introduction

File Input and OutputRead

my_file = open("output.txt","r")print my_file.read()my_file.close()

# ---# if we want to read from a file line by line, # rather than pulling the entire file in at once.

my_file = open("text.txt","r")print my_file.readline()print my_file.readline()print my_file.readline()my_file.close()

118 / 121

Page 119: Python - An Introduction

File Input and OutputDuring the I/O process, data is buffered: this means that it is held in atemporary location before being written to the file.Python doesn't flush the buffer—that is, write data to the file—until it'ssure you're done writing. One way to do this is to close the file. If youwrite to a file without closing, the data won't make it to the target file.

# Open the file for readingread_file = open("text.txt", "r")

# Use a second file handler to open the file for writingwrite_file = open("text.txt", "w")

# Write to the filewrite_file.write("Not closing files is VERY BAD.")write_file.close()

# Try to read from the fileprint read_file.read()read_file.close()

119 / 121

Page 120: Python - An Introduction

File Input and OutputFile objects contain a special pair of built-in methods: __enter__() and__exit__(). The details aren't important, but what is important is thatwhen a file object's __exit__() method is invoked, it automatically closesthe file. How do we invoke this method? With with and as.

with open("text.txt", "w") as textfile: textfile.write("Success!")# gak perlu close, close automatically# ---------------------------------------------------------

f = open("bg.txt")f.closed # Falsef.close()f.closed # True# ---------------------------------------------------------

with open("text.txt","w") as my_file: my_file.write("otong");if my_file.closed == False: my_file.close()print my_file.closed

120 / 121

Page 121: Python - An Introduction

Python - An Introduction

ENDEueung Mulyana | http://eueung.github.io/EL6240/py

based on the material @Codecademy

Attribution-ShareAlike CC BY-SA

121 / 121