#pass是空语句,一般用做占位语句,不做任何事情
for i in range(0,10):
print i
for letter in 'Room':
if letter == 'o':
pass
print('pass')
print(letter)
list = ['abcd', 786 , 2.23, 'runoob', 70.2]
#list[x:y]是从列表x到y之间的,不包括y位
print(list[1:3])
tinylist = [123, 'runoob']
print(list + tinylist)
t1 = ('abcd', 786 , 2.23, 'runoob', 70.2)
t2 = (1, )
t3 = ('a', 'b', ['A', 'B'])
t3[2][0] = 'X'
print(t3)
d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
print(d['Michael'])
s = set([1, 1, 2, 2, 3, 3])
print(s)
#7-1
import math
print(math.ceil(4.1)) #返回数字的上入整数
print(math.floor(4.9)) #返回数字的下舍整数
print(math.fabs(-10)) #返回数字的绝对值
print(math.sqrt(9)) #返回数字的平方根
print(math.exp(1)) #返回e的x次幂
#7-2
import random
ran = random.random()
print(ran)
print ("------- 设置种子 seed -------")
random.seed(10)
print ("Random number with seed 10 : ", random.random())
# 生成同一个随机数
random.seed(10)
print ("Random number with seed 10 : ", random.random())
#生成一个随机整数
ran = random.randint(1,20)
print(ran)
#7-3
a = "Hello "
b = "World "
print(a + b)
print(a * 3)
print(a[0])
print(a[1:4])
print('e' in a)
print('e' not in a)
new_str = '-'.join('Hello')
print(new_str)
print('Hello World!')
print("Hello World!")
print("The \t is a tab")
print('I\'m going to the movies')
print('''I'm going to the movies''')
html = '''
Friends CGI Demo
ERROR
%s
'''
print(html)
#声明一个列表
names = ['jack','tom','tonney','superman','jay']
#通过下标或索引获取元素
print(names[0])
print(names[1])
#获取最后一个元素
print(names[-1])
print(names[len(names)-1])
#获取第一个元素
print(names[-5])
#遍历列表,获取元素
for name in names:
print(name)
#查询names里面有没有superman
for name in names:
if name == 'superman':
print('有超人')
break
else:
print('没有超人')
#更简单的方法,来循环查询names里有没有superman
if 'superman' in names:
print('有超人')
else:
print('有超人')
#列表元素的添加
#声明一个空列表
girls = []
#append(),末尾追加
girls.append('杨超越')
print(girls)
#extend(),一次添加多个。把一个列表添加到另一个列表 ,列表合并。
models = ['刘雯','奚梦瑶']
girls.extend(models)
#girls = girls + models
print(girls)
#insert():指定位置添加,位置为元素的下标
girls.insert(1,'虞书欣')
print(girls)
#列表元素修改,通过下标找到元素,然后用=赋值
fruits = ['apple','pear','香蕉','pineapple','草莓']
print(fruits)
fruits[-1] = 'strawberry'
print(fruits)
'''
将fruits列表中的‘香蕉’替换为‘banana’
'''
#这个不能修改元素
for fruit in fruits:
if '香蕉' in fruit:
fruit = 'banana'
print(fruits)
#只能够通过索引来修改元素
for i in range(len(fruits)):
if '香蕉' in fruits[i]:
fruits[i] = 'banana'
break
print(fruits)
#列表元素删除
#1:del
words = ['cat','hello','pen','pencil','ruler']
del words[1]
print(words)
#2:remove
words = ['cat','hello','pen','pencil','ruler']
words.remove('cat')
print(words)
#3:pop(通过索引)
words = ['cat','hello','pen','pencil','ruler']
words.pop(1)
print(words)
animals = ['cat','dog','tiger','snake','mouse','bird']
print(animals[2:5])
print(animals[-1:])
print(animals[-3:-1])
print(animals[-5:-1:2])
print(animals[::2])
'''
生成10个不同的随机整数,并存至列表中
'''
import random
random_list = []
for i in range(10):
ran = random.randint(1,20)
if ran not in random_list:
random_list.append(ran)
print(random_list)
#以上代码中i没有发生变化,输出的列表只是一直在末尾添加了元素
#以下是改进并加入顺序的代码
random_list1 = []
i = 0
while i < 10:
ran = random.randint(1,20)
if ran not in random_list1:
random_list1.append(ran)
i+=1
print(random_list)
#默认升序
new_list = sorted(random_list)
print(new_list)
#降序
new_list = sorted(random_list,reverse =True)
print(new_list)
import random
tuple1 = ()
print(type(tuple1))
tuple3 = ('hello',)
print(type(tuple3))
random_list = []
for i in range(10):
ran = random.randint(1,20)
random_list.append(ran)
print(random_list)
random_tuple = tuple(random_list)
#元组的访问
print(random_tuple)
print(random_tuple)
print(random_tuple[0])
print(random_tuple[-1])
print(random_tuple[1:-3])
print(random_tuple[::-1])
#元组的修改另一种形式
t1 = (1,2,3)+(4,5)
print(t1)
t2 = (1,2) * 2
print(t2)
#元组的一些常用函数
print(max(random_tuple))
print(min(random_tuple))
print(sum(random_tuple))
print(len(random_tuple))
#统计元组中4的个数
print(random_tuple.count(4))
#元组中4所对应的下标,如果不存在,则会报错
print(random_tuple.index(4))
#判断元组中是否存在1这个元素
print(4 in random_tuple)
#返回元组中4所对应的下标,不会报错
if(4 in random_tuple):
print(random_tuple.index(4))
#定义一个元组
t3 = (1,2,3)
#将元组赋值给变量a,b,c(拆包)
a,b,c = t3
#打印a,b,c
print(a,b,c)
#当元组中元素个数与变量个数不一致时
#定义一个元组,包含5个元素
t4 = (1,2,3,4,5)
#将t4[0],t4[1]分别赋值给a,b;其余的元素装包后赋值给c(装包)
a,b,*c = t4
print(a,b,c)
print(c)
print(*c)
#定义一个空字典
dict1 = {}
dict2 = {'name':'杨超越','weight':45,'age':25}
print(dict2['name'])
#list可以转成字典,但前提是列表中元素都要成对出现
dict3 = dict([('name','杨超越'),('weight',45)])
print(dict3)
dict4 = {}
dict4['name'] = '虞书欣'
dict4['weight'] = 43
print(dict4)
dict4['weight'] = 44
print(dict4)
#字典里的函数 items() keys() values()
dict5 = {'杨超越':165,'虞书欣':166,'上官喜爱':164}
print(dict5.items())
for key,value in dict5.items():
if value > 165:
print(key)
#values() 取出字典中所有的值,保存到列表中
results = dict5.values()
print(results)
names = dict5.keys()
print(names)
#求小姐姐的平均身高
heights = dict5.values()
print(heights)
total = sum(heights)
avg = total/len(heights)
print(avg)
#如果能够取到值,则返回字典中的值,否则返回默认值170
print(dict5.get('赵小棠'))
print(dict5.get('赵小棠',170))
#字典中的两种删除方式
dict6 = {'杨超越':165,'虞书欣':166,'上官喜爱':164}
del dict6['杨超越']
print(dict6)
result = dict6.pop('虞书欣')
print(result)
print(dict6)