笨办法学python 粗略笔记(learn python the hard way)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36458870/article/details/79076653

笨办法学python 粗略笔记(learn python the hard way)

标签(空格分隔): python


# _*_ coding: utf_8 _*_
'''
### ex1
print("ex1 ." * 5 )

print('this is my first print report')
print('Hello World')
print('I don\'\t like typing')
print('\n'.join(' '.join('{}*{}={}'.format(i, j, i*j) for i in range(1, j+1)) for j in range(1, 10)))


### ex2
print("ex2 ." * 5 )

print('I could have code like this.')
print('this will run.')
print('倒着读代码是一个好的排除错误的方法')


### ex3
print("ex3 ." * 5 )

print('I will now count my chickens:')
print("Hens,", 25+30/6)
print('Roster,', 100-25*3%4)
print('now, I will count the eggs:')
print(3+21+5-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("Is it greater? ", 5>-2)
print("Is it less or equal?", 5<=-2)

print("this is float or int divide, 5%2, 5/2, 5//2: ", 5%2,  5/2,  5//2)

### ex4
print("ex4 ." * 5 )

print("这是python 编写技巧:")
print("1、在每一行都加上注释, 2、倒着读.py文件, 3、朗读你的.py文件,将每个字母都读出来")

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_capacity.")
print("we need to put about", average_passengers_per_car, "in each car.")

### ex5
print("ex5 ." * 5 )

my_name = 'steve wang'
my_age = 35
my_weight = 128
my_height = 175
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Black'

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("He's teeth are usually %s depending on the coffe." % my_teeth)
print("If Iass %d, %d, and %d I get %d." % (
    my_age, my_height, my_weight, my_age + my_height +my_weight))
print("""
# python 格式化字符
# %% 输出百分号
# %d 输出整数
# %c 字符和ASCII码
# %s 字符串
# %u 无符号整数(10)
# %o 无符号整数(16)
# %e 浮点数字(科学记数法)
# %f 浮点数字(小数点)
# %n 存储输出地字符数量放入参数列表中的下一个变量中
# m.n.   是显示的最小总宽度,n 是小数点后的位数(如果可用的话)
# %r 一般用在debug时候
""")
print ('%s, %s'%('one', 'two'))
# one, two
print ('%r, %r'%('one', 'two'))
# 'one', 'two'

### ex6
print("ex6 ." * 5 )

# round 函数  round(3.32342,2)  #3.32  .
round(3.66666, 3)

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

### ex7
print("ex7 ." * 5 )
print("Mary had a little lamb")
print("Its fleece was white as %s." % 'snow')
print("." * 10)

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
print("加逗号来将很长的代码输出来,直接回车也可以")
print(end1 + end2 + end3 + end4 + end5 
+ end6)

### ex8
print("ex8 ." * 5 )

formatter = "%r %r %r %r "

print( formatter % (1,2,3,4) )
print( formatter % ("one", "two", "three", "four") )
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 中文测试.")
)
### ex9
print("ex9 ." * 5 )

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, or5, or 6.
""")
print("可以使用\"\"\" \来打印很多行")

### ex 10
print("ex10 ." * 5 )

print("打印输出多行有两种方法:")
print("""
1. 使用\\n的方法
2.使用\"""\
""")
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* Fishes
\t* Catnip\n\t* Grass
"""

print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)

print("""
\t  是水平制表符
\v  是纵向制表符
\\  是打印出反斜杠的
\'  是打印单引号的
\"  是打印双引号的
\a  响铃符
\b  是退格符
\f  是换页符
\r  是回车的符号
""")
'''
'''
while True:
    for i in ["\"", "-", "|", "\\", "|"]:
        print("%r\r" % i,)
    break
'''
'''
### ex11
print("ex11 ." * 5 )

print("如何把需要的数据读入:")

print("How old are you? ")
age = input()
print("How tall are you? "),
height = input()
print("How much do you weight?"),
weight = input()

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

### ex12
print("ex12 ." * 5 )

age = input("How old are you?")
height = input("How tall are you?")
weight = input("How weight are you?")

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

print("try to execute the order: python -m pydoc input: " )

print("try to understand os sys file open modules' meanings")
print("""just go to the webside
http://blog.csdn.net/sinat_36458870/article/details/76020881
    """)

###ex13
print("ex13 ." * 5)

print("""
now we will try to write a script that can accept a script
""")

from sys import argv

script, first, second, third = argv
print("""
script, first, second, third = argv 
this line's function is unpack the first, second, third, to argv
""")

print("please input your age:")
age = input()
print("your are %r age" % age)
print("The script is called: ", script)
print("!!!!!!!!the up line is important, try to understand it ")
print("first :", first)
print("the second script:", second)
print("third script:", third)

###ex14
print("ex14 ." * 5)

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))
'''
###ex15
'''
print("ex15 ." * 5)
print("this exercise aims to open and close file")
print("ex15 is exactly challenge,please type it one more time")

from sys import argv 

script, filename = argv # 这行用argv 导入变量名(filename)
py = open(filename) # 把 filename打开并且赋值给py, 这里py 返回的是一个 file object东西
print("Here is your file %r:" % filename) # 输入你的文件名
print(py.read()) # py.read就是告诉系统hey, read , 把py这个文件给我读出来
py.close()

print("Type the filename again:")
file_again = input(">")
py_again = open(file_again)
print(py_again.read())
py_again.close()


###ex16
print("ex16 ." * 5)
print("this exercise aims to open, read, readline, write close a file.")

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.")
print("if you want that, hit RETURN")

input("?")
print("opening file ...")
target = open(filename, 'w')
print("Truncating the file. Goodbye!")


print("Now, I'm going to ask you for three lines")

l1 = input("line1: ")
l2 = input("line2: ")
l3 = input("line3: ")

print("I'm going to write these lines to the file.")

target.write(l1)
target.write("\n")
target.write(l2)
target.write("\n")
target.write(l3)

print("And finally, we close it.")
target.close()


from sys import argv
script, filename = argv
print("we are going to read %r" % filename)
f = open(filename, 'r')
print(f.readlines())
f.close()


###ex17
print("ex17 ." * 5)

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)
# indata = in_file.read()
indata = open(from_file).read()
print("The input file is %d bytes long" % len(indata))
print("dose the output file exists ? %r " % exists(to_file))

print("ready, hit RETURN to continue, CTRL-C to abort.")
input()

# out_file = open(to_file, 'w')
# out_file.write(indata)
out_file = open(to_file, 'w').write(indata)
print("alright, all done")
# out_file.close()
# in_file.close()
'''
# from sys import argv
# script, from_file, to_file = argv
# indata = open(from_file).read()
# out_file = open(to_file, 'w').write(open(from_file).read())
# print("done")
'''
###ex18
print("ex18 ." * 5)

# the 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("we got nothing")

print_two(1, 2)
print_two('1', '2')
print_two_again(1, 2)
print_one(1)
print_none()
'''

###ex19
'''
print("ex19 ." * 5)

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 party!")
    print("get the 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)



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 party!")
    print("get the blanket.\n")
a = int(input("input what you need"))
b = int(input("input num_of argv"))
cheese_and_crackers(a, b)

'''
###ex20
'''
print("ex20 ." * 5)

from sys import argv
script, input_file = argv

t = open(input_file, 'w')
l1 = input("l1:")
l2 = input("l2:")
l3 = input("l3:")

t.write(l1)
t.write(l2)
t.write(l3)

print("writing done")
t.close()

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("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)

###ex21
print("ex21 ." * 5)
print("函数可以返回东西")

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 multiply(a, b):
    print("MULTIPLYING %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 = multiply(90, 2)
iq = divide(100, 2)
print("Age: %d, height: %d, weight: %d, iq: %d" 
    % (age, height, weight, iq))
# A puzzle for the extra credit, type it anyway
print("Here is a puzzle")

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print("That becomes:", what, "Can you do it by hand")

###ex22 
print("ex22 ." * 5)
print("函数可以返回东西")
...

转载和疑问声明

如果你有什么疑问或者想要转载,没有允许是不能转载的哈
赞赏一下能不能转?哈哈,联系我啊,我告诉你呢 ~~
欢迎联系我哈,我会给大家慢慢解答啦~~~怎么联系我? 笨啊~ ~~ 你留言也行
你关注微信公众号1.听朕给你说:2.tzgns666,3.或者扫那个二维码,后台联系我也行啦!
tzgns666

终于写完了,赞赏一下下嘛!

(爱心.gif) 么么哒~么么哒~么么哒
爱心从我做起,贫困山区捐衣服,为开源社区做贡献!

码字不易啊啊啊,如果你觉得本文有帮助,三毛也是爱!真的就三毛,呜呜。。。

我祝各位帅哥,和美女,你们永远十八岁,嗨嘿嘿~~~

weiChat
Alibaba

我祝各位帅哥,和美女,你们永远十八岁,嗨嘿嘿~~~

猜你喜欢

转载自blog.csdn.net/sinat_36458870/article/details/79076653
今日推荐