Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python Programming

The document provides an overview of Python programming focusing on conditional and looping statements, including types of operators and their usage. It explains conditional statements such as if, if...else, and if...elif...else, along with looping structures like while and for loops, and includes examples for clarity. Additionally, it addresses common errors in Python, including syntax, runtime, and logical errors.

Uploaded by

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

Python Programming

The document provides an overview of Python programming focusing on conditional and looping statements, including types of operators and their usage. It explains conditional statements such as if, if...else, and if...elif...else, along with looping structures like while and for loops, and includes examples for clarity. Additionally, it addresses common errors in Python, including syntax, runtime, and logical errors.

Uploaded by

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

PYTHON PROGRAMMING – CONDITIONAL AND

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.

1. Arithmetic Operators- used for mathematical functions


2. Comparison (Relational) Operators- used for comparing values.
3. Assignment Operators- used to assign values to variables.
4. Logical Operators- used in logical calculations.

Arithmetic Operators

Comparison Operators

Logical Operators

Assignment Operators
Equal to “=” sign is used to assign value to variables.

CONDITIONAL STATEMENTS IN PYTHON


A conditional statement is a set of rules performed when a condition is met. There are three conditional
statements in Python:
1. The if statement:

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:

Write a program to check whether a number is odd or even using if statement.

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

A looping statement is used to execute a set of instructions multiple times.


1. while loop:

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:

for loop is used to repeat the elements of a list.


Syntax
for var in sequence:
Statement(s)
Example
Write a program to list the numbers from 1 to 5
numbers=[1,2,3,4,5]
for i in numbers:
print(i)

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.

range() function in for loop

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)

start – An integer number specifying at which position to start. Default is 0.


Stop – An integer number specifying at which position to stop.
Step – An integer number specifying the incrementation. Default is 1.

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:

➢ Missing parenthesis, colon, or comma.


➢ Incorrect indentation
➢ Making incorrect use of keyword
➢ Empty block of statements

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

I. Application based questions (with answers):


1. What will be the output of the following:
n=40
while n>20:
print(
n)n=n-
3
Ans:
40
37
34
31
28
25
22
2. Write a program to print the natural numbers from 1 to 10 using while loop.
Ans:

3. Write a program to check whether a number is positive or negative.


Ans:

4. Write a program to print first 5 multiples of 3 using while loop.


Ans:

5. Write a program to display any 5 names of your friends using loop.


Ans:

6. Write a program to display the first 10 multiples of 3 using for loop.

7. Write a program to check the greater of two numbers.


Ans:

You might also like