Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_04/code/Python_functions.ipynb
1904 views
Kernel: Python 3

Practice Python Functions

_Author: Ryan Dunlap (San Francisco)


Resources: Potentially Helpful Functions

Nowadays, most of the string module's components are already built into Python. However, it still has several constant values that can be useful, including:

string.ascii_uppercase
import string string.ascii_uppercase
'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.

# A: def square(x): return x**2 square(4)
16

2) Write a function that takes the height and width of a triangle and returns the area.

# A: def area_triangle(b,h): return 1/2*b*h area_triangle(3,4)
6.0

3) Write a function that takes a string as an argument and returns a tuple consisting of two elements:

  • A list of all of the characters in the string.

  • A count of the number of characters in the string.

# A: def string_manipulation(string): length_string = len(string) string_chars = list(set(string)) return (string_chars, length_string) _, char_count = string_manipulation('this is a sentence')
char_count
18

4) Write a function that takes two integers, passed as strings, and returns the sum, difference, and product as a tuple (with all values as integers).

# A: def int_string(x, y): x = int(x) y = int(y) return (x+y, x-y, x*y) int_string('1','2')
(3, -1, 2)

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.

for number, value in enumerate(['a','b','c']): print(number, value)
0 a 1 b 2 c
# A: def list_manipulation(l): l_reversed = l[::-1] odd_index = [] for number, i in enumerate(l): if number %2 ==1: odd_index.append(i) ## alt: l[1::2] return l_reversed, odd_index # return l[::-1], l[1::2] one liner list_manipulation([1,2,3,4])
([4, 3, 2, 1], [2, 4])

Challenge: Write a function that returns the score for a word. A word's score is the sum of the scores of its letters. Each letter's score is equal to its 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.

string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
list(zip(string.ascii_lowercase, range(1,27)))
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7), ('h', 8), ('i', 9), ('j', 10), ('k', 11), ('l', 12), ('m', 13), ('n', 14), ('o', 15), ('p', 16), ('q', 17), ('r', 18), ('s', 19), ('t', 20), ('u', 21), ('v', 22), ('w', 23), ('x', 24), ('y', 25), ('z', 26)]
# A: def word_score(input_string): string_lower = input_string.lower() values_dict = dict(zip(string.ascii_lowercase, range(1,27))) score = 0 for letter in string_lower: score += values_dict[letter] return score word_score('data')
26
values_dict = dict(zip(string.ascii_lowercase, range(1,26))) values_dict
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25}
my_string = 'this is a string' list(set(my_string))
['g', 't', ' ', 'a', 's', 'r', 'n', 'h', 'i']