整理一下学过的函数
print(' ')单双引号打印
print("Roosters",100-25*3%4)
打印文档以及计算结果
print(" " , 某物)
直接打进某物的值而不是变量名
print(f"let's tale about {my_name}")
total = my_age + my_height + my_weight
print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}")
——————————————————————————————————————————
练习5
f +花括号打印以及赋值
hilarious = False
joke_evaluation = "Isn't that joke so funny?!{}"
print(joke_evaluation.format(hilarious))
———————————————————————————————————————————
练习6
用+号链接字符串,.format 函数和{}的使用,注释一下format函数的作用,输出字符串格式化,看他format的是什么格式的内容
print('.'*10)
print (end1 + end2 + end3 + end4 + end5 + end6 ,end = ' ')
print (end7 + end8 + end9 + end10 + end11 + end12)
——————————————————————————————————————————
练习7
第一句,打印10个点,打印中的两种连接方式, + 与 ,
———————————————————————————————————————————
练习8
print( {} .format)占位符与变量format的使用
———————————————————————————————————————————
练习9
打印变量,带字符型的 以及\n转义符 ''' ''' 转义
———————————————————————————————————————————
练习10
转义符号 \t 为转义tab退4格
———————————————————————————————————————————
练习11
通过轮流输入input来获取变量的值,将赋值后的变量通过f“{变量名}”的形式输出
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 are {age} old, {height} tall and {weight} heavy.")
也可以将两步化成一步,如这样
age = input("How old are you?")
input后面的值为提示输入的值,前面的为输入值赋给的变量
———————————————————————————————————————————
练习13
导入功能 from ......import......导入模块/库
argv也是获取输入的意思,但他在powershell命令行中获取,如下,script是脚本的意思
$ python3.6 ex13.py first 2nd 3rd
导入脚本 第一个 第二个 第三个
———————————————————————————————————————————
练习14
提示和传递,把用户提示符设置成prompt,然后再input中显示它,argv要求在运行程序前读取值
而再程序运行中使用input去读值
xxx=input(prompt)
1 from sys import argv
2
3 script, user_name = argv
4 prompt = '> '
5
6 print(f"Hi {user_name}, I'm the {script} script.")
7 print("I'd like to ask you a few questions.")
8 print(f"Do you like me {user_name}?")
9 likes = input(prompt)
———————————————————————————————————————————练习15、16 阅读文件,编辑器
使用 open() 函数一定要保证关闭文件对象,即调用 close() 函数。
open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。
f=open(file, mode='r')
print(f.read())
- close - 关闭文件,就像编辑器中的 “文件->另存为”一样。
- read - 读取文件内容。你可以把读取结果赋给一个变量。
- readline - 只读取文本文件的一行内容。
- truncate - 清空文件。清空的时候要当心。
- write('stuff') - 给文件写入一些“东西”。
- seek(0) - 把读/写的位置移到文件最开头。
r.w.a代表read,write,append模式打开文件
———————————————————————————————————————————
练习17 从一个文件复制到另一个文件
in_file = open(from_file)
indata = in_file.read()
out_file = open(to_file,'w')
out_file.write(indata)
把一段文本复制到另一文本中,记得open完要close
另,本例出现两个新的函数,exists和len
———————————————————————————————————————————
练习18 定义函数(变量)
括号中的变量是有关于这个函数的变量,假如他带星号说明需要进一步解包
def print_two(*args):
arg1,arg2 = args #参数解包args,*args说明这是一个参数包不能直接调用
print(f"arg1:{arg1},arg2:{arg2}")