Skip to content

Instantly share code, notes, and snippets.

@EricPostMaster
Last active January 8, 2020 01:17
Show Gist options
  • Save EricPostMaster/0663c333df1723a411d007489339e50e to your computer and use it in GitHub Desktop.
Save EricPostMaster/0663c333df1723a411d007489339e50e to your computer and use it in GitHub Desktop.
Python_Practice_1.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Python_Practice_1.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/EricPostMaster/0663c333df1723a411d007489339e50e/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "WeEnqRRGpjwr",
"colab_type": "code",
"outputId": "155147a6-e0fd-47ec-94b8-f01421302e96",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"total = 0\n",
"\n",
"for i in range(0,100):\n",
" if i % 3 == 0 or i % 5 == 0:\n",
" total += i\n",
"print(total)\n",
"\n",
"\n",
"# Sum all multiples of 3 and 5 that are less than 100"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"2318\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Hu4GxAJLqRXw",
"colab_type": "code",
"outputId": "abd10601-8ca4-40f5-f584-6fa7e0201a0d",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
}
},
"source": [
"b = [20,10,5]\n",
"\n",
"for element in b:\n",
" print(element)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"20\n",
"10\n",
"5\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "SydtjSduq12t",
"colab_type": "code",
"outputId": "c0fd4921-5185-4281-9f9c-8d5f10ff0f11",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"print(b)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"[20, 10, 5]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "YDj1nCMbq5S1",
"colab_type": "code",
"outputId": "baa82f0b-2a8e-42cb-a8ae-4d723305136c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"# Sum all of the elements in list b\n",
"\n",
"total_b=0 # Variable that will hold the sum and starts at zero\n",
"\n",
"for element in b: # Loop through each of the elements in list b NOTE: for loops initialize an index for you. while loops do not.\n",
" total_b += element # Add the amount of the current element to the total_b variable\n",
"\n",
"print(total_b) # Print the total once the loop has completed."
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"35\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "YhmHdbkurH77",
"colab_type": "code",
"outputId": "01194cc4-92ef-47f4-ae94-04a77ed775b0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"given_list = [5, 4, 4, 3, 1, -2, -3, -5]\n",
"\n",
"total_positive = 0\n",
"i=0 # NOTE: while loops do not initialize an index for you\n",
"\n",
"while (given_list[i] > 0):\n",
" total_positive = given_list[i] + total_positive\n",
" i = i+ 1\n",
"print(total_positive)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"17\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ngEyZuAju6cZ",
"colab_type": "code",
"outputId": "b9678a75-41b0-44c8-e5a6-f7b0d0ee1264",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
" given_list2 = [5, 4, 4, 3, 1, -2, -3, -5, -7]\n",
"\n",
" total_negative = 0\n",
" i=0\n",
"\n",
" while i < len(given_list2):\n",
" if given_list2[i] < 0:\n",
" total_negative += given_list2[i]\n",
" i += 1\n",
"\n",
"print(total_negative)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"-17\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "tRsr905t2HxV",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment