{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Notebook 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For loops\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a `for loop` that prints each item in the list `['python','perl','java','C++']`\n", "\n", "Now modify your code so that it prints only the second character in each item (so: `y, e, a, +`)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## While loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a `while loop` that prints each item in the list `['python','perl','java','C++']`\n", "\n", "Modify your script so that it prints each item in the list + which position it has in the list: \n", "`python has position 0 in the list` \n", "`perl has position 1 in the list` \n", "`java has position 2 in the list` \n", "`C++ has position 3 in the list` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Answers\n", "\n", "### For loops" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "python\n", "perl\n", "java\n", "C++\n" ] } ], "source": [ "languages = ['python','perl','java','C++'] # Save all languages to a list\n", "for language in languages: # Use a foor loop to go to each item in turn in the list and do something...\n", " print(language) # In this case printing the output" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y\n", "e\n", "a\n", "+\n" ] } ], "source": [ "languages = ['python','perl','java','C++']\n", "for language in languages: \n", " print(language[1]) # language is an iterable item in fruits, so you can use index to access characters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### While loops" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "python\n", "perl\n", "java\n", "C++\n" ] } ], "source": [ "languages = ['python','perl','java','C++'] # Save all languages to a list\n", "i = 0 # Set a counter on where to start in the list (0)\n", "while i < len(languages): # Run loop as long as this statement is true. \n", " print(languages[i]) # Use i for index of languages\n", " i = i + 1 # Don't forget to increase i with 1, otherwise you have an infinite loop" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "python has position 0 in the list\n", "perl has position 1 in the list\n", "java has position 2 in the list\n", "C++ has position 3 in the list\n" ] } ], "source": [ "languages = ['python','perl','java','C++']\n", "i = 0 \n", "while i < len(languages): \n", " print(languages[i]+' has position '+str(i)+' in the list') # Use the counter i to print the position\n", " i = i + 1 " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }