博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编程Hokeydex(Python变量和数据类型简介)
阅读量:2517 次
发布时间:2019-05-11

本文共 7507 字,大约阅读时间需要 25 分钟。

The following post is a simple introduction to variables and the sorts of things commonly assigned to them in Python. If you are new to Python and/or programming, I encourage you to run each code block in your own editor as you read ( is great for quickly running a little code). Think up questions like “What would happen if I put an integer on the left side of the equals sign and the variable name on the right?”, and then try to answer them by playing around with the code and Googling anything you need extra help understanding. The building of a working Hokeydex complete with GUI and relational database is left as an exercise for the ambitious reader and hokeymon enthusiast.

以下文章是对变量以及Python中通常分配给它们的各种事物的简单介绍。 如果您不 Python和/或编程,建议您在阅读时在自己的编辑器中运行每个代码块( 非常适合快速运行一些代码)。 想出类似“如果在等号的左侧放置一个整数,在右侧放置一个变量名,会发生什么情况?”这样的问题,然后尝试通过遍历代码并使用Google搜索来查找需要您额外帮助的内容,以回答这些问题。理解。 雄心勃勃的读者和hokeymon爱好者将练习构建具有GUI和关系数据库的完整Hokeydex。

Professor Woke enters the laboratory.

沃克教授进入实验室。

Woke: Is the Hokeydex finished? Bash and Jerry each need one, and as you know they leave tomorrow morning on their quest to catch all the hokeymon.

:是对Hokeydex完成了吗? 巴什(Bash)和杰里(Jerry)各自需要一个,正如你所知,他们明天早上出发去追捕所有的hokeymon。

Aide: Sorry Professor, I’m still working out some of the bugs in the software.

艾德 :对不起,教授,我还在研究软件中的一些错误。

Woke: Alright, well, let’s see what you’ve got so far. For example, assuming hokeymon have been seen in the following order:

沃克 :好吧,让我们来看看到目前为止。 例如,假设按以下顺序显示了hokeymon:

  1. Myrtle
  2. Charmanderp
  3. Bulbasnore
  4. Ninefails
  5. Machump
  6. Bulbasnore
  7. Ninefails
  1. 默特尔
  2. 查曼德普
  3. 布尔巴斯诺尔
  4. 尼法尔
  5. 马赫姆普
  6. 布尔巴斯诺尔
  7. 尼法尔

…what does the Hokeydex do?

……Hokeydex会做什么?

## Data Type: Booleanhave_hokeymon_been_seen = Trueprint(type(have_hokeymon_been_seen))print(have_hokeymon_been_seen)
True

Woke: So all it does is tell us whether or not any hokeymon have been seen?

沃克 :所以它所做的就是告诉我们是否曾见过任何霍基蒙犬?

Aide: Uh…well, yes. Isn’t that enough, Professor?

艾德 :嗯...是的。 教授,还不够吗?

Woke: Sigh, no, not even close. At the very least it needs to tell us how many hokeymon we’ve seen.

沃克 :叹息,不,甚至没有接近。 至少它需要告诉我们我们已经看过多少个hokeymon。

Aide: Oh, that’s a great idea Professor! How about something like this?

艾德 :教授,这是个好主意! 这样的事情怎么样?

## Data Type: Integerhow_many_hokeymon = 7print(type(how_many_hokeymon))print(how_many_hokeymon)
7

Woke: But I feel like that number is too round. What if we also see just half of a Dewrong? Maybe half of it got eaten by a Drawini, and we found the other half washed up on shore. Now what?

沃克 :但是我觉得这个数字太圆了。 如果我们也只看到Dewrong的一半怎么办? 也许其中一半被Drawini吃了,我们发现另一半在岸上被冲走了。 怎么办?

## Data Type: Floathow_many_hokeymon = 7.5print(type(how_many_hokeymon))print(how_many_hokeymon)
7.5

Woke: That’s an improvement. But honestly, now that I think about it, we’re going about this all wrong. What we really need is for the Hokeydex to actually record the names of the hokeymon it sees, not just how many.

沃克 :这是一个进步。 但是说实话,既然我考虑了一下,我们就将这一切都弄错了。 我们真正需要的是Hokeydex实际记录它看到的hokeymon的名称,而不仅仅是记录多少。

## Data Type: Stringhokeymon = "Myrtle Charmanderp Bulbasnore Ninefails Machump Bulbasnore Ninefails"print(type(hokeymon))print(hokeymon)
Myrtle Charmanderp Bulbasnore Ninefails Machump Bulbasnore Ninefails

Woke: Hmm…better, but we need the names to be separated somehow.

沃克 :嗯…更好,但是我们需要以某种方式将名称分开。

## variable assignments on individual lineshokeymon1 = "Myrtle"hokeymon2 = "Charmanderp"hokeymon3 = "Bulbasnore"hokeymon4 = "Ninefails"hokeymon5 = "Machump"hokeymon6 = "Bulbasnore"hokeymon7 = "Ninefails"

Woke: Eh, too many lines of code. Can’t you somehow condense that into one line?

唤醒 :嗯,代码行太多。 您不能以某种方式将其浓缩为一行吗?

## multiple variable assignments on one lineh1, h2, h3, h4, h5, h6, h7 = "Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"

Woke: Warmer, definitely getting warmer. But a separate variable for each observed hokeymon seems a little inconvenient, doesn’t it?

醒来 :温暖,绝对温暖。 但是每个观察到的hokeymon都有一个单独的变量似乎有点不方便,不是吗?

Aide: Oh, I think I have it!

艾德 :哦,我想我有!

## Data Type: Listhokeymon = ["Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"]print(type(hokeymon))print(hokeymon)
['Myrtle', 'Charmanderp', 'Bulbasnore', 'Ninefails', 'Machump', 'Bulbasnore', 'Ninefails']

Woke: Much better! Now, say we want a version of this list that can’t be altered, just to make sure no careless changes corrupt our precious data. Is there a way to do that?

沃克 :好多了! 现在,假设我们想要一个不能更改的列表版本,只是为了确保没有粗心的更改会破坏我们的宝贵数据。 有没有办法做到这一点?

## Data Type: Tuplehokeymon = ("Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails")print(type(hokeymon))print(hokeymon)
('Myrtle', 'Charmanderp', 'Bulbasnore', 'Ninefails', 'Machump', 'Bulbasnore', 'Ninefails')

Woke: Very nice. But you know what, it looks like the duplicates are still there. The most important thing is to record the types of hokeymon we’ve seen. Recording every individual observed hokeymon one by one could get unorganized fairly quickly. Can we somehow get rid of the duplicates?

沃克 :很好。 但是您知道吗,看起来重复项仍然存在。 最重要的是记录我们所见过的hokeymon的类型。 逐一记录每个观察到的霍基蒙犬可能很快就会变得井井有条。 我们可以以某种方式摆脱重复吗?

## Data Type: Sethokeymon = {"Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"}print(type(hokeymon))print(hokeymon)
{'Charmanderp', 'Bulbasnore', 'Machump', 'Myrtle', 'Ninefails'}

Woke: Beautiful! And say again that we want a version of the hokeymon set that can’t be accidentally changed.

沃克 :好漂亮! 再说一遍,我们想要一个不会意外更改的hokeymon集版本。

## Data Type: Frozensethokeymon = frozenset(["Myrtle", "Charmanderp", "Bulbasnore", "Ninefails", "Machump", "Bulbasnore", "Ninefails"])print(type(hokeymon))print(hokeymon)
frozenset({'Charmanderp', 'Machump', 'Bulbasnore', 'Ninefails', 'Myrtle'})

Woke: I feel like we are on the verge of greatness!

沃克 :我觉得我们正处于伟大的边缘!

Aide: Thank you Professor, this is certainly easier with your help.

艾德 :谢谢教授,在您的帮助下,这当然会更容易。

Woke: Finally, let’s say we want to somehow record how many times we’ve seen a hokeymon. Is there a convenient way that can be done? To store the number of appearances alongside the name?

沃克 :最后,假设我们想以某种方式记录我们看过hokeymon的次数。 有没有方便的方法可以做到? 要在名称旁边存储外观数量?

## Data Type: Dictionaryhokeymon = {"Myrtle": 1, "Charmanderp": 1, "Bulbasnore": 2, "Ninefails": 2, "Machump": 1}print(type(hokeymon))print(hokeymon)
{'Charmanderp': 1, 'Bulbasnore': 2, 'Ninefails': 2, 'Machump': 1, 'Myrtle': 1}

Woke: Bingo! Okay, I think that’s a wrap. Bash and Jerry should be very pleased with their new Hokeydexes. And just think of all the experiments we can do once they bring back their samples. A few months of intensive hokeymon testing, and my cosmetics company will be up and running in no time!

醒了 :宾果! 好吧,我认为这很不错。 Bash和Jerry应该对他们的新Hokeydexes感到非常满意。 并想一想所有可以带回样品的实验。 几个月的hokeymon密集测试,我的化妆品公司很快就会成立并运营!

Aide: Wow, I feel so accomplished!

艾德 :哇,我觉得好厉害!

Woke: You certainly should. Don’t dawdle though, we need this Hokeydex software fully functioning by morning. Go ahead and polish off the details tonight.

沃克 :当然可以。 不过,请不要犹豫,我们需要此Hokeydex软件在早上之前能正常运行。 今晚继续完善细节。

Aide: Uhh…well Professor, all we’ve really done so far is just assign instances of different data types to variables. It’s not quite a functioning application yet. I could really use some help with…

艾德 :呃……好吧,教授,到目前为止,我们实际上所做的只是将不同数据类型的实例分配给变量。 它不是一个功能正常的应用程序。 我真的可以在...方面使用一些帮助

Woke: I’m sure you’ll figure it out. You have all night for goodness’ sake. See you bright and early!

沃克 :我相信你会弄清楚的。 为了良善,你整夜都是。 早日见!

翻译自:

转载地址:http://gzhwd.baihongyu.com/

你可能感兴趣的文章
生成商户订单号/退款单号
查看>>
cmake的一个编译报错
查看>>
linux下connect超时时间探究
查看>>
git使用——分支
查看>>
selenium之多线程启动grid分布式测试框架封装(一)
查看>>
突然的伤感
查看>>
Objective-C 链式编程思想
查看>>
iOS开发之XMPP即时通讯简单实现
查看>>
Java连接各类数据库
查看>>
Multiload-ng
查看>>
Java7并发编程实战(一) 线程的等待
查看>>
mysql update语句添加表关联查询
查看>>
开通了一个微信公众账号,主要想分享一些自己对于行业、技术和产品的思考以及收录精彩内容给读者...
查看>>
留言板
查看>>
c#$用法
查看>>
js通过as完成socket通信
查看>>
关于我书里提到的“挂多个类,使用类的组合”的一些补充
查看>>
JavaScrip中的循环语句
查看>>
IntelliJ IDEA和Webstorm 的下载安装、注册码(永久有效)
查看>>
停止使用非版本控制的可执行代码
查看>>