欧小麦 发表于 2018-8-6 23:27:30

笨办法学Python(python3.6)

练习1.第一个程序
# -*- coding: utf-8 -*-

print("Hello World!")
print("hello again")
print("I like typing this.")
print("this is fun.")
print('yay!printing.')
print("I'd much rather you 'not'.")
print('I"said"do not touch this.')


#意思为注释


练习2.注释和井号#
#A comment,this is so you can read your program later
#Anything after the# is ignored by the 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')

欧小麦 发表于 2018-8-6 23:42:12

练习3.数字和数学计算
print("i will now count my chickens:")

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 that 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 greater or equal?",5>=-2)
print("is it less or equal?",5<=-2)

欧小麦 发表于 2018-8-7 00:00:40

练习4.变量和命名
#定义变量并赋值
cars = 100
space_in_a_car = 4.0
drivers =30
passengers = 90

#定义变量并赋予计算结果
cars_not_driven = cars - drivers
cars_driven = driver
carpool_capacity = cars_driven = passengers/cars_driven

#输出
print("there are",cars,"cars availabel")
print("there are only",drivers,"driver avaiable")


print("there will be ",cars_not_driven,empty cars today.")
print("we can transpor",carpool_capacity,"people today")
print("we have",passengers,to carpool today.)
print("we need to put about",average_passengerper_car)

欧小麦 发表于 2018-8-10 20:29:28

练习5.更多的变量和打印
my_name = 'Zed A Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 #lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print("Let's talk about %s."%my_name)
print("He's %d inches tall."%my_height)
print("He's %d pounds 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 deepending 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))

欧小麦 发表于 2018-8-10 21:00:35

练习6.字符串和文本
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)

欧小麦 发表于 2018-8-10 21:18:09

欧小麦 发表于 2018-8-10 21:00
练习6.字符串和文本
x = "There are %d types of people."%10



%r 和%s的不同:%r显示变量“原始”的数值。
text="i am %d years old."%22
print ("I said: %s" % text )
>>>I said: i am 22 years old.
print ("I said: %r" % text )
>>>I said: 'i am 22 years old.'

欧小麦 发表于 2018-8-10 21:19:08

练习7.更多的打印(输出)
print("Mary had a little lamb.")
print("It's fleece was white as %s."%'snow')
print("And everywhere that Mary went.")
print("."*10) #重复10遍

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

print(end1+end2+end3+end4+end5+end6)
print(end7+end8+end9+end10+end11+end12)

欧小麦 发表于 2018-8-10 21:30:11

练习8.打印打印
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 didni't sing.",
               "so I said goodnight."))

欧小麦 发表于 2018-8-10 22:06:15

练习10.那是什么?
while True:
for i in ["/","-","|","\\","|"]:   #i迭代取值
    print("%s\r"%i)#%s格式化字符串 \r回车\\转义输出\
# / - | \ | 循环


欧小麦 发表于 2018-8-10 22:23:57

练习11.提问
print("How old are you?")
age = input()
print("How tall are you?")
height = input()
print("How much do you weigh?")
weight = input()

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

欧小麦 发表于 2018-8-13 10:03:38

练习12.提示别人
age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weight?")

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

欧小麦 发表于 2018-8-13 10:36:13

练习13.参数,解包,变量
from sys import argv

script,first,second,third = argv

print("the script is called:",script)
print("your first vraiable is:",first)
print("your second variable is:",second)
print("your third variable:",third)


#cmd中运行,运行命令python 文件名 参数1 参数2 参数3,如python ex1.py you and me

欧小麦 发表于 2018-8-13 10:50:00

练习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 = input(prompt)

print("where do you live %s"%user_name)
lives = input(prompt)

print("what kind of computer do you have?")
computer = 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))
#cmd中运行,如python ex1 小甲鱼

欧小麦 发表于 2018-8-13 11:02:12

本帖最后由 欧小麦 于 2018-8-13 11:07 编辑

练习15.读文件
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=input("->")#再次输入文件名

txt_again = open(file_again)#打开输入的文件名

print(txt_again.read())#打印第二次输入的文件

txt_again.close()#关闭文件

欧小麦 发表于 2018-8-14 20:16:53

练习16.读写文件
from sys import argv

script,filename = argv

print("we're going to erase %r."%filename)

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 = input("line 1:")#输入内容
line2 = input("line 2:")
line3 = 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("finally,we close it")
target.close()#关闭文件

#close关闭文件 read读取文件内容 readline读取文件中的一行 truncate清空文件 write写入文件

欧小麦 发表于 2018-8-14 20:49:23

练习17.更多文件操作
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))

in_file = open(from_file)#打开文件1
in_data = in_file.read()#读取文件1内容

print('the input file is %d bytes long'%len(in_data))#文件1大小

print('does the output file exist%r'%exists(to_file))#判断文件2是否存在
input()

out_file = open(to_file,'w')#写入模式打开文件2
out_file.write(in_data)#文件1内容写入文件2

print("ok,done")

out_file.close()#关闭文件
in_file.close()

欧小麦 发表于 2018-8-14 21:20:17

练习18.命名,变量,代码,函数
def print_two(*args):
    arg1,arg2=args
    print("arg1:%r,arg2%r"%(arg1,arg2))

def print_two_again(arg1,arg2):
    print("arg1:%r,arg2:%r"%(arg1,arg2))

def print_one(arg1):
    print("arg1:%r"%arg1)

def print_none():
    print("nothing")


print_two('a','b')
print_two_again('lilei','hanmeimei')
print_one('king')
print_none()

欧小麦 发表于 2018-8-14 21:34:28

练习19.函数和变量
def cheese_and_crackers(cheese_count,boxes_of_crackers):
    print("you have %d cheeses and %d boxes of cracker"%(cheese_count,boxes_of_crackers))
    print("that's enough for a party!")

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 = int(input('cheese?'))
amount_of_crackers = int(input('crackers?'))
cheese_and_crackers(amount_of_cheese,amount_of_crackers)

print('we can even do math inside too:')
cheese_and_crackers(9+2,11*2)

print('and we can combine the two,variables and math:')
cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)

欧小麦 发表于 2018-8-14 21:55:16

练习20.函数和文件
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)

欧小麦 发表于 2018-8-14 22:33:29

练习24.更多的练习
print("let's practive everything.")
print('you\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')

poem="""
\tThe lovely world
with logic so firmly planted
cannot discern \n the need of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""

print("-----------")
print(poem)#打印上面的诗
print("-----------")

five = 10 -2 +3 -6
print('this should be five%s'%five)

def secret_formula(started):#定义函数
    jelly_beans = started*500
    jars = jelly_beans / 1000
    crates = jars /100
    return jelly_beans,jars,crates

start_point=10000
beans,jars,crates=secret_formula(start_point)#执行函数,结果赋值

print("with a starting point of:%d"%start_point)
print("we'd have %d beans,%d jars, and %d creates."%(beans,jars,crates))

start_point=start_point/10

print("we can slso do that this way:")
print("we'd have %d beans,%d jars, and %d creates."%secret_formula(start_point))      
页: [1] 2
查看完整版本: 笨办法学Python(python3.6)