Tuples

preview_player
Показать описание
MIT 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016
Instructor: Dr. Ana Bell

This in-class question demonstrates tuples in Python, including the difference between tuples and strings.

License: Creative Commons BY-NC-SA
Рекомендации по теме
Комментарии
Автор

Regarding the fact that trailing commas are required in tuples containing a single object, this is because in addition to defining tuple literals, parentheses can also be used in Python to group expressions.

Combined with the fact that an expression can be used to assign a value within a tuple, the trailing comma is necessary in order to avoid semantic ambiguity.

An example:

x = (5 + 10) will assign an int with value of 15 to var x
whereas
x = (5 + 10, ) will assign a tuple containing a single int with value of 15 to var x

nashsok
Автор

This video have me a serious eureka moment about tuples. Did have to watch it twice, but I got there!

BaloWest
Автор

It feels like a trailing comma in a tuple literal declaration is a good candidate for a syntax error. I imagine there are uses for and history to the current handling, but I wonder what price is paid in bugs. C++ is full of this stuff due to its age and history, I’m hoping the relatively youthful Python 3.x has less :)

bakedbeings
Автор

- Why is there no syntax highlighting?
- Why is the code not using a monospaced font?
- Why is there a linebreak WITHIN IN THE STRING?
- Why are the tuples created inline with the function call?
- What does the function name have to do with the weather? Why does this show superficial semantics, which then add to the example at all?

Programming is hard enough as is. It looks to me like you're more interested in intimidating people for missing trailing commas in questionable syntactic sidecases, rather than passing excitement.

lecirconflex
Автор

How do you know that first is a string instead of a tuple? Because it doesn't have parentheses?

taoyan
Автор

In your case, t1 = ('cloud'), t2 = ('cold'), they are both tuples, so the answer shouldn't be ('sunny', 'cloudcold')?

chongke
Автор

The hand writing is worse than a six-year-old.

H-vi