Tags
python
Asked 7 years ago
12 Dec 2016
Views 936
python

python posted

Difference between Lists, Tuples, Sets & Dictionaries in python

i am good at php but not at python . so i want to Difference between Lists, Tuples, Sets & Dictionaries in
python ,

List

list=[1, 4, 9, 16, 25]


Tuples

list=("data",15,"awesome")


Sets

list=set("awesome")


Dictionaries

tel = {'jack': 4098, 'sape': 4139}


i can see syntax difference between but what are the other main difference between Lists, Tuples, Sets & Dictionaries in python
iPhone-coder

iPhone-coder
answered Apr 24 '23 00:00

Python has four main collection data types: lists, tuples, sets, and dictionaries. Each has different characteristics that make them useful for different purposes. Here's a brief overview of each:

1.Lists: Lists are ordered collections of items that can be changed (mutable). This means you can add, remove, or modify items in a list. Lists are created using square brackets and commas to separate items. For example:



my_list = [1, 2, 3, 4]

2.Tuples: Tuples are similar to lists, but they are immutable. Once created, you cannot change them. Tuples are created using parentheses and commas to separate items. For example:



my_tuple = (1, 2, 3, 4)

3.Sets: Sets are unordered collections of unique items. They cannot contain duplicates. Sets are created using curly braces or the set() function. For example:



my_set = {1, 2, 3, 4}
my_set = set([1, 2, 3, 4])

4.Dictionaries: Dictionaries are unordered collections of key-value pairs. Each key in a dictionary must be unique, and the keys are used to access the corresponding values. Dictionaries are created using curly braces and colons to separate key-value pairs. For example:



my_dict = {'apple': 1, 'banana': 2, 'orange': 3}

To summarize, lists and tuples are ordered collections that differ in their mutability, with lists being mutable and tuples being immutable. Sets and dictionaries are unordered collections, with sets containing only unique items and dictionaries consisting of key-value pairs.




Post Answer