repr():对象的可评估字符串表示形式(可以"eval()"它,这意味着它是一个求值为Python对象的字符串表示形式)
换一种说法:
>>> x = 'foo'
>>> repr(x)
"'foo'"
Run Code Online (Sandbox Code Playgroud)
问题:
repr(x)?(当我这样做时,我不会得到它们str(x))'foo'在做的时候会得到eval("'foo'")而不是x那个对象?练习Python很难,练习10.2:
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
Run Code Online (Sandbox Code Playgroud)
2:使用'''(三重单引号)代替.你能明白为什么你可以用它而不是"""吗?
我不明白为什么我会用'''而不是""".它给了我相同的输出.有人可以解释一下为什么我会使用三重单引号而不是三重双引号?他们之间有什么区别?
我是一个初学者,从学习Python的艰难方式学习Python.这是我学习的第一门编程语言,我被困在练习中.
练习:"解释为什么使用4.0而不仅仅是4".
cars = 100
space_in_a_car = 4.0 #Why does he uses 4.0 instead of 4 here?
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
print "There are", cars, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to …Run Code Online (Sandbox Code Playgroud) 我搜索了这个网站,但似乎这个练习来自于学习Python的难度,以前的问题没有涵盖.
我有这个练习:
1)找到字符串放在字符串中的所有位置.有四个地方.2)你确定只有四个地方吗?你怎么知道的?也许我喜欢说谎.
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not) #two strings inside a string, count is 2
print x
print y
print "I said: %r." % x #here, count is 3
print "I also said: '%s'." % y #here, count is 4
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious …Run Code Online (Sandbox Code Playgroud) 我有来自Learn Python The Hard Way的代码,我需要对它进行评论:
from sys import argv
Run Code Online (Sandbox Code Playgroud)
所以我做了:#imports来自sys模块的argv
我知道argv是做什么的,但是我无法搞清楚调用sys模块的这个元素是什么意思?一个变量,一个方法,一个函数?参数变量?
另外,考虑到这行代码,是否意味着所有sys模块都是在我的程序中导入的,或者只是argv?
我知道在使用时argv我必须输入文件作为参数(例如:)python ex15.py ex15_sample.txt,使用时raw_input我输入文件名作为输入.
但我似乎无法找出为什么获取文件名的方法会比另一种更好.有人可以解释原因吗?
python文档说:返回对象的长度(项目数).参数可以是序列(字符串,元组或列表)或映射(字典).
码:
from sys import argv
script, from_file = argv
input = open(from_file)
indata = input.read()
print "The input file is %d bytes long" % len(indata)
Run Code Online (Sandbox Code Playgroud)
文件内容: 一二三
运行这个简单的程序后,我得到输出:输入文件长14个字节
Qutestion:
我不明白,如果我的文件中只写了11个字符(一个两个三),len怎么可以返回14个字节,而不仅仅是11个?(顺便说一句,字节是什么?)在python解释器中如果我键入s ="一二三"然后我得到13,所以我很困惑.
行使:
这个文件中的重复次数太多了.使用字符串,格式和转义只用一个target.write()命令而不是6来打印line1,line2和line3.
书中的代码:
from sys import argv
script, filename = argv
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename, 'w')
print "Truncating the file. Goodbye!"
target.truncate()
print "Now I'm going to ask you for three lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "I'm going to …Run Code Online (Sandbox Code Playgroud) Ex21,额外学分3:一旦你完成拼图的公式,进入那里,看看修改功能的各个部分会发生什么.尝试有意改变它以产生另一个价值.
我的代码:
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
print "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight …Run Code Online (Sandbox Code Playgroud)