Python学习笔记(1)

    xiaoxiao2021-03-25  73

    字符串格式化

    name = raw_input("name:") age = raw_input("age:") job = raw_input("job:") print("Information of %s:\nNmae:%s\nAge:%s\nJob:%s" %(name,name,age,job))

    另一种方式

    name = raw_input("name:") age = raw_input("age:") job = raw_input("job:") msg = ''' Information of %s: Name:%s Age:%s Job:%s ''' %(name,name,age,job) print(msg)

    stritp去二边的空格

    name = raw_input("name:").strip() age = raw_input("age:") job = raw_input("job:").strip() msg = ''' Information of %s: Name:%s Age:%s Job:%s ''' %(name,name,age,job) print(msg)

    列表常用操作

    带__的不可用,最后几个可用 >>> dir(name_list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] 添加 >>> name_list.append("ERIC") >>> name_list ['alex', '65brother', 'tenglan', 'ERIC'] >>> name_list.index("65brother") 1 添加一个65brother >>> name_list.append("65brother") 统计共有几个65brother >>> name_list.count("65brother") 2 insert在第二个位置添加66brother >>> name_list.insert(2,"66brother") >>> name_list ['alex', '65brother', '66brother', 'tenglan', 'ERIC', '65brother'] pop是每次删除最后一个 >>> name_list.pop() '65brother' >>> name_list.pop() 'ERIC' remove指定删除某个 >>> name_list ['alex', '65brother', '66brother', 'tenglan'] >>> name_list.remove("66brother") >>> name_list ['alex', '65brother', 'tenglan'] sort排序数字最靠前 >>> name_list.sort() >>> name_list ['65brother', 'alex', 'tenglan'] >>> name_list.append("*") >>> name_list.append("!") >>> name_list.append("_") >>> name_list.append("99") >>> name_list.append(55) >>> name_list ['65brother', 'alex', 'tenglan', '*', '!', '_', '99', 55] >>> name_list.append(99) >>> name_list.sort() >>> name_list [55, 99, '!', '*', '65brother', '99', '_', 'alex', 'tenglan'] 用循环删除里面的65brother >>> name_list.insert(5,'65brother') >>> name_list.insert(2,'65brother') >>> name_list [55, 99, '65brother', '!', '*', '65brother', '65brother', '99', '_', 'alex', 'tenglan'] >>> for i in range(name_list.count('65brother')): ... name_list.remove("65brother") ... >>> name_list [55, 99, '!', '*', '99', '_', 'alex', 'tenglan']

    列表后续操作

    切片,顾头不顾尾 >>> a = [1,2,3,'a','b'] >>> a.sort() >>> a [1, 2, 3, 'a', 'b'] >>> a[0:2] [1, 2] >>> a.insert(2,4) >>> a [1, 2, 4, 3, 'a', 'b'] >>> a[0:3] [1, 2, 4] 步进 >>> a [1, 2, 4, 3, 'a', 'b'] >>> a[0:5:3] [1, 3] >>> a[0:5:2] [1, 4, 'a'] 取出最后二个 >>> a [1, 2, 4, 3, 'a', 'b'] >>> a[-3:-1] [3, 'a'] >>> a[-2:-1] 此种方法不成 ['a'] >>> a[-2:] 取后二个 取前三个 >>> a [1, 2, 4, 3, 'a', 'b'] >>> a[:3] [1, 2, 4] 排序,在python3中要用此种方法 >>> a[0:4] 把数字单独提取出来 [1, 2, 4, 3] >>> b=a[0:4] >>> b [1, 2, 4, 3] >>> b.sort() >>> b [1, 2, 3, 4] 扩展列表 >>> a [1, 2, 4, 3, 'a', 'b'] >>> b [1, 2, 3, 4] >>> a + b 直接相加 [1, 2, 4, 3, 'a', 'b', 1, 2, 3, 4] >>> a.extend(b) 这种方法很方便 >>> a [1, 2, 4, 3, 'a', 'b', 1, 2, 3, 4] >>> name = "Alex Li" >>> a.extend(name) >>> a [1, 2, 4, 3, 'a', 'b', 1, 2, 3, 4, 'A', 'l', 'e', 'x', ' ', 'L', 'i']

    python3

    >>> range(10) range(0, 10)

    python2

    >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    判断a里是否有4如果有打印dddddd

    >>> if 4 in a: ... print("ddddddd") ... ddddddd

    元组

    列表是中括号,元组是小括号

    元组是只读的 >>> t = (1,2,3,4) >>> t (1, 2, 3, 4) >>> dir(t) 只有count,index可用 ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

    读写文件

    第一 f = open("test.log","w") f.write("This is the first line\n") f.write("This is the second line\n") f.write("This is the 3 line\n") f.write("This is the 4 line\n") 第二 f = open("test.log","r") print f.read() f.close() 第三 f = open("test.log","r") for line in f: if "3" in line: print "This is the third line" else: print line, f.close()
    转载请注明原文地址: https://ju.6miu.com/read-40969.html

    最新回复(0)