Ultra rapid learning of Python
-
I'm being interviewed for Python position. I'm at the Hackerrank online coding test stage. I don't know anything about Python, and I believe I made it semi-clear in the talk with external recruiter (who might or might not have passed on this info to the company, or even notice). Me and the company are both on tight schedules, so I need to do the test by Sunday. That gives me 3 days to prepare, 3 of which I'll be helping my aunt move out from her apartment.
On the plus side, I already know about a dozen languages, and I have no problem adding another.
Is it possible to learn enough Python in about 10 hours to pass a 90min programming test with flying colors? Or should I pick another language (one I already know)?
-
Is the test more like Hello World, or more like build a full-fledged ecommerce system from scratch?
-
@gąska
And what other languages do you know?Python is conceptually simple:
-
Everything is an object (that includes ints, floats, strings, functions)
-
Behaviour is determined by methods (like length
len(x) = x.__len__()
, subscripts:x[n] = x.__getitem__(n)
or custom operators)
You can list all fields / methods of an object withdir(x)
or interpreter/IDE suggestions -
The syntax is usually sane and understandable, surprises are unlikely in everyday code
-
It is important to be able to use built-in data structures, especially
list
anddict
. They are like JS array and object, but with better syntax. Also: list and dict comprehensions. -
You can experiment quickly with Jupyter, where you can change and re-run your code
Also depends what the test is about and how much it is general programming (like algorithms) and how much a specific use-case/library (like django).
-
-
@adynathos said in Ultra rapid learning of Python:
@gąska
And what other languages do you know?C++, C#, Java, Scala, Ocaml, F#, Rust, ActionScript, Lua, JavaScript.
@adynathos said in Ultra rapid learning of Python:
Also depends what the test is about and how much it is general programming (like algorithms) and how much a specific use-case/library (like django).
It's 100% algorithms. The question is mostly about how hard it is to figure out what parts of standard library to use to solve a given Rosetta Code-style problem under stress, and how many caveats I have to remember. For example, C# is straight forward enough that if you know any other Java-like language, you don't even need to learn it to do basic stuff (and stdlib parts are highly googlable), whereas C++ takes months of practice if you want your hello worlds not crash instantly.
-
@adynathos said in Ultra rapid learning of Python:
The syntax is usually sane
Except for the indentation-dependant part
-
You can use LearnPython to learn the basics. It's all in the browser, so no need to download or install anything.
-
@gąska said in Ultra rapid learning of Python:
It's 100% algorithms. The question is mostly about how hard it is to figure out what parts of standard library to use to solve a given Rosetta Code-style problem under stress, and how many caveats I have to remember.
Sounds like spend the 10 hours doing Python practice problems on HackerRank or CodeWars and you should be fine. Or should at least have a good feeling about whether you're fine, or screwed. It looks like HackerRank includes enough helper code in most of their problems to get you going. Looks like they both have sections to see other solutions, so you can find out about any neat tricks to solve a problem that you didn't know about.
-
@gąska said in Ultra rapid learning of Python:
It's 100% algorithms. The question is mostly about how hard it is to figure out what parts of standard library to use to solve a given Rosetta Code-style problem under stress, and how many caveats I have to remember.
The Python data types are pretty simple. Honestly, just read through this, it's very well explained:
I think you should be able to know the language well within a few hours of reading and practice. It has a bunch of unique things (like slices or the "for-else" construct) but aside from that it's just a regular object-oriented imperative programming language.
-
@gąska said in Ultra rapid learning of Python:
It's 100% algorithms. The question is mostly about how hard it is to figure out what parts of standard library to use to solve a given Rosetta Code-style problem under stress, and how many caveats I have to remember. For example, C# is straight forward enough that if you know any other Java-like language, you don't even need to learn it to do basic stuff (and stdlib parts are highly googlable), whereas C++ takes months of practice if you want your hello worlds not crash instantly.
There is a bunch of "pythonic" constructs that you are supposed to use all the time. For example,
[user.name for user in users]
, which is the equivalent ofusers.map(user => user.name)
. They are pretty unique to python and will take a while to get a hang of.If there are actual python experts at the interview, they'll easily sniff you out as an imposter.
I'd switch languages if I can.
(That said, your basic
if
-s andfor
-s are about what you expect, so if you can get away with that, that can work. Just don't tell them you are an actual python programmer)
-
@gąska said in Ultra rapid learning of Python:
C++, C#, Java, Scala, Ocaml, F#, Rust, ActionScript, Lua, JavaScript.
Then you should have no problem, Python is close to Lua or JS.
Just look at the syntax and you will be fine, as @cartman82 said, thefor .. in
loops are common.Oh and regarding the
map
,map(f, [1, 2, 3])
is lazily evaluated and will not actually executef
until you iterate over the result (it returns a "generator" object).
-
@cartman82 said in Ultra rapid learning of Python:
There is a bunch of "pythonic" constructs that you are supposed to use all the time.
Which are all just syntactic sugar for stuff you'd use a
for
loop for in any other language. (It's still afor
loop; you just write it as part of an expression, rather than a series of statements.) You've already said you don't know the language at all, so if after three days you don't know all the neat tricks, but have learned the basic syntax and one or two bits of sugar well enough to code an algorithm that does what it's supposed to, I'd count that as a demonstration of your ability to learn quickly, and I think that's more important than knowledge of a specific language. No, you won't look like an expert, but you're not trying pass yourself off as one.
-
@adynathos said in Ultra rapid learning of Python:
Oh and regarding the map, map(f, [1, 2, 3]) is lazily evaluated and will not actually execute f until you iterate over the result (it returns a "generator" object).
Is there any language that evaluates that eagerly?
-
So, after a bit of practice, I did the test in Python after all. Turned out alright. For 2 of 3 questions, my solution timeouted on large test inputs, but there was simply too little time to think up some clever optimization for traversing all contiguous sublists of 100k-element list.
-
@gąska said in Ultra rapid learning of Python:
Is there any language that evaluates that eagerly?
Yes. But not Python, so it's pretty irrelevant for this thread. (It makes sense to do it if the uses of the language tend to be ones that use all the values of the resulting list, since not having function applications hanging around simplifies other stuff. But it's all a matter of when you pay the costs and exactly which costs you pay.)
-
@gąska said in Ultra rapid learning of Python:
traversing all contiguous sublists of 100k-element list
That's quite a few items anyway. A timeout is expected.
-
@dkf inorite
-
This post is deleted!
-
@timebandit said in Ultra rapid learning of Python:
@adynathos said in Ultra rapid learning of Python:
The syntax is usually sane
Except for the indentation-dependant part
once you get used to the concept that braces don't introduce scope indentation based makes much more sense.
also it does have the lovely side effect that you can't get bugs like this:
if (someErrorCondition) doAThing(); return false; // We failed return true; // We succeeded
in C like languages that snippit always returns false, which is a bug! in Python (once you swap the syntax) it does the right thing.
if someErrorCondition: doAThing() return False return True
this code snippit has no "something wasn't covered by an if statement that i thought it was" bug.
That being said i'll still use ES6 for my fun programming because that's for fun, and work currently is C# so no python there too. but i wouldn't turn my nose up at a job (or project) that used Python because of the indentation syntax
-
@accalia said in Ultra rapid learning of Python:
i wouldn't turn my nose up at a job (or project) that used Python because of the indentation syntax
I don't really get the hate for it either. I pretty much always indent my code in the same sort of way that Python mandates, so the only difference for me would be not having braces around things (and not arguing with my co worker who insists that braces shouldn't be required on an if with a single statement)
-
@jaloopa said in Ultra rapid learning of Python:
I don't really get the hate for it either.
as far as i can tell the hate comes from this one simple fact:
- YOU CAN'T TELL ME HOW TO FORMAT MY CODE! I DECIDE HOW TO FORMAT MY CODE! WHARRRRRGARBL!!!!!!!!!!!!!!!!
i mean if you can come up with a more rational reason i'd love to hear it but after years of listening to just about every argument about it, and participating in many a code formatting standards discussion for various teams it really does seem like it falls to this....
-
@accalia said in Ultra rapid learning of Python:
i mean if you can come up with a more rational reason i'd love to hear it but after years of listening to just about every argument about it, and participating in many a code formatting standards discussion for various teams it really does seem like it falls to this....
The biggest issue is that transferring snippets from elsewhere can be quite a bit more errorprone as some transport mechanisms can mess with indentation quite a bit. It's not a show-stopper, but it is irritating.
-
@dkf said in Ultra rapid learning of Python:
@gąska said in Ultra rapid learning of Python:
traversing all contiguous sublists of 100k-element list
That's quite a few items anyway. A timeout is expected.
That makes it entirely an algorithmic problem. Without knowing the details, I can't be more precise, but there is usually some dynamic programming type of approach that lets you not check every sublist. I have done some of the speed tests on Hackerrank and I see these among the harder problems there all the time. Another variant is computing the distances between every pair of nodes in a tree.
That said, all the examples I have seen of this have seemed contrived and not representative of things people generally actually have to program; the idea is to make you think about ways you can improve the efficiency of slow code.
-
@accalia The funny thing is Python is not even that strict when it comes to formatting: you can indent one section with one space and the next one with 14 spaces. The only requirement is that you indent each section with a consistent (non-zero) number of spaces. And who would ever write an
if
or afor
block and not indent it?
-
@anonymous234 said in Ultra rapid learning of Python:
And who would ever write an if or a for block and not indent it?
I used to work with someone who wrote code like that (in Java).
-
@accalia said in Ultra rapid learning of Python:
also it does have the lovely side effect that you can't get bugs like this:
if (someErrorCondition)
doAThing();
return false; // We failed
return true; // We succeededin C like languages that snippit always returns false, which is a bug!
Always put braces, even if they enclose only one line
-
@jaloopa said in Ultra rapid learning of Python:
my co worker who insists that braces shouldn't be required on an if with a single statement
Your co-worker is wrong.
-
@timebandit yeah, but I'm sick of telling him that. We've agreed on the compromise that we'll keep to the standards of whatever bit of code we're editing
-
@dkf said in Ultra rapid learning of Python:
@anonymous234 said in Ultra rapid learning of Python:
And who would ever write an if or a for block and not indent it?
I used to work with someone who wrote code like that (in Java).
I know only one person who does that. Did they also keep all their functions within 4 lines of code?
-
In case anyone wonders. Despite timeouts, I've got to the next stage. Yay.
-
@gąska said in Ultra rapid learning of Python:
Did they also keep all their functions within 4 lines of code?
Hahahahaa. No.
He was writing error-correcting parser generators for a programming language we were developing (that did correction by trying to make the minimal changes that would make the program syntactically correct, a fiendishly hard problem to solve) and his code was correspondingly long. He just didn't like using indentation; after all, he didn't need it in old school FORTRAN so why would he need it now?
-
@jaloopa said in Ultra rapid learning of Python:
@timebandit yeah, but I'm sick of telling him that.
Show him how it screwed Apple in the ass
-
@timebandit said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
also it does have the lovely side effect that you can't get bugs like this:
if (someErrorCondition)
doAThing();
return false; // We failed
return true; // We succeededin C like languages that snippit always returns false, which is a bug!
Always put braces, even if they enclose only one line
see. the point is that particular error is impossible in python.
only lint tools can show you the error (and then only maybe and only if you use them) in C like languages.
:-D
-
@dkf said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
i mean if you can come up with a more rational reason i'd love to hear it but after years of listening to just about every argument about it, and participating in many a code formatting standards discussion for various teams it really does seem like it falls to this....
The biggest issue is that transferring snippets from elsewhere can be quite a bit more errorprone as some transport mechanisms can mess with indentation quite a bit. It's not a show-stopper, but it is irritating.
The most annoying, IMO/IME, is that you can't just copy/paste a block into an interactive interpreter to see if it gives you the result you expect. You have to copy/paste one line at a time, fixing the indentation as you go. As you say, not a show-stopper, but...
-
@accalia said in Ultra rapid learning of Python:
the point is that particular error is impossible in python
It's also impossible if you always use braces like I suggested
-
@accalia said in Ultra rapid learning of Python:
@timebandit said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
also it does have the lovely side effect that you can't get bugs like this:
if (someErrorCondition)
doAThing();
return false; // We failed
return true; // We succeededin C like languages that snippit always returns false, which is a bug!
Always put braces, even if they enclose only one line
see. the point is that particular error is impossible in python.
Yeah, at the cost of other potential errors, and without the obvious red flag of missing braces to clue you into what happened..
-
-
@accalia said in Ultra rapid learning of Python:
@timebandit said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
also it does have the lovely side effect that you can't get bugs like this:
if (someErrorCondition)
doAThing();
return false; // We failed
return true; // We succeededin C like languages that snippit always returns false, which is a bug!
Always put braces, even if they enclose only one line
see. the point is that particular error is impossible in python.
only lint tools can show you the error (and then only maybe and only if you use them) in C like languages.
:-D
Or make the braces mandatory on a language level - I'm sure plenty of C-like languages have done that, though I couldn't name any for certain. I'm thinking Java though?
-
@boomzilla and no "please automagically re-indent my code for me" functionality
-
@pleegwat said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
@timebandit said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
also it does have the lovely side effect that you can't get bugs like this:
if (someErrorCondition)
doAThing();
return false; // We failed
return true; // We succeededin C like languages that snippit always returns false, which is a bug!
Always put braces, even if they enclose only one line
see. the point is that particular error is impossible in python.
only lint tools can show you the error (and then only maybe and only if you use them) in C like languages.
:-D
Or make the braces mandatory on a language level - I'm sure plenty of C-like languages have done that, though I couldn't name any for certain. I'm thinking Java though?
Perl does, but Perl has other ... issues.
-
@pleegwat said in Ultra rapid learning of Python:
Or make the braces mandatory on a language level - I'm sure plenty of C-like languages have done that, though I couldn't name any for certain. I'm thinking Java though?
Alas, java does not.
-
@accalia said in Ultra rapid learning of Python:
as far as i can tell the hate comes from this one simple fact:
YOU CAN'T TELL ME HOW TO FORMAT MY CODE! I DECIDE HOW TO FORMAT MY CODE! WHARRRRRGARBL!!!!!!!!!!!!!!!!
i mean if you can come up with a more rational reason i'd love to hear it but after years of listening to just about every argument about it, and participating in many a code formatting standards discussion for various teams it really does seem like it falls to this....
Lack of closing characters makes the indented pyramid of doom that much worse.
That's why I like leaving semicolons in javascript, even though they aren't strictly needed. Anything that add more "texture" to code makes it easier for me to find my way around.
-
@accalia said in Ultra rapid learning of Python:
@timebandit said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
also it does have the lovely side effect that you can't get bugs like this:
if (someErrorCondition)
doAThing();
return false; // We failed
return true; // We succeededin C like languages that snippit always returns false, which is a bug!
Always put braces, even if they enclose only one line
see. the point is that particular error is impossible in python.
But there's another error that's possible in Python and no other language:
for foo in foos: if something(foo): doOneThing() else doAnother()
-
@gąska Surely that'll throw an error about foo being out of scope?
Disclaimer: I do not speak python
EDIT: Damn you editing your post under me.
-
@pleegwat said in Ultra rapid learning of Python:
@gąska Surely that'll throw an error about foo being out of scope?
I edited code so it doesn't. But even the old version only threw error at runtime.
-
@dkf said in Ultra rapid learning of Python:
He was writing error-correcting parser generators for a programming language we were developing (that did correction by trying to make the minimal changes that would make the program syntactically correct, a fiendishly hard problem to solve)
-
@timebandit said in Ultra rapid learning of Python:
It's also impossible if you always use braces like I suggested
That's the thing. It's possible to forget the braces and still have something that builds and happily does the wrong thing. It's not possible to forget the indentation in Python; that will cause a parse error.
-
@masonwheeler said in Ultra rapid learning of Python:
It's not possible to forget the indentation in Python; that will cause a parse error.
???
if (condition): doSomething() doSomethingElse()
What I really wanted was
if (condition): doSomething() doSomethingElse()
Totally different outcome, and no parser error
-
@timebandit said in Ultra rapid learning of Python:
@accalia said in Ultra rapid learning of Python:
the point is that particular error is impossible in python
It's also impossible if you always use braces like I suggested
but not every developer uses them, and if they use them they can misuse them.
-
@timebandit said in Ultra rapid learning of Python:
@masonwheeler said in Ultra rapid learning of Python:
It's not possible to forget the indentation in Python; that will cause a parse error.
???
if (condition): doSomething() doSomethingElse()
What I really wanted was
if (condition): doSomething() doSomethingElse()
Totally different outcome, and no parser error
indeed, but you can't read the first and think the code is the second.
and if you can maybe a trip to the optometrists is in order?
-
@accalia said in Ultra rapid learning of Python:
@timebandit said in Ultra rapid learning of Python:
@masonwheeler said in Ultra rapid learning of Python:
It's not possible to forget the indentation in Python; that will cause a parse error.
???
if (condition): doSomething() doSomethingElse()
What I really wanted was
if (condition): doSomething() doSomethingElse()
Totally different outcome, and no parser error
indeed, but you can't read the first and think the code is the second.
and if you can maybe a trip to the optometrists is in order?
But I also can't read the first and know that it was supposed to be the second.