学习顺序
输入输出
#运行后会出现一个输入框,并且把输入值赋值给变量name
name = input("输入姓名")
#print除了能输出内容外,还能自动换行
print("111")
print()
#表示不让自动换行,在结果后面加上end得内容
print("111",end="")
"""
这是多行注释
"""
python数据类型
1.数值类型
在python里面二进制都是以0b或者0B开头的
八进制0o,十六进制0X
幂运算
pow(2,3)
2的三次方
在python想写5*10的8次方,用科学计数法:5E8
浮点型
取值范围-10 308次方到10 308次方
四舍五入
round(10.55,1)
10.6
保留一位小数
运算操作符以及一些运算函数
# 把10/3得到的结果取整数3.3333 用//就是3
10//3
# 就是相当于pow(10,3) 1000
10**3
通过type(x)可以查看数据类型
int(x),float(x)强转
math库
还有很多对数计算,三角函数计算等等
import math
math.ceil(10.2)
结果11,这个和round(10.2)的区别是ceil是向上取整,round是四舍五入。
字符串
用'',""都表示字符串,字符串主要就是索引和切片。
遍历字符串里的每个字符
for ch in "hello":
print(ch)
print("hello,"[0:3])
print("hello,"[:3])
# 打印倒数第一个字符
print("hello,"[-1])
# 从开始打印到倒数第二个
print("hello,"[0:-1])
print(ord('a')) 97
print(chr(97)) a
列表
python的列表里面可以放各种类型
list = [2,'A',1]
print(type(list))
list.append('new append')
print(list)
# 指定内容删除
list.remove('A')
print(list)
# 指定位置删除
list.pop(0)
print(list)
# 指定位置添加
list.insert(0,'INSERT')
print(list)
# del函数也能删除
del list[0]
print(list)
元祖
list = [2,'A',1] 他不能在改变了
字典
也就是java里的hashmap 创建字典的两个方法
第二种是通过数组的方式建立
hash = {1:'A',2:'B'}
print(hash)
{1: 'A', 2: 'B'}
key = [1,2,3]
value = ['A','B','C']
dict = {}
for k,v in zip(key,value):
dict[k] = v
print(dict)
# 两种获取值的方法,都是通过key
print(dict.get(1))
print(dict[1])
print(dict.get(5))
A
A
None
判断key是否存在dict中
print (1 in dict)
print (4 in dict)
# 输出所有的key和value,以及所有的key和value
print(dict.keys())
print(dict.values())
print(dict.items())
集合
set_Q = {'A','B','C'}
set_W = {'B','C','D'}
print(set_Q | set_W) #并集
print(set_Q & set_W) #交集
print(set_Q - set_W) #差集
^ 异或
python语句、函数、对象
python 的and相当于java的&&,or相当于 ||
for、while循环
打印乘法表
for i in range(1,10):
for j in range(1,i+1):
print(i + '*' + j + '=' + i*j + " ",end="")
print()
i = 1
while i <= 5:
print('*' * i)
i+=1
*
**
***
****
*****
函数
也可以polynomial(y=2,x=1,z=3)
不输入z就用默认的
多参数函数
def he(x,*args):
for i in args:
print(i)
return
he(0,1,2,3)
#多参数也能把列表当做参数
list = [1,2,3]
he(0,*list)