{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Statements Assessment Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_____\n", "**Use for, split(), and if to create a Statement that will print out words that start with 's':**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "st = 'Print only the words that start with s in this sentence'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "start\n", "s\n", "sentence\n" ] } ], "source": [ "for word in st.split():\n", " if word[0] == 's':\n", " print word" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "______\n", "**Use range() to print all the even numbers from 0 to 10.**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8, 10]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(0,11,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "**Use List comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x for x in range(1,50) if x%3 == 0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_____\n", "**Go through the string below and if the length of a word is even print \"even!\"**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "st = 'Print every word in this sentence that has an even number of letters'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "word <-- has an even length!\n", "in <-- has an even length!\n", "this <-- has an even length!\n", "sentence <-- has an even length!\n", "that <-- has an even length!\n", "an <-- has an even length!\n", "even <-- has an even length!\n", "number <-- has an even length!\n", "of <-- has an even length!\n" ] } ], "source": [ "for word in st.split():\n", " if len(word)%2 == 0:\n", " print word+\" <-- has an even length!\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Write a program that prints the integers from 1 to 100. But for multiples of three print \"Fizz\" instead of the number, and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\".**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for num in xrange(1,101):\n", " if num % 5 == 0 and num % 3 == 0:\n", " print \"FizzBuzz\"\n", " elif num % 3 == 0:\n", " print \"Fizz\"\n", " elif num % 5 == 0:\n", " print \"Buzz\"\n", " else:\n", " print num" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Use List Comprehension to create a list of the first letters of every word in the string below:**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "st = 'Create a list of the first letters of every word in this string'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[word[0] for word in st.split()]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Great Job!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }