学习资源:笨方法学习 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() # 删除每行结尾的 "
"
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'xe1x8axa0xe1x88x9bxe1x88xadxe1x8ax9b' <===> አማርኛ
b'xd0x90xd2xa7xd1x81xd1x88xd3x99xd0xb0' <===> Аҧсшәа
b'xd8xa7xd9x84xd8xb9xd8xb1xd8xa8xd9x8axd8xa9' <===> العربية
b'Aragonxc3xa9s' <===> Aragonés
b'Arpetan' <===> Arpetan
b'Azxc9x99rbaycanca' <===> Azərbaycanca
b'Bamanankan' <===> Bamanankan
b'xe0xa6xacxe0xa6xbexe0xa6x82xe0xa6xb2xe0xa6xbe' <===> বাংলা
b'Bxc3xa2n-lxc3xa2m-gxc3xba' <===> Bân-lâm-gú
b'xd0x91xd0xb5xd0xbbxd0xb0xd1x80xd1x83xd1x81xd0xbaxd0xb0xd1x8f' <===> Беларуская
b'xd0x91xd1x8axd0xbbxd0xb3xd0xb0xd1x80xd1x81xd0xbaxd0xb8' <===> Български
b'Boarisch' <===> Boarisch
b'Bosanski' <===> Bosanski
b'xd0x91xd1x83xd1x80xd1x8fxd0xb0xd0xb4' <===> Буряад
b'Catalxc3xa0' <===> Català
b'xd0xa7xd3x91xd0xb2xd0xb0xd1x88xd0xbbxd0xb0' <===> Чӑвашла
b'xc4x8cexc5xa1tina' <===> Čeština
b'Cymraeg' <===> Cymraeg
b'Dansk' <===> Dansk
b'Deutsch' <===> Deutsch
b'Eesti' <===> Eesti
b'xcex95xcexbbxcexbbxcexb7xcexbdxcexb9xcexbaxcexac' <===> Ελληνικά
b'Espaxc3xb1ol' <===> Español
b'Esperanto' <===> Esperanto
b'xd9x81xd8xa7xd8xb1xd8xb3xdbx8c' <===> فارسی
b'Franxc3xa7ais' <===> Français
b'Frysk' <===> Frysk
b'Gaelg' <===> Gaelg
b'Gxc3xa0idhlig' <===> Gàidhlig
b'Galego' <===> Galego
b'xedx95x9cxeaxb5xadxecx96xb4' <===> 한국어
b'xd5x80xd5xa1xd5xb5xd5xa5xd6x80xd5xa5xd5xb6' <===> Հայերեն
b'xe0xa4xb9xe0xa4xbfxe0xa4xa8xe0xa5x8dxe0xa4xa6xe0xa5x80' <===> हिन्दी
b'Hrvatski' <===> Hrvatski
b'Ido' <===> Ido
b'Interlingua' <===> Interlingua
b'Italiano' <===> Italiano
b'xd7xa2xd7x91xd7xa8xd7x99xd7xaa' <===> עברית
b'xe0xb2x95xe0xb2xa8xe0xb3x8dxe0xb2xa8xe0xb2xa1' <===> ಕನ್ನಡ
b'Kapampangan' <===> Kapampangan
b'xe1x83xa5xe1x83x90xe1x83xa0xe1x83x97xe1x83xa3xe1x83x9axe1x83x98' <===> ქართული
b'xd2x9axd0xb0xd0xb7xd0xb0xd2x9bxd1x88xd0xb0' <===> Қазақша
b'Kreyxc3xb2l ayisyen' <===> Kreyòl ayisyen
b'Latgaxc4xbcu' <===> Latgaļu
b'Latina' <===> Latina
b'Latviexc5xa1u' <===> Latviešu
b'Lxc3xabtzebuergesch' <===> Lëtzebuergesch
b'Lietuvixc5xb3' <===> Lietuvių
b'Magyar' <===> Magyar
b'xd0x9cxd0xb0xd0xbaxd0xb5xd0xb4xd0xbexd0xbdxd1x81xd0xbaxd0xb8' <===> Македонски
b'Malti' <===> Malti
b'xe0xa4xaexe0xa4xb0xe0xa4xbexe0xa4xa0xe0xa5x80' <===> मराठी
b'xe1x83x9bxe1x83x90xe1x83xa0xe1x83x92xe1x83x90xe1x83x9axe1x83xa3xe1x83xa0xe1x83x98' <===> მარგალური
b'xd9x85xd8xa7xd8xb2xd9x90xd8xb1xd9x88xd9x86xdbx8c' <===> مازِرونی
b'Bahasa Melayu' <===> Bahasa Melayu
b'xd0x9cxd0xbexd0xbdxd0xb3xd0xbexd0xbb' <===> Монгол
b'Nederlands' <===> Nederlands
b'xe0xa4xa8xe0xa5x87xe0xa4xaaxe0xa4xbexe0xa4xb2 xe0xa4xadxe0xa4xbexe0xa4xb7xe0xa4xbe' <===> नेपाल भाषा
b'xe6x97xa5xe6x9cxacxe8xaax9e' <===> 日本語
b'Norsk bokmxc3xa5l' <===> Norsk bokmål
b'Nouormand' <===> Nouormand
b'Occitan' <===> Occitan
b'Oxcaxbbzbekcha/xd1x9exd0xb7xd0xb1xd0xb5xd0xbaxd1x87xd0xb0' <===> Oʻzbekcha/ўзбекча
b'xe0xa8xaaxe0xa9xb0xe0xa8x9cxe0xa8xbexe0xa8xacxe0xa9x80' <===> ਪੰਜਾਬੀ
b'xd9xbexd9x86xd8xacxd8xa7xd8xa8xdbx8c' <===> پنجابی
b'xd9xbexdax9axd8xaaxd9x88' <===> پښتو
b'Plattdxc3xbcxc3xbctsch' <===> Plattdüütsch
b'Polski' <===> Polski
b'Portuguxc3xaas' <===> Português
b'Romxc3xa2nxc4x83' <===> Română
b'Romani' <===> Romani
b'xd0xa0xd1x83xd1x81xd1x81xd0xbaxd0xb8xd0xb9' <===> Русский
b'Seeltersk' <===> Seeltersk
b'Shqip' <===> Shqip
b'Simple English' <===> Simple English
b'Slovenxc4x8dina' <===> Slovenčina
b'xdaxa9xd9x88xd8xb1xd8xafxdbx8cxdbx8c xd9x86xd8xa7xd9x88xdbx95xd9x86xd8xafxdbx8c' <===> کوردیی ناوەندی
b'xd0xa1xd1x80xd0xbfxd1x81xd0xbaxd0xb8 / srpski' <===> Српски / srpski
b'Suomi' <===> Suomi
b'Svenska' <===> Svenska
b'Tagalog' <===> Tagalog
b'xe0xaexa4xe0xaexaexe0xaexbfxe0xaexb4xe0xafx8d' <===> தமிழ்
b'xe0xb8xa0xe0xb8xb2xe0xb8xa9xe0xb8xb2xe0xb9x84xe0xb8x97xe0xb8xa2' <===> ภาษาไทย
b'Taqbaylit' <===> Taqbaylit
b'xd0xa2xd0xb0xd1x82xd0xb0xd1x80xd1x87xd0xb0/tatarxc3xa7a' <===> Татарча/tatarça
b'xe0xb0xa4xe0xb1x86xe0xb0xb2xe0xb1x81xe0xb0x97xe0xb1x81' <===> తెలుగు
b'xd0xa2xd0xbexd2xb7xd0xb8xd0xbaxd3xa3' <===> Тоҷикӣ
b'Txc3xbcrkxc3xa7e' <===> Türkçe
b'xd0xa3xd0xbaxd1x80xd0xb0xd1x97xd0xbdxd1x81xd1x8cxd0xbaxd0xb0' <===> Українська
b'xd8xa7xd8xb1xd8xafxd9x88' <===> اردو
b'Tixe1xbaxbfng Vixe1xbbx87t' <===> Tiếng Việt
b'Vxc3xb5ro' <===> Võro
b'xe6x96x87xe8xa8x80' <===> 文言
b'xe5x90xb4xe8xafxad' <===> 吴语
b'xd7x99xd7x99xd6xb4xd7x93xd7x99xd7xa9' <===> ייִדיש
b'xe4xb8xadxe6x96x87' <===> 中文
现代的计算机本质上是一个巨大的开关阵列。计算机用电来触发这些开关的开启或关闭。这些开关用 1
表示开启、有电、接通,用 0
表示关闭、没电、切断。我们称这些 1
和 0
为『位』(bit)。
我们用「字节」(byte)表示一个 (8) 位(0
和 1
)的序列。
美国信息交换标准代码(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))个数字:
为了统一编码方式,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
,不会继续检查后面的语句。 - 任何包含
True
的or
语句,只要处理到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:ProgramDataAnaconda3libsite-packagesIPythoncoreinteractiveshell.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
语句的规则
- 每一条
if
语句必须包含一个else
。(为了防止出现未考虑到的条件出现) - 若这个
else
永远都不应该被执行到,因为它本身没有任何意义,则必须在else
语句后面使用一个叫die
的函数,让它打印出出错消息并且「死」给你看。 if
语句的嵌套不要超过两层,最好尽量保持只有一层。- 将
if
语句当作段落来对待,其中的每一个if-elif-else
组合就跟一个段落的句子组合一样。在这种组合的最前面和最后面留一个空行以作区分。 - 你的布尔测试应该很简单,如果它们很复杂,你需要在函数里面将它们的运算顺序事先放到一个变量里,并且为变量取一个名字。
调试小技巧
- 最好的调试方法是使用
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):
def __init__(self, ***)':
'class %%% has-a __init__ that takes self and *** params.',
'class %%%(object):
def ***(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}
")
except EOFError:
print('
Bye')
关于 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()