Python基础
基础数据类型
bool
z = 1 < 100
print(z)
True
type(z)
bool
string
print("Hello World" + "!")
Hello World!
hello_world = "Hello World"
hello_world[:5]
'Hello'
hello_world.split()
['Hello', 'World']
list
family = [1.80, 1.68, 1.2, 1.5]
family[0]
[1.8, 1.68]
max(family)
1.8
print(family.count(1.8))
family.append(1.8)
family
1
[1.8, 1.68, 1.2, 1.5, 1.8]
family.count(1.8)
2
# python list could contain different types...which is a mess to me
fam_height = ["father", 1.78, "mother", 1.68, "son", 1.80, "daughter", 1.65]
print(type(fam_height))
<class 'list'>
# slicing
# choose all
print(fam_height[:])
# first two
fam_height[:2]
['father', 1.78, 'mother', 1.68, 'son', 1.8, 'daughter', 1.65]
['father', 1.78]
# modification
fam_height[0:2] = ['lao wang', 1.77]
print(fam_height)
['lao wang', 1.77, 'mother', 1.68, 'son', 1.8, 'daughter', 1.65]
# append
fam_height = fam_height + ['second_son', 0.51]
print(fam_height)
fam_height.append('dau in law')
fam_height.append(170)
print(fam_height)
['lao wang', 1.77, 'mother', 1.68, 'son', 1.8, 'daughter', 1.65, 'second_son', 0.51]
['lao wang', 1.77, 'mother', 1.68, 'son', 1.8, 'daughter', 1.65, 'second_son', 0.51, 'dau in law', 170]
# delete
del(fam_height[6:8])
print(fam_height)
['lao wang', 1.77, 'mother', 1.68, 'son', 1.8, 'second_son', 0.51, 'dau in law', 170]
# locating the first one or error if not exist
print(fam_height.index(1.77))
1
# duplication
list_dup = ['a'] * 10
print(list_dup)
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
# iteration
for w in hello_world.split():
print(w)
Hello
World
tuple
# unchangable
t = ('t', 'u', 'p', 'l', 'e')
# error if you want to change
# t[0] = 'd'
type(t)
tuple
# slicing
t[2:4]
('p', 'l')
# index
t.index('t')
0
dictionary
europe = {'spain':'madrid', 'france':'paris','germany':'berlin','italy':'rome'}
print(europe['germany'])
berlin
# show keys and values
europe.values()
europe.keys()
europe.items()
dict_items([('spain', 'madrid'), ('france', 'paris'), ('germany', 'berlin'), ('italy', 'rome')])
# 查找某个key的value
europe['france']
'paris'
# 这样更安全
europe.get('france')
'paris'
# 或者查找前,先判断key是否存在
'britain' in europe
False
# 还可以通过给定默认值,使不存在的查询也有返回值
europe.get('britain', 'unknown')
'unknown'
# 字典的增删改
europe['britain'] = 'london'
europe['britain'] = 'London'
del(europe['britain'])
# iteration
for country, capital in europe.items():
print(country + "'s capital is " + capital)
spain's capital is madrid
france's capital is paris
germany's capital is berlin
italy's capital is rome
基础函数
help & ?
help(max)
Help on built-in function max in module builtins:
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
?max
print
print("Hello World !")
Hello World !
weight = 60
height = 1.7
bmi = weight / height**2
print(bmi)
20.761245674740486
type 显示数据类型
type(weight)
int
type(bmi)
float
len
len(fam_height)
8
len(hello_world)
11
range
seq = range(0,9)
print(type(seq))
print(seq)
print(list(seq))
<class 'range'>
range(0, 9)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
流程控制
if
x = 0
if x > 0 :
print('x 是正数')
elif x < 0 :
print('x 是负数')
else :
print('x 是 0')
x 是 0
for
family = [1.78, 1.68, 1.80, 1.65]
for height in family :
print(height)
1.78
1.68
1.8
1.65
# get index with enumerate
for index, height in enumerate(family) :
print('index ' + str(index) + ': ' + str(height))
index 0: 1.78
index 1: 1.68
index 2: 1.8
index 3: 1.65
while
# 计算 1..10的和
num = 1 # 循环开始前初始一个值num
sum = 0
while num <= 10:
sum += num
num = num + 1
print(sum)
55
函数
def fib(n):
'''get a list of fibnacci series to n'''
a, b = 0, 1
result = []
while a<n:
result.append(a)
a, b = b, a+b
return result
fib(5)
[0, 1, 1, 2, 3]
网友评论