In the last tutorial, we installed Python and Visual Studio Code, so it's time to put them to use. We will write a simple script that will fetch the current price of Bitcoin using the Coingecko API.
pip install requests
import requests
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
response = requests.get(url, params=parameters)
data = response.json()
price = data["market_data"]["current_price"]["usd"]
print("Current price:", price)
For the lazy ones, here's the entire code:
import requests
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
response = requests.get(url, params=parameters)
data = response.json()
price = data["market_data"]["current_price"]["usd"]
print("Current price:", price)
The code can be enhanced with an option to choose a currency and playing around with Coingecko API parameters. It all depends on your willingness and creativity.
The main takeaways from this tutorial are understanding what a variable is and what objects are. In future tutorials, there will be code that you can use to do something, so your task will be to delve into the key concepts of the language because the basis of programming is searching for information and reading documentation. Without that, you won't be able to write a program that someone else hasn't written because everything will be a problem for you.
Programming requires independent thinking, and what I'm doing is just an attempt to inspire others and show that sometimes you can do more with a few simple lines of code than you think.
W ostatnim tutorialu instalowaliśmy Pythona i Visual Studio Code pora więc zrobić z nich użytek. Napiszemy prosty skrypt, który będzie pobierał aktualną cenę bitcoin korzystając z api coingecko.
pip install requests
import requests
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
response = requests.get(url, params=parameters)
data = response.json()
price = data["market_data"]["current_price"]["usd"]
print("Current price:", price)
Dla leniuszków cały kod:
import requests
url = "https://api.coingecko.com/api/v3/coins/bitcoin"
parameters = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
"sparkline": "false"
}
response = requests.get(url, params=parameters)
data = response.json()
price = data["market_data"]["current_price"]["usd"]
print("Current price:", price)
Kod można ubarwić w opcję wybierania waluty, pobawić się parametrami api coingeko. Wszystko zależy od chęci i tego co sam wymyślisz.
Wnioski z tego tutoriala jakie powinny zostać wyciągnięte to przede wszystkim to jak wygląda i czym jest zmienna, oraz zanotować, że istnieje coś takiego jak obiekty.
W tutorialach będzie pojawiał się kod, który można do czego wykorzystać tak by nie powielać nudnych poradników a Twoim zadaniem będzie zgłębienie zaganień kluczowych dla danego języka, gdyż podstawą programowania jest szukanie informacji oraz czytanie dokumentacji. Bez tego nie będziesz w stanie napisać programu, którego nie napisał ktoś inny bo wszystko będzie dla Ciebie problemem.
Programowanie wymaga samodzielnego myślenia a to co ja robię to jedynie próba zajawienia innych i pokazania, że kilkoma prostymi liniami kodu można czasami zrobić więcej niż się komuś wydaje