{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Advanced Python Objects Test" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Advanced Numbers\n", "\n", "**Problem 1: Convert 1024 to binary and hexadecimal representation:**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0b10000000000\n", "0x400\n" ] } ], "source": [ "print bin(1024)\n", "print hex(1024)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 2: Round 5.23222 to two decimal places**" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5.23" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(5.23222,2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Advanced Strings\n", "**Problem 3: Check if every letter in the string s is lower case**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'hello how are you Mary, are you feeling okay?'\n", "\n", "s.islower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 4: How many times does the letter 'w' show up in the string below?**" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'twywywtwywbwhsjhwuwshshwuwwwjdjdid'\n", "s.count('w')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Advanced Sets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 5: Find the elements in set1 that are not in set2:**" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{2}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set1 = {2,3,1,5,6,8}\n", "set2 = {3,1,7,5,6,8}\n", "\n", "set1.difference(set2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 6: Find all elements that are in either set:**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 5, 6, 7, 8}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set1.union(set2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Advanced Dictionaries\n", "\n", "**Problem 7: Create this dictionary:\n", "{0: 0, 1: 1, 2: 8, 3: 27, 4: 64}\n", " using dictionary comprehension.**" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{0: 0, 1: 1, 2: 8, 3: 27, 4: 64}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{x:x**3 for x in range(5)}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Advanced Lists\n", "\n", "**Problem 8: Reverse the list below:**" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[4, 3, 2, 1]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1,2,3,4]\n", "\n", "l.reverse()\n", "\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 9: Sort the list below**" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [3,4,2,5,1]\n", "\n", "l.sort()\n", "\n", "l" ] }, { "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }