Python Programming · High School (9–10) · 2 courses, 26 worked problems, 30 practice questions
Much of the data in the world is text: messages, books, logs and web pages. Python stores text as strings and gives many tools to work with them. You can split a sentence into words with .split(), join words with .join(), change case with .lower(), and find or replace parts with .find() and .replace(). A loop and a dictionary can count how often each word appears. You can also clean messy input, check whether a word is a palindrome, make simple codes and read a whole text file.
Choose an answer, then check it. Each one shows a full explanation.
contacts = {"ravi": "98765"}. What does contacts.get("Ravi".lower()) give?
A message was coded with a shift of 19. Which shift, from 0 to 25, decodes it with the same function?
ranked = sorted([("Asha", 72), ("Ravi", 85), ("Meera", 64)], key=lambda p: -p[1]). What is ranked[0][0]?
commands = {"hi": greet}, where greet is a function. What does commands["hi"]() do?
Each one has a hint, the key idea and a full worked solution.
Splitting at a comma: what does print(len("a,b,,c".split(","))) show?
count() counts pieces that do not overlap. What does print("banana".count("an")) show?
A shift of 1 in the letter code: what is chr((ord("z") - ord("a") + 1) % 26 + ord("a"))?
What does print(" Hello World ".strip().lower()) show?
Why is a dictionary a good tool for a contact book?
What does contacts.get("zoya") give if there is no Zoya in the book?