一.基础语法
变量定义规范:
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