Exchange rates in Python
Tags: python
Published: 2019-09-03 17:54:00

The <a href="https://forex-python.readthedocs.io/en/latest/usage.html#currency-rates">forex_python module</a> can grab currency exchange rates for you. The worlds currencies (euro, dollar, pounds, inr) have different values.

In todays world there are many many fiat currencies. 
To list all currency rates you use:

    #!/usr/bin/python3
    from forex_python.converter import CurrencyRates
    c = CurrencyRates()
    c.get_rates('USD')  

In Python you can make a converter. The program below converts dollars to euros:

    #!/usr/bin/python3
    from forex_python.converter import CurrencyRates                    
                                                                    
    c = CurrencyRates()                                                 
    rate = c.get_rate('USD', 'EUR')                                     
    print(rate)                                                         
                                                                     
    price = input("Enter amount ($): ")                                 
                                                                    
    value = float(("{0:.2f}").format(float(price)*rate))                
    print("\u20ac " + str(value))              

<img src="https://proxy.duckduckgo.com/iu/?u=http%3A%2F%2Fbestanimations.com%2FMoney%2FCash%2Fcash-money-animated-gif-14.gif&f=1" alt="currencies">

## Lambda function

The later part you can do with a <a href="https://pythonprogramminglanguage.com/lambda/">lambda function</a>. This is easier to read in my opinion:

    #!/usr/bin/python3
    from forex_python.converter import CurrencyRates                    
                                                                    
    c = CurrencyRates()                                                 
    rate = c.get_rate('USD', 'EUR')                                     
    print(rate)                                                         
                                                                    
    price = input("Enter amount ($): ")                                 
                                                                    
    v = lambda x : round(float(x)*rate,2)                                
    y = v(price)                                                        
    print("\u20ac " + str(y))      

<img src="https://proxy.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia1.tenor.com%2Fimages%2F68450d30a7419dbfd525dffe016406b0%2Ftenor.gif%3Fitemid%3D8262581&f=1">

### The Millionaire test

So are you a millionaire in any currency? Lets find out. The method get_rates() returns a <a href="https://pythonprogramminglanguage.com/dictionary/">dictionary</a>. 

Then we iterate over that dictionary with items(), which goes over every key value pair. Then apply the lambda function and if there's more than 1.000.000 in the currency you are a millionaire.

    #!/usr/bin/python3
    from forex_python.converter import CurrencyRates                    
                                                                        
    c = CurrencyRates()                                                 
    price = input("Enter amount ($): ")                                 
                                                                        
    coins = c.get_rates('USD')                                          
    for key, value in coins.items():                                    
        v = lambda x : round(float(x)*value,2)                          
        y = v(price)                                                    
                                                                        
        if y > 1000000:                                                 
            print(str(key) + " " + str(y))                              
                                         
Example run:

    Enter amount ($): 1000
    IDR 14225107.43
    KRW 1214958.4


Related links:

* <a href="https://forex-python.readthedocs.io/en/latest/usage.html#currency-rates">Forex module</a>
* <a href="https://pythonprogramminglanguage.com/">Learn Python</a>