Learn Python through C (1) about languages, numeric types, and variables

Source: Internet
Author: User
Tags integer division

Strongly typed language, it is not possible to "continue execution after a program execution error, and the problem caused by this error causes the subsequent execution of possible arbitrary behavior" such as an example of such errors, such as buffer overflow in C, jump to the wrong address.

Weakly typed languages, type checking is less stringent, such as bias tolerance for implicit type conversions. For example, the implicit conversion of the int type and double type in the C language. (The individual considers the free conversion of int and pointer types, and an int in union to be more descriptive of the problem with 4 char)

Statically typed languages, problems caused by rejecting type errors at compile time

Dynamic type language, which does not report type errors at run time

C language is weak type, static type

Python is a strongly typed, dynamic type

Numeric type:

integer int

There are many types of integer data in C in memory, and Python has only one

Float type float

There are two types of float and double in C, and Python has only one

boolean bool

C with the macro true and false corresponds to constants 1 and 0, in Python true and false are separate data type bool, but the internal implementation is still 1 and 0, for example: 1+true==2,1*false==0

Plural type (in python, lowercase j, denotes imaginary part, not with other letters) complex

There is no such type in C, you need to define a struct and a series of arithmetic functions, but c99/c++ has added this data type. With this data type in Python, you can easily perform complex operations

Empty type Nonetype

With a macro null in C for NULL, corresponding to the constant 0, in Python, none is a separate data type nonetype, unlike the constant 0, can not be calculated with the value, in fact, it does not count as a numeric type

Common operators for numeric operations:

+-*% plus, minus, multiply, modulo, and C are the same
/Except, even if two integer data is divided, the resulting result may also be a floating-point number

Divisible, rounded down, with the same effect as the integer division in C, with no decimal parts

* * power, C corresponding function is POW, of course, directly with the exponentiation operator will be better to see and easy to understand

Assignment operators:
= + = = *=/=//= etc... and C's usage basically consistent, Python does not have + +, since 1 to use +=1

Variable:
Python's variables do not have to be declared in advance, without specifying a type, but each variable is of a type

Each variable and constant in Python has an ID that can be obtained by using the built-in function ID (), which allows you to see some features:

Experiment One: a = 2, B = 2, then see the ID (a), id (b), ID (2) can be found that their three values are the same, as if three pointers to the same address, and the address of the data is 2

Experiment Two: a = 1, id (a), a = 2, id (a), you can see that the ID of a has changed, which means that the two A is not the same a

In view of these phenomena, the variable A and b do not use independent storage space to store the value, but more like a pointer, pointing to a piece of memory area, ID () is taken out to the destination address, while taking the value of the variable A, first through the ID (a) to find the destination address, and then the destination address stored data out

After my test experiment, the ID of true\false\none is fixed, the ID of the integer data from 5 to 256 is fixed, accord with the phenomenon of experiment one, the same constant value is assigned to a and b respectively, their ID is the same, but if this range is detected, for example, 6, Carry out the next experiment

Experiment Three: a = -6,b =-6, at this time ID (a) = = 2518190220080,id (b) = = 2518190220496, two variable IDs are different, but the values are the same, indicating that their values separate space, stored the same value-6

Experiment four: On the basis of experiment three, b=a, this time ID (a) = = 2518190220080,id (b) = = 2518190220080, their ID is the same, pointing to the same address.

After experiment, the 6 is replaced by floating point, tuple and list, also accord with experiment three and experiment four

Experiment Five: A=[1,2],b=a,a.append (3), at this time, the ID (a) and ID (b) the same, and the value of B also becomes [A-v], which verifies that the variable does not actually hold the variable value, but the target address, the value of the variable, Go to the destination address first, and then take the value out .

Learn Python through C (1) about languages, numeric types, and variables

Related Article

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.