Skip to content

Commit 788c8ac

Browse files
authored
01-intro.md: Add text about built-in functions (#931)
* Add text about built-in functions
1 parent 0c3f9d5 commit 788c8ac

File tree

1 file changed

+66
-14
lines changed

1 file changed

+66
-14
lines changed

_episodes/01-intro.md

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ exercises: 10
55
questions:
66
- "What basic data types can I work with in Python?"
77
- "How can I create a new variable in Python?"
8+
- "How do I use a function?"
89
- "Can I change the value associated with a variable after I create it?"
910
objectives:
1011
- "Assign values to variables."
@@ -13,6 +14,7 @@ keypoints:
1314
- "Use `variable = value` to assign a value to a variable in order to record it in memory."
1415
- "Variables are created on demand whenever a value is assigned to them."
1516
- "Use `print(something)` to display the value of `something`."
17+
- "Built-in functions are always available to use."
1618
---
1719

1820
## Variables
@@ -31,7 +33,8 @@ This is great but not very interesting.
3133
To do anything useful with data, we need to assign its value to a _variable_.
3234
In Python, we can [assign]({{ page.root }}/reference.html#assign) a value to a
3335
[variable]({{ page.root }}/reference.html#variable), using the equals sign `=`.
34-
For example, to assign value `60` to a variable `weight_kg`, we would execute:
36+
For example, we can track the weight of a patient who weighs 60 kilograms by
37+
assigning the value `60` to a variable `weight_kg`:
3538

3639
~~~
3740
weight_kg = 60
@@ -59,41 +62,90 @@ Python knows various types of data. Three common ones are:
5962
* strings.
6063

6164
In the example above, variable `weight_kg` has an integer value of `60`.
62-
To create a variable with a floating point value, we can execute:
65+
If we want to more precisely track the weight of our patient,
66+
we can use a floating point value by executing:
6367

6468
~~~
65-
weight_kg = 60.0
69+
weight_kg = 60.3
6670
~~~
6771
{: .language-python}
6872

69-
And to create a string, we add single or double quotes around some text, for example:
73+
To create a string, we add single or double quotes around some text.
74+
To identify and track a patient throughout our study,
75+
we can assign each person a unique identifier by storing it in a string:
7076

7177
~~~
72-
weight_kg_text = 'weight in kilograms:'
78+
patient_id = '001'
7379
~~~
7480
{: .language-python}
7581

7682
## Using Variables in Python
77-
To display the value of a variable to the screen in Python, we can use the `print` function:
83+
84+
Once we have data stored with variable names, we can make use of it in calculations.
85+
We may want to store our patient's weight in pounds as well as kilograms:
7886

7987
~~~
80-
print(weight_kg)
88+
weight_lb = 2.2 * weight_kg
89+
~~~
90+
{: .language-python}
91+
92+
We might decide to add a prefix to our patient identifier:
93+
94+
~~~
95+
patient_id = 'inflam_' + patient_id
8196
~~~
8297
{: .language-python}
8398

99+
## Built-in Python functions
100+
101+
To carry out common tasks with data and variables in Python,
102+
the language provides us with several built-in [functions]({{ page.root }}/reference.html#function).
103+
To display information to the screen, we use the `print` function:
104+
84105
~~~
85-
60.0
106+
print(weight_lb)
107+
print(patient_id)
108+
~~~
109+
{: .language-python}
110+
111+
~~~
112+
132.66
113+
inflam_001
86114
~~~
87115
{: .output}
88116

89-
We can display multiple things at once using only one `print` command:
117+
When we want to make use of a function, referred to as calling the function,
118+
we follow its name by parentheses. The parentheses are important:
119+
if you leave them off, the function doesn't actually run!
120+
Sometimes you will include values or variables inside the parentheses for the function to use.
121+
In the case of `print`,
122+
we use the parentheses to tell the function what value we want to display.
123+
We will learn more about how functions work and how to create our own in later episodes.
124+
125+
We can display multiple things at once using only one `print` call:
90126

91127
~~~
92-
print(weight_kg_text, weight_kg)
128+
print(patient_id, 'weight in kilograms:', weight_kg)
93129
~~~
94130
{: .language-python}
95131
~~~
96-
weight in kilograms: 60.0
132+
inflam_001 weight in kilograms: 60.3
133+
~~~
134+
{: .output}
135+
136+
We can also call a function inside of another
137+
[function call]({{ page.root }}/reference.html#function-call).
138+
For example, Python has a built-in function called `type` that tells you a value's data type:
139+
140+
~~~
141+
print(type(60.3))
142+
print(type(patient_id))
143+
~~~
144+
{: .language-python}
145+
146+
~~~
147+
<class 'float'>
148+
<class 'str'>
97149
~~~
98150
{: .output}
99151

@@ -105,7 +157,7 @@ print('weight in pounds:', 2.2 * weight_kg)
105157
{: .language-python}
106158

107159
~~~
108-
weight in pounds: 132.0
160+
weight in pounds: 132.66
109161
~~~
110162
{: .output}
111163

@@ -116,7 +168,7 @@ print(weight_kg)
116168
{: .language-python}
117169

118170
~~~
119-
60.0
171+
60.3
120172
~~~
121173
{: .output}
122174

@@ -148,7 +200,7 @@ weight in kilograms is now: 65.0
148200
> ~~~
149201
> # There are 2.2 pounds per kilogram
150202
> weight_lb = 2.2 * weight_kg
151-
> print(weight_kg_text, weight_kg, 'and in pounds:', weight_lb)
203+
> print('weight in kilograms:', weight_kg, 'and in pounds:', weight_lb)
152204
> ~~~
153205
> {: .language-python}
154206
>

0 commit comments

Comments
 (0)