Bitwise operations
Bitwise & | ^ ~
>> 3&3
3
>> 3&1
1
>> 2|1
3
XOR or
^
Same as 0, not 1
Is
Determines whether two objects are equal, and the ID () values of two objects are equal
Python caches the smaller values of the -5~256, and the variable object ID values that point to them are equal
operator
#coding=utf-8import operatorprint (operator.add(1,1))print (operator.sub(2,1))print (operator.mul(2,3))print (operator.truediv(6,2))print (operator.contains("ab","a"))print (operator.pow(2,3))print (operator.ge(1,1))print (operator.gt(2,1))print (operator.le(1,2))print (operator.eq(1,1))
There are no CMP functions in the Python 3.4.3 version and are replaced by operator modules, which need to be imported when used in interactive mode.
Eval ()
expression evaluation, only a single-line expression can be executed, with a return value
>> s = "1+2+3"
>> Eval (s)
6
>> su = eval (s)
>> su
6
>> eval ("1+2")
3
EXEC ()
The EXEC function is similar to the Eval function and executes dynamic statements, except that the Eval function is only used to perform expression evaluation, while the EXEC function is primarily used to execute statement blocks and can execute multiple lines of statements with no return value;
>> e = ' print ' ("Hello") '
>> EXEC (e)
Hello
>> EXEC ("a = 2+3")
>> A
5
>> s = "" "
... for I in List (range (10)):
.. print (I+1)
... """
>> EXEC (s)
1
2
3
4
5
6
7
8
9
10
Standard output
Sys.stdout.write ()
>> Import Sys
>> Sys.stdout.write ("D")
D1
>> Sys.stdout.write ("DD")
Dd2
Standard error Output
>> sys.stderr.write ("Error")
5
Error>>>
Standard input
>> a = Sys.stdin.read ()
353
^z
>> A
' 353\n '
Print content saved to a file
import sysprint(‘Fatal error: invali input!‘,file=open("e:\\log.out","w"))print(‘Fatal error: invali input! ‘,file=sys.stderr)#打印错误
Math.PI
>> Import Math
>> Math.PI
3.141592653589793
Exercises:
1. Remove all the numbers in the string.
#coding=utf-8s = "a1b2c3b4d5dddddd"letters_list = []for v in s: if v not in "0123456789": letters_list.append(v)print("".join(letters_list))print("".join([v for v in s if v.isalpha()]))print("".join(filter(lambda x:x not in "0123456789",s)))print("".join(filter(lambda x:x.isalpha(),s)))
2, three number sort
#coding =utf-8
def sort_2(a,b,c): if a > b: a,b = b,a if a > c: a,c = c,a if b > c: b,c =c,b return a,b,cprint(sort_2(3,1,9))
3. Ask for the maximum value in a list
def max(a): #找到列表的最大值 max_num = a[0] for i in a: if i > max_num: max_num =ireturn max_numa = [1,2,3,4,5]print(max(a))
Python Learning (4)