Python
Python
TO PYTHON
What is program and programming
language
Ordered set of instructions or commands to be
executed by a computer is called a program.
Intelligence.
Working with Python
Python prompt
Execution Modes
interpreter:
● Interactive mode
Interactive Mode
● In the interactive mode, we can type a Python statement on the
>>> prompt directly.
● As soon as we press enter, the interpreter executes the statement
and displays the result(s).
● Working in the interactive mode is convenient for testing a single
line code for instant execution.
● But in the interactive mode, we cannot save the statements for
future use and we have to retype the statements to run them
Script Mode
● In the script mode, we can write a Python program in a file, save it and then use
the interpreter to execute the program from the file. Such program files have
a .py extension and they are also known as scripts.
● Python scripts can be created using any editor. Python has a built-in editor
called IDLE which can be used to create programs. After opening the IDLE, we
can click File>New File to create a new file, then write our program on that file
and save it with a desired name.
digit.
meaningful).
Identifiers
operands.
● Example :
>>> num1 = 5
>>> num2 = 6
>>> num1 + num2
11
Types of Operators
Subtraction - Operator
(-)
● Subtracts the operand on the right from the operand on
the left .
● Example :
>>> num1 = 5
>>> num2 = 6
>>> num1 - num2
Types of Operators
Multiplication -
Operator (*)
● Multiplies the two values on both sides of the operator.
● Example :
>>> num1 = 5
>>> num2 = 6
>>> num1 * num2
Types of Operators
Division - Operator (/)
● Divides the operand on the left by the operand on the
right of the operator and returns the quotient.
● Example :
>>> num1 = 5
>>> num2 = 2
>>> num1 / num2
2.5
Types of Operators
Modulus - Operator (%)
● Divides the operand on the left by the operand on the
right and returns the remainder .
● Example :
>>> num1 = 13
>>> num2 = 5
>>> num1 % num2
3
Types of Operators
Floor Division -
● Operator (//)
Divides the operand on the left by the operand on the right and
returns the quotient by removing the decimal part. It is
sometimes also called integer division.
● Example :
>>> num1 = 5
>>> num2 = 2
>>> num1 // num2
2
>>> num2 // num1
0
Types of Operators
Exponent - Operator (**)
● Raise the base to the power of the exponent.
● Example :
>>> num1 = 3
>>> num2 = 4
>>> num1 ** num2
81
Types of Operators
Relational Operators
them.
Types of Operators
Equals to - Operator
(==)
● If values of two operands are equal, then the condition is True,
otherwise it is False.
● Example :
>>>num1=10
>>>num2=0
>>> num1 == num2
False
Types of Operators
Not equal to - Operator
● (!=)
If values of two operands are not equal, then condition is True,
otherwise it is False.
● Example :
>>>num1=10
>>>num2=0
>>> num1 != num2
True
Types of Operators
Greater than - Operator
● If the value of the left(>)
operand is greater than the value of the
right operand, then condition is True, otherwise it is False.
● Example :
>>>num1=10
>>>num2=0
>>> num1 > num2
True
Types of Operators
Less than - Operator (<)
● If the value of the left operand is less than the value of the
right operand, the condition is true otherwise it is False
● Example :
>>>num1=10
>>>num2=0
>>> num1 < num2
False
Types of Operators
● If the value of the left oerand is less than the value of the right
operand, the condition is true otherwise it is False
● Example :
>>> num1 = 2
>>> num2 = num1
>>> num2
2
Types of Operators
● It adds the value of right side operand to the left side operand and assigns the
result to the left side operand.
● Note: x+ =y is same as x= x+y
● Example :
>>> num1 = 10
>>> num2 = 2
>>> num1 += num2
>>> num1
12
>>> num2
2
Types of Operators
● It subtracts the value of right side operand from the left side operand and
assigns the result to left side operand.
● Note: x− =y is same as x=x-y
● Example :
>>> num1 = 10
>>> num2 = 2
>>> num1 -= num2
>>> num1
8
Types of Operators
Logical Operators
● Example :
>>> num1 = 10
>>> num2 = -20
>>> num1 == 10 and num2 == -20
True
>>> num1 == 10 and num2 == 10
False
Types of Operators
Logical OR – Operator(or)
● If any of the two operands are True, then condition becomes
True .
● Example :
>>> num1 = 10
>>> num2 = 2
>>> num1 >= 10 or num2 >= 10
True
>>> num1 <= 5 or num2 >= 10
Types of Operators
Logical NOT – Operator(not)
● Used to reverse the logical state of its operand.
● Example :
>>> num1 = 10
>>> not (num1 == 20)
True
Types of Operators
Membership Operators
line.
value. But such variables are not useful to hold multiple data values, for
museum.
● For this, Python provides sequence data types like Strings, Lists, Tuples,
● Dictionary in Python holds data items in key-value pairs and Items are
enclosed in curly brackets { }.
● Dictionaries permit faster access to data.
● Every key is separated from its value using a colon (:) sign.
● The key value pairs of a dictionary can be accessed using the key.
● Keys are usually of string type and their values can be of any data type.
● In order to access any value in the dictionary, we have to specify its key in
square brackets [ ].
Example
#create a dictionary
>>> dict1 = {'Fruit':'Apple', 'Climate':'Cold', 'Price(kg)':120}
>>> print(dict1)
{'Fruit': 'Apple', 'Climate': 'Cold', 'Price(kg)': 120}
120
mutable and immutable
datatypes
● Mutable objects in Python are those that can be changed after they are
created.
● Example : lists,dictionaries,sets
● Immutable objects in Python are those that cannot be changed after they
● Any instruction written in the source code and executed by the Python
interpreter is called a statement.
● There are mainly four types of statements in Python,
○ print statements
○ Assignment statements
○ Conditional statements
○ Looping statements.
Comments
● Comments are used to add a remark or a note in the source code. Comments
● They are added with the purpose of making the source code easier for
humans to understand.
● In Python, a single line comment starts with # (hash sign).
Input and
Output
● Sometimes, we need to enter data or enter choices into a program.
● In Python, we have the input() function for taking values entered by input
device such as a keyboard. The input() function prompts user to enter data.
● Example,
>>> fname = input("Enter your first name: ")
Enter your first name: Arnab
● Due to errors, a program may not execute or may generate wrong output.
● There are mainly three types of errors. They are :
● Syntax errors
● Logical errors
● Runtime errors
Syntax Errors
● Python has rules that determine how a program is to be written. This is called
syntax.
parenthesis.
Logical Errors
● A logical error/bug (called semantic error) does not stop execution but the
program behaves incorrectly and produces undesired /wrong output.
● Since the program interprets successfully even when logical errors are
present in it, it is sometimes difficult to identify these errors.
● For example, if we wish to find the average of two numbers 10 and 12 and we
write the code as 10 + 12/2, it would run successfully and produce the result
16, which is wrong. The correct code to find the average should have been
(10 + 12) /2 to get the output as 11.
Runtime Error
● A runtime error causes abnormal termination of program while it is
if-else
if-elif-else
while loop
for loop
if..else
Statements
● The if..else statement, in which there are two possibilities and the
condition determines which one gets executed.
● Example : The user to enter their name and age, and then checks if
the entered age is greater than or equal to 18. If the age is greater
than or equal to 18, the program prints a message stating that the
person is eligible to vote.If the age is less than 18, the program
prints a message stating that the person is not eligible to vote.
Output:
Enter first number: 5
Enter second number: 6
The difference of 5 and 6 is 1
if..elif..else
Statements
● if...elif....else is used to check multiple conditions and execute statements
accordingly. Meaning of elif is elseif. We can also write elseif instead of elif for
more clarity.
first condition is false, then the next condition is checked, and so on. If one of the
conditions is true, then the corresponding indented block executes, and the if
statement terminates. After that, the statements outside the if..else are
Check whether a number is positive,
negative, or zero.
Output:
2 is an even Number
4 is an even Number
6 is an even Number
8 is an even Number
10 is an even Number
The range() Function
● A loop may contain another loop inside it. A loop inside another loop is
for loop