赞
踩
eval('3') # 3
eval('3') + eval('4') # 7
eval('abc') #报错,abc没有定义
x , y = y ,x #x,y互换
eval('34') == eval('3' + '4')
integer = eval(input('Enter an integer:'))
q = integer // 1000
b = integer // 100 % 10
g = integer % 1000 % 100 // 10
s = integer % 1000 % 100 %10
print(s,g,b,q)
ord('a') #返回字符的ascii码 97
chr(98) #返回ascii码的code字符 'b'
n = 3
id(n) #2426581877040
type(n) #int
s1 = 'HELLO'
s1 = s1.lower() # 'hello'
s1
s2 = 'hello'
s2 = s1.upper() #'HELLO'
s2
s = '\t Welcome \n'
s1 = s.strip() #'Welcome'
s1
s = '\t \n\t123e\t12\nsa \t\n\t'
s.strip() #'123e\t12\nsa'
'''\t,\f,\r,\n都属于空格'''
round(0.1532,2) # 0.15 format(0.125,'.2f') #'0.12' #数值类型默认右对齐,小数点也占一位 format(57.467657,'10.2f') #' 57.47' format(12345678.923,'10.2f') #'12345678.92' format(57.4,'10.2f') #' 57.40' format(57,'10.2f') #' 57.00' format(57,'.2f') #'57.00' #字符串默认左对齐 format('hello! Amber','20s') #'hello! Amber ' format('hello! Amber','<20s') #'hello! Amber ' format('hello! Amber','>20s') #' hello! Amber' format('hello! Amber!hello! Amber!','>20s') #'hello! Amber!hello! Amber!' # %也占一位 format(0.456789101112,'9.3%') #' 45.679%' format(0.456789101112,'<9.3%') #'45.679% '
str = 'Line1-abcdef \nLine2-abcdef \nLine3-abcdef' str.split() #['Line1-abcdef', 'Line2-abcdef', 'Line3-abcdef'] str.split(' ',1) #['Line1-abcdef', '\nLine2-abcdef \nLine3-abcdef'] s = 'apple,peach,banana,pear' s.split(',',2) #['apple', 'peach', 'banana,pear'] s = 'asda\t as\nsada saa\nasda' s.split('s') #['a', 'da\t a', '\n', 'ada ', 'aa\na', 'da'] s.split('\n') #['asda\t as', 'sada saa', 'asda']
random.randint(0,10) #8
random.random() #0.1632552069197507
random.randrange(0,10) #9
random.sample(range(1,100),3) #[25, 5, 82]
random.sample(range(1,100),4) #[4, 53, 28, 79]
if i > 0 :
x = 1
y = 1
可以使用任何宽度的空格,但一定要对齐
if x > 0:
y = 1
else:
y = -1
y = 1 if x > 0 else -1
if number % 2 == 0:
print(number,'is even.')
else:
print(number,'is odd.')
print(number,'is even.' if number % 2 == 0 else 'is odd.')
0 % 2 = 0 1 % 2 = 0
举例
乘法口诀表
#乘法口诀表
print(' Multiplication Table')
print(' '* 2 ,end = ' ')
for i in range(1,10):
print(format(i,'4d'),end = ' ')
print()
print('-' * 4 * 12)
for i in range(1,10):
print(i,end = '| ')
for j in range(1,10):
print(format(i * j,'4d'),end = ' ')
print()
for i in range(1,5): j = 0 while j < i: print(j,end = ' ') j += 1 # 0 0 1 0 1 2 0 1 2 3 i = 5 while i >= 1: num = 1 for j in range(1,i + 1): print(num,end = 'xxx') num *= 2 print() i -= 1 1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx for i in range(1,9): print(' ' * 4 * (8 - i),end = ' ') for j in range(0,i): print(format(2 ** j ,'3d'),end = ' ') for j in range(0,i - 1): print(format(2 ** (i - j - 1),'3d'),end = ' ') print()
找出最大公约数
#找出最大公约数 n1 = eval(input('Enter first integer:')) n2 = eval(input('Enter second integer:')) gcd = 1 k = 2 while k < min(n1,n2): if n1 % k == 0 and n2 % k == 0: gck = k k += 1 print(n1,'和',n2,'的最大公约数是',gck) #或者 n1,n2 = eval(input('Please enter two integer:')) for i in range(1,min(n1 + 1, n2 + 1)): if n1 % i == 0 and n2 % i == 0: a = i print(n1,'和',n2,'的最大公约数是',a)
break 和 continue
break帮助退出整个循环
continue帮助退出或跳过当前的迭代
sum = 0 number = 0 while number < 20: number += 1 sum += number if sum >= 100: break print('The number is ',number,'\nThe sum is ',sum) #The number is 14 #The sum is 105 sum = 0 number = 0 while number < 20: number += 1 if number == 10 or number == 11: continue sum += number print('The sum is ',sum) # 10 与 11并没有加入 #The sum is 189
#break and continue b = 1000 while 1: if b < 9: break b = b - 9 print('b is ',b) #b is 1 b = 1000 while 1: if b < 9: continue b = b - 9 print('b is ',b) #陷入死循环
#break and continue for i in range(1,4): for j in range(1,4): if i * j > 2: break print( i * j) print(i) 1 2 1 2 2 3 for i in range(1,4): for j in range(1,4): if i * j > 2: continue print( i * j) print(i) 1 2 1 2 2 3
#写一个程序,给出前50个素数,每行包含10个素数
print('The first 50 prime numbers are')
print()
count = 0
n = 2
while count <
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。