目录
print(*object,sep='',end='\n',......)
[input([prompt]), 输入的变量均为str字符串类型!](#input([prompt]), 输入的变量均为str字符串类型!)
一、python开发环境
python-3.8.6-amd64
二、python输入输出
(1)print输出函数
print(*object,sep='',end='\n',......)
*object: 这是一个可变参数,可以传入多个对象,这些对象会被依次打印出来。sep: 指定多个对象之间的分隔符,默认是一个空格。end: 指定打印结束后的字符,默认是换行符'\n'。
pythonprint("LanQiao",sep="needs",end="yuan\n") print("LanQiao","300",sep="needs",end="yuan")
(2)input输入函数
input([prompt]), 输入的变量均为str字符串类型!
在 Python 的文档和教程中,
input([prompt])中的[prompt]被方括号括起来,表示这是一个可选参数。这种表示方法是一种约定,用于指示该参数是可选的,而不是必须提供的。
pythoninput(["请输入:"]) print(type(input("未结束~")))
input()会读入一整行的信息
pythona=int(input("请输入:")) print((a,type(a))) b=int(input("请输入:")) # # input() 函数返回的是一个字符串, # 尝试将这个字符串直接转换为整数。 # 如果输入的内容包含空格或其他非数字字符, # 转换就会失败并抛出 ValueError 异常。 print(b)
例题1.1
海伦公式用于计算已知三边长的三角形面积。公式如下:
s=p(p−a)(p−b)(p−c)s=p(p−a)(p−b)(p−c)
其中,p 是半周长,计算公式为:
pythona = int(input("输入"));b = int(input("输入"));c = int(input("输入")) p = (a+b+c)/2 s=(p*(p-a)*(p-b)*(p-c))**0.5 print(s)
三、数据类型、运算符
数据类型
- int转float : 直接转换。例如,
int a = 5; float b = (float)a;,此时b的值为5.0。- float转int : 舍弃小数部分。例如,
float c = 3.7; int d = (int)c;,此时d的值为3。- int转bool : 非0转换为
True,0转换为False。例如,int e = 1; bool f = (bool)e;,此时f为True;如果e为0,则f为False。- bool转int :
False转换为0,True转换为1。例如,bool g = True; int h = (int)g;,此时h的值为1。- 转str : 直接转换。例如,
int i = 10; string j = (string)i;,此时j的值为"10"。
运算符
算术运算符:用于数学计算。
+:加法-:减法*:乘法/:除法//:整除(返回商的整数部分)%:求余(返回除法后的余数)**:幂(表示乘方运算)关系运算符:用于比较两个值。
>:大于<:小于==:等于!=:不等于>=:大于等于<=:小于等于赋值运算符:用于将值赋给变量。
=:赋值+=:加赋值(相当于x = x + y)-=:减赋值(相当于x = x - y)*=:乘赋值(相当于x = x * y)/=:除赋值(相当于x = x / y)%=:求余赋值(相当于x = x % y)//=:整除赋值(相当于x = x // y)**=:幂赋值(相当于x = x ** y)逻辑运算符:用于逻辑操作。
and:逻辑与or:逻辑或not:逻辑非成员运算符:用于判断一个值是否在序列中。
in:在...之中not in:不在...之中身份运算符:用于比较对象的身份(内存地址)。
is:是is not:不是例题1.2
运算器代码一
pythondef calculator(): while True: try: num1 = float(input("请输入第一个数字: ")) operator = input("请输入运算符 (+, -, *, /, //, %, **, >, <, ==,!=, >=, <=, and, or, not, is, is not): ") num2 = float(input("请输入第二个数字或值: ")) if operator in ('/', '//', '%') and num2 == 0: print("除数不能为零") continue operations = { '+': num1 + num2, '-': num1 - num2, '*': num1 * num2, '/': num1 / num2 if num2 else None, '//': num1 // num2 if num2 else None, '%': num1 % num2 if num2 else None, '**': num1 ** num2, '>': num1 > num2, '<': num1 < num2, '==': num1 == num2, '!=': num1 != num2, '>=': num1 >= num2, '<=': num1 <= num2, 'and': bool(num1) and bool(num2), 'or': bool(num1) or bool(num2), 'not': not bool(num1), 'is': num1 == num2, 'is not': num1 != num2 } if operator not in operations: print("无效的运算符,请重新输入。") continue result = operations[operator] print(f"结果是: {result}") if input("是否进行另一次计算?(y/n): ").lower() != 'y': break except ValueError: print("输入无效,请输入有效的数字或值。") if __name__ == "__main__": calculator()运算器代码二
pythondef calculator(): while True: try: num1 = input("请输入第一个数字: ") num1 = float(num1) operator = input( "请输入运算符 (+, -, *, /, //, %, **, >, <, ==,!=, >=, <=, and, or, not, is, is not): ") num2 = input("请输入第二个数字或值: ") num2 = float(num2) # 算术运算符 if operator == '+': result = num1 + num2 elif operator == '-': result = num1 - num2 elif operator == '*': result = num1 * num2 elif operator == '/': if num2 == 0: print("除数不能为零") continue result = num1 / num2 elif operator == '//': if num2 == 0: print("除数不能为零") continue result = num1 // num2 elif operator == '%': if num2 == 0: print("除数不能为零") continue result = num1 % num2 elif operator == '**': result = num1 ** num2 # 关系运算符 elif operator == '>': result = num1 > num2 elif operator == '<': result = num1 < num2 elif operator == '==': result = num1 == num2 elif operator == '!=': result = num1 != num2 elif operator == '>=': result = num1 >= num2 elif operator == '<=': result = num1 <= num2 # 逻辑运算符 elif operator == 'and': result = num1 and num2 elif operator == 'or': result = num1 or num2 elif operator == 'not': result = not num1 # 身份运算符 elif operator == 'is': result = num1 is num2 elif operator == 'is not': result = num1 is not num2 else: print("无效的运算符,请重新输入。") continue print(f"结果是: {result}") another_calculation = input("是否进行另一次计算?(y/n): ") if another_calculation.lower() != 'y': break except ValueError: print("输入无效,请输入有效的数字或值。") if __name__ == "__main__": calculator()


