Python基础学习(一)
1. f 字符串 可以在字符串中输入变量
print(f"the distance is{sqrt((x1-x2)**2+(y1-y2)**2)}"
2. input函数
a = int(input("Please input a:")
当提示超过一行时,可以将提示赋值给一个变量,再将变量传递给input()
3. 提取整数的个位数
print(f"the last digit of the integer is {a%10}")
4. 提取整数的倒数第二位数字
//:取整 %:取余
a//10%10
5. 提取整数的倒数第n位数字
print(f"the {n}th last digit of the integer is {a//10(n-1)%10}")
6.markdown语法
“-” 表示点·
“**” 表示加粗
7.三元表达式
// 语法:a if ( ) else ( )
// 例:取绝对值
a = float(input("a = "))
print(f"the absolute value of {a} is {a if a > 0 else -a}")
8. if语句基础语法
- 注意:if-else 结构适用于执行两种操作之一。在if-else语句中,总是会执行两个操作中的一个。
- if-elif-else 结构将依次检查每个条件,测试通过后,将执行紧跟其后的代码,并跳过余下的测试。-
- 如果有多个条件为True且每个条件为True时都需要采取相应措施,则应该使用一系列简单if条件。
if ***:
pass
elif ***:
pass
else:
pass # 注意缩进格式
# 温度转换
tem = input("Please input temperature degrees (ending with C for Celsius and F for Fehrenheit): ")
tem, kind = float(tem[:-1]), tem[-1]
if kind == "C":
print(f"{tem} degrees in Celsius is {tem*1.8+32} degrees in Fehrenheit.")
elif kind == "F":
print(f"{tem} degrees in Celsius is {(tem-32)/1.8} degrees in Fehrenheit.")
else:
print("The input format is wrong")
age = 12
if age < 4:
price = 0
elif age < 18: # 当在这里判断时,意味着age已经>=4
price = 25
else:
price = 40
print(f"Your admission cost is ${price}.")
9. 修改字符串的大小写
.title() 字符串的所有首字母改为大写
.upper() 字符串全部改为大写
.lower() 字符串全部改为小写
.rstrip() 删除末尾的空白
.lstrip() 删除开头的空白
.strip() 删除字符串两边的空白
10. 逻辑运算符
python中的逻辑运算符是 and、or、not
11.内置help函数 或 函数后?
help('print')
str.replace?