老师下面推导式生成的字典里面为啥值都是1
>>> dict_a = {key : value for key in 'python' for value in range(2)} >>> dict_a {'p': 1, 'y': 1, 't': 1, 'h': 1, 'o': 1, 'n': 1}
class Employee: id = 1000 def __init__(self,name,salary): self.name = name self.__salary = salary Employee.id += 1 def __add__(self, other): if isinstance(other,Employee): return "薪资和为{0}+{1}={2}".format(self.__salary,other.__salary,(self.__salary+other.__salary)) else: return "不是同类对象,无法相加!" @property def salary(self): return self.__salary @salary.setter#set方法修改私有属性的薪资 def salary(self,salary): if 1000< salary <50000: self.__salary = salary else: print("录入错误,薪资在1000-50000这个范围!") emp1 = Employee("高淇",30000) print(emp1.id) emp2 = Employee("高希希",20000) print(emp2.id) print(emp1.id)
运行截图:
提问:emp1.id在第二次打印的时候由1001变成了1002,我想emp1.id保持原来的id1001,如何解决这种问题?
#命令行模式 a = 257;b = 257 a is b True 比较 a = 257 b = 257 a is b False 这个有什么差异吗?
老师,请问我这么理解是否有问题
append增加元素的方式,是不是也创建了新的对象,他们的id都不一样了
老师,请问这是出了什么问题 #奥运五环 import turtle turtle width(10) turtle.color("blue") turtle.circle(50) turtle.color("black") turtle.penup() turtle.goto(120,0) turtle.pendown() turtle.circle(50) turtle.color("red") turtle.penup() turtle.goto(240,0) turtle.pendown() turtle.circle(50) turtle.color("yellow") turtle.penup() turtle.goto(60,-50) turtle.pendown() turtle.circle(50) turtle.color("green") turtle.penup() turtle.goto(180,-50) turtle.pendown() turtle.circle(50)
老师,这里不写return语句,直接写print不是也能出结果吗,为什么还要写return呢
>>>b ='i\'m a student'
>>>b
"i'm a student"
为什么下面会是双引号
>>>b ='i am "xiaoming"'
'i am "xiaoming"'
这里却是原样展示
score=int(input("输入一个分数(0-100):")) grade='' if score>100 or score<0: score="" else:="" if="">90: grade="A" elif score>80: grade="B" elif score>70: grade="c" elif score>60: grade="d" else: grade="e" print("分数是{0},等级是{1}".format(score,grade))
小于0时也输出东西了
老师,这边的get和set 是不是只是起到了,见代码知其意的效果,如果换成其他的名字也是没有问题的?当然下面print的时候,也是跟上面定义的方法名一样
字符串拼接可以通过+或者空格来实现,在不换行打印时,end=空字符串。打印出来的字符串是否和拼接的结果是一回事?
如:"aa"+"bb"="aabb"
print("aa",end="")
print("bb",end="")
结果也为"aabb"
老师你好:
请问“引用地址的赋值”是指堆吗?
laoshi
weishenmehuibaocuo
d=[x*3 for x in range(0,30)] in 动用是什么 没看明白?
老师好,请问:i为什么是0,1,2,而不是1,2,3?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637