Python & Twitter OAuth

I always need to rethink again whenever I need to deal with Twitter OAuth, which annoys me a lot. Therefore I decided to put it in here as a simple article, so I can easily find the information I need in the future.

Before testing Twitter OAuth, you will need to create your application in here. If you have any question about creating Twitter application, please follow this good article How to register a Twitter APP in 8 easy steps to create the app step by step.
Continue reading

Understand Common Sequence Data Types in Python – String, Tuple, and List

string, tuple, and list are the three common build-in ordered collection data types in Python. Those sequence data types share some common operations.

Common Sequence Operations in Python
Name Operator Example
reference: Operations on Any Sequence in Python (interactivepython.org), 5.6 Sequence Types
indexing [n]
            data = [1,2,3,4,5]
            data[3] # return 4
concatenation +
            data = [1,2,3,4,5]
            data + [9] # return [1,2,3,4,5,9]
repetition *
            data = [1,2,3,4,5]
            data * 2 # return [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
membership in
            data = [9,2,4,4,6,2,8]
            for val in data: print(val), # return 9 2 4 4 6 2 8
length len()
            data = [1,2,3,4,5]
            len(data) # return 5
slicing with step k [i:j:k]
            data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
            data[0:10:2] # return [1, 3, 5, 7, 9]
slicing [i:j]
            data = [1,2,3,4,5]
            data[1:3] # return [2,3]
Minimum min(data)
            data = [1,2,3,4,5]
            min(data) # return 1
Maximum max(data)
            data = [1,2,3,4,5]
            max(data) # return 5
Index data.index(sub[, start[, end]])
            data = [1,2,3,4,5]
            data.index(3) # return 2
Count data.count(i)
            data = [1,2,3,4,5,3]
            data.count(3) # return 2

Continue reading

Use Python to do Switch Case

The switch statement can be very useful sometimes, and it is pretty easy to handle multiple conditions instead of writing a lot of if else. Here is the example of writing switch statement in most high-level programming languages:

switch ( a ) {
    case b:
        // Code
        break;
    case c:
        // Code
        break;
    default:
        // Code
        break;
}

But the problem is, Python doesn’t have switch statement.
Continue reading

Python and simple recursion

This afternoon my husband and I talked about recursion, and we decided to write some recursion functions with different languages. He uses JavaScript, and I use Python.

Here is the simple question: Please use recursion to revert 12345 to 54321

def reverse(val, new_val):
    if val == '':
        print(new_val)
    else:
        reverse(val[:-1], new_val + val[-1:])

if __name__ == '__main__':
    reverse(raw_input('Please provide any number: '), '')

Continue reading

Use Python to implode array

I know it’s too simple to become an article, but I still want to put in here because it’s a little bit different from other languages.


data = ['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31']
result = ','.join(data)

print(result)
// 2,3,5,7,11,13,17,19,23,29,31