Notes on the next day for getting started with python, and the next day for getting started with python

Source: Internet
Author: User

Notes on the next day for getting started with python, and the next day for getting started with python

Main program file flag
If _ name __= = "_ main __":

When the program executes python 1.py, the program 1.py _ name _ is main
To call other files, __name _ indicates that the program name is _ name __


Everything is an object, and an object is created by a class.

 

Int internal functions
A = 95
B = 10
C = a. _ divmod _ (10) # used by PAGE, remainder
Print (c)
(9, 5)
A. _ ge _ () is greater than or equal to True,> =


Method of viewing classes
Type () method view type
Dir () to check the methods
Help () view detailed methods

Name = 'ERW'
Print (type (name) # obtain its class. For example, str, list, int
Print (dir (name) # quickly obtain the class and list all the members (functions) in the class ).

Str internal element


Name. _ contains _ ('contains element ')
A. _ eq _ () equivalent syntax of the string =
_ Format _ string formatting,
>>> A = 'Eric {0 }'
>>> A. _ format _ ('Alex ')
>>> A. format ('Alex ')
'Alexalexs'
_ Getattribute _ functions are used for reflection.
Name. capitalize () is capitalized.
Name. casefold () in lower case
Name. center (length ,'**--''~~ __')
>>> Print (8 * ', a, 8 *'*')
* ******** AlExV5 ********* can be implemented using the center
>>> A. center (20)
'Alexv5'
>>> A. center (20 ,'*')
* AlExV5 *******'

Name. count ('elemental) calculate the number of occurrences of repeated Elements
Name. encode () encodes the string
>>> A = 'Li jie'
>>> A. encode ('gbk ')
B '\ xc0 \ xee \ xbd \ xdc'


Name. endswith ('E') name does not end with e


With open('a.txt ') as f: # Use with to manage the context without writing f. close ()
F. write ()

String str
Remove blank strip
Split
Len (obj)
Index obj [1]
Sliced obj [] 1 indicates the second digit. 10 indicates 10th bits

List
Index
Slice obj []
Append
Delete del remove pop
Len
Loop for, while
Interrupt break, continue, return (method return), exit (Program exit)
Include 'sdfs' in [ssdff]
_ Contains __

Input parameters
Import sys
Print (sys. argv)


Dictionary usage

Two dictionary creation methods
A = {'k1 ', 'v1 '}
A = dict (k1 = 'v1 ')
Dic {"k1": "v1", "k2": "v2" }== dict (k1 = "v1", k2 = "v2 ")

Dic. clear
Dic. copy shallow copy
Dic. fromkeys (['k1 '], 'v1 ')
* Dic. get ('k1 ') # obtain the k1 Value
If the key does not exist, get returns (none) by default. No error is reported.
Print (dic. get ('k3 ', 'v3') value assignment key method that does not exist
Output
V1
V2
V3

Dic. items () Outputs both key and vale.
For k in dic. key ():
Print (k)
For v in dic. values ():
Print (v)
For k, v in items (): # simultaneous output
Print (k, v)

 

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.