欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python初学者_初学者的python基础

程序员文章站 2022-04-09 22:22:21
...

python初学者

Python is a high-level interpreted programming language that is used for a variety of tasks such as data analysis, machine learning and web development. In this post, we will walk through some basic concepts in python. Specifically, we will discuss how to define lists and dictionaries, iterate over lists and dictionaries, and define functions.

Python是一种高级解释型编程语言,可用于各种任务,例如数据分析,机器学习和Web开发。 在本文中,我们将逐步介绍python中的一些基本概念。 具体来说,我们将讨论如何定义列表和字典,遍历列表和字典以及定义函数。

Let’s get started!

让我们开始吧!

An important python data structure is the python list. A python list is simply a collection of objects. The objects can be strings, integers, floats etc.. An example is a list of names. To define a list of names in python we do the following:

一个重要的python数据结构是python列表。 python列表只是对象的集合。 对象可以是字符串,整数,浮点数等。示例是名称列表。 要在python中定义名称列表,请执行以下操作:

names = ['Bob', 'Sarah', 'Ted', 'Nicole']
print("Names: ", names)
python初学者_初学者的python基础

The names lists contain string objects. We can also define a list of float objects, such as heights in centimeters for each person:

名称列表包含字符串对象。 我们还可以定义一个浮动对象列表,例如每个人的身高(厘米):

heights = [180.0, 160.0, 190.0, 150.0]
print("Heights: ", heights)
python初学者_初学者的python基础

Now suppose we wanted a have a way to map names to a height value in centimeters. For example, given that Bob is 180 cm tall, is there a way to reliably get his height given his name. We can use dictionaries to achieve this. To construct this dictionary we do the following:

现在假设我们想要一种将名称映射到以厘米为单位的高度值的方法。 例如,假设鲍勃(Bob)身高180厘米,有没有一种方法可以可靠地根据他的名字确定他的身高。 我们可以使用字典来实现这一目标。 要构建此词典,我们需要执行以下操作:

names_height = {'Bob':180.0, 'Sarah':160.0, 'Ted':190.0, 'Nicole':150.0}
print("Names & Heights: ", names_height)
python初学者_初学者的python基础

Now, we can get Bob’s height by doing the following:

现在,我们可以通过执行以下操作来获得Bob的身高:

print("Bob's Height (cm): ", names_height['Bob'])
python初学者_初学者的python基础

And if we want Nicole’s height:

如果我们要妮可的身高:

print("Nicole's Height (cm): ", names_height['Nicole'])
python初学者_初学者的python基础

We can also construct the same dictionary using the ‘zip()’ and ‘dict()’ methods:

我们还可以使用'zip()'和'dict()'方法构造相同的字典:

names_height_2 = dict(zip(names, heights))
print("Names & Heights using dict() & zip(): ", names_height_2)
python初学者_初学者的python基础

Now suppose we wanted to convert the heights in the heights list to feet. To convert cm to feet we divide by 30.48. We can do this in a for-loop:

现在假设我们想将高度列表中的高度转换为英尺。 要将厘米转换为英尺,我们除以30.48。 我们可以在for循环中执行此操作:

heights_feet = [] #initialize empty list
for height in heights:
heights_feet.append(height/30.48)
print("Height in Feet: ", heights_feet)
python初学者_初学者的python基础

We can also use something called list comprehension to achieve the same task:

我们还可以使用称为列表理解的功能来完成相同的任务:

heights_feet_2 = [height/30.48 for height in heights]
print("Height in feet list comprehension: ", heights_feet_2)
python初学者_初学者的python基础

Similarly, dictionary comprehension can be used to construct a dictionary with names and height in feet:

类似地,字典理解可用于构建名称和高度以英尺为单位的字典:

names_height_feet = {name:height/30.48 for name, height in names_height.items()}
print("Names & Heights in Feet Dictionary Comprehension: ", names_height_feet)
python初学者_初学者的python基础

Finally, we can define a function that takes a dictionary with names/ heights (cm) pairs and prints a new dictionary with names/heights (ft) pairs:

最后,我们可以定义一个函数,该函数接受具有名称/高度(cm)对的字典,并打印具有名称/高度(ft)对的新字典:

def convert_to_feet(height_cm_dict):
height_feet_dict = {}
for key, value in height_cm_dict.items():
height_feet_dict[key] = value/30.48
print("Names & Heights in Feet Function Conversion: ", height_feet_dict)

And let’s call our function with our dictionary that contains centimeter heights:

让我们用包含厘米高度的字典来调用函数:

convert_to_feet(names_height)
python初学者_初学者的python基础

I’ll stop here but feel free to play around with the code yourself.

我将在这里停止,但您可以自己随意使用该代码。

结论 (CONCLUSIONS)

To summarize, in this post we discussed how to define lists and dictionaries in python. We discussed how to construct dictionaries from lists using the ‘dict()’ and ‘zip()’ methods. We also showed how to iterate over these objects using for-loops, list comprehension for lists and dictionary comprehension for dictionaries. Finally, we showed how to define a function that converts the centimeter height values in a dictionary to feet and store the new values in a separate dictionary. The code from this post is available on GitHub. I hope you found this post useful/interesting. Thank you for reading!

总而言之,在本文中,我们讨论了如何在python中定义列表和字典。 我们讨论了如何使用'dict()'和'zip()'方法从列表构造字典。 我们还展示了如何使用for循环,列表的列表理解和字典的字典迭代来遍历这些对象。 最后,我们展示了如何定义一个函数,该函数将字典中的厘米高度值转换为英尺,并将新值存储在单独的字典中。 这篇文章中的代码可在GitHub找到。 我希望您发现这篇文章有用/有趣。 感谢您的阅读!

翻译自: https://towardsdatascience.com/python-basics-for-beginners-5fac8dd4fe43

python初学者

相关标签: python 人工智能