今天要介紹的是關于使用簡單的方法解決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的信息,請繼續關注中培偉業。