Python Pioneers: A Beginner's Guide πŸ‡ͺπŸ‡Ή
8.82K subscribers
346 photos
40 videos
24 files
64 links
Welcome to Python Pioneers: Empowering Ethiopian students to embark on their coding journey.

πŸ“’ Channel: @python_pioneers
πŸ‘₯ Group: @python_pioneers_chat
Download Telegram
➑️⏭

Next : Accessing a List in Python

⌚️8 : 30 EAT
...
Accessing Lists in Python

α‰ Python ሡለ list αˆαŠ•αŠα‰΅αŠ“ α‰£αˆ…αˆͺያቡ αˆŒαˆ‹α‹ α‹°αŒαˆž αŠ₯αŠ•α‹΄α‰΅ αŠ₯αŠ•α‹°αˆšαˆαŒ αˆ© αŠ α‹­α‰°αŠ• αŠα‰ αˆ­α’ αŠ αˆαŠ• α‹°αŒαˆž α‹¨αŠ αŠ•α‹΅ list content αŠ₯αŠ•α‹΄α‰΅ access αˆ›αˆ¨αŒ αŠ₯αŠ•α‹°αˆαŠ•α‰½αˆ αŠ₯αŠ“α‹«αˆˆαŠ•α’

List access αˆˆαˆ›αˆ¨αŒ α‹¨αˆαŠ•αŒ α‰€αˆ›α‰Έα‹ α‹ˆα‹° 3 αˆ˜αŠ•αŒˆα‹Άα‰½ αŠ αˆ‰α‘

1. By using indexing

Indexing αˆ›αˆˆα‰΅ α‹¨αŠ αŠ•α‹΅αŠ• list element point α‹¨αˆαŠ“αˆ¨αŒα‰ α‰΅ αˆ˜αŠ•αŒˆα‹΅ αŠα‹α’

Syntax :
list_name[index_number]

Accessing the elements by index αˆ›αˆˆα‰΅, elements α‰ listኑ α‹αˆ΅αŒ₯ α‰£αˆ‰α‰ α‰΅ ቦታ access αˆ²α‹°αˆ¨αŒ‰ αŠα‹α’ Index α‹°αŒαˆž α‹¨αˆšαŒ€αˆαˆ¨α‹ ከ0 αŠα‹α’ α‹« αˆ›αˆˆα‰΅ α‹¨αŠ αŠ•α‹΅ list first element index ቁαŒ₯ሩ 0 αŠα‹α’

List index positive ም negative ም αˆŠαˆ†αŠ• α‹­α‰½αˆ‹αˆα’ Positive index indexing ከግራ α‹ˆα‹°α‰€αŠ αŠ₯αŠ•α‹°αˆ†αŠ α‹­αŠαŒαˆ¨αŠ“αˆ α‰ α‰°α‰ƒαˆ«αŠ’α‹ α‹°αŒαˆž negative index αŠ¨α‰€αŠ α‹ˆα‹° ግራ represent α‹«αˆ¨αŒ‹αˆα’

...to be contd

#AccessingLists
#Indexing

@python_pioneers
2. Accessing List Using Slicing Operator

Slicing αˆ›αˆˆα‰΅ α‹¨αŠ αŠ•α‹΅αŠ• main list sublist α‹ˆα‹­αˆ α‹¨αˆαˆˆαŒαŠ“α‰Έα‹αŠ• elements αˆˆα‹­α‰°αŠ• α‹¨αˆαŠ“α‹ˆαŒ£α‰ α‰΅ αˆ˜αŠ•αŒˆα‹΅ αŠα‹α’

Syntax :

list_name[start : stop : step]

start : Sliceኑ ዬቡ αŠ₯αŠ•α‹°αˆšαŒ€αˆαˆ­ represent α‹«αˆ¨αŒ‹αˆ, default value 0 αŠα‹α’

stop : represents the end index where slice should end.

▫️allow α‹¨αˆšα‹°αˆ¨αŒ max value = (last index + 1 )
β–«οΈαˆαŠ•αˆ value αŠ«αˆα‰°αˆ°αŒ α‹ የ listαŠ‘αŠ• last element index as default α‹­α‹ˆαˆ΅α‹³αˆα’

step : slice α‰ αˆšα‹°αˆ¨αŒ‰ elements αˆ˜αˆ€αŠ¨αˆ α‹«αˆˆα‹αŠ• index value increment represent α‹«αˆ¨αŒ‹αˆ

αŠ αŠ•α‹΅αŠ• list slice αŠ αˆ­αŒˆαŠ• α‹¨αˆαŠ“αŒˆαŠ˜α‹ αˆŒαˆ‹ list αŠα‹α’
i.e. it returns a list.

...ይቀαŒ₯αˆ‹αˆ πŸ‘

#Slicing
#Lists

@python_pioneers
πŸ‘2
Goodnight πŸ˜˜πŸ’€πŸ’€
πŸ‘4
😁😁😁😁😁😁😁😁😁😁

@python_pioneers
😁5πŸ‘Œ2
Programming Jokes!!!

1 : A guy walks into a bar and asks for 1.014 root beers.
The bartender says, β€œI’ll have to charge you extra, that’s a root beer float”.
So the guy says, β€œIn that case, better make it a double.”

2 : 3 Database SQL walked into a noSQL bar
A little while later....
They walked out
Because they COULDN'T FIND A TABLE


3 : The Programmer wife sent him to a grocer store.
Her instructions were "Buy Butter. See if they have eggs, If they do buy 10"

4 : Q: Why do Java Programmers wear glasses?
A: Because they can't C#

Real Programmers don't comment their codes!!
Because If it was hard to write.....
It must be hard to read....

Algorithm : word used by programmers when they do not want to explain
what they did

Keep Calm and LOVE programming!!!!!

Sololearn

#Jokes

@python_pioneers
@python_pioneers
Exercises

Deepen Your Knowledge About List Indexing and Slicing Through These QuestionsπŸ‘‡ and don't forget to comment your answers
1⃣. What will be the output of the following code?


   my_list = [10, 20, 30, 40, 50]
print(my_list[1:4])

a) [10, 20, 30]

b) [20, 30, 40]

c) [20, 30, 40, 50]

d) [30, 40, 50]
2⃣. Given the list

my_list = ['a', 'b', 'c', 'd', 'e']

, what does

my_list[-3:]

return?

a) ['a', 'b', 'c']

b) ['c', 'd', 'e']

c) ['d', 'e']

d) ['e']
3⃣. What will be the output of the following code?


   my_list = [1, 2, 3, 4, 5, 6]
print(my_list[::2])

a) [1, 2, 3, 4, 5, 6]

b) [1, 3, 5]

c) [2, 4, 6]

d) [6, 5, 4, 3, 2, 1]
4⃣. Which of the following will give you a reversed copy of the list my_list?

a) my_list[::-1]

b) my_list.reverse()

c) reversed(my_list)

d) my_list[:-1]
5⃣. What does the slice my_list[1:-1] do?

a) Returns a list with the first and last elements removed

b) Returns the entire list

c) Returns a list with the first element removed

d) Returns a list with the last element removed
6⃣. What will be the output of the following code?


   my_list = [5, 10, 15, 20, 25, 30]
print(my_list[1:5:2])

a) [10, 20]

b) [10, 15, 20, 25]

c) [10, 20, 30]

d) [10, 25]
7⃣. Given the list
my_list = ['p', 'y', 't', 'h', 'o', 'n']

, what does
my_list[:4]

return?

a) ['o', 'n']

b) ['p', 'y', 't', 'h']

c) ['p', 'y', 't', 'h', 'o']

d) ['t', 'h', 'o', 'n']
8⃣. If
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

, what does
my_list[2:8:2]

return?

a) [2, 3, 4, 5, 6, 7]

b) [2, 4, 6, 8]

c) [2, 6]

d) [4, 8]
9⃣. What will be the result of the following code?


   my_list = ['apple', 'banana', 'cherry', 'date']
print(my_list[::3])

a) ['apple', 'cherry']

b) ['apple', 'date']

c) ['banana', 'date']

d) ['apple', 'banana', 'cherry', 'date']
πŸ”Ÿ. What will be the output of the following code?


    my_list = [100, 200, 300, 400, 500]
print(my_list[1:5:3])

a) [200, 400]

b) [200, 500]

c) [200, 300, 400, 500]

d) [100, 300]
πŸ‘3πŸ”₯2
Python Pioneers: A Beginner's Guide πŸ‡ͺπŸ‡Ή pinned Β«Exercises Deepen Your Knowledge About List Indexing and Slicing Through These QuestionsπŸ‘‡ and don't forget to comment your answersΒ»
How to modify specific element of a list?

የ αŠ αŠ•α‹΅αŠ• list specific element α‰₯ቻ αˆˆα‹­α‰°αŠ• modify αˆ›α‹΅αˆ¨αŒ αŠ₯αŠ•α‰½αˆ‹αˆˆαŠ•α’

Syntax :
list_name[i] = new element

ለምሳሌ : my_list α‰ αˆšαˆˆα‹ list α‹αˆ΅αŒ₯ index 2 αˆ‹α‹­ α‹«αˆˆα‹αŠ• element('cap') α‹ˆα‹° 'book' αŠ₯αŠ•α‰€α‹­αˆ­α’

my_list = ['bag', 'pen', 'cap', 'pen']

my_list[2] = 'book'
print(my_list)

#Output : ['bag', 'pen', 'book', 'pen']

Old list : ['bag', 'pen', 'cap', 'pen']
Modified list : ['bag', 'pen', 'book', 'pen']

3. index() method

The index() method returns the position at the first occurrence of the specified item (value).

In Python, a method is a function that is associated with a class. It is defined inside the class and can be called using an instance of the class. Methods can perform operations on the data stored in the class and can also modify the state of the object.

ለምሳሌ : የ αŠ¨α‹šαˆ… list የ4αŠ• index αˆˆαˆ›α‹ˆα‰… αŠ¨αˆαˆˆαŒαŠ•

new_list = [1, 5, 6, 4, 7]

index_of_4 = new_list.index(4)

#Output : 3

to be cont'd...

@python_pioneers
@python_pioneers
πŸ€·β€β™‚1