Professional Documents
Culture Documents
Introduction To Python Programming - UNIT IV
Introduction To Python Programming - UNIT IV
Theory (1030106502)
Output: The information produced by the computer to the user is called output.
Python provides us with the two inbuilt functions as input() and output().
The syntax for input is input (prompt_message); the python will automatically identify
whether the user entered a string, number, or list; if the input entered from the user is not
correct, then python will throw a syntax error.
Example:
Control Statements
If Statement
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if if-block.
ck. The condition of if statement can be any valid
logical expression which can be either evaluated to true or false.
if expression:
statement
a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");
Output:
Enter a? 100
Enter b? 120
Enter c? 130
c is largest
The if-else
else statement provides an else block combined with the if statement which is
executed in the false case of the condition.
if condition:
#block of statements
else:
#another block of statements (else
(else-block)
Output:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Example:
Output:
Nested-if Statement
We can have an if…elif…else statement inside another if…elif…else statement. This
is called nesting in computer programming. Any number of these statements can be nested
inside one another. Indentation is the only way to figure out the level of nesting. Th
This can get
confusing, so it must be avoided if we can.
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)
Example:
var = 100
if var < 200:
print "Expression value is less than 200"
if var == 150:
print "Which is 150"
elif var == 100:
print "Which is 100"
elif var == 50:
print "Which is 50"
elif var < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"
Output:
Looping statements
A for loop is used for iterating over a sequence (that is a list, a tuple, a dictionary, a
set, or a string).
This is less like the for keyword in other programming languages, and works more
like an iterator method as found in other object
object-orientated
orientated programming languages.
Loop iterates until the final item of the sequence are reached.
Example:
Output:
apple
banana
cherry
While Loop
While loops are used in Python to iterate until a specified condition is met. However,
the statement in the program that follows the while loop is executed once the conditi
condition
changes to false.
while <condition>:
{ code block }
Example:
Output:
Python Loops
Python Loops
Python Loops
Python Loops
Nested Loops
If we have a piece of script that we want to run a number of times and then another
piece of script inside that script that we want to run B number of times, we employ a "nested
loop." When working with an iterable in the lists, these are widely utilized in Python.
Example:
for x in adj:
for y in fruits:
print(x, y)
Output:
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
The break is commonly used in the cases where we need to break the loop for a given
condition.
#loop statements
break;
Example:
for i in range(9):
if i > 3:
break
print(i)
Output:
0
1
2
3
Both Python while and Python for loops can leverage the continue statements.
Example:
# looping from 10 to 20
for iterator in range(10, 21):
Output:
10
11
12
13
14
16
17
18
19
20
When the pass statement is executed, nothing happens, but you avoid getting an error
when empty code is not allowed.
Example: