Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Unit IV Python Programming

Uploaded by

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

Unit IV Python Programming

Uploaded by

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

Unit IV

FUNCTION , STRINGS & LIST


IN
Python
Function :
• Function is a block of statement that performs a task. We can use a
function to do the task of same many times. Ex
• a=5 def calc _ sum (a,b) :
• b=10 sum=a+b
• Sum=a+b print(sum)
• Print(sum) return sum

• a=2 Calc_sum (2,10)


• b=10
• Sum=a+b
• Print(sum)

• 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

Elements of list are enclosed in a square brackets are separated by


comma.
List indices also starts from 0.
Python List
Python List
Python List
Python List
Python List
Python built in Function
Python built in Function
Python built in Function
Python built in Function
Python built in Function
Python built in Function
Python built in Function

Write a python program to compute the sum of odd and even


number for a given range of list

You might also like