Python Learning Path The next day

Source: Internet
Author: User

1. Import Module

#! /usr/bin/python

#-*-Coding:utf-8-*-

Import Sys

Print (SYS.ARGV)

2. Common methods of String:

Remove Blank: Strip

Split: Split

Length: Len (obj)

Index: obj[1]

Slice: Obj[1:],obj[0:9]

3, List creation method:

A = []1,2,3,4,5]

A = List (1,2,3,4,5)

4,#! /usr/bin/python

#-*-Coding:utf-8-*-

a = (1,2,{' k1 ': ' s1 '})

a[2][' K1 '] = 3

Print (a)

Note: The elements of the Ganso are immutable, the elements of the GANSO elements can be changed, and the above example is the dictionary change.

5, int __divmod__ usage

All =

Pager = 10

result = all.__divmod__ (10)

Print (Result)

After running: (9,5) 9 is the quotient, 5 is the remainder, __divmod__ is suitable for paging.

Note: The __rdivmod__ parameter is 10 and 95 for position, from left to right to right to left, 10 divided by 95, result (0,10).

6, int __ge__ usage

#!/usr/bin/env Python3    

Age = 18

result = age.__ge__ (19)

Print (Result)

Operating result: False, __gt__, __le__.

__pow__ function equals power.

7, int abs or __abs__ absolute value, ABS (-10), the input result is 10.

8, type () and Dir () get classes and information.

9. String __contains__ contains

#!/usr/bin/env Python3

name = ' Zhangsan '

result = name.__contains__ (' an ')

Print (Result)

Run Result: True, Zhangsan contains an, displaying the result as true.

10, the string Name.capitalize () function is the initial letter will become uppercase.

The string name.casefold () acts as uppercase letters into lowercase.

The string Name.center (10) is centered in 10 byte lengths, Name.ljust () on the left, and Name.rjust () on the right.

The number of occurrences of a in a string between string Name.count (a,0,10) 0-10.

The string name.lower () becomes lowercase.

The string Name.upper () becomes uppercase.

The string Name.startwith () begins with what.

The string Name.title () is capitalized in the first letter of the string.

The string name.swapcase () uppercase becomes lowercase, and lowercase becomes uppercase.

The string name.replace (' Oldstr ', ' newstr ') is replaced by the last position plus a number to decide on a replacement.

The string Name.lstrip () only goes to the left space, and the string Name.rstrip () only goes to the right space.

String Name.find () find the location, you can set the starting position, cannot find the return -1,name.index () can also find, not to find an error.

String Name.format () to splice.

Name = "a {0} as {1}"

result = Name.format (' s ', ' B ')

Print (Result)

Run output result: a s as b

11. Join Usage:

#!/usr/bin/env Python3

s = [' h ', ' e ', ' l ', ' l ', ' O ']

result = "". Join (s)

RESULT1 = "-". Join (s)

Print (Result)

Print (RESULT1)

Run Result: Hello and H-e-l-l-o, sequence connected.

12. Name.partition () is divided according to the bracketed content.

#!/usr/bin/env Python3

name = ' Youneedpython '

result = name.partition (' need ')

Print (Result)

Run Result: (' You ', ' need ', ' Python ')

13, Name.splitlines () split

#!/usr/bin/env Python3

Name = "" "

  Aa

  Bb

Cc

”””

result = Name.splitlines ()

Print (Result)

Running results: [', ' AA ', ' BB ', ' CC ']

14. The dictionary Dic.fromkeys () generates a new dictionary.

#!/usr/bin/env Python3

DIC = Dict (a1= ' B1 ', a2= ' B2 ')

New_dict = Dic.fromkeys ([' A1 ', ' A2 ', ' A3 '], ' B1 ')

Print (new_dict)

Run Result: {' a2 ': ' B1 ', ' A1 ': ' B1 ', ' A3 ': ' B1 '}

15, dic = Dict (a1= ' B1 ', a2= ' B2 ') dic.get equivalent to dic[' A1 '],dic.get (' A3 ', ' 111 '), A3 does not exist assignment 111, if not assigned will output none,dic[' A3 ' will be an error.

16, the following values set [11,22,33,44,55,66,77,88,99], all values greater than 66 are saved in the first key, the value less than 66 is saved in the second key.

Both: {' K1 ': greater Than, ' K2 ': less than or equal to 66}

Method 1:

#!/usr/bin/env Python3

DiC ={}
all_list = [11,22,33,44,55,66,77,88,99]
For i in All_list:
if i>66:
if "K1" in Dic.keys ():
dic[' K1 '].append (i)
Else:
dic[' k1 ' = [I,]
Else:
if "K2" in Dic.keys ():
dic[' K2 '].append (i)
Else:
dic[' k2 ' = [I,]
print (dic[' K1 ')
print (dic[' K2 ')

Operation result: [77,88,99] [11,22,33,44,55,66]

Method 2:

#!/usr/bin/env Python3

all_list = [11,22,33,44,55,66,77,88,99]
DiC ={}
a1 = []
A2 = []
For i in All_list:
if i>66:
a1.append (i)
Else:
a2.append (i)
dic[' k1 '] = A1
dic[' k2 '] = A2
Print (A1)
print (A2)
  Operation result: [77,88,99] [11,22,33,44,55,66]

Python Learning Path The next day

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.