笨方法学习Python(11-20)

以下学习内容以python2为基准

11、提问

print "How old are you?", 
age = raw_input()
print "So, you're %r old." % age
python ex11.py
How old are you? 35
So, you're '35' old

input()与raw_input()都是Python的内建函数,实现与用户的交互,但是功能不同。

raw_input可代表任意字符串

input在字符串上要加‘ ’

int类型最好使用input

12、提示别人

对于 raw_input 而言,你还可以让它显示出一个提示,从而告诉别人应该输入什么东西。你可以在 () 之间放入一个你想要作为提示的字符串,如下所示:

y = raw_input("Name? ")

pydoc是python内置的官方文档,类似于linux中的man

13、参数、解包、变量

from sys import argv 
script, first, second, third = argv 
print "The script is called:", script 
print "Your first variable is:", first 
print "Your second variable is:", second 
print "Your third variable is:", third
python ex13.py first 2nd 3rd
The script is called: ex13.py 
Your first variable is: first 
Your second variable is: 2nd 
Your third variable is: 3rd

14、提示和传递

from sys import argv
script, user_name = argv
prompt = '>'
print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)
print "Where do you live %s?" % user_name 
lives = raw_input(prompt) 
print "What kind of computer do you have?" 
computer = raw_input(prompt) 
print """ 
Alright, so you said %r about liking me. 
You live in %r. Not sure where that is. 
And you have a %r computer. Nice.
 """ % (likes, lives, computer)

$ python ex14.py Zed 
Hi Zed, I'm the ex14.py script. 
I'd like to ask you a few questions. 
Do you like me Zed? 
> yes Where do you live Zed? 
> America What kind of computer do you have? 
> Tandy 
Alright, so you said 'yes' about liking me. 
You live in 'America'. Not sure where that is. 
And you have a 'Tandy' computer. Nice.

15、读取文件

$ vi ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and  lots of fun to have in here.

我们要做的是打开 ex15_sample.txt

from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "Type the filename agein:"
file_again = raw_input(">")
txt_again = open(file_again)
print txt_again.read()

16、读写文件

from sys import argv
script, filename = argv
print "We're going to erase %r." %filename
print "If you don't want that, hit CRIL-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 write whese to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print "And finally, we close it."
target.close()

17、更多文件操作

from os.path import exists

print "%r" % exists(to_file)    #查看to_file有有没用存在,会返回布尔值

print "d%" % len(indata)    #查看有多少个字节

# 朝to_file里写入数据,写入indata

output = open(to_file, 'w')

output.write(indata)

output.close()    #关闭

18、命令、变量、代码、函数

#coding:utf-8
def nan_nad_nv(nan_count,nv_count):
        print "我们IT男生有%d。" % nan_count
        print "我们IT女生有%d。" % nv_count
print "IT人员数目为:"

>>> nan_nad_nv(60,20)
结果
IT人员数目为:
我们IT男生有60。
我们IT女生有20。

19、函数和变量

def cheese_and_crackers(cheese_count, boxes_of_crackers): 
    print "You have %d cheeses!" % cheese_count 
    print "You have %d boxes of crackers!" % boxes_of_crackers
cheese_and_crackers(20, 30)
结果
You have 20 cheeses!
You have 30 boxes of crackers!
amount_of_cheese = 10 
amount_of_crackers = 50 
cheese_and_crackers(amount_of_cheese, amount_of_crackers)
结果
You have 10 cheeses!
You have 50 boxes of crackers!

20、函数和文件

掌握以下参数意义

f.seek(0)

f.readline()

原文地址:http://blog.51cto.com/8672771/2073551

时间: 2024-08-30 10:50:50

笨方法学习Python(11-20)的相关文章

笨方法学习Python(1-10)

以下学习内容以python2为基准 UTF-8 #conding:utf-8    or    #__coding:utf-8__ 此句要置顶,表示代码支持UTF8的格式,最好每个代码文件都加上 注释 # A comment, this is so you can read your program later. 代码前加"#"表示的是注释,以后写每行代码的上一行记得都加上解释信息 python2与python3 print"abc"    #python2的写法

LPTHW 笨方法学习python 16章

根据16章的内容作了一些扩展. 比如,判断文件如果存在,就在文件后追加,如不存在则创建. 同时借鉴了shell命令中类似 cat <<EOF > test的方法,提示用户输入一个结尾符. 现在有一个小坑,怎么使用python去读取一个文件的行数,原来有os.system("wc -l filename")倒是可以,但是windows下如何操作呢?回头补填. #!/usr/bin/env python # -*- coding:utf-8 -*- from sys im

笨方法学习Python31-40

31.作出决定 复习了raw_input("> ") if 变量 == "Vaule": 执行语句 32.循环和列表 count = [1, 2, 3, 4, 5] for i in count: print "This is %d" % i            #循环打印出count的值 count.append(6)     #从最后增加值 count.insert(1,44)    #从第二位增加值44 range(20)     

笨办法学习python之模块、类、对象

模块就像字典 字典是python中唯一映射关系,它用一个事物对应另外一个事物,也就是所谓的key->value. 模块包含一些变量和函数,可以导入,并且可以用点(·)来操作访问变量和函数. 记住'从X获取Y的概念'. 他们相似,只是语法不同. 1.也就是说在python中有一条通用的模式: (1)有一个key =value的容器 (2)通过key从容器中获取数据 不同点: 在字典中key是字符串,写法为[key]:在模块中写法为.key,其余的地方一模一样. 类就像模块 可以认为模块就是pyth

笨方法学习Python21-30

21.函数可以返回东西 def add(a, b): print "ADDING %d + %d" % (a, b) return a + b age = add(30, 5) print "Age: %d" % age [[email protected] 21-40]# python t21.py ADDING 30 + 5 Age: 35 将函数配上参数值赋值给变量age 22.到现在你学到了哪些东西 =            #赋值 print       

python基础教程_学习笔记11:魔法方法、属性和迭代器

魔法方法.属性和迭代器 在python中,有的名称会在前面和后面各加上两个下划线,这种写法很特别.它表示名字有特殊含义,所以绝不要在自己的程序中使用这种名字.在python中,由这些名字组成的集合所包含的方法叫做魔法(或称特殊)方法.如果对象实现了这些方法中的某一个,那么这个方法会在特殊的情况下被python调用,而几乎没有直接调用它们的必要. 准备工作 为了确保类是新型的,应该把赋值语句__metaclass__=type放在你的模块的最开始,或者(直接或间接)子类化内建类(实际上是类型)ob

学习Python编程的11个资源

转自 http://blog.jobbole.com/71064/  用于以后学习备份 用 Python 写代码并不难,事实上,它一直以来都是被声称为最容易学习的编程语言.如果你正打算学习 web 开发,Python 是一个不错的选择,甚至你想学游戏开发也可 以从 Python 开始,因为用 Python 来构建游戏的资源实在是太多了.这是一种快速 学习语言的一种方法. 许多程序员使用 Python 作为初学语言,然后接着是像 PHP 和 Ruby 这样的语言.它也是2014最热门的 web 开

学习Python编程的11个精品资源

本文由 伯乐在线 - atupal 翻译自 Alex Ivanovs.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. 用 Python 写代码并不难,事实上,它一直以来都是被声称为最容易学习的编程语言.如果你正打算学习 web 开发,Python 是一个不错的选择,甚至你想学游戏开发也可 以从 Python 开始,因为用 Python 来构建游戏的资源实在是太多了.这是一种快速 学习语言的一种方法. 许多程序员使用 Python 作为初学语言,然后接着是像 PHP 和 Ruby 这样的语言.

python 学习笔记 11 -- 使用参数使你的程序变得更性感

当然,在之前的系列中,我已介绍如何给 Python 脚本传参,当然,今天不会继续介绍这么无聊的东东.首先使用 python 的sys.argv 传参的话,就固定了参数的个数.顺序以及格式,这么死的规定如何性感? I have a dream , to make my code much sexer ! 今天我们简单介绍一下如何更加随性的给 python 脚本传参.效果如下: [email protected]:/tmp$ python arg.py -h NAME: project with u