TGViewer
Channel Public Channel
Python Learning

Python Learning

@python_bds

Python learning resources

Beginner to advanced Python guides, cheatsheets, books and projects.

For data science, backend and automation.
Join πŸ‘‰ https://rebrand.ly/bigdatachannels

DMCA: @disclosure_bds
Contact: @mldatascientist
Subscribers
5.74K
Photos
582
Videos
2
Links
130

Showing posts older than #1147 Β· Back to latest

Older Posts 20 shown
Post #1146 1.73K
πŸš€ List vs Tuple in Python

Both store collections of data. 
But they differ in mutability and internal behavior.

1️⃣ List (Mutable) πŸ“¦

Can be modified after creation.

nums = [1, 2, 3]
nums.append(4)
print(nums)

Output:
[1, 2, 3, 4]
➀ How: Stored as a dynamic array 
➀ Wins: Flexible, easy to modify 
➀ Risk: Slightly higher memory usage 

2️⃣ Tuple (Immutable) πŸ”’

Cannot be modified after creation.

nums = (1, 2, 3)
nums.append(4)

Output:
AttributeError: 'tuple' object has no attribute 'append'
➀ How: Fixed-size structure 
➀ Wins: Faster iteration, lower memory usage 
➀ Risk: No modification allowed 

πŸ’‘ Key Difference

β€’ List β†’ Mutable & flexible 
β€’ Tuple β†’ Immutable & lightweight 

Use List when data changes. 
Use Tuple when data should stay constant.
  • ❀ 5
Post #1144 1.02K
🧠 LEGB Rule in Python (Scope Resolution)

When Python looks for a variable, 
it follows a fixed order called LEGB.

L β†’ Local 
E β†’ Enclosing 
G β†’ Global 
B β†’ Built-in 

Python searches in this order.


1️⃣ Local Scope (L) πŸ“ 
Variables defined inside a function.

x = 10

def func():
    x = 5
    print(x)

func()

Output:
5
➀ Python finds x inside the function first.


2️⃣ Enclosing Scope (E) πŸ”„ 
Variables in outer function (nested functions).

def outer():
    x = 20
    def inner():
        print(x)
    inner()

outer()

Output:
20
➀ Python finds x in the enclosing function.


3️⃣ Global Scope (G) 🌍 
Variables defined outside all functions.

x = 30

def func():
    print(x)

func()

Output:
30
➀ Python uses global x.


4️⃣ Built-in Scope (B) βš™οΈ 
Predefined names like len, print, etc.

print(len([1, 2, 3]))

Output:
3

πŸ’‘ Search Order

Local β†’ Enclosing β†’ Global β†’ Built-in 

Python stops searching once it finds the name.
  • ❀ 3
Post #1143 1.85K
Python Memory Model
  • ❀ 7
Post #1141 984
πŸš€ Shallow Copy vs Deep Copy in Python

When copying objects in Python, behavior changes for nested data structures.


πŸ“Œ Shallow Copy

A shallow copy creates a new outer object 
but inner objects remain shared references πŸ”— 

import copy

original = [[1, 2], [3, 4]]
shallow = copy.copy(original)

shallow[0].append(99)

print(original)
print(shallow)


Output:
[[1, 2, 99], [3, 4]]
[[1, 2, 99], [3, 4]]


πŸ‘‰ Nested objects are shared.


πŸ“Œ Deep Copy

A deep copy creates a completely independent copy 
including all nested objects 🧠 

import copy

original = [[1, 2], [3, 4]]
deep = copy.deepcopy(original)

deep[0].append(99)

print(original)
print(deep)


Output:
[[1, 2], [3, 4]]
[[1, 2, 99], [3, 4]]


πŸ‘‰ No shared references.


🎯 Quick Difference

β€’ Shallow β†’ Copies outer layer only 
β€’ Deep β†’ Copies entire structure 
β€’ Use deep copy for nested data
  • ❀ 4
Post #1140 854
πŸ”₯ is vs == in Python

Many developers confuse these two operators. 
But they check completely different things.

πŸ“Œ == (Equality Operator)

== checks whether values are equal.

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)


```text
Output:
True


πŸ‘‰ Values are the same 
πŸ‘‰ Even though they are different objects


πŸ“Œ is (Identity Operator)

is checks whether two variables point to the same object in memory.

python
a = [1, 2, 3]
b = [1, 2, 3]

print(a is b)


text
Output:
False


πŸ‘‰ They are stored at different memory locations 
πŸ‘‰ So identity is different


πŸ“Œ When is returns True

python
x = [1, 2, 3]
y = x

print(x is y)


text
Output:
True`


πŸ‘‰ Both reference the SAME object

🎯 Quick Difference

β€’ == β†’ Compares values 
β€’ is β†’ Compares memory identity 
β€’ Use == for value comparison 
β€’ Use is mainly for None checks
  • ❀ 5
Post #1138 1.05K
What's the output of the code snippet?
  • ❀ 2
Post #1137 972
🧠 Garbage Collection & Reference Counting in Python

Python automatically manages memory. 
Objects are deleted when they are no longer used.

πŸ“Œ Reference Counting

Every object tracks how many variables point to it. 
When the count becomes 0, it is removed.

a = [1, 2, 3]
b = a

del a
del b


Reference count:
a β†’ 1
a, b β†’ 2
after del β†’ 0
Object deleted


πŸ“Œ Circular Reference Problem

Two objects pointing to each other 
will never reach reference count 0.

class Node:
    def __init__(self):
        self.ref = None

a = Node()
b = Node()

a.ref = b
b.ref = a

del a
del b


Both objects still reference each other
Reference count β‰  0


πŸ“Œ Garbage Collector (GC)

Python’s GC:
β€’ Detects unreachable cycles 
β€’ Breaks circular references 
β€’ Frees memory safely 

🎯 Core Idea

Reference Counting β†’ Main mechanism 
GC β†’ Handles circular references
  • ❀ 4
Post #1136 850
Pass by Object Reference in Python

Is Python pass-by-value or pass-by-reference? 

➑️ Actually, Python uses Pass by Object Reference.

That means:
The function receives a reference to the same object.

πŸ“Œ Example: Mutable Object

def modify(lst):
    lst.append(4)

nums = [1, 2, 3]
modify(nums)

print(nums)


Output:
[1, 2, 3, 4]


πŸ‘‰ The original list changed 
Because the function received a reference 
to the SAME object in memory.


πŸ“Œ Example: Immutable Object

def modify(x):
    x = x + 1
    print("Inside:", x)

num = 10
modify(num)

print("Outside:", num)


Output:
Inside: 11
Outside: 10


πŸ‘‰ Integer did not change 
Because integers are immutable 
Reassignment creates a new object.


🎯 Core Idea

β€’ Functions receive object references 
β€’ Mutable objects can be modified 
β€’ Immutable objects create new objects 
β€’ Python is NOT pure pass-by-value 
β€’ Python is NOT pure pass-by-reference 

βœ… It is Pass by Object Reference.
  • ❀ 4
Post #1135 835

Forwarded from Programming, data science, ML - free courses by Big Data Specialist

Introduction to Computer Science and Programming Using Python (MIT OCW)

This course teaches computational thinking and Python programming from the ground up. It is rigorous, university level, and excellent for developers who want strong fundamentals rather than just syntax. Completely FREE and highly respected course.

πŸ“š 12 lectures with assignments
⏰ Duration: 9–12 weeks
πŸƒβ€β™‚οΈ Self Paced
Created by πŸ‘¨β€πŸ«: MIT Professors
πŸ”— Course Link

#Python #ComputerScience #MIT
βž–βž–βž–βž–βž–βž–βž–βž–βž–βž–βž–βž–βž–βž–
πŸ‘‰ Join @bigdataspecialist for more πŸ‘ˆ
MIT OpenCourseWare Introduction to Computer Science and Programming in Python | Electrical Engineering and Computer Science | MIT OpenCourseWare _6.0001 Introduction to Computer Science and Programming in Python_ is intended for students with little or no programming experience. It aims to provide students with an understanding of the role computation can play in solving problems and to help students…
  • ❀ 2
Post #1133 1.16K
What's the output of the code snippet?
Post #1131 1.3K
What's the output of the code snippet?
Post #1130 2.34K
10 Python Built-in Functions You Should Know
  • ❀ 2
Post #1127 1.26K
9 Hard Truths About Learning Python
  • ❀ 3
Older posts β†’
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook β†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 β†’