Tips for Python Users
[File Read and Write]
For opening a file, we can use open and close functions similar to c programming cases.
E.g., f = open( 'hello.txt', 'r')
[For .. in ..]
When we use for-in command, we must use : in the end of the line.
E.g., for line in lines:
[Operators]
Python use similar operator with C
[Boolean operation]
In Python, we can use 'and', 'or' and 'not' directly for operation
E.g., x or y
[True and False]
For Boolean variables, we can use True or False but not true or false
[String.Split()]
[String.join()]
An array can be joined into a string by the use of the join() method.
[Slice for list and array]
Slice commands are supported for list in Python as well as array and more advanced slice commands are supported for array similar to Matlab.
A[i][j] is used for multilayer list, A[1:2:10] is used for listing specific elements in list. In interestingly, first and last element indices can be omitted in Python such that A[:2:] then the first is 0 and the last is the number of elements.
For array, multi-dimension slice commands are supported. A[i,j,k] are indicating an element locating i, j, k position. This commands are not available for python basic list. It is only working when numpy is imported and the type of variable is set to array.
For opening a file, we can use open and close functions similar to c programming cases.
E.g., f = open( 'hello.txt', 'r')
[For .. in ..]
When we use for-in command, we must use : in the end of the line.
E.g., for line in lines:
[Operators]
Python use similar operator with C
[Boolean operation]
In Python, we can use 'and', 'or' and 'not' directly for operation
E.g., x or y
[True and False]
For Boolean variables, we can use True or False but not true or false
[String.Split()]
A string can be split to wrods in array form.
my_split_words = my_phrase.split()
http://www.daniweb.com/software-development/python/threads/187010[String.join()]
An array can be joined into a string by the use of the join() method.
>> sentence = ["there", "is", "no", "spoon"] >> string.join(sentence, " ") 'there is no spoon'
Slice commands are supported for list in Python as well as array and more advanced slice commands are supported for array similar to Matlab.
A[i][j] is used for multilayer list, A[1:2:10] is used for listing specific elements in list. In interestingly, first and last element indices can be omitted in Python such that A[:2:] then the first is 0 and the last is the number of elements.
For array, multi-dimension slice commands are supported. A[i,j,k] are indicating an element locating i, j, k position. This commands are not available for python basic list. It is only working when numpy is imported and the type of variable is set to array.
Comments
Post a Comment