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

Send and receive SMS using GSM modem or phone on Ubuntu | TechyTalk.info

Send and receive SMS using GSM modem or phone on Ubuntu | TechyTalk.info.

#go to terminal and give the msg as below
#sudo python sms.py 'CellNumber Msg you want to send'
import gammu
import sys
def main():
	words = sys.argv[1].split(None, 1)
	dest = words[0]
	msg = words[1]
	print msg + "-----" + dest
	#return
	sms = gammu.StateMachine()
	sms.ReadConfig()
	sms.Init()
	message = {
	    'Text': msg,
	    'SMSC': {'Location': 1},
	    'Number': destinationNumber,
	}
	print sms.SendSMS(message)

if  __name__ =='__main__':
	main()