內容選單標籤

2019年11月17日 星期日

CH6 python 運算





---------------------------------

---------------------------------

---------------------------------
---------------------------------

---------------------------------

---------------------------------
---------------------------------

---------------------------------

---------------------------------
---------------------------------

---------------------------------
>>> a='12345'
>>> b=7
>>> a
'12345'
>>> b
7
>>> print(a,b)
12345 7

>>> c=12;d=c+2
>>> c,d
(12, 14)

>>> list1=[5,4,7,1,6]
>>> print(sorted(list1))   #排序 小-->大
[1, 4, 5, 6, 7]



---------------------------------

>>> a=8
>>> b=13
>>> a&b
8

>>> a=3
>>> b=15
>>> a&b
3

>>> a=3
>>> b=15
>>> a|b
15

>>> a=8
>>> b=13
>>> a|b
13

>>> 11<<2
44                    #乘 2**2
>>> 11>>2
2                        #除 2**2

>>> a=1
>>> ~a 
-2
# 0001 --> 1110 即 以1's補數法,表示的負整數

>>> a=4
>>> ~a
-5
#4 --> 0100 --> 1011 --> -5


>>> a=2
>>> b=3
>>> a^b
1

>>> a=5
>>> b=7
>>> a^b
2

>>> a=2
>>> b=7
>>> a^b
5

>>> a=3
>>> b=15
>>> a^b
12
---------------------------------

>>> a=16
>>> bin(a)
'0b10000'
>>> oct(a)
'0o20'
>>> hex(a)
'0x10'





---------------------------------

>>> a=True
>>> b=False
>>> if(a):
print('True')
else:
print('False')

True

>>> c=(5>3)
>>> if c:
print('True')
else:
print('False')

True

>>> if(2):
print('True')
else:
print('False')

True

>>> if(0):
print('True')
else:
print('False')

False





>>> list1=[1,2,3]
>>> list2=[4,5,6]

>>> list1==list2
False

>>> list1=list2
>>> list1
[4, 5, 6]



---------------------------------運算

>>> b=a[0:2]+'A'+a[3:5]
>>> print(b)
12A45


>>> print(a[1:3])
23

>>> a='12345'
>>> for i in range(0,5):
print(i)


0
1
2
3
4



>>> a=123
>>> b="567"
>>> a+int(b)
690
>>> str(a)+b
'123567'
>>> c=str(a)+b
>>> c
'123567'
>>> c[3:5]
'56'



>>> print("c:\\workspace")
c:\workspace

>>> print(R"c:\\workspace")
c:\\workspace


>>> a="Hello"
>>> b="Python"
>>> a+b
'HelloPython'
>>> a*2
'HelloHello'
>>> a[1]
'e'
>>> a[2:4]
'll'
>>> 'H' in a
True
>>> 'M' not in a
True



>>> a=5
>>> b=3
>>> a+=b
>>> a
8


沒有留言:

張貼留言