├── LICENSE ├── Q10-提问 ├── Q10-提问.md ├── code10-1.py ├── code10-2.py ├── code10-3.py └── code10-4.py ├── Q13-参数-解包-变量 ├── Q13-参数-解包-变量.md ├── code13-1.py └── code13-2.py ├── Q5-更多的变量和打印 ├── Q5-更多的变量和打印.md ├── code5-1.py └── code5-2.py ├── Q6-字符串和文本 ├── Q6-字符串和文本.md ├── code6-1.py ├── code6-2.py └── code6-3.py ├── Q7-打印打印 ├── Q7-打印打印.md ├── code7-1.py ├── code7-2.py ├── code7-3.py ├── code7-4.py └── code7-5.py └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 轩灵 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Q10-提问/Q10-提问.md: -------------------------------------------------------------------------------- 1 | 试想如果我们想利用程度和别人交互呢?比如希望别人输入点东西什么的?还是很常见的吧?那么如果做呢?🤔 2 | 3 | 如果单说给点提示告诉别人输入什么的话,当然是可以用`print`了。 4 | 5 | ```python 6 | print('How old are you?') 7 | ``` 8 | 9 | 但是上面👆这样有一个问题哦,如何接受到用户的输入呢?额,这就不行了吧 ̄□ ̄||。所以现在就需要一个开拓新的领地了。 10 | 11 | 接下来隆重介绍的就是可以接收用户输入的`input()`函数😱! 12 | 13 | 你可以这样: 14 | 15 | `code10-1.py` 16 | ```python 17 | print('How old are you?') 18 | age = input() 19 | print(f"so,y're {age} old") 20 | ``` 21 | 运行之后,会显示`How old are you?`的提示语,然后等待你的输入。当你输入`24`之后,就会接着打印出`so,y're 24 old`。 22 | 23 | 怎么样?是不是很棒😏 24 | 25 | 但是我想你可能会有一个疑问,既然`input()`是一个函数,那能不能直接将提示语作为参数传入呢🤔? 26 | 27 | 你的猜测总是正确的😛,你已经悟了,当然是可以的! 28 | 29 | `code10-2.py` 30 | 31 | ```python 32 | age = input('How old are you?') 33 | print(f"so,y're {age} old") 34 | ``` 35 | 36 | 到这里,仿佛这次练习就可以结束了,但为师还有一个问题想要问问🐶。 37 | 38 | `code10-3.py` 39 | 40 | ```python 41 | print('how old are you?', input()) 42 | ``` 43 | 44 | 运行这段代码,你说会是什么效果呢? 45 | 46 | 我猜你第一次想的肯定不对🙅‍♂️,不过你的确应该亲自试一试,而不只是听我说实际的效果是什么。 47 | 48 | 实际上运行这段代码后,它什么反应都没有😱。但这不是真的🤥,放心好了,它只是在等待你的输入,然后你输入之后,它会打印。 49 | 50 | 就像这样: 51 | 52 | ```python 53 | 24 # 你的输入 54 | how old are you? 24 # 程序的输出 55 | ``` 56 | 57 | 你先输入24,之后程序会输入 `how old are you? 24` 58 | 59 | 既然你这么聪明,那想想这是为什么呢?😶 60 | 61 | 当然是因为 `input()`先执行了呀,然后再执行外层的 `print()` 函数。 62 | 63 | ✌️完美,手工🤚 回见😴 64 | 65 | --------------------------------------------------- 66 | 67 | 额,我又来了。 68 | 69 | 你有没有觉得`input()`的参数可以是一个变量?比如弄个提示符啥的?就像`>`。 70 | 71 | 还是运行一下代码比较好: 72 | 73 | `code10-4.py` 74 | 75 | ```python 76 | prompt = '>' 77 | print('how old are you?') 78 | age = input(prompt) 79 | print(age) 80 | ``` 81 | 82 | 效果如何呢?运行一下康康叭! 83 | 84 | 先是会输出 85 | ```python 86 | how old are you? 87 | > 88 | ``` 89 | 这时候等待你的输入,如果你的输入是 24 90 | 91 | 那么效果看起来会是这样滴: 92 | 93 | ```python 94 | how old are you? 95 | >24 96 | 24 97 | ``` 98 | 99 | OK,到这里你可以👩‍🍳出师了。 100 | 101 | 期待后面的练习叭~ 102 | -------------------------------------------------------------------------------- /Q10-提问/code10-1.py: -------------------------------------------------------------------------------- 1 | print('How old are you?') 2 | age = input() 3 | print(f"so,y're {age} old") -------------------------------------------------------------------------------- /Q10-提问/code10-2.py: -------------------------------------------------------------------------------- 1 | age = input('How old are you?') 2 | print(f"so,y're {age} old") -------------------------------------------------------------------------------- /Q10-提问/code10-3.py: -------------------------------------------------------------------------------- 1 | print('how old are you?', input()) -------------------------------------------------------------------------------- /Q10-提问/code10-4.py: -------------------------------------------------------------------------------- 1 | prompt = '>' 2 | print('how old are you?') 3 | age = input(prompt) 4 | print(age) -------------------------------------------------------------------------------- /Q13-参数-解包-变量/Q13-参数-解包-变量.md: -------------------------------------------------------------------------------- 1 | 不知道你想过没有,我们前面执行代码:`python code.py`时,可不可以在加一些东西呢?比如`python code.py1 flten`,执行这行命令会发生什么呢?🤔如果你没想过那真的是你的好奇心不够,难成大事🤫 2 | 3 | 实际上后面的`flten`不会对结果有任何影响。但如果你使用过Linux或者mac的话,会发现这是经常会遇到的,就是命令后面会加一些参数,那么它们一定是被接收到了呀。 4 | 5 | 我们还是回到Python里叭~ 6 | 7 | 如果说就是你执行了`python code.py1 flten`,而且你想获取到后面的输入,那应该怎么做呢?(你要说这么做是闲的那我也没办法🤷‍♀️) 8 | 9 | `code13-1.py` 10 | ```python 11 | from sys import argv 12 | 13 | script, first = argv 14 | 15 | print(f"script: {script}") 16 | print(f"first: {first}") 17 | 18 | ``` 19 | 20 | 看下结果,和你想的一样吗?不过要注意一点,这个需要在控制台输入命令行运行,也就是`python3 code1.py flten`才行😯,另外当然需要在当前的路径下执行了。 21 | 22 | 输入的结果: 23 | 24 | ```python 25 | script: code1.py 26 | first: flten 27 | ``` 28 | 29 | 会发现,输入的第一个参数是文件的名字??是滴,这就是`python3`的设计,而后面的才是执行文件后面你输入的参数。 30 | 31 | 那么如何取到的呢?🤔 32 | 33 | 看第一句代码`from sys import argv`,这个`sys`是python3提供的包 ,`argv`就是用来处理命令行参数的。 34 | 35 | 因此,就是你看到的结果了~ 36 | 37 | 另外,这里的两个语法需要注意一下,因为是经常会用到的,一个就是引入包: 38 | 39 | ``` 40 | from sys import argv 41 | ``` 42 | 43 | 这句上面已经解释过了。 44 | 45 | 还有一个叫做“解包”,其实就是把一个一个列表里的元素分解出来而已了。什么?难道`argv`返回的是一个列表??当然没错,康康下面的代码就知道了! 46 | 47 | `code13-2.py` 48 | ```python3 49 | from sys import argv 50 | 51 | arg = argv 52 | 53 | print(f"arg: {arg}") 54 | 55 | ``` 56 | 57 | 结果 58 | ```python3 59 | arg: ['code13-2.py', 'flten'] 60 | ``` -------------------------------------------------------------------------------- /Q13-参数-解包-变量/code13-1.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | script, first = argv 4 | 5 | print(f"script: {script}") 6 | print(f"first: {first}") 7 | -------------------------------------------------------------------------------- /Q13-参数-解包-变量/code13-2.py: -------------------------------------------------------------------------------- 1 | from sys import argv 2 | 3 | arg = argv 4 | 5 | print(f"arg: {arg}") 6 | -------------------------------------------------------------------------------- /Q5-更多的变量和打印/Q5-更多的变量和打印.md: -------------------------------------------------------------------------------- 1 | 这个习题中要学会的是如何创建嵌入变量内容的字符串,要在字符串中嵌入变量,需要使用`{}`特殊符号,把变量放在里面。字符串还必须以`f`开头,`f`是format格式化的意思。 2 | 3 | 可以试下👇的代码: 4 | 5 | `code5-1.py` 6 | ```python3 7 | 8 | my_name = 'flten' 9 | 10 | print(f"My name is {my_name}.") 11 | 12 | ``` 13 | 14 | 如何将浮点数四舍五入? 15 | 16 | 可以使用`round()`函数,接收两个参数,第一个参数当然是需要进行近似的数字,第二个数字是保留到小数点后几位,默认是0位,也就是只保留整数部分。请看👇的代码: 17 | 18 | 19 | `code5-2.py` 20 | ```python3 21 | num = 1.73333 22 | res = round(num) # 2 23 | print(res) 24 | 25 | res2 = round(num, 2) 26 | print(res2) # 1.73 27 | ``` -------------------------------------------------------------------------------- /Q5-更多的变量和打印/code5-1.py: -------------------------------------------------------------------------------- 1 | my_name = 'flten' 2 | 3 | print(f"My name is {my_name}.") -------------------------------------------------------------------------------- /Q5-更多的变量和打印/code5-2.py: -------------------------------------------------------------------------------- 1 | num = 1.73333 2 | res = round(num) # 2 3 | print(res) 4 | 5 | res2 = round(num, 2) 6 | print(res2) # 1.73 -------------------------------------------------------------------------------- /Q6-字符串和文本/Q6-字符串和文本.md: -------------------------------------------------------------------------------- 1 | 字符串格式化除了可以使用Q5中`f-string`之外,还可以使用`string.format()`的方式。看下👇的代码叭: 2 | 3 | `code6-1.py` 4 | ```python3 5 | types_of_people = 10 6 | print(f'the number is {types_of_people}') # the number is 10 7 | 8 | str = 'the number is {}' 9 | print(str.format(types_of_people)) # the number is 10 10 | 11 | ``` 12 | 13 | 看👀清楚了咩,使用`string.format()`的话需要目标字符串插入变量的位置留一个`{}`,在格式化目标字符串时将目标字符串作为参数传入`string.format()`中。但是这里有两个问题😯,一个是说`{}`只能放在目标字符串的末尾吗?还是可以任意位置呢?第二问题是说,如果我想传入多个变量呢😏(好贱哦)? 14 | 15 | 那么就试试叭: 16 | `等会,我觉得我可以猜猜🤪` 17 | 我猜第一☝️,木有问题,肯定是可以放到任意位置的;第二,当然也是可以传入多个变量了,相应的应该是给`string.format()`按顺序传入多个参数🤔。那到底是不是呢?一起来康康叭٩(๑>◡<๑)۶ 18 | 19 | `code6-2.py` 20 | ```python3 21 | types_of_people = 10 22 | print(f'the number is {types_of_people} or not') # the number is 10 or not 23 | 24 | num2 = 20 25 | num3 = 30 26 | str = 'the number is {}, or {}, or {}.' 27 | print(str.format(types_of_people, num2, num3)) # the number is 10, or 20, or 30. 28 | ``` 29 | 30 | 🙃我猜滴不错叭 31 | 32 | 还有一个智障🤨的问题(你打我呀)是这样的:就是🥺`string.format()`可以直接传入一个字符串而不是传入定义好的变量吗?😂毕竟代码里没有这样演示啊🥶盲猜 当然是可以了🙄 33 | 34 | `code6-3.py` 35 | ```python3 36 | str = 'the number is {}' 37 | print(str.format('10')) # the number is 10 38 | ``` 39 | -------------------------------------------------------------------------------- /Q6-字符串和文本/code6-1.py: -------------------------------------------------------------------------------- 1 | types_of_people = 10 2 | print(f'the number is {types_of_people}') # the number is 10 3 | 4 | str = 'the number is {}' 5 | print(str.format(types_of_people)) # the number is 10 -------------------------------------------------------------------------------- /Q6-字符串和文本/code6-2.py: -------------------------------------------------------------------------------- 1 | types_of_people = 10 2 | print(f'the number is {types_of_people} or not') # the number is 10 or not 3 | 4 | num2 = 20 5 | num3 = 30 6 | str = 'the number is {}, or {}, or {}.' 7 | print(str.format(types_of_people, num2, num3)) # the number is 10, or 20, or 30. -------------------------------------------------------------------------------- /Q6-字符串和文本/code6-3.py: -------------------------------------------------------------------------------- 1 | str = 'the number is {}' 2 | print(str.format('10')) # the number is 10 -------------------------------------------------------------------------------- /Q7-打印打印/Q7-打印打印.md: -------------------------------------------------------------------------------- 1 | 如果想按照输入的格式原封不动的输出出来该如何做呢🤔?额,没明白?我的意思是说,算了还是看个例子🌰叭: 2 | 3 | ```python3 4 | ' 5 | a 6 | b 7 | c 8 | d 9 | ' 10 | ``` 11 | 这就是想要输出的格式,但是直接这样输出能行吗?得试试,试试就试试🐧 12 | 13 | ```python3 14 | print( 15 | ' 16 | a 17 | b 18 | c 19 | d 20 | ' 21 | ) 22 | ``` 23 | 24 | 额,没错肯定是会报错的了,怎么能这样呢😰那...应该怎么样🐎 25 | 26 | 真想只有一个😬 27 | 28 | 尝试第一种方式: 29 | 30 | 如果我们使用`string.format()`呢🤔? 31 | 32 | 像这样 33 | 34 | `code7-1.py` 35 | ```python3 36 | str = "{} {} {} {}" 37 | print(str.format( 38 | "a", 39 | "b", 40 | "c", 41 | "d" 42 | )) 43 | ``` 44 | 可是`string.format`有这个功能吗😭?答案是,没有。所以它的输出是`a b c d` 45 | 46 | 看来,也只有使出那一招了👿 47 | 48 | 再来,尝试第二种方式: 49 | 50 | `code7-2.py` 51 | ```python3 52 | print("a\nb\nc\nd\n") 53 | ``` 54 | 55 | 没错,就是使用'\n'换行符👻让我们康康结果叭! 56 | 57 | ```python3 58 | a 59 | b 60 | c 61 | d 62 | ``` 63 | 64 | 结果还是很乐观的😜! 65 | 66 | 可是感觉,额,感觉这样写着好挤啊😨。那就分开一点,效果还不一样? 67 | 68 | `code7-3.py` 69 | ```python3 70 | print("a \n b \n c \n d \n") 71 | ``` 72 | 73 | 康康结果肿么样: 74 | 75 | `code7-3.py` 76 | ```python3 77 | a 78 | b 79 | c 80 | d 81 | ``` 82 | 额,略有那么一丢丢不一样,这是为什么呢🤔?噢~(恍然大悟),除了`a`,后面的字母每一个换行`\n`前面都有一个空格嘛。 83 | 84 | 那...还有别的方式吗?自然是有的了。 85 | 86 | 接下来开始第三次尝试: 87 | 88 | `code7-4.py` 89 | ```python3 90 | print(""" 91 | a 92 | b 93 | c 94 | d 95 | """) 96 | ``` 97 | 98 | 哦豁,没想到叭,这样是可以滴! 99 | 100 | `输出结果:` 101 | ```python3 102 | a 103 | b 104 | c 105 | d 106 | ``` 107 | 108 | 所以,字符串按照输入的格式原样输出就可以这样子喽!8过,既然三个双引号可以,那么三个单引号`'''`可以吗🤔?我猜行😂,你觉得咧? 109 | 110 | 那么开始第四次尝试: 111 | 112 | `code7-5.py` 113 | ```python3 114 | print(''' 115 | a 116 | b 117 | c 118 | d 119 | ''') 120 | ``` 121 | 122 | 么得问题! 123 | 124 | ```python3 125 | a 126 | b 127 | c 128 | d 129 | ``` 130 | 131 | `输出结果:` 132 | ```python3 133 | a 134 | b 135 | c 136 | d 137 | ``` 138 | 139 | 神奇不神奇🙅‍♂️ 140 | 141 | 最后,我们总结一下。算了,不想写了💩我觉得挺好懂的,就这样叭🥶回见🤨 142 | 143 | ----------------------------------------- 144 | 145 | 注意:❎ 146 | 147 | 额 ,我又回来了,是为了提醒大爷,`"""`中间是不能有空格的哈,比如这样`" " "`是不对的❌ -------------------------------------------------------------------------------- /Q7-打印打印/code7-1.py: -------------------------------------------------------------------------------- 1 | str = "{} {} {} {}" 2 | print(str.format( 3 | "a", 4 | "b", 5 | "c", 6 | "d" 7 | )) 8 | 9 | # a b c d -------------------------------------------------------------------------------- /Q7-打印打印/code7-2.py: -------------------------------------------------------------------------------- 1 | print("a\nb\nc\nd\n") 2 | -------------------------------------------------------------------------------- /Q7-打印打印/code7-3.py: -------------------------------------------------------------------------------- 1 | print("a \n b \n c \n d \n") 2 | 3 | -------------------------------------------------------------------------------- /Q7-打印打印/code7-4.py: -------------------------------------------------------------------------------- 1 | print(""" 2 | a 3 | b 4 | c 5 | d 6 | """) -------------------------------------------------------------------------------- /Q7-打印打印/code7-5.py: -------------------------------------------------------------------------------- 1 | print(''' 2 | a 3 | b 4 | c 5 | d 6 | ''') -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HandwayPython3 2 | HandWayPython3 笨办法学python3,来玩呀! 3 | --------------------------------------------------------------------------------