Python Functions and Methods

master
mehul 2018-06-15 16:33:31 +10:00 committed by GitHub
parent 72ff0a096a
commit 1796a65c16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1412 additions and 0 deletions

View File

@ -0,0 +1,964 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Function Practice Exercises\n",
"\n",
"Problems are arranged in increasing difficulty:\n",
"* Warmup - these can be solved using basic comparisons and methods\n",
"* Level 1 - these may involve if/then conditional statements and simple methods\n",
"* Level 2 - these may require iterating over sequences, usually with some kind of loop\n",
"* Challenging - these will take some creativity to solve"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## WARMUP SECTION:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### LESSER OF TWO EVENS: Write a function that returns the lesser of two given numbers *if* both numbers are even, but returns the greater if one or both numbers are odd\n",
" lesser_of_two_evens(2,4) --> 2\n",
" lesser_of_two_evens(2,5) --> 5"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def lesser_of_two_evens(a,b):\n",
" if a%2==0 and b%2==0:\n",
" return min((a,b))\n",
" else:\n",
" return max((a,b))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"lesser_of_two_evens(2,4)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"lesser_of_two_evens(2,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter\n",
" animal_crackers('Levelheaded Llama') --> True\n",
" animal_crackers('Crazy Kangaroo') --> False"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"def animal_crackers(text):\n",
" mylist = text.split()\n",
" if mylist[0][0].lower() == mylist[1][0].lower():\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"animal_crackers('Levelheaded Llama')"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"animal_crackers('Crazy Kangaroo')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### MAKES TWENTY: Given two integers, return True if the sum of the integers is 20 *or* if one of the integers is 20. If not, return False\n",
"\n",
" makes_twenty(20,10) --> True\n",
" makes_twenty(12,8) --> True\n",
" makes_twenty(2,3) --> False"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"def makes_twenty(n1,n2):\n",
" if n1 == 20 or n2 == 20 or sum((n1,n2))==20:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"makes_twenty(20,10)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"makes_twenty(2,3)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"makes_twenty(12,8)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LEVEL 1 PROBLEMS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name\n",
" \n",
" old_macdonald('macdonald') --> MacDonald\n",
" \n",
"Note: `'macdonald'.capitalize()` returns `'Macdonald'`"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"def old_macdonald(name):\n",
" mylist = []\n",
" for index,value in enumerate(name):\n",
" if index in (0,3):\n",
" mylist.append(value.upper())\n",
" else:\n",
" mylist.append(value)\n",
" \n",
" return ''.join(mylist)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'MacDonald'"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"old_macdonald('macdonald')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### MASTER YODA: Given a sentence, return a sentence with the words reversed\n",
"\n",
" master_yoda('I am home') --> 'home am I'\n",
" master_yoda('We are ready') --> 'ready are We'\n",
" \n",
"Note: The .join() method may be useful here. The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:\n",
"\n",
" >>> \"--\".join(['a','b','c'])\n",
" >>> 'a--b--c'\n",
"\n",
"This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:\n",
"\n",
" >>> \" \".join(['Hello','world'])\n",
" >>> \"Hello world\""
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"def master_yoda(text):\n",
" mylist = text.split()\n",
" yodalist = []\n",
" for word in mylist[::-1]:\n",
" yodalist += word+\" \"\n",
" \n",
" return ''.join(yodalist) "
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'home am I '"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"master_yoda('I am home')"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ready are We '"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"master_yoda('We are ready')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200\n",
"\n",
" almost_there(90) --> True\n",
" almost_there(104) --> True\n",
" almost_there(150) --> False\n",
" almost_there(209) --> True\n",
" \n",
"NOTE: `abs(num)` returns the absolute value of a number"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [],
"source": [
"def almost_there(n):\n",
" if abs(100-n) <= 10 or abs(200-n) <= 10:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"almost_there(104)"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"almost_there(150)"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"almost_there(209)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LEVEL 2 PROBLEMS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### FIND 33: \n",
"\n",
"Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.\n",
"\n",
" has_33([1, 3, 3]) → True\n",
" has_33([1, 3, 1, 3]) → False\n",
" has_33([3, 1, 3]) → False"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [],
"source": [
"def has_33(nums):\n",
" #print(list(item for item in zip(nums,nums[1:])))\n",
" for x,y in zip(nums,nums[1:]):\n",
" if x == y == 3:\n",
" return True\n",
" return False\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"has_33([1, 3, 3])"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"has_33([1, 3, 1, 3])"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"has_33([3, 1, 3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### PAPER DOLL: Given a string, return a string where for every character in the original there are three characters\n",
" paper_doll('Hello') --> 'HHHeeellllllooo'\n",
" paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"def paper_doll(text):\n",
" mylist = []\n",
" for letter in text:\n",
" mylist.append(letter*3)\n",
" return ''.join(mylist)"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'HHHeeellllllooo'"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"paper_doll('Hello')"
]
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'MMMiiissssssiiissssssiiippppppiii'"
]
},
"execution_count": 159,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"paper_doll('Mississippi')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### BLACKJACK: Given three integers between 1 and 11, if their sum is less than or equal to 21, return their sum. If their sum exceeds 21 *and* there's an eleven, reduce the total sum by 10. Finally, if the sum (even after adjustment) exceeds 21, return 'BUST'\n",
" blackjack(5,6,7) --> 18\n",
" blackjack(9,9,9) --> 'BUST'\n",
" blackjack(9,9,11) --> 19"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [],
"source": [
"def blackjack(a,b,c):\n",
" if sum((a,b,c)) <= 21:\n",
" return sum((a,b,c))\n",
" elif (a or b or c) == 11:\n",
" return sum((a,b,c)) - 10\n",
" else:\n",
" return 'BUST'"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(9 or 11) <= 60"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"blackjack(5,6,7)"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "argument of type 'int' is not iterable",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-180-ef261d118a84>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Check\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mblackjack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-177-73895faa56ca>\u001b[0m in \u001b[0;36mblackjack\u001b[1;34m(a, b, c)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m21\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32melif\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mb\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;36m11\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: argument of type 'int' is not iterable"
]
}
],
"source": [
"# Check\n",
"blackjack(9,9,9)"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'BUST'"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"blackjack(9,9,11)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### SUMMER OF '69: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.\n",
" \n",
" summer_69([1, 3, 5]) --> 9\n",
" summer_69([4, 5, 6, 7, 8, 9]) --> 9\n",
" summer_69([2, 1, 6, 9, 11]) --> 14"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def summer_69(arr):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Check\n",
"summer_69([1, 3, 5])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Check\n",
"summer_69([4, 5, 6, 7, 8, 9])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Check\n",
"summer_69([2, 1, 6, 9, 11])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CHALLENGING PROBLEMS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### SPY GAME: Write a function that takes in a list of integers and returns True if it contains 007 in order\n",
"\n",
" spy_game([1,2,4,0,0,7,5]) --> True\n",
" spy_game([1,0,2,4,0,5,7]) --> True\n",
" spy_game([1,7,2,0,4,5,0]) --> False\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def spy_game(nums):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Check\n",
"spy_game([1,2,4,0,0,7,5])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Check\n",
"spy_game([1,0,2,4,0,5,7])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Check\n",
"spy_game([1,7,2,0,4,5,0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### COUNT PRIMES: Write a function that returns the *number* of prime numbers that exist up to and including a given number\n",
" count_primes(100) --> 25\n",
"\n",
"By convention, 0 and 1 are not prime."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def count_primes(num):\n",
" pass\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Check\n",
"count_primes(100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Just for fun:\n",
"#### PRINT BIG: Write a function that takes in a single letter, and returns a 5x5 representation of that letter\n",
" print_big('a')\n",
" \n",
" out: * \n",
" * *\n",
" *****\n",
" * *\n",
" * *\n",
"HINT: Consider making a dictionary of possible patterns, and mapping the alphabet to specific 5-line combinations of patterns. <br>For purposes of this exercise, it's ok if your dictionary stops at \"E\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def print_big(letter):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"print_big('a')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Great Job!"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,448 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Methods and Functions"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"mylist = [num for num in range(0,11)]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'e'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mystring = 'hello'\n",
"mystring[1]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"#pig latin\n",
"\n",
"def pig_latin(myword):\n",
" vowel = 'aeiou'\n",
" if myword[0] in vowel:\n",
" myword += 'ay'\n",
" else:\n",
" myword = myword[1:] + myword[0] + 'ay'\n",
" \n",
" return myword\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'appleay'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pig_latin('apple')"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"# Function to return a string like aBcDeF\n",
"\n",
"def morph(mystring):\n",
" process_string = []\n",
" for index,value in enumerate(mystring):\n",
" if (index+1)%2==0:\n",
" process_string.append(value.upper())\n",
" else:\n",
" process_string.append(value.lower())\n",
" \n",
" return ''.join(process_string)"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hIwHaTsThEdEaL'"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"morph('hiWhatstheDeal')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### *args and **kwargs"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"d = {'k1':1,'k2':20}"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"21"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(d.values())"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"55"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(list(num for num in range(0,11)))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum((3,4,4))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"myset = {2,3,4,4}"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{2, 3, 4}"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myset"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(myset)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"# using args\n",
"\n",
"def percent(*args):\n",
" \"\"\"\n",
" function to return 5% of the sum of the parameters passed\n",
" \"\"\"\n",
" return sum(args)*0.05\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function percent in module __main__:\n",
"\n",
"percent(*args)\n",
" function to return 5% of the sum of the parameters passed\n",
"\n"
]
}
],
"source": [
"help(percent)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"percent(34,56,10)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"percent(10)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"def myfunc(*args):\n",
" mylist = []\n",
" for num in args:\n",
" if num%2==0:\n",
" mylist.append(num)\n",
" return mylist"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4, 6, 2, 6, 8]"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myfunc(2,4,6,2,6,8,3,5)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"# using kwargs\n",
"\n",
"def my_func(**kwargs):\n",
" print (kwargs)\n",
" \n",
" if 'France' in kwargs:\n",
" print ('Les Blues is {}'.format(kwargs['France']))"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'France': 1, 'Brazil': 2, 'Germany': 3}\n",
"Les Blues is 1\n"
]
}
],
"source": [
"my_func(France = 1, Brazil = 2, Germany = 3)"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"# using both together\n",
"\n",
"def futbol(*args,**kwargs):\n",
" print(args)\n",
" print(kwargs)\n",
" print(f\"The roster of {kwargs['Brazil']} is {args[0]} \")"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 2, 3)\n",
"{'France': 'blue', 'Brazil': 'yellow'}\n",
"The roster of yellow is 1 \n"
]
}
],
"source": [
"futbol(1,2,3,France='blue',Brazil='yellow')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}