Python learning notes (0) and python learning notes

Source: Internet
Author: User

Python learning notes (0) and python learning notes
What type of language is Python?

Python is a scripting language.

Python:Https://www.python.org/downloads/

Python version: Python 3.4.2-64bit

Scripting language (Scripting language) is a computer programming language. Therefore, it can also allow developers to write programs that let computers listen to their lives. It is usually an important principle to create a script language to quickly complete some complex tasks in a simple way. Based on this principle, this makes the scripting language easier than system programming languages such as C, C ++, and Java.
In addition, the script language has some other characteristics:

  • Syntax and structure are usually relatively simple
  • Learning and using are usually relatively simple.
  • Generally, the "explanation" of programs that are easy to modify is used as the running mode, without the need to "compile"
  • Program development capacity is superior to running performance

    A script can automate the operations that would have to be performed on the keyboard. A Shell script is mainly composed of commands that need to be input in the command line, or in a text editor, you can use scripts to combine some common operations into a set of serial. The language used to write such scripts is called the script language. Many scripting languages actually exceed the serial commands of simple USER commands and can write more complex programs.

     

    What is IDLE?

    IDLE is a Python Shell, which means "shell". Basically, it is a way to interact with the program by typing text! Like the Windows cmd window and the Linux black command window, they are all shell. With them, we can issue commands to the operating system. Similarly, we can use the IDLE shell to interact with Python. After installing Python, search for the IDLE directly in the search box.

    Open IDLE and see ">>>". The prompt "it" indicates that Python is ready and you are waiting to type the Python command.

    We can try to enter the command:

    >>> print("I love Python")

     

    As you can see, the Python IDLE directly prints the string I love Python

     

    You can try the wrong command:

    >>> System.out.println("I love Python");Traceback (most recent call last):  File "<pyshell#1>", line 1, in <module>    System.out.println("I love Python")NameError: name 'System' is not defined

    I found that the IDLE directly reported an error to us and kindly prompted us that NameError: name 'system' is not defined, which is not defined by System.

     

    Try to type the syntax of Python2:

    >>> print "I love Python"SyntaxError: Missing parentheses in call to 'print'

    It will tell us that it is an incorrect syntax.

     

    Enter a lot of incorrect information and try to enter the correct information, such as calculation:

    >>> print(5 + 8)13

     

    In IDLE, print computation, or even print without the print () function. You can directly input a number for computation, such:

    # Addition> 5 + 813 # subtraction, and negative output> 5-8-3 # subtraction, the output is a positive number> 9-54 # multiplication> 2*36 # Division, and the output is a floating point decimal number> 9/33.0 # If you want to output an integer, you need to use two delimiters "//"> 9 // 33

     

    Not only can it be used as a calculator, but it can also handle very large numbers:

    >>> 1234567890123456789 * 987654321098765432112193263113702179522374638011112635269

     

    It can also be used to add strings:

    # Adding strings> 'I' + 'love' + 'python' IlovePython '# It's ugly to add strings, we add a space after the word> 'I' + ''+ 'love' +'' + 'python' + '! 'I love Python! '# Adding Chinese and English >>> "I" + "" + "Python"' I love python'

     

    Python is not only simple to concatenate and add strings, but also can be multiplied by strings:

    >>> 'I love Python! ' * 3'I love Python! I love Python! I love Python! '

     

    Of course, not all strings can be multiplied. For example, if a string is multiplied by a string, an error is returned. Python is not as good as Python. An error is returned for the Division and subtraction of strings:

    # When adding strings and numbers, the following error occurs:> 'I love python' + 5 Traceback (most recent call last): File "<pyshell #7>", line 1, in <module> 'I love python' + 5 TypeError: Can't convert 'int' object to str implicitly # multiply a string by a string, and an error is returned >>>' I love Python! '* 'Love' Traceback (most recent call last): File "<pyshell #40>", line 1, in <module>' I love Python! '* 'Love' TypeError: can't multiply sequence by non-int of type 'str' # returns an error >>>' I love Python! '-'Love' Traceback (most recent call last): File "<pyshell #41>", line 1, in <module>' I love Python! '-'Love' TypeError: unsupported operand type (s) for-: 'str' and 'str' # returns a string Division Error >>>' I love Python! '/3 Traceback (most recent call last): File "<pyshell #42>", line 1, in <module>' I love Python! '/3 TypeError: unsupported operand type (s) for/: 'str' and 'int'

    Why >>> print ('I love fishc.com' * 5) can be executed normally, but >>> print ('I love fishc.com' + 5) returns an error?

    In Python, we cannot add two completely different things together, such as numbers and text, for this reason. >>> print ('I love python' + 5) will report an error. This is like saying, "what will happen if I add 5 to the sky ?" The same does not make much sense! However, doubling by an integer makes some sense. The example of the front side is to print the "I love Python" string five times.

     

     

    What is the role of print?

    After talking so much, I forget print (). So what is print?

    Print () displays some text in the output window. We can enter help (print) in IDLE for help.

    Among them, the built-in Methods sep and end of print are useful.

    Sep--- Insert a value between strings. The default value is a space, for example:

    # In print, each string is separated by commas (,). The default value is a space> print ('I am the first string' and' I am the second string ') I am the first string and I am the second string # if I want to leave no space in the two strings, I can use sep> print ('I am the first string ', 'I am the second string', sep = '') I am the first string I am the second string # Of course, you can also use other text to separate the string. >>> Print ('I am the first string',' I am the second string', sep = '--- my presence is to separate you ---') I am the first string-my presence is to separate you-I am the second string # Use commas to separate strings. to make it more effective, we estimate that a lot of commas are added. Print ('I', 'love', 'python', sep = ',') I, love ,,,,, python # \ n is a linefeed >>> print ('I am the first string',' I am the second string', sep = '\ n ') I am the first string and I am the second string

     

    End--- Append a value to the end of the string. The default value is a line break, for example:

    # In IDLE, input print and press enter to directly output the data. If a semicolon is used, you can write two commands in the output line. Is the Python IDLE very convenient? >>> Print ('I am in the first line! '); Print (' I'm in the second line! ') I am on the first line! I am on the second line! # If end is not added, the last value is \ n by default, but if end = ''is added, \ n is removed and an empty string is changed, so the two prints can be displayed on one line. Print ('I am in the first line! ', End = ''); print (' I am in the second line! ') I am on the first line! I am on the second line! >>> Print ('I am in the first line! ', End =' --- do not let the line break --- '); print (' I am in the second line! ') I am on the first line! --- Do not let the line feed --- I am in the second line!

     

    To embed a double quotation mark in a string, you can do this:

    # When we add the string "Double quotation marks" to "Double quotation marks", Python will think that this paragraph is over and that the next "Double quotation marks" will start to input the next paragraph, therefore, 'Ta' prompts you "syntax error"> print ("Python I like" it "very much. Do you like it? ") SyntaxError: invalid syntax # You can use a backslash to comment out" Double quotation marks "> print (" I like \ "\" very much in Python. Do you like it? ") I like" Python "very much. Do you like it? # You can enter a string in 'single quota', and then use double quotation marks to add the "Double quotation marks" to the string >>> print ('python I like "it" very much ", do you like it? ') I like "it" very much in Python. Do you like it? # Of course, you can also enter single quotation marks> print ("What's your name? \ NMy name's Python ") What's your name?
    My name's Python

     

    Input >>> 'python' is different from input >>> print ('python?

    Directly input the result and type to print it to the screen, while print prints the result to the screen. Try it and observe the result!

    # The output result is enclosed by quotation marks.> 'python' # The output result is not enclosed by quotation marks.> print ('python') Python

     

    In the text editor, the problem above is obvious. For example, in IDLE, press Ctrl + N to open the text editor.

    Print 'python' directly, click F5 to run, and find that there is nothing in IDLE

    After print () is added, print it out.

    Set Environment Variables

    Set the environment variables of your operating system so that you can easily access the Python Environment

    Anti-key computer-> properties-> advanced system settings-> environment variables-> system variables select "Path"-> edit-> Add the Python installation directory at the end of the variable value

    Why should we use Python3?

    Why should we use Python? What is the problem with Python2? It seems that many programmers are still using Python2?

    Quite a few programmers are using Python2, but Python3 is the future of Python development, just like XP and WIN7. In fact, the new features in Python3 are really amazing and worth further study! I feel that reading the python 3 and Python 2 code is not a problem for you!

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.