python学习-day2

列表:

   定义:在[]内,可以存放多个类型的值

   并以逗号隔开。

   一般用于存放学生的爱好,课堂的周期等

#定义一个学生列表,可存放多个学生
students = ['钱垚','李小龙','张全蛋']

print(students[1])#李小龙

student_info = ['杨波',84,'male',['泡8','喝酒']]
#取杨波同学所有爱好
print(student_info[3])
#取杨波同学第二个爱好
print(student_info[3][1])

 

一 数据类型:
    列表类型:
        定义:
            在[]内,可以存放多个任意类型的值,并以逗号隔开。
            一般用于存放学生的爱好,课堂的周期等等...
        优先掌握的操作:
            1、按索引存取值(正向存取+反向存取):即可存也可以取
            2、切片(顾头不顾尾,步长)
            3、长度
            4、成员运算in和not in
            5、追加
            6、删除
            7、循环
        需要掌握的:
            1、index
            2、count
            3、pop
            4、remove
            5、insert
            6、extend
    元组类型:
        定义:
            在()内,可以存放多个任意类型的值,并以逗号隔开。
        注意:
            元组与列表不一样的是,只能在定义时初始化值,不能对其进行修改。
        优点:
            在内存中占用的资源比列表要小。

    字典类型:
        作用:
            在{}内,可存放多个值,以key-value存取,取值速度快。
        定义:
            key必须是不可变类型,value可以是任意类型
# 4、成员运算in和not in
print('杨波' in student_info)#True
print('杨波' not in student_info)#False

#5、追加
student_info =['杨波', 72, 'male', ['泡8', '喝9']]
student_info.append('安徽最牛的学院,合肥学院')
print(student_info)

print(student_info[-2])
#['泡8', '喝9']
print(student_info[0:4:2])
#['杨波', 'male']
print('杨波' in student_info)#True
print('杨波' not in student_info)#False

#6、删除
del student_info[2]
print(student_info)'''

'''#需要掌握
student_info = ['123', 18, 'male', ['感悟', '喊麦'], 18]
# 1、index获取列表中某个值的索引
print(student_info.index(18))#1
# 2、count获取列表中某个值的数量
print(student_info.count(18))#2

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

sex = student_info.pop(2)
print(sex)
print(student_info)

#4、移除
student_info.remove(18)
print(student_info) #['123', ['感悟', '喊麦']]

name = student_info.remove('123')
print(name) #None
print(student_info)'''

#5、插入值
'''student_info = ['123',95,'male',['尬舞','喊麦'],95]
#在student_info中,索引为3的位置插入“合肥学院”
student_info.insert(3,'合肥学院')
print(student_info)'''

#6、extend 合并列表
'''student_info1 = ['123', 18, 'male', ['感悟1', '喊麦2'], 18]
student_info2 = ['456',95,'male',['尬舞','喊麦'],95]

student_info1.extend(student_info2)
print(student_info1)'''

元组:在()内,可以存放多个任意类型的值,并以逗号隔开。

注意:元组与列表不一样的是,只能在定义时初始化值,不能对其进行修改。

优点:在内存中占用的资源比列表要小。

'''tuple1 = (1, 2, 3, '五', '六')
print(tuple1)#(1, 2, 3, '五', '六')

print(tuple1[2])#3

print(tuple1[0:5:3])# (1, '五')

print(len(tuple1))#5

print(1 in tuple1)#True
print(1 not in tuple1)#False

for line in tuple1:
    print(line)'''

不可变类型:

#不可变类型
#int
'''number = 100
print(id(number)) #1978694208
number = 111
print(id(number)) #1978694560

#float
sal = 1.0
print(id(sal)) #2210740179472
sal = 2.0
print(id(sal)) #2210740179304


str1 = 'hello python!'
print(id(str1)) #2210741626416
str2 = str1.replace('hello', 'like')
print(id(str2)) #2210741626288'''

可变类型:

'''list1 = [1,2,3]
list2 = list1

list1.append(4)

print(id(list1))
print(id(list2))
print(list1)
print(list2)'''

字典类型:

在{ }内,以逗号隔开可存放多个值,以key-value存取,取值速度快。

定义:key必须是不可变类型,value可以是任意类型

'''dict1 = dict({'age':18,'name': 'nj'})
print(dict1) #{'age': 18, 'name': 'nj'}
print(type(dict1)) #<class 'dict'>

print(dict1['age']) #18

dict1['level'] = 9
print(dict1) #{'age': 18, 'name': 'nj', 'level': 9}
print(dict1['name']) #nj

print('name' in dict1) #True
print('nj' in dict1) #False
print('nj' not in dict1) #True

del dict1['level']
print(dict1) #{'age': 18, 'name': 'nj'}

print(dict1.keys()) #dict_keys(['age', 'name'])
print(dict1.values()) #dict_values([18, 'nj'])
print(dict1.items()) #dict_items([('age', 18), ('name', 'nj')])

for key in dict1:
    print(key) 
    print(dict1[key])

dict1 = {'age': 18,'name': 'nj'}

print(dict1.get('sex')) #None
print(dict1.get('sex','male')) #male'''
二 流程控制:
    if 判断:
        语法:
            if 判断条件:
                # 若条件成立,则执行此处代码
                逻辑代码
            elif 判断条件:
                # 若条件成立,则执行此处代码
                逻辑代码
            else:
                # 若以上判断都不成立,则执行此处代码
                逻辑代码
 
    while循环
        语法:
            while 条件判断:
                # 成立执行此处
                逻辑代码
    break  # 跳出本层循环
    continue  # 结束本次循环,进入下一次循环

循环:

# while循环
'''str1 = 'tank'
while True:
    name = input('请输入猜测字符: ').strip()
    if name == 'tank':
        print('tank success!')
        break

    print('请重新输入!')'''


#限制循环次数
'''str1 = 'nj'
num = 0

while num < 3:
    name = input('请输入猜测的字符:').strip()
    if name == 'nj':
        print('nj success!')
        break

    print('请重新输入!')
    num += 1'''

三 文件处理:
    open()
    写文件
        wt: 写文本
    读文件
        rt: 读文本
    追加写文件
        at: 追加文本
    注意: 必须指定字符编码,以什么方式写
        就得以什么方式打开。 如: utf-8
    执行python文件的过程:
        1.先启动python解释器,加载到内存中。
        2.把写好的python文件加载到解释器中。
        3.检测python语法,执行代码。
        SyntaxError: 语法错误!
    打开文件会产生两种资源:
        1.python程序
        2.操作系统打开文件
 
    文件处理之上下文管理:
        1、with可以管理open打开的文件,
        会在with执行完毕后自动调用close()关闭文件
        with open()
        2、with可以管理多个文件
 

文件处理:

'''with open('file.txt','w',encording='utf-8') as f:
    f.write('墨菲定律')

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

with open('file.txt','a',encording='utf-8') as f:
    f.write('围城')'''

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

jpg = res

with open('cxk_copy.jpg','wb') as f_w:
    f_w.write(jpg)

with open('cxk.jpg', 'rb') as f_r,open('cxk_copy.jpg','wb') as f_w:
    res = f_r.read()
    f_w.write(res)'''

四 函数
    什么是函数?
        函数指的其实一把工具。
    使用函数的好处:
        1.解决代码冗余问题。
        2.使代码的结构更清晰。
        3.易管理。
    函数的使用必须遵循: 先定义,后调用。
    函数定义语法:
        def 函数名(参数1, 参数2...):
            '''注释: 声明函数'''
            逻辑代码
            return 返回值
        def: defind 定义。
        函数名: 必须看其名知其意。
        (): 接收外部传入的参数。
        注释: 用来声明函数的作用。
        return: 返回给调用者的值。

函数:

#1、无参函数
'''def login():
    user = input('请输入用户名').strip()
    pwd = input('请输入密码').strip()

    if user == 'tank' and pwd == '123':
        print('login successful!')

    else:
        print('login error!')

print(login)

login()'''

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

    if user == username and pwd == password:
        print('login successful!')
        
    else:
        print('login error!')

login('tank', '123')'''


# 在定义阶段:位置形参
'''def func(x,y):
    print(x,y)
    
func(10,100)

def func(x,y):
    print(x,y)'''

#函数的嵌套定义
'''def func1():
    print('from func1...')

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

print(func1)

def f1():
    pass
def f2():
    pass

dic1 = {'1':f1,'2':f2}

choice = input('请选择功能编号:')
if choice == '1':
    print(dic1[choice])
    dic1[choice]()
    
elif choice == '2':
    print(dic1[choice])
    dic1[choice]()'''

 

posted @ 2019-06-25 23:54  寕1018  阅读(162)  评论(0)    收藏  举报