笨办法学python ex21-26

ex21
函数中有return可以返回值,没有return函数返回的是None

# -*- coding: utf-8 -*-

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"MULTIPLY %d * %d" %(a, b)
    return a * b

def divide(a, b):    
    print "DIVIDING %d / %d" %(a, b)
    return a / b


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 in 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?"

print '\n'
asd = float(raw_input('you can write fudian'))# float 可以输入浮点数 
print asd 
qwe = int(raw_input('you can\'t write fudian '))# raw_input可输入任何,返回字符串
print qwe # int 取整向下

ex24

# -*- coding: utf-8 -*- 

print"Let's practice everything."
print 'You \'d need to know \'bout escape with \\ that do  \n newlines and \t tabs.'

poem = """
\tThe lovely world 
with loqic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an expanation
\n\twhere there is none.
"""# \t tab 

print "-" * 10
print poem 
print "-" * 10 

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 crates." % (beans, jars, crates)

start_point = start_point / 10
print "We can also do that this way:"
print "We'd have %d beans, %d jars, %d crates." % secret_formula(start_point)

函数内部的变量是临时的,当函数返回后,返回值可以赋予一个变量
ex25

# coding=utf-8
def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ') # split分割函数
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words) #分类

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Print the last word after poping it off."""
    word = words.pop(-1)
    print word



def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    words = break_words(sentence)
    print_first_word(words)
    print_last_words(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_words(words)

在导入 ex25.py 模块时,可以两种方式
1. >>>import ex25
不需要 ex25.py,直接ex25就行
运行ex25中函数,需要 例:ex25.break_words(stuff)
每次先打’ex25.’
2. from ex25 import *
将ex25 的所有东西import 进来

下面是关于 split()函数的笔记

# stuff.split()使用默认分隔符,分空格; spilt 分割函数  
# stuff.split('.', 2) 以.为分隔符,分割两次
# stuff.spilt('.',3)[1] 分割三次,并取序列为1的
# u1, u2, u3 = stuff.split('.', 3) 把分割后的三部分分别保存到u1, u2, u3 中
# 去掉换行符 print stuff.split('\n')
# 可以分离文件名和路径
# >>>import os
# >>>print os.path.split('PATH')给出目录和文件名,输出路径和文件名;给出目录名,输出路径和空文件名
#例子
#>>> str = "hello boy<[www.doiido.com]>byebye"
#>>> print str.split("[")[1].split("]")
# www.doiido.com 

ex26

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print word

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)


print "Let's practice 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 needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 5
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 jeans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)


sentence = "All god \tthings come to those who weight."
import ex25
words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
print sorted_words

print_first_and_last(sentence)

print_first_and_last_sorted(sentence) 

你可能感兴趣的:(笔记本推荐)