ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

day02 python基础

2019-06-25 21:01:09  阅读:210  来源: 互联网

标签:info dict1 key python day02 基础 student print Yuan


列表:
    定义:在[]内,可以存放多个任意类型的值,并以逗号隔开。
 
#定义一个学生列表,可以存放多个学生
    students = ['sally','lin','anser','rola']
    print(students[1])   #lin
    student_info = ['Yuan',22,'male',['泡8','喝9']]
    print(student_info[3])
    #取杨波的第二个爱好
    print(student_info[3][1])

#成员运算
    print('Yuan' in student_info)  #True
    print('Yuan' not int student_info)   #False
  
#追加
    student_info = ['Yuan',22,'male',['泡8','喝9']]
    student_info.append = ('安徽最牛的学校,合肥学院')

#删除
    student_info = ['Yuan',22,'male',['泡8','喝9']]
    print(student_info[3]  #取Yuan所有的爱好
    print(student_info[3][1])   #取Yuan的第二个爱好

优先掌握的操作:
    1、按索引取值;既可存也可取
        print(student_info[-2]
    2、切片(顾头不顾尾,步长)
        print(student_info[0:4:2])
    3、长度
        print(len(student_info))
    4、成员运算in和not in
        print('Yuan' in student_info)
        print('Yuan' not in student_info)
    5、追加
        student_info = ['Yuan',22,'male',['泡8','喝9']]
        student_info.append('安徽最牛逼的学校,合肥学院')
        print(student_info)
    6、删除
        del student_info[2]
        print(student_info)
    7、循环
        for student in student_info:
        print(student)

#需要掌握的
    student_info = ['Yuan',22,'female',['泡8','喝9'],11]

    #获取列表中某个值的索引
    print(student_info.index(11))

    #获取列表中某个值的数量
    print(student_info.count(11))

    #取值,默认取值列表中最后一个值,类似删除
    #若pop()中写了索引,则索引对应的值
    student_info.pop()
    print(student_info)
    #取出列表中索引为2的值,并赋值给sex变量
    sex = student_info.pop(2)
    print(sex)
    print(student_info)

    #移除,把列表中的某个值的第一个值移除
    student_info.remove(95)
    print(student_info)
    name = student_info.remove('Yuan')
    print(name)
    print(student_info)

    #插入值
    student_info = ['Yuan',22,'female',['泡8','喝9'],11]
    #在student_info中,索引为3的位置插入“合肥学院”
    student_info.insert(3.'合肥学院')
    print(student_info)
   
    #extend 合并列表
    student_info1 = ['Yuan',22,'female',['泡8','喝9'],11]
    student_info 2= ['Xin',11,'male',['泡8','喝9'],22]
    #把student_info2所有的值插入到student_info1中
    student_info1.extend(student_info2)
    print(student_info1)
    #list(['夜','缘','天','墟'])

元组:
   定义:
      在()内,可以存放多个任意的值,并以逗号隔开。
   注意:
      元组与列表不同的是,只能在定义时初始化值,不能对其进行修改。
   优点:
      在内存中占用的资源比列表要小。
   
   # tuple(1,2,3,'五','六')  
   tuple1 = (1,2,3,'五','六')
   print(tuple1)

优先掌握操作:
   1、按索引取值(只能取)
      print(tuple1[2])
   2、切片(顾头不顾尾,步长)
      print(tuple1[0:5:3]
   3、长度
      print(len(tuple1))
   4、成员运算in和not in
      print(1 in tuple1)
      print(1 not in tuple)
   5、循环
      for line in tuple1:
         print(line)
         print(line,end='_')

不可变类型:
   变量的值修改后,内存地址一定不一样。
   数字类型:
      int
      float
   字符串类型:
      str
   元组类型:
      tuple
可变类型:
   列表类型:
      list
   字典类型:
      dict
 
   number = 100
   print(id(number))
   number = 111
   print(id(number))

   sa1 = 1.0
   print(id(sa1))
   sa1 = 2.0
   print(id(sa1))

   str1 = 'hello yuan!'
   print(id(str1))
   str2 =str1.replace( 'yuan','yu')
   print(id(str2))
   
   list1 = [1,2,3,4]
   print(id(list))
   list2 = list1
   list1.append(5)
   print(id(list1))
   print(list1)
   print(list2)
   
字典类型:
   作用:
      在{}内,以逗号隔开,可存放多个值。
      以key-value存取,取值速度快。
   定义:
      key必须是不可变类型,value可以是任意类型。

      dict1 = dict({'age':22,'name':'Yu'})
      print(dict1)
      print(type(dict1))

   #取值,字典名 + [],括号内写值对应的key
      print(dict1['age'])

#优先掌握的操作
1、按key存取值:可存可取
    #存一个level:9的值到dict1字典中
dict1['level'] = 9
print(dict1) #{'age':18,'name':'Yuan','level':9}
print(dict1['name']) #Yuan

2、长度
    print(len(dict1))

3、成员运算in和not in只判断字典中的key
    print('name' in dict1)
    print('Yuan' in dict1)
    print('Yuan' not in dict1)

4、删除
    del dict1['level']
    print(dict1) # {'age':18,'name':'Yuan'}
 
5、键key(),值values(),键值对items()

#得到字典中所有key
    print(dict1.keys())
#得到字典中所有值values
    print(dict1.values())
#得到字典中所有的items
    print(dict1.items())

6、循环
#循环遍历字典中所有的key
 
for key in dict1:
    print(key)
    print(dict1[key])

#get
dict1 = {'age':22,'name':'Yuan'}
print(dict1.get('age'))

# [] 取值
print(dict1['sex'])  #KeyError:'sex'

#get取值
print(dict1.get('sex')) #None
#若找不到sex,为其设置一个默认值
print(dict1.get('sex','male'))

if判断:
    语法:
        if 判断条件:
            #若条件成立,则执行此处代码
            逻辑代码
        elif 判断条件:
            #若条件成立,则执行此处代码
            逻辑代码

        else:
            #若以上条件都不成立,则执行此处代码
            逻辑代码

#判断两数大小
x = 10
y = 20
z = 30
#缩进快捷键,tab往右移四个空格,shift + tab往左移四个空格
if x>y:
    print(x)
elif z>y:
    print(z)
else
    print(y)



while循环
     语法:
         while 条件判断:
              #成立则执行此处
              逻辑代码

break   #跳出本层循环
continue    #结束本次循环,进入下一次循环

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

#限制循环次数
str1 = 'Yuan'
#初始值
num = 0
while num<3:
     name = input('请输入猜测的字符:').strip()
     if name == 'Yuan':
         print('Yuan success!')
         break
     print('请重新输入!')
     num +=1

追加写文本文件:
      a = open('file.txt','a',endcoding='utf-8')
      a.write('\n 合肥学院')
      a.close()

文件处理之上下文处理:

      # with可以管理open打开的文件

      会在with执行完毕后自动调用close()关闭文件。

      with open()

with open() as f "句柄"
  
#写
with open('file.txt','w',endcoding='utf-8') as f:
    f.write('墨菲定律')

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

#追加
with open('file.txt','r',endcoding='utf-8') as f:
    f.write('围城')
    # f.close()



#读取相片cxk.jpg
with open('cxk.jpg','rb',) as f:
    res = f.read()
    print(res)

jpg = res


#把cxk.jpg的二进制流写入cxk.jpg文件中
with open('cxk_copy.jpg','wb',) as f_w:
    f_w.write(jpg)

with 管理多个文件:
    #通过with来管理open打开的两个文件用句柄f_w,f_r
with open('cxk.jpg','rb') as f_r, open('cxk_copy.jpg','wb') as f_w:
    #通过f_r句柄把图片的二进制流读取出来
    res = f_r.read()
    #通过f_w句柄把图片的二进制流写入cxk_copy.jpg文件中
    f_w.write(res)

func(x=10,y=100)  

默认参数:
    在定义阶段,为参数设置默认值
  
def foo(x=10,y=200)
    print(x,y)
#不传参,则使用默认参数
foo()
#传参,使用传入的参数
foo(200,300)

函数的嵌套定义:
    在函数内部定义函数

函数对象:
    
函数的名称空间:
    内置:
        Python解析器自带的都称之为“内置名称空间”
    全局:
        所有顶着头写的变量,函数...都称之为“全名称空间”。
    局部:
        在函数内部定义的,都称之为“局部名称空间”。
    名称空间加载顺序:
        内置-->全局-->局部
    查找顺序:
        局部-->全局-->内置



函数嵌套定义:
  
def func1():
    print('from func1...')
    def func2():
        pritn('from func2...)
func1() 

#函数对象
print(func1)
def f1():
    pass
def f2():
    pass
dict1 = {'1':f1,'2':f2}
choice = input{'请选择功能编号:'}
if choice == '1':
    print(dic1[choice])
    dic1[choice]()

elif choice == '2':
   print(dic1(choice))
   dic1[choice]{}

#名称空间
#函数的嵌套定义
def func1():
    x=20
    print('from func1...')
    print(x)
    def func2():
        pritn('from func2...)

func1()

 

标签:info,dict1,key,python,day02,基础,student,print,Yuan
来源: https://www.cnblogs.com/weixinyuan/p/11086566.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有