From C + + to Python (one) (by Robinvane Suen)

Source: Internet
Author: User
0. ForewordThe two days of summer vacation at home, leisure to be okay, times of boredom, so the borrowed 3 months but not how to read the "Python language primer" roughly read it again, casually rewrite a part of the original program as a practice. As a parsing scripting language, I feel that Python has many different approaches to the C + + language, and I hereby record it. Since I've only been using Python for 3 days, I don't know much about the Python language, and if there are errors, please point out. 0.0. Applicable readers I think the quickest way to learn a new language is to make an analogy with the language that you are familiar with. As a result, we have a certain understanding of the computer programming language, in fact, the basic structure of various languages are similar, so we do not need to start from 0; second, the analogy makes it easier to remember the differences between languages so that you can avoid making mistakes. As the main programming language of the Chinese universities is usually selected by C + +, so this paper uses C + + and Python analogy. In other words, this article assumes that you have a certain understanding of C/n + +, and because Python and Java and C # have many similarities in object system and parameter values, if you know Java or C # language, it will certainly accelerate your understanding of the Python language, but this is not necessary. 0.1. Objective this article hopes to be able to provide a quick start to a Python language for a C + + programmer to help a C + + programmer quickly convert to a Python language. 0.2. Main reference material "Getting started with Python language": Mark Lutz & David Ascher, Chen Ge, Feng Dahui, O ' Reilly/China Power Press. 1. Python Overview 1.0. C + + and Python Although the title of this article is "from C + + to Python", it does not compare the "capabilities" of C/C + + and Python, which is a system language, and the code that is written with them is usually very fast, And you can do most of the common functions. Python, in turn, is a scripting language that uses Python to quickly write code that can run. Those who are C + + may be able to curse the speed of the Python program, but I think a language that can survive has its own reasons, as does the Python language. Today's computer processing speed is very fast, for some tasks, the speed is not the primary consideration. For example, to automate database access, the project team needs to make a code generator for database access. This is a common task involving many aspects of programming, including string processing, database access, and possibly a GUI system. This task is obviously not a good fit for C or C + +, because using C + + to develop, although it can save a bit of running time, but waste a lot of development time. Therefore, there is no good or bad language, only suitable for the wrong points. 1.1. Python language Overview Python is a "portable object-oriented scripting language." 1)           Python is object-oriented. It supports object-oriented concepts such as inheritance and multiple inheritance, polymorphism, and operator overloading. 2)           Python is portable. Programs written in the Python language are typically run on multiple platforms without modification, which is similar to Java. But Python also provides some plantform dependent modules that need to be careful when using these modules because they are not portable. 3)           Python is an explanatory language. To be precise, Python first compiles the source program into intermediate code (similar to Java and C #), and then interprets execution. Note, however, that the compilation process is transparent to programmers (this is different from Java and C #). 4)           Python is a dynamic language. It supports metadata programming, support Run-time compilation, execution, and so on modernAdvanced features of language. These are difficult or even impossible to implement in C + +. Python even allows you to add a member of an object at run time. 5           Everything is the object. The concept of objects in Python is much richer than in other languages, such as a class is an object, so you can use it at run time, unlike C + +, a class is a compile-time object. For this reason, Python is highly dynamic. 6)           automatic memory management. Like Java, you don't need to manage the memory that your code uses. In C and C + + programs, it was a nightmare. 7)           Others: Python can use the components written by C/COM + +, or embed the Python code in C + + code. 1.2. Python type, object system it is very important to understand the type and object system of a language, so I write it before the introduction of other details. In general, the object system of the Python language is closer to Java (this section may be skipped for people familiar with Java). There are two types of Python: The first is a value type, the second is a reference type (it does not have the so-called pointer type in C + +), and all numeric types (int, long, float, double) are value types. therefore: i = 1     #i is a integer j = i     #assign i to J j = 2     #change the VA Lue of J Print i   #the change of J does a not leads to the change of I, so I = 1 The following figure shows the relationship between I and J: #after the Code "J = i "I---1 j---1   #after the code" j = 2 "I---1 j---2 it can be seen that the memory location referenced by I and J is different, so J's change does not cause I change. In addition to the number type, other types of Python areis a reference type, that is to say: Arr_i = [1,2,3]      #arr_i is a array arr_j = Arr_i        #assign Arr_i to arr_j arr_j[0] = 0         #change The value of the "the" R_j print Arr_i          #arr_i [0] = 0 now. Use the figure below to see the mysteries Arr_i  /         /          [1, 2, 3]  #after "Arr_j = Arr_i"          /arr_j   /Run Arr_j[0] = 0 Arr_i/         /          [1, 2, 3]  #after "arr_j[0] = 0"          /arr_j/due to Arr_ I and Arry_j both refer to the same memory, so the Arr_j changes lead to arr_i changes. Review the C + + type system: Simply put, C + + can access an instance of a data type in three ways: values, pointers, and references (as if there are aliases ...), and there are three ways to access both integers and custom objects. Regardless of the reference, in the language of C + +, in Python, numbers are value types, and other objects are the equivalent of pointers. Of course, this is not entirely true, but many times this is enough to rule out your doubts. 2.  First Python program 2.0  run mode in this section we assume that we are in the Unix/linux system. Similar practices are done on other systems. Python provides a variety of ways to write and run a Python program. Here are only three kinds of: 1           interactive command line     The simplest way, The most convenient tool for learning languages is to use the Python command line to enter and run code. %python                          #启动python交互命令行工具 >>>print "Hello world!/n"    #python代码, Print the classic "Hello World" Hello world!             in standard output        #这个是python执行了上一行代码的输出 >>>                               #按ctrl + D exit 2)           Run module file    The   module file is simply a Python source code file, such as saving the following code as helloworld.py print "Hello world!/n" and running in the shell%python helloworld.py Hello world! 3)           run UNIX type scripts (not available for Windows and Dos) UNIX type scripts are also source files, but special flags are added. For example, save the following code as helloworld.py #!/usr/bin/python print "Hello world!/n" and then add helloworld.py execution permissions:%chmod +x helloworld.py helloworld.py            #执行python脚本程序 Hello world! 2.1.  Typical Python program structure below gives a typical Python program's organizational structure, running with UNIX scripts run 1  #!/usr/bin/python   2  import Math & nbsp;                                #use the module Math 3  from String Import atof                      #use the function "Atof" in module string   4  STRF = "0.1"     &nbs P;                          #declare A string variable 5  fval = atof (strf)    & Nbsp;     &n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.