-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_for_loop.py
More file actions
75 lines (67 loc) · 2.94 KB
/
Copy path12_for_loop.py
File metadata and controls
75 lines (67 loc) · 2.94 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#======================================================================================
# LOOPS
#--------------------------------------------------------------------------------------
# A loop is used to repeat a block of code multiple times automatically.
# Python has mainly two loops
# 1) for loop
# 2) while loop
#=====================================================================================
#======================================================================================
# FOR LOOP
#--------------------------------------------------------------------------------------
# A for loop is used to repeat a block of code for each item in a sequence.
# sequence can be: range(), list, tuple, string, set, dictionary
# for loop is used when you know how many times you want to repeat.
#--------------------------------------------------------------------------------------
# basic syntax
#--------------------------------------------------------------------------------------
# for variable in sequence:
# code
#======================================================================================
#--------------------------------------------------------------------------------------
# for loop with range(start(default is 0 and inclusive), stop(exclusive), step(default is 1))
#--------------------------------------------------------------------------------------
# printing whole numbers from 0 to 9
for i in range(10): # Here 10 is exclusive
print(f"Round:{i}")
# printing whole numbers from 1 t0 9
for i in range(1,10):
print(f"Round:{i}")
# printing even numbers under 10
for i in range(2,10,2):
print(f"Even:{i}")
# printing odd numbers under 10
for i in range(1,10,2):
print(f"Odd:{i}")
#--------------------------------------------------------------------------------------
# for loop with string
#--------------------------------------------------------------------------------------
text = "Python"
loop = 0
for letter in text:
loop += 1
print(f"Iteration {loop} & letter-{letter}")
#--------------------------------------------------------------------------------------
# for loop with list
#--------------------------------------------------------------------------------------
numbers = [10,20,30,40,50]
for number in numbers:
print(number)
# for loop real use cases
scores = [80, 50, 60, 75]
total = 0
for score in scores:
score += total
print(f"Current Total:{score}")
print(f"Final Total:{total}")
# data cleaning and transformation using for loop
files = [" DATA.csv", "report.txt ", " final.txt " ]
for file in files:
file = file.strip().lower().replace("txt", "csv")
print(file)
#--------------------------------------------------------------------------------------
# python challenge
# print the 7-times table from 1 t0 10 using for loop
#--------------------------------------------------------------------------------------
for i in range(1,11):
print(f"7x{i}={7*i}")