Done By: Bhavya Harshini .D 11504
Done By: Bhavya Harshini .D 11504
D
11504
Table of contents
Syntax and example
to
01 02
illustrate various Syntax and
forms of if example of for
statements loop and while
loop
03
Syntax and example
with explanation of
all string methods
Various Forms of if Statements
if If-else
If-elif-else Nested if
if Statement Syntax:
Syntax:
if condition:
# code to execute if condition is true
Example:
num = 10
if num > 5:
print('Number is greater than 5')
if-else Statement:
if condition:
# code if true
else:
# code if false
Example:
num = 3
if num > 5:
print('Number is greater than 5')
else:
print('Number is less than or equal to
5')
Syntax:
if-elif-else Statement
if condition1:
# code if condition1 is true
elif condition2:
# code if condition2 is true
else:
# code if both are false
Example:
num = 10
if num == 5:
print('Number is 5')
elif num == 10:
print('Number is 10')
else:
print('Number is something else')
Nested if Statement:
if condition1:
# block of code if condition1 is true
if condition2:
# block of code if condition2 is also true
else:
# block of code if condition2 is false
else:
# block of code if condition1 is false
num = 10
if num > 0:
print("The number is positive")
if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")
else:
print("The number is negative or zero")
Syntax and example of for loop and while
loopFOR LOOP
WHILE LOOP Syntax:
Syntax: for variable in sequence:
while condition: # code block to
# code block to execute execute
Example: Example:
count = 0 for i in range(5):
while count < 5: print(i)
print(count)
count += 1
String Methods
Example:
len(): Returns a= "Hello, World!“
length = len(a)
length of the print("Length of the
string string:", length)
Syntax:
len(object)
text = " "
print(text.isspace()) # Output:
True
string.isspace() checks if the string only contains
whitespace.
text2 = "Hello World"
print(text2.isspace()) # Output:
False
text = "HELLO WORLD"
print(text.isupper()) # Output:
True
string.isupper() checks if all alphabetic characters are
uppercase.
text2 = "Hello World"
print(text2.isupper()) # Output:
False
text = "HelloWorld"
checks if the string contains only
string.isalpha() print(text.isalpha()) # Output:
alphabetic characters (letters, no
True
spaces or numbers).
text = "Hello123"
string.isdi
string.st string.endswi
git()
string.istitl artswith( th()
checks if all e() )
characters in the checks if the
string are digits string ends with
checks if the string a specific
(0-9). is in title case
checks if the substring.
(every word starts
with a capital string starts
text = "12345" with a specific text = "Hello
print(text.isdigit letter). World"
substring. print(text.endswi
()) # Output:
text = "Hello World" th("World")) #
True
print(text.istitle()) # Output: True
Output: True text = "Hello
text2 = World" text2 = "Hello
"123abc" print(text.start Everyone"
text2 = "hello world"
print(text2.isdig print(text2.istitle()) # swith("Hello")) print(text2.ends
it()) # Output: Output: False (not # Output: True with("World")) #
False (contains title case) Output: False
letters)
text = "hello world"
title_text = text.title()
.title() Converts the first character of each word to
print(title_text) # Output: "Hello
uppercase.
World"