Python crash course for beginner 1 St Step : Playing with list

Abdul kahar muzakir
4 min readOct 17, 2022

--

Basic sintax and project base introduction

A list in python is very similiar to the notion of an array in other programming languages, list is a collection of items in a perticular order. You can make a list that includes the letters of the alphabets. list as being an indexed collection of related objects, with each slot in the list numbered from zero upward. Unlike array in alot of the other programming languages, thought, list are dynamic in python, in that they can grow and shrink on demand. List are mutable, in that you can change a list at any time by adding, removing or changing object

Here’s a simple example of a list

Run your code and see what happens : )

If you ask python to print a list, python returns its presentations of the list, including square bracket

How to accessing element in a list?

List are ordered collections, so you can access any element in list by telling python the position, or index, of the item desired. For example

Look in your console, what do you see after the code is executed? YES python returns just that element without square bracket : “Python”. In python considers the first item in a list to be a position 0, or not position 1. This is true of most programming language.

Change elements

Actually, list you create will be dynamic, meaning you’ll build a list and then change, add and remove element from a list. To change an element, use the name of the list followed by the index of the element you want to change, and then provide the new value you want that item to have.

Run your code and see what changes

The original list index 0 ‘Python’ as the first element replaced with ‘Dart’. You can change the value of any item in a list, not just the first item. Very easy :)

Add elements

You might want to add a new element to a list for many reasons, you might want to make a new programming languages in a list. Python provides several ways to add new data to existing. The simple ways to add a new element to a list is to append() the item to the list, the new element is added to the end of the list

You can add a new element at any postition in your list by using insert() method. You do this by specifying the index of the new element and the
value of the new item like this

Take a look at what’s changed, “Ruby” data appears at index 0 because, The insert() method opens a space at position 0 and stores the value “Ruby” at that location. This operation shifts every other value in the list one position to the right:

Next, Removing element

Often, You’ll want to remove an item or a set of items from a list , for example when yo don’t like programming language from the list. you’ll most likely want to remove it from the list of programming language. You can remove an item according to its position in the list or according to its value.

If you know the position of the item you want to remove from a list, use del statment for examlpe

The code at uses del to remove the first item , ‘Python’, from a list of programming language. You can remove an item from any position in a list using the del statement if you know its index

Try it yourself, practice what you learn then understand how it works! see you in the next post :)

--

--