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?

from sys import argv

script, filename = argv
txt = open(filename)
print(txt.read()) ah = input(">")
print(ah.read())

  

6. how to edit a file?

from sys import argv
script, filename = argv
target = open(filename, 'w') # open for writing, truncating the file first
target.truncate()
line1 = input()
line2 = input()
target.write(line1)
target.write("\n")
target.write(line2)
target.close()

  

6. copy a file

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

  

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

  

7. functions in python

def print_two(*args):
a, b = args
print("haha %r, and %r" % (a, b)) def haha():
print("mdzz") print_two('a', 'b')
haha()

  

8. functions and files

from sys import argv

script, input_file = argv

def print_all(f):
print f.read() def rewind(f):
f.seek(0) #seek function is file's function. It can represent the current position at the offset.
#default is 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)

  

9. some function

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

  

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

  

10. if statement

#if-statement
if ... :
#...
elif ... :
#...
else :
#...

  

11. for and list

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

  

12. while loop

a = input()
while int(a)>10:
print(a)
a = input()

  

13. with-as

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

  

14. assert-statement

#assert statement
#syntax:
# assert expression , [Arguments]
#If expression fails, python uses arguments expression.
def a(b):
assert b>1, print("wrong!")
b = input('>')
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. HAWQ + MADlib 玩转数据挖掘之(四)——低秩矩阵分解实现推荐算法

    一.潜在因子(Latent Factor)推荐算法 本算法整理自知乎上的回答@nick lee.应用领域:"网易云音乐歌单个性化推荐"."豆瓣电台音乐推荐"等. ...

  2. RxJava 1.x 笔记:过滤型操作符

    我真的是奇怪,上下班的路上看书.看文章学习的劲头特别大,到了周末有大把的学习时间,反而不珍惜,总想打游戏,睡前才踏踏实实地写了篇文章,真是服了自己! 本文内容为 RxJava 官方文档 学习笔记 作者 ...

  3. XMPP协议相关知识

    XMPP协议的组成 主要的XMPP 协议范本及当今应用很广的XMPP 扩展: RFC 3920 XMPP:核心.定义了XMPP 协议框架下应用的网络架构,引入了XML Stream(XML 流)与XM ...

  4. JAVA多线程------用1

    火车上车厢的卫生间,为了简单,这里只模拟一个卫生间,这个卫生间会被多个人同时使用,在实际使用时,当一个人进入卫生间时则会把卫生间锁上,等出来时 打开门,下一个人进去把门锁上,如果有一个人在卫生间内部则 ...

  5. php实现彩票走势图组选图用颜色区分

    找了好久都没有关于这个的东西,我也是一步一步从百度知道上问出思路来的 $xxx = $row['bai'] ; $yyy = $row['shi'] ; $zzz = $row['ge'] ; $zu ...

  6. BZOJ5125: [Lydsy1712月赛]小Q的书架(DP决策单调性)

    题意:N个数,按顺序划分为K组,使得逆序对之和最小. 思路:之前能用四边形不等式写的,一般网上都还有DP单调性分治的做法,今天也尝试用后者写(抄)了一遍.即: 分成K组,我们进行K-1次分治,get( ...

  7. HDU1576 A/B

    暴力出奇迹,我就知道没取余那么正当,肯定有什么奇淫怪巧,果然5分钟A掉. #include<cstdio> #include<cstdlib> #include<iost ...

  8. BestCoder Round #1 第一题 逃生

    // 等了好久,BESTCODER 终于出来了..像咋这样的毕业的人..就是去凑凑热闹// 弱校搞acm真是难,不过还是怪自己不够努力// 第一题是明显的拓扑排序,加了了个字典序限制而已// 用优先队 ...

  9. python笔记-19 javascript补充、web框架、django基础

    一.JavaScript的补充 1 正则表达式 1.1 test的使用 test 测试是否符合条件 返回true or false 1.2 exec的使用 exec 从字符串中截取匹配的字符 1.3 ...

  10. 基于C#的UDP协议的同步实现

    一.摘要 总结基于C#的UDP协议的同步通信. 二.实验平台 Visual Studio 2010 三.实验原理 UDP传输协议同TCP传输协议的区别可查阅相关文档,此处不再赘述. 四.实例  4.1 ...