# Python
Lists and tuples are arguably Python’s most versatile, useful data types. You will find them in virtually every nontrivial Python program. ❤️ 🔥 ✨
...A typical mutable object would be a list, like we had before. It could also be a dictionary. We can change those after the fact. Or it could also be a set. Those are all mutable. Then if you have a custom class, that’s also mutable by default. We can just go in and change attributes on the class. Now, what are some examples for objects that are immutable, that can’t be freely modified in Python? One typical example would be a string.
The string '123', we can’t actually reach in and modify characters in that string. So it’s immutable. Another example would be the tuple that I just showed you. Or another example could be a frozenset.
In short, a list is a collection of arbitrary objects, somewhat akin to an array in many other programming languages but more flexible. Lists are defined in Python by enclosing a comma-separated sequence of objects in square brackets ([]), as shown below:
>>> a = ['one', 'two', 'there', 'four']
>>> print(a)
['one', 'two', 'there', 'four']
A list can contain any assortment of objects. The elements of a list can all be the same type, or the elements can be of varying types.
Individual elements in a list can be accessed using an index in square brackets. This is exactly analogous to accessing individual characters in a string. List indexing is zero-based as it is with strings.
.... (to be added)
# Tips
Print:
# print("print("What to print")") <== double quotes inside a double quotes cause interpreter to think "print(" and ")" as string, and What to print is a command --> error.
print("print('What to print')")
print("Hello\nHello\nHello") # print new lines
print("What" + " "+ "is that ?")
print('String concatenation is done with the "+" sign')
print("String concatenation is done with the "+" sign") # different from above line!
print("Hello " + input("What is your name? ") + "!")
print(len(input("What is your name? ")))
Int and str data type
a = 123_456_789 # _ is the separator for large number.
print(a)
##############
num_char = len(input('What is your name ? '))
# str(num_char) is neccessary to convert int to str, for the following code works properly (can't concatenate string and number)
print("Your name has " + str(num_char) + " characters.")
print(type(a)) # show which type of data variable 'a' is.
# other functions: int(), float() to convert data type.
Mathematical Order
print(3 * 3 + 3/3 -3 + 3**3) # ans = 34
#PEMDAS but has additional left to right rule)
f_string
height = 1.9
weight = 60
#f-string
print(f"Your height is {height} m and your weight is {weight} kg")
#can concatenate any data types without converting
#result: Your height is 1.9 m and your weight is 60 kg
Assignment
# Assignment isn't what most beginners think it is. People think: a = 1 means that you are creating a container called a and putting a value 1 in it.
# In reality, what you are doing is creating an integer object on the right side of the =, and "tagging" that object with a name called a.
from random import randrange
def append_Arr(some_list):
some_list.append(randrange(-100, 100))
my_list = []
append_Arr(my_list)
print(my_list)
# In the global scope, we've created an empty list object and gave it a name my_list. Within append_Arr function, we assigned that empty list with the name some_list.
# The same object is tagged with multiple names. We are mutating some_list, which is the same object as my_list in the outer scope.
# Intermediate courses
https://www.pluralsight.com/courses/python-functional-programming
https://www.pluralsight.com/courses/python-design-patterns-playbook
https://www.pluralsight.com/paths/python-for-data-analysts
# Python Resources:
Python Pathlib:
https://miguendes.me/python-pathlib (opens new window)
Website containing various python algorithms:
https://siddhesh-agarwal.github.io/Python-Algorithms (opens new window)
https://www.reddit.com/r/Python/comments/rbqv5k/a_complete_26_week_course_to_learn_python_for/