博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
笨方法学python3
阅读量:4591 次
发布时间:2019-06-09

本文共 4554 字,大约阅读时间需要 15 分钟。

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

相关代码详见github地址:

习题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 4one two three fourTrue 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 = Falsejoke_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 thisscript, first, second, third = argvprint("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 ZedHi Zed,I'm the ex14.py script.'I'd like to ask ypu a few questions.Do ypu like me Zed?>yesWhere do ypu live Zed?>yichengWhat kind of computer do you have?>Dell'''script , user_name = argvprompt = '>'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 argvscript, input_file = argvdef 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 = 1print_a_line(current_line,current_file)current_line = current_line + 1print_a_line(current_line,current_file)current_line = current_line + 1print_a_line(current_line, current_file)

  

  

 

转载于:https://www.cnblogs.com/guohaoblog/p/11282948.html

你可能感兴趣的文章
T-SQL函数总结
查看>>
python 序列:列表
查看>>
web移动端
查看>>
5. Longest Palindromic Substring (DP)
查看>>
sql语句一些简单的用法
查看>>
领域驱动设计之聚合与聚合根实例一
查看>>
selenium中各个模块操作:下拉框、鼠标悬浮连贯、拼图拖拽操作
查看>>
C# 调用Windows图片查看器
查看>>
Excel系列教程(1):如何自动填充单元格
查看>>
jQuery中的冒泡事件和阻止冒泡
查看>>
pythonchallenge闯关 第13题
查看>>
linux上很方便的上传下载文件工具rz和sz使用介绍
查看>>
React之特点及常见用法
查看>>
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
java23中设计模式只责任链模式
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>