习题11:提问

  --  接受键盘的输入  raw_input

  input() 和 raw_input() 有何不同?
  input() 函数会把你输入的东西当做 Python 代码进行处理,这么做会有安全问题,你应该避开
这个函数。

代码:

print "how old are you?",

age = raw_input()
print "How tail are you?",
height = raw_input()
print "how much do you weight?",
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (
  age, height, weight)

结果:

习题12:提示别人

代码:

age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weight? ")

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)

结果:

习题13:参数、解包、变量

  --   将变量传递给脚本的方法(所谓脚本,就是你写的 .py 程序),涉及“参数”

  --   import 将python已有的功能引入脚本直接使用

  --   argv:参数变量

  --   解包:script, first, second, third = argv    把argv中的东西解包,将所有的参数一次赋予左边的变量名

  --   argv 和 raw_input() 有什么不同?

    不同点在于用户输入的时机。如果参数是在用户执行命令时就要输入,那就是 argv,如果是在脚本运行过程中需要用户输入,那就使用 raw_input()。

代码: 

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

结果:

习题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)

结果:

习题15:读取文件

  --   涉及两个文件,一个脚本文件.py文件,另一个是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.

--   open()  打开文件   返回的是一个“file object”

--   read()  读取文件内容

代码:

from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filenmae again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

结果:

习题16:读写文件

  --   close  关闭文件

  --   read  读取文件内容

  --   readline  读取文本文件中的一行

  --   write(stuff)   将stuff写入文件

代码:

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 write these 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:更多文件操作

  --   exists()  判断文件是否存在,存在,继续执行;不存在,新建文件再继续执行

代码:

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

#we could do these two on one line too, how?
in_file = open(from_file)
indata = in_file.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file wxist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()

结果:

习题18:命名、变量、代码、函数

  函数可做三洋事情:

   1、给代码片段命名,就跟“变量”给字符串和数字命名一样

   2、可以接受参数,就跟脚本接受argv一样

   3、通过使用#1和#2,可以创建“微型脚本”或者“小命令”

  介绍:

    def:定义

    函数名:可以随意取,最好能体现出函数的功能来,注意命名规范!不可重复

    参数:放在圆括号内

    格式:def  函数名称  (参数-多个参数以“,”隔开):

         函数内容  开始编写前缩进4个空格

    函数可以接收参数,也可以不接收参数

    运行/调用函数,函数名后紧跟(参数-可有可无,可多个)

代码:

# this one is like your scripts with argv
def print_two(*args):
  arg1, arg2 = args
  print "arg1: %r, arg2: %r" % (arg1, arg2)

# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
  print "arg1: %r, arg2: %r" % (arg1, arg2)

# this just takes one argument
def print_one(arg1):
  print "arg1: %r" % arg1

# this one takes no arguments
def print_none():
  print "I got nothin'."

print_two("Zed", "Shaw")
print_two_again("Zed", "Shaw")
print_one("First!")
print_none()

结果:

习题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
  print "Man that's enough for a party!"
  print "Get a blanket.\n"

print "We can just give the function numbers directry:"
cheese_and_crackers(20, 30)

print "OR, We can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50

cheese_and_crackers(amount_of_cheese, amount_of_crackers)

print "We can even do much inside too:"
cheese_and_crackers(10 + 20, 5 + 6)

print "And we can combine the two, variables and much:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

结果:

习题20:函数和文件

  --   更多文件操作  seek()   readline()

代码:

from sys import argv

script, input_file = argv

def print_all(f):
  print f.read()

def rewind(f):
  f.seek(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)

结果:

python入门 -- 学习笔记2的更多相关文章

  1. Python入门学习笔记4:他人的博客及他人的学习思路

    看其他人的学习笔记,可以保证自己不走弯路.并且一举两得,即学知识又学方法! 廖雪峰:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958 ...

  2. Python入门学习笔记

    了解 一下Python中的基本语法,发现挺不适应的,例如变量经常想去指定类型或者if加个括号之类的.这是在MOOC中学习到的知识中一点简单的笔记. Python的变量和数据类型: 1.Python这种 ...

  3. python入门学习笔记(三)

    10.函数 求绝对值的函数 abs(x) 也可以在交互式命令行通过 help(abs) 查看abs函数的帮助信息.调用 abs 函数:>>> abs(100)100>>& ...

  4. Python入门 学习笔记 (二)

      今天学习了一些简单的语法规则,话不多说,开始了:      二.数据类型                常用数据类型中的整形和浮点型就不多说了.                1.字符串     ...

  5. python入门 -- 学习笔记1

    学习资料:笨方法学Python 准备: 安装环境----请自行网络搜索(Windows安装很简单,和其他安装程序一样) 找一个自己习惯的编辑器(比如:sublime text 3) 创建一个专门的目录 ...

  6. Python入门 学习笔记 (一)

    原文地址:http://www.cnblogs.com/lujianwenance/p/5939786.html 说到学习一门语言第一步就是先选定使用的工具(环境).因为本人是个小白,所以征求了一下同 ...

  7. python入门学习笔记(二)

    6.6替换元素 7.tuple类型 7.1创建tuple 7.2创建单元素tuple    7.3"可变"的tuple 8.条件判断和循环 8.1,if语句   8.2,if... ...

  8. python入门学习笔记(一)

    写在开头:         A:python的交互式环境                                                                         ...

  9. python入门 -- 学习笔记4

    习题38:列表的操作 当你看到像 mystuff.append('hello') 这样的代码时,你事实上已经在 Python 内部激发了一个连锁反应.以下是它的工作原理: 1. Python 看到你用 ...

  10. python入门 -- 学习笔记3

    习题21:函数可以返回东西 过程解析: 1.定义函数:如def add(形参)函数 2.调用函数: add(实参)    别忘记加() 3.在函数调用的时候将实参的值传给形参,代入到函数中进行计算,r ...

随机推荐

  1. 深入理解Java中的synchronized锁重入

    问题导入:如果一个线程调用了一个对象的同步方法,那么他还能不能在调用这个对象的另外一个同步方法呢? 这里就是synchronized锁重入问题. 一.synchronized锁重入 来看下面的代码: ...

  2. less的入门教程

    CSS的短板 作为前端学习者的我们 或多或少都要学些 CSS ,它作为前端开发的三大基石之一,时刻引领着 Web 的发展潮向. 而 CSS 作为一门标记性语言,可能 给初学者第一印象 就是简单易懂,毫 ...

  3. 百战程序员-xml

    1.用自己的语言说出,为什么需要XML? XML 是一种元标注语言,该语言提供一种描述结构数据的格式.这有助于更精确地声明内容,方便跨越多种平台的更有意义的搜索结果.此外,XML 将起用新一代的基于 ...

  4. (拼接SQL语句)mysql中date类型,datetime类型

    : , . _ - /  %  &  # @ ! * | [ ] { }   ;  + = update ky set date = '18,9-2'  where id  = 1  // 2 ...

  5. css 易错点总结

    心得:思路清晰,细致,耐心. 慢慢来,先规划,再动手,先整体后局部,规划好整个页面先. 命名合理,且小心重复 防止标签嵌套错误,以及忘记闭合 行高要在字体后面,如下: 正确:font:400 15px ...

  6. 我发起并创立了一个 C 语言编译器 开源项目 InnerC

    本文是 VMBC / D#  项目 的 系列文章, 有关 VMBC / D# ,  见 <我发起并创立了一个 VMBC 的 子项目 D#>(以下简称 <D#>)  https: ...

  7. [转]PostgreSQL命令行使用手册

    启动pgsl数据库 1 pg_ctl -D /xx/pgdata start 查看pgsl版本 1 pg_ctl --version 命令行登录数据库 1 psql -U username -d db ...

  8. ASP.NET 4.0 :MasterPage母版页的ClientIDMode属性

    在ASP.NET 4.0之前我们总是要为控件的ClientID头疼,比如明明一个叫lblName的Label放在一个叫做grd的GridView里面后,在页面上改Label的ID就变成了诸如grd_c ...

  9. Eclipse配置Python的IDE

    我第一个用来实际应用的编程语言是Java,于是对Eclipse情有独钟.但是自从上手了Notepad++后,使用Eclipse的机会越来越少. 最近开始学习Python,因为对Python不太熟悉,有 ...

  10. Linux中redis安装配置及使用详解

    Linux中redis安装配置及使用详解 一. Redis基本知识 1.Redis 的数据类型 字符串 , 列表 (lists) , 集合 (sets) , 有序集合 (sorts sets) , 哈 ...