-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlength_one_tuples.Rmd
91 lines (69 loc) · 1.89 KB
/
length_one_tuples.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
---
jupyter:
orphan: true
jupytext:
notebook_metadata_filter: all,-language_info
split_at_heading: true
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.11.5
kernelspec:
display_name: Python 3
language: python
name: python3
---
# Length 1 tuples
Remember {ref}`tuples`. For example, here are a couple of length two tuples:
```{python}
first_tuple = (1, 2)
first_tuple
```
```{python}
second_tuple = (3, 4)
```
As for lists, you can add tuples, to concatenate the contents:
```{python}
tuples_together = first_tuple + second_tuple
tuples_together
```
## Length 1 tuples
Let us say you want to write code for a tuple with only one element.
You might think this would work:
```{python}
# Python interprets this parentheses in arithmetic.
just_a_number = (1)
just_a_number
```
Just a single number or string, with parentheses around it, does not make a
tuple, because Python interprets this as your usual brackets for arithmetic.
That means that:
```{python}
(1)
```
is exactly the same as:
```{python}
1
```
Why? Because, Python has to decide what expressions like this mean:
```python
# Wait - what do the parentheses mean?
(1 + 2) + (3 + 4)
```
Is this adding two one-element tuples, to give a two-element tuple `(3, 7)`? Or
is it our usual mathematical expression giving 3 + 7 = 10. The designer of the Python language decided it should be an arithmetical expression.
```{python}
# They mean parentheses as in arithmetic.
(1 + 2) + (3 + 4)
```
To form a length-1 tuple, you need to disambiguate what you mean by the parentheses, with a trailing comma:
```{python}
short_tuple = (1,) # Notice the trailing comma
short_tuple
```
So, for example, to add two length one tuples together, as above:
```{python}
# Notice the extra comma to say - we mean these to be length-1 tuples.
(1 + 2,) + (3 + 4,)
```