PYTHON Programming (CST310)
Lecture 2:
Introduction to PYTHON Programming,
Installation,
Identifiers
Variables
Department of Computer Science and Engineering
National Institute of Technology, Srinagar, Jammu and Kashmir
August 12, 2025
Python Programming Language
• Python is a general-purpose interpreted, interactive, object-oriented,
and high-level programming language.
• It was created by Guido van Rossum during 1985- 1990.
• Like Perl, Python source code is also available under the GNU General
Public License (GPL).
Used by:
• Google, Yahoo!, Youtube, Netflix, Facebook, Instagram
• Many Linux distributions
• Games and apps (e.g. Eve Online)
Few Features of Python
• Python is Interpreted − Python is processed at runtime by the interpreter. You do not
need to compile your program before executing it. This is similar to PERL and PHP.
• Python is Interactive − You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
• Easy to read and Learn: Python has few keywords, simple structure, and a clearly defined
syntax.
• Python supports Object-Oriented − Python supports Object-Oriented style or technique
of programming that encapsulates code within objects.
• Python is a Beginner's Language − Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from simple
text processing to WWW browsers to games.
• A broad support for libraries
• Portable − Python can run on a wide variety of hardware platforms and has the same
interface on all platforms.
• Databases: Python provides interfaces to all major commercial databases.
• GUI Applications can be designed in python using libraries like Tkinter.
History of Python
• Python was developed by Guido van Rossum in the late eighties and
early nineties at the National Research Institute for Mathematics and
Computer Science in the Netherlands.
• Python is derived from many other languages, including ABC, Modula-
3, C, C++, Algol-68, SmallTalk, and Unix shell and other scripting
languages.
• Python is copyrighted. Like Perl, Python source code is available
under the GNU General Public License (GPL).
Installing Python
• Python comes pre-installed in Operating Systems like Linux and Mac
OS X.
• In Windows, it does not come pre-installed.
• So, for installing the python on WINDOWS based OS,
goto : [Link]
Python has two main different versions:
1. Python 2.7: Support for python 2 has stopped and is no longer used much.
2. Python 3: Python 3 is used nowadays.
Python 2.7 was widely used in early days, however, in python 3 various
improvements and syntax changes were incorporated.
So, python 3 is not backward compatible with the Python 2, i.e., code of
python 3 will not work in python 2 and vice-versa.
Running Python
There are two different ways to start Python:
1. Interactive Interpreter using CMD
2. Python IDLE (Integrated Development and Learning Environment) :
You can run Python from a Graphical User Interface (GUI)
environment as well.
First Python Program
You can write your python program in one of following modes:
1. Interactive Mode: You interact directly with the python interpreter.
In this mode, whatever python statement you will write,
interpreter will give you its output as soon as you press the return key
(Enter).
This mode is used when you want to test and execute some 2-3
python Statements
This mode can be used as calculator also.
This mode is not used when you want to write code for solving complex
problems.
First Python Program
2. Script Mode: So, script mode is used for writing long codes in
python.
When we want to write a program, we use a text editor to write the
Python instructions into a file, which is called a script.
By convention, Python scripts have names that end with .py
[Link]
Our First Python Program
• Python does not have a main method like Java
• The program's main code is just written directly in the file
• Python statements do not end with semicolons
Comments in Python
• Comments are used for proper documentation of programs.
• They are used to tell you ‘what a particular piece of code does’ in English.
• They are also used to disable parts of your program if you need to remove them
temporarily.
There are two types of Comments:
1. Single Line Comments: # is used for commenting a line of code in python.
2. Multi Line Comments
Ït can be done using
Identifiers in Python
• Identifiers means the possible legal name that can be given to a variable,
function, class or object.
• A Python identifier is a name used to identify a variable, function, class,
module or other object.
• A legal identifier will follow the following rules
• Identifier will always start with an alphbhet (a-z or A-Z) or underscore _)
• And it can have zero or more alphabets, underscore and digit (0-9)
Which of following are Valid Variable Names?
_variable1
Variable_1
1variable
$name
Name$
______
Variables in Python
• Variables are nothing but reserved memory locations to store some values.
• Variables are the name given to a memory location.
• It is a basic unit of storage in a program.
• Variables in Python are not statically typed.
• The variables are dynamically typed in Python. We do not need to declare variables before using
them or declare their type. A variable is created the moment we first assign a value to it
• The data-types of a variable is given to that variable during the execution based upon the value that
is stored by the user in that variable.
age=10
will create a variable with name age and value 10.
Since, value 10 is stored in variable age, so during the execution, python interpreter will give the type
Integer to this variable.
• To check the datatype of any variable, use the type() function as :
type(variable_name)
• The value stored in a variable can be changed during program execution.
• A variable is only a name given to a memory location, all the operations done on the variable effects
that memory location.
Printing the value of a Variable in Python
A=10
print(A)
Output will be: 10
Similarly,
print(“value of variable A is ”,A)
Output will be: value of variable A is 10
• If we have following two variables:
fname=“John”
lname=“Smith”
How you will get the output as following using the above two variables:
My first name is John and My last name is Smith
How you will get the output as following using the above two variables:
My first name is John and My last name is Smith
print(“My first name is”,fname,”My last name is”,lname);
Age=10
AGE=20
age=30
If we declare the above three variables with same name, then what will
be the output of following statement
print(AGE)
Python is a Case-Sensitive Language
Age=10
AGE=20
age=30
print(AGE) #20
print(age) #30
How Variables are stored in memory
• Whenever you write the statement
‘a=100’
Object of type integer with value 100 is getting stored in some memory
location and variable with name “a” is pointing to that memory location.
So, variable with name “a” will point to a memory location where object 100
is stored.
You can check the memory location by id(variable_name)
id(a)
You can check the data-type of the variable using
type(a)
How Variables are stored in memory
Suppose, a=100
id(a) gives some memory location
If, we write a=200, then object with value 200 will be stored in some
new memory location and variable with name a will start pointing to
that memory location of 200.
id(a) will give new value.
How Variables are stored in memory
Suppose, a=100
b=100
So, here b will point to the same memory location where a was
pointing.
Because, value 100 was already stored as an object in some memory
location and for proper memory utilization, python does not store the
same value.
So, variable will point to the same memory location.
id(a)
Id(b)
How Variables are stored in memory
There are two types of memory which are used by python
• Stack Memory: used for storing the state of the program
• Stack memory is used for managing function calls and local variables within those
functions.
• When you call a function in Python, a new block (or frame) is added to the stack. This
frame contains all the local variables and context needed for that function.
• The stack follows the LIFO principle. The last function call made is the first one to be
completed and removed from the stack.
• Once a function completes its execution, its stack frame is automatically removed from
the stack, and the memory is freed.
• Heap Memory: used for storing the variables/objects
• Heap memory is where Python stores objects and data that need to persist beyond the
lifetime of a single function call.
• This includes global variables, objects, and any dynamically allocated memory.
• Objects are created and allocated memory in the heap. They remain there until they are
no longer needed and are garbage collected.
How Stack and Heap Work Together:
When you define a variable in a function
(e.g., a = 10),
a is stored in the stack frame of that function, but the actual integer object 10
resides in the heap.
The stack contains references (or pointers) to objects in the heap.
When the function finishes, the reference is removed from the stack, but the
object may still exist in the heap if there are other references to it.
How Variables are stored in memory
Python manager uses the concept called Reference counting, whenever a new
object is created it will update the reference count of the object.
A=100
b=100
Here, python memory manager creates only one object i.e "100" and reference
count is "2".
Whenever a new object is created python manager will checks the memory for
the object.
If object already exists python manager will not create new object instead it
references the name to the object and increases the reference counter of the
object.
Interned Small Integers and Interned Small Strings
• In Python, heap memory is used to store objects.
• Most objects are regular objects that are created on demand in the heap and managed by Python’s garbage
collector (GC).
• Python uses reference counting as the primary memory management technique. When an object’s reference
count reaches 0, it becomes eligible for deallocation. In CPython, memory for such objects is eventually freed
(often returned to Python’s internal memory allocator, not always immediately to the OS).
However, there are some special cases:
• Interned small integers: In Python, integers in the range -5 to 256 are preallocated in the heap at interpreter
startup. These objects persist for the entire lifetime of the program. Any variable assigned a value in this
range will refer to the same preallocated object (id() will return the same value every time).
• Interned strings: Certain short and identifier-like strings (e.g., variable names, some literals) are interned by
default, meaning a single shared object is used. These also live for the program’s lifetime. Additional strings
can be interned manually using [Link]().
• Floating-point numbers are not interned by default, since there are too many possible values and they’re
more expensive to store/cache.
So:
a = 256 and b = 256 → same object (id(a) == id(b) is True).
a = 256.0 and b = 256.0 → different objects (id(a) != id(b) is usually True)
Re-declaring the Variable:
We can re-declare the python variable once we have declared the
variable already.
Number = 100
print(“Initial Value: ", Number)
# re-declare the var
Number = 120.3
print("After re-declare:", Number)
Assigning a single value to multiple variables:
• Also, Python allows assigning a single value to several variables
simultaneously with “=” operators.
a = b = c = 10
print(a)
print(b)
print(c)
Assigning different values to multiple
variables:
• Python allows adding different values in a single line with
“,”operators.
a, b, c = 1, 20.2, “Nit Srinagar“
print(a)
print(b)
print(c)
Can we use the same name for different
types?
a = 10
a = “NIT Srinagar“
A=“NIT Srinagar”
print(a)
Output? Error or 10 or NIT Srinagar
Can we use the same name for different
types?
• If we use the same name, the variable starts referring to a new value
and type.
a = 10
a = “NIT Srinagar“
A=“NIT Srinagar”
print(a)
Performing different operations on Variables
a=10
b=20
Print(a+b) # this will add the two integers and give output 30
Performing different operations on Variables
a=10
b=20.5
Print(a+b) # What will be the output?
30 or 30.5
Performing different operations on Variables
a=10
b=20.5
Print(a+b)
Output will be 30.5
This concept is called as Type Promotion.
When you perform the addition a + b, the integer a is implicitly converted to a
floating-point number before the addition takes place.
Performing different operations on Variables
a=10
c=“NIT Srinagar”
d=“CSE”
print(c+d) // This will do the concatenation of String
Print(a+c) // this will give us error. (TypeError: unsupported
operand type(s) for +: 'int' and 'str’)
Output?
a=10
b=True
c=False
print(a+b)
print(a+c)
print(b+c)
Explanation of Previous Program
In Python,
the True boolean value is internally represented as an integer with the value 1.
False boolean value is internally represented as an integer with the value 0.
This allows you to perform arithmetic operations with boolean values as if they were
integers
When you perform the operation 10 + True, Python treats True as 1 and performs
the addition as follows: 10+1
Similarly, print(True+False) will perform addition as 1+0 and gives O/P 1
print(True==1) // Outputs True
Output?
x=10
y=x
print(x)
print(y)
x=20
print(y)
Python is an Interpreted Language
Since Python is an interpreted language, it executes the code line by line.
Suppose we have written 1000 lines of code and line 100 has some
issue/error.
What will happen?
Will it execute till 99 lines and while executing the 100th line, it will stop the
execution?
Or
It will not execute at all?
Output?
print("Line 1")
print("Line 2")
if True # Error in this line as : is not inserted here
print("Line 3")
print("Line 4")
Output?
print("Line 1")
print("Line 2")
error
print("Line 3")
print("Line 4")
The life of your Python code
When you run a .py file, Python doesn’t just start reading line 1 and executing right away.
The process looks like this:
Lexical Analysis (Tokenizing)
• Python breaks the source code (text) into tokens (keywords, identifiers, numbers, operators, etc.).
• Example:
if x > 5:
print("Hi")
Becomes
IF NAME(x) OP(>) NUMBER(5) COLON NEWLINE INDENT NAME(print) ...
Parsing:
• Python takes these tokens and checks if they fit the Python grammar rules.
• It builds an internal parse tree (or AST – Abstract Syntax Tree) that represents the structure of your code.
• If something breaks the rules (e.g., missing colon, wrong indentation), Python raises a SyntaxError here — before executing
anything.
Bytecode Creation
• If parsing succeeds, Python compiles the AST into bytecode (low-level instructions for the Python Virtual Machine).
• Execution
• The bytecode is executed line by line in the Python interpreter.