PROGRAMMING IN PYTHON
MCA-161A
4 Credits (3-0-2)
MCA 5
th
Sem (2020-21)
R K Dwivedi
Assistant Professor
Department of ITCA
MMMUT Gorakhpur
UNIT II: Control Flow and Other Programming Concepts
1.IterativeStatements:
ForLoops,WhileLoops,Break,Continue
2.Array:
LoopingArrayelements,Arraymethods
3.Functions:
LocalandGlobalVariables,
Definingandcallingthefunction,
Functionswitharguments,
Recursion
01-10-2020 Side 3
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1.IterativeStatements
01-10-2020 Side 4
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
ForLoop
01-10-2020 Side 5
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
ForLoop(using‘break’and‘continue’)
01-10-2020 Side 6
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
ForLoop(using‘else’)
01-10-2020 Side 7
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
ForLoop(NestedLoop)
01-10-2020 Side 8
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
WhileLoop
01-10-2020 Side 9
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
WhileLoop(using‘break’and‘continue’)
01-10-2020 Side 10
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
WhileLoop(using‘else’)
01-10-2020 Side 11
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
1. Iterative Statements
WhileLoop(NestedLoop)
01-10-2020 Side 12
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Some Important Concepts of “Range” within“Loops”
1. Iterative Statements
01-10-2020 Side 13
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Some Important Concepts of “Range” within“Loops” …continued
1. Iterative Statements
01-10-2020 Side 14
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2.Array
01-10-2020 Side 15
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Array (Looping Array Elements and Array Methods) …continued
01-10-2020 Side 16
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
2. Array (Looping Array Elements and Array Methods)
01-10-2020 Side 17
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Some Important Concepts of “Strings”
2. Array
01-10-2020 Side 18
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
3.Functions
01-10-2020 Side 19
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Defining and Calling a function
3.Function
01-10-2020 Side 20
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Passing Arguments
3. Function
If the number of arguments is unknown, add a * before the parameter name.
01-10-2020 Side 21
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Passing Arguments …continued
3. Function
You can also send arguments with the “key = value” syntax. Here, order of the arguments does not matter.
If you do not know how many “keyword arguments” will be passed in the function, add ** before the parameter name in the
function definition. Here, the function will receive a dictionary of arguments, and can access the items accordingly:
01-10-2020 Side 22
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Passing Arguments: Default Argument …continued
3. Function
If we call the function without argument, it uses the default value given duringfunction definition.
01-10-2020 Side 23
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Passing Arguments: Passing a List as an Argument …continued
3. Function
You can send any data types of argument to a function (string, number, list, dictionary etc.)
01-10-2020 Side 24
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
“return” and “pass” statements
3. Function
01-10-2020 Side 25
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Local and Global Variables
3. Function
01-10-2020 Side 26
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Recursion
3. Function
01-10-2020 Side 27
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
SomeProgrammingExercises
01-10-2020 Side 28
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
1. Write a program to check a number Evenor Odd.
01-10-2020 Side 29
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
2. Write a program to Weekdaysfor the corresponding number.
01-10-2020 Side 30
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
3. Write a program for printing the Star Triangle using Nested for
loop.
01-10-2020 Side 31
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
4. Write a program to check a number Primeor not.
01-10-2020 Side 32
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
5. Write a program to check a number Armstrongor not.
01-10-2020 Side 33
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
6. Write a program to Reversea string
01-10-2020 Side 34
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
7. Write a program to check a string whether Palindromeor not.
01-10-2020 Side 35
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
8. Write a program to Read & Printa Matrix.
01-10-2020 Side 36
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
9. Write a program to Transposea matrix.
01-10-2020 Side 37
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
10. Write a program for Matrix Multiplication in Python
01-10-2020 Side 38
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
11. Write a program to print Factorial using Function
01-10-2020 Side 39
MadanMohan MalaviyaUniv. of Technology, Gorakhpur
Programming Exercise
12. Write a program to print Fibonacci Sequence using Recursion