[['Connie', 'F', 8], ['Jacqueline', 'F', 14], ... ]
kids.sort( )
kids_file = open("kids.txt", "r") # Initialize two-d array (list of lists). kids = [ ] # Throw away first line kids_file.readline( ) for line in kids_file: fields = line.strip( ).split(" ") row = [fields[0], fields[1], int(fields[2])] kids.append(row) kids.sort( ) print(kids) for k in kids: print(k)we also looked at the older style with a loop that requires two readline statements, one before the loop starts and one at the the end of the loop:
kids_file = open("kids.txt", "r") # Initialize two-d array (list of lists). kids = [ ] # Throw away first line kids_file.readline( ) line = kids_file.readline( ) while line != "": fields = line.strip( ).split(" ") row = [fields[0], fields[1], int(fields[2])] kids.append(row) line = kids_file.readline( ) kids.sort( ) print(kids) for k in kids: print(k)
a = 3 print(a + 5, a.__add__(5)) print(a * 5, a.__mul__(5)) # Output 8 8 15 15
class Pair: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return f'({self.x}, {self.y})'
(3, 4) + (5, 6) = (8, 10) (3, 4) * 2 = (6, 8)
person = { "name" : "Alice", "gender" : "F", "age" : 11 }
value = person["gender"] print(value) # Output: F
print(person["age"])
person["cell_number"] = "312/456-7890" print(person)
Key | Value |
---|---|
"one" | "uno" |
"two" | "dos" |
"three" | "tres" |
"four" | "quatro" |
"five" | "cinco" |
spanish_number = {"one":"uno", "two":"dos", "three":"tres", "four":"quatro", "five":"cinco"} english = input("Enter an English number: ") if english in spanish_number: print("Spanish number: ", spanish_number[english]) else: print("Number not between 1 and 5")
import kid kids_file = open("kids.txt", "r") # Initialize dictionary. kids_dict = { } # Throw away first line kids_file.readline( ) for line in kids_file: fields = line.strip( ).split(" ") k = kid.Kid(fields[0], fields[1], int(fields[2])) kids_dict[fields[0]] = k # print(kids_dict) name = input("Enter a name: ") if name in kids_dict: k = kids_dict[name] print("Gender:" , k.gender, "Age:", k.age) else: print("Name not in dictionary.")
from pet import Pet pets = [ ] fin = open("pets.txt", "r") # Throw away first line. fin.readline( ) for line in fin: fields = line.split(",") p = Pet(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5]) pets.append(p) pets.sort( ) print(pets)
Animal ← Pet
Here is an input file
pets.txt with Pet information.from pet import Pet # Create empty dictionary pets_dict = { } # Open input file fin1 = open("pets.txt", "r") # Throw away first line fin1.readline( ) for line in fin1: fields = line.split(",") p = Pet(fields[0], fields[1], fields[2], \ fields[3], fields[4], bool(fields[5].strip( ))) pets_dict[fields[3]] = p print(pets_dict)
\A\d{5}\Z
\A\d{5}(-\d{4})?\Z
\A-?[1-9]\d*\Z
\A[a-z_]([a-z0-9]_)*\Z
# Import the Python regular expression module. import re reg_exp = << Your regular expression >> input_str = << Your input string >> print(re.match(reg_exp, input_str)Output: None if there is no match; a match object representing the first match if there is at least one match.