[Python] 1.笨办法学Python3小结I

1.准备工作

安装完Python后,选用一个简单的文本编辑器(Atom),一个命令行终端(PowerShell),在文本编辑器上输入指令并保存,并在命令终端行上查看结果,用cd文件的形式逐层选择合适的路径,再运行python ex1.py

2.注释和禁用

用#禁用代码或做注释

3.变量和命名

=的作用是将右边的值赋给左边的变量名,==的作用是检查两侧是否相等

4.格式化字符串(格式字符串)

name = "edward"  # 定义变量为文字的时候把引号加上
print(f"My name is {name}")  # 这种写法更好,f别忘了
print("My name is {x}".format(name))  # 这种写法没有f

5.字符串和文本的结合

a = "my name is {}"
b = "edward"
print(a.format(b))

# 或者这样

c = "my name is {}, I am {} years old"
print(c.format("edward",11))

6.转义序列

常用的:\\(转义\本身),\'(转义单引号),\“(转义双引号),\ d(换行),\t(水平)

另:一组 “”“之间可以放入任意多行文本

7.输入(输入)

x = "How old are you ?"
print(input(x))
print(input('What\'s your name?'))

8.参数arguement,解包解包和变量(将变量传递给脚本)

参数变量:这个变量保存着你运行的Python脚本时传递给Python的脚本的参数

解包:将参数一次赋值给左侧的变量

# 我的理解在脚本运行前先对变量进行一个对应的赋值
from sys import argv
script, first, second, third = argv
$ python test.py 1st 2nd 3rd
# 运行脚本时提供的参数个数要数目正好
# 如果在用户执行脚本前就要输入,用argv,在运行中输入用input

9.读取文件(open and read),('文件名。指令'的形式)

from sys import argv # 从系统标准库sys中导入argv模块, argv是「argument variable」参数变量的简写形式,一般在命令行调用的时候由系统传递给程序
script, filename = argv  # 命名要用到的参数变量

text = open(filename)  # 将打开对应的文件这个操作命名为text

print(f"Here's your file {filename}:")  #根据所给的filename打印一句话
print(text.read())  #执行text文件的读取命令

print("Type the filename again:")  #提示用户再次输入文件名
file_again = input("> ")  #输入文本名字 类似之前的argv

text_again = open(file_again) #将打开对应的文件命名为text_again

print(text_again.read())  #执行text_again的读取命令

顺序注意:先打开文件,再对打开的文件进行读取

a = open(文件名,'w')#w为写入,r为读取,a为追加,另外可以用'w +','r +','a +'的形式吧文件用同时读写的方式打开

10.常用的读写文件命令('文件名。指令'的形式)

收盘:等同于编辑器中的“文件” - “保存”

读:读取文件的内容,可以把结果赋值给一个变量

readline的:读取文本文件中的一行

截断:清空文件

写( “东西”):将 “东西” 写入文件

求(0):将读写位置移动到文件开头

# 从内置模块中载入参数变量
from sys import argv
  
# 定义参数变量的个数及名称
script, filename = argv  

print(f'We\'are going to erase {filename}')
print(f'If you don\'t want to that, hit CTRL-C(^C).')
print(f"If you wannt that, hit RETURN.")

input("?")

# 告诉用户打开了文件了
print("Opening the file...")  
# 将traget设置为打开的file文件,且格式为w,读取
target = open(filename, 'w')  

#告诉用户删除文件了
print("Truncating the file. Goodbye!")  
#删除文件,用targer.命令的形式指定对该文件的操作
target.truncate()  

#提示用户输入三行
print("Now I'm going to ask you for three lines.")  

line1 = input("line 1: ")  #第一行
line2 = input("line 2: ")  #第二行
line3 = input("line 3: ")  #第三行

#提示客户将会将这三行写进文件中
print("I'm going to write these to the file.")  

target.write(line1)  
target.write("\n")  
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

# 另一种写法
target.write(f"""
{line1}
{line2}
{line3}
""")

#告诉用户要关闭文件了
print("And finally, we close it.")
#关闭文件  
target.close()  

11.函数(DEF)

# this one is like your scripts with argv
def print_two(*args):
    arg1, arg2 = args
    print(f"arg1: {arg1}, arg2: {arg2}")

# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
    print(f"arg1: {arg1}, arg2: {arg2}")

# this just takes one argument
def print_one(arg1):
    print(f"arg1: {arg1}")

# this one take no arguements
def print_none():
    print("I got nothin'.")

print_two("Ryan","Edward")
print_two_again("Ryan","Edward")
print_one("First!")
print_none()

* ARGS中的*是告诉Python的把函数所有的参数都收进来,并且放到名为ARGS的列表中去,不常用

12.函数和变量

def cheese_and_crackers(cheese_count, boxes_of_crackers):
    print(f"You have {cheese_count} cheeses!")
    print(f"You have {boxes_of_crackers} boxes of crackers!")
    print("Man that's enough for a party!")
    print("Get a blanket.\n")

# 函数内可以只用数值
print("We can just give the function numbers directly:")
cheese_and_crackers(20, 30)

# 函数内可以只用变量
print("OR, we can use variables from our script:")
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)

# 函数内可以数值运算
print("We can even do math inside too:")
cheese_and_crackers(10 + 20, 5 + 6)

# 函数内可以变量和数值运算
print("And we can combine the two, variables and math:")
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

# 函数变量可以选用输入的值
# 要对输入值进行运算的话,要先用int()函数将其转化为数值格式再运算!!!!
print("And we can let you input the number:")
a = input("How many cheese do you have? > ")
b = input("How many crackers do you have? > ")
cheese_and_crackers(a, b)

# 函数变量可以选择启用时设置参数变量
# 注意事项:
# 1.三个参数都要用到
# 2.要对输入值进行运算的话,要先用int()函数将其转化为数值格式再运算!!!!
from sys import argv
script, c, d = argv
print(f"I'am your cook {script}. Let me tell you how many food we have...")
cheese_and_crackers(c, d)

总结:

1.函数内可以只使用数值,函数内也可以只使用变量,函数内可以数值运算,函数内也可以数值和变量运算,但是如果要对输入的值进行运算的话,要先用诠释函数转换成数值格式才能进行计算

2. + =操作为加法赋值运算,i + = 1等于i = i + 1

函数可以返回某些东西(return),返回相比于print更全能一些,两者的区别:https://www.zhihu.com/question/23765556

返回可以之后可以做后续计算和使用,打印的话打印完就没有了

猜你喜欢

转载自blog.csdn.net/Edward_is_1ncredible/article/details/81349577