python 关键知识点

学习资源:笨方法学习 python3

将变量传递给脚本——argv

  • 脚本:你编写的 .py 文件。
  • argv 参数变量(argument variable)保存着你运行 python 脚本的参数。

input() 脚本运行过程需要输入参数

from sys import argv

script, user_name = argv  # 解包
prompt = '> '    # 提示符

print(f"Hi {user_name}, I'm the {script} script.")
print("I'd like to ask you a few question.")
print(f"Do you like me {user_name}?")
likes = input(prompt)

print(f"Where do you live {user_name}?")
lives = input(prompt)

print("What kind of computer do you have?")
computer = input(prompt)

print(f'''
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice.
''')
x = input('sd ffg')
sd ffgdf
type(x)
str
x
'df'
echo 'This is a text file.' > a.txt   # 创建文件 
cat a.txt    # 查看文件
man cat   # 查看 cat 的帮助文档

函数中的 *args

告诉 python 把函数的所有参数都接收进来,然后放到名叫 args 的列表中去。

def print_two(*args):
    arg1, arg2 = args
    print(f'arg1: {arg1}, arg2: {arg2}')
print_two('Zed', 'Shaw')
arg1: Zed, arg2: Shaw

字符串 & 字节串 & 字符编码

import sys

script, encoding, error = sys.argv

def main(language_file, encoding, errors):
    line = language_file.readline()
    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)

def print_line(line, encoding, errors):
    next_lang = line.strip()
    raw_bytes = next_lang.encode(encoding, errors = errors)
    cooked_string = raw_bytes.decode(encoding, errors = errors)
    
    print(raw_bytes, "<===>", cooked_string)

languages = open('E:/Data/languages.txt', encoding = 'utf-8')

main(languages, encoding, error)

PowerShell:

python .\str_bytes_test.py utf-8 strict
def main(language_file, encoding, errors):
    line = language_file.readline()
    if line:
        print_line(line, encoding, errors)
        return main(language_file, encoding, errors)

def print_line(line, encoding, errors):
    next_lang = line.strip()     # 删除每行结尾的 "\n"
    raw_bytes = next_lang.encode(encoding, errors = errors)
    cooked_string = raw_bytes.decode(encoding, errors = errors)

    print(raw_bytes, "<===>", cooked_string)
with open('E:/Data/languages.txt', encoding = 'utf-8') as fp:
    main(fp, 'utf-8', 'strict')
b'Afrikaans' <===> Afrikaans
b'\xe1\x8a\xa0\xe1\x88\x9b\xe1\x88\xad\xe1\x8a\x9b' <===> አማርኛ
b'\xd0\x90\xd2\xa7\xd1\x81\xd1\x88\xd3\x99\xd0\xb0' <===> Аҧсшәа
b'\xd8\xa7\xd9\x84\xd8\xb9\xd8\xb1\xd8\xa8\xd9\x8a\xd8\xa9' <===> العربية
b'Aragon\xc3\xa9s' <===> Aragonés
b'Arpetan' <===> Arpetan
b'Az\xc9\x99rbaycanca' <===> Azərbaycanca
b'Bamanankan' <===> Bamanankan
b'\xe0\xa6\xac\xe0\xa6\xbe\xe0\xa6\x82\xe0\xa6\xb2\xe0\xa6\xbe' <===> বাংলা
b'B\xc3\xa2n-l\xc3\xa2m-g\xc3\xba' <===> Bân-lâm-gú
b'\xd0\x91\xd0\xb5\xd0\xbb\xd0\xb0\xd1\x80\xd1\x83\xd1\x81\xd0\xba\xd0\xb0\xd1\x8f' <===> Беларуская
b'\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd1\x81\xd0\xba\xd0\xb8' <===> Български
b'Boarisch' <===> Boarisch
b'Bosanski' <===> Bosanski
b'\xd0\x91\xd1\x83\xd1\x80\xd1\x8f\xd0\xb0\xd0\xb4' <===> Буряад
b'Catal\xc3\xa0' <===> Català
b'\xd0\xa7\xd3\x91\xd0\xb2\xd0\xb0\xd1\x88\xd0\xbb\xd0\xb0' <===> Чӑвашла
b'\xc4\x8ce\xc5\xa1tina' <===> Čeština
b'Cymraeg' <===> Cymraeg
b'Dansk' <===> Dansk
b'Deutsch' <===> Deutsch
b'Eesti' <===> Eesti
b'\xce\x95\xce\xbb\xce\xbb\xce\xb7\xce\xbd\xce\xb9\xce\xba\xce\xac' <===> Ελληνικά
b'Espa\xc3\xb1ol' <===> Español
b'Esperanto' <===> Esperanto
b'\xd9\x81\xd8\xa7\xd8\xb1\xd8\xb3\xdb\x8c' <===> فارسی
b'Fran\xc3\xa7ais' <===> Français
b'Frysk' <===> Frysk
b'Gaelg' <===> Gaelg
b'G\xc3\xa0idhlig' <===> Gàidhlig
b'Galego' <===> Galego
b'\xed\x95\x9c\xea\xb5\xad\xec\x96\xb4' <===> 한국어
b'\xd5\x80\xd5\xa1\xd5\xb5\xd5\xa5\xd6\x80\xd5\xa5\xd5\xb6' <===> Հայերեն
b'\xe0\xa4\xb9\xe0\xa4\xbf\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xa6\xe0\xa5\x80' <===> हिन्दी
b'Hrvatski' <===> Hrvatski
b'Ido' <===> Ido
b'Interlingua' <===> Interlingua
b'Italiano' <===> Italiano
b'\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa' <===> עברית
b'\xe0\xb2\x95\xe0\xb2\xa8\xe0\xb3\x8d\xe0\xb2\xa8\xe0\xb2\xa1' <===> ಕನ್ನಡ
b'Kapampangan' <===> Kapampangan
b'\xe1\x83\xa5\xe1\x83\x90\xe1\x83\xa0\xe1\x83\x97\xe1\x83\xa3\xe1\x83\x9a\xe1\x83\x98' <===> ქართული
b'\xd2\x9a\xd0\xb0\xd0\xb7\xd0\xb0\xd2\x9b\xd1\x88\xd0\xb0' <===> Қазақша
b'Krey\xc3\xb2l ayisyen' <===> Kreyòl ayisyen
b'Latga\xc4\xbcu' <===> Latgaļu
b'Latina' <===> Latina
b'Latvie\xc5\xa1u' <===> Latviešu
b'L\xc3\xabtzebuergesch' <===> Lëtzebuergesch
b'Lietuvi\xc5\xb3' <===> Lietuvių
b'Magyar' <===> Magyar
b'\xd0\x9c\xd0\xb0\xd0\xba\xd0\xb5\xd0\xb4\xd0\xbe\xd0\xbd\xd1\x81\xd0\xba\xd0\xb8' <===> Македонски
b'Malti' <===> Malti
b'\xe0\xa4\xae\xe0\xa4\xb0\xe0\xa4\xbe\xe0\xa4\xa0\xe0\xa5\x80' <===> मराठी
b'\xe1\x83\x9b\xe1\x83\x90\xe1\x83\xa0\xe1\x83\x92\xe1\x83\x90\xe1\x83\x9a\xe1\x83\xa3\xe1\x83\xa0\xe1\x83\x98' <===> მარგალური
b'\xd9\x85\xd8\xa7\xd8\xb2\xd9\x90\xd8\xb1\xd9\x88\xd9\x86\xdb\x8c' <===> مازِرونی
b'Bahasa Melayu' <===> Bahasa Melayu
b'\xd0\x9c\xd0\xbe\xd0\xbd\xd0\xb3\xd0\xbe\xd0\xbb' <===> Монгол
b'Nederlands' <===> Nederlands
b'\xe0\xa4\xa8\xe0\xa5\x87\xe0\xa4\xaa\xe0\xa4\xbe\xe0\xa4\xb2 \xe0\xa4\xad\xe0\xa4\xbe\xe0\xa4\xb7\xe0\xa4\xbe' <===> नेपाल भाषा
b'\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e' <===> 日本语
b'Norsk bokm\xc3\xa5l' <===> Norsk bokmål
b'Nouormand' <===> Nouormand
b'Occitan' <===> Occitan
b'O\xca\xbbzbekcha/\xd1\x9e\xd0\xb7\xd0\xb1\xd0\xb5\xd0\xba\xd1\x87\xd0\xb0' <===> Oʻzbekcha/ўзбекча
b'\xe0\xa8\xaa\xe0\xa9\xb0\xe0\xa8\x9c\xe0\xa8\xbe\xe0\xa8\xac\xe0\xa9\x80' <===> ਪੰਜਾਬੀ
b'\xd9\xbe\xd9\x86\xd8\xac\xd8\xa7\xd8\xa8\xdb\x8c' <===> پنجابی
b'\xd9\xbe\xda\x9a\xd8\xaa\xd9\x88' <===> پښتو
b'Plattd\xc3\xbc\xc3\xbctsch' <===> Plattdüütsch
b'Polski' <===> Polski
b'Portugu\xc3\xaas' <===> Português
b'Rom\xc3\xa2n\xc4\x83' <===> Română
b'Romani' <===> Romani
b'\xd0\xa0\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb9' <===> Русский
b'Seeltersk' <===> Seeltersk
b'Shqip' <===> Shqip
b'Simple English' <===> Simple English
b'Sloven\xc4\x8dina' <===> Slovenčina
b'\xda\xa9\xd9\x88\xd8\xb1\xd8\xaf\xdb\x8c\xdb\x8c \xd9\x86\xd8\xa7\xd9\x88\xdb\x95\xd9\x86\xd8\xaf\xdb\x8c' <===> کوردیی ناوەندی
b'\xd0\xa1\xd1\x80\xd0\xbf\xd1\x81\xd0\xba\xd0\xb8 / srpski' <===> Српски / srpski
b'Suomi' <===> Suomi
b'Svenska' <===> Svenska
b'Tagalog' <===> Tagalog
b'\xe0\xae\xa4\xe0\xae\xae\xe0\xae\xbf\xe0\xae\xb4\xe0\xaf\x8d' <===> தமிழ்
b'\xe0\xb8\xa0\xe0\xb8\xb2\xe0\xb8\xa9\xe0\xb8\xb2\xe0\xb9\x84\xe0\xb8\x97\xe0\xb8\xa2' <===> ภาษาไทย
b'Taqbaylit' <===> Taqbaylit
b'\xd0\xa2\xd0\xb0\xd1\x82\xd0\xb0\xd1\x80\xd1\x87\xd0\xb0/tatar\xc3\xa7a' <===> Татарча/tatarça
b'\xe0\xb0\xa4\xe0\xb1\x86\xe0\xb0\xb2\xe0\xb1\x81\xe0\xb0\x97\xe0\xb1\x81' <===> తెలుగు
b'\xd0\xa2\xd0\xbe\xd2\xb7\xd0\xb8\xd0\xba\xd3\xa3' <===> Тоҷикӣ
b'T\xc3\xbcrk\xc3\xa7e' <===> Türkçe
b'\xd0\xa3\xd0\xba\xd1\x80\xd0\xb0\xd1\x97\xd0\xbd\xd1\x81\xd1\x8c\xd0\xba\xd0\xb0' <===> Українська
b'\xd8\xa7\xd8\xb1\xd8\xaf\xd9\x88' <===> اردو
b'Ti\xe1\xba\xbfng Vi\xe1\xbb\x87t' <===> Tiếng Việt
b'V\xc3\xb5ro' <===> Võro
b'\xe6\x96\x87\xe8\xa8\x80' <===> 文言
b'\xe5\x90\xb4\xe8\xaf\xad' <===> 吴语
b'\xd7\x99\xd7\x99\xd6\xb4\xd7\x93\xd7\x99\xd7\xa9' <===> ייִדיש
b'\xe4\xb8\xad\xe6\x96\x87' <===> 中文

现代的计算机本质上是一个巨大的开关阵列。计算机用电来触发这些开关的开启或关闭。这些开关用 1 表示开启、有电、接通,用 0 表示关闭、没电、切断。我们称这些 10 为『位』(bit)。

我们用「字节」(byte)表示一个 \(8\) 位(01)的序列。

美国信息交换标准代码(ASCII)把数字与字母相对应。

0b1011010   # 二进制的 90
90
ord('Z')  # 字母 Z 的对应数字
90
chr(90) 
'Z'

有了 ASCII 这个约定,我们就可以用 \(8\) 位(也就是 \(1\) 字节)编码一个字符,然后就可以把字符串联在一起,合成一个单词。例如:'Zed A. Shaw' 我们可以用一组字节序列来表示:[90, 101, 100, 32, 65, 46, 32, 83, 104, 97, 119],代码如下:

[ord(name) for name in 'Zed A. Shaw']
[90, 101, 100, 32, 65, 46, 32, 83, 104, 97, 119]

再说一遍,类似上面的约定的序列最终还是会转换为开关的通断。

一个字节可以存放 \(256\)(即 \(2 ^ 8\))个数字:
\[ 0 \sim 255 \; \text{或} \; 00000000 \sim 11111111 \]

为了统一编码方式,Unicode(universal encoding)便出现了。

UTF-8(Unicode Transformation Format 8 Bits):把大部分常用的字符用 \(8\) 位编码,如果需要编码更多的字符,就「逃」去使用更大的数;这种编码方式很大的程度节省了存储空间。

在 python 中,string 是 UTF-8 编码的字符序列,是显示和处理文本的基础。bytes 则是 python 用来存储 UTF-8 字符串的原始字节序列,使用b"" 表示字节串

因而,如果要处理的是原始字节串(bytes),那么你就需要通过 .decode() 来获取字符串(string)。原始字节串不包含编码方式,它们就是字节序列,一堆数字而已,所以你必须告诉 python 把它解码成 UTF 字符串。即

解码字节串,编码字符串
(decode bytes, encode strings)

短路逻辑

  • 任何以 False 开头的 and 语句都会直接处理成 False,不会继续检查后面的语句。
  • 任何包含 Trueor 语句,只要处理到 True,就不会继续向下推算,而是直接返回 True 了。
  • Python 只会运行它遇到的是 True 的第一个块,所以如果有多个 elif 块,只有第一个为 True 的块才会运行。
from sys import exit
def glod_room():
    print("This room is full of gold. How much do you take?")
    
    choice = input('> ')
    if '0' in choice or '1' in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number.")
        
    if how_much < 50:
        print("Nice, you're not greedy, you win!")
        exit(0)
    else:
        dead("You greedy bastard!")
        

def dead(why):
    print(why, "Good Job!")
    exit(0)
glod_room()
This room is full of gold. How much do you take?
> 7
Man, learn to type a number. Good Job!



An exception has occurred, use %tb to see the full traceback.


SystemExit: 0



C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2971: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

exit(0) 可以中止某个程序,而其中的数字参数用来表示程序是否是遇到错误而中止的。exit(0) 表示程序正常退出。

if 语句的规则

  1. 每一条 if 语句必须包含一个 else。(为了防止出现未考虑到的条件出现)
  2. 若这个 else 永远都不应该被执行到,因为它本身没有任何意义,则必须在 else 语句后面使用一个叫 die 的函数,让它打印出出错消息并且「死」给你看。
  3. if 语句的嵌套不要超过两层,最好尽量保持只有一层。
  4. if 语句当作段落来对待,其中的每一个 if-elif-else 组合就跟一个段落的句子组合一样。在这种组合的最前面和最后面留一个空行以作区分。
  5. 你的布尔测试应该很简单,如果它们很复杂,你需要在函数里面将它们的运算顺序事先放到一个变量里,并且为变量取一个名字。

调试小技巧

  • 最好的调试方法是使用 print 在各个要检查的关键点将变量打印出来,从而检查那里是否有错。
  • 让程序一部分一部分地运行起来。不要等到写了一大堆代码文件后才去运行它们,写一点,运行一点,再修改一点。

.join() & .split()

ten_things = 'Apples Oranges Crows Telephone Light Sugar'
stuff = ten_things.split(' ')
stuff
['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar']
' & '.join(stuff)
'Apples & Oranges & Crows & Telephone & Light & Sugar'

面向对象(OOP)

练习:

import random
from urllib.request import urlopen
import sys

WORD_URL = 'http://learncodethehardway.org/words.txt'
WORDS = []

PHRASES = {
    'class %%%(%%%):':
    "Make a class named %%% that is-a %%%.",
    'class %%%(object):\n\tdef __init__(self, ***)':
    'class %%% has-a __init__ that takes self and *** params.',
    'class %%%(object):\n\tdef ***(self, @@@)':
    'class %%% has-a function *** that takes self ang @@@ params.',
    '*** = %%%()':
    'Set *** to an instance of class %%%',
    "***.***(@@@)":
    'From *** get *** function, call it with params self, @@@.',
    "***.*** = '***'":
    "From *** get the attribute and set it to '***'."
    
}

# do they want to drill phrases(短语) first
if len(sys.argv) == 2 and sys.argv[1] == 'english':
    PHRASES_FIRST = True
else:
    PHRASES_FIRST = False
    
# load up the words from website
for word in urlopen(WORD_URL).readlines():
    WORDS.append(str(word.strip(), encoding = 'utf-8'))
    

def convert(snippet, phrase):
    class_names = [w.capitalize() for w in random.sample(WORDS, snippet.count("%%%"))]
    other_names = random.sample(WORDS, snippet.count("***"))
    results = []
    param_names = []
    
    for i in range(0, snippet.count('@@@')):
        param_count = random.randint(1, 3)
        param_names.append(', '.join(random.sample(WORDS, param_count)))
        
    for sentence in snippet, phrase:
        result = sentence[:]
        
        # fake class names
        for word in class_names:
            result = result.replace("%%%", word, 1)
            
        # fake other names
        for word in other_names:
            result = result.replace("***", word, 1)
            
        # fake parameter lists
        for word in param_names:
            result = result.replace("@@@", word, 1)
            
        results.append(result)
        
    return results


# keep going until they hit CTRAL-D
try:
    while True:
        snippets = list(PHRASES.keys())
        random.shuffle(snippets)
        
        for snippet in snippets:
            phrase = PHRASES[snippet]
            question, answer = convert(snippet, phrase)
            if PHRASES_FIRST:
                question, answer = question, answer
            
            print(question)
            
            input("> ")
            print(f"ANSWER: {answer}\n\n")
except EOFError:
    print('\nBye')

关于 class Name(object)

在 python 3 中,你不需要在类名后面添加 object,但是 python 圈子的人相信「显式优于隐式」,所以,一般还是需要写上。

关于 super()

在 python 3 中 super() 等价于 super(Child, self)

class Parent(object):
    
    def altered(self):
        print("PARENT altered()")
        
class Child(Parent):
    
    def altered(self):
        print("CHILD, BEFORE PARENT altered()")
        super(Child, self).altered()
        print("CHILD, AFTER PARENT altered()")
        
dad = Parent()
son = Child()

dad.altered()
print('=='* 20)
son.altered()
PARENT altered()
========================================
CHILD, BEFORE PARENT altered()
PARENT altered()
CHILD, AFTER PARENT altered()
class Parent(object):
    
    def altered(self):
        print("PARENT altered()")
        
class Child(Parent):
    
    def altered(self):
        print("CHILD, BEFORE PARENT altered()")
        super().altered()
        print("CHILD, AFTER PARENT altered()")
        
dad = Parent()
son = Child()

dad.altered()
print('=='* 20)
son.altered()
PARENT altered()
========================================
CHILD, BEFORE PARENT altered()
PARENT altered()
CHILD, AFTER PARENT altered()
    原文作者:xinet
    原文地址: https://www.cnblogs.com/q735613050/p/9215050.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞