Beginner Python Notes

Sat, 17 Jul 2010

I'm learning Python for fun and so far I really like the syntax, it's very clean and straight-forward. If you are familiar with PHP this will come easily, it's just practice until the habit forms. Here are some of my beginner Python notes:

#
#	Beginning Python
#	--------------------
#	Quick Notes
#

# 	Comments
# 	<-- Use "#" for Single Line
""" <-- Use 3 quotes for multiline """


#	Import: 
#	Used for including libraries for more functionality, built-in or custom.
# 	----------------------
	import math
	math.ceil(18.7)
	

#	Function to Variable 
#	----------------------
	sq = math.sqrt # Note: Don't include the ()
	sq(8)
	

#	Handle Input
#	----------------------
#	input(): 		When working with expressions (like math problems)
#	raw_input():	Get info from user, convert to string
#	----------------------
	x = raw_input("Enter Name: ")
	print "Hey " + x
	raw_input("Press")	# Prevent the program from shutting down instantly.
	
	

#	String Manipulation
#	----------------------

	#	Concat String
	a = "dog"
	b = "cat"
	a + b
	
	#	You can NOT concat integer with string,
	#	So use one of these:
	num = str(18)
	print "I am " + num
	
	#	Convert anything into string	
	num2 = 13
	print "I am " + `num2`
	print "I am " + repr(num2)
	
	'horse'[2] 	#	Outputs: 'r'


#	Sequence (Array)
#	----------------------
	fruit = ['apple', 'banana', 'orange', 'pineapple']
	fruit[0]	#	Outputs: apple
	fruit[-1]	#	Outputs: pineapple
	
	test[3] = 'foo'	#	Change array element
	del test[3]	#	Delete array element


	#	String to Sequence (Array)
	str = list('string')	# Output: ['s', 't', 'r', 'i', 'n', 'g']
	
	#	Add AT Spot 4
	str[4:] = list('new')	# Output: ['s', 't', 'r', 'i', 'n', 'e', 'w']
	
	#	Add BETWEEN
	example[1:1] = [2,3,4]	# Output: ['s', 't', 2, 3, 4, 'r', 'i', 'n', 'e', 'w']
	
	# 	Remove Elements
	example[1:5] = [] 	# Output: ['s', 'r', 'i', 'n', 'e', 'w']

	#	Append Array
	#	* Must be one value, use Extend for multiple values
	blah = [1,2,3]
	blah.append(55)	#	Output: [1, 2, 3, 55]

	#	** Append array+array creates Multi-Dimensional	
	foo = [1,2]
	bar = [3,4]
	foo.append(bar)	#	Output: [1, 2, [3, 4]]
	
	#	Extend Array	(Like: array_merge, without removing duplicates)
	one = [1,2,3]
	two = [3,4,5]
	one.extend(two)	# one = [1,2,3,4,5]
	
	
	
#	Slice (array)
#	----------------------	
	digits = [0,1,2,3,4,5,6,7]
	
	digits[4:8]		#	Slice from places 4 to 8 (Does not include the 8th slot)
	digits[4:]		#	Slice from 4 to END
	digits[:10]		#	Slice from BEGINNING to 10
	digits[:]		#	Select ALL
	digits[::2]		#	Slice every OTHER items
	digits[1:8:2] 		#	Slice from 1 to 8, and SELECT every 2 items
	
	digits[::-1]		#	Select ALL, BACKWARDS
	digits[::-2]		#	Select ALL, BACKWARDS, SELECT every 2 items
	
	
#	Length	(like: strlen or count)
#	---------------------		
	test = [1,12,3,44,22,3]
	
	len(test)		#	Length of Sequence: 6
	len('string') 		#	Length of String: 	6
	max(test)		#	MAX of Sequence:	44
	min(test)		#	MIN of Sequence:	1
	max('test')		#	MAX of String:		t
	min('test')		#	MIN of String:		e
	
	
#	Methods	
#	----------------------	
#	Syntax:	object.method()


	#	Count/Search:
	fruit = ['apple', 'banana', 'orange', 'pineapple']
	fruit.count('banana')	#	Output: 1


	#	Search for key:
	fruit = ['apple', 'banana', 'orange', 'pineapple']
	fruit.index('orange')	#	Output:	2
	
	#	Insert a value
	fruit.insert(2, 'ONION')# Output: ['apple', 'banana', 'ONION', 'orange', 'pineapple']
	
	#	Remove a value
	fruit.pop(3)			# Output: Removes 'orange'
	fruit.remove('ONION')		# Output: Removes (Duplicates still only removes 1)
	
	
	#	Sorting
	fruit.reverse()			# Output: ['pineapple', 'ONION', 'banana', 'apple']

	foo = [500, 'dog', 200]
	foo.sort()			# Output:	[200, 500, 'dog']
	sorted('cbda')			# Output: ['a', 'b', 'c', 'd']