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??