Python - Lesson 1
Kannan Piedy
IIM | Python, Data science | Technical Lead and Manager with experience leading large teams and building large projects.
Python, for those who don't know is a general purpose programming language. It is a high level language , which in layman terms means that it was built in such a way that you can understand the code easily.
Which kind of makes sense as how it is described in python.org as
Python is a programming language that lets you work more quickly and integrate your systems more effectively.
A software is essentially a bunch of instructions written in a language telling the system what it should do on any particular action done by the user ; ie YOU.
For those who don't like to read a lot of theory and want to directly nose dive into the metaphoric mouth of Python , you can visit : https://www.souravsengupta.com/cds2015/python/LPTHW.pdf
Python comes pre-installed in Ubuntu and I would urge you to start using Linux as your primary choice of learning Python for the reminder of the lessons.
For anyone who needs to learn programming, let me teach you the way I learnt it. Let us write a set of instructions which when executed makes the program talk to us.
Let us call this action performed by the system as the standard output operation.
print "Hello World"
When the Python Interpreter reads this instruction, Yes there is an interpreter which actually interprets these instructions to the system. You see , we all speak different languages so the interpreter helps the system understand us. Now, When the Python Interpreter reads this instruction it understands there exist 2 objects.
The objects are print and "Hello World" respectively. These two are objects of 2 specific types. They have their own definitions within the Python language. The Object print, is a reference to a module (I'll come back to this in later chapters) and the Object "Hello World" is a "String".
A String is a bunch of characters referenced by a single name. However in this case "Hello World" does not have a label, hence its lifetime within the Systems memory ceases to exist at the end of execution of this statement. This is one of the primary things you need to learn about Python, If there is an Object without a label, then it will be deleted almost immediately.
The String Object Type is what we usually call as an Immutable object, which means this type of object has no function that can change its value. In Other languages the String is simply a character array which can be altered, but not in python. In Python a String Object cannot be modified.
Notice the code below :
A = "Hello World"
A[0] = "h"
Here we created the String Object "Hello World" and assigned a label 'A' to it. We have attempted to replace the first character of the String object "H" with "h". The code would certainly lead to an error.
TypeError: 'str' object does not support item assignment
The reason as mentioned before is because the string object in python once created cannot be changed.
Now let us look at another case :
A = "Hello World"
B = "Hello World"
This case scenario used above showcases not a change of the object rather an additional label has been added to the same Object.
Now let us look at another case :
A = "Hello World"
A = "The brown fox"
This case scenario used above showcases not the change in the String Object, but rather the same label has been reassigned to another object. It is prudent to note that, if there exists no other label pointing to "Hello World" then the String Object "Hello World" will be deleted from memory.
I hope I explained very clearly the way Python handles string objects and How to assign a string object in Python.
To be contd. (see you in part 2)
Lead Data Engineer
6 年Very precise and easy to understand. I like your attempt to explain the insights of Python which only few people know and put in efforts to understand. Other sites focus on the syntax and power of the language explaining 'how to code using Python' i appreciate your effort to explain 'the whys' this helps to understand and learn the language naturally and easily.