0% found this document useful (0 votes)
13 views

02 python intro installation identifiers Variables SS-2

Uploaded by

ajm8982
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

02 python intro installation identifiers Variables SS-2

Uploaded by

ajm8982
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

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 22, 2024
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 : https://www.python.org/
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.
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

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.

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.
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)

You might also like