learn python as a C++ programmer

As a C++ programmer, you do not need to buy a python book to learn it from the beginning. Bearing in your mind some differences between C++ and python is enough for you to read the source code written in python.

Instead of form a code block using brackets, python uses indent to form a code block,i.e.,the code lines with the same indent in front of the lines form a code block. The indent can be space(s), tab(s), or both, but they need to be same for all lines in the code block.

Python use “#” to begin a comment, use ”’ for multi-line comments.

Python use ‘, “, or ”’ to denote strings. ”’ can be used to quote multi-line string including new lines in it.

if, while, def, etc. end with :

list [1,2,3] is the counterpart of array in C++, but can contain different types of data.

tuple (1,2,3) is almost the same as list, but is read-only.

dict {1:”a”,2:”b”,3:”c”} is like map in C++

part of string(sub-string), list, tuple, can be obtained using the form like a[2:6], which means the third to the sixth elements,i.e., a[2],a[3],a[4],a[5], not including a[6]

A module (a .py file) contains variables and functions, which can be accessed in other scripts. To access the variables and functions in a module, import the module first(import modulename), then access with modulename.functionname or modulename.variablename.

From modulename import functionname will introduce functionname to the current script, so you can directly use function name without the modulename prefix.

import can be also used on package, i.e., import packagename. This will load/execute the file __init__.py in the folder named packagename. __init__.py usually imports other modules(.py files) in that folder.

Python variables do not have a type so they do not need delaration. They can store any type of value. A variable can have a value of one type this time and have a value of another type that time.

The member variables of object need not be declared when defining the class. They are defined using object.varname=value or self.varname=value. If the variable name begins with __(self.__varname=value), the variable is a private one and can not be accessed outside class. The variables and methods with __ both before and after have special meanings.

The variables declared when defining the class are the same as the static members in C++, and they are initialed when declared.

All methods in a class must have “self” as their first parameter.

contructor of a class has the fixed name __init__, the descontructor also has the fixed name __del__

create an instance of a class using obj=classname();

Like javascript, python allows to define function inside function, so called closure.

Python does not have “for(i=0;i<100;i++)”. The only form of for loop is “for var in list”. You can iterate an array by “for i in range(len(myarray)) print myarray[i]”

The expression in if statement is not enclosed by curl brackets. There is a semicolon at the end of if,while statements.

 

reference

Comments are closed, but trackbacks and pingbacks are open.