DEV Community

lordronjuyal
lordronjuyal

Posted on

day 08

date: 27 May, 2024.

Scope -- Everything we create of define in code is called namespace eg variables, functions, lists, etc. This namespace can be used or accessed in a region of code. This region is called scope of that namespace.
We have two scopes:

  1. Global scope -- This is scope of top-level code(top level means it's not inside any functions). We can access it anywhere in the code, but we can't change its value inside a function. This scope is good for constant variables.
  2. Local scope -- namespace inside a function has local scope. We can only access this namescape inside the function not outside of it. Now if we use same name inside a global scope and local scope, both will create two different namespaces. If we want to access and change a global namespace inside local, we have to use-- global name_of_namespace. Now we can use and change its value also, though doing this is not a good practice.

Other things I learned today-->

  1. using _ when looping over and not using the item.

  2. list1=[a,b]
    list2=[1,2]
    list1.expand(list2) >> list1 = [a,b,1,2]
    list1 += list2 #will give same result
    .append is used for adding single item

  3. We should all capital letters for a variable having contact value, eg PI= 3.14. This is used as the standard reference, it won't give any error if we deviate.

  4. to import multiple functions from module:
    from module import f1 , f2

Programs I created:-

  1. Guess the number
    https://replit.com/@rohitrj332024/guess-the-number-day-8#main.py

  2. Guess higher or lower followers(Instagram)
    https://replit.com/@rohitrj332024/higher-lower-start-day8


Personal -- I am happy that I am able to solve these problems. They are taking time, and I am also following proper procedures for solving them, like writing steps to do (breaking the problem into small parts), and making a flow chart( I make them on paper right now). I hope I will be able to build a good habit and it will help me when solving bigger projects later on.

Top comments (0)