Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5d079f7

Browse files
author
Amogh Singhal
authored
Create Python_Programming_Quiz.md
Added q1: difference between list and tuple
1 parent 382456c commit 5d079f7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎Python_Programming_Quiz.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#### 1. When do you use a list in place of tuple ? <br>
2+
3+
##### What are tuples ?
4+
A tuple is a data structure in-built in Python to store data, similar to arrays. <br>
5+
The syntax to declare a tuple is <br>
6+
``` location = (28.99, 51.34)``` <br>
7+
We use parenthesis `()` to declare a tuple <br>
8+
9+
In order to access a element, we use **indexing**. <br>
10+
``` latitude, longitude = location[0], location[1]```
11+
12+
Tuples also make use of **slicing**
13+
```
14+
fruits = ("apple", "banana", "orange")
15+
last_fruit = fruits[:-1]
16+
```
17+
Tuples can be deleted using the `del` command
18+
19+
##### What are lists ?
20+
Similar to tuple, lists is another built-in data structure of Python to store data <br>
21+
The synatx to declare a list is <br>
22+
`breakfast = ["bread", "butter", "eggs"]`
23+
24+
Similar to tuples , list also supports **indexing and slicing**
25+
26+
##### Difference between tuple and lists ?
27+
A list is mutable while a tuple is not. By mutabliity, I mean we can change the data stored inside the structure <br>
28+
29+
Everything in Python is a object. So when i declare a list<br>
30+
`numbers = [1,2,3,4,5]` <br>
31+
`numbers` is an object of the type `list` containing numbers 1 to 5 <br>
32+
In case I want to change the second element of the lis to 6, I will do this <br>
33+
`numbers[1] = 6` <br>
34+
`numbers` will now contain `[1,6,3,4,5]`
35+
36+
Doing the same with a tuple will give me an error <br>
37+
`TypeError: ‘tuple’ object does not support item assignment`
38+
39+
Suppose you want to delete a slice of the tuple, you will get the following error <br>
40+
`TypeError: ‘tuple’ object does not support item deletion`
41+
42+
##### Functions and methods to use on both ?
43+
Both the data structures support the following functions: <br>
44+
`len(), max(), min(), sum(), any(), all(), sorted()`
45+
46+
Common methods in list and tuple: `index(), count()` <br>
47+
Methods in list: `append(), insert(), remove(), pop(), clear(), sort(), and reverse()`
48+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /