Path: blob/master/lessons/lesson_02/code/python-foundations/practice/python-functions-lab-master/python-functions.ipynb
1906 views
Kernel: Python 2
Practice Python Functions
Author: Ryan Dunlap | DSI-SF
Resources - Potentially Helpful Functions
Now-a-days, most of the components of the String
module are already built into python however, it still has several constant values which can be useful, such as:
In [1]:
Out[1]:
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
1. Write a function that takes the length of a side of a square as an argument and returns the area of the square.
In [2]:
2. Write a function that takes the heigth and width of a triangle and returns the area.
In [3]:
3. Write a function that takes a string as an argument and returns a tuple consisting of two elements:
A list of all the characters in the string.
A count of the number of characters in the string.
In [4]:
4. Write a function that takes two integers, passed as strings, and returns the sum, difference, and product as a tuple (all values as integers).
In [5]:
5. Write a function that takes a list as the argument and returns a tuple consisting of two elements:
A list with the items in reverse order.
A list of the items in the original list that have an odd index.
In [6]:
Challenge Problem: Write a function that returns the score for a word. The score of the word is the sum of the scores of its letters. Each letter's score is equal to it's position in the alphabet.
So, for example:
A = 1, B = 2, C = 3, D = 4, E = 5
abe = 8 = (1 + 2 + 5)
Hint: The string library has a property ascii_lowercase
that can save some typing here.
In [7]: