Python: Difference between revisions

From Elvanör's Technical Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 12: Line 12:


* To cast a float to an integer, you can use the built-in int() function.
* To cast a float to an integer, you can use the built-in int() function.
== Exceptions ==
* Sample code to deal with an exception:
<pre>
try:
doSomething()
except :
print str(sys.exc_info()[0])
</pre>
This will give you information about the raised exception type.

Revision as of 08:52, 1 August 2008

Important changes from Java

  • When you pass an object to a function, and "reassign" it using its local function argument name, the outside objects won't get reassigned. This is because Python reassigns the local name only. This is a important difference from Java.

Useful techniques

  • To iterate over two lists at the same time, use the zip built-in function:
for a,b in zip(list1, list2):

Casts

  • To cast a float to an integer, you can use the built-in int() function.

Exceptions

  • Sample code to deal with an exception:
	try:
		doSomething()
	except :
		print str(sys.exc_info()[0])

This will give you information about the raised exception type.