Python Unit 2
Python Unit 2
❖ User-defined function:
We can create our own functions based on our
requirements.
❖ Syntax
In python each function is defined as
➢ Ex:
# A simple Python function
def max(x, y):
if(x>y):
return x
n_terms = 5
if n_terms <= 0:
print("Invalid input ! input a positive value")
else:
print("Fibonacci series:")
for i in range(n_terms):
print(recursive_fibonacci(i))
Output
Fibonacci series:
0
1
1
2
3
Output
Inside the function local total : 30
print(str[3]) print(s4)
print(str[4]) OUTPUT:
# It returns the IndexError because 6th index doesn't exist bcakle
print(str[6]) bca kle
✓ Comparison
OUTPUT:
H To compare two strings, to identify whether the two
E strings are equivalent to each other or not, or
L greater or smaller than the other.
L Name=KLE
O Name1=BCA
IndexError: string index out of range print("Are name and name 1 equal?")
print (name == name2)
❖ The str()function OUTPUT:
Python str() function returns the string version of the Are name and name 1 equal False
object.
Syntax: str(object, encoding=’utf-8?, errors=’strict’) ✓ Slicing
Parameters:
You can return a range of characters by using the slice
• object: The object whose string representation is to
syntax.
be returned.
Specify the start index and the end index, separated by
• encoding: Encoding of the given object.
a colon, to return a part of the string.
• errors: Response when decoding fails.
Example:
# Python program to demonstrate strings String = 'ASTRING'
✓ Join Python1
The string join() method returns a string by joining Python2
Python3
all the elements of an iterable (list, string, tuple),
\\ Backslash print("\\")
separated by the given separator. Output:
Example: \
text = ['Python', 'is', 'a', 'fun', 'programming', \' Single Quotes print('\'')
'language'] Output:
'
\\'' Double Quotes print("\"")
# join elements of text with space Output:
print(' '.join(text)) "
Output: \a ASCII Bell print("\a")
Python is a fun programming language \b ASCII print("Hello
\b World")
Backspace(BS)
✓ Traversing Output:
Hello World
Example 1:
\f ASCII Formfeed print("Hello
string_name = "Pythonprogram" \f World!")
Hello World!
# Iterate over the string \n ASCII Linefeed print("Hello
\n World!")
for element in string_name: Output:
print(element, end=' ') Hello
print("\n") World!
\r ASCII Carriege print("Hello
\r World!")
output: Return(CR)
Output:
Pythonprogram World!
Output:
❖ Python String Methods.
P
Method Description
y
capitalize() It capitalizes the first character of the
t String.
h isalnum() It returns true if the characters in the string
o are alphanumeric
n isalpha() It returns true if all the characters are
alphabets and there is at least one
character, otherwise False.
❖ Escape Sequences
isdecimal() It returns true if all the characters of the
Escape Description Example string are decimals.
Sequence isdigit() It returns true if all the characters are
\newline It ignores the new print("Python1
digits and there is at least one character,
\
line. otherwise False.
Python2 \
Python3") islower() It returns true if the characters of a string
Output: are in lower case, otherwise false.