1. How to run the python file?

python ...py

2. UTF-8 is a character encoding, just like ASCII.

3. round(floating-point number)

4. %r print the way you write. It prints whatever you write, even if you have special characters, such as '\n'.

5. how to open a file?

  1. from sys import argv
  2.  
  3. script, filename = argv
  4. txt = open(filename)
  5. print(txt.read())
  6.  
  7. ah = input(">")
  8. print(ah.read())

  

6. how to edit a file?

  1. from sys import argv
  2. script, filename = argv
  3. target = open(filename, 'w') # open for writing, truncating the file first
  4. target.truncate()
  5. line1 = input()
  6. line2 = input()
  7. target.write(line1)
  8. target.write("\n")
  9. target.write(line2)
  10. target.close()

  

6. copy a file

  1. from sys import argv
  2. from os.path import exists # os.path is a module of python and we need to use its exists function
  3.  
  4. script, from_file, to_file = argv
  5. print("Copying from %s to %s" % (from_file, to_file))
  6. in_file = open(from_file);
  7. indata = in_file.read() #write in one line: indata = open(from_file).read()
  8. print("The input file is %d bytes long" % len(indata)) #return the number of bytes of indata
  9. print("Does the output file exist? %r" % exists(to_file))
  10.  
  11. out_file = open(to_file, 'w') #we need ot write the new file
  12. out_file.write(indata) #directly use indata file to write out_file
  13. out_file.close()
  14. in_file.close()

  

  1. #much more easier one
  2. open(argv[2], 'w').write(open(argv[1]).read())

  

7. functions in python

  1. def print_two(*args):
  2. a, b = args
  3. print("haha %r, and %r" % (a, b))
  4.  
  5. def haha():
  6. print("mdzz")
  7.  
  8. print_two('a', 'b')
  9. haha()

  

8. functions and files

  1. from sys import argv
  2.  
  3. script, input_file = argv
  4.  
  5. def print_all(f):
  6. print f.read()
  7.  
  8. def rewind(f):
  9. f.seek(0) #seek function is file's function. It can represent the current position at the offset.
  10. #default is 0.
  11.  
  12. def print_a_line(line_count, f):
  13. print line_count, f.readline()
  14.  
  15. current_file = open(input_file)
  16.  
  17. print "First let's print the whole file:\n"
  18.  
  19. print_all(current_file)
  20.  
  21. print "Now let's rewind, kind of like a tape."
  22.  
  23. rewind(current_file)
  24.  
  25. print "Let's print three lines:"
  26.  
  27. current_line = 1
  28. print_a_line(current_line, current_file)
  29.  
  30. current_line = current_line + 1
  31. print_a_line(current_line, current_file)
  32.  
  33. current_line = current_line + 1
  34. print_a_line(current_line, current_file)

  

9. some function

  1. #split(str, num) function
  2. #return a list of all the words in the string
  3. #use str as a sperate
  4. #num represent the number of lines
  5. w="123 456 789"
  6. w.split() # result is ['123', '456', '789'], use the default mode
  7. w.split(' ', 1) #result is ['123', '456 789']
  8.  
  9. #sorted() function
  10. #It can be used to sort a string
  11. w='acb'
  12. w.sorted() #result is ['a', 'b', 'c']
  13.  
  14. #pop(i) function
  15. #remove the item at the given postion in the list and return it
  16. #if () is empty, we wiil remove and return the last item of the list
  17. w=[1,2,3]
  18. w.pop() #w=[1,2]
  19. w.pop(0) #w=[2]

  

  1. #If we are confused about a module,
  2. #we can use help(module) to learn about it.
  3. help(module)

  

10. if statement

  1. #if-statement
  2. if ... :
  3. #...
  4. elif ... :
  5. #...
  6. else :
  7. #...

  

11. for and list

  1. a = [1, 2, 3] #list
  2. b = [] #a mix list
  3. for i in range(0, 5):
  4. print("%d" % i)
  5. b.append(i)
  6. #range(a, b) is a, a+1, ... , b-1
  7. #list.append(obj) add the obj to a list

  

12. while loop

  1. a = input()
  2. while int(a)>10:
  3. print(a)
  4. a = input()

  

13. with-as

  1. #with-as statement is a control-flow structure.
  2. #basic structure is
  3. #with expression [as variable]:
  4. # with-block
  5. #It can used to wrap the excution of a block with method defined by a context manager.
  6. #expression is represented a class. In the class, we must have two functions.
  7. #One is __enter__(), the others is __exit__().
  8. #The variable is equal to the return of __enter__(). If we do not have [as variable], it will return nothing.
  9. #Then we will excute with-block. At the end, we will excute __exit__().
  10. #__exit__函数的返回值用来指示with-block部分发生的异常是否要re-raise,如果返回False,则会re-raise with-block的异常,如果返回True,则就像什么都没发生。
  11. import sys
  12. class test:
  13. def __enter__(self): #need one argument
  14. print("enter")
  15. return self
  16. def __exit__(self, type, value, trace): #need 4 arguments
  17. print(type, value, trace)
  18. return True
  19. def do(self):
  20. a=1/0
  21. return a
  22. with test() as t:
  23. t.do()
  24. #result
  25. #enter
  26. #<class 'ZeroDivisionError'> division by zero <traceback object at 0x1029a5188>
  27. #It's mostly used to handle the exception.
  28. #a esier simple
  29. with open(filename, 'w') as f:
  30. f.read()
  31. #We do not need to close the file. It can be closed itself.

  

14. assert-statement

  1. #assert statement
  2. #syntax:
  3. # assert expression , [Arguments]
  4. #If expression fails, python uses arguments expression.
  5. def a(b):
  6. assert b>1, print("wrong!")
  7. b = input('>')
  8. a(b)

Python basic (from learn python the hard the way)的更多相关文章

  1. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...

  2. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  3. 《Learn python the hard way》Exercise 48: Advanced User Input

    这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...

  4. 学 Python (Learn Python The Hard Way)

    学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...

  5. Python之路,Day1 - Python基础1

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  6. Python之路,Day1 - Python基础1(转载Alex)

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  7. Python之路,Day1 - Python基础1 --转自金角大王

    本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...

  8. python学习笔记(python简史)

    一.python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum) 目前python主要应用领域: ·云计算 ·WEB开发 ·科学运算.人工智能 ·系统运维 ·金融:量化交 ...

  9. Python第一天——初识Python

    python是由荷兰人Guido van Rossum 于1989年发明的一种面向对象的的解释型计算机程序设语言,也可以称之为编程语言.例如java.php.c语言等都是编程语言. 那么为什么会有编程 ...

随机推荐

  1. Eclipse下搭建SWT与Swing图形界面开发环境

    一.SWT与Swing介绍 SWT(StandardWidget Toolkit)则是由Eclipse项目组织开发的一套完整的图形界面开发包,虽然当初仅仅是IBM为了编写Eclipse的IDE环境才编 ...

  2. 如何从github下载项目的源代码,包含git客户端,直接下载,vs下载

    有好多小伙伴可能刚刚接触github,还不知道如果和github下载项目,此处写个博客统一的声明.从多种方式下载源代码,加深对git的理解. 首先先解释下git的含义,git是一个源代码的管理工具,通 ...

  3. windows cmd 命令行 —— 进程与服务

    1. 进程查看与操作 tasklist tskill pid 2. 服务查看与操作 net start net stop

  4. 高德地图 Android编程中 如何设置使 标记 marker 能够被拖拽

    由于本人对智能手机真心的不太会用,我本人大概是不到3年前才买的智能手机,用以前的索尼爱立信手机比较方便小巧,平时学习工作打个电话发个短信也就够了,出去吃饭一般都是朋友拿手机去弄什么美团团购啥的,然后我 ...

  5. crm--01

    需求: 将课程名称与班级综合起来 class ClassListConfig(ModelSatrk): # 自定义显示方式 def display_class(self,obj=None,is_hea ...

  6. vue中axios的深入使用

    如上所示一条简单的请求数据,用到了vue中axios,promise,qs等等 这里我将vue中用到的axios进行了封装方便日后使用  先对工具类进行封装utils/axios.js: // 引入模 ...

  7. js之无缝轮播图

      HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  8. 安装伪分布式hbase 0.99.0

    查看是否启动成功,输入jps,看到有HMaster和HQuorumPeer,浏览器输入http://localhost:16030/master-status,能打开说明成功 hbase(main): ...

  9. Cocos2d-X 3.2环境的配置

    大三寒假时间特别长,终于准备坐下来好好去学一直想涉足的游戏开发.既然准备学,就要找个出色的.跨平台的引擎来实现自己的计划.最终我选定了Cocos2d-X. 在折腾了很久之后,我终于把Cocos2d-X ...

  10. ES6中let和const详解

    let和var一样也是用来定义变量,不同之处在于let是块级作用域,只在所定义的块级作用域中生效,一个花括号便是一个块级作用域 {var a="我是var定义的";let b=&q ...