目录
前言
主要从数据类型(数字 布尔 字符串)、容器(列表 字典 集合 元组)、函数、类四个方面整理了python的基础知识。
一、数据类型
1、数字类型(Numbers)
代表整数和浮点数,原理与其它语言相同。
x=5 # 直接定义数字
print(x) # 输出"5"
print(x+1) # 直接做运算 输出"6"
print(x**2) # 两个**表示幂 即5^2=25 输出"25"
x += 1 # x = x+1
x *= 2 # x = x*2
## python的数字类型比较特殊,没有x+和x-运算符
print(type(x)) # 输出"<class 'int'>"
y = 5.5
print(type(y)) # 输出"<class 'float'>"
2、布尔类型(Booleans)
pyhton中使用的是英文单词而不是符号
T = True # 直接定义
F = False
print(type(T)) # 输出"<class 'bool'>"
print(T and F) # 与,输出"False"
print(T or F) # 或,输出"True"
print(not F) # 非,输出"True"
print(T != F) # 输出"True"
3、字符串类型(Strings)
hello='hello'
world = "world" # 单双引号均可
print(hello) # Prints "hello"
print(len(hello)) # 字符串长度;prints"5"
hw=hello+world # 字符串拼接直接使用+
print(hw) # prints "hello world"
hw12 ='%s %s %d' % (hello, world, 12)
print(hw12) # prints "hello world 12"
s = 'beautiful'
# 大小写变换
print(s.upper()) # prints"BEAUTIFUL"
print(s.capitalize()) # prints"Beautiful"
# 输出调整
print(len(s)) # prints"9"
print(s.rjust(11)) # prints" beautiful"
print(s.center(11)) # prints" beautiful "
print(s.replace('iful','y')) # prints"beauty"
print(' beauty '.strip()) # prints"beauty"
二、容器
1、列表
列表其实就是Python中的数组,但是可以它可以动态的调整大小,并且可以包含不同类型的元素。
xs =