Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list of odd numbers
#1
I need to write a function that generates a list of n odd numbers, starting at 1. For example, if the input is 12, the output would be
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]. I just started to learn python like 3 days ago and I have no idea where to start.

I was able to solve the similar question:

"Write a function that generates the first n natural numbers, starting at 1. The natural numbers are 1, 2, 3, 4, 5, ... You will need to create an empty list, add the numbers one by one, and return the list." with below code

def natural(n):
    L = list(range(n+1))
    L = [num for num in L if num] 
    return L


I think the solution for this problem would be similar to this code, but I have no idea where to fix.
Reply


Messages In This Thread
list of odd numbers - by cffiver2 - Jul-12-2019, 08:52 AM
RE: list of odd numbers - by DeaD_EyE - Jul-12-2019, 09:15 AM
RE: list of odd numbers - by perfringo - Jul-12-2019, 09:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,427 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 3,012 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Return the sum of the first n numbers in the list. pav1983 3 4,108 Jun-24-2020, 03:37 AM
Last Post: deanhystad
  numbers in list - max value and average stewie 2 2,829 Apr-01-2020, 06:25 PM
Last Post: buran
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,730 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,296 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,110 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  List with Random Numbers AnjyilLee 5 9,371 Oct-14-2017, 09:22 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