Course Title: PYTHON PROGRAMMING
Python User Input
● User Input
○ Python allows for user input.
○ That means we are able to ask the user for input.
username = input("Enter username:")
print("Username is: " + username)
Enter username: Joy University
Username is: Joy University
Python Loops
● Python has two primitive loop commands:
○ while loops
○ for loops
While Loops
The break Statement
● With the while loop we can execute a set of statements as ● With the break statement we can stop the loop even
long as a condition is true. if the while condition is true:
Example Example
Print i as long as i is less than 6: Exit the loop when i is 3:
i=1 i=1
while i < 6: while i < 6:
print(i) print(i)
i += 1 if i == 3:
break
i += 1
The else Statement
● With the else statement we can run a block of code once
The continue Statement
when the condition no longer is true: ● With the continue statement we can stop the current
iteration, and continue with the next:
Example Example
Print a message once the condition is Continue to the next iteration if i is 3:
false:
i=0
i=1 while i < 6:
while i < 6: i += 1
print(i) if i == 3:
i += 1 continue
else: print(i)
print("i is no longer less than 6")
Python Functions
● A function is a block of code which only runs when it is called.
Calling a Function
● You can pass data, known as parameters, into a function. ● To call a function, use the function name followed by
● A function can return data as a result. parenthesis
Creating a Function Example
In Python a function is defined using
the def keyword: def my_function():
print("Hello from a function")
Example
my_function()
def my_function():
print("Hello from a function")
Arguments
● Information can be passed into functions as arguments.
● Arguments are specified after the function name, inside the parentheses. You can add as many arguments as
you want, just separate them with a comma.
● The following example has a function with one argument (fname). When the function is called, we pass along a
first name, which is used inside the function to print the full name:
Example Number of Arguments
By default, a function must be called with the correct number
def my_function(fname): of arguments. Meaning that if your function expects 2
print(fname + " University") arguments, you have to call the function with 2 arguments, not
more, and not less.
my_function(“Joy")
my_function(“Enjoy") Example
my_function(“Njoy") This function expects 2 arguments, and gets 2 arguments:
def my_function(fname, lname):
print(fname + " " + lname)
my_function(“Joy", “University")
Keyword Arguments
● You can also send arguments with the key = value syntax.
● This way the order of the arguments does not matter.
Example
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = “Joy", child2 = “University", child3 = “Kanyakumari")
Default arguments
● Python allows function arguments to have default values. If the function is called without the argument, the argument
gets its default value.
● Python has a different way of representing syntax and default values for function arguments. Default
values indicate that the function argument will take that value if no argument value is passed during
the function call. The default value is assigned by using the assignment(=) operator of the
form keywordname=value.
Syntax:
def function_name(param1, param2=default_value2, param3=default_value3)
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard, 'Standard')
Example #1: Calling functions without keyword arguments
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard,
'Standard')
# 1 positional argument
student('John')
# 3 positional arguments John Mark studies in Fifth Standard
student('John', 'Gates', 'Seventh') John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
# 2 positional arguments John Seventh studies in Fifth Standard
student('John', 'Gates')
student('John', 'Seventh')
Example #2: Calling functions with keyword arguments
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard,
'Standard')
# 1 keyword argument
student(firstname ='John')
# 2 keyword arguments John Mark studies in Fifth Standard
student(firstname ='John', standard ='Seventh') John Mark studies in Seventh Standard
John Gates studies in Fifth Standard
# 2 keyword arguments
student(lastname ='Gates', firstname ='John')
Example #3: Some Invalid function calls
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard,
'Standard')
The code will throw an error because:
# required argument missing
student() In the first call, value is not passed for
parameter firstname which is the required
# non keyword argument after a keyword argument parameter.
student(firstname ='John', 'Seventh') In the second call, there is a non-keyword
argument after a keyword argument.
# unknown keyword argument In the third call, the passing keyword
student(subject ='Maths') argument is not matched with the actual
keyword name arguments.
Optional Arguments
The user can call the function by either passing those optional parameters or just passing
the required parameters.
There are two main ways to pass optional parameters in python
• Without using keyword arguments.
• By using keyword arguments.
Passing without using keyword arguments # Here b is predefined and hence is optional.
● Some main point to be taken care while passing def func(a, b=1098):
return a+b
without using keyword arguments is :
● The order of parameters should be maintained i.e. print(func(2, 2))
the order in which parameters are defined in
function should be maintained while calling the # this 1 is represented as 'a' in the function
function. and
● The values for the non-optional parameters should # function uses the default value of b
print(func(1))
be passed otherwise it will throw an error.
● The value of the default arguments can be either 4
passed or ignored. 1099
Example 2: we can also pass strings.
# Here string2 is the default string used
def fun2(string1, string2=“University"):
print(string1 + string2)
# calling the function using default value
fun2(‘Joy')
# calling without default value.
fun2(‘Joy', “University")
Joy University
Joy University
Passing with keyword arguments
def func(a, b, c=‘Joy'):
print(a, "type is", type(a))
print(b, "type is", type(b))
first call
print(c, "type is", type(c))
2 type is <class 'int'>
z type is <class 'str'>
# The optional parameters will not decide
2.0 type is <class 'float'>
# the type of parameter passed.
second call
# also the order is maintained
2 type is <class 'int'>
print("first call")
1 type is <class 'int'>
func(2, 'z', 2.0)
Joy type is <class 'str'>
third call
# below call uses the default
Joy type is <class 'str'>
# mentioned value of c
3 type is <class 'int'>
print("second call")
2 type is <class 'int'>
func(2, 1)
# The below call (in comments) will give an error
# since other required parameter is not passed.
# func('a')
print("third call")
func(c=2, b=3, a=‘Joy')
Python String Module
The Python string module provides a wide range of functions and constants related to string
manipulation. It makes our life easy when working with Python Strings. Whether we're building a text-
processing application, working with patterns, or cleaning up data, the string module simplifies many
common string operations.
Introduction to the Python String Module
The string module is part of Python’s standard library and is specifically designed for common
string operations. It provides constants representing commonly used sets of characters
(like lowercase and uppercase letters, digits, and punctuation) as well as utility functions
like capwords() for manipulating strings.
The module is useful for:
• Validating characters in a string.
• Formatting output.
• Handling templates in text processing.
We can import the string module by:
import string
Constants in the Python String Module
The string module provides several constants that represent predefined sets of characters.
These constants are useful for validating, cleaning, or manipulating strings.
1. ascii_letters
The ascii_letters
is a concatenation of all ASCII lowercase
and uppercase letters:
import string
print(string.ascii_letters)
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ
RSTUVWXYZ
Constants in the Python String Module
The string module provides several constants that represent predefined sets of characters.
These constants are useful for validating, cleaning, or manipulating strings.
2. ascii_lowercase
The ascii_lowercase contains all ASCII
lowercase letters:
import string
print(string.ascii_lowercase)
abcdefghijklmnopqrstuvwxyz
Constants in the Python String Module
The string module provides several constants that represent predefined sets of characters.
These constants are useful for validating, cleaning, or manipulating strings.
3. ascii_uppercase
The ascii_uppercase contains all ASCII
uppercase letters:
import string
print(string.ascii_uppercase)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Constants in the Python String Module
The string module provides several constants that represent predefined sets of characters.
These constants are useful for validating, cleaning, or manipulating strings.
4. digits
The digits contains all decimal digits from 0 to
9:
import string
print([Link])
0123456789
Constants in the Python String Module
The string module provides several constants that represent predefined sets of characters.
These constants are useful for validating, cleaning, or manipulating strings.
5. hexdigits
The hexdigits contains all characters used
in hexadecimal numbers (0-9 and A-F):
import string
print([Link])
0123456789abcdefABCDEF
Constants in the Python String Module
The string module provides several constants that represent predefined sets of characters.
These constants are useful for validating, cleaning, or manipulating strings.
6. octdigits
The octdigits contains all characters used
in octal numbers (0-7):
import string
print([Link])
01234567
Constants in the Python String Module
The string module provides several constants that represent predefined sets of characters.
These constants are useful for validating, cleaning, or manipulating strings.
7. punctuation
The punctuation contains all characters that are
considered punctuation marks:
import string
print([Link])
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~