Stack and Tuples.
Today I Learned: Wednesday 22nd June 2022

Photo by Mike Leyder on Unsplash

Stack and Tuples. Today I Learned: Wednesday 22nd June 2022

  • Flask is not compatible with working with a MERN stack. (MongoDB, Express, React, Node.) So my teammates will possibly be switching to another stack named FReMP. (Flask, React, MongoDB, Python).
    • I have limited experience with stacks. According to Fireship on Youtube, a stack is a ‘sandwich’ of technologies used to create a web app.
    • React is a JavaScript framework for the front-end, so I will need to get back to practicing Javascript.
    • Flask is Python based, and will be used to create the APIs to connect the front and back-end.
    • MongoDB is the database.
    • Python is used to create some part of the back-end. What exactly that is, I’m not sure.
  • For the Python PCEP training, I’ve been stuck on a problem involving finding prime numbers. I eventually got it done with some online help. Originally I tried to include the num > 1 condition from within the for statement. Like this:

    • if num > 1 and num % i == 0: \ return False \

    • With this code, the printout kept on missing the 2 on the list of primes up to 20.

    • So I found a solution online that wrote it like this.
    • if num > 1: \ for i in range(2, num): \ if (num % i) == 0: \

        return False \
      

      return True. \

    • That seemed to have worked. I’m not sure exactly why, I’m sure it will make sense in the future. I will move on with the training for now.

  • Almsot at the finish line now. I hope to do the test at the end of this week.
  • I just struggled a lot with a program trying to convert liters per 100km to miles per gallon.
    • The course hints and solutions helped, but I still don’t understand it entirely. I think I need better understand how to break down the number into more manageable numbers like meters.
  • Scope:

    • Usually if you create a variable from within a function, its scope is limited to that function. As in it is not accessible from outside of the function.
    • But there is keyword to get around this, ‘global’
    • If you put the keyword ‘global’ before the variable name, it will be visible from outside the function.
    • I even think it might override another variable of the same name from outside the function, that doesn’t have the ‘global’ keyword.
    • I wonder is it possible to use the global keyword outside of a function, and if so, what is the reason.
    • I’d also like to know the reasoning for using the global keyword instead of delcaring the variable outside the function in the first place. \

    • A variable declared outside a function has a scope inside the function body, unless the function defines a variable with the same name.

  • If you finish a line of code with a backslash \, it will tell Python to continue the line of code in the next line of code. \ This is useful for dealing with longer lines of code and improving code readability.
  • When a function calls itself, this is called recursion.
    • Recursive functions need whats called a base case in it, which is the condition to stop the recursion. \ If the base case is left out, the user will receive a RecursionError exception: \ RecursionError: maximum recursion depth exceeded
    • These functions are used when calculating the factorial or a number.
    • Note: By definition, 0! = 1 \ And 1! = 1
    • Here is the recursive function to find the factorial of a number: \ def getFactorial(n): \ if n < : \ return -1 \ if n < 2 \ return 1 \ else: \ return n * getFactorial(n-1)
  • A sequence is data which can be scanned by the for loop.
  • A tuple is an immutable sequence type.

    • If you want to create a tuple, you need to enter a comma after it when declaring it. Eg.:
      • one_element_tuple_1 = (1, ) \ or \ one_element_tuple_2 = 1.,
    • Tuples prefer to use parenthese.
    • Whereas lists prefer to use brackets []
    • To repeat, tuples cannot be modified: \ my_tuple = (1, 10, 100, 1000)

      my_tuple.append(10000)
      del my_tuple[0]
      my_tuple[1] = -10
      
None of the above functions or instruction will work because they are trying to change the value of the tuple. 


```
AttributeError: 'tuple' object has no attribute 'append'
```
  • I learned about tuples at some stage a number of days ago. I though that a tuple was a pair of elements in a dictionary. Or a key value pair. Maybe I just didnt understand it at the time.
  • Individual elements within are tuple are not deletable, but you can delete a whole tuple like this: \ my_tuple = 1, 2, 3,
del my_tuple
  • There is also method used to create tuples: tuple(). You can use this to create a tuple from a list or dictionary. Eg. \ my_tuple = tuple(my_list) \ \You can also convert a tuple into a list with the list() method: \ my_list = list(my_tuple) \ \Theres also the dict() method to convert sequential into a dictionary.
  • A dictionary is a one-way tool.
  • Hanging indents is the phrase used for indenting lines of code such as elements of a dictionary for the purpose of readability. Eg. \ dictionary = {
          "cat": "chat",

          "dog": "chien",

          "horse": "cheval"


          }
  • Since dictionaries are not a sequence type, you cannot use a for loop on them directly. (Because they are not iterable). \ However, there is a method named keys() which can get around this. Eg. \ dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for key in dictionary.keys():
            print(key, "->", dictionary[key]
  • Whats happening above is the .keys() method is turning the keys of the dictionary into a list temporarily, so the for loop can iterate through it.
  • Similarly, there is the .values() method, which turns the values into an iterable list instead.
  • Before Python 3.6’s release in December 2016, dictionaries were unordered.
  • There is also a .copy() function, which creates a copy of the specified list and stores it in a new variable.
  • I need to install React onto the computer, for our work project. It looks as though I cannot install it by using Homebrew. I needs to be installed using npm.
  • Top-level packages are packages that are not dependencies of another.
  • I’m reading through this guide: https://medium.com/@akhilmaulloo/the-fremp-stack-building-a-full-stack-web-application-91308e505250 \ … and it mentions cors. We touched on cors in our program, but I can’t remember what it does.
    • ‘Collection’ seems to be word that keeps on coming up around MongoDB. I will understand it eventually.
    • ‘pymongo’ is mentioned. I guess it’s a tool to connect python with mongodb.
    • ‘yarn’ is mentioned, I know it its some sort of command line tool. But I don’t know what it does.