Python 入门教程 9 ---- A Day at the Supermarket

简介:  第一节      1 介绍了for循环的用法         for variable in values:               statement      2 for循环打印出列表的每一项         for it...


 第一节

     1 介绍了for循环的用法

        for variable in values:

              statement

     2 for循环打印出列表的每一项

        for item in [1 , 2 , 3]:

             print item

        那么将打印出1,2,3

     3 练习:使用for循环,把列表中的每一项打印出来

names = ["Adam","Alex","Mariah","Martine","Columbus"]
# use for loop
for str in names:
    print str

 第二节

     1 介绍了我们可以使用for循环打印出字典中的每一个key

     2 比如这个例子,我们可以打印出key为foo的value值为bar

# A simple dictionary
d = {"foo" : "bar"}
for key in d: 
    # prints "bar"
    print d[key] 

      3 练习:打印出字典webster的所有key对应的value  

webster = {
	"Aardvark" : "A star of a popular children's cartoon show.",
    "Baa" : "The sound a goat makes.",
    "Carpet": "Goes on the floor.",
    "Dab": "A small amount."
}
# Add your code below!
for key in webster:
    print webster[key]

 第三节

     1 介绍了for里面我们可以添加if/else语句来判断

     2 比如

for item in numbers: 
    if condition: 
        # Do something

     3 练习:只输出列表中的7个数

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for num in a:
    if(a < 7):
        print a

 第四节

     1 介绍了在函数里面使用for循环

     2 练习

        1 写一个函数名为fizz_count,参数为列表x 

        2 函数返回列表中值为"fizz"的个数

# Write your function below!
def fizz_count(x):
    sum = 0
    for str in x:
        if(str == "fizz"):
            sum = sum+1
    return sum

 第五节

     1 练习:把以下的对应关系建立成字典prices

# the key and values
"banana": 4
"apple": 2
"orange": 1.5
"pear": 3
# you code here
prices = {}
prices["banana"] = 4
prices["apple"] = 2
prices["orange"] = 1.5
prices["pear"] = 3

 第六节

     1 练习:按照以下的格式输出

item
price: x
stock: x
# such as
apple
price: 2
stock: 0

# the first dictionary
prices = {}
prices["banana"] = 4
prices["apple"] = 2
prices["orange"] = 1.5
prices["pear"] = 3
# the second dictionary
stock = {}
stock["banana"] = 6
stock["apple"] = 0
stock["orange"] = 32
stock["pear"] = 15
# you code here
for key in prices:
    print key
    print "price: "+prices[key]
    print "stock: "+stock[key]

 第七节

     1 练习

        1 创建一个列表名叫groceries,有三个值分别为"banana","orange",  "apple"

        2 写一个函数名叫compute_bill,参数是列表food

        3 利用循环计算出food中所有物品的总价格

shopping_list = ["banana", "orange", "apple"]

stock = { "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    sum = 0
    for str in food:
        sum = sum + prices[str]
    return sum

 第八节

     1 练习

        1 创建一个列表名叫groceries,有三个值分别为"banana","orange",  "apple"

        2 写一个函数名叫compute_bill,参数是列表food

        3 利用循环计算出food中所有物品的总价格,但是我们在求价格的时候我们一个先判断在stock字典中对应的值是否大于0,如果是的话才进行求和并且把stock对应的value值减一

shopping_list = ["banana", "orange", "apple"]

stock = { "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    sum = 0
    for str in food:
        if(stock[str] > 0):
           sum = sum + prices[str]
           stock[str] = stock[str]-1
    return sum

 


目录
相关文章
|
6月前
|
Python Windows
Python入门教程:从基础到实践
Python入门教程:从基础到实践
78 0
|
6月前
|
Ubuntu Shell 数据库
Python Qt5 入门教程
Python Qt5 入门教程
111 1
|
7月前
|
机器学习/深度学习 数据采集 数据挖掘
Python 数据分析入门教程:Numpy、Pandas、Matplotlib和Scikit-Learn详解
Python 数据分析入门教程:Numpy、Pandas、Matplotlib和Scikit-Learn详解
142 0
|
1月前
|
JSON C语言 C++
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
63 1
|
1月前
|
算法 程序员 C++
【Python 基础教程 运算符06】Python3运算符超详细解析:全面入门教程,初学者必读
【Python 基础教程 运算符06】Python3运算符超详细解析:全面入门教程,初学者必读
92 2
|
1月前
|
算法 程序员 C++
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
108 1
|
1月前
|
存储 程序员 C++
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
50 0
|
3月前
|
机器学习/深度学习 测试技术 开发者
最新PyCharm下载安装以及Python环境搭建教程(含Python入门教程)
最新PyCharm下载安装以及Python环境搭建教程(含Python入门教程)
188 1
|
6月前
|
监控 应用服务中间件 nginx
软件开发入门教程网之Python uWSGI 安装配置
软件开发入门教程网之Python uWSGI 安装配置
27 0
|
6月前
|
SQL 关系型数据库 MySQL
软件开发入门教程网之Python MySQL - mysql-connector 驱动 2
软件开发入门教程网之Python MySQL - mysql-connector 驱动
35 0