0% found this document useful (0 votes)
1 views13 pages

Class VIII Python II

Uploaded by

rahinisah36
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views13 pages

Class VIII Python II

Uploaded by

rahinisah36
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMMING

CHAPTER -6: STRING OPERATIONS

As we have already learnt that Stings are the sequences of characters enclosed
in single, double, or triple quotes. Strings can be manipulated by using operations
like concatenation, replication, membership etc.
Concatenation operator(+)

Concatenation refers to creating a new string by adding two strings. The


concatenation operator , +, concatenates strings written on both sides of the operator
and creates a new string.
For example
>>>’tea’ + ‘pot’
will result ‘teapot’

NOTE : The concatenation operator joins the two strings without space.

You can also store the result of two strings concatenation in a variable.

NOTE : You can’t combine numbers and strings with a + operator.


Operands Operation Example
data type performed by + NOTE : The + operator
Numbers Addition >>> 9 + 9 must have both operands
18 of same type, either of
Strings Concatenation >>>’9’ + ‘9’ number types (for
99 addition) or of string type
(for concatenation) . It
cannot work with one
operand as string and one
as a number.

Expression Valid / Invalid Result


print(9.5+3) Valid( float and integer) 12.5
print(4 +5) Valid( integer and integer) 9
print(“4” +”5”) Valid(string + string) 45
print(“4” +5) Invalid(string and integer) TypeError

Replication operator(*)

Replication refers to creating a new string by repeating the given string a specified
number of times.
For Example:
>>> ‘Hello’ * 3 will
result in ‘HelloHelloHello’

NOTE : While using replication operator, one operand must be a string and the other
a number.

Membership operator(+)

Python offers two membership operators for checking whether a particular character
or substring exists in the given string or not.

‘in’ operator: It returns True if the character / substring exist in the given string,
otherwise False.
‘not in’ operator: It returns True if the character / substring does not exist in the
given string, otherwise False.
Syntax:
<substring> in <string>
<substring> not in <string>
For example:
>>>”a” in ‘China’ will give True
>>>”in” in ‘China’ will give True
>>>”ch” in ‘China’ will give False, ‘ch’ is not in ‘China’(Python is a case
sensitive language)
>>>”ind” in ‘China’ will give False
>>>”ind” not in ‘China’ will give True
>>>”na” not in ‘China’ will give False
PYTHON PROGRAMMING

CHAPTER -7: CONDITIONAL STATEMENT IN PYTHON

There are situations in life when we need to decide based on a condition(s). For
example, if it rains, I will stay at home or else I shall visit the market. Similar
situations arise in programming as well, where we need to make some decisions and
based on these decisions, execute a certain set of commands. We need to determine
which action is to be taken and which statement is to be executed if the outcome is
true or false.

Conditional / Selection statements are used in a program for decision making. The
result of the computer’s decision for a condition will always be either True or False.
Decision making in Python is done by the following conditional statements:

1. if statement
2. if…else statement
3. if…elif…else ladder

if statement

In Python, we use the if statement to test a condition and decide the execution of a
block of statements based on that conditional result. The if statement checks, the given
condition then decides the execution of a block of statements. If it is True, then the
block of statements is executed and if it is False, then the block of statements is
ignored. The execution flow of if statement is as follows.

Syntax:
NOTE: When we define an ‘if’ statement, the block of statements must be specified
using indentation.
Consider the following program-

When we run the above code-


(a) The output when a number divisible by 5 is entered.

(b) The output when a number not divisible by 5 is entered.

In the above code, a%5==0 is the test expression/ condition. The body of ‘if’ block is
executed only if this condition evaluates to True. If the test expression / condition is
False, then the statements inside the ‘if’ block are skipped. Let’s take another
example:

When the above code is executed, the output is –

Number is positive
The if statement is easy
In the above example, we have created a variable named ‘number’ and assigned a
value 10 to it. Notice the test condition, here, since number is greater than 0, the
condition evaluates True.

If we change the value of variable


to a negative integer. Let's say -5.
When the above code is executed,
the output is –

The if statement is easy


This is because the value of number is less than 0. Hence, the condition evaluates to
False and, the body of the ‘if’ block is skipped. if...else Statement

The if..else statement evaluates the defined test expression and executes the body of
the ‘if’ block only when test condition is found to be True. If the condition is False, the
body of else is executed.
Note: Indentation is used to separate the “if” and “else” blocks.

The syntax of the if..else statement is given below:

if test expression:
Body of if else:
Body of else

Example of if...else
age = input(“Enter Your Age”)
if age >= 18:
print(“You are eligible to vote”
else:
print(“You are not eligible to vote”)

In the above example, if the age entered by the person is greater than or equal to 18,
then the message “You are eligible to vote “ will be printed, otherwise, “You are not
eligible to vote” will be printed.
if...elif...else Statement
The elif statement allows you to check multiple expressions for TRUE and execute a block of code as
soon as one of the conditions evaluates to TRUE.
If the condition for ‘if’ is False, it checks the condition of the next ‘elif’ block and
so on. If all the conditions are False, body
of ‘else’ is executed.
Only one block among the several
if...elif...else blocks is executed according
to the condition.

The syntax of the if..elif..else statement


is given below:

if expression1: statement(s)
elif expression2: statement(s)
elif expression3: statement(s)
else:
statement(s)
Consider the following example:

Running the above code for the following conditions- (a)


The output when a positive number is entered.

(b) The output when a negative number is entered.

(c) The output when zero is entered.


Example of if...elif...else

Output
1. Fill in the blanks:
(a) _________________ statements in Python are used for decision making.
(b) if is used for _____________________ in Python.
(c) The ‘else’ block will be executed if the condition returns to _________
(d) The ‘else’ statement is ______________(optional / mandatory) with ‘if’ block.
(e) To specify multiple conditions while making decisions, ___________
statement is used.
2. Find the output of the following code:
a=10
if a > = 10 :
print(“Your guess is correct”)
print(“Good Going!”)
3. Consider the following code.
fr=input(“Enter your favourite fruit”)
if fr = = ‘Apple’ :
print(“We got it!”)
else:
print(“We could not get!”)
What will be the output, if the user enters - (a) Apple (b) Banana

4. Consider the following code:


ex = float (input(“Enter your expenses”))
if ex >=5000:
print(“You are an extravagant”)
else:
print(“You spend your money wisely”)
print(“SAVE MONEY!”)
What will be the output, if the user enters-
(a) 2000 (b) 5000 (c) 9000
5. Consider the following code:
ex = float (input(“Enter your expenses”))
if ex > 5000:
print(“More Expenses”)
elif ex == 5000:
print(“Balanced Expenses”)
else:
print(“Less Expenses”)
print(“SAVE MONEY!”)
What will be the output, if the user enters-
(a) 2000 (b) 5000 (c) 9000

6. Consider the following code. Underline the statements which contains


error(s), and rewrite the code after correction.
num = 100
if num < 100
print(“Number less than 100”)
else if num = = 100 :
print(“Number equal to 100”)
Else :
Print(“ Number greater than 100”)
7. Programming Questions:

1. Write a program in Python to print whether a person is eligible for voting or not.

2. Write a program in Python to enter the runs scored by a batsman in a match. Based
on the scored runs by the batsman, the program should display a message “Century”
or “No Century”.

3. Write a program in Python to check whether the number entered is even or not.

4. Write a program in Python to input three numbers from the user, then print ‘You
Won’ if the sum of input numbers is greater than 1000 otherwise print “You lose”
5. Write a program to input principle amount and time and calculate simple interest.
If time is more than 10 years, calculate simple interest at the rate of 12% p.a.,
otherwise calculate it at the rate of 10% p.a.

6. Write a program to input a number and check whether it is a multiple of 2, 5 or 7.

You might also like