Skip to content

Forum in maintenance, we will back soon 🙂

Notifications
Clear all

python fuctions.

6 Posts
3 Users
1 Reactions
34 Views
(@jay-kim)
Posts: 35
Trusted Member Customer
Topic starter
 

Defining a Function 📝

Here’s how you define a function:

def function_name():
# do something

Let’s define a function named greet That prints a greeting:

def greet():
print("Hello, Python learner! 🚀")

Calling a Function 📞

After defining a function, you can call it by its name followed by parentheses. Let’s call our greet function:

greet() # Output: Hello, Python learner! 🚀

Functions with Parameters 📦

Sometimes, we want our function to perform an operation on a variable. We can achieve this by defining a function with the parameters:

def greet(name):
print(f"Hello, {name}! 🚀")

Now, when we call the greet Function, we need to provide a name:

greet("Alice") # Output: Hello, Alice! 🚀

Functions that Return Values 🎁

Sometimes, we want our function to give us back a result that we can use later. For this, we use the return statement:

def square(number):
return number ** 2

When we call this function with a number, it gives us the square of that number:

result = square(5)
print(result) # Output: 25

Exercise 🏋️‍♀️: Now, your task is to create a function called calculate_average that takes a list of numbers as an argument and returns their average. Test your function with different lists of numbers to ensure it works correctly.

Here’s a skeleton to get you started:

defcalculate_average(numbers):
# Calculate the sum of the numbers
# Divide the sum by the length of the numbers list to get the average
# Return the average
 
numbers = [1, 2, 3, 4, 5]
print(calculate_average(numbers))# This should print the average of the numbers in the list
 
 
 
 
 
you wrote this thing under the video. I can't understand anything. 
 
how come you put different thing between your video and the sentenses.
 
don't wanna give up and want to keep going but not sure why I take this course.. nothing on my head about this.
 
Do I give up?
 
 
Posted : 07/11/2023 12:24 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

Hi Jay, the reason I put different examples is to help you understand the concept instead of copying the same code.

I understand that you are not familiar with coding. But if you are looking to learn some skills that really helps you in the future, you should not give up. but I can't oblige you to.

am here to help and explain anything.

 

Did you understand the concept behind "Functions" in general?

do you know what is a function? and how to declare and call it.

 
Posted : 07/12/2023 10:57 am
(@jay-kim)
Posts: 35
Trusted Member Customer
Topic starter
 

@admin

 

no I don't think I got it. 

I also think it'll help me for some future. and I never thought this could be easy.

but to be honest, no clue what you're saying..

feel like I'm a super silly..

 
Posted : 07/12/2023 12:48 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

My humble opinion. go again to the first lecture in the python course. and go one by one, and see exactly you are lost, so I can help there. because all concepts are connected and related. 

for example; did you understand what is a variable? and what is an operation?

 
Posted : 07/13/2023 10:48 am
(@jay-kim)
Posts: 35
Trusted Member Customer
Topic starter
 

@admin okay thanks for your advice. I'll not give up. so hope you don't give up me as well 🙂

 

 
Posted : 07/14/2023 12:58 pm
(@kunal-lonhare)
Posts: 28
Eminent Member Customer
 

@jay-kim Hey brother I am also new can we connect on WhatsApp and help each other wherever we are stuck? Because waiting for Hasan sir on the forum may take time. What do you say? My WhatsApp number is +91 7000870010.

 
Posted : 07/17/2023 8:02 am
Share:
[the_ad_group id="312"]