Тёмный

Tuples 

MIT OpenCourseWare
Подписаться 5 млн
Просмотров 58 тыс.
50% 1

MIT 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016
View the complete course: ocw.mit.edu/6-0001F16
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
More information at ocw.mit.edu/terms
More courses at ocw.mit.edu

Опубликовано:

 

14 фев 2017

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 27   
@nashsok
@nashsok 2 года назад
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
@BaloWest
@BaloWest 4 года назад
This video have me a serious eureka moment about tuples. Did have to watch it twice, but I got there!
@akbarrauf2741
@akbarrauf2741 7 лет назад
thank you ,mit
@AnmolKumar-dh3lh
@AnmolKumar-dh3lh Год назад
Had fun!! Thanks teacher!!
@wah704
@wah704 Год назад
That handwriting 💀💀
@bakedbeings
@bakedbeings 4 года назад
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 :)
@taoyan1523
@taoyan1523 3 года назад
How do you know that first is a string instead of a tuple? Because it doesn't have parentheses?
@carmenc7251
@carmenc7251 3 года назад
she mentioned abt it that if it has no comma in the bracket then it's a string
@Mullemeck83
@Mullemeck83 3 года назад
Yes, I guess for first to be a tuple it would need to be something like first = (t1[0], t2[0])
@bobanmilisavljevic7857
@bobanmilisavljevic7857 4 месяца назад
Because of the comma after the string. Without the comma it's just a string
@lecirconflex
@lecirconflex 5 лет назад
- 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.
@bakedbeings
@bakedbeings 4 года назад
I think a markdown -> html convertor with language based hilighting is a great way to make lecture notes for coding these days.
@bee_irl
@bee_irl 3 года назад
It's a Google doc, which answers your first three questions. If you open these assignments on the website, it uses monospace font and has proper line breaks. True, she could have used a screenshot in the question, but if this is your biggest gripe with the lecture, you can't complain much.
@lecirconflex
@lecirconflex 3 года назад
@@bee_irl You're missing the point.
@augustocarrillo5927
@augustocarrillo5927 Год назад
estaba buena la teacher
@chongke6215
@chongke6215 6 лет назад
In your case, t1 = ('cloud'), t2 = ('cold'), they are both tuples, so the answer shouldn't be ('sunny','cloudcold')?
@tommypham5874
@tommypham5874 6 лет назад
t1 is not a tuple. Without the comma t1 = 'cloud' is a string as opposed to t2 = 'cold', which is a tuple denoted by the comma. Since t1 is a string, think of it as a single set of multiple characters and t2 as a set of two elements 'cold' and empty. The first element of a string is the first character whereas the first element of a tuple is the first entire element itself. So the method would evaluate to ("Sunny", "ccold")
@intuit13
@intuit13 6 лет назад
I don't think that's true anymore. I don't think a single-element tuple needs the comma to "make" it a tuple any longer. I can't test this right now, but I was pretty sure this was one of the differences with 3.6 or 3.6.5 that I noticed while reading from a book that was using 3.5 (another being that dictionaries are no longer random-ordered, but instead are now insertion-ordered).
@intuit13
@intuit13 6 лет назад
someone correct me if I'm wrong
@mqL49J
@mqL49J 5 лет назад
Python 3.7.0: >>> type(('cloudy')) >>> type(('cold',))
@lecirconflex
@lecirconflex 5 лет назад
If you look closely, you can see that the first one is t1 = ('cloud'). In Python, this makes this value a string, because there is no comma within the braces. This is similar to (1 + 2), which doesn't create a tuple either. The second one, t2, however, has a trailing comma ('cold',) - This creates a tuple. The tuples are created inline, therefore the braces are required to disambiguate the tuples from the parameters of the function call. However, the braces aren't even required to create a tuple. You could do t2 = 'cold', which would create a tuple (because of the comma). This means that the call is actually always_sunny('cloudy', ('cold', )). Therefore t1[0] gives you a c, but t2[0] gives you the whole string.
@user-bm6xs6ih5n
@user-bm6xs6ih5n 6 лет назад
The hand writing is worse than a six-year-old.
@vincentdsouza8711
@vincentdsouza8711 5 лет назад
She's actually using a stylus as touch input on the screen
@adarshkumbhar4848
@adarshkumbhar4848 2 года назад
Better than yours...
Далее
Simple Lists
2:49
Просмотров 31 тыс.
7. Testing, Debugging, Exceptions, and Assertions
41:33
#kikakim
00:18
Просмотров 2,2 млн
8. Object Oriented Programming
41:44
Просмотров 607 тыс.
2024 MIT Integration Bee - Finals
1:09:25
Просмотров 487 тыс.
19. Phase-locked Loops
41:47
Просмотров 134 тыс.
Turing machines explained visually
8:46
Просмотров 272 тыс.
Python lists, sets, and tuples explained 🍍
15:06
Просмотров 226 тыс.
Python lists vs. arrays: How similar are they?
11:45
Просмотров 15 тыс.