python之(20)基础总结(3)
发布日期:2021-05-14 00:18:19 浏览次数:16 分类:博客文章

本文共 40429 字,大约阅读时间需要 134 分钟。

1���Python3 ������

���������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������Python������������������������������������print()���������������������������������������������������������������������������


1���������������������

���������������������������������������������������������������������������������

  • ������������������ def ��������������������������������������������������������� ()���
  • ������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������
  • ��������������������� : ������������������������
  • return [���������] ��������������������������������������������������������������������������� return ��������������� None���


������

Python ������������������ def ���������������������������������

def ���������������������������:    ���������

������������������������������������������������������������������������������������������������

def max(a, b):    if a > b:        return a    else:        return b a = 4b = 5print(max(a, b))

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=57220import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')5

2���������������

1������������(mutable)���������������(immutable)������

��� python ������strings, tuples, ��� numbers ������������������������������ list,dict ���������������������������������

  • ������������������������������ a=5 ������������ a=10��������������������������������� int ��������� 10��������� a ��������������� 5 ������������������������ a ������������������������������ a���

  • ��������������������������� la=[1,2,3,4] ������������ la[2]=5 ��������� list la ������������������������������������la���������������������������������������������������������

python ������������������������

  • ������������������������ C++ ������������������ ��������������������������������� fun(a)������������������ a ��������������������� a ������������������������ fun(a)��������������� a ��������������������������������� a���

  • ��������������������� C++ ��������������������� ��������������������� fun(la)������������ la ������������������������������ fun ��������� la ���������������

python ���������������������������������������������������������������������������������������������������������������������������������������

2���python ������������������������

������ id() ������������������������������������

def change(a):    print(id(a))   # ���������������������������    a=10    print(id(a))   # ��������������� a=1print(id(a))change(a)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=57289import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')140737191977360140737191977360140737191977648
3������������������������

������������������������������������������������������������������������������������������������������������������

# ������������������def changeme( mylist ):   "���������������������"   mylist.append([1,2,3,4])   print ("���������������: ", mylist)   return # ������changeme������mylist = [10,20,30]changeme( mylist )print ("���������������: ", mylist)

������������������������������������������������������������������������������������������������������

���������������:  [10, 20, 30, [1, 2, 3, 4]]���������������:  [10, 20, 30, [1, 2, 3, 4]]

3���������

���������������������������������������������������������

  • ������������
  • ���������������
  • ������������
  • ���������������
1���������������

������������������������������������������������������������������������������������������������

2������������������

������������������������������������������������������������������������������������������������������������

��������������������������������������������������������������������������������������� Python ���������������������������������������������

# ������������������def printinfo(name, age):    "������������������������������"    print("������: ", name)    print("������: ", age)    return# ������printinfo������printinfo(age=50, name="runoob")

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=49258import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')������:  runoob������:  50
3���������������

��������������������������������������������������������������������������������������������������������� age ������������������������������

# ������������������def printinfo(name, age=35):    "������������������������������"    print("������: ", name)    print("������: ", age)    return# ������printinfo������printinfo(age=50, name="runoob")print("------------------------")printinfo(name="runoob")

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=49324import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')������:  runoob������:  50------------------------������:  runoob������:  35
4������������������������*������������������

��������������������������������������������������������������������������������������������������������������������� 2 ������������������������������������������

������������������������ ** ������������������������������������������������������ * ���������������������(tuple)��������������������������������������������������������������������������������� * ������������������������������������

���������������������

def functionname([formal_args,] *var_args_tuple ):   "������_���������������"   function_suite   return [expression]

������������ * ���������������������(tuple)���������������������������������������������������������

def printinfo(a,*b):    print(a)    print(b)printinfo("abd",1,2,3)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=49396import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')abd(1, 2, 3)

������������������ ** ������������������������������������

def printinfo( arg1, **vardict ):   "���������������������������"   print ("������: ")   print (arg1)   print (vardict) # ������printinfo ������printinfo(1, a=2,b=3)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=49454import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')������: 1{'a': 2, 'b': 3}

������������������������ * ������������������������������������

def f(a,b,*,c):    return a+b+cprint(f(1,2,c=12))

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=49568import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')15

5���������������

python ������ lambda ������������������������

��������������������������������� def ������������������������������������������������

  • lambda ������������������������������������ def ���������������
  • lambda���������������������������������������������������������������������lambda������������������������������������������
  • lambda ���������������������������������������������������������������������������������������������������������������
  • ������lambda������������������������������������������������C���C++���������������������������������������������������������������������������������������������������
1���������

lambda ������������������������������������������������

lambda [arg1 [,arg2,.....argn]]:expression
# ������������������sum = lambda arg1, arg2: arg1 + arg2 # ������sum������print ("������������������ : ", sum( 1, 2))print ("������������������ : ", sum( 2, 5 ))

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=49647import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')������������������ :  3������������������ :  7

6���return������

return [���������] ���������������������������������������������������������������������������������������������return������������None��������������������������������������������������������������������������� return ������������������

# ������������������def sum( arg1, arg2 ):   # ������2���������������."   total = arg1 + arg2   print ("��������� : ", total)   return total # ������sum������total = sum( 10, 20 )print ("��������� : ", total)

���������

��������� :  30��������� :  30

7���������������������

Python3.8 ��������������������������������� / ������������������������������������������������������������������������������������������������

������������������������������ a ��� b ���������������������������������c ��� d ��������������������������������������������� e ��� f ������������������������:

def f(a, b, /, c, d, *, e, f):    print(a, b, c, d, e, f)
f(10, 20, 30, d=40, e=50, f=60)

2���Python3 ������������

1���������

Python������������������������������������������������������������������������������������������������������������������������������������������������������������

��������� Python ���������������������

������ ������
list.append(x) ��������������������������������������������������� a[len(a):] = [x]���
list.extend(L) ������������������������������������������������������������������ a[len(a):] = L���
list.insert(i, x) ��������������������������������������������������������������������������������������������������������������� a.insert(0, x) ������������������������������������ a.insert(len(a), x) ��������� a.append(x) ���
list.remove(x) ��������������������� x ������������������������������������������������������������������������������
list.pop([i]) ������������������������������������������������������������������������������������a.pop()��������������������������������������������������������������������������� i ������������������������������������������������������������������������������������������������������������ Python ���������������������������������������������
list.clear() ������������������������������������del a[:]���
list.index(x) ������������������������������ x ���������������������������������������������������������������������������
list.count(x) ������ x ������������������������������
list.sort() ������������������������������������
list.reverse() ���������������������������
list.copy() ���������������������������������a[:]���

��������������� insert, remove ��� sort ������������������������������������������

1������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ append() ��������������������������������������������������������������������� pop() ���������������������������������������������������������������

a = [66.25, 333, 333, 1, 1234.5]a.append(1)a.append(2)print(a)a.pop()print(a)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=50250import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')[66.25, 333, 333, 1, 1234.5, 1, 2][66.25, 333, 333, 1, 1234.5, 1]
2������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

from collections import dequea = deque([66.25, 333, 333, 1, 1234.5])# ���������a.append(100)a.append(101)print(a)# ���������a.popleft()a.popleft()print(a)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=50380import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')deque([66.25, 333, 333, 1, 1234.5, 100, 101])deque([333, 1, 1234.5, 100, 101])
3������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

��������������������������� for ������������������������������������������������ for ��� if ������������������������������������������������������ for ��� if ���������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������

���������python���������������������������������java���������+���

a = [2, 4, 6]a1= [4, 3, -9]b=[3*x for x in a]print("b���������",b)c=[3*x for x in a if x >3]print("c���������",c)d=[x*y for x in a for y in a1]print("d���������",d)e=[x+y for x in a for y in a1]print("e���������",e)m=[a[i]*a1[i] for i in range(len(a))]print("m���������",m)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=50988import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')b��������� [6, 12, 18]c��������� [12, 18]d��������� [8, 6, -18, 16, 12, -36, 24, 18, -54]e��������� [6, 5, -7, 8, 7, -5, 10, 9, -3]m��������� [8, 12, -54]
4���������������������

������������

# ���������������������3X4���������������matrix = [[1, 2, 3, 4], [5, 6, 7, 8],[9, 10, 11, 12],]print(matrix)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=51096import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

2������������������

���������������������������������������������������

t = 12345, 54321, 'hello!'u = t, (1, 2, 3, 4, 5)print(u)

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=51224import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))

��������������������������������������������������������������������������������������������������������������������������������� ������������������������������������������������������������������������������������


3���������

���������������������������������������������������������������������������������������������������

������������������({})��������������������������������������������������������������������� set() ��������� {} ���������������������������������������������������������������

4���������

������������������������ Python ������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������=>���������������������������������������������������������������������������

������������������������������������������{}���

������������������������������������������������������������ items() ���������������������������

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}>>> for k, v in knights.items():...     print(k, v)...gallahad the purerobin the brave

��������������������������������������������������� zip() ���������

>>> questions = ['name', 'quest', 'favorite color']>>> answers = ['lancelot', 'the holy grail', 'blue']>>> for q, a in zip(questions, answers):...     print('What is your {0}?  It is {1}.'.format(q, a))...What is your name?  It is lancelot.What is your quest?  It is the holy grail.What is your favorite color?  It is blue.

��������������������������������������������������������������������� reversed() ���������

>>> for i in reversed(range(1, 10, 2)):...     print(i)...97531

��������������������������������������� sorted() ������������������������������������������������������������

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']>>> for f in sorted(set(basket)):...     print(f)...applebananaorangepear

3���Python3 ������

���������������������������������������������������������������������������������.py������������������������������������������������������������������������������������������������ python ������������������

1���import ������

import module1[, module2[,... moduleN]

2���from ��� import ������

Python ��� from ���������������������������������������������������������������������������������������������

from modname import name1[, name2[, ... nameN]]

3���from ��� import * ������

���������������������������������������������������������������������������������������������������������������

from modname import *

4���__name__������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������__name__���������������������������������������������������������������

#!/usr/bin/python3# Filename: using_name.pyif __name__ == '__main__':   print('���������������������')else:   print('���������������������')

��������� ������������������������__name__���������������������'__main__'������������������������������������������������������������

���������__name__ ��� __main__ ������������������������ _ _ ���������������������������������������

5���dir() ������

��������������� dir() ������������������������������������������������������������������������������������:

import osprint(dir(os))

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=52830import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/test.py', wdir='C:/app/PycharmProjects/tensorflow')['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']

 4���Python3 ���������������

1���print������

���������������������������������

for x in range(1, 11):    print(repr(x).rjust(2), repr(x * x).rjust(3), end=' ')    print(repr(x * x * x).rjust(4))

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=54864import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/Test/__init__.py', wdir='C:/app/PycharmProjects/tensorflow/Test') 1   1    1 2   4    8 3   9   27 4  16   64 5  25  125 6  36  216 7  49  343 8  64  512 9  81  72910 100 1000

���������

��������������������������������������� rjust() ������, ���������������������������, ���������������������������

���������������������, ��� ljust() ��� center()��� ������������������������������������, ������������������������������������

��������������� zfill(), ������������������������������ 0

2���format

��������������������������� (���������������������) ��������� format() ���������������������

������������������������������������������������ format() ������������������������������

import mathstr="Google"str1="Runoob"print("{0}���{1}".format(str,str1))print("������ PI ��������������� {0:.3f}".format(math.pi)) print('������ PI ������������������%5.3f���' % math.pi)
table = {'Google': 1, 'Runoob': 2, 'Taobao': 3} for name,num in table.items(): print("{0:10}==>{1:10d}".format(name,num))

���������

C:\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm 2019.1.1\helpers\pydev\pydevconsole.py" --mode=client --port=55681import sys; print('Python %s on %s' % (sys.version, sys.platform))sys.path.extend(['C:\\app\\PycharmProjects', 'C:/app/PycharmProjects'])Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]Type 'copyright', 'credits' or 'license' for more informationIPython 7.12.0 -- An enhanced Interactive Python. Type '?' for help.PyDev console: using IPython 7.12.0Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] on win32runfile('C:/app/PycharmProjects/tensorflow/Test/__init__.py', wdir='C:/app/PycharmProjects/tensorflow/Test')Google���Runoob������ PI ��������������� 3.142Google    ==>         1Runoob    ==>         2Taobao    ==>         3

3���������������

Python��������� input() ���������������������������������������������������������������������������������

input ������������������Python���������������������������������������������������

str = input("������������");print ("���������������������: ", str)

������

������������python���������������������:  python

4������������������������

open() ������������������ file ���������������������������������:

open(filename, mode)
  • filename���������������������������������������������������������
  • mode���������������������������������������������������������������������������������������������������������������������������������������������������������������������������(r)���

������������������������������������������

������ ������
r ������������������������������������������������������������������������������������������������
rb ���������������������������������������������������������������������������������������������
r+ ���������������������������������������������������������������������������
rb+ ���������������������������������������������������������������������������������������������
w ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
wb ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
w+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
wb+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
a ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
ab ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
a+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
ab+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������

������ r r+ w w+ a a+
��� + +   +   +
���   + + + + +
������     + + + +
������     + +    
��������������� + + + +    
���������������         + +

���������������

f.write()

f.write(string) ��� string ������������������, ���������������������������������

#!/usr/bin/python3 # ������������������ f = open("/tmp/foo.txt", "w") num = f.write( "Python ������������������������������\n������������������������!!\n" ) print(num) # ��������������������� f.close()

f.tell()

f.tell() ���������������������������������������, ������������������������������������������������

f.seek()

������������������������������������, ������������ f.seek(offset, from_what) ���������

from_what ������, ��������� 0 ������������, ��������� 1 ������������������, 2 ���������������������������������

 

  • seek(x,0) ��� ��������������������������������������������������� x ���������
  • seek(x,1) ��� ���������������������������������x���������
  • seek(-x,2)���������������������������������������x���������

from_what ���������������0���������������������������������������������������������

>>> f = open('/tmp/foo.txt', 'rb+')>>> f.write(b'0123456789abcdef')16>>> f.seek(5)     # ���������������������������������5>>> f.read(1)b'5'>>> f.seek(-3, 2) # ������������������������������������13>>> f.read(1)b'd'

f.close()

������������������ (������������������������������������ b ���), ������������������������������������������������

������������������������������, ������ f.close() ������������������������������������������������������������������������������������������������

���������������

f.read()

������������������������������������������ f.read(size), ���������������������������������, ���������������������������������������������

size ������������������������������������������ ��� size ������������������������, ���������������������������������������������������������

#!/usr/bin/python3# ������������������f = open("/tmp/foo.txt", "r")str = f.read()print(str)# ���������������������f.close()

���������

Python ������������������������������������������������������!!

f.readline()

f.readline() ��������������������������������������������������� '\n'���f.readline() ������������������������������, ������������������������������������������

#!/usr/bin/python3# ������������������f = open("/tmp/foo.txt", "r")str = f.readline()print(str)# ���������������������f.close()

f.readlines()

f.readlines() ������������������������������������������

������������������������ sizehint, ������������������������������, ������������������������������������

#!/usr/bin/python3# ������������������f = open("/tmp/foo.txt", "r")str = f.readlines()print(str)# ���������������������f.close()

���������

['Python ������������������������������\n', '������������������������!!\n']

������������������������������������������������������������:

# ������������������f = open("/tmp/foo.txt", "r")for line in f:    print(line, end='')# ���������������������f.close()

���������

Python ������������������������������������������������������!!

pickle ������

python���pickle������������������������������������������������������

������pickle������������������������������������������������������������������������������������������������������������

������pickle���������������������������������������������������������������������������������������������

���������������

#!/usr/bin/python3import pickle# ������pickle������������������������������������data1 = {'a': [1, 2.0, 3, 4+6j],         'b': ('string', u'Unicode string'),         'c': None}selfref_list = [1, 2, 3]selfref_list.append(selfref_list)output = open('data.pkl', 'wb')# Pickle dictionary using protocol 0.pickle.dump(data1, output)# Pickle the list using the highest protocol available.pickle.dump(selfref_list, output, -1)output.close()
#!/usr/bin/python3import pprint, pickle#������pickle������������������������python������pkl_file = open('data.pkl', 'rb')data1 = pickle.load(pkl_file)pprint.pprint(data1)data2 = pickle.load(pkl_file)pprint.pprint(data2)pkl_file.close()

5���Python3 File(������) ������

open() ������

Python open() ������������������������������������������������������������������������������������������������������������������������������������������������������������������ OSError���

��������������� open() ��������������������������������������������������� close() ���������

open() ���������������������������������������������������(file)���������(mode)���

open(file, mode='r')

���������������������������

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

������������:

  • file: ������������������������������������������������������
  • mode: ���������������������������
  • buffering: ������������
  • encoding: ������������utf8
  • errors: ������������
  • newline: ���������������
  • closefd: ���������file������������
  • opener: ������������������������������������������������������������������������������������������

mode ������������

������ ������
t ������������ (������)���
x ������������������������������������������������������������������������
b ������������������
+ ������������������������������(������������)���
U ���������������������Python 3 ���������������
r ������������������������������������������������������������������������������������������������
rb ������������������������������������������������������������������������������������������������������������������������������������������������������������
r+ ���������������������������������������������������������������������������
rb+ ���������������������������������������������������������������������������������������������������������������������������������������
w ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
wb ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
w+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
wb+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
a ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
ab ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
a+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
ab+ ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������ b ���

file ������

file ������������ open ��������������������������������� file ������������������������

������ ���������������
1

������������������������������������������������������������

2

������������������������������������������������������������������������������, ������������������������������������������������

3

������������������������������������(file descriptor FD ������), ���������������os���������read���������������������������������

4

��������������������������������������������� True��������������� False���

5

Python 3 ������ File ��������������� next() ���������

������������������������

6

������������������������������������������������������������������������������

7

��������������������� "\n" ���������

8

������������������������������������������sizeint>0������������������������sizeint������������, ������������������������ sizeint ������, ������������������������������

9

���������������������������������������

10

���������������������������

11

��������������������������������������������������������� size ��������������� size ��������������������������������������������������������������������������������� windows ������������������������2������������������

12

���������������������������������������������������������������

13

������������������������������������������������������������������������������������������������������

上一篇:项目实战 从 0 到 1 学习之Flink (27)FlinkSql教程(一)
下一篇:python之(19)基础总结(2)

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月06日 21时51分54秒