Part - 1 πŸ“ƒ~ Lists in Python ~πŸ“ƒ

Part - 1 πŸ“ƒ~ Lists in Python ~πŸ“ƒ

Β·

5 min read

A very fun thing to work with imo. Really makes it easier when grouping things up and you WILL be using this extensively when you're all professionals working as Data Scientists ehehe (and future billionaires) :D ...... But let's start from the beginning. SO!~


To give you all a fair bookish definition before getting into it,

"In Python, a list is a collection of items that can be of any type (such as integers, strings, or other objects) and is represented by square brackets. It allows for easy manipulation of the items within the list, such as adding, removing, or accessing elements."

Now one IMPORTANT thing to note here, which also explains the title of this blog, is that -> Python can make lists comprising of different Data Types.

For example :-

my_list = [1, 'hello', 3.14, True] 

print(my_list)

This will print out

[1, 'hello', 3.14, True]

without any error, even when the list comprises one element of each data type, i.e., integer, string, float, and boolean.

πŸ“Length of the list ->

The length of any list means the number of elements that are there in our list, pretty straight forward to find this as well but,

✨ The more we know ✨

Anyways,

print(len(my_list))

4

Simple, easy. Olrighttt~

Indexing ->

Indexing basically refers to the position of the elements inside a list.

🚩 The First Element Starts With 0 as its index and then it moves further up 🚩

so in our list, my_list, I'll show you what position is assigned to what element,

Indexing

Elements

0

1

1

'hello'

2

3.14

3

True

We can also print out specific elements from the list from their indexing, such as :-

print("First entry: ", my_list[0])
print("Third entry: ", my_list[2])

And this will give us :-

First entry: 1

Third entry: 3.14

βœ‚οΈ Slicing ->

To put it simply, Slicing in Python is a mechanism that allows you to extract a subset of elements from a sequence object such as a list. It is basically a way of accessing a range of elements from the sequence object without modifying the original object.

The syntax to use slicing is :-

sequence[ start : stop : step ]

If the definition sounded too professional and you don't get the syntax, it will all be clear with an example~! But first, we will take a new nice and long list for this cause it'll make it much easier to understand. So~!

new_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Now, we will perform various functions of slicing on it so each and everything is clear as to how it is used.

print(new_list[0:5])

OR

print(new_list[:5])

#They are the same thing because when no starting point is given, the slicing just starts from the element at 0 index

Now a really important thing to remember,

🚩 We exclude the element at which the index is ending 🚩

The explanation for this is understood by the syntax of slicing that I mentioned above, the last element is where the slicing "stops", so that's why the last element is excluded. So, the output of the above slicing will be

[1, 2, 3, 4, 5]

even when the slicing shows 6 elements from index 0 to 6.

-> For another example :-

print(new_list[2:6]) #Print element from index 2 to 6 EXCLUDING THE ELEMENT                          AT INDEX 6

So looking at our indexing, the output will be :-

[3, 4, 5, 6]

Similarly, as before, we excluded the element "7" which was present at 6th index and printed the rest.

-> Now looking at skipping :-

print(new_list[0:9:2])

this means taking two steps and printing, which also means printing every other element.

Output : [1, 3, 5, 7, 9]

I'll explain this with a badly drawn example but it will be clear :p

The slicing starts at 0 so the first element gets printed, i.e. '1'

Now, consider every arrow as a step, goes up, step 1, comes down, step 2. And the element at which the arrow lands, gets printed, so the element at index 2, i.e. '3' and so on.

If the step was of 3, like

print(new_list[0:9:3])

Then the steps would look something like :-

You get my point, moving on to other example, we will take another new list, but this time, it will be a list containing strings.

important thing to remember in string as a list -

🚩 Spaces and symbols each have their own index and are counted as elements. 🚩

and since we know how slicing works, we will understand this easily. πŸ‘¨β€πŸŽ“

my_string = "Hello, World!" 

print(my_string[0:5]) 

print(my_string[7:13])  

print(my_string[::2])

output 1 :- "Hello"

output 2 :- "World!"

output 3 :- "Hlo ol!"


Welp! That's all the time I have for now, I will pick up lists and finish a small part that is remaining and we will then be officially done with the intro to Python! Excited to see how we all proceed to Advance Python together real soon! Thanks to everyone who read it and made it till here! πŸ’œ If this blog even solved one doubt you might've had, its purpose is fulfilled. Thanks for reading everyone! :D

And as always~~~~

Consistency is the Key πŸ—


Peace~πŸ•ŠοΈ

Yash K.

Β