练习

def spliting():
print('-'*60)
import math
print(math.pi) # 3.141592653589793
print(math.sqrt(4)) # 2

import random
print(random.randint(1,10))
print(random.choice([1,2,3,4]))

spliting()
s = 'lux'
print(s[-1]) # x
print(s[len(s)-1]) # x
print(s) # lux
s = 'j'+ s[1:]
print(s) # jux
print(s.find("j")) # 0 or -1
s = s.replace('u','in')
print(s) # jinx
s = 'mk 111'
print(s.split()) # ['mk', '111']
print(s.isalpha()) # salse
print('yi'.isalpha()) # true
s = 'a b c d'
print(s)

spliting()

import re
match = re.match('hello[ ](.)world','hello python world')
print(match.group(1)) # 'python '
match = re.match('/(.)/(.)/(.*)','/bin/home/localjack')
print(match.groups()) # ('bin', 'home', 'localjack')
print(match.group()) # /bin/home/localjack

spliting()

a = ['a','b','c']
print([a*2 for a in a]) # ['aa', 'bb', 'cc']
print([a+b for a in a for b in '123'])

['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3']

d = {'a':1,"b":2,"d":4,"c":3}
print(list(d.keys())) # ['a', 'b', 'd', 'c']

for k in d:
print(k,d[k])

for k in sorted(d):
print(k,d[k])

a = [3,4,6,1,2]
print(a.sort())
print(a)
a = [3,4,6,1,2]
print(sorted(a))

for i in 'spam':
print(i.upper())

x= 4
while x > 0:
print('spam '*x)
x-=1

spam spam spam spam

spam spam spam

spam spam

spam

a = [a2 for a in [1,2,3]]
print(a) # [2, 4, 6]
a = []
for i in [1,2,3]:
a.append(i
2)
print(a) # [2, 4, 6]

spliting()

f = open('text.txt','w')
f.write('hello ')
f.write('world ')
f.close()

with open('text.txt','w') as f:
f.write('first bolood ')
f.write('jinx ')

f = open('text.txt')
f = f.read()
print(f.split()) # ['first', 'bolood', 'jinx']

x = set('spam')
y = set('xayz')
print(x & y) # {'a'}
print(x | y) # {'p', 'y', 'm', 'x', 'z', 's', 'a'}
print(x-y) # {'s', 'm', 'p'}

for i in range(1,6):
for j in range(1,6):
if i >= j:
print('*',end = '')
else:
print(' ',end = '')
print()

spliting()

import decimal
print(3.141+1)
print(1/3)
print((2/3)+(1/2))
d = decimal.Decimal('3.141')
print(d+1)
a = decimal.Decimal('1.00') / decimal.Decimal('3.00')
print(a) # 0.3333333333333333333333333333
decimal.getcontext().prec = 2
a = decimal.Decimal('1.00') / decimal.Decimal('3.00')
print(a) # 0.33
print(0.1+0.1+0.1) # 0.30000000000000004
decimal.getcontext().prec = 1
a = decimal.Decimal(0.1)+decimal.Decimal(0.1)+decimal.Decimal(0.1)
print(a) # 0.3

decimal.getcontext().prec 可以设置decimal小数的精度 实现小数的精确计算

spliting()

from fractions import Fraction

f = Fraction(2,3)
print(f+1) # 5/3
print(f + Fraction(1/2)) # 7/6

fractions 中的 Fraction可以实现分数的计算

spliting()

print(bool('bb')) # True
print(bool( )) # False

print(type([1])) # <class 'list'>
print(type(type))

spliting()

if type([1]) == type([]):
print('yes')
if type([1]) == list:
print('yes')
if isinstance([1],list):
print('yes')

class Worker:
def init(self,name,pay):
self.name = name
self.pay = pay
def lastName(self):
return self.name.split()[-1]
def giveRaise(self,precent):
self.pay *= (1.0 + precent)
bob = Worker('Bob Smith', 50000)
sue = Worker('Sue Jones', 60000)
print(bob.lastName())
print(sue.lastName())
sue.giveRaise(.10)
print(sue.pay)

原文地址:https://www.cnblogs.com/jibandefeng/p/11125836.html