python之幾個練習題

2021-08-18 15:44:57 字數 2323 閱讀 7051

練習題一:列印99乘法表

#!/usr/bin/env python

# -*- coding:utf8 -*-

# @time : 2018/4/10 21:35

# @author : hantong

# @file : 20180410_lianxi.py

foriinrange(1,10):

forjinrange(1,i+1):

h = i*j

i -= 0

print("x=".format(j, i, h),end=' ')

print('\n')

執行結果:

1x1=1 

1x2=2 2x2=4 

1x3=3 2x3=6 3x3=9 

1x4=4 2x4=8 3x4=12 4x4=16 

1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 

1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 

1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 

1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 

1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 

練習題二:

a,bc,d是0-9的整數,abcd*9=dbca,計算a,b,c,d的值

#!/usr/bin/env python

# -*- coding:utf8 -*-

# @time : 2018/4/12 11:41

# @author : hantong

# @file : 20180412_lianxi.py

# a,b,c,d

是0-9

的整數,

abcd*9=dbca

,計算a,b,c,d

的值forainrange(1,10):

forbinrange(0,10):

forcinrange(0,10):

fordinrange(1,10):

if(1000*a+100*b+10*c+d)*9 == (1000*d+100*c+10*b+a):

print("a=,b=,c=,d=".format(a,b,c,d))

執行結果:

a=1,b=0,c=8,d=9

編碼問題:

str和unicode都是basestring的子類。嚴格意義上說,str其實是位元組串,它是unicode經過編碼後的位元組組成的序列。對utf-8編碼的str』漢』使用len()函式時,結果是3,因為實際上,utf-8編碼的』漢』 == 『\xe6\xb1\x89』。unicode才是真正意義上的字串,對位元組串str使用正確的字元編碼進行解碼後獲得,並且len(u』漢』) == 1。再來看看encode()和decode()兩個basestring的例項方法,理解了str和unicode的區別後,這兩個方法就不會再混淆了:

#!/usr/bin/env python

# -*- coding:utf8 -*-

# @time : 2018/4/12 11:41

# @author : hantong

# @file : 20180412_lianxi.py

u =u''print(u)

print (repr(u))

s = u.encode('utf-8')

u2 = s.decode('utf-8')

print(repr(s))

執行結果:

漢'漢'

b'\xe6\xb1\x89'

python書中練習題 python練習題

1 定義乙個空列表,接收從鍵盤輸入的整數,把列表傳給乙個從大到小排序的函式,再輸出排序後的列表的值 listex b 0 a int input 請輸入列表長度 while b a num int input 請輸入字元 b 1 print listex sum 0 for i in range 0...

python的練習題 Python練習題

1 使用while迴圈輸入1 2 3 4 5 6 8 9 10 i 0while i 10 i i 1 if i 7 continue print i 結果 e python python python test.py1 2 求1 100的所有數的和 i 0sum 0 while i 100 i 1...

python練習題之11 15

11.題目 古典問題 有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數為多少?usr bin python f1 1 f2 1 n int input please mouth numbers for i in range ...