02 Python Intro Installation Identifiers Variables SS
02 Python Intro Installation Identifiers Variables SS
Lecture 2:
Introduction to PYTHON Programming,
Installation,
Identifiers
Variables
Used by:
• Google, Yahoo!, Youtube
• 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
• Heap Memory: used for storing the variables/objects
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.
Re-declaring the Variable:
We can re-declare the python variable once we have declared the
variable already.
Number = 100
print(“Initial Value: ", Number)
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)