title: "An R Markdown document converted from "01.ipynb""
一、“单值”——标量类型
1.标量类型
1.1 整数类型int
a = 1
type(a)
1.2 浮点数值float
b = 11.0
type(b)
1.3 字符串类型str
c = "3"
type(c)
d =" python"
d = "python"
type(d)
'python'
type('python')
1.4 布尔值类型bool
b
a
a == b
a > 0
type(a > 0)
1.5 空值NoneType
e = None
type(e)
2.标量之间的互相转换
a = 2
type(a)
b = str(a)
b
type(b)
c = int(b)
c
type(c)
type(2.5)
int(2.5)
c="d"
int(c)
3.标量的运算
a = 3
b = 2
a + b
a / b
a // 4
a = "3"
b = "2"
c=a + b
c
int(c)
小结:
1.标量类型:int/float/str/bool/NoneType
2.标量之间的互相转换
3.标量的运算
二、数据结构
1.常见数据结构
1.1 元组,tuple()
a = (1, "2", 3)
type(a)
a[0]
type(a[1])
b =((1, 2, 3),(4, 5))
b[0]
1.2 列表,list[]
c = [1, 2 ,3]
type(c)
d = [(1,2,3),"python", 4, None]
d[0]
type(d[0])
type(d[2])
type(d[3])
1.3 字典,dict{}, keys-values, 键值对
d = {'a' : 'some value', 'b' : [1, 2, 3, 4]}
type(d)
d.keys()
d.keys()
d.values()
e = [1, 2, 3, "python", (4, 5)]
f = [6, 7, 8 ,9, 10, 11]
zip(e,f)
g = dict(zip(e,f))
g
h = list(zip(e,f))
h
zip(e,f)
1.4 集合,set{}
a = {1, 2, 3,4,4}
a
type(a)
b = {1, 2, 2, 3, 3,}
b
a == b
2.数据结构之间的互相转换
a = (1, 2, 3)
b = list(a)
b
c = tuple(b)
c
a == c
c = ((1, 2), (3, 4), (5, 6))
d = list(c)
d
e = dict(c)
e
f = dict(d)
f
d = list(f)
d
e = tuple(f)
e
d_ = list(f.values())
d_
小结:
一、单值——标量类型
1.标量类型:int/float/str/bool/None
2.标量之间的互相转换
3.标量的运算
二、数据结构
1.常见数据结构:tuple/list/dict/set
2.数据结构之间的互相转换
三、列表
1.列表list的运算
a = [1, 2 ,3]
b = [4, 5, 6]
a+b
a = (1, 2 ,3)
b = (4, 5, 6)
a + b
2.列表list的索引
a = [1, 2, 3, 4, (5, 6)]
a[0]
a[0] = "p"
a
a.append((7,8,9))
a
a[4]
a[5] = 5.5
a
a[-1]
a.append(9)
a
b = ([1, 2, 3], 4, 5)
b=list(b)
b.append(6)
b[0]
b[0].append(6)
b
3.列表list的方法及常用函数(删除,合并)
a = [2, 1, 3, 4, 5]
a.pop(4)
a
a.append("10 决策树.ipynb")
a
a.insert(1,5)
a
a.pop(-1)
a
sorted(a)
b = sorted(a)
b
a
c = list(reversed(a))
c
reversed(a)
四、函数
1.内置函数
a = [2, 1, 3, 4]
b = [5, 6, 7, 8]
sorted(a)
a
reversed(a)
zip(a,b)
list(zip(a,b))
2.自定义函数def
def qua(x, y):
return x*2 + y*2
qua(2, 3)
qua(5, 6)
3.lambda函数
quadratic_sum_ = lambda x, y: x**2 + y**2
quadratic_sum_(4, 5)
quadratic_sum_(3, 4)
五、控制流
1.for循环
list_ = [2, 1, 3, 5, 4, 6]
total = 0
for i in list_:
if i < 3:
total = total + i #total += i
total
total = 0
for i in list_:
if i == 5:
break
total = total + i
total
total=1
for i in list_:
if i==5:
continue
total=total+1
total = 0
for i in list_:
if i == 5:
continue
total += i
total
total = 0
for i in list_:
if i == 5:
pass
total += i
total
total = 0
for i in list_:
total += i
total
a = []
b = []
c = []
for i in list_:
if i < 3:
a.append(i)
elif i > 2 and i < 5:
b.append(i)
else:
c.append(i)
a
b
c
2.while循环
n = 100
total = 0
i = 1
while i <= n:
total = total + i
i += 1
n=100
total=0
i=1
while i <=n:
total=total+1
i+=1
total
n = 100
total = 0
i = 1
while i <= n:
total = total + i
i += 1
if i == 50:
break
total
total = 0
for i in list_:
while i == 5:
pass
total += i
小结:
一、单值——标量类型
1.标量类型:int/float/str/bool/None
2.标量之间的互相转换
3.标量的运算
二、数据结构
1.常见数据结构:tuple/list/dict/set
2.数据结构之间的互相转换
三、列表
1.列表list的索引
2.列表list的运算
3.列表list的属性方法及常用函数
四、函数
1.内置函数:sorted, reversed, zip
2.自定义函数
3.lambda函数
五、控制流
1.for循环:if, elif, else
break, pass, continue
2.while循环
六、Python内置库
import os
import sys
import re
import sqlite3
import tkinter
os.listdir()
os.listdir('input')
网友评论