It is currently 27 Feb 2021, 11:53

• Featuring Members' Works (Image Attachments) •
1: member
2: member
3: member
4: member
5: member

GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  🡆 Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Lists in Python

Simple lessons to get your started in Python language
tim
GL Blogger

Lists in Python

Post#1 by tim » 08 Nov 2018, 17:21


In Python, a list is just an ordered collection of items (similarly to how a string is series of characters).
In a list, items can hold anything (for example strings, or numbers).

To declare a list, we declared comma separated items inside square brackets for example like below.
[1,"two",3,4]

The example above is a list that has the number 1 as the first item, the string "two" as the second item, the number 3 as the third item and the number 4 as the fourth item.

we can declare a variable and set it to hold a list like below
my_list = [1,"two",3,4]

Now to refer to the 1st item (because in Python indexes start at zero), we just have to write:
my_list[0]

There are some functions that we can use to manipulate (add/delete items from) a list. The function/method to add an item to end of list is called append, for example
my_list.append(5)
will add an item that is the number 5 to the end of list.
The function/method to delete an item at a certain index in the list is called del, for example
del(my_list[1])
will delete the item at index 1 (which is the 2nd item) from my_list.

To play with what you've learned in this lesson, go here

Next Lesson: Ranges and For Loops in Python Previous Lesson: Numbers in Python
aka Tin

         

Krikor
Forum Member

Post#2 by Krikor » 11 Nov 2018, 14:14


Tim,
Is it possible to add an item at a certain index in the list or just to the end of the list? :?:

Something like when you delete an item from the list.
The function/method to delete an item at a certain index in the list is called del, for example
del(my_list[1])
Thx Tim!
"I feel that in both art and music, it's not the success that matters but the pleasure it gives you. Focus on the pleasure and the learning will come naturally." - Brian Weston

tim
GL Blogger

Post#3 by tim » 11 Nov 2018, 14:58


@Krikor ,

There's an insert function.
For example if your list is named my_list then the syntax is mylist.insert(index,item).
example code:
mylist = [1,2,3]

mylist.insert(1,5)

print(mylist)


try playing with it here.
aka Tin

         


GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  🡆 Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Post Reply New Topic
  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest