python学习手册第一章部分代码练习

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\nb\tc\r\nd’
print(s)

spliting()

import re
match = re.match(‘hello \tworld’,‘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(i2)
print(a) # [2, 4, 6]

spliting()

f = open(‘text.txt’,‘w’)
f.write(‘hello\n’)
f.write(‘world\n’)
f.close()

with open(‘text.txt’,‘w’) as f:
f.write(‘first bolood\n’)
f.write(‘jinx\n’)

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值