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

部署运行你感兴趣的模型镜像

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

参考:mosh的python教程

一、入门

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

二、基本类型

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

string

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

  • 加减乘除运算 + - * /
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))  # 向上取。

类型转换

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

bool(0)  # False
# bool 除了0以外,其他数字都是True
bool('')  # 空字符是False,有字符是True

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

### Python Mosh 教程笔记 #### 函数定义与调用 函数是一段可重复使用的代码块,用于执行特定的任务。在 Python 中,可以使用 `def` 关键字来定义函数。 ```python def greet(name): """ 打印问候语。 参数: name (str): 被问候人的名字。 """ print(f"Hello, {name}!") ``` 此函数接受一个参数并打印一条消息[^1]。 #### 条件判断 条件结构允许程序基于不同情况做出不同的行为。Python 使用 `if`, `elif`, 和 `else` 语句实现条件逻辑: ```python age = 20 if age >= 18: print("成年人") elif age < 13: print("儿童") else: print("青少年") ``` 这段代码会根据年龄变量的值输出相应的描述[^2]。 #### 循环结构 循环使得能够多次执行一段代码直到满足某个条件为止。常见的两种形式为 `for` 循环和 `while` 循环: ```python # For loop example fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # While loop example count = 0 while count < 5: print(count) count += 1 ``` 这些例子展示了如何遍历列表以及通过计数器控制循环次数. #### 文件操作 处理文件是编程中的基本需求之一,在 Python 中可以通过内置模块轻松完成读写文件的操作: ```python with open('example.txt', 'w') as file: file.write('这是一个测试') with open('example.txt', 'r') as file: content = file.read() print(content) ``` 这里展示了一个简单的创建新文本文件并向其中写入字符串的过程;接着再次打开该文件以只读模式读取其内容.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值