day02 python基础2

students= ['高某某','石谋谋','王某','孙某某']
print(students[0])

student_info = ['高某某', 18, 'woman', ['唱', '跳', 'rap'], 18]
print(student_info[3])
print(student_info[3][0])
print(student_info[0:4:2])
student_info.append('nb')
print(student_info)
del student_info[2]
print(student_info)

# #index获取列表某个值的索引
print(student_info.index(18))
#
# #count获取列表中某个值的数量
print(student_info.count(18))

# 取值,默认取列表中最后一个值
# 若pop()括号中写了索引,则取索引对应的值
student_info.pop()
print(student_info)
student_info.pop(2)
print(student_info)

# 移除,移除列表中一个值
student_info.remove(18)
print(student_info)

# 插入
# 在student_info中索引为3的位置插入
student_info.insert(3, '合肥学院')
print(student_info)

# extend 合并列表
student_info1 = ['高某某', 18, 'woman', ['唱', '跳', 'rap', '篮球']]
student_info2 = ['孙某某', 18, 'male', ['唱', '跳', 'rap']]
student_info1.extend(student_info2)
print(student_info1)

# 元组
tuple1 = (1, 2, 3, '高某某')
print(tuple1)
print(tuple1[0:4:3])

# 字典
# hey必须是不可变类型,value可以是任意类型
dict1 = dict({'age': 18, 'name': '高某某'})
print(type(dict1))

# 字典取值,字典名+[]
print(dict1['age'])
#
# #存
dict1['level'] = 6
print(dict1)
#
# #获取字典中key(),value(),键值items()
print(dict1.keys())
print(dict1.values())
print(dict1.items())

# get
print(dict1.get('name'))
# 若找不到可以设置一个默认值
print(dict1.get('sex', 0))

while True:
name = input('请输入猜测的字符:').strip()
if name == 'tank':
print('tank success!')
break
print('请重新输入!')
num += 1

# 文件
f = open('file.txt', mode='wt', encoding='utf-8')
with open('file.txt', mode='wt', encoding='utf-8') as f:
f.write('fatenanshan')

with open('filel.txt', 'w', encoding='utf-8') as f:
f.write('fatenanshan')

with open('filel.txt', 'r', encoding='utf-8') as f:
res = f.read()
print(res)

with open('1.jpg', 'rb') as f:
res = f.read()
print()


# 函数
# 1.无参函数
def login():
user = input('请输入用户名').strip()
pwd = input('请输入密码').strip()
if user == 'wj' and pwd == '123':
print('login successful')
else:
print('error')


login()


# 2.有参函数
def login(username, password):
user = input('请输入用户名').strip()
pwd = input('请输入密码').strip()
if user == username and pwd == password:
print('login successful')
else:
print('error')


login('wj', '123')


# 3.空函数

def fun(x, y):
print(x, y)


fun(18, 23) # 不能少传,不能多传


# 默认参数
def foo(x=10, y=20):
print(x, y)


foo() # 不传参,则使用默认参数
foo(200, 300) # 传参,使用传入参数

'''
函数的嵌套定义:在函数定义函数
函数对象:函数的内存地址
函数的名称空间:
内置:Python解析器自带的
全局:所有顶头写的变量、函数
局部:函数内部定义的
名称空间加载顺序:
内置--->全局--->局部
名称空间查找顺序:
局部--->全局--->内置
'''
x = 20


def func1():
print('from funcl...')
print(x)
# x=30 代码是由上自下执行,print(x)无法引用x=30


def dunc2():
print('from func2...')


func1()

猜你喜欢

转载自www.cnblogs.com/fatenanshan/p/11086816.html
今日推荐