Python – Coding with fun

How to reverse a string?

s = 'abcdef'

s = s[::-1]

print s 

Explanation: “::” is known as stride notation

s[::2]
s[::-2]

Returns “ace” and “fdb” respectively.

How to read from console in Python?

name = raw_input()

Want to read an integer?

num = raw_input()
num = int(num) # raw_input returns string, so convert it to integer

Swap values in python:

a, b = b, a

My motivation to learn python is: life is short – you need Python