02 python intro installation identifiers Variables SS-2
02 python intro installation identifiers Variables SS-2
Lecture 2:
Introduction to PYTHON Programming,
Installation,
Identifiers
Variables
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.
Game.py
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.
A=10
print(A)
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(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.
30 or 30.5
Performing different operations on Variables
a=10
b=20.5
Print(a+b)
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)