[Python] Basics

Source: Internet
Author: User

Http://www.pythonclub.org/python-basic/start

Http://my.oschina.net/captaintheron/blog/6437

I. Python variable types

Python has a variable type, and it will forcibly check the variable type.

The built-in variable types are as follows:

# Integer integer_number = 90 # Floating Point float_number = 90.4 # plural complex_number = 10 + 10j # list sequence sample_list = [1, 2, 3, 'abc'] # dictionary sample_dic = {"key": value,} # tuple read-only sequence sample_tuple = (, "AB") # nested sample_nest =, 3), {, 'key': [1, 2]}, 3]
2. Everything is an object
'str str'.split() # ['str', 'str'] # 1..is_integer()   # True # is_integer(1)     # True # 1.0.is_integer()  # False [3,2,1].sort()    # [1,2,3] sort([2,1,3])     # [1,2,3]
Iii. Python program process control condition judgment Structure
flag1 = some_valueflag2 = other_valueif flag1:    do_function_1()elif flag2:    do_function_2()else:    do_function_3()
Loop Structure
for i in range(0, 10):    print(i) for i in ['a','b','c','dd','eee']:    print(i)

Iv. String

Representation Method:
* A string is identified by single, double, or triple quotes.
* Line feed is allowed for strings identified by triple quotation marks.
* Strings identified by single quotation marks or double quotation marks can be split using a backslash.
* Add "R" before the string to invalidate escape characters (for example, "\ n" and "\ t ").
* After adding "U" to the string, it will be defined as a unicode string (Unicode-escape encoding can be used in the string)
* "U" and "R" can be used together and marked as "ur"

OPERATOR:
* You can use "+", for example, "ABC" + "def"
* Two string values are automatically connected (only strings, variables and expressions are not allowed), such as "str" "ing"
* You can use "*", for example, "ABC" * 5.
* Use the function "Len ()" to return the string length

Split (slice ):
* You can use subscript (INDEX) to query a string, for example, "word [4], word [0: 2], word [2:], word [: 4]"
* Subscript starts from 0
* The query string is read-only and cannot be assigned a value using the string index.
* Word [: 2] + word [2:] equals to word
* The index can be negative. If it is negative, the index starts from the right.

#1. copy string # strcpy (sstr1, SSTR2) sstr1 = 'strcpy' SSTR2 = sstr1sstr1 = 'strcp2' print SSTR2 #2. connection string # strcat (sstr1, SSTR2) sstr1 = 'strcat' SSTR2 = 'append' sstr1 + = sstr2print sstr1 #3. search for characters # strchr (sstr1, SSTR2) sstr1 = 'strchr' SSTR2 = 'R' NPOs = sstr1.index (SSTR2) print NPOs #4. comparison string # strcmp (sstr1, SSTR2) sstr1 = 'strchr' SSTR2 = 'strch' print CMP (sstr1, SSTR2) #5. whether the scan string contains the specified character # strspns (sstr1, SSTR2) sstr1 = '000000' SSTR2 = '000000' # sstr1 and chars both in sstr1 and sstr2print Len (sstr1 and SSTR2) #6. string Length # strlen (sstr1) sstr1 = 'strlen' print Len (sstr1) #7. convert lowercase characters in the string to uppercase characters # strlwr (sstr1) sstr1 = 'jcstrlwr 'sstr1 = sstr1.upper () print sstr1 #8. append the specified string # strncat (sstr1, SSTR2, n) sstr1 = '000000' SSTR2 = 'abcdef 'N = 3sstr1 + = SSTR2 [0: N] print sstr1 #9. string specified length comparison # strncmp (sstr1, SSTR2, n) sstr1 = '000000' SSTR2 = '123bc' n = 3 Print CMP (sstr1 [0: N], SSTR2 [0: n]) #10. copy characters of the specified length # strncpy (sstr1, SSTR2, n) sstr1 = ''sstr2 = '000000' n = 3sstr1 = SSTR2 [0: N] print sstr1 #11. string comparison, Case Insensitive # stricmp (sstr1, SSTR2) sstr1 = 'abcefg' SSTR2 = 'abcefg' print CMP (sstr1.upper (), sstr2.upper () #12. replace the first n characters of the string with the specified character # strnset (sstr1, CH, n) sstr1 = '000000' CH = 'R' n = 3sstr1 = N * CH + sstr1 [3:] print sstr1 #13. scan string # strpbrk (sstr1, SSTR2) sstr1 = 'cekjgdklab 'SSTR2 = 'gka' NPOs =-1for C in sstr1: If C in SSTR2: NPOs = sstr1.index (c) breakprint NPOs #14. flip string # strrev (sstr1) sstr1 = 'abcdefg' sstr1 = sstr1 [:-1] print sstr1 #15. search for the string # Python strstr # strstr (sstr1, SSTR2) sstr1 = 'abcdefg' SSTR2 = 'cde' print sstr1.find (SSTR2) #16. split string # strtok (sstr1, SSTR2) sstr1 = 'AB, CDE, fgh, ijk 'sstr2 =', 'sstr1 = sstr1 [sstr1.find (SSTR2) + 1:] print sstr1

 

5. User-Defined Functions

# Statement structure: def variable name (parameter list): Return Value of the statement body

Note:
* A function must be defined before it can be used. That is, the function must be defined before its statements are used.
* When calling a function, add "()" after the function name; otherwise, the address of the function is called.
* If the "return" statement or "return" statement is null in a function, the returned value is "NONE"
* Variable names defined by the event can be directly used in function parameters.
* All the variables in Python are global variables. After the variables are modified in the function, they are modified when used outside the function.
* The variables in the function are generally local variables. However, because the list and dictionary are transmitted with addresses, the original variable content will be modified.
* You can add "Global" to declare a global variable. For example, "Global X"
* You can use "alias .doc" to call the multi-line string of the Start row of the function. Function Description

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.