Archive | Python RSS feed for this section

Some Context Behind Python Style Guide PEP-8

12 Sep

A few days ago, I had to find out about the Python Style Guide, PEP-8the hard way (thanks SublimeLinter!). I’m now better at styling my Python, but it always helps having some context behind why you’re doing what you’re doing.

So I’m thankful to StackOverflow responder Mikko Ohtamaa for giving me a little more background about PEP-8 in the comments of my previous blog post. Here is what he wrote:

Continue reading

Django != Rails

9 Sep

Yesterday, I had a few hours to spare on fun learning, so I decided to check out Django. I’m learning Python now, so I figured I should be able to “quickly” make web apps by relying solely on Python and the relavent web framework instead of going back to my Ruby on Rails comfort zone.

I started going through this tutorial and was even able to deploy something to Heroku (I would really prefer Heroku over AppEngine…). Unfortunately, I ran into some serious errors that would take me a lot more time to figure out than I had, so I had to leave Django, at least for now.

Continue reading

How To Check If A List Contains A Sublist With Python

3 Sep

While working through the CodingBat exercises today, I came across the following challenge:

Given an array of ints, return True if .. 1, 2, 3, .. appears in the array somewhere.

array123([1, 1, 2, 3, 1]) → True
array123([1, 1, 2, 4, 1]) → False
array123([1, 1, 2, 1, 2, 3]) → True

I personally challenged myself to refactor my solutions into one line of code, which is forcing me to look into some more advanced Python methods. Here’s my solution:

Continue reading

How To Count Substrings In A String With Python

3 Sep

I’m still working on the CodingBat Python exercises, and have found this problem a bit challenging to put into one line of code:

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

After a few minutes of thinking through it and checking out this answer on StackOverflow, I’ve come up with a solution!

Continue reading

How To Filter Lists By Index In Python

3 Sep

While doing the CodingBat challenges this morning, I challenged myself to refactor each of the solutions into one line of code (if possible). I was doing great, until I ran into this problem:

Given a string, return a new string made of every other char starting with the first, so “Hello” yields “Hlo”.

string_bits(‘Hello’) → ‘Hlo’
string_bits(‘Hi’) → ‘H’
string_bits(‘Heeololeo’) → ‘Hello’

Here is my first attempt:

Continue reading

The Ternary Operator Trick In Python

1 Sep

The Ternary Operator is really handy for refactoring a simple if else statement into one line of code. Unfortunately, Python doesn’t have the standard ? : operator built in. So, what should you do to refactor your code?

Continue reading

Hello Python! I Think We’ll Be Great Friends

1 Sep

And so it starts again. I’m now learning a new language, Python, and couldn’t be more excited about it.

As a beginner programmer, it is important to be exposed to as many languages as possible early on (even though it’s hard!), so we can see and learn from the patterns across languages. Just like with real-world languages, the more computer languages you know, the easier it is to pick up new ones.

Continue reading