SamiTech Code pinned «Hey What I've learned today....🤔.👇 #Python function arguments: A function can have arguments, which are values you can pass to the function when…»
GM friends🌅
Are you study well with me? If you're, nice keep it up don't stop. For what we do, we have to have discipline. Keep going your today challenge's your tommorow success.👌
Stay focused, with our Machine Learning journey💪 Ask question, answer for the question posted in this channel. Don't leave it out to practice you may got something usefull.
And pls share this channel for your friends you may know, Cuz we've to grow, learn, share ideas together...👈 Thanks🙏
Are you study well with me? If you're, nice keep it up don't stop. For what we do, we have to have discipline. Keep going your today challenge's your tommorow success.👌
Stay focused, with our Machine Learning journey💪 Ask question, answer for the question posted in this channel. Don't leave it out to practice you may got something usefull.
And pls share this channel for your friends you may know, Cuz we've to grow, learn, share ideas together...👈 Thanks🙏
Q2.What is the correct keyword for defining functions in Python?
Anonymous Quiz
17%
function
83%
def
0%
defined
👍1
Hello family HRU all? how studying is going on just right now I've started Arbitrary keyword Arguments **kwargs...keep studying with me. Yup
Why we use **kwargs(**keyword arguments)? Cuz, it allows us to handle a variable number of keyword arguments, which can be useful when we want to create flexible
function that can accept different types of input without having to define a fixed number of parameters. here code eg;👇
city & country isn't passed as a key but since there is ** before the kwargs it can accept it. key point to understand: **kwargs collects all keyword arguments, Stored as a dictionary, Python converts this to:👇
function that can accept different types of input without having to define a fixed number of parameters. here code eg;👇
def my_func(**kwargs):
print("Type", type(kwargs))
print("Name: ", kwargs["name"])
print("Age: ", kwargs["age"])
print("All data: ", kwargs)
my_func(name = "Alice", age = 21, city = "New York", country = "USA")
city & country isn't passed as a key but since there is ** before the kwargs it can accept it. key point to understand: **kwargs collects all keyword arguments, Stored as a dictionary, Python converts this to:👇
kwargs = {
"name": "Alice",
"age": 21,
"city": "New York",
"country": "USA"
}
👏1
And now I've learned ... Combining *args and **kwargs
Before that, what is d/t between *args and **kwargs?
*args is used to pass a variable number of non-keyword arguments to a function,
while **kwargs is used to pass a variable number of keyword arguments to a function.
Send the output of this👆inside of the comment section, share for us what you understand.👌
Before that, what is d/t between *args and **kwargs?
*args is used to pass a variable number of non-keyword arguments to a function,
while **kwargs is used to pass a variable number of keyword arguments to a function.
def student_info(student_id, *courses, **details):
print("Student ID: ", student_id)
print("Courses: ", courses)
print("Details: ", details)
student_info(101, "Math", "Cs", "IT", name = "Bob", age = 20, city = "AA")
Send the output of this👆inside of the comment section, share for us what you understand.👌
🔥1
Q3: keyword arguments are often called? Q4: If you do not know the number of arguments that will be passed into your function, there is a prefix you can add in the function definition, which prefix?👇
And don't forget to push your code to the github🫡
def my_function(_kids): # here'_' fill it?
print("The youngest child is " + kids[2])
And don't forget to push your code to the github🫡
🔥1
SamiTech Code pinned «Why we use **kwargs(**keyword arguments)? Cuz, it allows us to handle a variable number of keyword arguments, which can be useful when we want to create flexible function that can…»
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