Python trivial syntax

Source: Internet
Author: User
Tags arithmetic operators

1 Python is a strongly typed language, variables must be declared and initialized, and variable names can represent different types of variables in different places;
2 the variable name in the C language represents the location of the data, while the variable name in Python is just a reference to an object, and the ID (variable) can give the variable address, type (variable) to give the variable types;
3 identifiers that begin with an underscore, such as _foo, cannot be imported with the From module import * statement. There are two underlined identifiers, such as __bar, that are used to implement class-private properties. There are two underlined identifiers, such as __init__, which are reserved by special methods;
A 4 null type (None) indicates that the value is an empty object, such as a function that does not explicitly define the return value to return none. An empty type has no attributes and is often used as the default value for optional parameters in a function. The Boolean value of None is false;
5 Python uses true and false to define a real, you can directly use A=true or A=false to define a Boolean variable;
6 in Python, none, 0 in any numeric type, empty string ' ', empty tuple (), empty list [], empty dictionary {} are treated as False, and other objects are true;
7 python supported data types int long float complex, we actually understand that Python's long and int are the same type, because when an integer exceeds the range of int, Python automatically upgrades it to a long integer. The Python float is a floating-point number of 64bit.
8 Defining a standard string (str) in Python can use single quotes, double quotes, and three quotation marks, which makes Python input text more convenient than Java. For example, when the contents of STR contain double quotation marks, they can be defined in single quotation marks, and vice versa. You can use the three-quote definition directly when you have special characters such as line breaks in characters. This makes it a lot easier not to remember the chaotic escape characters. Of course, Python also supports escape characters, and the meaning is basically the same as Java. To define a Unicode string in Python, you need to precede the quotation marks with a character U. s= "Hello", s[0] means h,s[-1] represents I.
9 The variables defined by global are global variables.
The list in Python is similar to ArrayList in Java for sequential storage structures. The list is denoted by a symbol [], in which the middle element can be any type (including the list itself to implement a multidimensional array), and the elements are separated by commas. The value or assignment can be indexed by position, just like a C array. The value of the list can be changed. A good feature of Python's core data types is that they support arbitrary nesting. It can be nested in any combination. A direct application of this feature is the implementation matrix, or a multidimensional array in Python.
11 of people who have used dictionaries in Java should not be unfamiliar with Hashtable, a hash table in Python is a dictionary (dict). Like set, a dictionary is an unordered storage structure that includes the keyword (key) and the value of the keyword (value). Dictionary={key:value} #当有多个键值时, split with a comma.
A 12-tuple is very similar to a list, which uses a () rather than a [] enclosed sequence. Tuples are faster than lists, but tuples are immutable sequences that, like STR, cannot change their values in situ. A tuple is write-protected.
The set in Python and the collection in Java are not a concept, this is the problem of translation, Python is a collection of unordered, non-repeating set of elements, similar to the concept of a set of mathematics, it can be intersection, and, difference, complement and other logical operations. The syntax for common collections is: S=set ([' A ', ' B ', ' C '). However, the set has changed a lot in Python3.0, and the syntax for creating a collection has become: s={1,2,3}, with curly braces, similar to the dict to be mentioned behind. If you pass in a repeating element in set, the collection will automatically merge it.
The arithmetic operators provided by Python + 、-、 *,/,%, + =, power (* *), and divisible (//). comparison operator = = = > < >= <=, there is also an IS operator to determine whether two variables point to the same memory space, plus a b?x:y.
The logical operators of the python are significantly different from Java, and Python uses the keyword and or not instead of the logical operators in the Java language && | | !, moreover, the operands that participate in the logical operators in Python are not limited to Boolean types, and any type of value can participate in the logical operation.
16 The general form of the connotation of the list is as follows, we can write the connotation of the list within [] as one line or as many lines. [Expression for item1 in sequence 1 ... for itemn in sequence N if condition expression]
17 There are several namespaces available in a Python program anywhere. Each function has its own namespace, called the local namespace, which records the variables of the function, including the parameters of the function and locally defined variables. Each module has its own namespace, called the global namespace, which records the variables of the module, including functions, classes, other imported modules, module-level variables, and constants. There is also a built-in namespace, which can be accessed by any module, which holds built-in functions and exceptions.
18 when a line of code is going to use the value of the variable x, Python will go to all available namespaces to find the variable, in the following order: 1. Local namespace-specifically, the method of the current function or class. If the function defines a local variable x, Python uses that variable and then stops the search. 2. Global namespace-specifically, the current module. If the module defines a variable called X, a function or a class, Python will use that variable and then stop the search. 3. Built-in namespaces-Global for each module. As a final attempt, Python will assume that X is a built-in function or variable. If Python cannot find X in these namespaces, it will discard the lookup and throw a Nameerror exception, passing there is no variable named ' x ' such a piece of information, like many things in Python, that the namespace can be run directly Access. In particular, local namespaces can be accessed through the built-in locals function. Global (module level) namespaces can be accessed through the GLOBALS function
Python is a dynamic language, and variables do not need to be defined before they are used, so you can not add member variables to the class definition, but add them dynamically at run time.
The Python class provides something like the C # constructor: __init__ (note that there are two underscores before and after), the class will call this function first when instantiated, and we can do the initialization of variables by overriding the __INIT__ function. Unlike C #, Python does not support parameterless initialization functions, and you need to specify at least one parameter for the initialization function, the object instance itself (self).
Python supports static methods as well as C #. In C #, you need to declare a static method with the keyword static, and in Python it is implemented by the static method modifier @staticmethod.
221 python files are called a module, and a bunch of module form a package. For example, the XML folder in the Lib directory is a package that is used to complete the application development of XML. There are several sub-packages in the XML package: DOM, sax, etree, and parser. File __init__.py (note Two) is the registration file for the XML package, and if it does not, Python will not recognize the XML package. The XML package is defined in the system dictionary. The package must contain at least one __init__.py file, and the contents of the file can be empty. __init__.py is used to indicate that the current folder is a package.
The built-in properties of the Python, __name__ __doc__ __file__ __package__

Python trivial syntax

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.