Simple code is confusing me

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 12
Reputation: sanoski is an unknown quantity at this point 
Solved Threads: 0
sanoski sanoski is offline Offline
Newbie Poster

Simple code is confusing me

 
0
  #1
Apr 24th, 2008
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 = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']

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 = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']
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 * ['th'] -- + 7 * ['th'], 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 17
Reputation: rikxik is an unknown quantity at this point 
Solved Threads: 3
rikxik rikxik is offline Offline
Newbie Poster

Re: Simple code is confusing me

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

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

HTH
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 237
Reputation: katharnakh is an unknown quantity at this point 
Solved Threads: 33
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Simple code is confusing me

 
0
  #3
Apr 24th, 2008
Originally Posted by sanoski View Post
..
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 * ['th'] -- + 7 * ['th'], 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.
  1. operator usage function
  2. + sq1 + sq2 concatenates to sequences
  3. * 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']
>>> type(endings)
<type 'list'>


hope you understand.
katharnakh.
Last edited by katharnakh; Apr 24th, 2008 at 2:13 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: sanoski is an unknown quantity at this point 
Solved Threads: 0
sanoski sanoski is offline Offline
Newbie Poster

Re: Simple code is confusing me

 
0
  #4
Apr 24th, 2008
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
Last edited by sanoski; Apr 24th, 2008 at 5:22 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Simple code is confusing me

 
0
  #5
Apr 24th, 2008
When you see code like this, sometimes it's enlightning to print out the variable for test. Yes, you can multiply lists.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Simple code is confusing me

 
0
  #6
Apr 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: sanoski is an unknown quantity at this point 
Solved Threads: 0
sanoski sanoski is offline Offline
Newbie Poster

Re: Simple code is confusing me

 
0
  #7
Apr 24th, 2008
Originally Posted by scru View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 717 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC