I recently bought a book on Python, and I'm working my way through it. However, I've come to a very simple program, but I can't understand a certain part of the code. Can someone explain?

# Print out a date, given year, month, and day as numbers
months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]

# A list with one ending for each number from 1 to 31
endings = + 17 * \
+ + 7 * \
+

year = raw_input('Year: ')
month = raw_input('Month (1-12): ')
day = raw_input('Day (1-31): ')
month_number = int(month)
day_number = int(day)

# Remember to subtract 1 from month and day to get a correct index
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print month_name + ' ' + ordinal + ', ' + year

OK, pretty simple. But here's the part that confuses me.

# A list with one ending for each number from 1 to 31
endings = + 17 * \
+ + 7 * \
+

Ok, I understand what it does. I just don't understand why. It just adds the ending to the month. September 11th, etc. But what's with the + 17 multiplied by 'th' -- + 7 multiplied by 'th'? This is really driving me nuts, and I know the answer is right in front of me, which makes it even worse. Not just the + 17 * -- + 7 * , but the whole thing. Can someone please explain how and why this code works?

Thanks in advance, everyone. I'd be lost without you guys.

NAES_1 commented: I was also confused with this part. I am also learning from the same book but confused at this part (ending=[])) +0

Recommended Answers

All 6 Replies

April 1st, 2008
April 2nd, 2008
April 3rd, 2008
01:  April 4th, 2008
02:  April 5th, 2008
03:  April 6th, 2008
04:  April 7th, 2008
05:  April 8th, 2008
06:  April 9th, 2008
07:  April 10th, 2008
08:  April 11th, 2008
09:  April 12th, 2008
10:  April 13th, 2008
11:  April 14th, 2008
12:  April 15th, 2008
13:  April 16th, 2008
14:  April 17th, 2008
15:  April 18th, 2008
16:  April 19th, 2008
17:  April 20th, 2008
April 21st, 2008
April 22nd, 2008
April 23rd, 2008
01:  April 24th, 2008
02:  April 25th, 2008
03:  April 26th, 2008
04:  April 27th, 2008
05:  April 28th, 2008
06:  April 29th, 2008
07:  April 30th, 2008
April 31st, 2008

The 17 takes care of the first 17 consecutive 'th' ocurrances & the 7 takes care of the next series.

HTH

..
Ok, I understand what it does. I just don't understand why. It just adds the ending to the month. September 11th, etc. But what's with the + 17 multiplied by 'th' -- + 7 multiplied by 'th'? This is really driving me nuts, and I know the answer is right in front of me, which makes it even worse. Not just the + 17 * -- + 7 * , but the whole thing. Can someone please explain how and why this code works?
...

Types of sequence in Python
- String
- List
- Tuples

some of the sequence type operators. Below list is applicable to *all* types of sequence.

operator   usage			function
  +         sq1 + sq2			 concatenates to sequences
  * 	    exp * sq2			 repeat sequence exp time
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']

is a single line statement spanning multiline. \ is used to indicate the same.


breaking above expression,

>>> endings = ['st', 'nd', 'rd']
+ ['th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th']
+ ['st', 'nd', 'rd']
+ ['th', 'th', 'th', 'th', 'th', 'th', 'th']
+ ['st']
>>> endings
['st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th', 'th', 'st']
>>> [B]type(endings)
<type 'list'>[/B]

hope you understand.
katharnakh.

Thanks a lot everyone. This cleared it up perfectly. I think I was confusing myself trying to associate the code with the month. I should have known the month was irrelevant. What I should have been focusing on was the days. The multiplying of strings kind of threw me.

But after reading these well delivered answers, it makes perfect sense. As is expected, the code is simply executed in order (obviously). So another way it could be expressed is to forget the month altogether for a moment, and just focus on 1 through 31.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

So it could be explained like this. It starts with assigning the months, 'January through December', into the variable "months". The next sequence which confused me, simply sets up another variable with the days 1 through 31, each one named appropriately. Starting with 'first, second, and third', then followed by 17 'th's (fourth, fifth, sixth, seventh, and so on). then another second, third, and fourth, then seven more 'th's for the twenty fourth through the thirtieth, and finally ending on the last 'st' for the 31st.

Beautiful. Thanks a lot, you guys

When you see code like this, sometimes it's enlightning to print out the variable for test. Yes, you can multiply lists.

Okay dude, Im not actually here to help, but I have a friendly suggestion:

don't ever say simple python code confuses you if you'd like to be taken seriously. I came in here expecting to read a stupid person's rant. Of course, your problem wasn't being confused by simple python code, just being thrown off by one of the language features.

By the way, I hope you enjoy python as much as I do.

Happy coding.

Okay dude, Im not actually here to help, but I have a friendly suggestion:

don't ever say simple python code confuses you if you'd like to be taken seriously. I came in here expecting to read a stupid person's rant. Of course, your problem wasn't being confused by simple python code, just being thrown off by one of the language features.

By the way, I hope you enjoy python as much as I do.

Happy coding.

That makes sense. Keep in mind, though, I'm totally new to this stuff. I doubt I'd be taken very seriously right now, anyway. Sure, I am a pretty quick learner, and I love everything about computers. But I respect anyone smarter than I am. Humility isn't something that bothers me. Compared to the majority of folks in here, this was a stupid question. There wasn't really a way to hide that. So I figured it was only fair to be upfront and honest, rather than to try and make lame excuses for my confusion.

"I'm tired." | "I lost my glasses." | "My contacts fell out." | "I can't turn on my other computer that has all my notes." | "The dog ate ate my power supply again."

If I did something like that, people really wouldn't take me seriously. However, you do have a point. I probably should have been more descriptive with my title. In the future I'll choose my words more carefully.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.