今天我们开始学习python3编程之python基础

一.基础语法

变量定义规范:

python是弱类型语言,变量定义之前是不需要声明的

1.变量只能包含字母、数字和下划线,且不能以数字开头

2.变量区分大小写

3.变量名不能包含空格、连字符和特殊字符,不能使用python的关键字例如:if

查看python的关键字用以下命令查看

python 复制代码
import keyword
print(keyword.kwlist)

python的字符串引用有:" "、' '、""" """、''' '''.在python里单引号和双引号的效果是一样的

变量值对调

python 复制代码
a = 10
b = 20
a, b = b, a
print(a, b)

二.数据类型

1.number 数字类型,不可变

int 整型 :n1=10

float 浮点型 :n2=3.14

complex 复数 :1 + 1j

2.string 字符串类型,不可变

str1 = "hello"

3.bool 布尔型

b1 = True

b2 = False

4.list 列表类型,可变

list1 = [1,2,3"hello",Ture]

list2 = [1,[2,3],4]

5.tuple 元组,不可变

tuple1 = (1,2,3"hello",False)

6.dict 字典类型,可变 key:value 键值对 key唯一 key不可变

dict1 = {"name": "张三", "age": 20, "isStudent": Ture}

7.set 集合,可变 无序 不重复

set1 = {1,2,3,4,5,5,6,4,5}

print(set1)

三.数学运算

运算符号:+ - * / // % **

python 复制代码
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
13                  加法 
7                   减法
30                  乘法
3.3333333333333335  除法 # python对浮点数的精度不够
3                   整除
1                   取余
1000                幂运算

字符串运算,拼接

python 复制代码
str1 = "hello"
str2 = "world"
str3 = str1 + str2
print(str3)
helloworld

重复

python 复制代码
str4 = str1 * 3
print(str4)
hellohellohello
相关推荐
Johny_Zhao6 小时前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
IVEN_14 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang15 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮15 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling15 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮19 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽19 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python