Unit IV Python Programming
Unit IV Python Programming
• If you are writing the same program every time then the same can be
converted to defined function.
Function :
Function :
• def calc_avg (a,b,c):
• sum=a+b+c
• avg=sum/3
• print(avg)
• return avg
•calc_avg(2,3,4)
•Functions in python : 1) Built in Function 2) User defined Function
•Built in functions : print, len, type and range
•User defined functions : Which we write
• Recursion in python : it is a function that calls itself one or more times
until it encounters a base case which prevents further recursion. A
base is case which leads to problem solution without further
recursion. If base case is missing then leads to infinity.
• Local and Global Variables :
• Global variables are defined out side a python function. A global
variables can be accessed and modified any where in the in the
program.
• Local variables are defined inside the a python function. Local
variables are visible and available for modification only within the
scope of their definition.
• Preference will be given to the local variable.
• Functional Arguments in Python:
• Positional
• Keyword
• Variable
• Positional : No of arguments should be same in both function call and
function definition.
• Position should be followed. Ex
• Keyword :
• Order or the position not required
• Initialization will be done based on keyword (name)
Python Strings
Python Strings
Access String Characters in Python
We can access the characters in a string in three ways.
Indexing: One way is to treat strings as a list and use index values. For example,
Negative Indexing: Similar to a list, Python allows negative indexing for its strings.
For example,
Python Strings
Python Strings
Python Strings
Python Strings
Python String Functions :
Numerous built-in functions in Python are available to modify strings
format(): It is used to convert the framework string and the provided
values into a formatted string.
split(): To divide a string into a sequence of strings depending on a
separator, use the Python string split() function.
upper(): The Python function str.upper() allows us to transform a
string to uppercase.
lower(): This function generates a new lowercase string.
replace(): The replace() function of the Python language is used to
construct a new string by replacing some characters in an existing
string.
Python List