I've started to learn python this morning, I've decided on 2.7 at the moment as the vast majority of stuff out there is written in that and i learn best by unpicking other peoples code to understand it and then be able to write my own.
First question is, generally, am i right to do that? Or bearing in mind i see this as a step to learning things like Java and some version of C, should i just learn python 3? My only experience in programming is VBA (and Basic on the Acorn Electron!)
I'm using http://codingbat.com/python to learn, as above i learn best by being set a task and then working out how to do it and this seems to have got me off to a flying start, are there any similar sites, as codingbat seems limited in how far it goes, i've completed 1/4 of the tasks in an hour or so.
And then a specific question regarding writing the code.
The example answers always do a check to make sure that a string is at least as long as any substring you try and extract from it, which i can see the logic of doing, but there doesn't appear to be a need to do as python seems to deal with it ok.
For example....
I wrote
Which works fine, if a string is less than 2 characters, the str[:::] function seems to return a 0 if it gets an error.
The example answer, however, had a check at the start to manually return 0 if the string length was less than 2.
Why? I know in something like this it makes no odds, but simplistically, those couple of lines aren't needed and just add overhead to the code? Or is it a case of what I've done in allowing python to catch and correct the error is bad practice?
Also, why do they assign the derived substring to the variable "sub" before checking it against the last 2 characters of the string? Surely as each time the code loops that variable is re-assigned and then only used once, it's more efficient to do it my way and not bother assigning the variable?
Thanks in advance..
First question is, generally, am i right to do that? Or bearing in mind i see this as a step to learning things like Java and some version of C, should i just learn python 3? My only experience in programming is VBA (and Basic on the Acorn Electron!)
I'm using http://codingbat.com/python to learn, as above i learn best by being set a task and then working out how to do it and this seems to have got me off to a flying start, are there any similar sites, as codingbat seems limited in how far it goes, i've completed 1/4 of the tasks in an hour or so.
And then a specific question regarding writing the code.
The example answers always do a check to make sure that a string is at least as long as any substring you try and extract from it, which i can see the logic of doing, but there doesn't appear to be a need to do as python seems to deal with it ok.
For example....
Given a string, return the count of the number of times that a substring length 2 appears in the string and also as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't count the end substring).
last2('hixxhi') → 1
last2('xaxxaxaxx') → 1
last2('axxxaaxx') → 2
I wrote
Code:
def last2(str):
result = 0
wsfind = str[-2:]
for i in range(len(str)-1):
if wsfind == str[i-1:i+1]:
result = result +1
return result
Which works fine, if a string is less than 2 characters, the str[:::] function seems to return a 0 if it gets an error.
The example answer, however, had a check at the start to manually return 0 if the string length was less than 2.
Code:
def last2(str):
# Screen out too-short string case.
if len(str) < 2:
return 0
# last 2 chars, can be written as str[-2:]
last2 = str[len(str)-2:]
count = 0
# Check each substring length 2 starting at i
for i in range(len(str)-2):
sub = str[i:i+2]
if sub == last2:
count = count + 1
return count
Why? I know in something like this it makes no odds, but simplistically, those couple of lines aren't needed and just add overhead to the code? Or is it a case of what I've done in allowing python to catch and correct the error is bad practice?
Also, why do they assign the derived substring to the variable "sub" before checking it against the last 2 characters of the string? Surely as each time the code loops that variable is re-assigned and then only used once, it's more efficient to do it my way and not bother assigning the variable?
Thanks in advance..