Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 11: Python Lists

2 Replies, 1037 Views

[Image: 1*RJMxLdTHqVBSijKmOO5MAg.jpeg]

Lists:
      Today we are discussing something new. Even if you worked with other programming languages this is new for you, yes we are talking about lists. List is a new concept if you know about arrays this is similar to it. As you know python has no built-in arrays so lists work as arrays in python. The data types we mentioned in earlier threads were primitive data types. And now we are talking about non-primitive data types. Primitive data types are the basis for other non-primitive data types. List is a collection of data and it is a complex data type for example,

Code:
vowels = ['a','e','i','o','u']

print(vowels)


The values of the list are stored in square brackets. Every element in the list is indexed and can be accessed individually indexing of elements starts from 0. If you want to access the first element of the list “vowels” you should use 0 index for it. Let's code it..

Code:
vowels = ['a','e','i','o','u']
print(vowels[0])
print(vowels[4])

In this way you can access any of the elements in the list. And if you want to access an element which is not in the list it will generate an error like there are 5 elements in the list ”vowels” and if you call vowels[5] it will generate an error because there is nothing on index 5. You can also use other types of data in the list.

Code:
# Program to store different types of data in the list
myList = ["Name","age",200,178.5]
print(myList)
print(myList[0])

Operations on list:
We can do more cool stuff with our lists we can add two lists like

Code:
values= ["Covid", 300]
attributes = ["Name","age"]

print(attributes + values)


You can use the ‘in’ function to check whether an element is present inside the list or not.


Code:
# Program to check whether a character belongs to vowels or not

vowels = ['a','e','i','o','u']

ans = input()

print(ans in vowels)

List method:

     We use the append function to add an element on the end of the list like..

Code:
# Program to demonstrate append function
grade = ['a','b','b','c']
print(grade)
grade.append('d')
print(grade)

Now what if you want to insert at first or in between? For this we use the insert function


Quote:listName.insert(index , value)

Now let’s have a look at the code..


Code:
# Program to demonstrate insert function
grade = ['a','b','b','c']
print(grade)
grade.insert(0, 'd')
grade.insert(2, 'a')
print(grade)


Here is another function that will returns the length of list i.e how many elements the list have that is the len function

Code:
# Program to demonstrate len function
grade = ['a','b','b','c']
print(len(grade))

We can store the value returned by this function and use it when we traverse (Accessing all
elements of list) the list.

Clear function is used to remove all the elements from a list

Code:
# Program to demonstrate clear function
number = [1,2,3,4,5,6,7,8,9]
print(number)

number.clear()

print(number)

Strings also act like lists. You can say that a string is a list of characters. But you can edit a character in a list but can’t in strings. Let's see it in code.

Code:
# Program

vowels = ['a','e','i','o','u']

vowels[4] = 'z'

print(vowels)


This program will replace ‘u’ with ‘z’. And if we try to replace a character in a string it will generate an error. Have a look at this piece of code to understand it better.

Code:
str = "My name is Covid"
print(str[6])


This program will print 6th character in str but if you want to change the 6th character it will generate error now look at this code

Code:
str = "My name is Covid"
str[6] = 'a'
print(str)

This code will generate an error

Idea Keypoint: Black space is also a character and has its own index

Accessing elements of list using loops:

First look at this code….

Code:
vowels = ['a','e','i','o','u']
print(vowels[0])
print(vowels[1])
print(vowels[2])
print(vowels[3])
print(vowels[4])


As you can see we are repeating the print statement so where there is repeating there is loop so we can use loop here. Now let’s print this program using a loop.

Code:
vowels = ['a','e','i','o','u']
length = len(vowels)

for i in range(length):
    print(vowels[i])


similarly , we can use loops to iterate over strings Let’s have a look at this code:

Code:
str = "Hi There, I am a Human"
for i in str:
    print(i)


You can do many cool stuff with that like we can count the repetition of a character in string

Code:
# Program to calculate how many times a character is repeated

str = "Sorry, not sorry."
count = 0
for i in str:
    if i == 'r':
        count += 1

print(count)


List Slices:
List slices means accessing a portion of list which we want so for that basic we use ‘:’ between the indices now let’s have a look at that code

Code:
number = [1,2,3,4,5,6,7,8,9]
print(number[3:6])


This will print the element of the list between indices 3 and 6 if you use a negative sign with index it will start from the right side. for example,



Quote:
number[-1] = 9

we can also use the steps for slice for example you want to print an element and ignore the next two and again print an element for that we use steps in list slicing for example.

Code:
number = [1,2,3,4,5,6,7,8,9]

print(number[::3])

if you want to reverse a list use negative value as steps in list slicing. it will start printing from 9 to 1.

Code:
number = [1,2,3,4,5,6,7,8,9]

print(number[::-1])


That’s it for now, we have covered almost each and everything about lists in this thread In upcoming threads we will cover some other cool concepts of Python. 



" Keep Good care of your Health
 Allah Hafiz and Good Bye Till the next post"
"Keep Learning and Keep exploring"
Love me like you do Heart
Amazing, Thanks for a very detailed tutorial ✅🖤
Rs
* Thankful to Allah *
Kurdy
(04-23-2022, 05:42 PM)Mr.Kurd Wrote: Amazing, Thanks for a very detailed tutorial ✅🖤
Thanks for your precious comment ❣️
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,249 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,639 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,675 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,844 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,608 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,842 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,793 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,833 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,826 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,828 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)