

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















1 # Stores a name in a variable 2 3 name = input("What's your name? ") 4 print(f"hello, {name}")
names0.py
1 # Stores names in a list 2 3 names = [] 4 5 for _ in range(3): 6 names.append(input("What's your name? ")) 7 8 for name in sorted(names): 9 print(f"hello, {name}")
names1.py
1 # Writes to a file 2 3 name = input("What's your name? ") 4 5 file = open("names.txt", "w") 6 file.write(name) 7 file.close()
names2.py
1 # Appends to a file 2 3 name = input("What's your name? ") 4 5 file = open("names.txt", "a") 6 file.write(f"{name}\n") 7 file.close()
names3.py
1 # Reads from a file 2 3 with open("names.txt") as file: 4 lines = file.readlines() 5 6 for line in lines: 7 print("hello,", line.rstrip())
names5.py
1 # Reads from a file, one line at a time 2 3 with open("names.txt") as file: 4 for line in file: 5 print("hello,", line.rstrip())
names6.py
1 Hermione,Gryffindor 2 Harry,Gryffindor 3 Ron,Gryffindor 4 Draco,Slytherin
students0.csv
1 # Reads a CSV file 2 3 with open("students0.csv") as file: 4 for line in file: 5 row = line.rstrip().split(",") 6 print(f"{row[0]} is in {row[1]}")
students0.py
1 # Sorts a list of strings 2 3 students = [] 4 5 with open("students0.csv") as file: 6 for line in file: 7 name, house = line.rstrip().split(",") 8 students.append(f"{name} is in {house}") 9 10 for student in sorted(students): 11 print(student)
students2.py
1 # Reads a CSV file into a list of dict objects, creating empty dict first 2 3 students = [] 4 5 with open("students0.csv") as file: 6 for line in file: 7 name, house = line.rstrip().split(",") 8 student = {} 9 student["name"] = name 10 student["house"] = house 11 students.append(student) 12 13 for student in students: 14 print(f"{student['name']} is in {student['house']}")
students3.py
1 # Reads a CSV file into a list of dict objects 2 3 students = [] 4 5 with open("students0.csv") as file: 6 for line in file: 7 name, house = line.rstrip().split(",") 8 students.append({"name": name, "house": house}) 9 10 for student in students: 11 print(f"{student['name']} is in {student['house']}")
students5.py
1 # Sorts a list of dictionaries using a function 2 3 students = [] 4 5 with open("students0.csv") as file: 6 for line in file: 7 name, house = line.rstrip().split(",") 8 students.append({"name": name, "house": house}) 9 10 11 def get_name(student): 12 return student["name"] 13 14 15 for student in sorted(students, key=get_name): 16 print(f"{student['name']} is in {student['house']}")
students6.py
1 Harry,"Number Four, Privet Drive" 2 Ron,The Burrow 3 Draco,Malfoy Manor
students1.csv
1 # Reads a CSV file using csv.reader 2 3 import csv 4 5 students = [] 6 7 with open("students1.csv") as file: 8 reader = csv.reader(file) 9 for row in reader: 10 students.append({"name": row[0], "home": row[1]}) 11 12 for student in sorted(students, key=lambda student: student["name"]): 13 print(f"{student['name']} is from {student['home']}")
students8.py