{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Fundamentals: Introduction to Jupyter and Python \n", "# SOLUTIONS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 🥊 Challenge 1: Printing \n", "\n", "Write your own `print` statement in the code cell below. Follow the syntax of the example above, and change the text in the quotation marks." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hi, I'm Tom\n" ] } ], "source": [ "# Write your own awesome print statement here!\n", "print('Hi, I\\'m Tom')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 🥊 Challenge 2: Executing Cells Multiple Times\n", "\n", "Try using **Shift + Enter** to run the following cell three times. What is the output? Run the cell one more time. Is the output the same?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ANSWER: `a` keeps increasing. This is because each time we run the cell, the value in memory is updated and 1 is added." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 🥊 Challenge 3: Debugging Variable Names\n", "\n", "The following two blocks of code include variable names that cause an error. For each block of code, consider the following questions:\n", "1. Which rule is being broken? Can you find this information in the error message?\n", "2. What guidelines aren't being followed? \n", "3. How would you change the code?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.5310000000000024\n" ] } ], "source": [ "# use consistent style (e.g. snake case)\n", "avg_age = 30.332\n", "another_variable = 28.801\n", "print(avg_age - another_variable)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Zimbabwe is a country in Africa\n" ] } ], "source": [ "# Variable names cannot include symbols like .\n", "country_1 = 'Zimbabwe'\n", "\n", "# Variable names are case-sensitive \n", "continent = 'Africa'\n", "\n", "print(country_1, 'is a country in', continent)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## 🥊 Challenge 4: What The...\n", "\n", "What does the following error seem to tell you? Google the error and see if you can fix it!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "something went wrong\n" ] } ], "source": [ "# SyntaxError: unmatched ')'\n", "# We need to remove the second closing parenthesis\n", "print('something went wrong')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 🥊 Challenge 5: Swapping Values\n", "\n", "Let's say we have two variables and we want to swap the values for each of them. \n", "\n", "Does the following method accomplish the goal?\n", "\n", "💡 **Tip**: What is the value of first and last at the end of the cell?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "start = 1997\n", "end = 1952\n", "\n", "start = end\n", "end = start" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using a third temporary variable (you could call it `temp`), swap the first and last variables, so that `start = 1952` and `end = 1997`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1952 1997\n" ] } ], "source": [ "start = 1997\n", "end = 1952\n", "\n", "# YOUR CODE HERE\n", "temp = start\n", "start = end\n", "end = temp\n", "\n", "print(start,end)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" } }, "nbformat": 4, "nbformat_minor": 4 }