python第二章实操作业
1.使用python表达数学式
在IDLE中输入以下代码
a = "(5+10*x)/5-13*(y-1)*(a+b)/x+9*(5/x+(12+x)/y)"
print(a)
结果如下所示
(5+10*x)/5-13*(y-1)*(a+b)/x+9*(5/x+(12+x)/y)
2.从控制台输入用户的月薪,进行运算计算出年薪。打印输出用户的年薪
新建py文件,输入以下代码:
money = input("请输入您的月薪:")
val = float(money)
val *= 12
print("您的年薪为:",val)
输出如下所示:
3.使用字符串复制,用计算机打印出“爱你一百遍”,打印 100 次
在IDLE中输入以下代码:
>>> a = "爱你一百遍"
>>> print(a*100)
输出如下所示:
4.将”to be or not to be”字符串倒序输出
在IDLE中输入以下代码:
>>> a = "to be or not to be"
>>> print(a[::-1])
输出为:
eb ot ton ro eb ot
5.将”sxtsxtsxtsxtsxt”字符串中所有的 s 输出
在IDLE中输入以下代码:
>>> a = "sxtsxtsxtsxtsxt"
>>> print(a[::3])
输出为:
sssss
6.判断如下输出结果,并文字解释原因:
>>> a = "abd_33"
>>> b = "abd_33"
>>> c = "dd#"
>>> d = "dd#"
>>> a is b #输出 true or false?
>>> c is d #输出 true or false?
a is b
的输出结果为True
,c is d
的输出结果为Flase
,因为python具有字符串驻留机制机制,会将仅包含字母,数字以及下划线的字符串缓存下来。
7.写出如下代码打印的结果:
>>> c = "名字是{name},年龄是{age}"
>>> c.format(age=19,name='高淇')
输出结果为:
名字是高淇,年龄是19