Python Programming
Python Programming
LOOPING STATEMENTS
Operators
Operator - symbol that represents an action such as +, -, *
Operand - numbers on which an operator act.
Example- 2+3
+ is operator
2 and 3 are operands
Types of Operators:
Python language supports the following types of operators.
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators
Equal to “=” sign is used to assign value to variables.
The if statement evaluates whether a statement is true or false and it runs only if the condition is true.
Syntax
if expression:
Statement(s)
2. The if…else statement:
The if…else statement has two blocks. One is the if block which is the same as above and the other is
else block. If the expression in the if block is FALSE, it executes the code in the else block.
Syntax
if expression:
Statement(s)
else:
Statement(s)
3. The if…elif…else statement:
This statement is used to handle multiple conditions together. There can be multiple elif blocks, but only
one if and else blocks.
Syntax
if expression:
Statement(s)
elif expression2:
Statement(s)
else:
Statement(s)
Example 1:
num=10
if num%2==0:
print(“It is an even number”)
Example 2:
Write a program to check whether a number is odd or even using if…else statement
num=5
if num%2==0:
print(“It is an even number”)
else:
print(“It is an odd number”)
Example 3:
Write a program to print the grades of a student according to her marks.
marks=78
if marks>=80:
print(“Your grade is
A”)elif marks<80 and
marks>=70:
print(“Your grade is
B”)elif marks<70 and
marks>=60:
print(“Your grade is C”)
else:
print(“Your grade is D”)
LOOPING STATEMENTS IN PYTHON
The while loop iterates a block of statements as long as the given condition is true.
When the condition becomes false, it stops the execution of the loop.
Syntax
while expression:
Statement(s)
Example:
Write a program to display the word “Hello” five times on the screen.
count=1
while count<=5:
print(“Hello”)
count=count+1
2. for loop:
In this example, „numbers‟ is a list of 5 numbers. In the first iteration, the variable
„i‟ is assigned the first value in the list (that is, 1). Then the statement block is
executed (that is print(i) prints the value 1). Each list item is assigned to the
variable „I‟ and this process continues until the list is completed.
The range( ) function is used to loop through a set of code a specified number of
times. The range( ) functions returns a sequence of numbers, starting from 0 by
default and increment by 1 by default and ends at a specified number.
Syntax:
1. range(stop)
2. range(start, stop)
3. range(start, stop, step)
Example 1:
Write a program to print the numbers till 5 using range( ) function.
Example 2:
Write a program to print the numbers from 2 to 10 using range( ) function.
Example 3:
Write a program to print the even numbers between 4 and 20.
ERRORS IN PYTHON:
1. Syntax Errors
Syntax error occurs when certain rules are not followed.
In Python, the syntax errors occur due to the following:
2. Runtime Errors
An error that occurs during the execution of the program is called a runtime
error. When a program is stopped because of runtime error, we say that the
program has crashed.
Some points due to which runtime error may occur are as follows:
➢ Accessing an element of a list which doesn’t exists.
➢ Making use of a variable which is not defined.
3. Logical Errors
These errors occur when a program executes but does not produce the intended
result. The error is caused due to the wrong logic used in the program. No
explicit error was shown and thus we must find the error by ourselves.
SAMPLE PROGRAMS