2. Accessing List Using Slicing Operator
Slicing ααα΅ α¨α αα΅α main list sublist ααα α¨αααααΈαα elements ααα°α α¨αααα£α α΅ αααα΅ ααα’
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
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
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
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
Sololearn
Programming Jokes!!! | Sololearn: Learn to code for FREE!
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
The bartender says, βIβll have to charge you extra, thatβs a root beer floatβ.
So the guy says
Exercises
Deepen Your Knowledge About List Indexing and Slicing Through These Questionsπ
1β£. What will be the output of the following code?
a)
b)
c)
d)
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
, what does
return?
a)
b)
c)
d)
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?
a)
b)
c)
d)
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
a)
b)
c)
d)
my_list?a)
my_list[::-1] b)
my_list.reverse() c)
reversed(my_list) d)
my_list[:-1]5β£. What does the slice
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
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?
a)
b)
c)
d)
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
, what does
return?
a)
b)
c)
d)
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
, what does
return?
a)
b)
c)
d)
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?
a)
b)
c)
d)
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?
a)
b)
c)
d)
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 αα΅α¨α α₯αα½αααα’
ααα³α : my_list α ααα list αα΅α₯ index 2 αα α«ααα element('cap') αα° 'book' α₯ααααα’
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 αααα α¨αααα
to be cont'd...
@python_pioneers
@python_pioneers
α¨ α αα΅α 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).
ααα³α : α¨ α¨αα 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
α¨25 α₯α α¨α α¨α α°αα΅ α¨αα«αΈαα α₯α«α
If you're ready show me your π
β 3 : 15
@python_pioneers
If you're ready show me your π
β 3 : 15
@python_pioneers
π11
Python Pioneers: A Beginner's Guide πͺπΉ
α¨25 α₯α α¨α α¨α α°αα΅ α¨αα«αΈαα α₯α«α If you're ready show me your π β 3 : 15 @python_pioneers
Please send me only your answers.
Send it to : @Mntsnt_ZP
The winner will be the person who gets the most correct answers in the shortest amount of time.
Good Luck!π€
@python_pioneers
Send it to : @Mntsnt_ZP
The winner will be the person who gets the most correct answers in the shortest amount of time.
Good Luck!π€
@python_pioneers
π2
1. What is the output of the following code?
A) [1, 2, 3]
B) [2, 3, 4]
C) [2, 3, 4, 5]
D) [1, 2, 3, 4]
2. What is the result of running the following code?
A) [5, 4, 3, 2, 1]
B) [1, 2, 3, 4, 5]
C) [5, 4, 3, 2]
D) [4, 3, 2, 1]
3. What will be the output of the following code snippet?
A) [1, 2, 6, 4, 5]
B) [1, 2, 3, 6, 5]
C) [1, 2, 6, 3, 4]
D) [1, 2, 3, 4, 6]
4. What does the following code do?
A) Removes the element at index 2 from the list
B) Removes the last element from the list
C) Adds the element at index 2 to the list
D) Replaces the element at index 2 with a new value
5. How can you check if a specific value exists in a list in Python?
A) Using the in keyword
B) Using the exists() method
C) Using the exists keyword
D) Using the contains() method
#Quiz
#PrizeWinningQuiz
@python_pioneers
@python_pioneers
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4])
A) [1, 2, 3]
B) [2, 3, 4]
C) [2, 3, 4, 5]
D) [1, 2, 3, 4]
2. What is the result of running the following code?
my_list = [1, 2, 3, 4, 5]
new_list = my_list[::-1]
print(new_list)
A) [5, 4, 3, 2, 1]
B) [1, 2, 3, 4, 5]
C) [5, 4, 3, 2]
D) [4, 3, 2, 1]
3. What will be the output of the following code snippet?
my_list = [1, 2, 3, 4, 5]
my_list[2] = 6
print(my_list)
A) [1, 2, 6, 4, 5]
B) [1, 2, 3, 6, 5]
C) [1, 2, 6, 3, 4]
D) [1, 2, 3, 4, 6]
4. What does the following code do?
my_list = [1, 2, 3, 4, 5]
del my_list[2]
A) Removes the element at index 2 from the list
B) Removes the last element from the list
C) Adds the element at index 2 to the list
D) Replaces the element at index 2 with a new value
5. How can you check if a specific value exists in a list in Python?
A) Using the in keyword
B) Using the exists() method
C) Using the exists keyword
D) Using the contains() method
#Quiz
#PrizeWinningQuiz
@python_pioneers
@python_pioneers
π1