= "banana", "apple", "orange" fruits
02-02-24 (Friday)
Lord, you rule the world with a good and rightful code.
We ask for your wisdom to see things correctly and represent them faithfully.
Fill us with your justice so that we may know how to reflect it in the way we code,
Give us humility so that we may know the limits of our knowledge and its application in society,
Give us the courage to stand firmly when we feel pressured to go beyond these limits,
And give us responsibility as rulers we may be in the areas of society where you put us.
Lord, we want to reflect your good rule, so that people of all nations may fear your name.
Make known to us the path of life;
For in your presence there is fullness of joy;
and at your right hand are pleasures forevermore (Psalm 16).
Amen.
1 Tuples
- As every sequence type, it is ordered and subscriptable with integer indexes.
- Immutable, represented just as values separated with commas, or separated with commas and enclosed with parentheses:
or
= ("banana", "apple", "orange") fruits
1.1 Why immutables?
Why use tuples since they can’t change?
- They uses less memory and demands less processing.
- They don’t need to be copied when attributing to a new variable (see later).
1.1.1 What if I really want to change?
1] = "pineapple" # try changing "apple" to "pineapple" fruits[
TypeError: 'tuple' object does not support item assignment
To “change” a tuple, for example, you would basically need to either:
- Create a new tuple (with the changes you want), or
- Convert the tuple to a list, change the value you want (lists are mutable), and convert it back to a tuple.
- Which would mean creating a new list object (from the tuple), and then creating a new tuple object (from the list)… which can be actually very inefficient and costly…
2 Strings
- Are immutable, but unlike tuples, they only permit characters as its contents. Specified with
""
.
= "Hello World"
a print(a[1])
print(a[4:9])
e
o Wor
Important: since they can’t change, all the methods applied to immutable objects are making copies of them! For example:
= "Hello"
a = a.upper()
b print(b)
print(id(a))
print(id(b))
HELLO
140710663361264
140711816027504
3 Dictionaries
- Mapping types: have keys which map to values
- A pair of key and value is called an entry
- Dictionaries are NOT ORDERED! But they are subscriptable (with the keys - which have to be immutable objects)
= {'Sarah':'476-3321', 'Nathan':'351-7743'}
phones = {'Name':'Molly', 'Age':18}
users = {} # an empty dictionary emp
You can add a new entry by just specifying it:
'Bob'] = '123-4567'
phones[print(phones)
{'Sarah': '476-3321', 'Nathan': '351-7743', 'Bob': '123-4567'}
Or also change values using the keys:
'Sarah'] = '999-9999'
phones[print(phones)
{'Sarah': '999-9999', 'Nathan': '351-7743', 'Bob': '123-4567'}
To delete, use:
del phones['Nathan']
print(phones)
{'Sarah': '999-9999', 'Bob': '123-4567'}
4 Tips for homework - cryptography
- The function
ord()
will convert a character to its corresponding number in the ASCII table - Then you can add the increment to this number, and convert it back to a character using
chr()
. For example:
= 2
increment = 'a'
letter = chr( ord(letter) + increment ) # adding the increment and converting back to character
c_letter print(c_letter) # should be 'c'
c
- Now, the problem is how to make
'z'
cycle back to'a'
and vice-versa, right? - For that, let’s transpose
'a'
to 0, and'z'
to 26. We just remove 97 from theord()
value:
= ord(letter) - 97 # transposed
tr_letter = tr_letter + increment c_tr_letter
- Now, notice that if the final letter we get goes over 26, we have gone over
'z'
. To make it go back to'a'
, then, we just use the modulus operation, to get it back to zero when it gets to 26. Again:
= ord(letter) - 97 # transposed
tr_letter = (tr_letter + increment) % 26 c_tr_letter
- Now we just add the 97 again to match the ASCII table. In sum:
= 2
increment = 'z'
letter = ord(letter) - 97 # transposed
tr_letter = (tr_letter + increment) % 26 # adding the increment and limiting to 26
c_tr_letter = chr( c_tr_letter + 97 )
c_letter print(c_letter) # should be 'b'
b
- If you want everything in just one line:
= chr(((ord(letter) - 97 + increment) % 26 ) + 97) c_letter
5 Reviewing some things we saw in lab
5.1 Subscripts of subscripts (Lab 3.1)
Notice that we have a list of tuples. To access individual items, we use subscripts of subscripts:
[][][]...
How, for example, do we access the 2nd tuple (
('hey', 'ho')
) on the 3rd list inside the list?How do we access the word
ho
?
= [(1,2,'hey'), None, [123, ('hey', 'ho'), 123, {}], [[]], 1] x
- How do we access the value the character
'h'
in the word'hi'
in the dictionary inside the tuple?
= 123, None, 321, {12: True, 'potato': 0, 123:123, 'tomato': 'hi'} y
5.2 Lists of objects (Las 3.2)
- We can fill a list with turtles! We started a list with 4 empty spaces, and put the 4 different turtles there.
= 4*[None] # our list of (ninja) turtles
tmnt for i in range(4): # the following code will run 4 times, at each time, variable "i" will have values 0, 1, 2 and 3
= turtle.Turtle() # create a turtle and assign it to the position "i" in the list tmnt[i]
- You can make each turtle do its stuff, then, by calling them
tmnt[<number_of_turtle>].<method...>()
5.3 Dictionary filling (Lab 3.3)
- Let’s make a dictionary with the turtles by name? Suggestion:
"Leonardo", "Michelangelo", "Raphael", "Donatello"
= "Leonardo", "Michelangelo", "Raphael", "Donatello"
tmnt_names = {} # empty dictionary
tmnt for i in range(4):
= turtle.Turtle() tmnt[tmnt_names[i]]
- Attention to the assignment
tmnt[tmnt_names[i]]
. See what is happening? We are setting the key to be the string in the list accessed with the index “i”. - You can now call each turtle by name just by typing, for example,
tmnt['Leonardo'].<method...>()
. Cowabunga!
6 A world of 0’s and 1’s
- Think about how much we can represent as digital information: numbers, text, images…
- See, for example, how integers and floats are represented as binary numbers
- The ASCII table, for example, is used to represent text. And the UNICODE system is used for more character variety.
- The bitmap format, for example, is also a way to represent an image with many triplets corresponding to red, green and blue color intensities.
“For computers to reason about data at all, they currently must reduce all information to bits. Bits are simply 1’s and 0’s, nothing more: the symbol 1 has no inherent meaning, nor does the symbol 0. The origin of the word”bit” was from the 1948 paper by Claude Shannon, who was trying to find a way to represent the theoretically smallest possible unit of information to solve problems of audio compression in telephones. […] All that was left, from Shannon’s perspective, was “pure” information, with no inherent meaning: two symbols with which to represent phone call audio, and anything else in the universe: 1 and 0, strung together in arbitrarily long sequences to represent anything.” Amy J. Ko, “Encoding Information”
- How can we explain the success of this digital encoding? Basically, it is the success of digital electronics. Digital information is movable, stable and manipulable.
“How can we act remotely on little-known events, places and people? Answer: bringing home these events, places and people. How can you do this if you are far away? By inventing means that (a) make them movable so that they can be brought, (b) keep them stable so that they can be brought and carried without distortion, decomposition or deterioration, and (c) are combinable in such a way that, whatever the matter of which they are made, can be accumulated, aggregated or shuffled like a deck of cards. […] The history of science [and technology] is largely the history of the mobilization of anything that can be made to move and embark on a journey home, entering the universal census.” - Bruno Latour, Science in Action, p. 348 and 350
- For humans, it really doesn’t help to code everything as simple 2-symbol sequences (0’s and 1’s) - it becomes illegible. But for automated machines, it is extremely efficient - from the point of view of design, stability and speed.
- Thus, we need:
- Devices to convert reality to digital information - sensors
- Devices to let us manipulate this digital information - interfaces (screens, paper, etc)
- Devices to convert our digital information back to reality - actuators
- Thus, we need:
- Thus we are kind of trapped in the interface bottleneck: everything we usually do in the world needs to be done through screens…
7 Should we really encode everything as information?
How much are we losing by encoding things as information? Remeber: data is always a selective portrait of reality (full of biases).
Data privacy is a big issue in today’s society, called by sociologist Shoshanna Zuboff as The Age of Surveillance Capitalism. To have data about people and things is is to have power and value.
Or, as philosopher Byung-Chul Han puts it, everything today is coerced into visibility and transparency The Transparency Society. “I am seen, therefore I am”.
The secret things belong unto the LORD our God: but those things which are revealed belong unto us and to our children for ever, that we may do all the words of this law. Deuteronomy 29:29
7.1 Moderating our curiosity
To acknowledge and protect what should be hidden, we need to cultivate a virtuous curiosity.
- Thomas Aquinas speaks of the difference between studiositas (virtue) and curiositas (vice) (see more in this interesting article). There are at least 7 vices of curiosity:
ARROGANCE: seeking knowledge of things that no one is supposed to know;
NOSYNESS: seeking knowledge that may belong to some people, but not to us;
DISTRACTION: seeking knowledge of things that are not convenient to know at a certain time;
IMMODERATION: wanting to know something with an unhealthy desire (all forms of curiosity are failures of temperance, but this label helps to isolate this specific aspect);
IMPERTINENCE: seeking to know things in a more certain way than one can know, doing violence to the object of knowledge;
SUPERFICIALITY: disrespecting the object of knowledge, being content with a superficial understanding and quickly moving on to something else;
POSSESSIVENESS: delighting not in the object of knowledge, but in the act of knowing it. It resembles, on an intellectual level, the vice of greed.
7.2 Presence versus re-presence
Furthermore, when we deal with digital information, we are only dealing with past - a frozen portrait of something that happened. To live in interfaces is to live in the past.
The contrast to this would be to live in the present. To live in the present, we acknowledge presence, and not re-presence (representations).
Thus, as Christians seeking the common good, we would really have to think about an equilibrium between past and present, data and current life, virtual and material. Maybe we are living in a world where this can be quite unbalanced…
“Physical reality seems to recede in proportion as man’s symbolic activity advances. Instead of dealing with things themselves, man is, in a sense, constantly talking to himself. He has become so involved in linguistic forms, in artistic images, in mythical symbols or in religious rites that he cannot see or know anything except through the interposition of an artificial medium.” Ernest Cassirer, “An Essay on Man”