笨方法学python3

阅读《笨方法学python3》,归纳的知识点

习题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)

  

  

原文地址:https://www.cnblogs.com/guohaoblog/p/11282948.html

时间: 2024-08-05 04:08:01

笨方法学python3的相关文章

笨方法学Python3(21-44)

接着前天的总结 习题21:函数可以返回某些东西 定义函数的加减乘除,以及嵌套使用 习题22:回顾以前学的知识 习题23:字符串.字节串和字符编码 综合运用字符串.函数.文件读取等知识.详情如下: from sys import argv script, encoding, error = argv # 命令行参数处理 def main(language_file, encoding,errors): line = language_file.readline() # 就是用readline()处

Day 2 笨方法学Python

手打第25个练习,出错的地方有: def 定义后indent 4个空格,第一行空了以后,直接换行是跟上面对其的,但是运行时是错误的,我的解决方法是,重新手动空格4个: 还发现一个问题就是,中文解释,以前老是出错 # -*- coding : utf-8 -*- 网上看到的加上这个就可以了,但是我的还是出错.今天偶然在削微寒的博客http://www.cnblogs.com/xueweihan/的GIthub上找到了答案 #coding:utf-8 换成这个语句就可以了.以后,尽量每句都加上注释,

笨方法学python(5)加分题

这篇对应的是习题17,更多文件操作 # -*- coding: utf-8 -*- #对文件更多操作复制A文件的内容到B文件 #from sys import argv from os.path import exists prompt = "> " from_file = raw_input("please input the filename where you want to copy from: >") #in_file = open(from_

笨方法学python(6)加分题--列表与字典的区别

he string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIG

笨方法学python(4)加分题

自己在看笨方法学python这本书,把自己觉得有学到东西的记下来,并不是每个习题都有记录 这次对应的是:习题 6: 字符串(string)和文本 这次只要是想说明一下,在print语句中,只要带有格式化字符的,会当作格式化来处理 脚本1: 结果1: 打出的结果没有%r,那是因为当作格式化处理了 脚本2: 结果2: 会报错,因为print joke_evaluation %hilarious 是格式化的标识 脚本3: 结果3:

[IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果你觉得英文版看着累,当当网有中文版,也有电子版可以选择. 我试着将其中的代码更新到Python 3.同时附上一些自己的初学体会,希望会对你有帮助. 中文版有人把书名翻译为<笨办法学python>,其实我觉得叫做<学Python,不走寻常路>更有意思些. 作者的意思你可以在序言中详细了解

python3 _笨方法学Python_日记_DAY5

Day5 习题  24:  更多练习 1 print("Let's practice everything.") 2 print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.') 3 4 poem = """ 5 \tThe lovely world 6 with logic so firmly planted 7 cannot discern \n

python3 _笨方法学Python_日记_DAY4

Day4 习题  19:  函数和变量 1 def cheese_and_crackers(cheese_count, boxes_of_crackers): 2 print("You have %d cheeses!" % cheese_count) 3 print("You have %d boxes of crackers!" % boxes_of_crackers) 4 print("Man that's enough for a party!&q

python3 _笨方法学Python_日记_DAY6

Day6 习题  36:  设计和调试 If 语句的规则1. 每一个"if 语句"必须包含一个 else.2. 如果这个 else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做 die 的函数,让它打印出错误信息并且死给你看,这和上一节的习题类似,这样你可以找到很多的错误.3. "if 语句"的嵌套不要超过 2 层,最好尽量保持只有 1 层. 这意味着如果你在 if 里边又有了一个 if,那你就需要把第二个 if 移到另一个