You are on page 1of 38

Introduction

to
python
Session 1 :
Python Basics
Introduction to python :
● Python was created by Guido Van Rossum, a
Dutch programmer .
● The first version Python 0.9.0 was released in
1991.
● Python 2.x and 3.x are most popular version .
● Python is considered as one of the most popular
and preferred language by programmers due to
its simplicity.
● Python is used to develop various applications
and web services.
Features of python :
1. Simple syntax and free to use
2. Open source language
3. High level programming language
4. Rich library of pre-designed modules
5. Portable and case-sensitive language
6. Compiler and interpreter based language
7. Support both programming approaches (OOP & POP)
8. Efficient use of memory
9. Dynamic typing
10. Language extensibility
Compiler & interpreter :
PYTHON IDLE :
● Interpreter and compiler are language converter
software which are used to convert high level language
into machine language and machine language into high
level language .
● Interpreter executes the program line by line and
compiler executes the program all at once .
● Program written by programmer is called as source
code.
Compiler & interpreter :
PYTHON IDLE :
● Source code is converted by compiler into intermediary
code called as byte code.
● Byte code is then converted by interpreter into machine
code inside PVM ( Python Virtual Machine) .
● Errors related to grammar and structure of the
programming language is called as syntax error .
● Errors which occur during the time when program is
running , is called as run-time error .
PYTHON IDLE :
● ItPYTHON IDLEdevelopment
stands for integrated : and learning
environment .
● The commands are written in front of the python
command prompt ‘>>>’ .
● >>> symbol on python IDLE shows that the python shell is
ready to take commands from the user and to execute it .
● Python shell interprets the commands and scripts
submitted to it.
● The set of instructions in python is called script .
Interactive / Command mode Script mode

i) It executes one line of statement at Whole program is executed once .


a time.
ii) It is used to write small programs . It is used to write lengthy programs .

iii) It automatically displays the output It does not display the result
just after entering one line of input. automatically .

iv) the input and output statements are input and output statements are
compressed together . shown separately.

v) programs written in interactive We can save and edit the programs


mode can not be saved and edited . and use them later. The Python file is
saved with extension “.py” .
Python Variable :
● Variable is a computer memory location in which we can store the
PYTHON IDLE :
values to be used in the program .
● A variable can store only one data at a time.
● If a new value is assigned to a variable, the previous value gets
overwritten .
● Syntax :
Variable-name = value
Example : ( Here A, B and C are variables)
A = 34
B = 65.9
C = “ hello ”
Rules to write the variable name:
● Starting -> either alphabet(a-z,A-Z) or underscore
PYTHON
symbol ( _ )
IDLE :
● Consists of -> alphabet(a-z,A-Z), underscore ( _ ) ,
digits ( 0-9 )
● Special characters (symbols) as well as space are not
allowed.
● It can be of any length that is, variable name can have
as many letters as the user wants.
● Case sensitive (age and Age are different variables .)
Data types

Integer String
( int ) Float
( str )
Data Types :
1. int : represents positive or negative whole numbers . There are three
PYTHON IDLE :
categories like :
a. Plain integer :
i. Represents small whole number
ii. Occupies 4 bytes in the memory
b. Long integer :
i. Represents huge numbers that lie beyond the range of plain
integers
ii. Values are suffixed with letter “L”
iii. Occupies more than 4 bytes in the memory
c. Boolean : represents logical values in the form of true or false .
Here 0 represents false and 1 represents true .
1. Operators are the symbols which are used to
perform any operation in expression.
2. Operands are the values which are involved
in an expression.
Arithmetic operators :

PYTHON IDLE :
● Arithmetic operators are used with numeric values to
perform common mathematical operations.
● These are classified into two categories as - unary and
binary operators.
● Binary arithmetic operators perform operation on exactly
two operands while unary arithmetic operators process
exactly one operand .
operator symbol function example
addition + To add two given values 9 + 6 = 15
subtraction - To subtract one from another 9-6=3
multiplication * To find product of two given values 9 * 6 = 54
(asterisk)
division / To divide and find the quotient in 9 / 6 =1.5
form of decimal value
Integer(floor) // To divide and find the quotient but 9 // 6 = 1
division gives only the integral part,
ignoring the decimal values
Modulus % To divide and find the remainder 9%6=3
value
Exponentiation ** To find the power of a number 4 ** 3 = 64
(4x4x4=64)
String operators :

PYTHON IDLE :
● Like arithmetic operators work on numerical values,
string operators work only on string values.
● There are two string operators :

operator symbol function example

Concatenation + Used to join two string ‘Hari’ + ‘om’ =


with each other ‘Hariom’
replication * Repeats the string for a ‘Om’ * 3 =
(asterisk) given no of times ‘OmOmOm’
Indexing in string :
● In Python, strings are considered as an ordered sequences of
PYTHON IDLE :
characters.
● Individual characters in a string can be accessed by specifying
the string name followed by a number in square brackets ([]).
● String indexing in Python is zero-based: the first character in
the string has index 0, the next has index 1, and so on.
● String index can also be specified with negative numbers, in
which case indexing occurs from the end of the string
backward: -1 refers to the last character, -2 the second-to-last
character, and so on.
Example :

N A T U R E
print() :
● Python uses print() to display the user defined message or
PYTHON
output IDLE
of any program : screen .
on the
● print() accepts values to be displayed as parameter and
displays them as output.
Syntax :
print(“message to be displayed”)
Or
print(‘message to be displayed’)
● Starting and ending quotes of the message should be same.
input() :
● In python , input() is used to display a prompt to the user
toPYTHON
enter a value . IDLE :
● input() accepts the value as string(default) from user and
store it in the variable .
Syntax :
Variable_name = input(“message”)
Example :
name=input(“enter your name:”)
Implicit & explicit data type conversion :
● During an operation, python automatically does data
PYTHON IDLE :
conversion which is known as implicit data type
conversion.
● The conversion of one data type into another data type
manually by user as per the requirement, is known as
explicit data type conversion. There are three
functions used for conversion :
○ int() - converts the given value to integer .
○ float() - converts the given value to float .
○ string() - converts the given value to string .
Comments in python :
● The statements which are added to a program to make the
PYTHON IDLE :
code easier to understand for the user which are different
from program code, are known as comments.
● These statements are ignored while execution and will not
be shown in output.
● Comment statements are optional which means a user can
add it according to the need.
● There are two types of comment lines : single and multiple
Single line comment Multiple line comment

i) Written in one single line Written in multiple lines.

ii) # (hash ) symbol is used at Triple double quotes (“““)


starting is used at starting as well
as ending .
iii) Automatically terminates Does not automatically
at the end of line terminates at the end of
line
iv) Visible in red colour Visible in green colour
Session 2 :
Decision making
and loops
Conditional
statement

if if…else if…elif…else
statement statement statement
1. if statement :
● It is used to evaluate only one condition.
● If the output of the condition is true, it will
execute the if-block statements followed by it.
● If the output of condition is false , it will skip
statements.
● Syntax :
if(condition):
if-block statement(s)
2. if…else statement :
● It is also used to evaluate only one condition.
● If the output of the condition is true, it will execute
the if-block statements followed by it.
● If the output of condition is false , it will execute the
else-block statements .
● Syntax :
if(condition):
if-block statement(s)
else :
else-block statement(s)
3. if…elif…else statement :
● It is used to evaluate more than one condition.
● Whichever condition will be true, statements
followed by that condition will be executed.
● If no condition is true , it will execute the else-block
statements .
● Elif statement can be written as many times the user
wants to check condition.
Syntax of if…elif…else statement :

PYTHON IDLE :
if(condition 1):
Statement 1
elif(condition 2) :
Statement 2
.
.
.
.
else :
else-block statement(s)
Loop in python :
● A loop is a block that contains the statements to be
executed repeatedly .
● The loop executes on the basis of conditions
associated with it .As long as the conditions return
true, the loop executes otherwise terminate .
● Executing statements repeatedly according to our
requirements is called a loop .
● Loops helps us to write the same program in fewer lines
which saves time, memory space and effort also.
1. while loop :
● The code in a while loop will execute repeatedly as
long as the condition associated with it returns true
.
● While loop must contain followings :
○ The while keyword
○ A condition
○ A colon
○ An indented block of code
● Syntax : Output :
initialization 1
while ( condition ) : 2
Loop body 3
Step value 4
● Example : 5
i=1
while ( i<=5) :
print(i)
i=i+1
2. for loop :
● for loop is generally considered as counter loop .
● for loop uses a counter variable(usually i) to count
through a list of values created by range() and
execute the code written in loop body. Output :
● Syntax : 1
for i in range(initial , final , step) : 2
Loop body 3
● Example : 4
for i in range(1,6) : 5
print(i)
● Three values of range() are :
○ Initial value : An integer number specifying at which
position to start. Default is 0.
○ Final value : An integer number specifying at which
position to stop (not included in list).
○ Step value : An integer number specifying the
increment or decrement. Default is 1.
● range() generates one number less than the final
value . range(1,5) will create a list like [1,2,3,4] .
Number 5 will not be included in the list .
Infinite loop :
● An infinite loop is also known as an endless loop ,
meaning the loop will never end.
● It is a sequence of instructions in a program which loops
endlessly.
● It happens either the provided condition can never meet
or loop has not any terminating condition .
Examples of infinite loop :
● i=2 ● i=1
while (i>0): while True:
print(i) print(i)
i=i+1 i=i+1

● i=1 ● while 1 :
while ( i<7) : print(“True”)
print(i)
break keyword :
● The break keyword is used for getting the program
execution to get out of loop .
● If the execution reaches break statement, it immediately
exits the loop body .
continue keyword :
● When the program execution reaches a continue
statement , the program execution immediately jumps
back to the start of loop and rechecks the loop’s
condition.
● It helps in continuing the program execution .

You might also like