Basic learning of Python (i)

Source: Internet
Author: User
Tags bitwise integer division natural string

Python features (slightly, find a lot of online, but I think to use later to summarize his characteristics)

First, the basic concept

1. There are four types of Python: integers, long integers, floating-point numbers, complex numbers

integers, such as 1

Large integers with long integers

Floating-point numbers such as 2.31,3e-2

Complex numbers such as 1+2J, 1.1+2.2j (PS:

shaped likeis called the plural (complex number), which specifies that I is the imaginary unit, and(A, B is any real number)we will be pluralReal part of a complex Z is called Rez=athe imaginary part of the real number B, called the complex Z, is recorded as imz=b. Imaginary parts.

2. String (sequence of characters)

Single quotes in Python are identical to double quotes, such as Var str= "123" is equivalent to var str = ' 123 '.

Use three quotation marks ("' or" ") to specify a multiline string.

Escape character: ' \ '.

Natural string, by adding R or R in front of the string. such as r "Hello world \ n" will be considered part of the string, rather than the newline escape character.

Python allows processing of Unicode strings, prefixed with u or u, such as U "Hello world".

The string is immutable. ( similar to Java string constant pool, memory optimized )

In the literal sense of cascading, such as "This" and "is" "a" "pig" will be automatically converted to this is a pig.

3. Rules for identifiers

The first character must be a letter or an underscore ' _ ', not a number.

Identifiers are case sensitive.

4. Objects

Everything is object in a Python program.

5. Logical line and physical line

A physical line is a line of text that we can see when we write the program, and the logical line is what Python sees.

Python uses semicolons to identify the end of a logical line, but in practice each physical line is typically written with only one logical line, which avoids the use of semicolons.

A logical line can be written in multiple physical rows, as follows:

s = "Peter is  a dog"

The above \ is known as an "explicit row connection", as well as:

Print "Peter"

6. Indent

White space in Python is very important, the beginning of the white space is the most important, also known as indentation. Whitespace (spaces and tabs) at the beginning of a line is used to determine the indentation level of logical lines, which determines the statement
Group. This means that statements at the same level must have the same indentation, and each set of such statements is called a block. (PS: Do not use a mix of spaces and tabs to indent because they do not work when crossing different platforms )

Second, operator and expression

1. Operators and their usage
Operator Name Description Example
+ Add Two objects added 3 + 5 get 8. ' A ' + ' B ' gets ' ab '.
- Reducing Get negative numbers or one number minus another number -5.2 Gets a negative number. 50-24 get 26.
* By Two number multiplied or returns a string that is repeated several times 2 * 3 gets 6. ' La ' * 3 get ' lalala '.
** Power Returns the Y power of X 3 * * 4 gets 81 (i.e. 3 * 3 * 3 * 3)
/ Except x divided by Y 4/3 get 1 (integer division to get an integer result). 4.0/3 or 4/3.0 get 1.3333333333333333
// Take the Divide Returns the integer part of the quotient 4//3.0 Get 1.0
% Take the mold Returns the remainder of a division 8%3 gets 2. -25.5%2.25 gets 1.5
<< Move left Shifts a number of bits to the left by a certain number (each number in memory is represented as a bit or binary number, i.e. 0 and 1) 2 << 2 get 8. --2 is represented as 10 by bit
>> Move right To shift a number of bits to the right by a certain number >> 1 got 5. --11 is represented as 1011 by bits, and 1 bits to the right is 101, which is the decimal 5.
& Bitwise-AND Number of bitwise AND 5 & 3 Get 1.
| Bitwise OR The number of bitwise OR 5 | 3 Get 7.
^ Bitwise XOR OR The bitwise XOR of the number 5 ^ 3 Get 6
~ Rollover by bit The bitwise rollover of X is-(x+1) By 6.
< Less than Returns whether x is less than Y. All comparison operators return 1 for true, and return 0 for false. This distinction is equivalent to the special variable true and false. Note that these variable names are capitalized. 5 < 3 returns 0 (that is, false) and 3 < 5 returns 1 (that is, true). The comparison can be arbitrarily connected: 3 < 5 < 7 returns TRUE.
> Greater than Returns whether x is greater than Y 5 > 3 returns TRUE. If the two operands are numbers, they are first converted to a common type. Otherwise, it always returns false.
<= Less than or equal Returns whether x is less than or equal to Y x = 3; y = 6; X <= y returns True.
>= Greater than or equal Returns whether x is greater than or equal to Y x = 4; y = 3; X >= y returns True.
== Equals Compare objects for Equality x = 2; y = 2; x = = y returns True. x = ' str '; y = ' StR '; x = = y returns false. x = ' str '; y = ' str '; x = = y returns True.
!= Not equal to Compares two objects for inequality x = 2; y = 3; X! = y returns True.
Not Boolean "Non" If x is true, returns false. If x is False, it returns true. x = True; Not Y returns false.
and Boolean "and" If x is False,x and Y returns false, it returns the computed value of Y. x = False; y = True; X and Y, which returns false because X is false. In this case, Python does not calculate y because it knows that the value of this expression is definitely false (because X is false). This phenomenon is called short-circuit calculation.
Or Boolean "or" If x is true, it returns true, otherwise it returns the computed value of Y. x = True; y = False; X or Y returns true. Short-circuit calculations are also available here.
2. Operator precedence (low-to-high)

Operator Describe
Lambda Lambda expression
Or Boolean "or"
and Boolean "and"
Not X Boolean "Non"
In,not in Member Test
Is,is not Identity testing
<,<=,>,>=,!=,== Comparison
| Bitwise OR
^ Bitwise XOR OR
& Bitwise-AND
<<,>> Shift
+,- Addition and subtraction
*,/,% Multiplication, division and redundancy
+x,-x PLUS sign
~x Rollover by bit
** Index
X.attribute Property Reference
X[index] Subscript
X[index:index] Addressing segment
F (Arguments ...) Function call
(Experession,...) Binding or tuple display
[Expression,...] List display
{Key:datum,...} Dictionary display
' Expression,... ' String conversions

Basic learning of Python (i)

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.