The difference between Python's base 2 and 3, and the For and while loops

Source: Internet
Author: User
Tags logical operators

1, the difference of Python2\python3: Python2\python3 difference:
One, code duplication, Python2 code repetition function reuse, inclusion of other language parts, not standardized
Python3 to be integrated, simple and beautiful
Compile: Compile all the code into binary once and then run it again.
Cons: Low development efficiency, no cross-platform

Interpreted: When the program starts executing, the code is interpreted as a line of binary, executed.
Cons: Low development efficiency

Python is a bit:
1, the development of high efficiency, by the third-party library directly call, such as PHP can not connect
2. Portability
3, expandable, embeddable High: Join the C language speed to execute fast

Types of Python:
CPython: The most widely used, first converted into C bytecode, to the system
Jpython: Byte code converted to Java, to system
Other languages Python: all the same, convert to the corresponding byte code
PyPy: The code is converted to bytecode all at once and then to the system

Second, the Python2 uses the ASCII, does not recognize the Chinese: Python3 uses the utf-8 character
Three, Python2 has a long int i 3 no
Four, python2 in print can not add () but 3 to add ()
Five, 2 is raw_input equals 3 of input

2. Variables: Variables:
The results of the operation in the program are charged to the memory for easy follow-up transfer:
Cannot start with a number, cannot be a python keyword, and a digital letter underline is arbitrary
Variables to have a gram of descriptive
Variable do not use Chinese
Variable cannot be too long
A = 1
b = 2 .....
All can
3, constant: constant:
Constant amount: π, xxx number
All uppercase variables are constants and placed at the top of the file
Opent_work = 323423, this is the constant
4. Note: note:
Single Use #
Multiple lines "" "" "" "or" "

5. Basic Data type:
int numeric type calculation. Algorithms such as subtraction

STR: string is quoted in quotation marks.
What data type is a type (object)
Useful: Store simple small amounts of data
Large range with double quotes, small range with single quotes
Multiple lines can use three quotes, such as product list, multi-line
can add + string concatenation, and multiply str string numeric type

Boolean value,
True and Flase
0 for Flase The others are true
6, Input:
Interacting with the user, input comes out of a string
Input ("description information")

7. Formatted output:
% placeholder%s string%d number
If you use%, you must escape the percent in addition
If you want to convert a string to a number, you can wrap it with an int (variable), and the string must be all numbers.

Name = input ("name")
Age = Input ("Age")
Job = input ("job")
Hobby = input ("hobby")

Meg= "" "------------info of%s-----------
Name:%s
Age:%d
Job:%s
Hobbie:%s
-------------End-----------------
"" "% (name,name,int (age), Job,hobby)
Print (MEG)

8. If statement:

If condition:
Results
Here is an example of a TAB key or four spaces in the result here
Two Equals is the comparison = =, one = is the assignment

If condition:
Meet conditional execution Code
Elif Conditions:
The above conditions are not satisfied.
Elif Conditions:
The above conditions are not satisfied.
Elif Conditions:
The above conditions are not satisfied.
Else
All the above conditions are not satisfied, go this part.

9. While
A = 1
Flag = True
While flag:
Print (a)
A = A+1
If a ==5050:
Flag = False

Note: The flag here is used for two loops to jump out of a stop
Break: Jump out of the loop with his column
Continue: Ends this loop, but also executes

10. Logical operators:
And Or not

() "Not" and "or
Priority level
1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

11. Code:
Electrical signal 01010101010101010
Computer:
A ' SIIC code 128
7-bit identification of one byte
Upgrade to 8 bits a byte 256 2 of the 8-time Square
00000001 = = = one character
Universal code AH: Unicode includes all countries
16 bytes Represents a character
00000001 00000001 = = 2 of a character 16 Times Square

Change: 4 byte table one character is 32 times
11111111 11111111 11111111 11111111 represents a byte 2 of the 32-time Square
Waste of resources:

Upgrade Utf-8: Minimum 8-bit:
English 8 bit one byte, 00000001
Europe 16 bits Two bytes, 00000001 00000001
Asia 24 bits Three bytes, 00000001 00000001 00000001

Conversion of units:
8bit ==1bytes
1024bytes ==1kb
1024KB ==1MB
1024MB = = 1G

Utf-16 a minimum of 16 characters

Underlying data type:
Dict Dictionary
dict:{' name ': ' xxx ', ' Age ': 13}
Print (dict[' name '])
can print out xxx
Key values can be numbers and strings, lists, etc.

The difference between Python's base 2 and 3, and the For and while loops

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.