My notes: Learning CS50 Python - Lecture 2 - Loops

0:00 Introduction

0:24 Loops

0:50 cat.py

3:24 while

Use diamonds for questions

The code

_______

i = 0

while i <= 3:

print("meow")

i += 1

_____

16:21 for and list

for and ls

ls= adding to the strings

all variables in one place

Why is this poorly written

-you need to mention everytime to be able to print meow

-Need to look at the extremes,

If you can meow in a million times, you can just type in 1M in the range and not manually putting it like in the previous code

naming a variable under single underscore to make it "pythonic"

for _ in range(3):

print("meow")

---

print("meow" *3)

result= '

meowmeowmeow

------

/n = if we want to have spaces

print("meow\n" * 3)

result

meow

meow

meow

------

print("meow\n" * 3, end="")

result:

meow

meow

meow

---------

27:00 Validating Input

infinite loop= goes

----

while True:

n = int(input("What's n?"))

if n > 0:

break

for _ in range (n):

print("meow")

#we meow with the given number)

#common paradign in python but only until the user gives you the value

-----

32:34

def (main):

meow(3)

def meow(n):

for _ in range(n):

print("Meow!")

----

def (main):

number= get_number()

meow(3)

def get_number():

while True:

n=int(input("What's n" ))

return n

def meow(n):

for _ in range(n):

print("Meow!")

main()

-----

def main():

number = get_number()

meow(number)

def get_number():

while True:

n = int(input("What's n"))

if n > 0:

break

return n

def meow(n):

for _ in range(n):

print("Meow!")

main()

34:59 Iteration with Lists

How to print list

students = ("Hermione", "Harry", "Ron")

print()

----

students = ("Hermione", "Harry", "Ron")

print(students[0])

----

students = ("Hermione", "Harry", "Ron")

print(students[0])

print(students[1])

print(students[2])

-----

Can use a For Loop to reitierate anything

students = ("Hermione", "Harry", "Ron")

for student in students:

print(student)

//i could use underscore but it will be too cryptic

for student in students:

print(s)

students = ("Hermione", "Harry", "Ron")

for student in students:

print(student)

//

40:36 len

len -will tell you the length

students = ("Hermione", "Harry", "Ron")

for i in range(lens(students)):

---------------------------

students = ("Hermione", "Harry", "Ron")

for i in range(lens(students)):

print(i+1, students(i))

45:47 Dictionaries

dict ==> dictionaries data structure that allow you to associate

keys and values

ls-mutliple set of values vs dictionaries: more than words can be other variables

//

students = ("Hermione", "Harry", "Ron", "Draco")

houses = ("Gryffindor", "Gryffindor", "Gryffindor", "Slytherin")

for i in range(lens(students))

print(i+1, students(i))

{} <-- curly braces

--

students = {

"Hermione": "Gryffindor",

"Harry": "Gryffindor",

"Ron": "Gryffindor",

"Draco": "Slytherin",

}

print(students["Hermione"])

print(students["Harry"])

print(students["Ron"])

print(students["Draco"])

------------

students = {

"Hermione": "Gryffindor",

"Harry": "Gryffindor",

"Ron": "Gryffindor",

"Draco": "Slytherin",

}

print(students["Hermione"])

print(students["Harry"])

print(students["Ron"])

print(students["Draco"])

for student in students:

print(student)

----

students = {

"Hermione": "Gryffindor",

"Harry": "Gryffindor",

"Ron": "Gryffindor",

"Draco": "Slytherin",

}

print(students["Hermione"])

print(students["Harry"])

print(students["Ron"])

print(students["Draco"])

for student in students:

print(student, students[student])

-----

students = {

"Hermione": "Gryffindor",

"Harry": "Gryffindor",

"Ron": "Gryffindor",

"Draco": "Slytherin",

}

print(students["Hermione"])

print(students["Harry"])

print(students["Ron"])

print(students["Draco"])

for student in students:

print(student, students[student], sep=",")

-----

57:02 Lists of Dictionaries

students = [

{"name": "Hermione", "house": "Gryffindor", "patronus": "Otter"},

{"name": "Harry", "house": "Gryffindor", "patronus": "Stag"},

{"name": "Ron", "house": "Gryffindor", "patronus": "Jack Ruselle"},

{"name": "Draco", "house": "Slytheric", "patronus": "None"},

]

None---> literally None

First letter is capital

No need to put "" in none

------

students = [

{"name": "Hermione", "house": "Gryffindor", "patronus": "Otter"},

{"name": "Harry", "house": "Gryffindor", "patronus": "Stag"},

{"name": "Ron", "house": "Gryffindor", "patronus": "Jack Ruselle"},

{"name": "Draco", "house": "Slytheric", "patronus": None},

]

for student in students:

print (student["name"])

Result:

Hermione

Harry

Ron

Draco

---

for student in students:

print (student["name"], student["house"],sep="," )

----

final

students = [

{"name": "Hermione", "house": "Gryffindor", "patronus": "Otter"},

{"name": "Harry", "house": "Gryffindor", "patronus": "Stag"},

{"name": "Ron", "house": "Gryffindor", "patronus": "Jack Ruselle"},

{"name": "Draco", "house": "Slythericn", "patronus": None},

]

for student in students:

print (student["name"], student["house"],sep="," )

1:05:02 Nested Loops

Convert this

print ("#")

print ("#")

print ("#")

to this

for _ in range(3):

print("#")

-------

def main():

print_column(3)

def print_column(height):

for _ in range (height):

print("#")

main()

-----

def main():

print_column(3)

def print_column(height):

for _ in range (height):

print("#\n"* height, end="")

main()

---

def main():

print_row(4)

def print_row(width):

print("?"*width)

main()

result

????

----

def main():

print_square(3)

def print_square(size):

for i in range(size):

main()

----

def main():

print_square(3)

# for each row in square

def print_square(size):

for i in range(size):

#for each brick in row

for j in range(size):

#print brick

print("#", end="")

print()

main()

why do i have print outside inner loop but inside inner loop

so that it prints for each row and every brick

def main():

print_square(3)

def print_square(size):

for i in range(size):

print("#"* size)

main()

----

def main():

print_square(3)

def print_square(size):

for i in range(size):

print_row(size)

def print_row(width):

print("#"*width)

main()

---in decomposing it

1:20:17 Conclusion

Projects

camelCase

with debugging

camelCase=input("")

snake_case=""

for i in range(len(camelCase)):

print("camelCase[i]" + camelCase[i])

if camelCase[i].isupper():

snake_case += "_" + camelCase[i].lower()

else:

snake_case += camelCase[i]

print("snake_case" + snake_case)

print("snake_case:", snake_case)

Final Output

camelCase = input("camelCase: ")

snake_case = ""

for i in range(len(camelCase)):

if camelCase[i].isupper():

snake_case += "_" + camelCase[i].lower()

else:

snake_case += camelCase[i]

print("snake_case:", snake_case)

////

camelCase = input("camelCase: ")

snake_case = ""

for i, char in enumerate(camelCase):

if char.isupper():

if i > 0:

snake_case += "_"

snake_case += char.lower()

else:

snake_case += char

print("snake case:", snake_case)

amount_due = 50

while amount_due > 0:

coin = int(input("Insert Coin: "))

if coin == 5 or coin == 10 or coin == 25:

amount_due -= coin

if amount_due > 0:

print("Amount Due:", amount_due)

else:

print("Change Owed:", abs(amount_due))

break