Home  >  Article  >  Backend Development  >  What are the differences between c++ and python?

What are the differences between c++ and python?

尚
Original
2019-07-04 10:40:5551912browse

What are the differences between c++ and python?

The program has two execution methods, interpreted execution and compiled execution.

PYTHON is a scripting language that is interpreted and executed without compilation, so it is very convenient and fast, and can cross-platform very well. It is especially suitable for writing some small tools and small programs.

C is a language that needs to be compiled and run. It is compiled on a specific machine and run on a specific machine. It has high operating efficiency, safety and stability. But compiled programs are generally not cross-platform.

Learning a programming language cannot depend on whether it is popular or not. But it depends on its role in a certain field. Just like compilation, although it is not popular now, it is still very popular in some industries. PYTHON is indeed very powerful, but it will not become a mainstream programming language in three to five years. At least not in the world of Windows program development.

It is recommended to use C as the main learning direction and PYTHON as an auxiliary tool. Of course, it would be better if everyone could learn the general principles.

Python plays the role of a script in game development. For example, if a web game needs a new function, you can use python for secondary development. It is very simple. C acts as the kernel in game development because it has the fastest execution speed. As for what to learn, it depends on your own interests, because the idea of ​​​​programming is the same, and the ability to solve problems is the accumulation of time and work experience. The scripting languages ​​are python and ruby ​​but ruby ​​is Japanese.

Short overview

Interpretation and execution:

The interpreter executes on the spot according to the input data without generating any target program

Compilation and execution:

First compile the source code into the target language (such as machine language) and then connect it to the generated target program through the linker for execution

Differences in syntax:

1. Abbreviation Indentation

The requirements for "indentation" in Python are relatively strict. In Python, different levels of indentation are used to represent different levels of code blocks. The following code:

a = 1
if a == 1:
    b = 5
    b = b + a
else:
    b = 6
    b = b + 2

In C, use brace pairs {} to mark code blocks.

2. Global variables

In Python, the value of a global variable cannot be modified directly inside a function definition, otherwise an error will be reported directly. The following code:

def f(x):
    print a
    a = a / 2
    return a + x
a = 3
f(5)

In the program file, you will find an error like this: local variable 'a' referenced before assignment. As you can see, the variable a in function f(x) is treated as a local variable instead of an external global variable. If you change it to the following code, no error will be reported:

def f(x):
    print a
    return a +
a = 3
f(5)

You can see that global variables can be accessed inside the function, but they cannot be modified directly. If you want to modify it, you can use global to mark a as a global variable. The code is as follows:

def f(x):
    global a
    print a
    a = a / 2
    return a + x
a = 3
f(5)

3. The format of the for statement

In Python, the format of the for statement is as follows:

for iter_var in iterable_object:
    suite_to_repear

As you can see, in Python, iterable objects (such as strings, lists, tuples, dictionaries, files, etc.) are used to form loops.

4. Use of keyword parameters

In Python, you can use "keyword" parameters to disrupt the order of parameter passing in the original definition of the function, as shown in the following code:

def fun(sh,ch):
    return 2*sh + ch
print fun(ch = 1, sh = 5)

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of What are the differences between c++ and python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn