笨办法学python集

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?")
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,535评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,106评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,668评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,863评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,874评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,362评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,748评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,717评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,249评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,280评论 3 345
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,408评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,020评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,727评论 3 337
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,191评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,320评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,946评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,473评论 2 365

推荐阅读更多精彩内容