Python entry notes 01 and python entry 01

Source: Internet
Author: User

Python entry notes 01 and python entry 01
☆Write and execute the code in the Python interactive environment:-enter Python in cmd. If you see >>>, you can enter the Python code and press enter to execute the code. -If you do not see it, there are two possible reasons: 1. the Python environment is not installed. Please download it from the Python official website (www.python.org. 2. This is because Windows will search for Python.exe (Python environment) based on the Path set in the "environment variable" of "Path ). If not, an error is returned. If "Add Python 3.X to PATH" is missing when installing the Python environment, manually Add the Path of Python. exe to "PATH. If you do not know how to modify the environment variables, we recommend that you re-run the Python environment Installer. Remember to check "Add Python 3.X to PATH 」.I. Variables and strings 1.1 Python Variables-Create a variable in Python, such as var = 123 ☆note: Python is case sensitive. For example, variable A and variable a are not the same variable. A semicolon (;) is not required at the end of a Python statement. make sure that Python is readable. # Example: Create a variable var with a value of 123 and output the variable var: var = 123 print (var) # example: open the file in the specified path and add content to the file, then read the content of the file and print: file = open ('e:/PY_Test/file.txt ', 'a +') file. write ('Hello World! + ') Content = file. read () file. close () print (content ); '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''★Different modes in the open () function: r can only be read; r + can be read and written, and no non-existent files are created. Writing from the top will overwrite the content in the previous position; w can only be written, overwrite the entire file. If the file does not exist, create the file. If the file exists, overwrite the entire file. If the file does not exist, create the file; a + is readable and writable. It reads content from the top of the file and adds content from the bottom of the file. If no content exists, it is created. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''1.2. Python string-Any text between double quotation marks, single quotation marks, or three quotation marks, for example, "abcdefg" 'hijklmnop' ''' feiojgioejgoiegjeoijg '''. ☆Note: The three quotation marks (''') are used for long texts or instructions. You can wrap them as long as they are not completed. -Python string merging:★The string merging in Python is the same as that in Javascript, with the + sign. # Example: st01 = 'abc' st02 = 'defg' stR = st01 + st02print (stR) # output: abcdefg ☆note: different Data Types in Pyhton cannot be merged, but some methods can be used for conversion. If you do not know the type of the variable, you can useType ()Function to view the type. For example, the forced type conversion of print (type (word) Pyhton: # example: num = 1st = '1' print (num +Int (St)) # Output: 2 print (Str (Num)+ St) # output: 11-Python string copy:★In Python, not only can strings be merged by 'add' (I .e. +), but also the string can be copied by 'multiplication '(I .e. # Example: st = 'Good! '* 3 print (st) # output: Good! Good! Good! ☆Note: because the Chinese notes will cause an error, you need to add a line of magic note # coding: UTF-8 at the beginning of the File, you can also find in the settings "File Encodings" set to UTF-8. -Shards and indexes of Pyhton strings:★In Python, strings can be indexed and sharded using string [x], that is, a [] is added. The slice of a string can be seen as finding out what you want to intercept from the string, copying the length of a short segment, and storing it in another place, instead of modifying the source file string. Each string obtained from the shard can be considered as a copy of the original string. (Similar to C) # example: name = 'My Name is Jay 'print (name [0]) # output: Mprint (name [-3]) # output: jprint (name [8:12]) # output: is Jprint (name [8:13]) # output: is Japrint (name [5:]) # output: me is Jayprint (name [: 5]) # output: my Na '''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''★The index location of each character in 'My Name is Jay 'In this string is as follows: ── ┬ ┐ │ M │ y │ N │ a │ m │ e │ I │ s │ J │ a │ y │ ── ┼ ── │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │ 12 │ 13 │ ── ┼ ── ┼ ┤ │-14 │-13 │-12 │-11 │-10 │-9 │-8 │-7 │-6 │-5 │-4 │-3 │-2 │-1 │ ── ┴ ── ┴ ── ┘ name [0] Table The following is a string of 0 characters: M name [-3], indicating that the number is-3, that is, the last 3rd characters, that is: J name [] indicates that the intercepted number starts from 8th characters and ends with a position of 12th characters but does not contain 12th characters, that is, is J name [5:] this statement represents the string fragment from the character number 5 to the end, that is, me is Jay name [: 5] indicates the part of a character starting from the character number 0 to the character number 5 but not containing 5th characters. That is, My Na may be easy to confuse, it can be imagined that [5:] is from the 5 to the last, and the programmer is too lazy to count the number of characters, so it is omitted to write. [: 5] is from the top to the top of the 5, it is also too lazy to write 0, so it is written as this. For example: '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''' # Another example: word = 'ds Ds' find _ the_evil_in_your_friends = word [0] + word [] + word [-3:-1] print (find_the_evil_in_your_friends) # output: fiend-Python's string Method: Python is an object-oriented programming language, and objects have various functions and features. The terminology is called ---- Method ). To facilitate understanding, we assume that cars in daily life are "objects", that is, cars. As we all know, cars have many features and functions. "Open" is an important function of cars, so cars use the "open" function, in Python programming, we can express it as follows: car. drive () Example: (The calling method in Python is similar to Javascript) # in this example, use * to hide the number ranging from 4th to 9th in the phone number: phoneNum = '000000' new _ phoneNum = phoneNum. replace (phoneNum [], '* 6) print (new_phoneNum) # output: 131 ******* 89 ☆note: it can even be like the following, use the 'string' to call the method: print ('aaabbbccc '. replace ('bbb ','-'* 3) # output: aaa --- ccc ''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''★Replace () method of the Pyhton string: in the brackets of the replace () method in the above example, the first parameter phone _ number [] indicates the part to be replaced, the following parameter '* 6 indicates the character to be replaced, that is, multiply * by 6 to display 6 Characters *. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''' # Example, simulate the phone number Association function: search = '000000' num01 = '000000' num02 = '000000' num03 = '000000' print (search + 'in' + num01 + '+ str (num01.find (search) + 1) + 'bits to '+ str (num01.find (search) + len (search) + 'bit ') print (search + 'in '+ num02 +' + str (num02.find (search) + 1) + 'to' + str (num02.find (search) + len (search) + 'bit') print (search + 'in' + num03 + ',' + str (num03.find (search) + 1) + 'bitwise to nth '+ str (num03.find (search) + len (search) + 'bit') # output: #168 to 16800300612 bits in 1st # 3rd to 168 bits in 13501680299 # 5th to 7th bits in 168 to 16700700168 bits in 9th '''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''★Find () method of the Pyhton string: find the location of the specified string in this string.★The len () method of Pyhton: calculates the object length and returns it as an integer. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''' # Example: print ('{} a word she can get what she {}. '. format ('with', 'came') print ('{preposition} a word she can get what she {verb} '. format (preposition = 'with', verb = 'came') print ('{0} a word she can get what she {1}. '. format ('with', 'came') # output: # With a word she can get what she came. # With a word she can get what she came. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''★Pyhton string format () method: replace {}, {number}, or {string} In this string with the string specified in the parameter of format () method. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''' # String method format () usage: print ('{}{}'. format ('hello', 'World') # do not set the specified location, in the default order. Print ('{0} {1}'. format ('hello', 'World') # Set the specified location. Print ('{1} {0} {1}'. format ('hello', 'World') # Set the specified location. Print ('{a} {B} {a}'. format (a = 'hello', B = 'World') # Set the specified location. # Output: # hello world # world hello world # hello world hello # example. Fill in the blank City Data in the URL using the string format () method: city = input ("write down the name of city:") url = "http://apistore.baidu.com/microservice/weather? Citypinyin = {}". format (city) print (url) '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''★Pyhton input () method: used to receive input from the console. In Python3, input () treats all input as strings and returns the string type. Input ('parameter') parameters are displayed as prompts. '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''' ''' # Input () the method is as follows: name = input ('enter Your Name: ') print ('your name is' + name) # output: # enter Your Name: enter Your Name # Your Name is the Name you entered

 

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.