python等笔记

所用python版本3.11

代码:

poem = '''/

Programming is fun

When the work is done

if you wanna make your work also fun:

         use Python!

'''

f = file('poem.txt', 'w') # open for 'w'riting

f.write(poem) # write text to file

f.close() # close the file

f = file('poem.txt')

# if no mode is specified, 'r'ead mode is assumed by default

while True:

     line = f.readline()

     if len(line) == 0: # Zero length indicates EOF

         break

     print (line,)

     # Notice comma to avoid automatic newline added by Python

f.close() # close the file

错误提示:

Traceback (most recent call last):

File "D:/Software/Python/006", line 8, in <module>

    f = file('poem.txt', 'w') # open for 'w'riting

NameError: name 'file' is not defined

问题分析: 在Python3中,file()被废弃,不过可以用open() 代替,即f = file('poem.txt', 'w')替换为f = open('poem.txt', 'w'), 再f = file('poem.txt')替换为f = open('poem.txt'),更改后运行正常。

注:此文章为蔚蓝光辉博客原创,本人个人网站:www.11eyes.com 转载请注明出处。

================================

>>>def powersum(power, *args):
...     '''Return the sum of each argument raised to specified power.'''
...     total = 0
...     for i in args:
...          total += pow(i, power)
...     return total
...
>>> powersum(2, 3, 4)
25
请问其中25这个数字怎么得来的我初学,讲解的详细点。

你有 help(pow) 命令可以看到POW是用来做幂运算的,你的POWERSUM(2,3,4)实现的就是:3的平方加上4的平方。

http://wenwen.soso.com/z/q189009642.htm

===================================

Linux命令与Solaris命令的比较 http://blog.chinaunix.net/u1/46451/showart_364538.html

查看进程状态
Linux:   top
Solaris: prstat
在Solaris下可以通过pkg-get -i top来下载安装top软件,prstat -L可以进一步显示每个线程的状态

猜你喜欢

转载自blog.csdn.net/hej027/article/details/5707223