Python基础学习(一)

本文介绍了Python的基础知识,包括f字符串的使用来插入变量,input函数获取用户输入,提取整数的个位数和倒数位数的方法,markdown语法的基本规则,三元表达式的应用,以及if语句和逻辑运算符的语法。此外,还提到了内置的help函数和字符串操作如大小写转换。
摘要由CSDN通过智能技术生成

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?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值