Python dynamic and strong-type usage example

Source: Internet
Author: User
This article mainly introduces the usage of python dynamic and strong types, and compares the usage of python dynamic and strong types with the C # instance. For more information, see the examples in this article to analyze the usage of python dynamic and strong types. Share it with you for your reference. The details are as follows:

Python variable declaration and definition

Unlike C #, Python does not need to define its type before using a variable. run the following example:

i = 12 print i

We can see from the above that variable I does not need to be defined before use, but must be declared and initialized. Run the following example:

i = 1print i + j

The above code will generate an exception: "NameError: name 'J' is not defined". The Python prompts that the variable j is not defined. This is different from weak languages such as BASIC. In BASIC, no exception is generated when the above code is executed. you can try it in the excel vba development environment and change print to MsgBox. The result is 1. This shows that Python is not a weak language similar to BASIC.

On the other hand, there is a big difference between Python and C #, that is, the same variable name can (at different stages) represent different types of data during the program running. let's take a look at the following example:

i = 1print i,type(i),id(i)i = 10000000000print i,type(i),id(i)i = 1.1print i,type(i),id(i)

The type of variable I has undergone int, long, and float changes during program execution, which is very different from static type languages (such as C. As long as a variable obtains a data type, the static language will always be of this type. the variable name represents the memory location used to store data. The variable names used in Python are only references of various types of data and objects, and the memory location for storing data is obtained by using id, the input data 1, 10000000000, and 1.1 are stored in the memory locations indicated by id, until the garbage collector pulls it away (when the system determines you no longer use it ). This is a typical feature of dynamic language. it determines the type of a variable when assigning values to it.

On the other hand, Python is strongly typed. try to run the following example:

#-*-Coding: UTF-8-*-I = 10; j = 'SS' print I + j # print str (I) + j to output 10 ss

An exception occurs: "TypeError: unsupported operand type (s) for +: 'int' and 'str '". In BASIC and other weak languages, the above example runs normally and returns results (although sometimes unexpected.

Therefore, Python is both a dynamic language and a strong language, which is different from C. C # programmers may spend some time adapting to the declaration, definition, and usage of Python variables, but I believe you will soon like it, because it makes things easier (and not unsafe ). In addition, C #4.0 has begun to define and use variables in a similar way (by adding the keyword dynamic before the variable name). If you have learned Python variables first, it will be able to adapt to the dynamic programming features of C #4.0 faster.

I hope this article will help you with Python programming.

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.