Python Functions
Python Functions
Python Functions
Output:
This is Python
C
This is a multiline string with more than one line code.
Example :
123.34, 456.23, 156.23 # Floating point data
12.E04, 24.e04 # Exponent data
Complex number is made up of two floating point values, one each for the real and
imaginary parts.
5.8.2 Boolean Data type
A Boolean data can have any of the two values: True or False.
Example :
Bool_var1=True
Bool_var2=False
Example :
Char_data = ‘A’
String_data= "Computer Science"
Multiline_data= ”””String data can be enclosed in single quotes or
double quotes or triple quotes.”””
Evaluation
Part - I
Choose the best answer (1 Marks)
1. Who developed Python ?
A) Ritche B) Guido Van Rossum
C) Bill Gates D) Sunder Pitchai
2. The Python prompt indicates that Interpreter is ready to accept instruction.
A) >>> B) <<<
C) # D) <<
3. Which of the following shortcut is used to create new Python Program ?
A) Ctrl + C B) Ctrl + F
C) Ctrl + B D) Ctrl + N
4. Which of the following character is used to give comments in Python Program ?
A) # B) & C) @ D) $
5. This symbol is used to print more than one item on a single line.
A) Semicolon(;) B) Dollor($)
C) comma(,) D) Colon(:)
6. Which of the following is not a token ?
A) Interpreter B) Identifiers
C) Keyword D) Operators
Part - II
Answer the following questions : (2 Marks)
1. What are the different modes that can be used to test Python Program ?
2. Write short notes on Tokens.
3. What are the different operators that can be used in Python ?
4. What is a literal? Explain the types of literals ?
5. Write short notes on Exponent data?
Part - III
Answer the following questions : (3 Marks)
1. Write short notes on Arithmetic operator with examples.
2. What are the assignment operators that can be used in Python?
3. Explain Ternary operator with examples.
4. Write short notes on Escape sequences with examples.
5. What are string literals? Explain.
Part - IV
Answer the following questions : (5 Marks)
1. Describe in detail the procedure Script mode programming.
2. Explain input() and print() functions with examples.
3. Discuss in detail about Tokens in Python
Learning Objectives
• To learn through the syntax how to use conditional construct to improve the efficiency of
the program flow.
• To apply iteration structures to develop code to repeat the program segment for specific
number of times or till the condition is satisfied.
6.1 Introduction
Programs may contain set of statements. These statements are the executable segments
that yield the result. In general, statements are executed sequentially, that is the statements
are executed one after another. There may be situations in our real life programming where
we need to skip a segment or set of statements and execute another segment based on the test
of a condition. This is called alternative or branching. Also, we may need to execute a set of
statements multiple times, called iteration or looping. In this chapter we are to focus on the
various control structures in Python, their syntax and learn how to develop the programs
using them.
67
Sequential
Alternative or
Branching
Iterative or Looping
Example 6.1
# Program to print your name and address - example for sequential statement
print ("Hello! This is Shyam")
print ("43, Second Lane, North Car Street, TN")
Output
Hello! This is Shyam
43, Second Lane, North Car Street, TN
In the above syntax if the condition is true statements - block 1 will be executed.
Example 6.2
# Program to check the age and print whether eligible for voting
x=int (input("Enter your age :"))
if x>=18:
print ("You are eligible for voting")
Output 1:
Enter your age :34
You are eligible for voting
Output 2:
Enter your age :16
>>>
As you can see in the second execution no output will be printed, only the Python
prompt will be displayed because the program does not check the alternative process when the
condition is failed.
Syntax:
if <condition>:
statements-block 1
else:
statements-block 2
69 Control Structures
if condition is if condition is
true condition false
Statement Statement
block -1 block -2
Exit
Fig. 6.1 if..else statement execution
if..else statement thus provides two possibilities and the condition determines which
BLOCK is to be executed.
An alternate method to rewrite the above program is also available in Python. The
complete if..else can also written as:
Syntax:
variable = variable1 if condition else variable 2
71 Control Structures
True
Body of else
Body of elif
Note
if..elif..else statement is similar to nested if statement which you have learnt in C++.
Average Grade
>=80 and above A
>=70 and <80 B
>=60 and <70 C
>=50 and <60 D
Otherwise E
Output 1:
Enter mark in first subject : 34
Enter mark in second subject : 78
Grade : D
Output 2 :
Enter mark in first subject : 67
Enter mark in second subject : 73
Grade : B
Note
In the above example of if and elif statement are both indented four spaces, which
is a typical amount of indentation for Python. In most other programming languages,
indentation is used only to help make the code look pretty. But in Python, it is required
to indicate to which block of code the statement belongs to.
73 Control Structures
Iteration or loop are used in situation when the user need to execute a block of code
several of times or till the condition is satisfied. A loop statement allows to execute a statement
or group of statements multiple times.
False
Condition
True else
Statement 1
Statement 1 Statement 2
Statement 2 ...
... Statementn
Statementn
Further
Statements
of Program
Fig 6.3 Diagram to illustrate how looping construct gets executed
Python provides two types of looping constructs:
• while loop
• for loop
XII Std Computer Science 74
Syntax:
while <condition>:
statements block 1
[else:
statements block2]
while Expression:
Statement (s)
Condition
if conditions is true
if condition is
Conditional Code
false
Example 6.6: program to illustrate the use of while loop - to print all numbers
from 10 to 15
Output:
10 11 12 13 14 15
75 Control Structures
Note
print can have end, sep as parameters. end parameter can be used when we need to give
any escape sequences like ‘\t’ for tab, ‘\n’ for new line and so on. sep as parameter can be
used to specify any special characters like, (comma) ; (semicolon) as separator between
values (Recall the concept which you have learnt in previous chapter about the formatting
options in print()).
Example 6.7: program to illustrate the use of while loop - with else part
Syntax:
for counter_variable in sequence:
statements-block 1
[else: # optional block
statements-block 2]
The for .... in statement is a looping statement used in Python to iterate over a sequence
of objects, i.e., it goes through each item in a sequence. Here the sequence is the collection of
ordered or unordered values or even a string.
XII Std Computer Science 76
In the above example 6.8(a), we have created a string “Hello World”, as the sequence.
Initially, the value of x is set to the first element of the string, (i.e. ‘H’), so the print statement
inside the loop is executed. Then, the control variable x is updated with the next element of
the string and the print statement is executed again. In this way the loop runs until the last
element of the string is accessed.
In the same way, example 6.8(b) prints the string “Hello World”, five times, until the control
variable x reaches last element of the given sequence.
Instead of creating sequence of values manually, we can use range().
The range() is a built-in function, to generate series of values between two numeric intervals.
The syntax of range() is as follows:
range (start,stop,[step])
Where,
start – refers to the initial value
stop – refers to the final value
step – refers to increment value, this is optional part.
77 Control Structures
Yes
Last item
reached?
No
Body of for
Exit loop
Fig 6.5 for loop execution
Example 6.9: #program to illustrate the use of for loop - to print single
digit even number
for i in range (2,10,2):
print (i, end=' ')
Output:
2468
Note
In Python, indentation is important in loop and other control statements. Indentation
only creates blocks and sub-blocks like how we create blocks within a set of { } in languages
like C, C++ etc.
Here is another program which illustrates the use of range() to find the sum of numbers
1 to 100
Note
for loop can also take values from string, list, dictionary etc. which will be dealt
in the later chapters.
79 Control Structures
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
False
Condition
True else
Statement 1
Statement 1 Statement 2
... ...
break Statementn
...
continue Further
... Statements
Statementn of Program
Enter loop
false
Condition
true
break?
yes
Exit loop
no
Remaining body of loop
81 Control Structures
The above program will repeat the iteration with the given “Jump Statement” as string.
Each letter of the given string sequence is tested till the letter ‘e’ is encountered, when it is
encountered the control is transferred outside the loop block or it terminates. As shown in the
output, it is displayed till the letter ‘e’ is checked after which the loop gets terminated.
One has to note an important point here is that ‘if a loop is left by break, the else part
is not executed’. To explain this lets us enhance the previous program with an ‘else’ part and
see what output will be:
Note that the break statement has even skipped the ‘else’ part of the loop and has
transferred the control to the next line following the loop block.
(ii) continue statement
Continue statement unlike the break statement is used to skip the remaining part of a
loop and start with next iteration.
Syntax:
continue