Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list from defined function
#1
Hey I'm trying to learn how to define a function.
As an example I made a "only want even number from a range" function

def even_numbers_only(a, b):
even_numbers_list = []
for i in range(a, b):
if (i % 2) == 0:
even_numbers_list.append(i)
return even_numbers_list

even = [even_numbers_only(1000, 3001)]
print(even)


But I don't understand how i can output a list from a defined function
Reply
#2
Please put your code in Python code tags, as you have been warned about that before. You can find help here.
Reply
#3
(Jan-05-2020, 10:02 AM)Coastal Wrote: Hey I'm trying to learn how to define a function.
As an example I made a "only want even number from a range" function

def even_numbers_only(a, b):
    even_numbers_list = []
    for i in range(a, b):
        if (i % 2) == 0:
            even_numbers_list.append(i)
        return even_numbers_list

even = [even_numbers_only(1000, 3001)]
print(even)
But I don't understand how i can output a list from a defined function

even = [even_numbers_only(1000, 3001)]
you have to remove the [ ]


even = even_numbers_only(1000, 3001)
You can directly print the output without added to a var
print(even_numbers_only(1000, 3001))
Reply
#4
Some suggestions:

- use meaningful parameter names
- take andvantage that True and False are integer subclass with respective values of 1, 0.

Some ideas:

def get_evens(start, stop):
    evens = []
    for i in range(start, stop+1):      # we need +1 if we want to include stop
        if not i % 2:
            evens.append(i)
    return evens
However, if one already writes functions then list comprehension should be covered topic. So this function can be written:

def get_evens(start, stop):
    return [i for i in range(start, stop+1) if not i % 2]
But if function works on continuous range of numbers one can just verify first number and then use range with step:

def get_evens(start, stop): 
    if start % 2: 
        return list(range(start+1, stop+1, 2)) 
    else: 
        return list(range(start, stop+1, 2)
One can observe that only difference between returns above is value of dividing by 2 and therefore we can write this function like:

def evens(start, stop): 
    return list(range(start+start % 2, stop+1, 2))
If function result is used to iterate over then there is no need to convert into list and range can be returned.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 511 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,161 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Getting NameError for a function that is defined JonWayn 2 1,056 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,773 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,235 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,588 May-25-2022, 07:22 PM
Last Post: Gribouillis
Sad Function defined by branches antoniogalante 1 1,820 Dec-16-2020, 11:35 PM
Last Post: deanhystad
  function call at defined system time? Holon 5 3,154 Oct-06-2020, 03:58 PM
Last Post: snippsat
  Function will not return variable that I think is defined Oldman45 6 3,435 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How do I find if a function has been defined? AndyHolyer 3 2,207 Jul-24-2020, 01:39 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020