For Loops With Strings

  Рет қаралды 64,722

MIT OpenCourseWare

MIT OpenCourseWare

Күн бұрын

MIT 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016
View the complete course: ocw.mit.edu/6-0...
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
@Zoronoa01
@Zoronoa01 6 жыл бұрын
00:23
@akbarrauf2741
@akbarrauf2741 8 жыл бұрын
thank you ,mit
@abdelkarimjaja7740
@abdelkarimjaja7740 5 жыл бұрын
great lecture, thanks, MIT
@dg2q
@dg2q Жыл бұрын
do we count the 2 spaces?
@alcikelly2496
@alcikelly2496 Жыл бұрын
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 Жыл бұрын
@@alcikelly2496 gotcha thanks
@gpjapan
@gpjapan 8 жыл бұрын
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"
@kikykeeyansyah4021
@kikykeeyansyah4021 6 жыл бұрын
Are the spaces include?
@funnyh0rr0r31
@funnyh0rr0r31 6 жыл бұрын
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 4 жыл бұрын
@@shokobata Then why is the answer not 10? m = 1, i = 2, t = 1, space = 2, u = 1, space = 2, r = 1
@thewryreport
@thewryreport 4 жыл бұрын
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 Жыл бұрын
@@thewryreport thanks, i also think the answer is 10 before
@jfritz6233
@jfritz6233 5 жыл бұрын
The for loop really should have an else: statement in case the result is false.
4. Decomposition, Abstraction, and Functions
41:09
MIT OpenCourseWare
Рет қаралды 239 М.
5. Tuples, Lists, Aliasing, Mutability, and Cloning
41:28
MIT OpenCourseWare
Рет қаралды 233 М.
1%🪫vs 100%🔋
00:36
Аришнев
Рет қаралды 3,3 МЛН
Python String Slicing Examples with For Loops | Tutorial
11:51
Coding with Estefania
Рет қаралды 18 М.
3. String Manipulation, Guess and Check, Approximations, Bisection
45:02
MIT OpenCourseWare
Рет қаралды 387 М.
Naming Things in Code
7:25
CodeAesthetic
Рет қаралды 2,3 МЛН
For Loop Pattern Program In Java #25
15:49
Alex Lee
Рет қаралды 172 М.
2. Branching and Iteration
43:31
MIT OpenCourseWare
Рет қаралды 593 М.
How To Learn Any Skill So Fast It Feels Illegal
13:48
Justin Sung
Рет қаралды 1 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Richard Feynman: Can Machines Think?
18:27
Lex Clips
Рет қаралды 1,6 МЛН
For Loops - Beau teaches JavaScript
6:37
freeCodeCamp.org
Рет қаралды 97 М.
Deep Dive into LLMs like ChatGPT
3:31:24
Andrej Karpathy
Рет қаралды 641 М.