name = 'ABDMLBM'
height = 175
weight = 140
# "My name is 'Nick', my height is 180, my weight is 140"
print('My name is %s,my height is %s,my weight is %s'%(name,height,weight))
print('My name is {},my height is {},my weight is {}'.format(name,height,weight))
print(f'My name is {name},my height is {height},my weight is {weight}')
while True:
girl_age =int( input('输入美女年龄'))
if girl_age < 18:
print('不接受未成年')
elif girl_age >= 18 and girl_age < 25:
print('心动表白')
elif girl_age >= 25 and girl_age < 45:
print('阿姨好')
else :
print('奶奶好')
i = 1
while i < 101:
oi = i % 2
if oi == 0:
print(i)
i += 1
预习while循环,猜年龄游戏升级版,有以下三点要求:
age = 25
count = 0
while count < 4:
user_age = int(input('请输入你的年龄:'))
if user_age == age:
print('你猜对了')
break
elif user_age > age:
print('你猜大了')
else:
print('你猜小了')
count += 1
if count != 3 :
continue
else:
print('你已经猜了三次,答"Y"或者"y"还想再玩,答"N"或者"n"退出')
user_player = input('请输入:')
if user_player == "Y" or user_player == "y":
count = 0
else:
break
s = 'hello alex alex say hello sb sb'
l=s.split()
print(l)
dic = {}
for item in l:
if item in dic:
dic[item]=dic[item]+1
else:
dic[item]=1
print(dic)
from collections import Counter
import re
with open('a.txt', 'r', encoding='utf-8') as f:
txt = f.read()
c = Counter(re.split('\W+',txt)) #取出每个单词出现的个数
print(c)
ret = c.most_common(10) #取出频率最高的前10个
print(ret)
def mao_pao(li):
for i in range(len(li)):
for j in range(len(li)):
if li[i] < li[j]:
li[i],li[j] = li[j] ,li[i]
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078
import random
li = list(range(10))
random.shuffle(li)
print(li)
mao_pao(li)
print(li)
#方式一
li = [1,5,5,4,12,3,1,5]
print(list(set(l)))
#方式二
li = [1,5,5,4,12,3,1,5]
def func(li):
l = []
for i in li:
if i not in l:
l.append(i)
return l
print(func(li))
#方式一:递归版
li = [1,2,3,4,5,6,7,8,9,10]
def erfen(li,aim ,start=0 ,end=len(li)-1):
if start <= end:
mid = (start+end)//2
if li[mid] >aim : #如果中间的值比目标值大,就从左边找
return erfen(li,aim,start,mid-1)
elif li[mid]<aim : #从右边找
return erfen(li,aim,mid+1,end)
else:
return mid
ret = erfen(li,10)
print(ret)
#方式二:循环版
li = [1,2,3,4,5,6,7,8,9,10]
def erfen(li,aim,start=0,end=len(li)-1):
while start <= end:
mid = (start+end) //2
if li[mid] < aim:
start = li[mid] +1
elif li[mid] >aim:
end = li[mid] -1
else:
return mid
print(erfen(li,10))
def f(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)
f(2) #[0,1]
f(3,[3,2,1]) #[3,2,1,0 1 4]
f(3) #[0,1,0,1,4]
# 方式一
def str_reverse(str):
return str[::-1] #从前到后步长为-1
print(str_reverse('string'))
# 方式二
def str_reverse2(str):
l = list(str)
l.reverse()
new_str = ''.join(l)
return new_str
print(str_reverse2('string'))
# 0 1 2 3 4
# 0 5 7
sums=sum(map(lambda x:x+3,list(range(5))[::2]))
print(sums)
List=[-2,1,3,-6]
List.sort(key=abs) #按照绝对值从小到大排序
print(List)
import datetime
data = '20170130'
def func(n,data):
date=datetime.datetime.strptime(data,'%Y%m%d')
da=datetime.timedelta(n)
return date+da
print(func(2,data))
import os
def print_code():
with open(os.path.join(os.getcwd(),__file__),'r',encoding='utf-8') as f:
return f.read()
print(print_code())
# 方式一
list1 = [1,2,3,5,8,4,2,6]
list2 = [11,44,21,2,54,77]
print(list(set(list1+list2)))
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078
#方式二
list1.extend(list2)
print(list(set(list1)))
#方式三
def list_union(list1,list2):
for i in list2:
list1.append(i)
return list1
print(list(set(list_union(list1,list2))))
答:
lambda 表达式也就是匿名函数,一些简单的功能需要实现的时候可以用匿名函数,一行语句体搞定。
它语法简单,简化代码,不会产生命名冲突,污染命名空间。
Python提供了map,filter,reduce等函数方法,提供了装饰器,闭包等语法
tuple:元组,不可变数据类型,访问效率高,适合存储一些常量数据,可以作为字典的键使用
list:列表,是可变数据类型,可以通过下标索引取值
dict:字典,是可变数据类型,存储方式为键值对的形式,可以通过相对应的键获取相对应的值。key支持多种数据类型
alist = [{'name': 'a', 'age': 20}, {'name': 'b', 'age': 30}, {'name': 'c', 'age': 25}]
alist.sort(key=lambda x:x['age'],reverse=True)
print(alist)
s = "k:1|k1:2|k2:3|k3:4"
str_list = s.split('|')
print(str_list)
d = {}
for i in str_list:
key ,value = i.split(':')
d[key] = value
print(d)
def fib(n):
if n<=2:
return 1
else:
return fib(n-1) + fib(n-2)
print(fib(10))
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。