Welcome to the Recap of Key Concepts Learned in our Introduction to Python course! In this final chapter, we will review the main concepts and skills that you have learned throughout the course.
Summary
Throughout this course, you learned about the basics of Python programming, including its syntax, data types, and control structures. You also learned about working with lists, dictionaries, and other data structures, as well as creating and using functions and classes.
You learned about handling exceptions and working with files, as well as connecting to and querying a database. Additionally, you were introduced to the concepts of data science and machine learning, and learned about the popular Python libraries NumPy, Pandas, and Matplotlib for data manipulation and visualization.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.
Write a function that takes a list of numbers as an argument and returns the sum of the numbers.
def sum_list(numbers):
total = 0
for number in numbers:
total += number
return total
print(sum_list([1, 2, 3])) # 6
print(sum_list([5, 10, 15])) # 30
Write a function that takes a list of strings and returns a new list with the strings sorted in alphabetical order.
def sort_strings(strings):
return sorted(strings)
print(sort_strings(['cat', 'dog', 'bird'])) # ['bird', 'cat', 'dog']
print(sort_strings(['apple', 'banana', 'cherry'])) # ['apple', 'banana', 'cherry']
Write a function that takes a dictionary and returns a list of tuples, where each tuple contains a key and its corresponding value.
def dict_to_tuples(d):
tuples = []
for key, value in d.items():
tuples.append((key, value))
return tuples
print(dict_to_tuples({'a': 1, 'b': 2, 'c': 3})) # [('a', 1), ('b', 2), ('c', 3)]
print(dict_to_tuples({'x': 10, 'y': 20, 'z': 30})) # [('x', 10), ('y', 20), ('z', 30)]
Write a class that represents a circle with a radius attribute, and a method that returns the circumference of the circle.
class Circle:
def init(self, radius):
self.radius = radius
def circumference(self):
return 2 * 3.14 * self.radius
c = Circle(5)
print(c.circumference()) # 31.4
Write a try-except block that reads a file and prints its contents, but catches an exception if the file does not exist and prints an error message instead.
try:
with open('file.txt', 'r') as f:
print(f.read())
except FileNotFoundError:
print('Error: File not found')
Congratulations on completing the Introduction to Python course!