1+ #Once you understand logic, you can apply the same principles to looping
2+
3+ languages = ["Python" , "C++" , "Java" ]
4+
5+ for x in languages :
6+ print (x )
7+
8+ #The body of this loop (the indented part) will run 4 times
9+ #Each time is known as an iteration
10+ #Each iteration, the variable x will contain one element
11+ #iteration 1 will be the first element
12+ #iteration 2 will be the next, and so on.
13+ #The loop automatically stops.
14+
15+ #The variable name is irrelevant
16+ for language in languages :
17+ print (language )
18+
19+
20+ ########## PRINT WITHOUT NEWLINE ##########
21+
22+ print ("One" , end = " " )
23+ print ("Line" )
24+
25+ #This is useful for loops where you don't want to spam the terminal
26+ for language in languages :
27+ print (language , end = " " )
28+ print ()
29+ #can end with newline tfor future prints
30+
31+ #You could alternatively use end="\t" or end="" or whatever you want.
32+
33+ #You might be confused why we have to put end and not just:
34+ print ("DOEN'T AFFECT END" , " " )
35+
36+ #Because this is interpretted as a second argument to print
37+ #resulting in a printed " " followed by a newline.
38+ #We must use end= (This is an example of keyword argument)
39+
40+
41+ ########## RANGE FUNCTION ##########
42+
43+
44+ #instead of iterating through a list, we can use a function called range
45+ #This loop runs 10 times
46+ for i in range (10 ):
47+ print ("loading..." , end = " " )
48+ print ()
49+
50+ #i will obtain a value each iteration based on the range we select.
51+ #for range(10), we get 0-9:
52+
53+ for i in range (10 ):
54+ print (i , end = " " )
55+ print ()
56+
57+ #Important: the value to end at is never part of the range.
58+ #With range(10) we get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
59+
60+
61+ ########## RANGE START POSITION ##########
62+
63+
64+ #We can count 1-10 instead of 0-9 if desired:
65+ for i in range (1 , 11 ):
66+ print (i , end = " " )
67+ print ()
68+
69+ #We can start anywhere and end anywhere
70+ for i in range (5 , 7 ):
71+ print (i , end = " " )
72+ print ()
73+
74+
75+ ########## RANGE STEP ##########
76+
77+
78+ #Go the other way.
79+ #9 included. -1 not included (result is 9-0)
80+ for i in range (9 , - 1 , - 1 ):
81+ print (i , end = " " )
82+ print ()
83+
84+ #The third argument is called the step. we can set it to whatever
85+ #Count down from 100 to 0, by 10s.
86+ for i in range (100 , - 1 , - 10 ):
87+ print (i , end = " " )
88+ print ()
89+
90+ ########## SUM OF LIST ##########
91+
92+ #https://docs.python.org/3/library/math.html
93+ #methods taking an iterable can take a range (sum, prod)
94+
95+ #Sum of numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
96+ print (sum (range (1 , 11 )))
97+
98+
99+ ########## LIST FROM RANGE ##########
100+
101+ #We can make the range an official list using the list ctor
102+ #https://docs.python.org/3/library/stdtypes.html#list
103+ numbers = list (range (1 , 11 ))
104+ print (numbers )
105+
106+ #This is the besr way to easily print a list
107+ #as print(range(1,11)) doesn't work as expected
108+
109+ ########## FOR LOOP WITH INDEX ##########
110+
111+ languages = ["Python" , "C++" , "Java" ]
112+ for i in range (len (languages )):
113+ print (i , languages [i ])
114+
115+ #len will return 3. Passing 3 to range gives us 0, 1, 2
0 commit comments