张拽拽

导航

Python_day3

字符串 将用户输入字符串中的大写成员转小写,小写成员转大写
ord() chr()

s = input('str:')

new_str = ''
for i in s:
	if 'a' <= i <= 'z':
		# new_str += chr(ord(i) - (ord('a')-ord('A')))
		new_str += i.upper()
	elif 'A' <= i <= 'Z':
		# new_str += chr(ord(i) + (ord('a')-ord('A')))
		new_str += i.lower()
	else:
		new_str += i
print(new_str)

 可变的序列类型:

  •     列表list
# 定义
l = [] l = [1,2,3,'hello', [11,22]] ls = list('hello') ls2 = list(range(100)) print(ls) print(ls2) # 索引 print(l[0]) print(l[4]) print(l[4][0]) # 切片 print(l[:4]) print(l[1:]) print(l[1::2]) # 增 l[1:1] = [100] print(l) l.append(110) print(l) l.insert(2, 119) print(l) # 删 l[1:3] = [] print(l) l.pop(3) print(l) l.remove([11,22]) print(l) # 修改 l[0] = 120 l[1:3]

有序的不可变的序列类型:

  •   元组tuple
# 定义
t = () print(type(t)) t1 = (1,) print(type(t1)) t2 = tuple([1,2,3,4, 'hello']) print(t2) # 索引 切片 print(t2[0]) print(t2[::-1]) print(t2) # 成员运算符 for t in t2: print(t) print('*****************************') t3 = (5,6,2,12,7,8,7,1,100) print(max(t3)) print(min(t3)) print(len(t3)) print(t3.index(7)) print(t3.count(7))

 

 练习题 (字符串补充)

import random

words = ('hello', 'python', 'boys', 'apple', 'world', 'eat')

# 'apple'
word = random.choice(words)

# 'palep'
save = word
'''
rand_str = ''
while word: # 不是空串
	rand_ind = random.randrange(len(word)) # random.choice(range(len(word)))
	rand_str += word[rand_ind]	
	word = word[:rand_ind]+word[rand_ind+1:] # 切除抽取的成员
'''
listword = list(word)
random.shuffle(listword)
rand_str = ''.join(listword)


print('请猜测%s是什么单词, 你有三次机会:' % rand_str)

for i in range(3):
	input_str = input('第%d次:' % (i+1))
	if input_str == save:
		print('恭喜你!答对了')
		break
	else:
		if i < 2:
			print('很遗憾!再试一次')
		else:
			print('游戏结束!已经没有机会了')

 Δ   1

'''
1990.1.1 星期1
2018.11.1
'''

year, month = eval(input('请输入年份和月份(year,month):'))

sumdays = 0
# 1990~year
for y in range(1990, year):
	sumdays += (365+(1 if (y % 4 == 0 and y % 100 != 0 or y % 400 == 0) else 0))

# year.1.1 ~ year.month.1
for m in range(1, month):
	if m == 1 or m == 3 or m == 5 or m == 7 or \
		m == 8 or m == 10 or m == 12:
		sumdays += 31 
	elif m == 4 or m == 6 or m == 9 or m == 11:
		sumdays += 30
	else:
		if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
			sumdays += 29
		else:
			sumdays += 28

sumdays += 1

weekday = sumdays % 7

# 计算month月有多少天
if month == 1 or month == 3 or month == 5 or month == 7 or \
	month == 8 or month == 10 or month == 12:
	monthdays = 31 
elif month == 4 or month == 6 or month == 9 or month == 11:
	monthdays = 30
else:
	if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
		monthdays = 29
	else:
		monthdays = 28

# 打印日历
m = '一二三四五六七八九十'
if month == 11:
	monthstr = '十一'
elif month == 12:
	monthstr = '十二'
else:
	monthstr = m[month-1]
print('\33[34m{:>7}月 {}'.format(monthstr, year))
print('\33[0m', end='') # 关闭属性
print('\33[47m日 一 二 三 四 五 六')
print('\33[0m', end='') # 关闭属性
print('   '*weekday, end='')
for d in range(1, monthdays+1):
	print('{:>2}'.format(d), end=' ' if (weekday+d)%7 else '\n')	
print()

 Δ 2

'''
1990.1.1 星期1
2018.11.1
'''

days31 = [1,3,5,7,8,10,12]
days30 = [4,6,9,11]

year, month = eval(input('请输入年份和月份(year,month):'))

sumdays = 0
# 1990~year
for y in range(1990, year):
	sumdays += (365+(1 if (y % 4 == 0 and y % 100 != 0 or y % 400 == 0) else 0))

# year.1.1 ~ year.month.1
for m in range(1, month):
	if m in days31:
		sumdays += 31 
	elif m in days30:
		sumdays += 30
	else:
		sumdays += (29 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else 28)

sumdays += 1

weekday = sumdays % 7

# 计算month月有多少天
if month in days31: 
	monthdays = 31 
elif month in days30:
	monthdays = 30
else:
	if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
		monthdays = 29
	else:
		monthdays = 28

# 打印日历
m = '一二三四五六七八九十'
if month == 11:
	monthstr = '十一'
elif month == 12:
	monthstr = '十二'
else:
	monthstr = m[month-1]
print('\33[34m{:>7}月 {}'.format(monthstr, year))
print('\33[0m', end='') # 关闭属性
print('\33[47m日 一 二 三 四 五 六')
print('\33[0m', end='') # 关闭属性
print('   '*weekday, end='')
for d in range(1, monthdays+1):
	print('{:>2}'.format(d), end=' ' if (weekday+d)%7 else '\n')	
print()

  

posted on 2018-12-07 18:04  张拽拽  阅读(192)  评论(0编辑  收藏  举报