-3

I'm a new programmer and I'm having a lot of trouble understanding for loops and while loops. In what situations would I know to use a for loop and in what situations would I know to use a while loop?

Also, could you explain to me what these 2 codes mean? I have a a lot of confusion.

1 function:

def every_nth_character(s, n):
      """ (str, int) -> str

      Precondition: n > 0

      Return a string that contains every nth character from s, starting at index 0.

      >>> every_nth_character('Computer Science', 3)
      'CpeSee'
      """

      result = '' 
      i = 0

      while i < len(s): 
            result = result + s[i]
            i = i + n 
      return result

****What does s[i] mean?****

2nd function:

def find_letter_n_times(s, letter, n):
      """ (str, str, int) -> str

      Precondition: letter occurs at least n times in s

      Return the smallest substring of s starting from index 0 that contains
      n occurrences of letter.

      >>> find_letter_n_times('Computer Science', 'e', 2)
      'Computer Scie'
      """
      i = 0 
      count = 0
      while count < n:
            if s[i] == letter: 
                  count = count + 1
            i = i + 1 
      return s[:i]

what does s[i] and s[:i] mean??

The6thSense
  • 8,103
  • 8
  • 31
  • 65
sarah
  • 1
  • 3
  • 1
    they are know as [slicing](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) – The6thSense Oct 19 '15 at 11:47
  • https://docs.python.org/2/tutorial/introduction.html – khelwood Oct 19 '15 at 11:49
  • [slicing](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation), [for-loop](http://stackoverflow.com/questions/1292189/how-does-python-for-loop-work) – Eli Korvigo Oct 19 '15 at 11:49

2 Answers2

1

S is a list of characters 'Computer Science'["C","o","m","p"...], and i is the indexposition for each item/character in the list S, so in your case you've stated that your loop counts each third(3) item in S as long as there are items in S, that is, S[i] = [C], S[i]=[p], S=[e], S[i]= C, S[i]=p, where i is each third element in S. In the second case you've defined i as a variable with value 0, after each loop i increases with +1, i = i + 1, and [:i] means return elements in S up to the latest looped slice, for instance "Computer Scie" + one additional loop would give you "Computer Scien" (i = 9 (the current range of S/number looped characters in S) -> i+1 (increased by +1) -> i=10 (i = 10, S[i]=10 means the first 10 indexpositions/charachters in S]

user1749431
  • 559
  • 6
  • 21
0

Your first question about differencies in while and for loops is completely answered here.

Strings and indexing:

  • Variable s holds a string value. As you may have noticed, it has been submitted as an argument for every_nth_character(s, n) function.
  • Now every letter in a string is in some position and that position is called index. indexing starts from 0. So if we have a string s containing value 'foo123', it's first character s[0] is 'f' and the last character s[5] = 3.
  • String can be cutted and sliced using ':' in the index field. Referring to the previous example, we have determined string s. Now you can take only first three characters of that string by using s[:3] and get 'foo' as a result. Read more about it here

Loops:

  • While and for loops start over and over again until they reach the limit you have determined.
  • For example:

    x = 0 while x < 5: print x x = x + 1

    prints numbers from 0 to 4. Variable x increases +1 at every single run and the loop ends when x reaches value 5.

Get familiar with Python documentation page, it will help you a lot in future especially in basic things. Google search: Python (your-python-version) documentation

Community
  • 1
  • 1
G4mo
  • 597
  • 1
  • 8
  • 18