OS: ubuntu22.04
藍天的名字叫白雲。長的非常可愛。
互動模式、離開Ctrl+D
vscode
--------------------
vscode: extensions
1.Text Editor-->Font
Text Editor-->FormattingText Editor-->Files
Text Editor-->Files2.Window-->ZoomLevel
Text Editor-->FormattingText Editor-->Files
Text Editor-->Files2.Window-->ZoomLevel
從2019/10開始重練
--------------- /home/kk/02.py
cnt=int(input("共要輸入多少個數字:"))
lst=[]
for i in range (0,cnt):
n=int(input("第"+str(i+1)+"數字:"))
lst.append(n)
print("lst=",lst)
Maxi=len(lst)
for i in range(0,Maxi):
print(lst[i],end='+' if i<Maxi-1 else '=')
else:
print(sum(lst))
lst=[]
for i in range (0,cnt):
n=int(input("第"+str(i+1)+"數字:"))
lst.append(n)
print("lst=",lst)
Maxi=len(lst)
for i in range(0,Maxi):
print(lst[i],end='+' if i<Maxi-1 else '=')
else:
print(sum(lst))
---------------
for i in range(1,9+1):
for j in range(1,9+1):
print("{0:1d}*{1:1d}={2:2d} ".format(i,j,i*j),end='')
print()
for j in range(1,9+1):
print("{0:1d}*{1:1d}={2:2d} ".format(i,j,i*j),end='')
print()
---------------ASCII 轉換
>>> ord('A')
65
>>> chr(65)
'A'
65
>>> chr(65)
'A'
>>> bin(65)
'0b1000001'
'0b1000001'
>>> oct(65)
'0o101'
>>> hex(65)
'0x41'
'0o101'
>>> hex(65)
'0x41'
# 2進制無法套用此法
>>> print('\101') 8進制
A
>>> print('\x41') 16進制
A
A
>>> print('\x41') 16進制
A
---------------10進制轉2、8、16
>>> "{0:b}".format(17)
'10001'
>>> "{0:o}".format(17)
'21'
>>> "{0:x}".format(17)
'11'
'10001'
>>> "{0:o}".format(17)
'21'
>>> "{0:x}".format(17)
'11'
>>> format(17,'b')
'10001'
>>> format(17,'o')
'21'
>>> format(17,'x')
'11'
'10001'
>>> format(17,'o')
'21'
>>> format(17,'x')
'11'
>>> bin(17)
'0b10001'
>>> oct(17)
'0o21'
>>> hex(17)
'0x11'
'0b10001'
>>> oct(17)
'0o21'
>>> hex(17)
'0x11'
# 2進制無法套用此法
>>> print("%b" %(17))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: unsupported format character 'b' (0x62) at index 1
>>> print("%o" %(17))
21
>>> print("%x" %(17))
11
--------------------------------------i、d 皆代表10進制
>>> print('%i' %(17))
17
>>> print('%d' %(17))
17
--------------------------------------
>>> print(f'{17:b}')
10001
>>> print(f'{17:o}')
21
>>> print(f'{17:x}')
11
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: unsupported format character 'b' (0x62) at index 1
>>> print("%o" %(17))
21
>>> print("%x" %(17))
11
--------------------------------------i、d 皆代表10進制
>>> print('%i' %(17))
17
>>> print('%d' %(17))
17
--------------------------------------
>>> print(f'{17:b}')
10001
>>> print(f'{17:o}')
21
>>> print(f'{17:x}')
11
----------------------------2、8、16進制轉10----------------------------
>>> int('10001',2)
17
>>> int('21',8)
17
>>> int('11',16)
17
17
>>> int('21',8)
17
>>> int('11',16)
17
>>> int(0b10001)
17
>>> int(0o21)
17
>>> int(0x11)
17
17
>>> int(0o21)
17
>>> int(0x11)
17
----------------------------16進制轉2---------------------------
bin(0xc)
'0b1100'
----------------------------2進制轉16---------------------------
hex(0b1100)
'0xc'
---------------
運算的種類
數值運算:+ - * / % // **
字串運算:+ * [ ]
數值運算:+ - * / % // **
字串運算:+ * [ ]
邏輯運算:AND OR NOT
比較運算:< > == != <> >= <=
位元運算:& | ~
比較運算:< > == != <> >= <=
位元運算:& | ~
//0 為False、非0則True
//有0,則傳回0;均非0則傳回後一個值
>>> 2 and 0
0
0
>>> 0 and 2
0
>>> 2 and 1
1
>>> 1 and 2
2
0
>>> 2 and 1
1
>>> 1 and 2
2
//至少有一個非0時,傳回第一個非0
>>> 2 or 0
2
>>> 0 or 2
2
>>> 2 or 1
2
2
>>> 0 or 2
2
>>> 2 or 1
2
>>> bin(1)
'0b1'
>>> bin(2)
'0b10'
>>> 1 & 2
0
>>> 1 | 2
3
'0b1'
>>> bin(2)
'0b10'
>>> 1 & 2
0
>>> 1 | 2
3
//右移,原數除2^1;左移,原數*2^1
>>> 3>>1
1
>>> 3<<1
6
1
>>> 3<<1
6
>>> 3>>2
0
>>> 3<<2
12
0
>>> 3<<2
12
--------------- 範圍
>>> lst=[1,2,3,4,5]
>>> lst
[1, 2, 3, 4, 5]
>>> for i in lst:
... print(i,end=' ')
...
1 2 3 4 5
[1, 2, 3, 4, 5]
>>> for i in lst:
... print(i,end=' ')
...
1 2 3 4 5
>>> lst[0:5]
[1, 2, 3, 4, 5]
>>> lst[3:5]
[4, 5]
[4, 5]
>>> for i in range(1,5+1):
... print(i,end=' ')
...
1 2 3 4 5
... print(i,end=' ')
...
1 2 3 4 5
---------------
---------------