Python Advice / Tips please

Soldato
Joined
19 Mar 2012
Posts
6,586
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....

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..
 
Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
If I were to learn Python now it would be v3, and just try to be aware of the differences in the earlier versions.

As for the code both are fine, their version is probably more "clean" (easier for future developers to understand what's going on)
 
Caporegime
Joined
18 Oct 2002
Posts
32,623
unfortunately that is one of the downsides of python, there is still no clear movement towards v3 AFAIK. For learning purposes it doesn't matter though.

Also, IMO if you want to learn java or c# learn java or c#, or better yet C or C++ as a general foundation. Python is easy to learn but has some peculiarities so it can be hard to unlearn the idiosyncrasies. Plus you will learn a certain mindset of how to encode problems and program solutions without learning the important underpinnings. IMO it is much more important to understand the fundamentals and how data structures and algorithms work rather than treating these as black boxes and plugging together the pieces.

I love python, it is easy, it is good for beginners, I just don't think it is good if you want to be a good developer. We have interviewed many people with a python background and have never really been impressed with the code they write in lower languages or their understanding of memory, data structures, algorithms, performance, tradeoffs. We are much more likely to hire a mathematician who has never seen a line of code in their life, give them a couple of books and some supervision and get a great developer after a couple of months.
 
Caporegime
Joined
18 Oct 2002
Posts
32,623
^ Thread hijack, what do you do D.P. I'm always interested in how low level your understanding of programming seems to be?

I develop software for startup, a lot of machine learning, optimization, search, signal processing, GIS, etc. but also making some client facing servers and smartphone apps.

I am not meaning low level machine code here, just fundamentals of how algorithms and data structures actually work instead of treating them like a black box. Personally I find it shocking when we interview a developer who doesn't know the differences/performances/tradeoffs/advantages/functionality between hash table, heap, linked list, binary tree etc., let alone being able to code a tree or sorting algorithm for example.

Now I do love python and I know why it is popular for beginners, i just don't think it is a good way to go if you want to really progress and become a good developer in a different language.
The way I see it is like having a doctor that simply sees some symptoms and prescribes a drug without understand what the illness is, why the symptoms are there, what other tests might be helpful, how the drug works. Someone who has no understanding of medicine shouldn't be treating people. It is also counter-intuitive to learn something high level without first learning the low level stuff. In everything we do in life we first learn the basics and build up from their, you don't learn maths at Stephen hawking level without first learning to add and subtract.
 
Last edited:
Soldato
OP
Joined
19 Mar 2012
Posts
6,586
Cracking stuff thanks, mate, I'm flying through it now, it can seem a bit slow when you know about assigning variables and boolean stuff but in actual fact it's really hammering home the syntax changes coming from VBA.

edit:I hadn't seen the other replies, they've given me food for thought! D.P, do you have any suggestions for how to get started on C or C++?

I wanted to learn a "simple" language first to get some good working practises, my entire knowledge of VBA is self taught and very haphazard, I'm sure i do things "incorrectly" even though they work.
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
32,623
Cracking stuff thanks, mate, I'm flying through it now, it can seem a bit slow when you know about assigning variables and boolean stuff but in actual fact it's really hammering home the syntax changes coming from VBA.

edit:I hadn't seen the other replies, they've given me food for thought! D.P, do you have any suggestions for how to get started on C or C++?

I wanted to learn a "simple" language first to get some good working practises, my entire knowledge of VBA is self taught and very haphazard, I'm sure i do things "incorrectly" even though they work.

We give our new hires with no previous programming experience a few books:

Programming: Principles and Practice Using C++ (2nd Edition) Paperback – June 2, 2014 by Bjarne Stroustrup


C++ Primer (5th Edition) Paperback
by Stanley B. Lippman

Accelerated C++: Practical Programming by Example Paperback
by Andrew Koenig


Accelerated C++ is concise and good for those who know basic concepts of programming, you don't get bogged down. The other 2 are excellent but quite lengthy, taking you form no knowledge of programming to being a solid c++ developer. A mix of these books, some encouragement and supervision, and help form the internet is mostly what is needed.
 
Soldato
OP
Joined
19 Mar 2012
Posts
6,586
Would you say that I'm best to use the Koenig book to get started and then learn the newer stuff? People online talk about versions of C++ and that "Accelerated C++" is based on an old version? But then the book does get good reviews.
 
Caporegime
Joined
18 Oct 2002
Posts
32,623
Would you say that I'm best to use the Koenig book to get started and then learn the newer stuff? People online talk about versions of C++ and that "Accelerated C++" is based on an old version? But then the book does get good reviews.

The accelerated book is good but it doesn't hold your hand or teach you the basics much, e.g. it assumes you know about iteration so will teach you a for loop in C++.

c++11 has added many new and useful concepts but they don't make anything in taught from the older lessons obsolete.
e.g. in c++11 you have a foreach concept:
Code:
for (auto item : container)
{
 // do something with item
}
while previously you would have to define the iterator which is more code.
Code:
for (vector<int>::iterator it = container.begin() ; it != container.end(); ++it)
 {
 // it is a pointer to the next item in the container.
}
}

This still works fine, c++11 will just rewrite the c++11 code into something that looks like the C++98 code. The more verbose c++98 version allows greater flexibility.
 
Last edited:
Soldato
OP
Joined
19 Mar 2012
Posts
6,586
Cool thanks.

I've got an idea about stuff, I seem to have the brain for programming.

In VBA I've learnt to use For Loops, array variables, call other procedures, build Excel based applications using Forms, etc so I'm not completely fresh to a lot of the concepts.

I even remember, vividly. sitting at a mates house when i was about 10/11 learning about Booleans, AND, OR, NOT, etc and "getting it".
 
Caporegime
Joined
18 Oct 2002
Posts
32,623
The accelerated book might work for then.

What you could do is play around with python for a while but don't get too entrenched in the python way of doing things. Then start C++ and try to learn the same basics. I would then start looking for simple algorithms or data-structures try to code. We get our interns to do things like spell checking, sorting, make a binary tree, something with recursion (Fibonacci)
 
Soldato
OP
Joined
19 Mar 2012
Posts
6,586
Cheers again mate.

Messing on with Python I'd managed the pigeon English translator no problem at all within a couple of hours of starting to install Python.

I've ordered the Accelerated book and one of the others, the third I got a Kindle sample to have a little read.

I'm pretty committed to this, it's something I've always wanted to do properly but never managed to. Upcoming redundancy and having a child means I should have time to get cracking with C++.

Any exercises or more detail on the sort of thing you get your interns to do that you can share would be great.
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
32,623
A spell checker is a great early algorithm once you know the fundamentals:

We normally give them this wiki page and ask for an efficient spell checker for long strings
http://en.wikipedia.org/wiki/Levenshtein_distance
There is loads of example code out there so it is a case of researching the problem, thing of the solutions (data-structures, algorithm) and then coding it up.

There are lots of interesting decisions to make, e.g. how to represent the lexicon/dictionary.

There are loads of simpler problems to start with.

Project euler is a great palce to learn:
https://projecteuler.net/problems

pick some problems to solve. Great fun really.
 
Last edited:
Soldato
OP
Joined
19 Mar 2012
Posts
6,586
So the book arrived this morning, got cracking with it, read the first 3 chapters and then knocked this up without reference to the book or internet.

It calculates the sum of all factors of 3 or 5 that are less than 1000.

I can already see things i want to do with it, such as take the 2 numbers in as an input and then work from there, adding in an array and sorting it to make sure that they are in the right order for the code to still work.

I'd also like to build it so that no matter how many numbers you enter it works out the sum of the factors, add something in there to avoid any checking of the second number if it's a factor of the other, etc, etc.

But anyway, the question i had is....i deliberately did it this way to avoid looping through every number from 1-999, but i'm not sure if this way is more efficient or if another way would be. is there a way to test 2 bits of code that do the same thing to see which is more efficient? Or is that something that comes with experience and a bit more learning?

Code:
#include <iostream>

int main()	{

	//mul1 and mul2 must be entered in ascending order
	int mul1 = 3;
	int mul2 = 5;
	int lim = 1000;
	int count = 1;
	int wsum = 0;

	while ((count * mul1) < lim) {
		wsum += (count * mul1);

		if ((count * mul2) % mul1 != 0 && (count * mul2) < lim) {
			wsum += (count * mul2);
		}
		++count;
	}
	std::cout << wsum << std::endl;
	return 0;
}

edit:

In an attempt to further reduce the looping, which I'm assuming takes processing power that's not needed, I've done most of the work using maths up front and then used a loop to remove the duplicate numbers.

Code:
#include <iostream>

using std::cout;	using std::endl;
using std::cin;

int main()	{
	
	int mul1 = 3;
	int mul2 = 5;
	int count = 1;
	int wsum = 0;
	

	cout << "Enter Limit : ";
	int lim;
	cin >> lim;
	--lim;

	int mul1d = lim / mul1;
	cout << mul1d << endl;
	
	if (mul1d % 2 == 0)	
		wsum = mul1d / 2 * (mul1d * mul1 + mul1);
	
	else{
		wsum = mul1d / 2 * (mul1d*mul1 + 2 * mul1) + mul1;
	}

	int mul2d = lim / mul2;
	if (mul2d % 2 == 0)
		wsum += mul2d / 2 * (mul2d * mul2 + mul2);

	else{
		wsum += mul2d / 2 * (mul2d*mul2 + 2 * mul2) + mul2;
	}

	
	while (count * mul1 * mul2 < lim) {

		wsum -= count * mul1 * mul2;
		++count;
	}

	
	cout << wsum << endl;
	
	return 0;
}

or...

Code:
#include <iostream>

using std::cout;	using std::endl;
using std::cin;


int sumtolim(int mul, int lim)	{
	
	int wsum2 = 0;

	int muld = lim / mul;

	if (muld % 2 == 0)
		wsum2 = muld / 2 * (muld * mul + mul);
	else
		wsum2 = muld / 2 * (muld*mul + 2 * mul) + mul;

	return wsum2;
}

int main()	{

	int mul1 = 3;
	int mul2 = 5;
	int wsum = 0;

	cout << "Enter Limit : ";
	int lim;
	cin >> lim;
	--lim;
	int mul1d = lim / mul1;

	wsum += sumtolim(mul1, lim);
	wsum += sumtolim(mul2, lim);
	wsum -= sumtolim(mul1 * mul2, lim);

	cout << wsum << endl;

	return 0;
}
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
32,623
Easiest is to just record the time it takes. There are different ways to get the time depending on OS and accuracy requirements. To be more accurate loop the code 1000 times or more such that it takes like 30s to compute.

There are better profiling tools but not needed for a begginer.

Looks like you have jumped right in. You look to be off to an excellent start. It is good to think about different methods and their advantages. I wouldn't worry too much about micro-optimization (small minor speed ups). Better to get a basic understanding of big O notation and try to evaluate how the runtime expects to increase with more data/inputs/items.sometimes the mathvlookd horrible buy you only need to understand the concept.
 
Back
Top Bottom