第二节练习

第二节练习

a=b=2  # 链式赋值
print(a)
print(b)
c=id(a)  #地址
print(c)
c=type(a) #类型
print(c)
a,b=2,3 # 系列解包赋值
a,b=b,a
print(a)
print(b)
# 基本运算
print(3+2)
print(30-5)
print(30/5)
print(30//5)
print(5*6)
print(7%4)
print(2**3)
a=divmod(13,3)
print(a)
print()
# int()实现类型转换
a=int(3.14)
print(a)
a=float(3)
print(a)
print(round(3.14))
# 增强型赋值运算符
a+=2
print(a)
a-=2
print(a)
a*=2
print(a)
a/=2
print(a)
a//=2
print(a)
a**=2
print(a)
a%=2
print(a)
#计算当前时间
import time
a=time.time()
print(a)
b=a//60
print(b)
print(int(b))
print(int(b)//60//24)
# 画折现,计算距离
import turtle
import math
x1,y1=100,100
x2,y2=200,100
x3,y3=500,200
x4,y4=400,50
turtle.goto(x1,y1)
turtle.goto(x2,y2)
turtle.goto(x3,y3)
turtle.goto(x4,y4)
distance=math.sqrt((x4-x1)**2+(y4-y1)**2)
print(distance)

#布尔值运算
b=2+True
print(b)
#比较运算符
a,b=10,20
print(a==b)
print(a!=b)
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)
#逻辑运算符
a=True
b=False
print(a or 30)
print(a or 30/0)
print(b or 30)
print(a and 30)
print(b and 30)

#同一运算符
a,b=111,111
print(id(a))
print(id(b))
print(a==b)
print(a is b)
a,b=1111,1111
print(id(a))
print(id(b))
print(a==b)
print(a is b)
# 字符串编码
print(ord("高"))
print(chr(10300))

a="I am a teacher!"
print(a)
a='i am a "tom"'
print(a)
a='''name="liang" 
sex="famale" age=18'''
print(a)
c=""
print(len(c))
a="I\nlove\nyou"
print(a)
a="i\'m a teacher"
print(a)

a="aaa"+"bbb"
print(a)
a="aaa""bbb"
print(a)
a="aaa"*3
print(a)
print("aa",end="*")
print("bb",end="*")
print("cc")
myname=input("请输入你的名字:")
"""
a=str(3.14)
print(a[-1])
print(a.replace(".","高"))
print(a[:])
print(a[1:3])
print(a[-2:-5:1])
a=" to be or not to be"
print(a[::-1])
a="sxt"*5
print(a)
print(a[0::3])
a=["to","you"]
print(a)
print('*'.join(a))
a,b="abd_33","abd_33"
print(a is b)
a,b="abd#","abd#"
print(a is b)
print(id(a))
print(id(b))
print(a==b)
print("a" in a)
print("ddd" in a)
a="名字是{0},年龄是{1}"
b=a.format("小明",20)
print(b)
a="名字是{name},年龄是{age}"
b=a.format(name="小明",age=20)
print(b)

上一篇:python学习总结1(基本语法)


下一篇:中国大学MOOCPython语言程序设计(北京理工大学)第5-6周学习笔记和课后练习