Python Learning Manual (1 Getting Started-data type)

Source: Internet
Author: User
Tags floor division new set set set stack trace

UNIX Env Find Tips

On some UNIX systems, it is possible to avoid hard-coded paths to the Python interpreter in such a way that it is written in a specific first line of comments in a file.

# ! usr/bin/env/python
... script goes here

After this is written, the ENV program can locate the Python interpreter through the search settings of the system search path (for example, in the vast majority of Unix shells, by searching all directories listed in the PATH environment variable). This approach can make the code more portable because it is not necessary to hardcode the location of the Python interpreter in the first line of all code.

Considerations for using Import and reload

You need to remember to use parentheses when calling reload, and remember to use reload when the latest code runs. In addition, reload is not transitive, overloading a module will only overload the module, and not be able to reload any modules imported by the module, so sometimes you have to reload multiple files. Because of these complexities, it is a good idea to avoid using import and reload to start the program from now on. Import and reload are common test methods in Python, but it's best not to abuse them.

  Python's core data types

Object type

Numeric type integer, long integer, float, complex type

String character sequences similar to C language

list [1,[2, ' three '],4]

Dictionary {' Food ': ' Spam ', ' taste ': ' Yum '}

Tuples (1, ' Spam ', 4, ' U ')

File MyFile = open (' Eggs ', ' r ')

Set set (' abc '), {' A ', ' B ', ' C '}

Other types of None,bool

Programming unit type functions, modules, classes,

Code stack trace for implementation-related type compilation

Python's numeric types include: integers (infinite precision), floating-point numbers, 16 binary, octal, binary, plural, set, fraction types, and Boolean and solid types. The imaginary part of the complex type is represented by J or J, and octal is represented by a 0o or 0O prefix (the first is zero and the second is the letter O).

About string types See: http://sebug.net/paper/python/ch04s03.html

A tuple (TULPE) differs in that it cannot be changed once it is created. In other words, tuples are sequences that cannot be changed. As with dictionaries and lists, tuples support mixed types and nesting.

In contrast to C + +, there are no switch statements in Python, and you can use if..elif..else statements to do the same work (in some cases, it is quicker to use a dictionary.) And, Python in the while statement, you can join an ELSE statement!!! This is not the same as C + +.

 

The For loop of Python is fundamentally different from a for loop for C + +. C # Programmers will notice that Python's for loop is very similar to a foreach loop in C #. Java programmers will notice that it is similar to the for (int i:intarray) in Java 1.5.

In C + +, if you want to write for (int i = 0; i < 5; i++), then in Python, you write for I in range (0,5). You'll notice that Python's for loop is simpler, clearer, and less error-prone.

Predefined operators in C + + can be operands only to the base data type, not to user-defined types such as classes. However, in the Python language, operator overloading can also be applied to classes.

In Python, variables are characterized by the following: 1 variables are created when they are first assigned. 2 variables are replaced with their values when used in an expression. 3 variables must have been assigned before an expression is used. Variables like objects do not need to be declared at the beginning (this is completely different from the C + +).

Technically, the difference between the default interactive echo and print is equivalent to the built-in repr and STR functions: Both functions can transform arbitrary objects into their string patterns. Repr (that is, the default interactive mode echo) produces structures as if they were code. STR (that is, the print statement) is transformed into a format that is generally more user-friendly.

Python Division: Division in Python3.0 and Python2.6 is slightly different, X/y in Python2.6 or previous versions, this pair of operation integers will omit the fractional part, and the floating point number will remain fractional. In the Python3.0 version, it will turn into true division (which keeps the decimal parts). X//y is the floor division, which does not take into account the type of the object, it always saves the fractional part of the result and gets the whole number.

In Python3.0, the built-in function is used to create the collection object, but a new set constant form is added, using the curly braces that are reserved for the dictionary. Set ([1,2,3,4]) and {1,2,3,4} are equivalent in Python3.0. Regardless of how the collection is created, Python3.0 uses the new constant format to display it. In Python3.0, the built-in set function is still required to create an empty collection or to build a collection from an existing iteration object, but the new constants facilitate the initialization of a collection with a known structure.

Set set is a powerful and flexible object, but there is a limitation in both Python3.0 and Python2.6, and we want to keep in mind that the collection can only contain immutable (hashed) object types because of its implementation. Therefore, lists and dictionaries cannot be embedded in the collection, but if you need to store composite values, tuples are an embeddable one. Tuples compare full values when tuples are used in a collection operation. The ability to assemble is also immutable and therefore cannot be embedded directly into other collections. If you need to store a collection in another collection, you can call Frozenset just as you would call set. However, it creates an immutable collection that cannot be modified and nested within other collections. The set parsing constructs are introduced in Python3.0, similar to the list parsing constructs. However, it is written in curly braces instead of square brackets, and is used for collections instead of lists.

Python Learning Manual (1 Getting Started-data type)

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.