Functions
Functions
Functions
1. “def” Statements with Parameters
2. Return Values and return Statements
3. The None Value
4. Keyword Arguments and print()
• Python provides several builtin functions like print(), input(), and len() etc.,
Function calls
• For example,
• end to specify what should be printed at the end of its arguments
• Parameters and variables that are assigned in a called function are exist in that
function’s local scope called a local variable.
• Variables that are assigned outside all functions are said to exist in the global
scope is called a global variable.
• But not 0
1. Exception Handling
2. A Short Program: Guess the Number
• Getting an error, or exception, in program means the entire program will crash.
• Instead, the program should detect errors, handle them, and then continue to run.
• Just as string values are typed with quote characters to mark where the string begins and ends, a
list begins and ends with square bracket, [].
• Values inside the list are also called items. Items are separated with commas ,
• The value [] is an empty list that contains no values, similar to '', the empty string.
print(currencies)
# create a list
numbers = [2, 3, 5, 2, 11, 2, 7]
# check the count of 2
count = numbers.count(2)
print('Count of 2:', count)
# Output: Count of 2: 3
23PLCS21- Introduction to Python Programming Dr.Pallavi H B
9.sort()
• sort items in a list in ascending order
prime_numbers = [11, 3, 7, 5, 2]
# sort the list
prime_numbers.sort()
print(prime_numbers)
# Output: [2, 3, 5, 7, 11]
# mixed list
prime_numbers = [2, 3, 5]
# copying a list
numbers = prime_numbers.copy()
print('Copied List:', numbers)
# Output: Copied List: [2, 3, 5]
output
Example for i in [0, 1, 2, 3]: 0
for i in range(4): print(i) 1
print(i) 2
3
It loop through its clause with the variable i set to a successive value
in the [0, 1, 2, 3] list in each iteration
When there are duplicates of the value in the list, the index of its first appearance is returned.
append() insert()
If the value appears multiple times in the list, only the first instance of the value will be removed.
***R***
* * *E * * *
***J***
***I***
spam = cheese
Output
('color', 'red')
('age', 42)
>>> spam.keys()
dict_keys(['color', 'age'])
>>> list(spam.keys())
['color', 'age']
• A Tic-Tac-Toe Board
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
>>> spam =
'SpamSpamBaconSpamEggsSpamSpa
>>> spam = ' Hello World '
m'
>>> spam.strip()
>>> spam.strip(‘mapS')
'Hello World'
'BaconSpamEggs'
>>> spam.lstrip()
'Hello World '
>>> spam.rstrip()
‘ Hello World'23PLCS21- Introduction to Python Programming Dr.Pallavi H B
Useful String Methods
• Copying and Pasting Strings with the pyperclip Module
• The pyperclip module has copy() and paste() functions that can send text to
and receive text from your computer’s clipboard.
• Sending the output of your program to the clipboard will make it easy to paste
it to an email, word processor, or some other software.
if account in PASSWORDS:
pyperclip.copy(PASSWORDS[account])
print('Password for ' + account + ' copied to clipboard.')
else:
print('There is23PLCS21-
no account named
Introduction ' + account)
to Python Programming Dr.Pallavi H B
Project: Adding Bullets to Wiki
Markup
• Step 1: Copy and Paste from the Clipboard
• Step 2: Separate the Lines of Text and Add the Star
• Step 3: Join the Modified Lines
import pyperclip
text = pyperclip.paste()
• compile function returns a Regex pattern object (or simply, a Regex object).
• search function:- scan through the given string/sequence, looking for the first
location where the regular expression produces a match.
• group function returns the string matched by the re
Example Program
import re
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('My number is 415-555-4242 and 431-444-242.')
print('Phone number found: ' + mo.group())
Output
Phone number found: 415-555-4242
18CS55
More Pattern Matching with Regular
Expressions
• Grouping with Parentheses
>>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
>>> mo = phoneNumRegex.search('My number is 415-555-4242.')
>>> mo.group(1)
'415' >>> mo.groups()
>>> mo.group(2) ('415', '555-4242')
'555-4242' >>> areaCode, mainNumber = mo.groups()
>>> mo.group(0) >>> print(areaCode)
'415-555-4242' 415
>>> mo.group() >>> print(mainNumber)
'415-555-4242' 555-4242
(Ha){3}
(Ha)(Ha)(Ha)
(Ha){3,5}
((Ha)(Ha)(Ha))|((Ha)(Ha)(Ha)(Ha))|((Ha)(Ha)(Ha)(Ha)(Ha))
18CS55
More Pattern Matching with Regular
Expressions
• Matching Specific Repetitions with Curly Brackets