0% found this document useful (0 votes)
4 views

Done By: Bhavya Harshini .D 11504

Uploaded by

Medha Harini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Done By: Bhavya Harshini .D 11504

Uploaded by

Medha Harini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

DONE BY : BHAVYA HARSHINI .

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"

text = "hello world"


string.capitali capitalized_text = text.capitalize()
converts the first character of the string to
ze() uppercase and the rest to lowercase.
print(capitalized_text) # Output: "Hello
world"

text = "hello world, hello everyone"


string.count() returns the number of times a specified substring count_hello = text.count("hello")
appears in the string. print(count_hello) # Output: 2

text = "hello world"


position = text.find("world")
string.find() returns the position (index) of the first occurrence print(position) # Output: 6
of a specified substring, or -1 if not found.
per() Returns: True if all
alphabetic
converts all characters in the
converts all characters in the string are
characters in the string to lowercase,
lowercase. otherwise False.
string to
uppercase. text = "hello
text = world"
text = "hello "HELLO print(text.islower
world" WORLD" ()) # Output:
True
uppercase_te lowercase_te
xt = xt = text2 = "Hello
text.upper() text.lower() World"
print(text2.islow
print(upperca print(lowerc er()) # Output:
se_text) ase_text) False
string.replace() replaces all text = "hello world"
occurrences of a replaced_text =
text.replace("world",
specified substring
"everyone")
with a new print(replaced_text) #
substring. Output: "hello
everyone"

string.split() splits the string at text = "apple,banana”


each occurrence of split_text = text.split(",")
print(split_text) # Output:
the specified
['apple', 'banana']
delimiter and returns
a list of parts.
string.partition() splits the string into text = "hello world"
three parts at the first partitioned_text =
occurrence of a text.partition(" ")
specified separator. print(partitioned_text)
# Output: ('hello', ' ',
'world')
string.index()

returns the index of the first


occurrence of a specified substring
and raises an error if the substring is
not found. separator.join()
The .join() method takes all elements of
text = "hello world" an iterable (like a list) and joins them
index_of_world = text.index("world") into a single string, using the specified
print(index_of_world) # Output: 6 separator between each element.

words = ["hello", "world"]


joined_text = " ".join(words)
print(joined_text) # Output: "hello world"

You might also like