1 - Introduction To Python
1 - Introduction To Python
1 - Introduction To Python
12/1/2021 1
Contents
• History, Features and Applications
• Versions and Installation
• First Python Application
• Keywords
• Variables and Identifiers
• Constants and Literals
• Operators
• Multi-Line statements and Comments
• Python Input and Output
12/1/2021 2
Introduction - History
• Developed by:- Guido van Rossum
• When:- In late eighties and early nineties
• Where:- At the National Research Institute for
Mathematics and Computer Science in
Netherlands.
• Named after a comedy television show
“Monty Python's Flying Circus”.
12/1/2021 3
Basic Features of Python
1. High level language - A high-level language (HLL) is
a programming language such as C, FORTRAN, or Pascal that
enables a programmer to write programs that are more or less
independent of a particular type of computer. Such languages are
considered high-level because they are closer to human languages
and further from machine languages. The main advantage of high-
level languages over low-level languages is that they are easier to
read, write, and maintain. Ultimately, programs written in a high-
level language must be translated into machine language by
a compiler or interpreter.
2. Interpreted - Python is processed at runtime by the interpreter
(not compiler)
3. Integrated – It can be integrated with other languages like C , C++ ,
Java.
4. Object-Oriented - Python supports Object-Oriented style or
technique of programming. It supports objects , class , methods,
inheritance, encapsulation, Polymorphism and data abstraction.
4
Basic Features of Python
5. Easy-to-learn as it has simple structure with few keywords and a clearly
defined syntax.
6. Easy-to-read: Code is clearly defined.
7. Easy-to-maintain: Source code can be maintained easily.
8. Broad standard library: Supports large standard library and provide rich
set of module and functions for rapid application development. Examples
are Numpy , Pandas , Scikit learn etc.
9. GUI Programming: Supports GUI applications that can be created and
ported to many system calls, libraries and windows systems.
10. Open source and free : It is freely available at official web address.
11. Cross platform language: It can run equally on different platforms
such as windows , Linux , Unix etc.
12/1/2021 5
Installations
• The latest version of Python is Python
3.8.0. It’s documentation released on 14
October 2019.
• You can install Python on any operating
system.
12/1/2021 6
First Program
• Running Python – Interactive Mode
– Type Python on command prompt.
C:\> python for Windows
$python for Linux
– You will get python prompt as follows
>>>
– Output is
Hello, World!
12/1/2021 7
First Program
• Create a file first.py Python programs have .py
extensions
• Type the code
print(“Hello, World!”)
• Output is
Hello, World!
12/1/2021 8
First Program
• The code we wrote is
– print(“Hello, World!”)
• print() is an output function, which outputs to
display device
• “Hello, World!” is an argument sent to the
function, keeping it in double quotes suggests
we are sending string argument.
12/1/2021 9
Input and output
• We use the print() function to output data to the standard
output device (screen).
print('This sentence is output to the screen')
#Output: This sentence is output to the screen
12/1/2021 10
format() function
• Syntax : { } .format(value)
• Parameters :
(value) : Can be an integer, floating point
numeric constant, string, characters or even
variables.
• Return Type : Returns a formatted string with
the value passed as parameter in the
placeholder position.
12/1/2021 11
• Syntax : { } { } .format(value1, value2)
• Parameters :
(value1, value2) : Can be integers, floating point numeric
constants, strings, characters and even variables. Only
difference is, the number of values passed as parameters in
format() method must be equal to the number of
placeholders created in the string.
12/1/2021 12
Input and Output (contd.)
• Output formatting
– The output can be formatted as follows
x=10;y=5
print('The value of x is {} and y is {}'.format(x,y))
#output: The value of x is 10 and y is 5
– Formatting numbers
x=5.23456
print('The value of x is %3.2f' %x)
print('The value of x is %3.3f' %x)
#Output: The value of x is 5.23
The value of x is 5.235
12/1/2021 13
Input and Output (contd.)
• To allow flexibility in the program, we might want to take the
input from the user.
• In Python, we have the input() function to allow this. The
syntax for input() is
• input([prompt])
– where prompt is the string we wish to display on the screen. It is
optional.
12/1/2021 14
Keywords
• In programming, a keyword is a word that is reserved by
a program because the word has a special meaning. Keywords can be
commands or parameters. Every programming language has a set
of keywords that cannot be used as variable names. Keywords are
sometimes called reserved names. The list of Python keywords is as
follows
and elif if print
as else import raise
assert except in return
break exec is try
class finally lambda while
continue for Not with
def from or yield
del global pass
12/1/2021 15
Variables
• In programming, a variable is a value that can change, depending on
conditions or on information passed to the program.
• Python variables do not need explicit declaration to reserve memory
space.
• The declaration happens automatically when you assign a value to a
variable.
• The equal sign (=) is used to assign values to variables.
• The operand to the left of the = operator is the variable name and the
operand to the right of the = operator is the value stored in the variable.
• Python allows you to assign a single value to several variables
simultaneously. a = b = c = 1
• You can also assign multiple objects to multiple variables. For example −
• a, b, c = 1, 2, "john“
12/1/2021 16
Identifiers
A Python identifier is a name used to identify a variable,
function, class, module or other object.
Rules for naming
– A variable name must start with a letter or the underscore
character
– A variable name cannot start with a number
– A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
– Variable names are case-sensitive (age, Age and AGE are
three different variables)
12/1/2021 17
Constants
• A constant is a type of data storage whose value cannot be
changed.
• It is helpful to think of constants as containers that hold
information which cannot be changed later.
• . In Python, constants are usually declared and assigned on
a module
• Here, the module means a new file containing variables,
functions etc. which is imported to main file.
• Inside the module, constants are written in all capital
letters and underscores separating the words.
• Example:
– PI = 3.14
– GRAVITY = 9.8
12/1/2021 18
Variables v/s Constants
12/1/2021 19
Literals
• Literal is a raw data given in a variable or
constant. In Python, there are various types of
literals they are as follows:
– Numeric Literals
– String Literals
– Boolean Literals
– Special Literals
– Literal Collection
12/1/2021 20
Variables, Identifies, Literals
𝐱 = 𝟏𝟎
Name of variable
Value of Variable
Or
Or
Identifier
Literal
12/1/2021 21
Operators
• Operators are special symbols in Python that carry out
arithmetic or logical computation.
• Following is the set of operators which Python
supports
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Logical (Boolean) Operators
4. Bitwise Operators
5. Assignment Operators
6. Special Operators
• Identity Operator
• Membership Operator
12/1/2021 22
Operators (Contd..)
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication etc.
Operator Meaning Example
12/1/2021 23
Operators(Contd..)
Program
• x = 15
• y=4 Output
• print('x + y =', x+y ) • x + y = 19
• print('x - y =',x-y) • x - y = 11
• print('x * y =',x*y) • x * y = 60
• print('x / y =',x/y) • x / y = 3.75
• print('x // y =',x//y) • x // y = 3
• print('x ** y =',x**y) • x ** y = 5062
12/1/2021 24
Operators(Cont..)
• Comparison operators (Relational) are used to compare values. It either
returns True or False according to the condition.
Operator Meaning Example
Greater that - True if left operand is greater than
> x>y
the right
< Less that - True if left operand is less than the right x < y
12/1/2021 26
Operators(Contd..)
• Logical operators are the and, or, not operators.
• Program • Output
• x = True
• y = False
• print('x and y is',x and y) • x and y is False
• print('x or y is',x or y) • x or y is True
• print('not x is',not x) • not x is False
12/1/2021 27
Operators(Contd..)
• Bitwise operators act on operands as if they were string of binary digits. It
operates bit by bit, hence the name.
• For example, 2 is 10 in binary and 7 is 111.
• In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)
12/1/2021 28
Operators(Contd..)
Operator Example Equivatent to
• Assignment operators
= x=5 x=5
are used in Python to
+= x += 5 x=x+5
assign values to
variables. -= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
12/1/2021 29
Operators(Contd..)
• Special operators Identity operators
– is and is not are the identity operators in Python. They are used to check if
two values (or variables) are located on the same part of the memory. Two
variables that are equal does not imply that they are identical.
12/1/2021 30
Operators(Contd..)
Programs
• x1 = 5
• y1 = 7
• x2 = 'Hello'
• y2 = 'Hello'
• print(x1 is not y1) ------- True
• print(x1 is y1) ------- False
• print(x2 is not y2) ------- False
• print(x2 is y2) ------- True
12/1/2021 31
Operators(Contd..)
• Special operators Membership operators
• In and Not In are the membership operators in Python. They are used to test
whether a value or variable is found in a sequence (string, list, tuple, set and
dictionary).
Program Output
x = 'Hello world'
print('H' in x) True
print('hello' not in x) True
12/1/2021 32
Multi-line Statement
• Instructions that a Python interpreter can execute are called statements.
• End of a statement is marked by a newline character.
• Example – Following are some examples of statements in Python
– print(“hello”)
– x=3
• A statement can continue for multiple lines using / (line continuation)
character
• Example
– y=9+ /
– (2*x)
• Some default line continuation characters are as follows
– Y=9+(
– 2*x)
• We can also write multiple statements at one line
– X=3; y=9+(2*x)
12/1/2021 33
Comments
• In computer programming, a comment is a programmer-readable
explanation or annotation in the source code of a computer program.
• They are added with the purpose of making the source code easier for
humans to understand, and are ignored by compilers and interpreters.
• In Python, we use the hash (#) symbol to start writing a comment. It
extends up to the newline character.
Program Program
# This line will not get executed ‘’’ This also will not
Print(“Hello, World!”) get executed ‘’’
Print(“Hello, World!”)
Output
Output
Hello, World!
Hello, World!
12/1/2021 34
Installing Jupyter Notebook
• Python is a requirement (Python 3.3 or
greater, or Python 2.7) for installing the
Jupyter Notebook. I recommend using
the Anaconda distribution to install Python
and Jupyter.
• https://www.anaconda.com/distribution/
12/1/2021 35
Your browser should now look
something like this:
Note that right now you are not actually running a Notebook, but instead you
are just running the Notebook server. Let’s actually create a Notebook now!
12/1/2021 36
Creating a Notebook
12/1/2021 37
Your web page should now look like
this:
12/1/2021 38
Running Cells
12/1/2021 39
Running Cells
• When you run a cell, you will notice that there
are some square braces next to the word In to
the left of the cell. The square braces will auto
fill with a number that indicates the order that
you ran the cells. For example, if you open a
fresh Notebook and run the first cell at the top
of the Notebook, the square braces will fill
with the number 1.
12/1/2021 40
Getting Started with Python Programs
1. Python Program to Print Hello world!
2. Python Program to Add Two Numbers
• Use arithmetic operator + to add two numbers.
• Add Two Numbers Provided by The User
3. Python Program to Find the Square Root
4. Python Program for simple interest
5. Python Program for Program to find area of a
circle
6. Program to print ASCII Value of a character
12/1/2021 41
Program to print ASCII Value of a character
• Python code using ord function :
• ord() : It coverts the given string of length one, return an integer
representing the unicode code point of the character. For example,
ord(‘a’) returns the integer 97.
12/1/2021 42
Python Program for simple interest
• Simple interest formula is given by : Simple Interest = (P x T x R)/100
Where , P is the principle amount, T is the time and ,R is the rate
• P=1
• R=1
• T=1
•
• # Calculates simple interest
• SI = (P * R * T) / 100
•
• # Print the resultant value of SI
• print("simple interest is", SI)
12/1/2021 43
12/1/2021 44