Python Lists

Gauloran

Kıdemli Moderatör
7 Tem 2013
8,096
585
local
In Python, a list is a collection of items, which can be of any data type. Lists are ordered, mutable, and can contain duplicates. They are one of the most commonly used data structures in Python.

python-list.jpg


Here's an example of how to create a list in Python:

Python:
my_list = [1, 2, 3, "four", 5.6, True]

This creates a list with six items, including integers, strings, floats, and a Boolean value. You can access items in a list by their index, which starts at 0. For example, to access the first item in the list, you would use:


Python:
my_list[0]

This would return the value 1. You can also use negative indexing to access items from the end of the list. For example, to access the last item in the list, you would use:

Python:
my_list[-1]

This would return the value True.

Lists can be modified by adding, removing, or changing items. Here are some common methods for modifying lists:
  • append(item): adds an item to the end of the list​
  • insert(index, item): inserts an item at a specific index​
  • remove(item): removes the first occurrence of an item from the list​
  • pop(index): removes and returns the item at a specific index​
  • sort(): sorts the items in the list in ascending order​
  • reverse(): reverses the order of the items in the list​
Here's an example of how to use some of these methods:

Python:
my_list = [1, 2, 3, "four", 5.6, True]
my_list.append("six")
my_list.insert(4, "five")
my_list.remove(3)
my_list.pop(0)
my_list.sort()
my_list.reverse()

After running this code, the my_list variable would contain the following items, in this order: "six", 5.6, "four", True, 2, "five".

64r6iny.png

 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.