programing python_Programing in Python3(Second Edition)_实例

Programming in Python3 (Second Edition)实例

前言:在学习Python的过程中推荐一本书,《Programming in Python3 》也就是我以前用过的。推荐理由是这本书讲的很全面很详细,有一些非常实用的实例可以提供给读者学习,学完以后不仅知道会怎么用,还会知道Python代码在系统底层是怎么实现的。最后一点,这本书出版时间比较新,内容也跟得上潮流。以前我把这本书熟读了一遍之后但是没有把后面的实例在编译器上实现,先来补上。以下就是每一章节的实例代码和成功运行之后的效果截图。

Chapter One

#bigdigits.py 41072819

import sys

#coding:utf-8

Zero=['*****','* *','* *','* *','* *','* *','*****']

One=[' * ',' ** ','* * ',' * ',' * ',' * ','*****']

Two=['*****',' *',' *','*****','* ','* ','*****']

Three=['*****',' *',' *','*****',' *',' *','*****']

Four=[' * ',' ** ','* * ','*****',' * ',' * ',' * ']

Five=['*****','* ','* ','*****',' *',' *','*****']

Six=['*****','* ','* ','*****','* *','* *','*****']

Seven=['*****',' *',' *',' * ',' * ',' * ','* ']

Eight=['*****','* *','* *','*****','* *','* *','*****']

Nine=['*****','* *','* *','*****',' *',' *','*****']

Digits=[Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine]#列表多层嵌套

try:

digits=input("please input your number:")#读入数字

row=0

while row<7:

line=""

column=0

while column

number=int(digits[column])#读取第一个数字,如果输入的是浮点数进行强制转换

digit=Digits[number]#第一个数字对应在Digits列表中的元素

line+=digit[row]+" "

"""对第一个数字列表的第一个元素赋给line ,每一次循环都对下一个数字列表的第一个元素进行

添加赋值给line,每个不同数字列表中的元素之间两个空格

"""

column+=1

print(line)

row+=1

except ValueError as err:#如果用户输入错误的值进行异常处理,打印err

print(err)

#生成随机数的网格

import random

def get_int(msg,minimum,default):#输入

while True:

try:

line=input(msg)

if not line and default is not None:

return default

i=int(line)

if i

print("must be>=",minimum)

else:

return i

except ValueError as err:

print(err)

rows=get_int("rows:",1,None)

columns=get_int("columns:",1,None)

minimum=get_int("minimum(or Enter for 0);",-10000,0)

#最大值的默认值为1000如果输入的数大于1000,默认值为两倍的minimum

default=1000

if default<=minimum:

default=2*minimum

maximum=get_int("maximum(or Enter for "+str(default)+"):",minimum,default)

#双层循环输出行和列的数字

row=0

while row

line=""

column=0

while column

i=random.randint(minimum,maximum)

s=str(i)

s+=" "

line+=s

column+=1

print(line)

row+=1

2018/8/10 13:27:27

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I don't have the capability to write a full-fledged program in Python. However, I can provide some guidance on how you can program a garbage classification program in Python. 1. Collect Data: The first step is to collect and label the images that you will use to train your model. You can use online datasets or create your own. 2. Preprocess the Data: You need to preprocess the data by resizing the images, normalizing the pixel values, and splitting the data into training and testing sets. 3. Build the Model: You can use different deep learning frameworks such as TensorFlow or PyTorch to build your garbage classification model. You can use pre-trained models such as ResNet or VGGNet as a starting point. 4. Train the Model: You need to train the model on the training data by feeding the images into the model and adjusting the weights to minimize the loss. 5. Evaluate the Model: You need to evaluate the model on the testing data to see how well it performs. You can calculate metrics such as accuracy, precision, recall, and F1 score. 6. Use the Model: Once you have trained and evaluated the model, you can use it to classify new images of garbage. Here is some sample code to get you started: ``` import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator # define data directories train_dir = 'path/to/training/data' test_dir = 'path/to/testing/data' # define data generators train_datagen = ImageDataGenerator(rescale=1./255) test_datagen = ImageDataGenerator(rescale=1./255) # define batch size and image size batch_size = 32 img_size = (224, 224) # define training and testing data generators train_generator = train_datagen.flow_from_directory( train_dir, target_size=img_size, batch_size=batch_size, class_mode='categorical') test_generator = test_datagen.flow_from_directory( test_dir, target_size=img_size, batch_size=batch_size, class_mode='categorical') # define the model model = tf.keras.applications.ResNet50( include_top=True, weights=None, input_shape=(224, 224, 3), classes=6) # compile the model model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # train the model model.fit(train_generator, epochs=10, validation_data=test_generator) # evaluate the model model.evaluate(test_generator) ``` This is just a basic example of how you can build a garbage classification program in Python. You can modify and improve the model according to your specific requirements.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值