0% found this document useful (0 votes)
4 views9 pages

Python Notes

This document provides a comprehensive guide on core data types in Python, including strings, lists, tuples, dictionaries, and sets, along with practical examples and important edge cases. It covers slicing, the use of map and filter functions, and common logic patterns for lab questions. Additionally, it highlights common mistakes and final memory rules to help users understand mutable vs immutable types effectively.

Uploaded by

goludimpiu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Python Notes

This document provides a comprehensive guide on core data types in Python, including strings, lists, tuples, dictionaries, and sets, along with practical examples and important edge cases. It covers slicing, the use of map and filter functions, and common logic patterns for lab questions. Additionally, it highlights common mistakes and final memory rules to help users understand mutable vs immutable types effectively.

Uploaded by

goludimpiu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

This is the kind of “master sheet” that can seriously level you up fast

I’ll give you a clean, exam-ready + prac cal + edge-case heavy set of notes — the kind that
helps in labs and tricky ques ons.

PART 1: CORE DATA TYPES (PRACTICAL UNDERSTANDING)

STRING (str)

 Immutable (cannot change in-place)

s = "hello"

s[0] = "H" # ERROR

✔ What you CAN do

s = "hello"

s[0] # 'h'

s[-1] # 'o'

s[1:4] # 'ell'

s[::-1] # reverse

✔ Modify via trick

s = "hello"

s = "H" + s[1:] # "Hello"

LIST (list)

 Mutable

l = [1,2,3]

l[0] = 10 # works

✔ Access

l[1]

l[-1]

l[1:3]
TUPLE (tuple)

 Immutable

t = (1,2,3)

t[0] = 10 # ERROR

DICTIONARY (dict)

 Key-value pairs

d = {"a":1, "b":2}

d["a"] #1

[Link]("a") # 1

[Link]("x") # None (safe)

SET (set)

 Unique elements

s = {1,2,3}

PART 2: SLICING (VERY IMPORTANT)

Syntax:

s[start:end:step]

Examples:

s = "abcdef"

s[1:4] # 'bcd'

s[:3] # 'abc'

s[::2] # 'ace'
s[::-1] # reverse

Works on:

 string

 list

 tuple

PART 3: MAP, FILTER (ADVANCED BUT EASY)

map()

map(func on, iterable)

 Takes: func on + iterable

 Returns: map object (iterator)

l = [1,2,3]

result = list(map(lambda x: x*2, l))

# [2,4,6]

filter()

filter(func on, iterable)

 Returns: iterator

l = [1,2,3,4]

result = list(filter(lambda x: x%2==0, l))

# [2,4]

PART 4: SUPER IMPORTANT EDGE CASES

1. Func ons returning None

These MODIFY original list:

l = [3,1,2]
x = [Link]()

print(x) # None

List of such func ons:

 append()

 extend()

 insert()

 remove()

 sort()

 reverse()

 clear()

Rule:

If it modifies list → returns None

2. sorted() vs sort()

l = [3,1,2]

sorted(l) # [1,2,3]

l # unchanged

[Link]()

l # changed

3. split() vs split(" ")

s = "a b c"

[Link]()

# ['a','b','c']
[Link](" ")

# ['a','','b','','','c']

4. Strings are immutable

s = "abc"

[Link]("a","z") # returns new string

print(s) # s ll "abc"

5. join() expects strings

l = [1,2,3]

"".join(l) # ERROR

✔ Fix:

"".join(map(str, l))

6. Copy vs reference

l1 = [1,2]

l2 = l1

[Link](3)

print(l1) # [1,2,3]

✔ Fix:

l2 = [Link]()

7. Tuple single element trap


t = (5) # int

t = (5,) # tuple

8. Dic onary key rules

 Keys must be immutable

d = {[1,2]: "x"} #

d = {(1,2): "x"} #

9. get() vs direct access

d["x"] # error if missing

[Link]("x") # None

10. in operator

"a" in "apple" # True

2 in [1,2,3] # True

"a" in {"a":1} # checks keys

PART 5: CONVERSIONS (VERY USEFUL)

String → List

list("abc") # ['a','b','c']

List → String

"".join(['a','b']) # "ab"

Tuple ↔ List

t = tuple([1,2])

l = list((1,2))
Dict keys/values

d = {"a":1,"b":2}

[Link]() # dict_keys

[Link]() # dict_values

[Link]() # (key,value)

PART 6: COMMON LOGIC PATTERNS (LAB QUESTIONS)

Count frequency

d = {}

for x in s:

d[x] = [Link](x,0) + 1

Reverse string

s[::-1]

Remove spaces

"".join([Link]())

Max occurring element

max(d, key=[Link])

PART 7: COMMON IIT LAB ERRORS

Mistake 1

l = [3,1,2]

print([Link]()) # None
Mistake 2

s = "abc"

s[0] = "z" # error

Mistake 3

" ".join([1,2]) # error

Mistake 4

for i in range(l): #

✔ Correct:

for i in range(len(l))

Mistake 5

if s.find("a"):

Wrong because -1 is True

✔ Correct:

if s.find("a") != -1:

FINAL MEMORY RULES

GOLDEN SHORTCUTS

 split() → string ➜ list

 join() → list ➜ string

 list methods → return None

 string methods → return new string

 sorted() → new list

 sort() → modifies
LAST THING (REAL UNDERSTANDING)

If you remember just this:

Mutable → changes itself → returns None


Immutable → returns new object

You will solve 80% of tricky ques ons instantly.

If you want next level (seriously killer level):

 I can give you 20 IIT-level tricky ques ons

 or convert THIS into a perfect printable cheat-sheet PDF (be er than before)

 or make a 1-page revision sheet

Just tell me what you want next

You might also like