String and loops

Please put a glance over the following code: how does it works to give us the output for 12 times…

s = “Python Rocks”
for ch in s:
print(“Hello”)

#Output:
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

Hi, Uday.

As you can observe that the length of the “s” is 12 and for loop will iterate character by character 12 times. So it is printing “Hello” 12 times.
Kindly run the below code for more insights.

print(len(s))
for ch in range(len(s)):
print(ch)
print(“Hello”)

All the best!

2 Likes

It gives the below output: and that is the indices? that 0 (“zero”) will start counting before ‘P’ of ‘Python’?

0
1
2
3
4
5
6
7
8
9
10
11

The below is the output.
Yes you are correct for every index, it will print the “Hello”.

1 Like

Please, let help explain the working of the following 2 codes…I am confused.

Code1:

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
count = 0
for w in nums:
count = count + 1
print(count)

Code 2

accum = 0
for w in range(11):
accum = accum + w
print(accum)

Also, help explaining the difference between the following 2 codes and why the output varies

# Code1:
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for w in nums:
    accum = 0
    accum = accum + w
print(accum)


#Code2:

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
accum = 0
for w in nums:
  
    accum = accum + w
print(accum)

Hi, Good question!.

  1. Code1

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for w in nums:
accum = 0 # Here for every iteration the accum is initialized to 0.
accum = accum + w # It will do the sum but the accum value will again become zero
as it is inside for loop.
print(accum)

  1. Code2

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
accum1 = 0 # Here it is initialized to zero only once
for w in nums:
accum1 = accum1 + w # The sum values will be added iteratively.
print(accum1)

Note :- Once print the “accum” inside for loop and you will understand the difference.

All the best!

1 Like

Write one for loop to print out each element of the list several_things . Then, write another for loop to print out the TYPE of each element of the list several_things . To complete this problem you should have written two different for loops, each of which iterates over the list several_things , but each of those 2 for loops should have a different result.

#the above is the problem: Below I have written the code but not getting the expected result. Please assist.

several_things = [“hello”, 2, 4, 6.0, 7.5, 234352354, “the end”, “”, 99]

for i in several_things:
print(“several_things=”,several_things)
break;
for j in several_things:
print(type(several_things))
break;

Hi, Uday.

  1. As per question your for loop should print out each element of the list several_things , but you are just printing the list “several_things” only so it will print this only. so use i or j as these are you element instead of “several_thing”.
  2. You are using a break, this will break the loop just after 1st iterations only and you will get just the first element so remove it.

Now you can do it.

All the best!

I will try by implementing it. Thanks.

How well the following code diagnose the working of for loop

w = range(10)

count = 0

for num in w:
print(num)
count += num
print(count)
print(count)

Yes, this code will surely diagnose the the working of for loop as you are iterating the sum of all the elements one by one.
If you write the print(count) outside the for loop you will get the total sum.
if you write the count inside the for loop you will get the sum at every iterations.

All the best!

1 Like

Hello,

Please help me in writing code for the following problem:

Create a variable, b , and assign it the value of 15 . Then, write code to see if the value b is greater than that of a . If it is, a ’s value should be multiplied by 2. If the value of b is less than or equal to a , nothing should happen. Finally, create variable c and assign it the value of the sum of a and b .

#My Code: # I haven’t realised something?

a = 20
b = 15

value = 0
if value in (b > a):
value = a *2
print(value)
elif value in b<=a:
close()
value = a + b
value_of_the_sum = value
print(value_of_the_sum)

a = 20 # as per question a value is not defined.
b = 15

value = 0
if (b > a): #why you are using “in” this is a condition out is Boolean. “in” is the membership operator to check if element is there or not in ant data structures. not in comparisons.
value = a *2
print(value)
elif b<=a:
pass # if nothing to do then just write “pass”

c = a + b

value_of_the_sum = c
print(value_of_the_sum)

Kindly go through the course again!
It is explained in very detailed manner.

All the best!

1 Like

Thank you. Your advice and instructions are really helpful. I have got it.

In how many ways, can we identify a string in the list:

names = ["Sally ", “Molly”, “Dolly”, “Kally”, “Phil”, “Gyll”]

how can we get to know if string “kally” is present in the list?
logic? please! #with and without using count()

names = [ “Sally”, “Molly”, “Dolly”, “Kally”, “Phil”,“kally”]
search_element=input()

With count

count=0
index=[]
for i in range(len(names)):
if names[i].lower()==search_element:
count=count+1
index.append(i)
if count==1:
print("Total times “+search_element+ " present is “+ str(count )+” and present at index”+ str(index))
else:
print("Total times “+search_element+ " present are “+ str(count )+” and present at index”+ str(index)) ‘’’

Without count

for i in names:
if i==search_element:
print(search_element+" present at "+ str (names.index(i)) ) # This will give the index of last element.

I added an extra element “kally” to make it more clear.

All the best!

1 Like

Create an empty list called resps. Using the list

percent_rain, for each percent, if it is above 90, add the

string ‘Bring an umbrella.’ to resps, otherwise if it is

above 80, add the string ‘Good for the flowers?’ to resps,

otherwise if it is above 50, add the string ‘Watch out for

clouds!’ to resps, otherwise, add the string ‘Nice day!’

to resps.

Note: if you’re sure you’ve got the problem right

but it doesn’t pass, then check that you’ve matched up the

strings exactly.

Unable to get myself to the logic, please help understand…below is my code but to only print ‘Nice day!’

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]

resps = []

if len(percent_rain) > 90:
resps.append(“Bring an umbrella.”)

else:
if len(percent_rain) > 80:
resps.append(“Good for the folowers?”)
else:
if len(percent_rain) > 50:
resps.append(“Watch out for clouds!”)
else:
resps.append(“Nice day!”)
print(resps)

Hello, I have improvised on my code for the above query. Please help me with the desired output:

resps = percent_rain[0]
for eachcent in percent_rain:
if resps>90:
resps = eachcent
print(“Bring an umbrella”)
elif resps>80:
resps = eachcent
print(“Good for the flowers?”)
elif resps>50:
resps = eachcent
print(“Watch out for the clouds”)

Shall I write nested conditional for the desired output here?

Hi,
You are comparing by taking one by one element in the percent_rain, the same can be achieved using a for loop iterating through the list.

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]

resps = []

for percent in percent_rain:
if percent > 90:
resps.append(“Bring an umbrella.”)
elif percent > 80:
resps.append(“Good for the flowers?”)
elif percent > 50:
resps.append(“Watch out for clouds!”)
else:
resps.append(“Nice day!”)

print(resps)

All the best!