hey buddies, now I'm learning the 4 types of Python Scope. 👉🏾Do you know in python there are 4 types of python scope: Local Scope, enclosing, global, and built-in. As long as we learn together. we'll give clarification for each of them. Stay tuned with me. 👀
🔥2
What you have to know🤔 1. Local Scope: A variable defined inside a function is in the local scope of that function and can only be accessed within that function.
2. Global Scope: A variable defined outside of any function is in the global scope and can be accessed from anywhere in the code.
👉🏾here key point: global keyword: it allow you to modify a global variable inside a function
3. Enclosing Scope: A variable defined in an enclosing function is in the enclosing scope and can be accessed by nested function.
👉🏾here key point: nonlocal keyword: is used to work with variables inside functions, belongs to the outer func. it allows you to modify the outer function variable 4. Built-in scope: A variable defined in the built-in scope is a built-in function or constant that is always available in python.
Try to catch up python scope in your mind, not memorizing just you should understand. 😄 keep sharing your idea, question in the group discussion. Remember: Learning together makes Smart💪
x = 8#this's not local variable, cuz it's outside of functiondef my_func():
x = 8 # x is a local variable
print(x)
my_func()
2. Global Scope: A variable defined outside of any function is in the global scope and can be accessed from anywhere in the code.
y = "global variable" # y is a global variable
def my_func():
print(y) # accessing global variable inside a function
my_func()
print(y) # accessing global variable outside a function
👉🏾here key point: global keyword: it allow you to modify a global variable inside a function
def my_func():
global y # to modify the global variable
y = "global variable modified here"
my_func()
print(y)
3. Enclosing Scope: A variable defined in an enclosing function is in the enclosing scope and can be accessed by nested function.
def outer_func():
z = 4 # z is in the enclosing scope
def inner_func():
nonlocal z # to modify the enclosing variable
z += 1
print(z) # accessing enclosing variable inside nested function
inner_func()
outer_func()
👉🏾here key point: nonlocal keyword: is used to work with variables inside functions, belongs to the outer func. it allows you to modify the outer function variable 4. Built-in scope: A variable defined in the built-in scope is a built-in function or constant that is always available in python.
print(len("Hello")) # len is a built-in function print(max(3, 8, 2)) # max is a built-in function
Try to catch up python scope in your mind, not memorizing just you should understand. 😄 keep sharing your idea, question in the group discussion. Remember: Learning together makes Smart💪
👍1👏1
Think of it without using any resources, even Codeeditor🚫 What will be the output of the following code?
give your answer in the comment section↩️
1 what will be the output of the following code?
x = 2
def my_func():
x = 7
my_func()
print(x)
give your answer in the comment section↩️
🥰1
SamiTech Code pinned «What you have to know🤔 1. Local Scope: A variable defined inside a function is in the local scope of that function and can only be accessed within that…»
Take a look:👇 Python Decorators:
A decorator is a function that take another function as an argument
and extends the behaviour of the latter function without explicitly modifying it.
#basic decorator: @decorator_name apply above the function
🤔Think of it: defined function changecase as a super class then greet() function is inherited the property of the super class applying @changecase above the sub-functions(the inherited function), we don't have to write the logic just only the return value. However the logic is defined inside of the changecase, return type is upper, so our out will be uppercase text.
A decorator is a function that take another function as an argument
and extends the behaviour of the latter function without explicitly modifying it.
#basic decorator: @decorator_name apply above the function
def changecase(func):
def myinner():
return func().upper()
return myinner
@changecase
def greet():
return "hello from a function"
print(greet())
🤔Think of it: defined function changecase as a super class then greet() function is inherited the property of the super class applying @changecase above the sub-functions(the inherited function), we don't have to write the logic just only the return value. However the logic is defined inside of the changecase, return type is upper, so our out will be uppercase text.
Good morning guys
Today, I'm about to back to campus.
I wish the best for all of us.
Just we'll keep learn together..💪
Today, I'm about to back to campus.
I wish the best for all of us.
Just we'll keep learn together..💪
🔥1
GM everyones, Cuz of connection problem, I didn't post here whatever, today morning we'll gonna continue our learning journey...Thanks👍
Python Lambda Functions:
A lambda function can take any number of arguments, but can only have one expression.
)
NB: why we use lambda functions? 👀
Use lambda functions when an anonymous function is required for a short period of time. Keep in mind about python lambda functions, if you've any questions regarding to this you're welcome🫡
A lambda function can take any number of arguments, but can only have one expression.
#syntax: lambda arguments : expression A lambda function that adds two numbers:
add = lambda x, y: x + y
print(add(4, 9))
A lambda function that multiply two numbers:
multiply = lambda a, b: a * b
print(multiply(3, 8)
)
Use lambda functions when an anonymous function is required for a short period of time. Keep in mind about python lambda functions, if you've any questions regarding to this you're welcome🫡
Good Afternoon guys I hope you're doing well and this's our topic we'll focus on it Python Recursion & Fibonacci Sequence. Up to that keep shining
👍1
🤔Have you heard about recursive? I hope you learned that in math Python recursion:
it's when a function calls itself.
A simple recursive function that counts down from 5:
"""every recursive function must have two parts:
1. Base case - a condition that stops the recursion
2. Recursive case - the function calling itself with a modified argument
here without a base case, the function would call itself forever, causing a stack overflow error🥺"""
it's when a function calls itself.
A simple recursive function that counts down from 5:
def countdown(n):
if n <= 0:
print("Done!")
else:
print(n)
countdown(n - 1)
countdown(5)
"""every recursive function must have two parts:
1. Base case - a condition that stops the recursion
2. Recursive case - the function calling itself with a modified argument
here without a base case, the function would call itself forever, causing a stack overflow error🥺"""
def factorial(n):
#Base case
if n == 0 or n == 1:
return 1
#Recursive case
else:
return n * factorial(n - 1)
print(factorial(5))
Fibonacci Sequence:
is a classic example where each number is the sum of the two preceding ones.
The Sequence starts with 0 and 1
eg; 0, 1, 1, 2, 3, 5, 8, 13,... here we can use recursion to find a specific number in the sequence: means 0 + 1 = 1 then 1 + 1 = 2,...
Find the 7th number in the fibonacci sequence:
Understand Fibonacci then we'll move to the next step, we're now gained basic knowledge of python programming.😀💪
is a classic example where each number is the sum of the two preceding ones.
The Sequence starts with 0 and 1
eg; 0, 1, 1, 2, 3, 5, 8, 13,... here we can use recursion to find a specific number in the sequence: means 0 + 1 = 1 then 1 + 1 = 2,...
Find the 7th number in the fibonacci sequence:
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(7))
Understand Fibonacci then we'll move to the next step, we're now gained basic knowledge of python programming.😀💪
SamiTech Code pinned «🤔Have you heard about recursive? I hope you learned that in math Python recursion: it's when a function calls itself. A simple recursive function that counts down from 5: def countdown(n): if n <= 0: print("Done!")…»
Good Morning guys💫, Since we covered basic python programming, I think it's good to start 👇
What do you think please give your thought in the comment section.✍️✍️✍️
Data stracture & Algorithm.
What do you think please give your thought in the comment section.✍️✍️✍️
Phase 1 – Foundation📊 1.Time and Space Complexity: What we have to know?👇 -What is Time Complexity? -What is Space Complexity? -Why we need Complexity? -Big-O Notation -Common types of time complexity -How to understand each time complexity? -Focus on interview questions -Practice problem so guys since, we've started the 2nd phase DSA, let's prepare ourselves for our journey and stay focus on it we'll become smart💪
❤1
As a note👇
What is Time Complexity?
Time Complexity tells us:
How the running time of an algorithm grows as input size increases. NB: We don't measure time in seconds.
We measure number of operations.
What is Space Complexity?
Space Complexity tells us:
How much extra memory an algorithm uses as input size increases.
Why we need Complexity?
Imagine two programs solving the same problem:
Program A → works fast for small input but slow for large input
Program B → works fast even for large input
👉 Complexity helps us choose the better one.
👍2