Python – a powerful, flexible, and widely-used programming language worldwide. With its simple and readable syntax, Python has become one of the favorite tools for programmers, from beginners to top experts. In this article, we will learn about What is Python and 10 advanced online python exercises, from its basic characteristics to advanced exercises to help you master your knowledge and skills.
What is Python?
Python is an interpreted programming language, with simple and understandable syntax. Created in the 1990s by Guido van Rossum, Python quickly became one of the most popular languages in the world. With a design philosophy focused on simplicity and readability, Python is often used in various fields such as web development, data science, artificial intelligence, and many other applications.
Not only popular for its ease of learning and use, Python is also powerful in handling a wide range of programming tasks. From web development and data analysis to artificial intelligence and machine learning, Python provides rich tools and libraries to solve any programming problem.
Additionally, the Python community is large, strong, and enthusiastic. From online forums to workshops and online courses, Python programmers have many opportunities to learn and share knowledge with each other, helping to enhance skills and develop in this field.
Some notable applications of Python
Python is widely used in many different fields due to its flexibility and versatility. Here are some main areas where Python is applied:
Web development
Python is the top choice for backend web development with famous frameworks like Django and Flask.
Django is a powerful full-stack web framework used to build complex web applications like Instagram, Disqus, Mozilla.
Flask is a lightweight and flexible web framework used to build simple and fast web applications.
Data science
Python provides many powerful libraries for data analysis, machine learning, and artificial intelligence such as NumPy, Pandas, scikit-learn, TensorFlow, etc.
NumPy is a library for scientific computing and efficient array processing.
Pandas is a library for data table manipulation and data analysis.
scikit-learn is a library for machine learning algorithms such as classification, regression, clustering, etc.
TensorFlow is a library for deep learning applications.
Application development
Python can be used to develop various types of applications, from desktop applications to mobile applications and games.
The Kivy library allows developing cross-platform mobile applications with Python.
The Pygame library allows developing 2D and 3D games with Python.
Automation
Python is often used to automate repetitive tasks, saving time and effort.
The Selenium library allows automating web browser tasks with Python.
The Robot Framework library allows automating tasks on computers with Python.
10 advanced Python online exercises with sample code
The following part contain 10 advanced programming exercises in the Python language, along with source code to help you build demo applications.
Phonebook management
The exercise allows creating a phonebook management application that allows users to add and display contacts. It uses a dictionary variable to store contact information.
You can add additional features such as deleting/editing contacts (user name + phone number), as well as adding decorations to the program.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Tạo biến danh bạ
danh_ba = {}
def them_lien_he(): ten = input(“Nhập tên liên hệ: “) so_dien_thoai = input(“Nhập số điện thoại: “) danh_ba[ten] = so_dien_thoai
def xem_danh_ba(): for ten, so_dien_thoai in danh_ba.items(): print(f“{ten}: {so_dien_thoai}”)
while True: print(“\n1. Thêm liên hệ”) print(“2. Xem danh bạ”) print(“3. Thoát”) lua_chon = input(“Chọn chức năng: “) if lua_chon == “1”: them_lien_he() elif lua_chon == “2”: xem_danh_ba() elif lua_chon == “3”: break |
Calculating Fibonacci sequence using recursion
Create a Python program to calculate the value of the nth Fibonacci number using recursion.
The Fibonacci sequence is a series of natural numbers starting with two numbers 0 and 1, then each subsequent number in the sequence is generated by adding the two preceding numbers. This sequence is named after the Italian mathematician Leonardo Fibonacci, who introduced it in the late 12th century through his book “Liber Abaci” (Book of Calculation) in 1202.
The Fibonacci sequence starts as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
The general formula for calculating the nth Fibonacci number (n is a natural number) is:
F(n) = F(n-1) + F(n-2)
With F(0) = 0 and F(1) = 1 as the two starting points of the sequence.
01
02 03 04 05 06 07 08 09 10 |
def fibonacci(n):
if n <= 0: return 0 elif n == 1: return 1 else: return fibonacci(n–1) + fibonacci(n–2)
n = int(input(“Nhập số n: “)) print(“Giá trị Fibonacci thứ”, n, “là”, fibonacci(n)) |
Building a simple calculator
Create a simple calculator application that allows users to input addition, subtraction, multiplication, and division operations. Additionally, you can add some other operations if desired.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
def cong(a, b):
return a + b
def tru(a, b): return a – b
def nhan(a, b): return a * b
def chia(a, b): if b != 0: return a / b else: return “Không thể chia cho 0”
while True: print(“\n1. Cộng”) print(“2. Trừ”) print(“3. Nhân”) print(“4. Chia”) print(“5. Thoát”) lua_chon = input(“Chọn phép tính (1/2/3/4/5): “) if lua_chon == “5”: break so1 = float(input(“Nhập số thứ nhất: “)) so2 = float(input(“Nhập số thứ hai: “)) if lua_chon == “1”: print(“Kết quả:”, cong(so1, so2)) elif lua_chon == “2”: print(“Kết quả:”, tru(so1, so2)) elif lua_chon == “3”: print(“Kết quả:”, nhan(so1, so2)) elif lua_chon == “4”: print(“Kết quả:”, chia(so1, so2)) |
Time management
Create a time management application that allows users to add and view events. Additionally, you can add functionality to delete and edit events if desired.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 |
su_kien = {}
def them_su_kien(): ngay = input(“Nhập ngày (dd/mm/yyyy): “) noi_dung = input(“Nhập nội dung sự kiện: “) su_kien[ngay] = noi_dung
def xem_su_kien(): for ngay, noi_dung in su_kien.items(): print(f“{ngay}: {noi_dung}”)
while True: print(“\n1. Thêm sự kiện”) print(“2. Xem sự kiện”) print(“3. Thoát”) lua_chon = input(“Chọn chức năng: “) if lua_chon == “1”: them_su_kien() elif lua_chon == “2”: xem_su_kien() elif lua_chon == “3”: break |
Sorting a list
Create a Python program to sort a list of integers in ascending order.
01
02 03 04 05 06 07 08 09 10 11 12 13 |
# Lấy danh sách từ người dùng
danh_sach = [] n = int(input(“Nhập số phần tử trong danh sách: “))
for i in range(n): phan_tu = int(input(“Nhập phần tử thứ {}: “.format(i+1))) danh_sach.append(phan_tu)
# Sắp xếp danh sách danh_sach.sort()
# In danh sách sau khi sắp xếp print(“Danh sách sau khi sắp xếp:”, danh_sach) |
Building a geometric shape class
Create a geometric shape class with attributes such as length, width, and methods to calculate area and perimeter.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 |
class HinhHoc:
def __init__(self, chieu_dai, chieu_rong): self.chieu_dai = chieu_dai self.chieu_rong = chieu_rong
def tinh_dien_tich(self): return self.chieu_dai * self.chieu_rong
def tinh_chu_vi(self): return 2 * (self.chieu_dai + self.chieu_rong)
# Sử dụng lớp HinhHoc hinh = HinhHoc(5, 3) print(“Diện tích:”, hinh.tinh_dien_tich()) print(“Chu vi:”, hinh.tinh_chu_vi()) |
Finding prime numbers using the Sieve of Eratosthenes
Create a Python program to find all prime numbers less than or equal to a given number n using the Sieve of Eratosthenes algorithm.
The Sieve of Eratosthenes algorithm is a method for finding all prime numbers within a given range of positive integers. This algorithm is named after the ancient Greek mathematician Eratosthenes of Cyrene, who developed it in the 3rd century BC.
The operation of the Sieve of Eratosthenes algorithm is as follows:
- Start with a list of integers from 2 to n, where n is the upper limit of the range to be checked.
- Start with the first number in the list (2) and mark all its multiples (i.e., all numbers greater than 2 that are divisible by 2) as not prime.
- Continue with the next number in the list that has not been marked and repeat step 2 until you have checked all numbers in the list or until you reach the square root of n (because if an integer is not a prime number, it must have a prime factor less than or equal to the square root of n).
- All remaining numbers in the list after completing the above steps will be prime numbers.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 |
def sang_eratosthenes(n):
is_prime = [True] * (n + 1) p = 2 while p**2 <= n: if is_prime[p]: for i in range(p**2, n + 1, p): is_prime[i] = False p += 1
primes = [i for i in range(2, n + 1) if is_prime[i]] return primes
n = int(input(“Nhập số n: “)) print(“Các số nguyên tố nhỏ hơn hoặc bằng”, n, “là:”, sang_eratosthenes(n)) |
Task management program
Create a task management application that allows users to add, modify, delete, and display a list of tasks.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# Mã nguồn ứng dụng quản lý công việc
cong_viec = []
def them_cong_viec(): cv = input(“Nhập công việc: “) cong_viec.append(cv)
def xem_cong_viec(): for i, cv in enumerate(cong_viec, 1): print(f“{i}. {cv}”)
while True: print(“\n1. Thêm công việc”) print(“2. Xem danh sách công việc”) print(“3. Thoát”) lua_chon = input(“Chọn chức năng: “) if lua_chon == “1”: them_cong_viec() elif lua_chon == “2”: xem_cong_viec() elif lua_chon == “3”: break |
Multi-function calculator
Unlike creating a simple calculator, a multi-function calculator has additional functions such as exponentiation and square root calculation.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import math
def tinh_luy_thua(a, b): return a**b
def tinh_can_bac_hai(a): return math.sqrt(a)
while True: print(“\n1. Cộng”) print(“2. Trừ”) print(“3. Nhân”) print(“4. Chia”) print(“5. Lũy thừa”) print(“6. Căn bậc hai”) print(“7. Thoát”) lua_chon = input(“Chọn phép tính (1/2/3/4/5/6/7): “) if lua_chon == “7”: break so1 = float(input(“Nhập số thứ nhất: “)) if lua_chon != “6”: so2 = float(input(“Nhập số thứ hai: “)) if lua_chon == “1”: print(“Kết quả:”, so1 + so2) elif lua_chon == “2”: print(“Kết quả:”, so1 – so2) elif lua_chon == “3”: print(“Kết quả:”, so1 * so2) elif lua_chon == “4”: if so2 != 0: print(“Kết quả:”, so1 / so2) else: print(“Không thể chia cho 0”) elif lua_chon == “5”: print(“Kết quả:”, tinh_luy_thua(so1, so2)) elif lua_chon == “6”: print(“Kết quả:”, tinh_can_bac_hai(so1)) |
Creating a number guessing game
Create a number guessing game that allows users to guess a randomly generated number between 1 and 100 and try to guess the correct number.
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 |
import random
so_may_tinh = random.randint(1, 100) so_luot = 0
while True: so_luot += 1 so_nguoi_choi = int(input(“Đoán số (1-100): “)) if so_nguoi_choi < so_may_tinh: print(“Số bạn đoán nhỏ hơn số máy tính.”) elif so_nguoi_choi > so_may_tinh: print(“Số bạn đoán lớn hơn số máy tính.”) else: print(“Chính xác! Bạn đã đoán đúng số”, so_may_tinh, “sau”, so_luot, “lượt.”) break |
Remember to check and handle errors when using these code snippets to ensure the stability of the program. Good luck with these exercises!
Buy Cheap Proxies at proxyv6.net
If you are looking for a way to protect your IP address and browse the web safely, buying cheap proxies at proxyv6.net may be an ideal solution. With this proxy service, you can anonymize your IP address and enjoy many online security benefits.
The combination of proxyv6.net and blocked website helps you check and improve the anonymity of your network connection easily and effectively. Additionally, using this affordable proxy service provides the ability to access local content and overcome geographical limitations conveniently.
Don’t hesitate; explore the cheap proxy packages at proxyv6.net today to experience safety and privacy when using the internet