一.python的输入
1.input函数:
给变量赋值:如pw=input(),将input的返回值赋给变量pw,但由于缺少提示信息,因此pw的意义不明晰。
pw = input('please enter your password') print(f'the password you entered is {pw}')
这样打出来的代码会比较好一点,但是要注意到,input的返回值是string类型而非number类型.
要想更好的利用input的函数,可以使用类型转换:
# 1.Input the price of apple price = input('input the price of apple:$') # 2.Get the weight of the apples weight = input('input the weight of apple:kg') # 3.Output the result print(f'The total price of apple is ${float(price)*float(weight)}')
这段代码用于计算苹果的总价格(单价X重量)使用input函数输入苹果的重量和价格,最后将他们转换为浮点数进行运算并输出.
此外,还有eval函数,eval('1')还原字符串为原来的数据类型,如‘1’转换为1.(前提是,这个字符串能够被转换,单引号里可以是变量名或者值)
ev=eval('1'),此时ev=1.
str(x),把任何的值转换为字符串.
释放变量:del(num)
二.Python运算符
与其他语言差异不大,主要的是Python console控制台。
2**3,2的3次方;
5.0//2.0=2.0 //是整除符号
逻辑运算符,基本的逻辑运算符与其他语言的相同,不同的是:
&&变为and,||变为or,!变为not