# Lesson 5 Notes/Projects

In python, there are many ways to one thing. For example, there are three ways to easily and to let python handle putting every number in a certain range, into a list. The first way is:

list1 = list(range(10))
print(list1) # outputs [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


The second way is:

list1 = [num for num in range(10)]
print(list1) # outputs [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


The third way is:

list1 = []
for num in range(10):
list1.append(num)
print(list1) # outputs [0, 1, 2,3, 4, 5, 6, 7, 8, 9]


If you were to run these three different blocks of code, you will see that they all output the exact same thing. The first method simply force converts the numbers in the range of 0 - 10 into a list, which basically just splits the numbers up, and adds them to a list. The second method uses list comprenhension to add every item in the range of 0 - 10 to a list. The third method basically does basically what the second method does, except it takes more lines of code to write. The for loop assigns a new variable to each number in the range of 0 - 10, and adds that number to the list, and when the list is called, it simply outputs everything that is inside of itself. These three methods all work, and outputs the exact same result.

Now what if we wanted to keep track of what index of an element is what in a list? Or what if we wanted to see how the index position of something in a list, and not just blindly print the element out? Well, python has a awesome function called enumerate(). Basically, what enumerate() does, is it keeps track of the number of iterations something that is being iterated through has gone through. This means that enumerate() works especially well with something like a for loop, since a for loop works extremely well with iterations. Here is an example of how the enumerate() function works in a actual block of code:

list1 = ["aiyc", 'bob', "joe", "jeff"]
for index, item in enumerate(list1):
print((index, item))


The code above basically returns a tuple containing the index of where each item in the list "list1" is, and the value contained at the index position that was stated in the tuple before it. You could also not specify any other value other than the "item "variable to be defined in the for loop, and if you printed out the item variable, you will see that it will sort the index or count of the iterations from the for loop, however it might be better to actually define the count of the iterations from the enumerate() function, this way you know what it's enumerating and iterating through and counting, and not just counting something.

When using a conditional statement, and let's say you only need to print out one thing, or execute one line of code if that condition is met, then you don't necessarily have to put the line of code that you want to execute in a line inside of it, you could simply put it on the same line. This works fine when you use this syntax in terminal, but in a proper development environment such as pycharm, pycharm really doesn't like it when you use that kinda syntax in your code, and it will warn you saying that your code should be in the line inside and after the place where the condition is defined.

Again, the reverse thinking method really helps when you are trying to fully understand a code, when you are working line by line to comprehend exactly what each line of code does. For example, in this code:

i = 0
while True:
print(i)
i = i + 1
if i > 5:
break


In this code, python first recognizes that the value of the variable "i" is equal to 0, and that as long as the program is being run, it should print out the value of i, and add 1 to the value of i each time after printing it. Then it checks to see if the value of i is equal to 5, and if it is, then it should exit the loop, and therefore stop the program. The reverse thinking method comes into play for these few lines:

print(i) # outputs 0, 1, 2, 3, 4, 5
i = i + 1
if i > 5:
break


We know that if the value of i is greater than 5, then python will exit the loop and stop the program. And we know that everytime the loop occurs, we add one to the value of i. We know that 5 > 5 = False, since 5 is not greater than 5, however the value of 6 is greater than 5, which means that i's value is False, which means that the loop stops, and it does not print out 5, which means it only prints 0, 1, 2, 3, 4, 5.

Basic person information description program:

name = input("Please enter your name: ")
school = input("Please enter the name of the school that you go to: ")
print("Getting basic information...")
print("************************************")
print(f"Hi! My name is {name}")
print(f"I am a {gender}.")
print(f"I am {age} years old.")
print(f"I go to {school}.")

name = input("请输入您的名字： ")
gender = input("行输入您的性别：男/女 ")
age = input("请输入您的年龄： ")
school = input("请输入您去的学校：")
print("正在获取人员的基本信息...")
print("************************************")
print(f"你好！我的名字是{name}.")
print(f"我是一个{gender}人.")
print(f"我上{school}.")

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。QQ、微信在线，随时响应！V：Jiabcdefh
AI悦创 » Lesson 5 Notes/Projects