python+Mosh网课笔记01入门+02基本类型

太久没写python代码了,学机器学习重新拾起python,笔记比较简陋。

参考:mosh的python教程

一、入门

  • 用vscode编写代码。
  • 下载了autopep8插件用于代码格式化。
  • 下载了pylint插件用于代码报错提示。

二、基本类型

  • int,bool(True,False),string,float
  • 注释 单行# 多行''' '''

string

python 复制代码
course = "python programming"
print(len(course))  # 长度
print(course[0])
print(course[-1])  # 返回字符串最后一个字母。
print(course[0:3])  # 返回从0开始 3个字符。
print(course)

first = "Mosh"
last = "Hamedani"
full = f"{first} {last}"  # f用作格式化
print(full)

course = "python programming  "
print(course.upper())  # 所有字母大写
print(course.lower())  # 所有字母小写
print(course.title())  # 每个单词首字母大写
print(course.strip())  # 开头和结尾都删除空格
print(course.rstrip())  # 删除结尾的空格
print(course.find("pro"))  # 返回字符出现的位置,没有则返回-1
print(course.replace("p", "j"))  # 用后面这个字符替换前面那个。
print("pro" in course)  # 返回True,False

numbers

  • 加减乘除运算 + - * /
python 复制代码
import math
print(10/3)  # 输出无限循环小数。
print(10//3)  # 向下取整。输出3
print(10 ** 3)  # **是指数。输出1000

print(round(2.9))  # 向上取整。
print(abs(-2.9))  # 绝对值

print(math.ceil(2.2))  # 向上取。

类型转换

python 复制代码
x = input("x= ")
print(type(x))  # 默认str类型
y = int(x)+1  # str转换为int类型

bool(0)  # False
# bool 除了0以外,其他数字都是True
bool('')  # 空字符是False,有字符是True
相关推荐
明月_清风4 小时前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风4 小时前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风1 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风1 天前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly2 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling2 天前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook2 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风2 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风2 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python