1. Pythone Getting system time
Import Datetimenowtime=datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ') #现在pastTime = (Datetime.datetime.now () -datetime.timedelta (Hours=1)). Strftime ('%y-%m-%d%h:%m:%s ') #过去一小时时间afterTomorrowTime = (Datetime.datetime.now () + Datetime.timedelta (days=2)). Strftime ('%y-%m-%d%h:%m:%s ') #后天tomorrowTime = (Datetime.datetime.now () + Datetime.timedelta (Days=1)). Strftime ('%y-%m-%d%h:%m:%s ') #明天print (' \ n ', nowtime, ' \ n ', pasttime, ' \ n '), Aftertomorrowtime, ' \ n ', tomorrowtime)
2. Formatted output
Str= "%s:%d"% ("Hello-world", 123)
3.if Else
x = Int (input ("Please enter a integer:")) if x < 0: x = 0 print (' negative changed to zero ') elif x = = 0 : Print (' Zero ') elif x = = 1: print (' single ') Else: print (' more ')
4. String contains
The first method: in
string = ' HelloWorld '
If ' World ' in string:
print ' Exist '
Else
print ' not exist '
The second method: find
string = ' HelloWorld '
If String.find (' world ') = = 5: #5的意思是world字符从那个序开始, because W is located in sixth, and the order is 5, so Judge 5
print ' Exist '
Else
print ' not exist '
The third method: Index, which is similar to find, and is the ordinal of the start of a character.
If String.index (' World ') >-1: #因为-1 means no characters found, so judging >-1 means you can find
print ' Exist '
If not found, the program throws an exception
5. String segmentation
st0= ' Iisongiiihuaniiiigongi ' Print (st0.split (' I ')) results are: [', ', ', ' song ', ' ', ' ', ' Huan ', ', ', ', ' gong ', ']
6. Python does not support switch
View Python official: PEP 3103-a switch/case Statement, Implementing a Switch case requires that the variables to be judged are hashed and comparable,
Conflict with the flexibility that Python advocates. In the implementation, the optimization is not good, probably to the last worst case assembly is the same as the if Else group.
Python does not support switch.
7. Python implements the struct body
Scenario: Referencing multiple external variables in a class using only Globa
# Filename:p.pyclass Employee: Passjohn = Employee () # Create a empty Employee record# Fill The fields of the RECORDJ Ohn.name = ' John Doe ' john.dept = ' computer lab ' john.salary = 1000>>> import p>>> p.john<p.employee in Stance at 0xb71f50ac>>>> P.john.name ' John Doe ' >>> p.john.dept ' computer lab ' >>> p.john.salary1000
8. Transformation
int (x [, Base]) converts x to an integer long (x [, Base]) converts X to a long integer, float (x) converts x to a floating-point number complex (real [, IMAG]) create a complex str (x) to convert object x to String repr (x) to convert object x to expression string eval (str) Used to calculate a valid Python expression in a string, and returns an object tuple (s) converts the sequence s to a tuple list (s) to convert the sequence s to a listing chr (x) Converts an integer to a character UNICHR (x) converts an integer to a Unicode character, Ord (x) converts a character to its integer value, hex (x) Converts an integer to a hexadecimal string Oct (x) converts an integer to an octal string chr (' a ') = ' A ' ord (' A ') =65int (' 2 ') =2;str (2) = ' 2 '
9. Creating a File Server
Execute command python-m simplehttpserver port number, the default port number is 8000.
Python is not a small problem