{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**This notebook is an exercise in the [Intro to Programming](https://www.kaggle.com/learn/intro-to-programming) course.  You can reference the tutorial at [this link](https://www.kaggle.com/alexisbcook/arithmetic-and-variables).**\n\n---\n","metadata":{}},{"cell_type":"markdown","source":"This exercise will get you started with running your own code.  \n\n# Set up the notebook\n\nTo begin, run the code in the next cell.\n- Begin by clicking inside the code cell.  \n- Click on the triangle (in the shape of a \"Play button\") that appears to the left of the code cell.\n- If your code was run sucessfully, you will see `Setup Complete` as output below the cell.\n\nInstead of clicking on the triangle, you can also run code by pressing Shift + Enter on your keyboard.  Try this now!  Nothing bad will happen if you run the code more than once.","metadata":{}},{"cell_type":"code","source":"# Set up the exercise\nfrom learntools.core import binder\nbinder.bind(globals())\nfrom learntools.intro_to_programming.ex1 import *\nprint('Setup complete.')","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:02.982990Z","iopub.execute_input":"2022-08-05T07:50:02.983406Z","iopub.status.idle":"2022-08-05T07:50:03.024146Z","shell.execute_reply.started":"2022-08-05T07:50:02.983375Z","shell.execute_reply":"2022-08-05T07:50:03.023076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The code above sets up the notebook so that it can check your answers in this exercise.  You should never modify this code.  (Otherwise, the notebook won't be able to verify that you have successfully completed the exercise.)\n\nAfter finishing all of the questions below, you'll see the exercise marked as complete on the [course page](http://www.kaggle.com/learn/intro-to-programming).  Once you complete all of the lessons, you'll get a course completion certificate!\n\n# Question 1\n\nNext, you will run some code from the tutorial, so you can see how it works for yourself.  Run the next code cell without changes.","metadata":{}},{"cell_type":"code","source":"print(\"Hello, world!\")\n\n# DO NOT REMOVE: Mark this question as completed\nq1.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.026027Z","iopub.execute_input":"2022-08-05T07:50:03.026372Z","iopub.status.idle":"2022-08-05T07:50:03.036101Z","shell.execute_reply.started":"2022-08-05T07:50:03.026340Z","shell.execute_reply":"2022-08-05T07:50:03.035287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You just ran code to print `Hello world!`, which you should see in the output above.\n\nThe second line of code (`q1.check()`) checks your answer.  You should never modify this checking code; if you remove it, you won't get credit for completing the problem.\n\n# Question 2\n\nNow, you will print another message of your choosing.  To do this, change `print(\"Your message here!\")` to use a different message.  For instance, you might like to change it to something like:\n- `print(\"Good morning!\")`\n- `print(\"I am learning how to code :D\")`\n\nOr, you might like to see what happens if you write something like `print(\"3+4\")`.  Does it return 7, or does it just think of `\"3+4\"` as just another message?\n\nMake sure that your message is enclosed in quotation marks (`\"`), and the message itself does not use quotation marks. For instance, this will throw an error: `print(\"She said \"great job\" and gave me a high-five!\")` because the message contains quotation marks.  If you decide to take the Python course after completing this course, you will learn more about how to avoid this error in [Lesson 6](https://www.kaggle.com/colinmorris/strings-and-dictionaries).\n\nFeel free to try out multiple messages!","metadata":{}},{"cell_type":"code","source":"# TODO: Change the message\nprint(\"Your message here!\")\nprint(\"Good morning!\")\nprint(\"I am learning how to code :D\")\n# DO NOT REMOVE: Mark this question as completed \nq2.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.037720Z","iopub.execute_input":"2022-08-05T07:50:03.038051Z","iopub.status.idle":"2022-08-05T07:50:03.047652Z","shell.execute_reply.started":"2022-08-05T07:50:03.038021Z","shell.execute_reply":"2022-08-05T07:50:03.046707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Question 3\n\nAs you learned in the tutorial, a comment in Python has a pound sign (`#`) in front of it, which tells Python to ignore the text after it.\n\nPutting a pound sign in front of a line of code will make Python ignore that code.  For instance, this line would be ignored by Python, and nothing would appear in the output:\n```python\n#print(1+2)\n```\nRemoving the pound sign will make it so that you can run the code again. When we remove the pound sign in front of a line of code, we call this **uncommenting**.\n\nIn this problem, you will uncomment two lines in the code cell below and view the output:\n- Remove the `#` in front of `q3.hint()`.  To avoid errors, do NOT remove the `#` in front of `# Uncomment to view hint`.  \n- Next, remove the `#` in front of `q3.solution()`.\n\nAs in the previous questions, do not change the final line of code that marks your work as completed.","metadata":{}},{"cell_type":"code","source":"# Uncomment to get a hint\nq3.hint()\n#print(1+2)\n# Uncomment to view solution\nq3.solution()\n\n# DO NOT REMOVE: Check your answer\nq3.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.081189Z","iopub.execute_input":"2022-08-05T07:50:03.082117Z","iopub.status.idle":"2022-08-05T07:50:03.096460Z","shell.execute_reply.started":"2022-08-05T07:50:03.082077Z","shell.execute_reply":"2022-08-05T07:50:03.095641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the next question, and in most of the exercises in this course, you will have the option to uncomment to view hints and solutions.  Once you feel comfortable with uncommenting, continue to the next question.\n","metadata":{}},{"cell_type":"markdown","source":"# Question 4\n\nIn the tutorial, you defined several variables to calculate the total number of seconds in a year.  Run the next code cell to do the calculation here.","metadata":{}},{"cell_type":"code","source":"# Create variables\nnum_years = 4\ndays_per_year = 365 \nhours_per_day = 24\nmins_per_hour = 60\nsecs_per_min = 60\n\n# Calculate number of seconds in four years\ntotal_secs = secs_per_min * mins_per_hour * hours_per_day * days_per_year * num_years\nprint(total_secs)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.098049Z","iopub.execute_input":"2022-08-05T07:50:03.098559Z","iopub.status.idle":"2022-08-05T07:50:03.104271Z","shell.execute_reply.started":"2022-08-05T07:50:03.098528Z","shell.execute_reply":"2022-08-05T07:50:03.103454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create Variables\nbirths_per_min = 250\nhours_per_day = 24\nmins_per_hour = 60\nbirths_per_day = births_per_min * mins_per_hour * hours_per_day\nprint(births_per_day)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.105661Z","iopub.execute_input":"2022-08-05T07:50:03.106355Z","iopub.status.idle":"2022-08-05T07:50:03.113373Z","shell.execute_reply.started":"2022-08-05T07:50:03.106323Z","shell.execute_reply":"2022-08-05T07:50:03.112532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Use the next code cell to:\n- Define a variable `births_per_min` and set it to 250.  (There are on average 250 babies born each minute.)\n- Define a variable `births_per_day` that contains the average number of babies born each day.  (To set the value of this variable, you should use `births_per_min` and some of the variables from the previous code cell.)\n\nRemember you can always get a hint if you need it!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🌶️ Question 5\n\n(Questions marked with a 🌶️ will be a little bit more challenging than the others!  Remember you can always get a hint or view the solution.)\n\nThe [Titanic competition](https://www.kaggle.com/c/titanic) is Kaggle's most famous data science competition. In this competition, participants are challenged to build a machine learning model that can predict whether or not passengers survived the Titanic shipwreck, based on information like age, sex, family size, and ticket number.\n\nRun the next code cell without changes to load and preview the titanic data.\n\nDon't worry about the details of the code for now - the end result is just that the all of the titanic data has been loaded in a variable named `titanic_data`.  (In order to learn how to write this code yourself, you can take the [Python course](https://www.kaggle.com/learn/python) and then the [Pandas course](https://www.kaggle.com/learn/pandas).)","metadata":{}},{"cell_type":"code","source":"# Uncomment to get a hint\nq4.hint()\n\n# Uncomment to view solution\nq4.solution()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.115102Z","iopub.execute_input":"2022-08-05T07:50:03.115657Z","iopub.status.idle":"2022-08-05T07:50:03.129737Z","shell.execute_reply.started":"2022-08-05T07:50:03.115627Z","shell.execute_reply":"2022-08-05T07:50:03.128570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data from the titanic competition\nimport pandas as pd\ntitanic_data = pd.read_csv(\"../input/titanic/train.csv\")\n\n# Show the first five rows of the data\ntitanic_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.157139Z","iopub.execute_input":"2022-08-05T07:50:03.158453Z","iopub.status.idle":"2022-08-05T07:50:03.199897Z","shell.execute_reply.started":"2022-08-05T07:50:03.158401Z","shell.execute_reply":"2022-08-05T07:50:03.198717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The data has a different row for each passenger.  \n\nThe next code cell defines and prints the values of three variables:  \n- `total` = total number of passengers who boarded the ship\n- `survived` = number of passengers who survived the shipwreck\n- `minors` = number of passengers under 18 years of age\n\nRun the code cell without changes.  (Don't worry about the details of how these variables are calculated for now.  You can learn more about how to calculate these values in the [Pandas course](https://www.kaggle.com/learn/pandas).)","metadata":{}},{"cell_type":"code","source":"# Number of total passengers\ntotal = len(titanic_data)\nprint(total)\n\n# Number of passengers who survived\nsurvived = (titanic_data.Survived == 1).sum()\nprint(survived)\n\n# Number of passengers under 18\nminors = (titanic_data.Age < 18).sum()\nprint(minors)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:03.202162Z","iopub.execute_input":"2022-08-05T07:50:03.202881Z","iopub.status.idle":"2022-08-05T07:50:03.215568Z","shell.execute_reply.started":"2022-08-05T07:50:03.202837Z","shell.execute_reply":"2022-08-05T07:50:03.214399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So, \n- `total = 891` (there were 891 passengers on board the Titanic),\n- `survived = 342` (342 passengers survived), and \n- `minors = 113` (113 passengers were under the age of 18).\n\nIn the code cell below, replace the underlines (`____`) with code to calculate the values for two more variables:\n- `survived_fraction` should be set to the fraction of passengers who survived the Titanic disaster.\n- `minors_fraction` should be the fraction of passengers who were minors (under the age of 18).\n\nFor each variable, your answer should be a number between 0 and 1.  \n\nIf you need a hint or want to view the solution, you can skip to the next code cell and uncomment the appropriate lines of code (`q5.hint()` and `q5.solution()`).","metadata":{}},{"cell_type":"code","source":"# TODO: Fill in the value of the survived_fraction variable\nsurvived_fraction = survived/total\n\n# Print the value of the variable\nprint(survived_fraction)\n\n# TODO: Fill in the value of the minors_fraction variable\nminors_fraction = minors/total\n\n# Print the value of the variable\nprint(minors_fraction)\n\n# DO NOT REMOVE: Check your answer\nq5.check()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:52:12.783776Z","iopub.execute_input":"2022-08-05T07:52:12.784278Z","iopub.status.idle":"2022-08-05T07:52:12.796698Z","shell.execute_reply.started":"2022-08-05T07:52:12.784213Z","shell.execute_reply":"2022-08-05T07:52:12.795248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uncomment to receive a hint\nq5.hint()\n\n# Uncomment to view the solution\nq5.solution()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T07:50:43.979392Z","iopub.execute_input":"2022-08-05T07:50:43.980126Z","iopub.status.idle":"2022-08-05T07:50:43.993123Z","shell.execute_reply.started":"2022-08-05T07:50:43.980090Z","shell.execute_reply":"2022-08-05T07:50:43.992053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bonus Exercise\n\nAs an **optional** next step, you're encouraged to make a submission to the Titanic competition.  \n> To do this, follow the instructions in the **[notebook here](https://www.kaggle.com/alexisbcook/titanic-tutorial)**.  \n\nThe notebook is beginner-friendly and does not assume you have any experience with coding.  At the same time, you'll learn how to author your own notebooks on Kaggle, and it gives you a great idea of what you'll be able to do if you continue to learn data science!\n\n# Keep going\n\nCongratulations!  You have just finished the first exercise in the Intro to Programming course!  \n\nYou are now ready to learn how to **[organize your code with functions](https://www.kaggle.com/alexisbcook/functions)**.","metadata":{}},{"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [course discussion forum](https://www.kaggle.com/learn/intro-to-programming/discussion) to chat with other learners.*","metadata":{}}]}