Home  >  Article  >  Backend Development  >  The difference between python and c

The difference between python and c

(*-*)浩
(*-*)浩Original
2019-06-25 15:14:421650browse

We all know that the bottom layer of Python is implemented in C language, but we should not use C language thinking and style to write Python code. There are many differences between Python and other languages. Here is a simple analysis:

The difference between python and c

"Indentation" and "{}" (recommended learning: Python video tutorial)

Unlike C, C, Java and other languages ​​that use curly braces {} to separate code segments, the code indentation method used in Python separates code blocks.

' and "

There is a strict difference between single quotes (') and double quotes (") in C language , single quotes represent a character, which actually corresponds to an integer value in the character set used by the compiler. For example in ASCII, 'a' corresponds to the number 97. Double quotes represent strings, which end with '\0' by default.

But in python, there is no obvious difference between single quotes and double quotes. There are only slight differences in usage when the input string content is different.

>>> string1 = "He said ,\"Hello\" "     #字符串中本身的双引号需要转义
>>> string1
'He said ,"Hello" '
>>> string2 = 'He said ,"Hello" '        #字符串本身的双引号不需要转义
>>> string2
'He said ,"Hello" '

list and array

Arrays in C language contain numbers, while lists in python can contain many different data elements.

.import and include

To use that library function in C language, you need to introduce the header file with include, while in python you need to introduce other modules or functions It needs to be introduced with import.

The different mechanism between the two is that include in C language tells the preprocessor that the content of the file specified by this include should appear as a local source file, while in python import can be imported through a simple import, or import numpy as np

global variables

In C language, declare global variables. If the value is constant, Then you can directly declare it with #define. If you only declare the global and the value of the variable is variable, then it can be directly similar to int a. In python, when declaring a global variable, you need to add global, similar to global a. When using it in a function, you need to declare global a first. Otherwise, if you use a directly, python will re-create a new local object and assign the new value to it. The value of the original global variable will not change

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

The above is the detailed content of The difference between python and c. 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