Coding Lesson (18-22 Nov)
Monday
Last time I already learned about Indexing and Slicing. Today, I learned on Kaggle about Removing Items and Adding Items in Lists.
Removing Items
To remove items on the list, you only need to use .remove()
For example:
flowers_list.remove("hard-leaved pocket orchid")
print(flowers_list)
pink primrose,canterbury bells,sweet pea,english marigold,tiger lily,moon orchid,bird of paradise,monkshood,globe thistle
Adding Items
To add items on the list, you will need to use .append()
For example:
flowers_list.append("dandelion")
print(flowers_list)
pink primrose,hard-leaved pocket orchid,canterbury bells,sweet pea,english marigold,tiger lily,moon orchid,bird of paradise,monkshood,globe thistle,dandelion
Tuesday
Continue learning on Kaggle. I learned about "Lists are not just for strings". Lists can have items with any data type, including booleans, integers, and floats. Here's an example:
hardcover_sales = [139, 128, 172, 139, 191, 168, 170]
print("Length of the list:", len(hardcover_sales))
print("Entry at index 0:", hardcover_sales[0])
Length of the list: 7
Entry at index 0: 139
Here hardcover_sales is a list of integers. You can add length, extend the list, etc. You can also use minimum with min() and maximum with max().
print("Minimum:", min(hardcover_sales))
print("Maximum:", max(hardcover_sales))
Minimum: 128
Maximum: 191
And to add every item in the list you can use sum().
print("Total books sold in one week:", sum(hardcover_sales))
Total books sold in one week: 1107.
Wednesday
Learning to a more "advanced" Python course on Kaggle. I started by learning "Hello, Python", like syntax, variable assignment, and arithmetic operators. Let's just start from variable assignment:
spam_amount = 0
print(spam_amount)
# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)
spam_amount = spam_amount + 4
if spam_amount > 0:
print("But I don't want any spam!")
viking_song = "Spam " * spam_amount
print(viking_song)
The spam_amount = 0 is a variable assignment. Here I create a variable and assign it the value of 0 using =, which is called the assignment operator.
Thursday
Today, I learned about loops in Python. Loops in Python provides three ways of executing which is, while, for, and nested. All of them have similar basic function, but they differ in their syntax and condition-checking time.
For now, I learned the while loop. So, what is while loop? a while loop is used to execute a block of statements repeatedly until a given condition is "satisfied". When the condition becomes false, the line immediately after the loop in the program is executed. Here's the example:
count = 0
while (count < 2):
count = count + 1
print("Hello Friend")
else:
print("In Else Block")
Hello Friend
Hello Friend
In Else Block
On the example, I use a while loop to print Hello Friend two times and use a variable called count from 1 to 2. Also, the else line is executed because the while loop condition becomes false.
Comments
Post a Comment