0% found this document useful (0 votes)
2 views44 pages

Python part (2)

The document discusses Python variables, emphasizing that they store references to objects rather than values, and explains the importance of using variables for dynamic data handling. It clarifies that Python has no primitive data types, as everything is an object, and outlines the implications of this design choice. Additionally, it covers concepts like object identity, the use of None, and the differences between objects and values in Python.

Uploaded by

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

Python part (2)

The document discusses Python variables, emphasizing that they store references to objects rather than values, and explains the importance of using variables for dynamic data handling. It clarifies that Python has no primitive data types, as everything is an object, and outlines the implications of this design choice. Additionally, it covers concepts like object identity, the use of None, and the differences between objects and values in Python.

Uploaded by

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

Python part (2):

Question 1) Q1) IN pytjon variable assigning first one literal there: like 10, 3.2 or some string etc we
assign it to variable like x = 10 or x = "geeks" why there is a situation which we need to use varibsles ,
directly we can use literals only right? please explain it, is it like input can be many literals or different
how you can write there should one container which can store anything here what happens is some
literal which came is stoere in meomeory at some location of bytes like 0x654 this is the starting byte
or bit of that and x holds the whole bytes or bits , and it is refrence to that location right? or how
explain htis in depth please // I want to clearly understand how things happen in sequence please
and form one memoery location refence how the same varibale sog oto other location as refrence x
= 10 x = "geeks" it will go to geeks right? go in dpeth and explain please if possible show me how
integer stores in real memeory, clearly please in depth in bits please and when you do some
operation how it changes in bits operations

Ans:

So why variables?

🔹 Variables exist because:

1. Values change

2. Values are reused

3. Values come from input / computation

4. Programs need names, not raw numbers

Example:

Here:

 You don’t know the literal beforehand

 You need a container/name to refer to the value later

So yes — your intuition is correct:

“There should be one container which can store anything”

In Python, that “container” is a name bound to an object.

2️⃣ Very important rule (Python mental model)


❗ Python variables do NOT store values

❗ Python variables store references to objects

This is the single most important idea.


⚠️Key insight

Variables don’t move objects


Variables move references

5️⃣ Why does Python work this way?

Because:

 Safe (no accidental memory overwrite)

 Flexible (dynamic typing)

 Efficient for object reuse


🔟 Final mental model (MOST IMPORTANT)

Python execution model:

1. Literal appears

2. Object is created in heap

3. Variable name is bound to object

4. Reassignment = name points elsewhere

5. Objects never change if immutable

6. Bits change only when new object is created

🧠 One-line summary

Python variables are names pointing to objects; objects live in memory; reassignment moves the
name, not the object; integers are immutable and stored as arbitrary-precision binary digits.

Questions:

Q2) in python what is considered Boolean true or True like x = true or x = True which is Boolean
which is string? please tell me IN c++, 1 is considered as true and 0 as false , somehting like that
there in python also, if yes tell me? show some exampels please

Ans:
Questions:

I heard in python there is no primiti types, everyhting is class, like integer or anyhting so there is
nothing like data type concept right? how many classes are there in python tell me some important
ones

Ans:

1️⃣ “Python has no primitive types” — is this TRUE?

✅ Conceptually TRUE

❌ Practically MISLEADING if not explained

Correct statement:

Python has no primitive data types like C/C++.


Everything in Python is an object (instance of a class).

That means:

2️⃣ Then does Python have “data types” or not?

✅ YES — Python has types

❌ NO — Python does NOT have primitive vs non-primitive distinction

In Python:

 Type = Class

 Object = Instance of class

So when people say:

“There are no data types in Python”

They actually mean:

“There are no primitive data types”


6️⃣ Are Python types slower because of this?

Yes — slightly slower, but:

 Safer

 More flexible

 Easier to reason about

 Garbage-collected

This is a design tradeoff.

7️⃣ How many classes are there in Python?

There is NO fixed number because:

 Built-in classes

 User-defined classes

 Third-party classes

But Python has ~100+ built-in classes


Let’s classify the important ones 👇
🧠 Final mental model (IMPORTANT)

Python has types, implemented as classes.


There are no primitive types.
Every value is an object.
Every object has a type (class).
Every class ultimately inherits from object.

One-line summary

Python has no primitives; “data types” exist but they are classes; everything is an object, including
numbers, functions, and even classes themselves.

Questions:

Q4) We will discuss about swapping two varibales basic program, we all know using 3 variable we can
do but without using 3 variable can we do? and also not to use any binary opertors show swapping
between two different dat types like string an dinteger and same types

Ans:
7️⃣ Can this be done in C / Java?

❌ NO (directly)
Because:

 They assign one variable at a time

 RHS is not evaluated as a tuple

Python’s feature is called:

Multiple assignment / unpacking

🔟 One-line mental model (VERY IMPORTANT)

Python swaps variables by rebinding names using a temporary tuple, not by moving or modifying
the objects themselves.

Questions:

Q6) Explain about id() method in python, how it works and what it do, what is the use case? explain
with good example how it works inernally a = 10 b = 10 id (a) and id(b) will be same id method gives
you id of the object not varibales, here object means what? can a literal be object? In python literals,
are sotered at same location, if they have smae value i mean a = 10 b = 10 so 10 where it is stored, a
and b pointing to smae adrres right? like pointers cpython implementation gives you memory
address, what about jython

Ans:

❓ What is an “object” in Python?

An object is:

 A chunk of memory

 With:
o Type

o Value

o Reference count

o Methods

Yes:

✅ A literal IS an object in Python


5️⃣ Are all literals stored at the same location?

❌ NO — very important misconception

Truth:

 Some literals may be reused


 Some are not

 This depends on:

o Type

o Value

o Implementation

Examples:

Integers

Why?

 String interning is selective

 Only identifiers / short strings often cached


❌ Bad use cases

 Comparing values

 Storing permanently

 Pointer arithmetic

 Logic decisions

11️⃣ Final mental model (IMPORTANT)

id() identifies an object, not a variable.


Literals are objects.
Variables are names pointing to objects.
CPython uses memory address as id, but this is not guaranteed across Python implementations.

One-line summary

id() returns the unique identity of an object during its lifetime; in CPython this is the memory
address, but conceptually it’s just an opaque object identifier.

Questions:

Q7) then i have one doubt, you are saying varibales ar refrence a = 10 b = 10 if i do b++ then a should
also be changed right, because value in memory becomes increased , wheras a also points ot smae ?
or not what happens here? in c++ it will change , what is caing this difference
Ans:

Excellent doubt — this question shows you’re thinking correctly, and the confusion is very common
when coming from C/C++.

Short answer first:

❌ a will NOT change in Python


because integers are immutable and variables are references, not memory cells

Now let’s go slow, precise, and deep, and also fix one important syntax issue.

2️⃣ Your reasoning (very logical, but C-style)

You’re thinking:

a and b point to same memory


b increases value in memory
a should also see the change

This would be 100% correct in C++.

But Python does NOT work like that.


Questions:

Q8) is operatoe in python, checks two opernads have same identifere or not right? why it is used,
when we will use this if two variable are active at the same time, they can th ave same difftifers
means? what it can be right we have seen example also Q9) in pyhton data type is associated with
actual objects or values i want to know difference between objects and values , what we can call as
object and more type of the object which this variable refres to

Ans:
8️⃣ One clean rule to remember

Objects have types and values.


Variables have no type — they only reference objects.
is checks object identity, == checks value equality.

Final one-line summaries

Q8:
is checks whether two names point to the same object, not whether values are equal.

Q9:

In Python, data types belong to objects, not variables; an object is a runtime entity with identity,
type, and value.

If you want next:

 🔍 is vs == real-world bugs

 🧠 Object lifecycle (creation → deletion)

 ⚙️Mutable object traps

 🧪 Memory diagrams practice

Questions:

Explain about None type in python, how it is sued in DSA and in real software coding in which
situations we need to be careful to avoid errors?

Ans:
🔟 Mental model (VERY IMPORTANT)

None means “no object / absence of value”, not “zero” or “false”.


It is a singleton object used as a sentinel in Python.

One-line summary

None is Python’s explicit “no value” object, heavily used in DSA for termination and in real
software to represent missing or optional data—and must always be checked using is None.

You might also like