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

Python 5

The document provides an overview of various string methods in Python, such as isalpha(), isdigit(), isalnum(), isupper(), islower(), isspace(), and isidentifier(), along with their syntax and examples. It also explains control statements, including if, if-else, and if-elif structures, detailing how to use them with relational and logical operators. Additionally, it includes multiple programming exercises to practice these concepts.

Uploaded by

harinipb03
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 views11 pages

Python 5

The document provides an overview of various string methods in Python, such as isalpha(), isdigit(), isalnum(), isupper(), islower(), isspace(), and isidentifier(), along with their syntax and examples. It also explains control statements, including if, if-else, and if-elif structures, detailing how to use them with relational and logical operators. Additionally, it includes multiple programming exercises to practice these concepts.

Uploaded by

harinipb03
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

SOME MORE STRING METHODS:

isalpha()
Syntax
[Link]()
The isalpha() method returns True if all the characters are alphabet letters (a-z).

print("ComputEr".isalpha()) #True

print("12ComputEr$%".isalpha()) #False

isdigit() :
Syntax
[Link]()

It is a method used to check whether the object (string) is digits or not . If all the
characters are numeric digits then it returns True otherwise False.
Example :
st = “12345”
print( [Link]()) # True
print( ‘Asst78’.isdigit() ) # False
print("7896".isdigit()) #True
print("567CS".isspace()) #False
print("78 8".isspace()) #False

isalnum()
Syntax
[Link]()
The isalnum() method returns True if all the characters are alphanumeric, meaning
alphabet letter (a-z) and numbers (0-9).
Example of characters that are not alphanumeric: (space)!#%

print("ComputEr".isalnum()) #True
print("ComputEr34".isalnum()) #True
print("12ComputEr$%".isalnum()) #False
print("12Compu tEr".isalnum()) #False

isupper() :
Syntax
[Link]()

It is a method used to check whether the object (string) is upper case or not. If all the
alphabets are uppercase then it returns True otherwise False.

print("COMPUTER".isupper()) #True
print("Computer".isupper()) #False
print("COMP UTER".isupper()) #True
print("COMP UTER785@#".isupper()) #True

islower() :
Syntax
[Link]()

It is a method used to check whether the object (string) is lower case or not. If all the
alphabets are lowercase then it returns True otherwise False.

st = “kala”
print([Link]()) # False
print([Link]()) # True

isspace() :
Syntax
[Link]()

It is a method used to check whether all the characters of the string are space or not. If
they are space it returns True, else False.

print(" ".isspace()) #True


print("".isspace()) #False
print("C S".isspace()) #False
print("7 8".isspace()) #False

isidentifier()
Syntax
String. isidentifier()
Check if the strings are valid identifiers:
The isidentifier() method returns True if the string is a valid identifier, otherwise False.
A string is considered a valid identifier if it only contains alphanumeric letters (a-z)
and (0-9), or underscores (_). A valid identifier cannot start with a number, or contain
any spaces.
Example
a = "MyFolder"
b = "Demo002"
c = "2bring"
d = "my demo"

print([Link]()) #True
print([Link]()) #True
print([Link]()) #False
print([Link]()) #False
if statement / Control Statement

Relational Operators : < , > , <=, >= ,== , !=


Logical Operators : not, and, or
If statement

General syntax :
if(condition) :
Statement

 If statement is the control statement used to enforce condition.


 if is a key word
 Condition is checked. If the condition is True(Non Zero) then the
statement is executed. If condition is False(Zero) then the statement is
ignored.
 The statement can be Simple (Single) or Compound(Multiple) . The
simple statement is nothing but one single statement. Compound
statement is nothing but multiple statements(More than one statement)
 The statements to be executed if the condition is True should be given
with indentation. By default 4 spaces. ( Minimum 1 space is a must for
indentation)
 Whether the statements are simple or compound, indentation is a must
 The brackets ( ) given with condition is optional. Ie, the condition can be
given with or without brackets()
 The relational operators >, < , >=, <= , ==, != are used to compare two
quantities. Ex . if a >=b :

 Multiple conditions should be given using at least one of the logical
operator. Ex. if a>b and b > c :

 The multiple statements can be typed in the same line separated by


;(semicolon)

Example :
1. Write a program to accept a number and print its absolute value
a = int(input(“Enter a integer “))
if (a < 0) :
a = −a
print (“absolute value is = “, a)

2. Write a program to accept two integers and print the greater number
A = int( input(“Enter a integer “))
B = int( input(“Enter a integer “))
If (A > B) :
print(A, “is the greater number “)
if (B > A) :
print(B, “is the greater number “)

3. Write a program to accept a real number and check whether it is a pure


integer or not.
a = float ( input(“Enter a real “))
if (a – int(a) == 0) :
print(“It is pure integer “)
if (a – int(a) != 0) :
print(“It is number with decimal part “)

4. Write a program to accept two integers A, B and print A is bigger or B is


bigger or Both are equal
A = int( input(“Enter a integer “))
B = int( input(“Enter a integer “))
if (A > B) : print(A, “is the greater number “)
if B > A : print(B, “is the greater number “)
if A == B : print(“Both are Equal “)
5. Write a program to accept a integer and find the square of the number
if it is negative and square root if the accepted integer is positive.
6. Write a program to accept a integer and print whether it is ODD/EVEN.

Multiple condition
More than one condition can be combined using logical operators and / or /
not.
1. ( x > y ) and ( y > z) is similar to giving x >y > z
If both the conditions are true, it results to True
2. (x > y) is similar to giving not(x <y)
3. (x >y) or (y > z) if any one condition is true, it result true

7. Write a program to accept three integers A, B and C print A is bigger or B is


bigger or C is bigger or all three are equal

A = int(input("Enter the First data : "))


B= int(input("Enter the Second data : "))
C = int(input("Enter the Third data : "))
if A > B and (B > C) :
print(A," is Maximum ")
if (B > A and B > C) :
print(B," is Maximum ")
if C > A and C > B :
print(C," is Maximum ")
if A == B and B == C :
print("All are Equal" )

If..else statement :
General syntax :
if (condition) :
statement_1
else :
statement_2
 if and else are keywords
 if the condition is true(Non zero) then statement_1 is executed. If
the condition is false (Zero) then statement_2 is executed.
 The statements can be simple or compound.

Simple if Condition Simple if..else Condition

if..else (nested ) Condition (If within if condition)


Example :
8. Write a program to accept two integers and print the greaternumber
9. Write a program to accept a real number and check whether it is a pure
integer or not.

a = float ( input(“Enter a integer “))


if (a – int(a) == 0) :
print(“It is pure integer “)
else :
print(“It is Real number “)
[Link] a program to accept a integer and print whether it is
positive/negative.
[Link] a program to accept the age of a person and print whether he is
eligible to vote or NOT. (morethan 18 – eligible to vote)
[Link] a program to accept the marks in 3 subjects of a student and find
the total and average. If the average is morethan or equal to 40 then
print “PASS” else print the message “FAIL”.

If..elif statement :
If (condition_1) :
statement_1
elif (condition_2) :
statement_2
elif (condition_3) :
statement_3
elif (condition_n-1 ) :
statement_n-1
else :
statement_n

 If, elif, else are keywords


 Condition_1 is checked and if it is true, statement_1 is executed and
rest of the statements is ignored.
 condition_2 is checked and if it is true, statement_2 is executed and
rest of the statements is ignored.
 condition_3 is checked and if it is true, statement_3 is executed and
rest of the statements is ignored.
 condition_n-1 is checked and if it is true, statement_n-1 is executed and
rest of the statements is ignored.
 If all the conditions are false, it directly goes to the statement_n.
 The statement_1, statement_2…statement_n can be simple or
compound. It must be indented.
Program list – 3

[Link] to accept a integer and print whether it is divisible by 5 or not.


[Link] to accept a number and print whether it is ODD/EVEN/ZERO
[Link] to accept a number and print whether it is
POSITIVE/NEGATIVE/ZERO
[Link] to accept a number and print whether it is POSITIVE ODD/
POSITIVE EVEN / NEGATIVE ODD / NEGATIVE EVEN / ZERO
[Link] to accept an integer and print its corresponding character if the
integer is positive. Display an error message if the number is negative.
[Link] to accept a radius and height of two cylinders and compare its
volumes and display which has got higher capacity.
[Link] to accept the value of x and print the value of Y,
Y=4
Y=2
Y=6
[Link] to calculate the Ideal weight of the person. Accept the height (IN
inches), weight (pounds) and chest(inches) measurements.
Ideal weight =
If weight > ideal weight + 10 then “OVER WEIGHT”
Else If weight < ideal weight – 10 then “UNDER WEIGHT”
Else “IDEAL WEIGHT”
[Link] to accept a character and print whether it is Uppercase alphabet or
Lowercase alphabet or Other character.
22. WAP to accept a character and invert the case of the character. (If the
character is in uppercase convert to lowercase, if it is in lowercase then
convert t uppercase. Other characters to be retained as it is
[Link] to accept the values of a, b, c in the quadratic equation of the form
and print the nature of roots ( equal,
real , imaginary)
[Link] to accept the number of units consumed and calculate the
electricity
Units < =200 Rs.1.50 / unit
Units <= 500 Rs.2.75 / unit
Units <= 750 Rs.3.50 / unit
Units > 750 Rs.5.00 / unit

25. WAP to accept the number of units consumed and calculate the
electricity
For the first 200 Units, Rs.1.50 / unit
For the next 300 Units Rs.2.75 / unit
For the next 250 units Rs.3.50 / unit
For the rest of the Units Rs.5.00 / unit
26. WAP to calculate the amount, the employee should get. Accept the
name, number of hours and rate/hour from the user. Assume the
number of hours is always greater than 50.
Single time payment for 1st 75 hours as number of hours x rate/hour
1.5 times for the next 10 hours
Double the rate for the remaining hours
27..WAP to calculate the commission when the name and sales is accepted
from the user. The conditions to calculate the commission is given below
:
Sales < 1000 -> No commission
1000 < sales < 5000 -> 2% of sales
5000 < sales < 10000 -> 3 % of sales
Sales >= 10000 -> 5% of sales
28. WAP to calculate the tax when total pay is given. The conditions are
If total pay < 1000, tax = 15% of total pay or Rs175, whichever is less
If total pay >= 1000 and <1800, tax = 18% of total pay or Rs.300
whichever is less
If total pay > 1800, 25% of total pay or 450 rs. Whichever is less.

Nested If structure
If (condition_1) :
If(condition_2) :
If (condition_3) :
Statements
elif :
statements
else :
statements
elif :
statements
else :
statements
[Link] a program to accept a character. If it is an alphabet, check if it is
uppercase or lower case. If it is an uppercase alphabet check if it is a
vowel or not. If it is a lower case alphabet check if it is a consonant or
not.

Shorthand if else :
If only one statement to be executed when the condition is satisfied (Not
satisfied also) everything can be given in a single line using a shorthand
if..else notation.
Syntax : Statement_1 if (Condition) else Statement_2
Statement_1 is executed when the condition is True and if the Condition is
false Statement_2 is executed. Statement_1, Statement_2 must be a
single statement

Examples :
1. X=5 if 4 > 3 else X= 7
2. print(“Odd”) if (a % 2 != 0) else print(“Even”)
3. print(“Negative”) if (a<0) else print(“Positive”)

Program List – 4

30. Write a program to find the eligibility of a person to vote. Accept name,
age, nationality. A person is eligible to vote when he/she is more than 18
years of age and the nationality is “Indian”
31. Write a program to accept the hair and eye color of a person. If the eye
color is Blue and the hair color is Black then the person is “selected” for
the play else “Rejected”.
[Link] a program to accept the date and month. If the date is 1 and the
month is “January” then print the message “HAPPY NEW YEAR”. If the
date is 25 and the month is “December” then print the message
“MERRY CHRISTMAS”. For all other combination print “HAVE A NICE
DAY”
[Link] a flower show, the committee is decided to have only Roses
moreover they wanted the rose to be in Red or white in colour only.
Accept the name of a flower, if the accepted flower is “Rose” then
accept the colour of the flower. If the colour is Red or white then print
the message “SELECTED FOR THE FLOWER SHOW” else display the
message “SORRY”. If the flower is not Rose then print the message
“SORRY”
[Link] a restaurant one Coffee and Tea are only available as Hot drinks and
Limca and Maaza as Cold drinks. Write a program to accept the choice
from the user and print whether the drink is AVAILABLE are NOT.
[Link] a program to accept a word and check whether it is a
palindrome or not.
[Link] a program to accept a sentence & invert the case of the first and
last characters.
[Link] a program to accept a sentence and check whether the middle
character is a vowel or not if the length is odd .
[Link] a program to accept a sentence and interchange the first and the
last character in the same string.
[Link] a program to accept a sentence and print the alternate
characters in the given sentence.
[Link] a program to accept a sentence and also accept the lower and
upper index from the user. Print all the characters from the lower
index to the upper index.
******************************

You might also like