Python Basics Grammar

Python supports five basic numerical types, three of which are integer types.

● int (signed integers)

 long (long integers)

 bool (Boolean values)

● float (floating point real numbers)

● complex (complex numbers)

1. addition, subtraction, multiplication, division

+ - * / // % **

/:  classic division, if the operands are both integers, it will perform floor division, while for floating point numbers, it represents true division. If true division is enabled, then the division operator will always perform that operation, regardless of operand types.

//:  "floor" division (rounds down to nearest whole number).

A new division operator ( // ) has been created that carries out floor division: it always truncates the fraction and rounds it to the next smallest whole number toward the left on the number line, regardless of the operands' numeric types.

** : exponentiation operator, the double star/asterisk.

2. two "not equal" comparison operators:

!= and <> 

The latter is slowly being phased out, so we recommend against its use.

 3. and or not

 expression conjunction operators:

4. String

Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes.

字符串可单引号也可双引号。

Triple quotes (three consecutive single or double quotes) can be used to escape special characters. (原样输出)

Subsets of strings can be taken using the index ( [ ] ) and slice ( [ : ] ) operators, which work with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.  (左闭右开区间)

print a[-1]: 输出最后一个字符

字符串从0开始计时。

The plus ( + ) sign is the string concatenation operator

asterisk ( * ) is the repetition operator. Here are some examples of strings and string usage。 (字符串重复)

 5.  Lists and Tuples

Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed. Tuples are enclosed in parentheses ( ( ) ) and cannot be updated (although their contents may be). Tuples can be thought of for now as "read-only" lists.

Subsets can be taken with the slice operator ( [] and [ : ] ) in the same manner as strings.

6. Dictionaries

Dictionaries (or "dicts" for short) are Python's mapping type and work like associative arrays or hashes found in Perl; they are made up of key-value pairs. Keys can be almost any Python type, but are usually numbers or strings.

Values, on the other hand, can be any arbitrary Python object.

Dicts are enclosed by curly braces ( { } ).

 7. Statements and Syntax

Some rules and certain symbols are used with regard to statements in Python:

● Hash mark ( # ) indicates Python comments

● NEWLINE ( \n ) is the standard line separator (one statement per line)

● Backslash ( \ ) continues a line

● Semicolon ( ; ) joins two statements on a line

● Colon ( : ) separates a header line from its suite

● Statements (code blocks) grouped as suites

● Suites delimited via indentation

● Python files organized as modules

The following are defined as having false values in Python:

● None

● False (Boolean)

Any numeric zero:

● 0 (integer)

● 0.0 (float)

● 0L (long integer)

● 0.0+0.0j (complex)

● "" (empty string)

● [] (empty list)

● () (empty tuple)

● {} (empty dictionary)

Any value for an object other than those above is considered to have a

true value, i.e., non-empty, non-zero, etc.

 

8. Value comparisons

 

< Less than bool

> Greater than bool

<= Less than or equal to bool

>= Greater than or equal to bool

== Equal to bool

!= Not equal to bool

<> Not equal to bool

Object comparisons

is The same as bool

is not Not the same as bool

Boolean operators

not Logical negation bool

and Logical conjunction bool

or Logical disjunction bool

The following familiar factory functions were formerly built-in functions:

● int(), long(), float(), complex()

● str(), unicode(), basestring()

● list(), tuple()

● type()

9. *Bit Operators (Integer-Only)

Python integers may be manipulated bitwise and the standard bit operations are supported: inversion, bitwise AND, OR, and exclusive OR (aka XOR), and left and right shifting. Here are some facts regarding the bit operators:

● Negative numbers are treated as their 2's complement value.

● Left and right shifts of N bits are equivalent to multiplication and division by (2 ** N) without overflow checking.

● For longs, the bit operators use a "modified" form of 2's complement, acting as if the sign bit were extended infinitely to the left.

The bit inversion operator ( ~ ) has the same precedence as the arithmetic unary operators, the highest of all bit operators.

The bit shift operators ( << and >> ) come next, having a precedence one level

below that of the standard plus and minus operators, and finally we have the bitwise AND, XOR, and OR operators (&, ^, | ), respectively.

~  >   << and >>   >  &, ^, |

Bitwise Operator Function

~num              (unary) invert the bits of num, yielding -(num + 1)

num1 << num2      num1 left shifted by num2 bits

num1 >> num2      num1 right shifted by num2 bits

num1 & num2             num1 bitwise AND with num2

num1 ^ num2             num1 bitwise XOR (exclusive OR) with num2

num1 | num2             num1 bitwise OR with num2

           

猜你喜欢

转载自www.cnblogs.com/wkwzn2019/p/13167308.html