Python learning notes (continuously updated) and python learning notes

Source: Internet
Author: User
Tags integer division

Python learning notes (continuously updated) and python learning notes

Start:

 

A: python interactive environment B: use Notepad ++ to write A python program and save it as A. py file

1. Data Types in Python: integer, floating point number, String, Boolean (True, False), null (None) None cannot be understood as 0, because 0 is meaningful, none is a special null value.

In addition, Python provides multiple data types, such as list and dictionary, and allows you to create custom data types.

2. print statement

The statement can also keep up with multiple strings separated by commas (,) to form a string of output:

3. Comment

4. Variables

In a Python program, a variable is represented by a variable name. The variable name must be a combination of uppercase and lowercase English letters, numbers, and underscores (_), and cannot start with a number;

In Python, equal sign "=" is a value assignment statement that assigns any data type to a variable. The same variable can be assigned multiple times and can be of different types, for example:

This variable is a dynamic language and corresponds to a static language. The static language must specify the variable type when defining the variable. If the type does not match during the assignment, an error is returned. For example, if Java is a static language, the value assignment statement is as follows (// represents a comment ):

This is why dynamic languages are more flexible than static languages.

Finally, it is important to understand the representation of variables in computer memory. When we write:a = 'ABC'The Python interpreter does two things: 1. Create'ABC'2. CreateaAnd point it'ABC';

5. Define a string

5.1, string can be used''Or""If the string itself contains'What should I do? For example, we want to represent a string I'm OK In this case, you can use" "If the string contains", We can use' 'Including: 'learn "Python" in imooc ';

If the string contains both'Include"What should I do? At this time, some special characters of the string must be "escaped ".\Escape. To represent a stringBob said "I'm OK".Because 'and "May cause ambiguity, we insert\It indicates that this is a common character and does not represent the start of the string. Therefore, this string can also be expressed as: 'Bob said \ "I \'m OK \". 'note: Escape Character \ is not included in the string content. Common escape characters include \ n, which indicates line breaks \ t, which indicates a Tab character \ which indicates the character itself

5.2. raw string and Multiline string in python

If a string contains many characters that need to be escaped, It is very troublesome to escape each character. To avoid this situation, we can add a prefix before the stringrIt indicates that this is a raw string, and the characters in it do not need to be escaped. Example: R '\(~ _~) /\(~ _~) /';r'...'The notation cannot represent a multi-line string or contain'And"To represent a multi-line string, you can use'''...'''Representation: the representation of the above string is exactly the same as the following: 'line 1 \ nLine 2 \ nLine 3' can also be added before the multi-Line string r To convert the multi-line string into a raw string:

5.3, Unicode string in python

There is another encoding problem in the string, because the computer can only process numbers. If you want to process text, you must convert the text into numbers before processing. The earliest computer was designed to use eight bits as a byte. Therefore, the maximum integer represented by a word energy saving is 255 (Binary 11111111 = decimal 255 ), 0-255 is used to indicate uppercase and lowercase English letters, numbers, and symbols. This encoding table is called ASCII encoding. For example, the uppercase letter A is encoded as 65 and the lowercase letter z is encoded as 122.

To express Chinese characters, it is clear that one byte is not enough. It requires at least two bytes and cannot conflict with ASCII encoding. Therefore, China has developed GB2312 encoding to encode Chinese characters.

Similar to other languages such as Japanese and Korean. Unicode came into being to unify the encoding of all texts. Unicode unifies all languages into a set of encodings, so that there will be no garbled issues.

Unicode usually represents a character in two bytes. The original English encoding is changed from single-byte to dual-byte. You only need to set the height of all bytes to 0.

Because Python was born earlier than the Unicode standard, the earliest Python only supports ASCII encoding, and the common string 'abc' is inside Python.

Python later added support for Unicode, and the Unicode strings are represented by U'... '. For example, note: Chinese characters cannot be normally displayed without u. In additionuIn addition, there is no difference with a common string, and The Escape Character and multi-line representation are still valid: if the Chinese string encounters UnicodeDecodeError In the Python environment, this is because there is a problem with the format of saving the. py file. You can add comments in the first line to tell the Python interpreter to read source code with UTF-8 encoding. Save it as... with Notepad ++ and save it in UTF-8 format.

Integer and floating point number in python 5.4

Different from mathematical operations, Python's Integer Operation results are still integers and floating-point calculation results are still floating-point: 1 + 2 #=> integer 3 1.0 + 2.0 #=> floating point number 3.0, but the result of the mixed operation of the integer and floating point number becomes a floating point number: 1 + 2.0 #==> floating point number 3.0

Why do we need to distinguish between integer and floating-point operations? This is because the result of integer calculation is always accurate, and the result of floating point calculation is not necessarily accurate, because the computer memory is too large to accurately express the infinite loop decimal places, such 0.1 If it is changed to binary, it indicates an infinite number of loops. Isn't the result a floating point number when Division operations of integers encounter division? Let's give it a try: 11/4 #=> 2 many beginners are surprised that, even if Python's integer division, the result is still an integer, and the remainder is directly thrown away. However, Python provides a remainder calculation % to calculate the remainder: 11% 4 #=> 3 if we want to calculate the exact result of 11/4, according to the "mixed integer and floating-point calculation result is a floating-point number" rule, it is okay to convert one of the two numbers into a floating-point number and then perform the operation: 11.0/4 #==> 2.75

5.4. boolean type in python

We have learned that Python supports boolean data, and onlyTrueAndFalseThere are two types of values, but the boolean type has the following operations: and operation: the calculation result is True only when both boolean values are True.

Or operation: If one Boolean value is True, the calculation result is True. Non-operation: convert True to False, or convert False to True:

Boolean operations are used in the computer for condition determination. Based on the True or False calculation result, the computer can automatically execute different subsequent codes. In Python, The boolean type can perform and, or, and not operations with other data types. See the following code: the calculation result is not a boolean type, but a string 'a = t ', why? Because Python0,Null String''AndNoneIf it is regarded as False, other numeric values and non-null strings are regarded as True. Therefore, to explain the above results, an important rule involving the and or operation is short-circuit calculation.

6. List and Tuple types

6.1 create a list

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 






 

 

 

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.