Skip to content

Instantly share code, notes, and snippets.

@sualeh
Last active June 25, 2024 03:03
Show Gist options
  • Save sualeh/c972ebfd1892c84ddb97e8503844bc24 to your computer and use it in GitHub Desktop.
Save sualeh/c972ebfd1892c84ddb97e8503844bc24 to your computer and use it in GitHub Desktop.
python_class.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"private_outputs": true,
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/sualeh/c972ebfd1892c84ddb97e8503844bc24/python_class.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Introductory Python Class\n"
],
"metadata": {
"id": "XYC0TkDsxrlY"
}
},
{
"cell_type": "markdown",
"source": [
"## Project 1 - Rolling Dice"
],
"metadata": {
"id": "4OwB9zS8xr0t"
}
},
{
"cell_type": "code",
"source": [
"# Import the random library to generate random numbers\n",
"import random\n",
"\n",
"# Generate a random number between 1 and 6 (inclusive)\n",
"die_face_value = random.randint(1, 6)\n",
"\n",
"# Print the generated number\n",
"print(f\"Dice roll: {die_face_value}\")"
],
"metadata": {
"id": "76OfP8ThxrCq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "ZTA4vnf6WZQZ"
}
},
{
"cell_type": "markdown",
"source": [
"Follow on:\n",
"1. Print a picture of the die in ASCII art.\n",
"2. Ask for number of dice to roll, and shows pictures of all the dice.\n",
"3. Review full project with explanation at [Build a Dice-Rolling Application With Python](https://realpython.com/python-dice-roll/)."
],
"metadata": {
"id": "lpHIcDmv0T83"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"## Project 2 - Get the Current Weather"
],
"metadata": {
"id": "Oa-O_SDxxU-H"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "GDhbF92VxIDM"
},
"outputs": [],
"source": [
"# Import the requests library to handle API requests\n",
"import requests\n",
"\n",
"# Define the URL for the API request, specifying the latitude and longitude\n",
"# for the location (Berlin in this case)\n",
"url = \"https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&current=temperature_2m\"\n",
"\n",
"# Send a GET request to the specified URL\n",
"response = requests.get(url)\n",
"\n",
"# Parse the JSON response data\n",
"data = response.json()\n",
"\n",
"# Print the current temperature and its unit\n",
"# Access the \"temperature_2m\" field from the \"current\" section and\n",
"# the unit of the \"temperature_2m\" field from the \"current_units\"\n",
"# section of the JSON response\n",
"temperature = data[\"current\"][\"temperature_2m\"]\n",
"unit = data[\"current_units\"][\"temperature_2m\"]\n",
"print(f'{temperature}{unit}')"
]
},
{
"cell_type": "markdown",
"source": [
"Follow on:\n",
"1. Show temperature for Chicago.\n",
"2. Use a Python library to find the current location, and show temperature for that location."
],
"metadata": {
"id": "IaULJlu603cf"
}
},
{
"cell_type": "markdown",
"source": [
"### Project 3 - Get Stock Prices"
],
"metadata": {
"id": "jUId_D6v1MqQ"
}
},
{
"cell_type": "code",
"source": [
"# Import the requests library to handle HTTP requests\n",
"import requests\n",
"\n",
"# Define the URL for the API request, specifying the function, ticker symbol,\n",
"# and API key for Alpha Vantage\n",
"url = \"https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=IBM&apikey=demo\"\n",
"\n",
"# Send a GET request to the specified URL\n",
"response = requests.get(url)\n",
"\n",
"# Parse the JSON response data\n",
"data = response.json()\n",
"\n",
"# Print the stock symbol and its current price\n",
"# Access the \"01. symbol\" field from the \"Global and\n",
"# the \"05. price\" field from the \"Global Quote\" section of the JSON response\n",
"symbol = data[\"Global Quote\"][\"01. symbol\"]\n",
"price = data[\"Global Quote\"][\"05. price\"]\n",
"print(f'{symbol} is {price}')"
],
"metadata": {
"id": "XqDMoyBl1LL2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Follow on:\n",
"1. Use the [Alpha Vantage API](https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=IBM&apikey=demo) to display the price of IBM\n",
"2. Make a list of your favorite stocks. Put these symbols in an array. Loop over the array and print the stock symbol and price.\n",
"3. Create an array of tuples like this (\"IBM\", \"International Business Machines Corporation\"), and print the company name, stock symbol and price.\n",
"4. Research on how to get today's price as well as yesterday's price on [Alpha Vantage API Documentation](https://www.alphavantage.co/documentation/). Print out how much the change in price was (up or down). Do NOT put a specific date in your code. In other words, your code should work every day without asking for a date. You should get today's date using Python.\n",
"5. Put that information on stock holdings in the tuple as well, like this: (\"IBM\", \"International Business Machines Corporation\", 500). Use this to calculate total stock holdings, and also how much it is up or down from yesterday.\n",
"6. Export the daily report into an Excel file, using code like that below."
],
"metadata": {
"id": "XKj-1ksi3NDu"
}
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"from google.colab import files\n",
"\n",
"# Create data set\n",
"data = [('Tom', 10), ('Nick', 15), ('Julie', 14)]\n",
"df = pd.DataFrame(data, columns=['Name', 'Age'])\n",
"\n",
"# DEBUG - print dataframe\n",
"print(df)\n",
"\n",
"# Create Excel file from data set\n",
"with pd.ExcelWriter('ages.xlsx') as writer:\n",
" df.to_excel(writer, sheet_name=\"Kid's Ages\")\n",
"\n",
"# Download the file\n",
"files.download('ages.xlsx')"
],
"metadata": {
"id": "iEBYxoj1WJCp"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment