You are on page 1of 3

INDEX

1. List and Tuples


2. List methods
3. List slicing
4. Dictonary
5. Dictonary function
6. Sets in python

## Lists and tuples

1. List example --> var1 = ["Balveer Singh", "Raghuveer", "Ranveer", 56, 17]

2. Accessing List --> print(var1) // print whole lIst


3. Access List index --> print(var1[0]) // print "Balveer singh"
4. List is mutable can change but tuple is not, tuple starts with small brackets()

## List Methods

1. Example --> var1 = [6, 5, 9, 1, 7]


These sort and reverse type funcs changes the orignal lists
var1.sort(); print(var1) --> sort the list
var1.reverse(); -snip- --> reverse the list
var1.sort().reverse() --> can be used both to first sort and then reverse

2. print(max(var1)) --> This will print max number in the list


print(min(var1)) --> This will print minimum number in the list

3. var1.append(9) --> This will append 9 at the end of list


4. var1.insert(1, 67) --> This will insert 67 at the index of 1
5. var1.remove(9) --> remove number 9 from the list
6. var1.pop() --> remove the last element or number from the list

7. var1[0] = 90 --> It will change the value of 0 index to 90

## List Slicing

1. List slicing is same as the string slicing

## Dictionary : Key value pair


Dict can contain list, tuple, dict and all
1. Example : Normal Dict
var1 = {"Happy":"Life", "sad":"me", "Rest":"Neutral"}

2. Example : Nested Dict


var1 = {"Happy":"Life", "sad":"me", "Rest":"Neutral", "nD":{"hello":"hi",
"420":googbye}}

3. Example: Accessing items of dict


print(var1["Happy"]) --> Output will be "Life"
print(var1["nD"]["hello"]) --> --snip-- "hi"

4. Adding items to DIct


var1["hardeep"] = "Singh" --> It will add key value pair of this to existing
dict var1

5. Delete items or key value pair from dict


del var1["Happy"]

## Dictionary Functions

1. var2 = var1.copy() // It will create a copy of var1 dict so no


change will done if changes
happen in var2 as it is a copy of var1

2. var2 = var1 // If changes happen in var2 and it will made in


var1 also.

3. print(var1.get("Happy")) // It will print the value or Happy

4. var1.update({"Acer":"Nitro"}) // It will add this key value pair at the end of


var1 dict

5. print(var1.keys()) // Print out all keys from var1 dict

6. Print(var1.items()) // Print all key value pairs of dict called


items.

## Sets in python

Sets can only contain unique values


Example:

1. var1 = set() // this is empty set


2. var1_From_list = set([1, 2, 3, 4]) // out will be a set (set from list)
3. another_var = [1, 2, 3, 4]; var1_From_list = set(another_var) // another set
from list
4. var1.add(1) // it will add 1 in empty set or existing set
5. var1.remove(1) // it will remove 1 from set

Many more sets function but not much usefull


var1.union() and var1.intersection() // Research more when it is needed

You might also like