第一章语言基础
第一节编程基础
1 python开发环境
第一个Python程序
python"># 打印"Hello World"
print("Hello World")
# 打印2的100次方
print(2 ** 100)
# 打印1+1=2
print("1+1=",1 + 1)
"""
Hello World
1267650600228229401496703205376
1+1= 2
"""
利用Python编写自己的第一个Python程序(自由发挥)
python">print("我是python,你是谁?")
a = input()
print("我知道了,你是",a)
"""
我是python,你是谁?
小潘
我知道了,你是 小潘
"""
2 python输入与输出
python"># 默认以空格分隔,默认以换行结尾
print("Hello","World",123,sep = '+')
print("Hello","World",123,sep = '-')
print("Hello","World",123)
print('-'*20)
print("Hello","World",123,sep = '+',end='?')
print("Hello","World",123,sep = '-')
print("Hello","World",123,end="apple")
print("11111")
"""
Hello+World+123
Hello-World-123
Hello World 123
--------------------
Hello+World+123?Hello-World-123
Hello World 123apple11111
"""
python">print(1)
print("Hello World")
a = 1
b = 'running'
print(a,b)
print("aaa""bbb")
print("aaa","bbb")
print("www","lanqiao","cn",sep='.')
"""
1
Hello World
1 running
aaabbb
aaa bbb
www.lanqiao.cn
"""
python">a = input() # 输入整数123,input()函数把输入的都转换成字符串
print(type(a)) # 输出a的类型
a = int(a) # 转换成整数
print(type(a)) # 输出a的类型
a = input("input:") #输入字符串Hello
print(type(a)) # 输出a的类型
"""
123
<class 'str'>
<class 'int'>
input:Hello
<class 'str'>
"""
python"># 输入三条边的长度
a = int(input("第一条边为:"))
b = int(input("第一条边为:"))
c = int(input("第一条边为:"))
# 计算半周长
p = (a+b+c)/2
s = p*(p-a)*(p-b)*(p-c)
s = s**0.5
# 输出面积
print("三角形面积是:",s)
"""
第一条边为:3
第一条边为:4
第一条边为:5
三角形面积是: 6.0
"""
python">a = int(input("请输入a的值:"))
b = int(input("请输入b的值:"))
# 检查输入是否为正整数
if a <= 0 or b <= 0:
print("输入的必须是正整数。")
else:
print("a+b=", a + b)
print("a-b=", a - b)
print("a*b=", a * b)
print("a/b=", a / b)
print("a^b=", a ** b)
python">"""
a,b = int(input("何时开始")),int(input("何分开始"))
c,d = int(input("何时停止")),int(input("何分停止"))
"""
a = int(input("何时开始"))
b = int(input("何分开始"))
c = int(input("何时停止"))
d = int(input("何分停止"))
e = c - a
f = d - b
print("总共游泳:",e*60+f,"分钟")
"""
何时开始12
何分开始0
何时停止13
何分停止1
总共游泳: 61 分钟
"""
3 常量、变量与运算符
python">a = 2 + 3
print(a)
a = 3 - 2
print(a)
a = 3/2
print(a)
a = 3**2
print(a)
a = 0.1 + 0.1
print(a)
a = 0.1 + 0.2
print(a)
a = 0.1 * 3
print(a)
"""
age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
"""
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
print(type(message))
"""
5
1
1.5
9
0.2
0.30000000000000004
0.30000000000000004
Happy 23rd Birthday!
<class 'str'>
"""
python">a = 3.7
print(a,type(a))
a = int(a)
print(a,type(a))
a = -1.5
print(a,type(a))
a = int(a)
print(a,type(a))
a = 1.9999
print(a,type(a))
a = int(a)
print(a,type(a))
a = 1.9999 + 0.001
print(a,type(a))
a = int(a)
print(a,type(a))
"""
3.7 <class 'float'>
3 <class 'int'>
-1.5 <class 'float'>
-1 <class 'int'>
1.9999 <class 'float'>
1 <class 'int'>
2.0009 <class 'float'>
2 <class 'int'>
"""
python">a = 5
print(a,type(a))
a = bool(a)
print(a,type(a))
a = 0
print(a,type(a))
a = bool(a)
print(a,type(a))
a = 0.0
print(a,type(a))
a = bool(a)
print(a,type(a))
a = -1
print(a,type(a))
a = bool(a)
print(a,type(a))
a = True
print(a,type(a))
a = bool(a)
print(a,type(a))
a = True
print(a,type(a))
a = str(a)
print(a,type(a))
"""
5 <class 'int'>
True <class 'bool'>
0 <class 'int'>
False <class 'bool'>
0.0 <class 'float'>
False <class 'bool'>
-1 <class 'int'>
True <class 'bool'>
True <class 'bool'>
True <class 'bool'>
True <class 'bool'>
True <class 'str'>
"""
python">a = 10
print("a=",a,"type(a)=",type(a))
print("float(a)=",float(a),"type(float(a))=",type(float(a)))
print("bool(a)=",bool(a),"type(bool(a))=",type(bool(a)))
print()
b = 7.8
print("b=",b,"type(b)=",type(b))
print("str(b)=",int(b),"type(str(b))=",type(int(b)))
print("str(b)=",str(b),"type(str(b))=",type(str(b)))
print()
c = "12.3"
print("c=",c,"type(c)=",type(c))
print("float(c)=",float(c),"type(float(c))=",type(float(c)))
"""
a= 10 type(a)= <class 'int'>
float(a)= 10.0 type(float(a))= <class 'float'>
bool(a)= True type(bool(a))= <class 'bool'>
b= 7.8 type(b)= <class 'float'>
str(b)= 7 type(str(b))= <class 'int'>
str(b)= 7.8 type(str(b))= <class 'str'>
c= 12.3 type(c)= <class 'str'>
float(c)= 12.3 type(float(c))= <class 'float'>
"""
python">a = 25
b = 10
# 除法运算符
c = a/b
print(c,type(c))
# 整除运算符
c = a//b
print(c,type(c))
# 转换成浮点数
c = float(c)
print(c,type(c))
"""
2.5 <class 'float'>
2 <class 'int'>
2.0 <class 'float'>
"""
a = 25
b = 10
# 关系运算符,运算结果为True或者False
c = a == b
print(c,type(c))
c = a > b
print(c,type(c))
c = a < b
print(c,type(c))
"""
False <class 'bool'>
True <class 'bool'>
False <class 'bool'>
"""
a = 25
b = 10
#赋值运算符,将等号右边的运算结果存入等号左边的变量
c = a + b
print(c,type(c))
# c = c + a
c += a
print("c=",c)
# c = c/a
c /= a
print("c=",c)
"""
35 <class 'int'>
c= 60
c= 2.4
"""
python">a = 25
b = 10
# 并且:两者均为True才为True
c = a > 0 and b > 0
print(c)
# 或者:只要有一者为True即为True
c = a < 0 or b > 5
print(c)
# 非,not,True -> False,False -> True
c = not a
print(c)
"""
True
True
False
"""
python">def calculator():
while True:
try:
# 输入第一个操作数
operand1 = input("请输入第一个操作数(可以是整数、浮点数、字符串或布尔值):")
operand1 = eval(operand1)
# 输入运算符
operator = input("请输入运算符(+、-、*、/、//、%、**、==、!=、>、<、>=、<=、and、or、not、in、is、is not):")
# 输入第二个操作数
operand2 = input("请输入第二个操作数(可以是整数、浮点数、字符串或布尔值):")
operand2 = eval(operand2)
# 执行运算
if operator == "+":
result = operand1 + operand2
elif operator == "-":
result = operand1 - operand2
elif operator == "*":
result = operand1 * operand2
elif operator == "/":
if operand2 == 0:
print("错误:除数不能为零")
continue
result = operand1 / operand2
elif operator == "//":
if operand2 == 0:
print("错误:除数不能为零")
continue
result = operand1 // operand2
elif operator == "%":
if operand2 == 0:
print("错误:除数不能为零")
continue
result = operand1 % operand2
elif operator == "**":
result = operand1 ** operand2
elif operator == "==":
result = operand1 == operand2
elif operator == "!=":
result = operand1 != operand2
elif operator == ">":
result = operand1 > operand2
elif operator == "<":
result = operand1 < operand2
elif operator == ">=":
result = operand1 >= operand2
elif operator == "<=":
result = operand1 <= operand2
elif operator == "and":
result = operand1 and operand2
elif operator == "or":
result = operand1 or operand2
elif operator == "not":
result = not operand1
elif operator == "in":
if isinstance(operand1, (list, tuple, str)) and operand2 in operand1:
result = True
else:
result = False
elif operator == "is":
result = operand1 is operand2
elif operator == "is not":
result = operand1 is not operand2
else:
print("未知运算符")
continue
# 输出结果
print(f"结果:{result}")
except Exception as e:
print(f"发生错误:{e}")
continue
# 询问是否继续或退出
continue_calc = input("是否继续计算?(y继续/n退出):")
if continue_calc.lower() == 'n':
print("退出程序")
break
if __name__ == "__main__":
calculator()