Python Tutorial - 9. For loop

preview_player
Показать описание
The topic which I will explain to you today is about “for statement” also known as “loop statement”, “for” loop, “while statement”, how to create “for” loop, why “for” loop is used.

Topics that are covered in this Python Video:
0:00 Introduction
3:34 Create a for loop (Syntax of for loop)
8:17 Why for loop is used?
8:26 Use of range() function
21:37 While statement

Next Video:

Рекомендации по теме
Комментарии
Автор

# i am counting tails also for my practise

head_count = 0
tail_count=0
results=["heads", "tails", "tails", "heads", "tails", "heads", "heads", "tails", "tails", "tails"]
for item in results:
if item=="heads":
head_count=head_count+1
elif item=="tails":
tail_count=tail_count+1
else:
pass
print("the total head is:", head_count)
print("the total head is:", tail_count)

sabaridharan
Автор

#Multi loop is not required for exercise3

for i in range(1, 6):
print (i*"*")

Output:
*
**
***
****

basilam
Автор

You are just an awesome teacher thank you so much for best lessons

ishwarade
Автор

for num in range(0, 6):
for i in range(num):
print("*", end=" ")
print(" ")

abhishekr.p
Автор

1)result=['heads', 'tail', 'heads', 'heads', 'tail', 'heads']
count=0
c=0
for i in range(len(result)):
if result[i]=='heads':
count=count+1
else:
c=c+1
print( 'head count is:', count)
print('tail count is', c)


2)expense_list=[2350, 2100, 2500, 3100, 2980]
num=input('Enter the expenditure:')
num=int(num)
for i in range(len(expense_list)):
if expense_list[i] != num:
continue
else:
print ('the expenditure was in', i+1, 'month')


3)n=input('character:')
n=str(n)
for i in range(6):
print(i*n)

pavi_thra_gowda
Автор

num=int(input("enter the number of rows:")
for i in range(1, num+1)
for j in range(1, 1+i)
print("*", end=" ")
print()



if you dont understand then try to debug it and see how the two loop works.

calistussolomon
Автор

results=["heads", "tails", "tails", "heads", "heads", "heads", "tails", "heads", "heads", "tails"]
total=0
for i in results:
if i == "heads":
total=total+1
print("Total of heads:", total)

...works

meestevacor
Автор

Hello, sorry! I have a question. At the answer in the second exercise in For/While Statement. Why did you put month=-1? I don't get it and why did you put if month!=-1 at the final? Thanks

HeiterTijera
Автор

Could you please explain your code in for loop chapter question 2. why did you choose month = -1?

karthikchekuri
Автор

Very low volume in videos. Please can you increase your volume from next time.

mayurpatil
Автор

for the exercises, I looked at your answers and did exactly how you put it, but in mine, it doesn't work out. like if I put 3100 for the expense amount, it will say 3x 'You didn't spend 3100 in any month'. Why is that?

urbansingerr
Автор

As of 4:35, the value of each "item" variable in the for loop is equal to a specific value in your list (e.g. 2340, 2500, etc.)

As of 12:49, the value of each "i" variable in the for loop refers to an index number of the list.

How can the value of each "item" and each "i" variable be different, when they are referring to the same list?

BoxerDogs
Автор

Hi Sir, ur command doesnt define "item" like that in earlier exercise "dish" in cuisine.

milindbharankar
Автор

for incrementation it will be better if we use i+=1 instead of i=i+1

weshop
Автор

why when we change variable name 'total' in for loop we get only the last number from list and not sum of all numbers (when we want to print it out of course.... for example anything = total + exp[i]
print (anything)

markobe
Автор

Hi Sir,

Where i can find the answers on ur website?

yahiagamal
Автор

Result=("heads", "tails", "heads", "heads")
Count=0
For i in range(Len(result)):
If I==heads
Count=count+1
Print("number of heads :", count)

Veermidu
Автор

result=["heads", "tails", "tails", "head", tails", "head", "head", "tails", "tails", "tails"]
total=0
for i in result:
if i ==head:
total=total + 1
i = i + 1
print("Total", total)

sahayaajay
Автор

can anyone provide a solution to the 2nd problem in the exercise?

OffDutyCoder
Автор

exp_list=[2340, 2500, 2100, 3100, 2980]
mon_list=["Jan", "Feb", "March", "April", "May"]
a=int(input("Enter the expense amount "))
for i in range(len(exp_list)):
if a!=exp_list[i]:
continue
else:
print("Expense of ", a, "occured in ", mon_list[i])
break

ameyamayekar
welcome to shbcf.ru