- Python历史背景
Guido van rossum "龟叔, 1989年
"简单,明确,优雅"
解释型语言
高级语言
- Python的基本数据类型
内置类型:
布尔类型:True False
数字类型:int float
文本类型:str
序列类型:list tuple range
集合类型:set
映射类型:dict
1.布尔类型:True False
2.数字类型:int float
# 定义一个整型变量
a = 1
b = int(100)
s = '100'
s = int(s) # 强转
print(type(s))
print(type(a))
print(type(b))
a = 10.2
print(type(a))
运算符优先级:
单目运算符:~ + -(正负)
算术运算符:** / * // % + -
位运算符:& | ^ >> <<
关系运算符:> >= < <= == !=
赋值运算符:= += -= /= *= //= %= **=
身份运算符:is is not
成员运算符:in not in
逻辑运算符:not and or
位运算符:& | ^ >> <<
# 位运算(& | ^ ~ << >>)
a = '100'
print(int(a, 2))
b = 100
print(bin(b))
c = 66
print(bin(c))
print(b & c)
print(b | c)
print(b ^ c)
关系运算符:> >= < <= == !=
# 关系运算符(> >= < <= == !=) 逻辑运算符(not and or)
print(10 > 100)
print(10 > 3 and 10 < 5)
print(-8 and 5)
print(0 and 5)
print(5 and 100)
print(5 and 100 < 5)
print(0 and 100 < 5)
print(10 > 5 and 2)
print('**********************')
print(-8 or 5)
print(0 or 5)
print(5 or 100)
print(5 or 100 < 5)
print(0 or 100 < 5)
print(10 > 5 or 2)
print('**********************')
print(10 and 5 or 0)
print(10 or 5 and 0)
print(not 5 and 10 > 5 or 100)
a = 100
b = a
print(a is b)
print(a, b)
a = 200
b = 200
print(a is b)
print(a, b)
赋值运算符:= += -= /= *= //= %= **=
# 赋值运算 # a = a + 2 a += 2 # -= *= /= //= %= print(a)
print(1+True)
身份运算符:is is not
成员运算符:in not in
逻辑运算符:not and or
a = 10
b = 3.0
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
函数:
int()
divmod()
abs()
# 函数
print(abs(-100))
print(pow(2, 8))
print(divmod(8, 3))
3.字符串:str
定义:'' "" '''''' """"""
# 字符串类型
# 定义
s1 = 'hi'
s2 = "hello"
s3 = """moring"""
s4 = '''moring
good'''
print(s1, s2, s3, s4)
s = str(100)
print(type(s))
运算符::+ / *n / in / not in
# 字符串中应用的运算符
print(s1 + s2)
print(s1 * 5)
s1 += s2
print(s1, s2)
# 成员运算符
print('hi' in s1)
print('hg' not in s1)
有序的----》索引(0) "abc"
s[index] 0<= index < len(s) -len(s)<= index <= -1
切片:
s[start:end:step]
start<= index < end----> index += step
# 索引和切片 正向递增(0~len(s)-1) 反向递减(-1~-len(s)) s = 'python' print(s[0]) print(s[-1]) print(s[1:3]) # s[a:b] a<=<b s[start=0:end=len(s):step=1]
print(s[-3:-1])
print(s[-1:-3:-1])
print(s[:3])
print(s[:])
print(s[::2])
函数:
len()
max() / min() / .index() / .count()
# 函数
print(s[len(s)-1])
print(max(s))
print(min(s))
print(s.index('h'))
print(s.count('n'))
.format()
# 格式化字符串
n = 5
s = '我今天瘦了%d两'% n
print(s)
print('我今天%s高兴' % '很')
height=169
weight=80.5
print('我的身高是{}cm,体重是{}kg'.format(height, weight))
print('我的身高是{1}cm,体重是{0}kg'.format(weight, height))
name = 'python'
age = 29
print('我的名字{nameid}, 年龄是{ageid}'.format(ageid=age, nameid=name))
s = '我太帅了'
print('{:*>20}'.format(s))
print('{:*<20}'.format(s))
print('{:*^20}'.format(s))
f = 10.8998765
print('{:.2f}'.format(f))
4.序列类型:list tuple range
5.集合:set
6.映射类型:dict
- 读入和输出
input()
print()
# 读入
s = input('请说的什么:')
print(type(s))
print(s)
n = int(s)
print(type(n), n)
s = input('请输入两个整型数:')
print(s)
a, b = eval(s) # 将字符串'num1, num2'转换为整型数num1, num2
print(a, b)
# 两个数的交换
b = b ^ c
c = b ^ c
b = b ^ c
print(b, c)
a = 4
print(a << 2)
print(a >> 2)