-
Notifications
You must be signed in to change notification settings - Fork 41
/
for-loops.py
42 lines (28 loc) · 1.43 KB
/
for-loops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# ------------------------------------------------------------------------------------
# Tutorial: For-loops in Python
# ------------------------------------------------------------------------------------
# We use for loops when we have to access elements in a DS or while working within a range of numbers.
# One of the loops which helps us to iterate through a particular DS.
anime = ['Luffy', 'Naruto', 'Zoro', 'Midoriya', 'Lelouch', 'Levi', 'Natsu', 'Sanji', 'Shanks', 'Kakashi', 'Kageyama']
# What we have to do is print each of these names in list anime
for name in anime:
print(name)
# This will help you to access and print each of the names in the list.
# Alternate
length = len(anime)
for i in range(length):
print(anime[i])
# Also there is an |else| stance for each of the loops so here it is for for-loop:
for name in anime:
print(name)
else:
print('Loop has finished')
# It gives us a message when the loop is finished.
# Practice with the below challenge
# ------------------------------------------------------------------------------------
# Challenge: You have given a list of numbers and you have to output how many of the numbers are even and how many are odd.
# list = [ 3, 21, 4, 56, 34, 69, 45, 63, 99 ]
# Example : list = [ 2, 4, 5, 8, 34, 21 ]
# Output:- Odd : 2, Even : 4
#
# ------------------------------------------------------------------------------------