ex01简单print语句
#-*- coding:utf-8-*-
print "hello world"
print "Hello again"
print "i like typing this"
print "this is fun"
print 'yap printing.'
print "i'd much rather you 'not’."
print 'I "said" do not touch this.'
ex02注释
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "i could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."
ex03运算符,浮点数
print "I will now count my chicken:"
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
print "Now i will count the eggs:"
print 3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6
print "Is it true 3 + 2 < 5 - 7?"
print 3 + 2 < 5 - 7
print "What is 3 + 2?", 3 + 2
print "What is 5 - 7", 5 - 7
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5 >= -2
print "Is it less or equal?", 5 <= -2
print "----------------------------------"
print "I will now count my chicken:"
print "Hens", 25.0 + 30.0 / 6.0
print "Roosters", 100.0 - 25.0 * 3.0 % 4.0
print "Now i will count the eggs:"
print 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0
print "Is it true 3 + 2 < 5 - 7?"
print 3.0 + 2.0 < 5.0 - 7.0
print "What is 3.0 + 2.0?", 3.0 + 2.0
print "What is 5.0 - 7.0", 5.0 - 7.0
print "Oh, that's why it's False."
print "How about some more."
print "Is it greater?", 5.0 >= -2.0
print "Is it less or equal?", 5.0 <= -2.0
ex04使用变量
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven
print "There are", cars, "cars available."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to put about", average_passengers_per_car, "in each car."
ex05格式化字符串
my_name = 'Gao'
my_age = 31 # not a lie
my_height = 174 # cm
my_weight = 80 # kg
my_eyes = 'Black'
my_teeth = 'White'
my_hair = 'Black'
print "Let's talk about %s." % my_name
print "He's %d cm tall." % my_height
print "He's %d kg heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (
my_age, my_height, my_weight, my_age + my_height + my_weight)
ex06格式化字符串,多行文本
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary, do_not)
print x
print y
print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e
ex07字符串连接
print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10 #what's that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11+ end12
ex08格式化字符串
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
ex09换行符,打印多行字符
# Here's some new stange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print "Here are the days: ", days
print "Here are the months: ", months
print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
ex10转义字符
# coding: utf-8
# 转义字符 escape sequence
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
转义字符
\ 反斜杠(、)
' 单引号(‘)
" 双引号(“)
\a ASCII 响铃符(BEL)
\b ASCII 退格符(BS)
\f ASCII 进纸符 (FF)
\n ASCII 换行符 (LF)
\N{name} Unicode [数据库]中的字符名,其中name是它的名字,仅适用于Unicode
\r ASCII 回车符 (CR)
\t ASCII 水平制表符
\uxxxx 值为16位十六进制值xxxx的字符(仅适用于Unicode)
\Uxxxxxxxx 值为32位十六进制值xxxxxxxx的字符(仅适用于Unicode)
\v ASCII 垂直制表符 (VT)
\ooo 值为八进制值ooo的字符
\xhh 值为十六进制数hh的字符
#打印出风车转得效果
while True:
for i in ["/","-","|","\\","|"]:
print "%s\r" % i,
ex11输入
print "How old are you?"
age = raw_input()
print "How tall are you?"
height = raw_input()
print "How much do you weigh?"
weight = raw_input()
print "So, you're %r old, %r tall and %r heavy." %(
age,height,weight)
print "So, you're %s old, %s tall and %s heavy." %(
age,height,weight)
ex12提示输入
age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")
print "So, you're %r old, %r tall and %r heavy." %(
age, height, weight)
print "So, you're %s old, %s tall and %s heavy." %(
age, height, weight)
ex13参数传递
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
ex14argv参数传值
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)
ex15打开文件
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
txt.close()
print "Type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
txt_again.close()
ex16读写文件
# coding:utf-8
#方法1
'''''
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. Goodby!"
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()
'''
#方法2
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. Goodby!"
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 + "\n" + line2 + "\n" + line3 + "\n")
print "And finally, we close it."
target.close()
ex17文件复制
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 exist? %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()
ex18命名变量代码函数
# 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 nothing."
print_two("Z","W")
print_two_again("Z","W")
print_one("First!")
print_none()
ex19函数和变量
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 directly:"
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 math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)
print "And we can combine the two, variables and math:"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
ex21 函数返回值
def add(a,b):
print("ADDING %d + %d" %(a,b))
return a + b
def subtract(a,b):
print("SUBTRACTING %d - %d" % (a,b))
return a - b
def multipyl(a,b):
print("MULTIPLYTING %d * %d" % (a,b))
return a * b
def divide(a,b):
print("DIVIDING %d / %d" % (a,b))
return a / b
print("Let's do some math with just functions!")
age = add(30,5)
height = subtract(78,4)
weight = multipyl(90,2)
iq = divide(100,2)
print("Age:%d,Height:%d,Wight:%d,IQ:%d" %(age,height,weight,iq))
#A puzzle for the extra credit,type it in anyway.
print("Here is a puzzle.")
what = add(age,subtract(height,multipyl(weight,divide(iq,2))))
print("That becomes:",what,"Can you do it by hand?")
网友评论