| | |
Simple code is confusing me
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
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?
OK, pretty simple. But here's the part that confuses me.
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.
•
•
•
•
# 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
•
•
•
•
# A list with one ending for each number from 1 to 31
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
+ ['st']
Thanks in advance, everyone. I'd be lost without you guys.
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 3
Python Syntax (Toggle Plain Text)
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 * ['th'] -- + 7 * ['th'], but the whole thing. Can someone please explain how and why this code works?
...
- String
- List
- Tuples
some of the sequence type operators. Below list is applicable to *all* types of sequence.
Python Syntax (Toggle Plain Text)
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']
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.
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
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
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.
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.
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.
•
•
Join Date: Apr 2008
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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.
"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.
![]() |
Similar Threads
- Updated : Simple ASP.Net Login Page (ASP.NET)
- How to represent a space and a tab? (C++)
- simple linked lists (C)
- Word Descrambler.... (C++)
- Linking 2 files. (C)
- Safe Menu's ? (HTML and CSS)
- help! I don't know what's wrong with my simple code (C)
- writing a simple cat program (C)
Other Threads in the Python Forum
- Previous Thread: Like function in Python
- Next Thread: Regarding "popen2" module
Views: 717 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt






