PythonThinking_T4打卡

T4总结:
创建函数\n
函数与变量:传入参数
函数与文件操作?
函数return实现四则运算
input

#创建函数、参数个数
# 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 takes no arguments
def print_none():
    print("I got nothin'.")


print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
arg1: Zed, arg2: Shaw
arg1: Zed, arg2: Shaw
arg1: First!
I got nothin'.

**\*args 中的 \* 是什么作用?**这是告诉 Python 取所有的参数给函数,然后把它们放在 args 里放成一列,很像你之前学的 argv ,只不过这个是为函数设置的。这种不常用,除非有特殊需要。

#函数和变量:传入参数
def cheese_and_crackers(cheese_count,boxes_of_crackers):
    print("you have",cheese_count,"cheeses!")
    print("you have",boxes_of_crackers,"boxes of crackers!\n")

print("We can just give the function numbers directly:")
cheese_and_crackers(20,30)

print('or,we can use variables from our scripy:')
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:")
# print("!",amount_of_cheese)
cheese_and_crackers(10+20,5+6)

print("And we can combine two,variables and math:/n")
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)

# print("Let's input")
# amount_of_cheese=int(input())
# amount_of_crackers=int(input())
# cheese_and_crackers(amount_of_cheese,amount_of_crackers)

#函数和变量 HDW exe19
def cheese_and_crackers(cheese_count,boxes_of_crackers):
    print("you have",cheese_count,"cheeses!")
    print("you have",boxes_of_crackers,"boxes of crackers!\n")

print("We can just give the function numbers directly:")
cheese_and_crackers(20,30)

print('or,we can use variables from our scripy:')
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 two,variables and math:/n")
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)

# print("Let's input")
# amount_of_cheese=int(input())
# amount_of_crackers=int(input())
# cheese_and_crackers(amount_of_cheese,amount_of_crackers)

We can just give the function numbers directly:
you have 20 cheeses!
you have 30 boxes of crackers!

or,we can use variables from our scripy:
you have 10 cheeses!
you have 50 boxes of crackers!

we can even do math inside too:
you have 30 cheeses!
you have 11 boxes of crackers!

And we can combine two,variables and math:/n
you have 110 cheeses!
you have 1050 boxes of crackers!

1.实现input输入

2.变量传递给函数是暂时的:
在函数中在函数中创建建 amount_of_cheese 这个个变量会改量会改变 cheese_count 这个个变量量吗?? 不会的,这些变量是相互独立并存在于函数之外的。它们之后会传递给函数,而且是“暂时版”,只是为了让函数运行。当函数退出之后,这些暂时的变量就会消失,

可以在函数里调用函数

前置复习:
?需要查阅资料

  • close - 关闭文件,就像编辑器中的 “文件->另存为”一样。
  • read - 读取文件内容。你可以把读取结果赋给一个变量。
  • readline - 只读取文本文件的一行内容。
  • truncate - 清空文件。清空的时候要当心。
  • write(‘stuff’) - 给文件写入一些“东西”。
  • seek(0) - 把读/写的位置移到文件最开头。
#函数和文件 HDW exe20
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)
  File "<ipython-input-8-de0b9daa0ebb>", line 7
    print(f.read())
    ^
IndentationError: expected an indented block
#函数return实现四则运算 e21
def add(a, b):
    print("ADDING %d + %d"%(a,b))
#   print(f"ADDING {a} + {b}")这种写法可用于替换
    print(f"ADDING {a} + {b}")
    return a + b

def subtract(a, b):
    print(f"SUBTRACTING {a} - {b}")
    return a - b

def multiply(a, b):
    print(f"MULTIPLYING {a} * {b}")
    return a * b

def divide(a, b):
    print(f"DIVIDING {a} / {b}")
    return a / b


print("Let's do some math with just functions!")

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}\n")
#可知return赋值给了age、height

# A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.")
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
#嵌套,一级一级算就OK
print("That becomes: ", what, "Can you do it by hand?\n")

print("Let's iuput")
num1 = int(input("输入第一个数字: "))
num2 = float(input("输入第二个数字: "))
age=add(num1,num2)
print(age)
Let's do some math with just functions!
ADDING 30 + 5
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLYING 90 * 2
DIVIDING 100 / 2
Age: 35, Height: 74, Weight: 180, IQ: 50.0

Here is a puzzle.
DIVIDING 50.0 / 2
MULTIPLYING 180 * 25.0
SUBTRACTING 74 - 4500.0
ADDING 35 + -4426
ADDING 35 + -4426.0
That becomes:  -4391.0 Can you do it by hand?

Let's iuput
输入第一个数字: 1
输入第二个数字: 2
ADDING 1 + 2
ADDING 1 + 2.0
3.0

Python3.6新增了一种f-字符串格式化
格式化的字符串文字前缀为’f’和接受的格式字符串相似str.format()。它们包含由花括号包围的替换区域。替换字段是表达式,在运行时进行评估,然后使用format()协议进行格式化。

#以上只是概述,看看PYHDW函数讲解如何展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值