中培伟业IT资讯频道
您现在的位置:首页 > IT资讯 > 软件研发 > Python的做事方式是什么?

Python的做事方式是什么?

2020-08-28 17:52:28 | 来源:中培企业IT培训网

今天要介绍的是关于使用简单的方法解决python问题的。作为一名编码人员,从描述数据类型开始学习和解释语言新概念的方法。那么python中的数据类型是什么? 数据类型或简单类型是数据的属性,它告诉解释器程序员打算如何使用数据。众所周知,python中最常见的数据类型是float(浮点数),int(整数),str(字符串),bool(布尔值),列表和dict(字典)。但是与C或C ++不同,Python将自动确定将数据存储为哪种数据类型。但是您需要了解数据类型,才能将数据从一种类型转换为另一种类型(也称为Typecasting或Type conversion)。让我们看一个基本数据类型(清单)的示例,以整体上理解语言和编程。

## I am simply creating a list and printing out the type of indivisual elements in the list ..

List = [1,2.0,"Hello World",True,[1,2,3,4,5],{'key':'value'}]# I am using for in loop because the size of the list is fixed ..for element in List:

print(type(element))# or you can also use list compreshension

elements = [type(element) for element in List]

print(elements)

  #output //

<CLASS'INT'>

<CLASS'FLOAT'>

<CLASS'STR'>

<CLASS'BOOL'>

< class'list'> <

class'dict'>

[<CLASS'INT'>,,<CLASS'STR'>,<CLASS'BOOL'>,<CLASS'LIST'>,<CLASS'DICT'>]]

记住 —除了python提供的数据结构外,我们可以在python中创建自己的数据结构。您要做的就是创建一个类,并在该类中定义自己的方法。这 就是创建熊猫数据框的方法……但是我有一天会写它。现在,让我们坚持一些基本知识!

  基本清单方法

让我们看一下一些List方法,用于从列表中插入和删除元素…

1. append:在列表中添加元素。性能:将元素附加到列表末尾的时间复杂度为o(1),而其他任何地方均为o(n)

2. remove:删除列表中的元素。性能:删除列表中元素的时间复杂度为o(n)

搜索列表中元素的时间复杂度始终为o(1)

## List Methods ...

# insertion

List.append((1,5.0))

print(List)

# seraching an element in the list which is already in a list ...

list_in_list = List[4][0]

print(list_in_list)

# deletion

List.remove(1)

print(List)

#output //

[1,2.0,'Hello World',True,[1,2,3,4,5],{'key':'value'},(1,5.0)]

1

[2.0,'Hello世界”,真,[1、2、3、4、5],{'键':'值'},(1、5.0)]

  操作清单项目

1. lambda函数:它们以1行编写,用于定义自定义函数;

2. map()方法(在DataScience中也称为向量方法):使用map函数将您定义的自定义函数映射到列表的每个元素。map方法采用2个参数,您要应用的功能b。列出您想要上述功能的应用;

3. enumerate()方法: enumerate()方法将计数器添加到可迭代对象并返回它。返回的对象是一个枚举对象。

custom_list = [[1,2,3,4,5],["Cat","Dog","Dog","Tiger","Lion","Goat"]]

# custom function to find square of a number

def square_of_number(x):

return x*x

# using lambda functions to create a new list from exisitng list but with cube of each items in the list..

cube_of_number = lambda x:x*x*x

squares_of_list_items = lambda x:x*x

# using map function to apply a function to all elements of the list

new_list_of_squares = list(map(squares_of_list_items,custom_list[0]))

new_list_of_cubes = list(map(cube_of_number,custom_list[0]))

print(new_list_of_squares)

print(new_list_of_cubes)

# Using Enumerate to find the count of Each animal in custum_list

for i,x in enumerate(custom_list[1]):

print('iterater is :', i ,'and value is ', x)

#output //

[ 1、4、9、16、25

]

[1、8、27、64、125 ] <枚举对象,位于0x7f0cf44e4b40>

iterater为:0,值为Cat

迭代器:1,值为Dog

迭代器:2并且值是Dog

迭代器是:3和值是Tiger

迭代器是:4并且值是Lion

迭代器是:5并且值是山羊

  发电机和装饰器:

1. Python生成器是创建迭代器/可迭代对象的简单方法。它可以在函数调用时暂停,也可以在整个程序的生命周期中通过使用next()函数在任何时间恢复。

2.装饰器是python中的一流对象。这意味着它们可以在任何函数中通过引用传递。装饰器只是一个奇特的名称,也是将函数作为参数传递给另一个函数的方式。它们也被称为可调用对象,它接受一个函数并返回或改变该函数的行为。

# Generators :

def generator():

for i in range(5):

yield i

print(generator) # output://

a = generator()

print(next(a)) # output:// 0

print(next(a)) # output:// 1

# Decorators

def function1():

print('this is function 1')

def function2(function_argument):

function_argument()

print('this is function 2')

new_object = function2(function1) # output:// this is function 1 this is function 2

# using it as a decorator function ...

@function2

def function1():

print('this is function 1') # output:// this is function 1 this is function 2

* args和** kwargs

args和** kwargs是最重要的关键字,在我的编码风格和编程实践中,我个人经常使用它们。好吧,想象一下!您正在创建函数,却不知道预期的参数数量。在这种情况下,我们使用* args,如果它是字典,则使用** kwargs。

def arr(*args):

for i in args:

print(i)

arr(1,2,3,4) # output: // 1 2 3 4

arr(1,2) # output:// 1 2

Python是一种很棒的编程语言。它易于学习且易于实施。它支持许多程序包,因此许多黑客,数据科学家和后端开发人员都使用它来执行日常任务。想了解更多关于Python的信息,请继续关注中培伟业。

标签: Python 软件研发