Second Internal Study Material Python
Second Internal Study Material Python
Jumping Statements:
break
Terminate the current loop. Use the break statement to come out of the loop instantly.
Have to use the keyword 'break'
Example: Output:
for i in range(9): 0
if i > 3: 1
break 2
print(i) 3
continue
Skip the current iteration of a loop and move to the next iteration
Have to use the keyword continue
Example: Output:
for i in range(9): 0
if i = = 3: 1
continue 2
print(i) 4
5
6
7
8
pass
The pass statement is used as a placeholder for future code.
Do nothing. Ignore the condition in which it occurred and proceed to run the program
as usual
When the pass statement is executed, nothing happens, but you avoid getting an error
when empty code is not allowed.
# having an empty for loop like this, would raise an error without the pass statement
2. Python Functions
my_function()
3. Recursive Function:
The function call itself is called recursive function
Example: Output:
def sum(n):
total = 0 5050
for index in range(n+1):
total = total +sum(index)
return total
result = sum(100)
print(result)
Python *args
As in the above example we are not sure about the number of arguments that can be passed to
a function. Python has *args which allow us to pass the variable number of non keyword
arguments to function.
Example: Output:
def adder(*num): Sum: 8
sum = 0 Sum: 22
for n in num: Sum: 17
sum = sum + n
print("Sum:",sum)
adder(3,5)
adder(4,5,6,7)
adder(1,2,3,5,6)
Python **kwargs
Python passes variable length non keyword argument to function using *args but we
cannot use this to pass keyword argument.
For this problem Python has got a solution called **kwargs, it allows us to pass the
variable length of keyword arguments to the function.
Example: Output:
def keyarg(**arguments): ('arg1', velu)
for arg in arguments.items(): ('arg2', 'Mithran')
print(arg) ('arg3', 'Shyam')
# function call
keyarg(arg1 ="velu", arg2 = "Mithran", arg3 ="Shyam")
5. Looping Statements:
1. While Loop in Python
A while loop is used to execute a block of statements repeatedly until a given condition is satisfied.
When the condition becomes false, the line immediately after the loop in the program is executed.
Example: Output:
count = 0 Mithran
Mithran
while (count < 3):
Mithran
count = count + 1
print("Mithran")
Example: Output:
0
n=4 1
for i in range(0, n): 2
print(i) 3
6. Explain in details about various String handling function
capitalize() Converts the first character of the string to a capital (uppercase) letter
Specifies the amount of space to be substituted with the “\t” symbol in the
expandtabs()
string
isalnum() Checks whether all the characters in a given string is alphanumeric or not
isnumeric() Returns “True” if all characters in the string are numeric characters
Returns “True” if all characters in the string are printable or the string is
isprintable()
empty
Function
Name Description
isspace() Returns “True” if all characters in the string are whitespace characters
rindex() Returns the highest index of the substring inside the string
rsplit() Split the string from the right by the specified separator
strip() Returns the string with both leading and trailing characters