What are the syntax differences between Python and C

Time:2021-4-8

Python is a cross platform languageComputer programming language. It is a high-level integration of interpretive, compiler, interactive and object-orientedscripting language . Originally designed for writing automated scripts (shell), with the continuous update of the version and the addition of new language features, more and more are used for independent, large-scale project development.

C language is a process oriented and abstract general programming language, which is widely used in the bottom development. C language can be used in a simple waycompile, processing low levelstorage. C language produces only a small amount of datamachine languageAnd a high efficiency programming language that can run without any running environment support. Although the C language provides a lot of low-level processingfunctionHowever, it still maintains the cross platform characteristics, and the C program written in a standard specification can be used in theEmbedded processorAs well as supercomputers and many other operating platformsComputer platformCompile on.

Although they are all programming languages, there are many differences between them.

C is the compiler language and Python is the interpreter language. This is also mentioned by other respondents, which is indeed an important distinction. The biggest difference between compilation and interpretation is that you need to write an entry function, which is the main thing in C language, but Python can’t write it.

The advantage of compiler type is “static”, code can not be compiled line by line, it must be compiled as the whole project, which facilitates type checking and reduces the error rate of runtime; runtime efficiency is higher, because the compiler can coordinate all aspects and generate more optimized machine instructions; once the compilation is completed, it can be directly executed again in machine language.

The advantage of an interpretative language is that it is “dynamic” and each line of code can be executed independently (except for code blocks). In this way, real-time interaction, adjustment of running program and real-time asynchronous debugging can be carried out flexibly.

Python is both a dynamically typed language and a strongly typed language. When you first assign values to variables, they determine the type of variable. C language is a static type language, which determines the data type at compile time. Most statically typed languages guarantee this by requiring that the data type of any variable be declared before it is used.

All data in Python is represented by objects or relationships between objects. Function is object, string is object, everything is the concept of object. Each object has three properties: entity, type, and value.

There is no concept of object in C, only “data representation”. For example, if there are two int variables A and B, if you want to compare their sizes, you can use a = = B to judge them, but if they are two string variables A and B, you must use StrCmp to compare them, because at this time, a and B are essentially pointers to strings. If you use = = to compare them directly, the comparison is actually the value address stored in the pointer.

Python “dynamic type”, I can understand, because I don’t need to declare type when writing. But you said it was a “hit”? I can’t accept that. Which strongly typed language can assign values to variables at will? As mentioned earlier, the variable that stores an object is actually just the address of the object in memory (while “value type”, such as small integer, generally stores the value directly). Variables are stored in the stack and objects are placed in the heap. The stacks were stacked neatly one by one and opened randomly. When we execute a = [1, 2, 3], the python runtime environment generates a new list object in the “heap”, returns the first address of the object, assigns it to the new variable a, and pushes it into the stack. After that, if a = {X ‘: 1} is executed, only the address in a will be changed to a dictionary object. Therefore, a variable can store any object, not because the variable itself can be changed at will, but because the pointer can point to anything.

In addition, JavaScript, a real weakly typed language, will not report an error when adding a string to an integer, calling a nonexistent method, or transferring an integer parameter to a floating-point number. So you can believe that Python is a strongly typed language. (supplement: more thorough weakly typed languages, such as lisp, even implement weakly typed languages on machines)

Python has six standard data types: number, string, list, tuple ancestor, set, and dictionary. There are four types of numbers: integer, Boolean, floating point and complex. C language can also be divided into four types: basic type, enumeration type, void type and derived type. Basic types: integer and floating point.

However, the basic data structure, the principle of computer composition, these are all languages that need to be understood, otherwise they will always only know a little bit.

Learning C language is difficult and Python is simple, but learning C language first can lay a better foundation. After all, to be a programmer, you can’t speak only one language. Most programmers need to know at least three languages.

This work adoptsCC agreementReprint must indicate the author and the link of this article