笨方法学python3
阅读《笨方法学python3》,归纳的知识点
相关代码详见github地址:https://github.com/BMDACMER/Learn-Python
习题1:安装环境+练习 print函数使用 主要区别双引号和单引号的区别
习题2:注释符号#
习题3:运算符优先级,跟C/C++, Java类似
以下运算符优先级:从下往下以此递增,同行为相同优先级 Lambda #运算优先级最低
逻辑运算符: or
逻辑运算符: and
逻辑运算符:not
成员测试: in, not in
同一性测试: is, is not
比较: <,<=,>,>=,!=,==
按位或: |
按位异或: ^
按位与: &
移位: << ,>>
加法与减法: + ,-
乘法、除法与取余: *, / ,%
正负号: +x,-x
习题4:变量名+打印 介绍了以下这种形式的打印
print("There are", cars, "cars available.")
习题5:更多打印方式 比如 f"XXX"
my_name = 'Zed A. Shaw'
print(f"Let's talk about {my_name}")
习题6:继续使用f"XX"打印
习题7:format打印方式,可采用end=‘ ’代替换行
print("Its fleece was white as {}.".format('show'))
print(end1 + end2 + end3 + end4 + end5 + end6, end=' ')
print(end7 + end8 +end9 + end10 +end11 + end12)
显示如下:
Its fleece was white as show.
Cheese Burger
习题8:更多打印方式(这种打印方式见的较少)
formatter = '{} {} {} {}'
print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
print(formatter.format(True, False, False, True))
print(formatter.format(formatter, formatter, formatter, formatter))
# print("\n")
print(formatter.format(
"Try your",
"Own text here",
"Maybe a poem",
"Or a song about fear"
))
显示如下:
1 2 3 4
one two three four
True False False True
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
Try your Own text here Maybe a poem Or a song about fear
习题9:多行打印,换行打印
小结:打印方式
1)print("There are", cars, "cars available.") # 变量名直接打印
2)print(f"Let's talk about {my_name}") # 带有f"{}"的打印方式
3) 变量名+format()
hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))
4)print("Its fleece was white as {}.".format('show')) #
5)formatter = '{} {} {} {}'
print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
6)print('''
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want,or 5,or 6
''') # 多行打印
习题10:转义字符
习题11:介绍 input()函数,并使用到习题5介绍的打印方式
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weight?", end=' ')
weight = input()
print(f"So,you're {age} old,{height} tall and {weight} heavy.")
习题12:介绍input()函数 加提示符
age = input("How old are you?")
print("How old are you? {}".format(input())) # 先运行后面的在运行前面的提示
习题13: 参数、解包和变量
from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third) # 注: 把argv中的东西取出,解包,将所有的参数依次复制给左边的这些变量
>>python ex13.py first second third
练习14:提示与传递 主要讲解 input()函数 配合输入格式
from sys import argv
'''
PS E:\dev\code\笨方法学python3> python ex14.py Zed
Hi Zed,I'm the ex14.py script.'
I'd like to ask ypu a few questions.
Do ypu like me Zed?
>yes
Where do ypu live Zed?
>yicheng
What kind of computer do you have?
>Dell
'''
script , user_name = argv
prompt = '>' print(f"Hi {user_name},I'm the {script} script.'")
print("I'd like to ask you a few questions.")
print(f"Do ypu 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.
''')
练习15:读取文件
txt = open(filename)
print(f"Here's your file {filename}:")
print(txt.read())
txt.close()
练习16:读写文件
close:关闭文件。跟你的编辑器中的“文件”->“保存”是一个意思
read:读取文件的内容
readline:只读取文本文件中的一行
truncate:清空文件,请小心使用该命令
write('stuff'):将“stuff”写入文件
seek(0): 将读写位置移动到文件开头
练习17:继续读写文件
in_file = open(from_file)
indata = in_file.read() out_file = open(to_file,'w')
out_file.write(indata)
练习18:函数
练习19:函数与变量
pytyon在定义函数的时候跟C/C++区别蛮大,python不需要定义参数类型,定义某一函数时 可参考如下:
def function(vailable1, vailable2,*):
练习20: 函数和文件
from sys import argv script, input_file = argv def print_all(f):
print(f.read()) def rewind(f):
f.seek(0)
# 带有行号的打印内容
def print_a_line(line_count,f):
print(line_count, f.readline()) current_file = open(input_file) # 接受的参数,打开文件 print("First let's print the whole file:\n") print_all(current_file) # 打印该文件 print("Now let's rewind, kind of like a tape.") rewind(current_file) # 将读写位置移动到文件开头(第一个字符的前面一个位置) 方便后续读取 如不执行该操作,后续打印为空 print("Let's print three lines:") current_line = 1
print_a_line(current_line,current_file) current_line = current_line + 1
print_a_line(current_line,current_file) current_line = current_line + 1
print_a_line(current_line, current_file)
笨方法学python3的更多相关文章
- 笨方法学Python3(21-44)
相关代码详见github地址:https://github.com/BMDACMER/Learn-Python 接着前天的总结 习题21:函数可以返回某些东西 定义函数的加减乘除,以及嵌套使用 习题2 ...
- "笨方法学python"
<笨方法学python>.感觉里面的方法还可以.新手可以看看... 本书可以:教会你编程新手三种最重要的技能:读和写.注重细节.发现不同.
- 笨方法学python 22,前期知识点总结
对笨方法学python,前22讲自己的模糊的单词.函数进行梳理总结如下: 单词.函数 含义 print() 打印内容到屏幕 IDLE 是一个纯Python下自带的简洁的集成开发环境 variable ...
- 《笨办法学Python3 》入坑必备,并不是真笨学!!!
<笨办法学Python3 >免费下载地址 内容简介 · · · · · · 本书是一本Python入门书籍,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的 ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨方法学python--安装和准备
1 下载并安装python http://python.org/download 下载python2.7. python2.7并不是python3.5的旧版本. python2现在应用较广,网上资料较 ...
- 《笨方法学Python》加分题32
注意一下 range 的用法.查一下 range 函数并理解它在第 22 行(我的答案),你可以直接将 elements 赋值为 range(0, 6) ,而无需使用 for 循环?在 python ...
- 《笨方法学Python》加分题20
加分练习通读脚本,在每一行之前加注解,以理解脚本里发生的事情.每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量,在每次调用时,打印出 current_line ...
- 《笨方法学Python》加分题15
本题本题开始涉及文件的操作,文件操作是一件危险的事情,需要仔细细心否则可能导致重要的文件损坏. 本题除了 ex15.py 这个脚本以外,还需要一个用来读取的文件 ex15_sample.txt 其内容 ...
随机推荐
- [转帖]String、StringBuilder与StringBuffer
String.StringBuilder与StringBuffer https://www.jianshu.com/p/37f3799bdb56 1.String String本质 String是不可 ...
- C++引用与常量
常量: 在C++中有许多种数据类型(如int,float,bool等等).而这些数据类型又可以声明定义出变量与常量两种不同的具体数据.它们两种分类的标准是不一样的,是两个角度可以叠加的分类,举个栗子: ...
- bypass-media 模式30秒挂断
语音正常,但是通话30秒后自动挂断, 服务器为阿里云,专网模式 修改ext-sip-ip 为公网ip
- Nginx官方文档翻译(转)
add by zhj: 由并发网组织翻译,赞 <Nginx官方文档>WebSocket代理 <Nginx官方文档>配置文件中的单位 <Nginx官方文档>控制ngi ...
- BZOJ3145 [Feyat cup 1.5]Str 后缀树、启发式合并
传送门--BZOJCH 考虑两种情况: 1.答案由一个最长公共子串+可能的一个模糊匹配位置组成.这个用SAM求一下最长公共子串,但是需要注意只出现在\(S\)的开头和\(T\)的结尾的子串是不能够通过 ...
- Java的常用API之Object类简介
Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...
- Java学习:数组的使用和注意事项
数组 数组的概念:是一种容器,可以同时存放多个数据值 数组的特点: 数组是一种引用数据类型 数组当中的多个数据,类型必须统一 数组的长度在程序运行期间不可以改变 数组的初始化:在内存当中创建一个数组, ...
- Cglib 与 JDK动态代理
作者:xiaolyuh 时间:2019/09/20 09:58 AOP 代理的两种实现: jdk是代理接口,私有方法必然不会存在在接口里,所以就不会被拦截到: cglib是子类,private的方法照 ...
- 【vue】搭建vue环境以及要安装的所有东西
参考地址: https://www.cnblogs.com/laizhouzhou/p/8027908.html
- Python面向对象之私有属性和私有方法
01. 应用场景及定义方式 应用场景 在实际开发中,对象 的 某些属性或方法 可能只希望 在对象的内部被使用,而 不希望在外部被访问到 私有属性 就是 对象 不希望公开的 属性 私有方法 就是 对象 ...