Python Module 1 Notes
Python Module 1 Notes
Module-1
1) Demonstrate with example print (), input () and string replication. (6M) [June/July 2023]
The print() Function
The print() function displays the string value inside the parentheses on the screen.
The line print ('Hello world!') means “Print out the text in the string 'Hello world!'.”
When Python executes this line, you say that Python is calling the print () function and
the string value is being passed to the function.
A value that is passed to a function call is an argument.
The quotes are not printed to the screen. They just mark where the string begins and
ends; they are not part of the string value.
Note: We can also use this function to put a blank line on the screen; just call print ()
withh nothing in between the parentheses.
The Input Function
The input() function waits for the user to type some text on the keyboard and press
ENTER
This function call evaluates to a string equal to the user’s text, and the previous line of
code assigns the myName variable to this string value.
We can think of the input() function call as an expression that evaluates to whatever
string the user typed in. If the user entered 'Al', then the expression would evaluate to
myName = 'Al'.
String Replication.
The * operator is used for multiplication when it operates on two integer or floating-
floating
point values.
But, when the * operator is used on one string value and one integer value, it becomes
the string replication operator.
The * operator can be used with only two numeric values (for multiplication) or one
string value and one integer value (for string replication). Otherwise, Python will just
display an error
ror message.
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 1
Introduction to Python Programming 2023-24
2) Develop a program to generate Fibonacci square of length (N), Read N from the console.(6M)
[June/July 2023]
Output:
3) Explain elif, for, while, break and continue statements in python with examples for each.
(8 M) [June/July 2023]
elif Statements:
While only one of the if or else clauses will execute, we may have a case where we
want one of many possible clauses to execute.
The elif statement is an “else if” statement that always follows an if or another elif
statement.
It provides another condition that is checked only if all of the previous conditions
were False.
In code, an elif statement always consists of the following:
1. The elif keyword
2. A condition (that is, an expression that evaluates to True or False)
3. A colon
4. Starting on the next line, an indented block of code (called the elif clause)
Example:
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 2
Introduction to Python Programming 2023-24
Flow Chart:
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 3
Introduction to Python Programming 2023-24
This is why range(5) results in five iterations through the clause, with i being set to 0,
then 1, then 2, then 3, and then 4.
The variable i will go up to, bu
butt will not include, the integer passed to range().
Flowchart:
Example:
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 4
Introduction to Python Programming 2023-24
In the while loop, the condition is always checked at the start of each iteration (that is,
each time the loop is executed).
If the condition is True, then the clause is executed, and afterward, the condition is
checked again.
The first time the condition
condition is found to be False, the while clause is skipped.
Break Statements:
There is a shortcut to getting the program execution to break out of a while loop’s clause
early.
If the execution reaches a break statement, it immediately exits the while loop’s clause.
In code, a break statement simply contains the break keyword.
Example:
The first line 1 creates an infinite loop; it is a while loop whose condition is always True.
True
(The expression True, after all, always evaluates down to the value True.)
The program execution will always enter the loop and will exit it only when a break
statement is executed. (An infinite loop that never exits is a common programming bug.)
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 5
Introduction to Python Programming 2023-24
Just like before, this program asks the user to type your name 2.
Now, however, while the execution is still inside the while loop, an if statement gets
executed 3 to check whether name is equal to your name.
If this condition is True, the break statement is run 4, and the execution moves out of the
loop to print('Thank you!') 5.
Otherwise, the if statement’s clause with the break statement is skipped, which puts the
execution at the end of the while loop.
At this point, the program execution jumps back to tthehe start of the while statement 1 to
recheck the condition. Since this condition is merely the TrueBoolean value, the
execution enters the loop to ask the user to type your name again.
Flowchart:
Continue statements:
Like break statements, continue statements
stat are used inside loops.
When the program execution reaches a continue statement, the program execution
immediately jumps back to the start of the loop and reevaluates the loop’s condition.
Example and Output:
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 6
Introduction to Python Programming 2023-24
If the user enters any name besides Joe 1, the continue statement 2 causes the program
execution to jump back to the start of the loop.
When it reevaluates the condition, the execution will always enter the loop, since the
condition is simply the value True. Once they make it past that if sstatement,
tatement, the
th user is
asked for a password 3.
If the password entered is swordfish, then the break statement 4 is run, and the execution
jumps out of the whilee loop to print Access granted 55.
Otherwise, the execution continues to the end of the while loo
loop,
p, where it then jumps back
to the start of the loop.
Flowchart:
4) What are User Defined Functions? How can we pass parameters in User Defined Functions?
Explain with suitable example (5 Marks) [June/July 2023]
When we call the print() or len() function, we pass in values, called arguments in this
context, by typing them between the parentheses.
We cann also define our own functions that accept arguments.
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 7
Introduction to Python Programming 2023-24
The definition of the hello() function in this program has a parameter called name 1.
A parameter is a variable that an argument is stored in when a function is called. The first
time the hello() function is called, it’s with the argument 'Alice' 3.
The program execution enters the function, and the variable name is automatically set to
'Alice', which is what gets printed by the print() statement 2.
One special thing to note about parameters is that the value stored in a parameter is
forgotten when the function returns.
5) Expalin Local and Global Scope with variables for each.(8M) [June/July 2023]
Parameters and variables that are assigned in a called function are said to exist in that
function’s local scope.
Variables that are assigned outside all functions are said to exist in the global scope.
A variable that exists in a local scope is called a local variable, while a variable that
exists in the global scope is called a global variable.
A variable must be one or the other; it cannot be both local and global.
When a scope is destroyed, all the values stored in the scope’s variables are forgotten.
There is only one global scope, and it is created when your program begins. When your
program terminates, the global scope is destroyed, and all its variables are forgotten.
A localscope is created whenever a function is called. Any variables assigned in this
function exist within the local scope. When the function returns, the local scope is destroyed,
and these variables are forgotten.
Scopes matter for several reasons:
1. Code in the global scope cannot use any local variables.
2. However, a local scope can access global variables.
3. Code in a function’s local scope cannot use variables in any other local scope.
4. We can use the same name for different variables if they are in different scopes. That is,
there can be a local variable named spam and a global variable also named spam.
Local Variables Cannot Be Used in the Global Scope
Consider this program, which will cause an error when you run it:
The error happens because the eggs variable exists only in the local scope created when
spam() is called.
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 8
Introduction to Python Programming 2023-24
Once the program execution returns from spam, that local scope is destroyed, and there is no
longer a variable named eggs.
Global Variables Can Be Read from a Local Scope
Consider the following program:
Since there is no parameter named eggs or any code that assigns eggs a value in the
spam() function, when eggs is used in spam(), Python considers it a reference to the
global variable eggs. This is why 42 is printed when the previous program is run.
6) Develop a program to read the name and year of birth of a person. Print whether the person is
a senior citizen or not. (7M) [June/July 2023]
Output:
Enter the name of person: Maha
Enter the year of birth of person: 1985
Maha is not a senior citizen.
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 9
Introduction to Python Programming 2023-24
7) Write a python program to guess the secret number between 1 to 25 within 5 guess if the
number is same then it is right guess else wrong guess. (8M) [Nov/ Dec 2023]
This is a simple “guess the number” game. When we run this program, the output will
look something like this:
Output:
Prof. Jeevika Mary. A, Asst. Prof, Dept. of ECE, Dr. TTIT, KGF Page 10