1.
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.
for x in [0, 1, 2]: Output:
pass
# having an empty for loop like this, would raise an error without the pass statement
2. Python Functions
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Creating a Function
In Python a function is defined using the def keyword:
def my_function():
print("Hello from a function")
Calling a Function
To call a function, use the function name followed by parenthesis:
def my_function():
print("Hello from a function")
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)
Advantages of using recursion
A complicated function can be split down into smaller sub-problems utilizing recursion.
Sequence creation is simpler through recursion than utilizing any nested iteration.
Recursive functions render the code look simple and effective.
Disadvantages of using recursion
Recursive functions are generally slower than non-recursive function.
It may require a lot of memory space to hold intermediate results on the system stacks.
Hard to analyze or understand the code.
It is not more efficient in terms of space and time complexity.
4. Keyword Arguments:
In Python, we can pass a variable number of arguments to a function using special
symbols. There are two special symbols:
1. *args (Non Keyword Arguments)
2. **kwargs (Keyword Arguments)
We use *args and **kwargs as an argument when we are unsure about the number of
arguments to pass in the functions.
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")
2. For loops in Python
For loops are used for sequential traversal.
for loop executes the code block until the sequence element is reached.
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
Table of Python String Methods
Function
Name Description
capitalize() Converts the first character of the string to a capital (uppercase) letter
casefold() Implements caseless string matching
center() Pad the string with the specified character.
count() Returns the number of occurrences of a substring in the string.
encode() Encodes strings with the specified encoded scheme
endswith() Returns “True” if a string ends with the given suffix
Specifies the amount of space to be substituted with the “\t” symbol in the
expandtabs()
string
find() Returns the lowest index of the substring if it is found
format() Formats the string for printing it to console
format_map() Formats specified values in a string using a dictionary
index() Returns the position of the first occurrence of a substring in a string
isalnum() Checks whether all the characters in a given string is alphanumeric or not
isalpha() Returns “True” if all characters in the string are alphabets
isdecimal() Returns true if all characters in a string are decimal
isdigit() Returns “True” if all characters in the string are digits
isidentifier() Check whether a string is a valid identifier or not
islower() Checks if all characters in the string are lowercase
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
istitle() Returns “True” if the string is a title cased string
isupper() Checks if all characters in the string are uppercase
join() Returns a concatenated String
ljust() Left aligns the string according to the width specified
lower() Converts all uppercase characters in a string into lowercase
lstrip() Returns the string with leading characters removed
maketrans() Returns a translation table
partition() Splits the string at the first occurrence of the separator
replace() Replaces all occurrences of a substring with another substring
rfind() Returns the highest index of the substring
rindex() Returns the highest index of the substring inside the string
rjust() Right aligns the string according to the width specified
rpartition() Split the given string into three parts
rsplit() Split the string from the right by the specified separator
rstrip() Removes trailing characters
splitlines() Split the lines at line boundaries
startswith() Returns “True” if a string starts with the given prefix
strip() Returns the string with both leading and trailing characters
swapcase() Converts all uppercase characters to lowercase and vice versa
title() Convert string to title case
translate() Modify string according to given translation mappings