Python: Get list of previous 12 months from number int

If I have a month integer like 6, how can I get the names of the previous 12 months? So passing in 6 would give me a list like

m =['January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December']
pos = 6 # current month is June
for i in range(pos,pos-12,-1):
    print(m[i-1]) # in real m[pos] == July . so m[pos-1] == June

8.2. calendar — General calendar-related functions, as the first day of the week, and Sunday as the last (the European convention). Creates a Calendar object. firstweekday is an integer specifying the first day of the week. Return an iterator for the month month (1–12) in the year year. This iterator will return all days (as objects) for the month and all days  C:\programs\time>pep8 --first C:\programs\time>python Month name from number 5: Short Name: Jan Full Name: January List of all months from calendar

You can use itertools.cycle, skipping the x previous and returning the 12 next from a reversed months list:

import itertools

def get_month_from_int(i):
  reverse_months = itertools.cycle([
  'December','November','October','September','August','July', 'June','May','April', 'March','February','January'
  for _ in range(i):
  return [next(reverse_months) for _ in range(12)]

Here you have the live example

calendar — General calendar-related functions, Creates a Calendar object. firstweekday is an integer specifying the first day of the week. Return an iterator for the month month (1–12) in the year year. This iterator will return all days (as objects) for the month and all days before the The CSS class for a weekday occurring in the previous or coming month. The name of the month will be translated to you locale configuration (according to python documentation, you can take a look there to replace that with, for example, the abbreviated name). This is a perfect list to be used as the default list of options with a django ChoiceField or MultipleChoiceField form field ;D.

I know it has been answered but I think there a moret pythonic way using calendar module

import calendar

current = 11
last_12_months = [calendar.month_name[1:][i] for i in range(current - 12, current)] # starting from the last
reverse_last_12_months = reversed(last_12_months) # starting from the current

Python: Print the calendar of a given month and year, Python Exercises, Practice and Solution: Write a Python program to 'l' specifies the number of lines that each week will use. import calendar y = int(input("Input the year : ")) m = int(input("Input the month : ")) print(calendar.month(y, 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30. %f Microsecond as a decimal number [0,999999], zero-padded on the left (1) %H Hour (24-hour clock) as a decimal number [00,23]. %I Hour (12-hour clock) as a decimal number [01,12]. %j Day of the year as a decimal number [001,366]. %m Month as a decimal number [01,12]. %M Minute as a decimal number [00,59].

Python Exercise: Get next day of a given date, Last update on February 26 2020 08:09:22 (UTC/GMT +8 hours). Python Conditional: Exercise - 41 with Solution. Write a Python program to get next day of a given date. year = int(input("Input a year: ")) if (year % 400 == 0): leap_year Input a year: 1974 Input a month [1-12]: 2 Input a day [1-31]: 15 The  date1.month - date2.month returns -2; 12*(date1.year - date2.year) returns 12-2 + 12 = 10; Important Point Suppose you want to calculate the number of months between 31/10/2018 and 01/11/2018, the above suggested method will return 1 as two dates lie in a month difference. You may find it incorrect as number of days in between two dates is 1.

Python Datetime Tutorial: Manipulate Times, Dates, and Time Spans, Become a master of times and dates in Python as you work with the datetime and Creating Date Objects; Getting year and month from the date; Getting month day and Also, we may not want all of this date and time data — if we're doing Datetime will give us the day of the week as a number using its  Write a Python Program to find the Last Digit in a Number with a practical example. This python program allows the user to enter any integer value. Next, Python is going to find the Last Digit of the user-entered value. This Python last digit in a number program is the same as above. But this time, we separated the logic and placed it in a

ADD_MONTHS function - Amazon Redshift, ADD_MONTHS( {date | timestamp}, integer) The ADD_MONTHS function adds 12 months to each value from the Previous topic: Date and time functions. The python function randint can be used to generate a random integer in a chosen interval [a,b]: >>> import random >>> random.randint(0,10) 7 >>> random.randint(0,10) 0 A list of random numbers can be then created using python list comprehension approach:

  • Did you try anything? Let us see some code!
  • lists[n-1::-1] + lists[-1:n-1:-1]
  • Looks like you should be able to start with a list of the months in reverse, and then rotate it by the correct number.