Live Exchange Rate Quotes Using Python
Python Techniques — Using Python to Create Live Exchange Rates
Python is a versatile and widely-used programming language known for its simplicity, readability, and flexibility. With its clean and concise syntax, Python is a great choice for beginners and experienced developers alike.
Its extensive standard library and rich ecosystem of third-party packages enable it to excel in various domains, from web development and scientific computing to data analysis, artificial intelligence, and automation. Python’s cross-platform compatibility and active community make it an ideal tool for solving diverse challenges, allowing developers to write elegant and efficient code for a wide range of applications.
This article will be part of a limited series of using Python for fun stuff.
Creating an Infinite Exchange Rate Loop
A live exchange rates screen is a graphical user interface (GUI) that displays real-time or near-real-time exchange rates for various currency pairs. It provides users with up-to-date information about the current exchange rates, allowing them to monitor currency fluctuations and make informed decisions.
The aim is to create an archaic version with a simple lines of code. It can be a first prototype before a more sophisticated version.
First, pip install the required library:
pip install forex_python
Write the following code:
from forex_python.converter import CurrencyRates
import time
def display_exchange_rates(base_currency, target_currencies):
c = CurrencyRates()
print("Exchange Rates:")
for currency in target_currencies:
rate = c.get_rate(base_currency, currency)
print(f"{currency}: {rate}")
if __name__ == "__main__":
base_currency = "EUR"
target_currencies = ["SEK", "JPY", "GBP", "AUD"] # Add more currencies as needed
while True:
display_exchange_rates(base_currency, target_currencies)
time.sleep(1) # Display rates every second
The output looks something like this:
Exchange Rates:
SEK: 11.7195
JPY: 157.35
GBP: 0.8618
AUD: 1.6779
Exchange Rates:
SEK: 11.7195
JPY: 157.35
GBP: 0.8619
AUD: 1.6778
Exchange Rates:
SEK: 11.7193
JPY: 157.35
GBP: 0.8618
AUD: 1.6779