Python Learning Notes Series----(i) Python introduction

Source: Internet
Author: User
Tags floor division

One months ago, just as determined to learn the system under the Python, although the previous study of Java, learned C + +, but also more skilled use of Java to do automated testing to understand the business logic in C + +, but actually there are so many things themselves still unclear, today determined, Began the python system of learning, before I have seen Liu Co tutorial, read the harvest is really less, but the total feeling is less what, behind the thought of the next, I think it should be their own learning method ~ ~ ~ between learning RF experience, I think it's really a good way to learn the official documents for someone who wants to learn python, and then I'm going to stick with python2.7.11 's learning notes.

Today, I learned the third chapter of Python's official website, the informal introduction of Python, the actual is still a little harvest ~~~https://docs.python.org/2/tutorial/introduction.html

1.1 Using Python as a calculator

After the python is installed, in the DOS window, after entering Python, enter Python interactive mode, you can use Python to make a calculator (and quite powerful)

2 2 4  - 5*6>>> (5.0*645.0  85.01.627  27

This kind of simple operation above, I probably have some understanding before, but there are 2 points to note:

First: Division/Operation: The data type after division is determined by the operand, if it is a division of data of type int, the use of division is that floor division Gets the return value of int, if one operand is of type float, The classic division is used to get the float type. For example:

 - 3  int int int 5  - 3.0  int float float 5.666666666666667

Second: Division//Operation: The data type after division is also determined by the operand, using floor division. General pick-up is used "//"

Third: The last expression can be replaced with an underscore. This feature makes Python a better use for a calculator.

12.5  - 100.50>>> price * tax12.5625>>> price + _113.0625  2)113.06

1.2 Strings

The string object is enclosed in single or double quotation marks, and is often seen in the following pattern:

>>>'Spam eggs'# Single Quotes'Spam eggs'>>>'doesn\ ' t'# Use \'To escape the single quote ..."doesn ' t">>>"doesn ' t"# ... or useDoublequotes instead"doesn ' t"

There are also some general operations of string objects, such as: concatenation of strings, according to the string index (integer, or negative) to take a single character, the string is sliced operation to take a substring, the character of the string immutable character, the operation of the string (repeating operator: *, concatenation operator: +, But not for variables of type string).

A. What happens when a character in a string is not an escaped character? Like what

>>> print ' C:\some\ame ' prints what results? That must be

      C:\some         ame
Apparently it's a newline symbol, but the actual author's willingness is not, and adding r in front of the string actually solves the problem.
  >>> print r ' C:\some\ Name '  C:\some\name 
B. How do you express a beautiful string that needs to be printed in multiple lines?
 a = 1   b  = 2   str  = " loleina   "  print   '   a is %
    
     d b 
     is %d c  is %" " % ( A,B,STR)  

There are 2 ways, one line of expression, in the need to break the line with \ n line, the second is to use the "..." format to display multiple lines of the problem, which increases the readability of the code.

1.3 Unicode Strings (Python2 series only)

Starting with python2.0, there is a new data type of Unicode Strings, but in the advent of Python3, the concept has been weakened. The default encoding format for python2.* is ASCII, and python3.* 's default encoding format has been replaced with Unicode, so 2.* is still necessary to mention it. In the Python2 series, you can manipulate non-ASCII characters by using this object. It can be arbitrarily converted to strings. For example, there is now a string variable s, save Chinese characters "test", now direct printing will be error, because there is a non-ASCII character, python2.* can not be parsed, if you add a U, it will be displayed normally (within the integrated development tool, Set the default encoding for integration tools and engineering to UTF-8)

#-*-CODING:GBK-*-if __name__=="__main__":    ' test  '    = S1.decode (' gbk ') = Unicode (S1,     ' GBK ' )    print S1

As above code snippet in JetBrains pycharm 2016.1.2, use 2.7.11 to do the interpreter, did a small test ~ ~

1. Do not set the source file encoding format, input Chinese, after direct printing, will prompt the existence of ' NON-ASCII ', compilation does not pass

2. Set the source file encoding format to GBK, after entering Chinese, printing garbled

3. Set the source file encoding format to GBK, enter chinese s1 = U ' Test ', print normal

4. Set the source file encoding format to GBK, enter Chinese, first decode the string decode or Unicode method, after printing normal

5. Set the source file encoding format to Utf-8, enter Chinese after the direct output normal

6. Set the default code for the tool and project to GBK, after entering Chinese, the print is normal.

And then a little bit clear ~ ~ Declare a non-ASCII string in the IDE of the Python2 series, do not declare the encoding format of the source file, compile is not passed, because the source file Python2 parser tries to use the default ASCII code to compile, but found that there is a non-ASCII code string If the encoding format of the source file on this basis is GBK, the compilation can be passed, but the printed string is garbled, why is it garbled? Because the IDE is using UTF-8 encoding format, and the source file is in the GBK format, so there will be garbled in Chinese, simple to solve garbled there are 3 possible ways, one way: the source files need to print the string decoded to Utf-8 (or Unicode) and then print Another method is to modify the encoding format of the source file directly to Utf-8, and the last is to modify the IDE encoding format to GBK; The second method is obviously the best method.

For still do not understand, God horse is encoded format, ASCII code, unicode,utf-8,gbk,gb2312, these are the coding format between the practice and the difference is what, I recommend can look under the Liu Co 2.7 Tutorial under the string and coding this article, after reading should be something to be harvested. The main content of this chapter is the above part, not to be continued ~ ~

Python Learning Notes Series----(i) Python introduction

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.