For Loops With Strings

Поділитися
Вставка
  • Опубліковано 14 лют 2017
  • 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
    @abdelkarimjaja7740 5 років тому +1

    great lecture, thanks, MIT

  • @akbarrauf2741
    @akbarrauf2741 7 років тому +2

    thank you ,mit

  • @Zoronoa01
    @Zoronoa01 5 років тому +8

    00:23

  • @gpjapan
    @gpjapan 7 років тому

    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 5 років тому +5

      "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 5 років тому +9

      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"

  • @kikykeeyansyah4021
    @kikykeeyansyah4021 5 років тому +3

    Are the spaces include?

    • @funnyh0rr0r31
      @funnyh0rr0r31 5 років тому +2

      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 роки тому +5

      @@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 роки тому +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 7 місяців тому

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

  • @dg2q
    @dg2q Рік тому +2

    do we count the 2 spaces?

    • @alcikelly2496
      @alcikelly2496 10 місяців тому +2

      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 9 місяців тому

      @@alcikelly2496 gotcha thanks

  • @jfritz6233
    @jfritz6233 4 роки тому

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