For Loops With Strings

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 how loops can be used with strings in Python.
License: Creative Commons BY-NC-SA
More information at ocw.mit.edu/terms
More courses at ocw.mit.edu

Пікірлер: 17

  • @abdelkarimjaja7740
    @abdelkarimjaja77405 жыл бұрын

    great lecture, thanks, MIT

  • @akbarrauf2741
    @akbarrauf27417 жыл бұрын

    thank you ,mit

  • @Zoronoa01
    @Zoronoa016 жыл бұрын

    00:23

  • @gpjapan
    @gpjapan7 жыл бұрын

    Thank you for your lecture. I was confused by this code. I think it is because I am confused about char1 and char2. char2 does not refer to the 2nd character in s2. Is char undefined? Python knows what char refers to? Thanks again for your lecture. It is very helpful.

  • @phillipgonzalez9776

    @phillipgonzalez9776

    6 жыл бұрын

    "char1" and "char2" are just the names of temporary variables that will represent each character in the strings s1 and s2, respectively, because of the for loops.

  • @phillipgonzalez9776

    @phillipgonzalez9776

    6 жыл бұрын

    Another thing that could be potentially confusing is that the print statement says "common letter" but the code actually is comparing every character, including the " " characters, hence "common letter" being printed 7 times for the matches "m", "i", "t", " ", "u", " ", "r"

  • @dg2q
    @dg2q Жыл бұрын

    do we count the 2 spaces?

  • @alcikelly2496

    @alcikelly2496

    10 ай бұрын

    Yes, you're absolutely right! It prints "common letter" for "m", "i", "t", " ", "u", " ", and "r", which is quite misleading, because it is checking every CHARACTER in the string, not just letters.

  • @dg2q

    @dg2q

    10 ай бұрын

    @@alcikelly2496 gotcha thanks

  • @kikykeeyansyah4021
    @kikykeeyansyah40215 жыл бұрын

    Are the spaces include?

  • @funnyh0rr0r31

    @funnyh0rr0r31

    5 жыл бұрын

    Where are included? In string every symbol considered an element and it's part of string. For example string "nice day" will have 8 symbols. " nice day " - 10 symbols.

  • @shokobata

    @shokobata

    4 жыл бұрын

    @@funnyh0rr0r31 If you mean if the loop checks if the spaces are equal then yes

  • @thewryreport

    @thewryreport

    3 жыл бұрын

    @@shokobata Then why is the answer not 10? m = 1, i = 2, t = 1, space = 2, u = 1, space = 2, r = 1

  • @thewryreport

    @thewryreport

    3 жыл бұрын

    OK I think I get it now, it's because of the break, it only counts the 1st instance of each letter, so it's just m + i + t + space + u + space + r once each

  • @hieunguyenminh4677

    @hieunguyenminh4677

    8 ай бұрын

    @@thewryreport thanks, i also think the answer is 10 before

  • @jfritz6233
    @jfritz62334 жыл бұрын

    The for loop really should have an else: statement in case the result is false.