This month, I am re-introducing myself to Python via a DataCamp course.
I am taking the Data Scientist With Python track, starting with the “Introduction to Python” module.
Here’s what I have covered so far:
- The basic data types are integer, float, string, and Boolean.
- Lists in Python can contain a mix of data types, including lists.
- Python indexes start from 0. Negative indexes measure from the end of the list.
- We can a list by slicing multiple elements from an existing list.
- We can find an element in a list that is within a list.
- We can change list elements by index, add elements to the end of a list, and delete elements based on index.
- Changing a copy of a list may change the original list, unless we use list() when creating the new version.
Here’s some sample code:
#Create list with various data types
mylist = [“hi”, 1, True, [“hello”, “world”], [3.5, False], 420, 1.23, [“X”,”Y”,”Z”]]
#Get first element of list (index starts at 0)
mylist[0]
#Get last element in list
mylist[-1]
#Get elements in index 3 and 4 (slicing has inclusive start and exclusive end)
mylist[3:5]
#Get all elements up to index 3 (exclusive end)
mylist[:4]
#Get all elements starting at index 4
mylist[4:]
#Find item in a list within a list (this would return “Y” based on mylist)
mylist[-1][1]
#Change item within list using index
mylist[2] = False
#Change multiple items in list using index
mylist[0:2] = [“bye”,2]
#Add list elements
mylist + [“adding”,”elements”]
#Deleting list element
del(mylist[-1])
That’s all for this week. Enjoy.