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
Sad Function defined by branches antoniogalante 1 547 Dec-16-2020, 11:35 PM
Last Post: deanhystad
  function call at defined system time? Holon 5 1,188 Oct-06-2020, 03:58 PM
Last Post: snippsat
  Function will not return variable that I think is defined Oldman45 6 1,388 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How do I find if a function has been defined? AndyHolyer 3 876 Jul-24-2020, 01:39 PM
Last Post: Gribouillis
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 817 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  Name 'function' not defined, although it is... dougDimmadome_ 2 2,810 Jun-23-2020, 03:25 PM
Last Post: dougDimmadome_
  python library not defined in user defined function johnEmScott 2 1,000 May-30-2020, 04:14 AM
Last Post: DT2000
  Problem with user defined main menu function stefzeer 3 1,010 Mar-27-2020, 06:12 AM
Last Post: buran
  Getting the error like function not defined .. suggest correct code raghava 1 929 Feb-04-2020, 11:20 PM
Last Post: micseydel
  User Defined function not working Raj_Kumar 4 1,198 Dec-17-2019, 12:44 PM
Last Post: buran

Forum Jump:

User Panel Messages

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