a = ['club', 'diamond', 'heart', 'spade'] print(a[0][3], a[0][1], a[0][2], a[2][1], \ sep='', end=' ') print(a[1][0], a[3][4], a[1][3], a[1][4], \ a[1][5], a[3][0], sep='')Ans: blue demons. The first index is the string in the list. The second index is the letter in that string.
s = 'apple' print(str(s), repr(s))
apple 'apple'apple is the user friendly representation; 'apple' is the technical representation.
=== Module: person.py ====================== class Person: def __init__(self, name, gender, age): self.name = name self.gender = gender self.age = age === Module: test1.py ======================= from person import Person p = Person("Bill", "M", 23) print(str(p)) print(repr(p)) === Output: ================================ <person.Person object at 0x032C1118> <person.Person object at 0x032C1118>
def __str__(self): return f"{self.name};{self.gender};{self.age}" # Output: Alice;F;11 <person.Person object at 0x034B1118>
def __repr__(self): return f"<{self.name};{self.gender};{self.age}>" # Output: Alice;F;11 <Alice;F;11>
def __repr__(self): return f"<{str(self)}>"
def __repr__(self): return str(self)
from card import Card c1 = Card(12, 'H') # Rank of Queen is 12 c2 = Card(14, 'H') # Rank of Ace is 14 c3 = Card(11, 'C') # Rank of Jack is 11 c4 = Card(11, 'H') c5 = Card(11, 'S') poker_hand = [c1, c2, c3, c4, c5] print(poker_hand)This is the output:
[<card.Card object at 0x038440B0>, <card.Card object at 0x03AA6950>, <card.Card object at 0x03AA6970>, <card.Card object at 0x03AA6910>, <card.Card object at 0x03CCBF30>]It turns out that when a list objects is printed like this: print(poker_hand), the __repr__ method is used by Python to display the individual objects in the list. Recall that, if we use the default repr method to print c2, which is the ace of hearts, we get
print(repr(c2)) # Output: <card.Card object at 0x03AA6950>This is the object from the Card class in the card module that is stored at the computer memory location 03AA6950 hex, which is not very useful for us. To get a more useful representation of the cards in the list, we need to define our own custom __repr__ method, which will be called in the print statement
print(repr(c2))
def __repr__(self): return '<' + str(self) + '>'and run the print statement again:
print(repr(c2)) # Output: <AH>
from card import Card c1 = Card(12, 'H') # Rank of Queen is 12 c2 = Card(14, 'H') # Rank of Ace is 14 c3 = Card(11, 'C') # Rank of Jack is 11 c4 = Card(11, 'H') c5 = Card(11, 'S') poker_hand = [c1, c2, c3, c4, c5] print(poker_hand) # Output: [<QH>, <AH>, <JC>, <JH>, <JS>]Now we get more useful output when printing the list because our custom __repr__ method is used when printing the list.
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC 2D 3C 4D 5C 6D 7C 8D 9C 10D JC QD KC AD 2H 3C 4H 5C 6H 7C 8H 9C 10H JC QH KC AH 2S 3C 4S 5C 6S 7C 8S 9C 10S JC QS KC AS Ans: # Add this __repr__ method to the # Card class: def __repr__(self): return '<' + str(self) + '>' # Script to create list of cards that # represents a standard 52 card poker deck. from card import Card cards = [ ] for suit in ['C', 'D', 'H', 'S']: for rank in range(2, 15): card = Card(rank, suit) cards.append(card) print(cards)
a = ['dog', 'gerbil', 'cat', 'rabbit', 'mouse'] b = [28, 15, 93, 31, 11, 10] Ans: a.sort( ) print(a) b.sort( ) print(b)
def __lt__(self, other): return self.age < other.age
# Source code file: test1.py from person import Person p1 = Person("Alice", "F", 11) p2 = Person("Bob", "M", 13) print(p1 < p2, p2 < p1) # Source code file: test2.py from person import Person import unittest class MyUnitTestClass(unittest.TestCase): def test_1(self): self.assertEqual(p1 < p2, True) self.assertEqual(p2 < p1, True) if __name__ == '__main__': unittest.main( )
if __name__ == '__main__': unittest.main( )that is found at the end of the unit test scripts that we use in IT 212.
Name of module: __main__
Name of module: module2
if __name__ == '__main__': unittest.main( )If the unit test script is being run directly by Python so that __name__ == '__main__', execute the script, but don't execute the script if it is being invoked by another module.