-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_functions.rmd
113 lines (86 loc) · 3.04 KB
/
python_functions.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
---
title: "Functions"
author: "JM_Wiggins"
date: '2022-06-07'
output: html_document
---
[python.org](https://www.python.org/)
## Functions
### Syntax
define using `def` keyword, semicolon after function_name() is required!
```{python eval=FALSE}
def function_name(argument1, argument2): #semicolon here
what_to_evaluate
return output ##when you want to send the output to another part of the code
def function_name2(argument1, argument2):
what_to_evaluate
print(output) #when you want to show the output to a human
```
if there is no `return` or `print` the function will return `none`
Note: `print` is a function you call; `return` is a python keyword
[list of python keywords](https://www.w3schools.com/python/python_ref_keywords.asp)
More [keyword info](https://realpython.com/python-keywords/)
get a list of python keywords in python session:
```{python eval=FALSE}
help("keywords")
```
Function Example:
```{python}
def hyp_sq(a,b):
sum = a**2 + b**2
return sum
print(hyp_sq(918,846)) #to print the results to the console
```
Function Example with [`f-strings`](https://docs.python.org/3/whatsnew/3.6.html#pep-498-formatted-string-literals)
```{python}
def allowed_driving(name, age):
"""Print'{name}is allowed to drive' or '{name} is not allowed to drive' based on check against the MIN_DRIVING_AGE constant"""
MIN_DRIVING_AGE = 18
if age >= MIN_DRIVING_AGE:
print(f'{name} is allowed to drive')
else:
print(f'{name} is not allowed to drive')
```
if `what_to_evaluate` is an `if`, `else`, `elif`, or a `for` loop a semicolon must also be included after the `what_to_evaluate`
```{python eval=FALSE}
def function_name(argument1, argument2): #semicolon here
if what_to_evaluate: #semicolon here too
return output
elif evaluate_something_else: #semicolon here too
return different_output
```
Example:
```{python}
def animalNames(letter): #semicolon
if letter == 'a': #semicolon
return 'Aardvark'
elif letter == 'b': #semicolon
return 'Baboon'
elif letter == 'c': #semicolon
return 'Caracal'
print(animalNames('a')) #note that a had to be in '' to work
```
Looping through a dictionary:
```{python}
GAME_STATS = dict(sara=0, bob=1, tim=5, julian=3, jim=1)
def print_game_stats(games_won):
"""Loop through games_won's dict (key, value) pairs (dict.items)
printing (print, not return) how many games each person has won,
pluralize 'game' based on number.
Expected output (ignore the docstring's indentation):
sara has won 0 games
bob has won 1 game
tim has won 5 games
julian has won 3 games
jim has won 1 game
(Note that as of Python 3.7 - which we're using atm - dict insert order
is retained so no sorting is required for this Bite.)
"""
for k, v in games_won.items():
if v >= 2 or v == 0:
print('{} has won {} games'.format(k, str(v)))
else:
print('{} has won {} game'.format(k, str(v)))
pass
print_game_stats(GAME_STATS)
```