Skip to content

Commit 0a854f7

Browse files
Caleb CurryCaleb Curry
authored andcommitted
working on loops
1 parent 52ee76a commit 0a854f7

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

‎beginner_python/05-logic-and-if.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,29 @@
8484
print("You may have some years left in you")
8585

8686

87+
########## CONDITIONS WITH BOOLEANS ##########
88+
89+
90+
#When working with a boolean variable, you may think to do this
91+
caleb_is_cool = False
92+
if caleb_is_cool == True:
93+
print("Let's be friends")
94+
else:
95+
print("Eww")
96+
97+
98+
#since the == operator returns True or False, and the variable is already false...
99+
#We can do this instead:
100+
caleb_is_cool = False
101+
if caleb_is_cool:
102+
print("Let's be friends")
103+
else:
104+
print("Eww")
105+
106+
87107
########## LOGICAL OPERTATORS #########
88108

109+
89110
#Logical operators can be used to make complex expressions that ultimately evaluate to true or false.
90111
#and --> both must be true
91112
#or --> one must be true

‎beginner_python/06-loop-basics.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
print("loading...", end= " ")
4848
print()
4949

50+
#i is known as in iteration variable
5051
#i will obtain a value each iteration based on the range we select.
5152
#for range(10), we get 0-9:
5253

‎beginner_python/07-more-looping.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@
9494
i -= 2
9595
print()
9696

97+
98+
########## CONVERTING WHILE TO FOR AND BACK ###########
99+
100+
101+
#I think it is important to comfortably work with for and while
102+
#Both loops need an initialization, condition, and update.
103+
104+
initialization = 5
105+
stop_at = 10
106+
incremenet = 1
107+
108+
for i in range(initialization, stop_at, increment):
109+
print("for loop:", i)
110+
111+
112+
while(initialization < stop_at):
113+
print("while loop:", initialization)
114+
initialization += increment
115+
116+
117+
#To make them match, the update should come at the end of the while
118+
#When passing a variable to range, the variable is not affected
119+
#Range returns new data and initialization is left at 5.
120+
121+
97122
########## ELSE WITH WHILE ##########
98123
#The else statement for a while will execute if no break is hit.
99124

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#The only form of nesting we've encountered so far was an if statement within a loop.
2+
languages = ["Python", "C++", "Java", "Perl", "C#"]
3+
4+
for language in languages:
5+
if language == "C#":
6+
print(language + " found")
7+
break
8+
9+
#proper nesting is vital inside of python. Unlike many other languages,
10+
#python is whitespace sensitive. The position of the code is important
11+
12+
for language in languages:
13+
if language == "C#":
14+
print(language + " found")
15+
break
16+
17+
#^^ This loop does something completely different.
18+
#Syntactically, the only difference is the position of the break keyword.
19+
#It will break at the end of the first iteration no matter what
20+
21+
########## NESTED IF ##########
22+
23+
logging = True
24+
logging_in = True
25+
name = "Caleb"
26+
27+
if logging_in == True:
28+
if logging == True:
29+
print(name + " is logging in.") #log console, file, or db.
30+
print("Welcome, " + name)
31+
32+
########## CONVERTING COMPLEX CONDITIONAL TO NESTED IF ###########
33+
34+
#It is common in more complex code to see nested if statements
35+
#This can often replace complex expressions that use numerous logical operators
36+
#Either way is not better, just whatever is clearer to you
37+
38+
age = 20
39+
fun = False
40+
likes_to_dance = True
41+
42+
if (age < 30 or fun == True) and likes_to_dance:
43+
print("You're invited to the party!")
44+
else:
45+
print("get lost freakbag") #most general
46+
47+
#This could alternatively be represented with nested if statements
48+
#and can allow more specific responses
49+
50+
if(age < 30 or fun == true):
51+
if likes_to_dance:
52+
print("You're invited to the party!")
53+
else:
54+
print("How could you not like to dance?") #Specific to dancing
55+
else:
56+
print("You're either too old or not fun enough to be in my party")
57+
#Note how we can't easily say which. We say OR.
58+
59+
60+
########## INTRO TO NESTED FOR ##########
61+
62+
#If you are fairly new, nested loops can be confusing. Pay attention!
63+
#Stop texting and looking at memes...CODE!
64+
#We can loop a loop using a nested loop.
65+
#The inner loop will run each iteration of the outer loop.
66+
67+
for i in range(4):
68+
for j in range(5):
69+
print(j, end=" ")
70+
print() #go to next line
71+
72+
#The result? The inner loop prints 0 - 4. We then do this 4 times
73+
#After each complete 0-4 print, we go to the next line with print()
74+
#This puts the next loop on the next line of output

‎beginner_python/09-nested-loops.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
########## NESTED FOR LOOP PT2 ###########
2+
3+
#The previous section had this loop.
4+
#The only thing we did was print j and a new line.
5+
for i in range(4):
6+
for j in range(5):
7+
print(j, end=" ")
8+
print() #go to next line
9+
10+
#Review this and make sure you understand what is going on.
11+
12+
#We could also say what iteration of the outer loop we are on
13+
for i in range(4):
14+
print("iteration " + str(i), end=": ")
15+
for j in range(5):
16+
print(j, end=" ")
17+
print() #go to next line
18+
19+
#We could also do a more complicated loop to work with the numbers.
20+
for i in range(4):
21+
print("Count by " + str(i), end=": ")
22+
for j in range(5):
23+
print(j*i, end=" ")
24+
print("...") #go to next line
25+
26+
#What's going on here?
27+
#We are taking the outer iteration variable and multiplying to by the inner
28+
#in other words, we are counting to 5 each time
29+
#but we are multiplying the result by 0, 1, 2, 3
30+
31+
########## OUTER LOOP VARIABLES IN INNER range() - TRIANGLES #########
32+
33+
#I had no idea what to call this section. Whatever.
34+
35+
#We can always use the outer loop iteration variable within the inner loop.
36+
#What if we wanted to count to 9 for each iteration,
37+
#but start with a higher number each time?
38+
39+
for i in range(10):
40+
for j in range(i, 10):
41+
print(j, end=" ")
42+
print()
43+
44+
#The first iteration i will be 0, so we get: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
45+
#The second iteration i will be 1, so we get 1, 2, 3, 4, 5, 6, 7, 8, 9
46+
#etc.
47+
48+

0 commit comments

Comments
 (0)