Module - 1-1
Module - 1-1
of CSE, NIEIT
Application Development
using Python
MODULE 1
• PYTHON BASICS
• FLOW CONTROL
• FUNCTIONS
Python is widely used even when it is somehow slower than other languages because:
Easy to learn and code
Free and high level language (easy to read)
Portable
Object Oriented
Large collection of packages and modules
Interpreted (each and every line can be executed directly without compiling the whole code)
There are several ways to run the code, but the following 3 ways are the most popular:
1. Text Editor (eg: Sublime text, Atom etc)
1. They are not exclusively for Python.
2. Full IDE’s (eg: PyCharm , Spyder etc)
1. Best used for larger program with Python specific functionalities
3. Notebook Environments (eg: Jupyter Notebook)
1. Great for learning, seeing input and output next to each other, visualization and mark-down
notes (doesn’t use the same .py file extension though)
Sublime Text
Anaconda Navigator
Python programs can also have text values called strings, or strs (pronounced “stirs”).
Always surround your string in single quote (') characters (as in 'Hello' or 'Goodbye cruel world!') so
Python knows where the string begins and ends.
You can even have a string with no characters in it, '', called a blank string.
The str(), int(), and float() functions will evaluate to the string, integer, and
floating-point forms of the value you pass, respectively.
Note that if you pass a value to int() that it cannot evaluate as an integer, Python will display
an error message.
int() can also be used to round off floating values
You almost never want your programs to start from the first line of code and
simply execute every line, straight to the end.
Flow control statements can decide which Python instructions to execute
under which conditions.
In a flowchart, there is usually more than one way to go from the start to the end. The same is
true for lines of code in a computer program.
Flowcharts represent these branching points with diamonds, while the other steps are
represented with rectangles. The starting and ending steps are represented with rounded
rectangles.
We first need to learn how to represent those yes and no options, and you need to understand
how to write those branching points as Python code
The and, or, and not operators are called Boolean operators because they always operate on the
Boolean values True and False.
While expressions like 4 < 5 aren’t Boolean values, they are expressions that evaluate down to
Boolean values.
Flow control statements often start with a part called the condition, and all are followed by
a block of code called the clause.
Condition
Conditions are arithmetic or Boolean operations which always evaluate down to a Boolean
value, True or False.
A flow control statement decides what to do based on whether its condition is True or False.
if statements
An if statement’s clause (that is, the block following the if statement) will execute
if the statement’s condition is True.
else statements
The else clause is executed only when the if statement’s condition is False.
elif statements
While only one of the if or else clauses will execute, you may have a case where
you want one of many possible clauses to execute.
break
If the execution reaches a break statement, it immediately
exits the while loop’s clause
continue
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.
pass
It is a null statement. The interpreter does not ignore
a pass statement, but nothing happens and the statement
results into no operation(eg: abstract functions)
What if you want to execute a block of code only a certain number of times?
The first time it is run, the variable i is set to 0. The print() call in the clause will print Jimmy
Five Times (0).
Eg:
All Python programs can call a basic set of functions called built-in functions, including the print(),
input(), and len() functions you’ve seen before.
Python also comes with a set of modules called the standard library.
The last flow control concept to cover is how to terminate the program.
This always happens if the program execution reaches the bottom of the instructions.
However, you can cause the program to terminate, or exit, by calling the sys.exit() function.
Eg:
As you can see, it is similar to if/while statement declaration but with def keyword
When you call the print() or len() function, you pass in values, called arguments in this context, by
typing them between the parentheses.
You can also create your own functions which takes these arguments.
When you call the len() function and pass it an argument such as 'Hello', the function call
evaluates to the integer value 5, which is the length of the string you passed it.
In general, the value that a function call evaluates to is called the return value of the function.
A return statement consists of the following:
The return keyword
The value or expression that the function should return
In Python there is a value called None, which represents the absence of a value.
None is the only value of the NoneType data type (similar to Null, Nil, Void or undefined).
This value-without-a-value can be helpful when you need to store something that won’t be confused for a real
value in a variable.
Behind the scenes, Python adds return None to the end of any function definition with no return statement.
Also, if you use a return statement without a value (that is, just the return keyword by itself), then None is
returned.
Dept. of CSE, NIEIT
Keyword Arguments and print()
The first position indicates the starting point and the second one indicates the end point.
However, keyword arguments are identified by the keyword put before them in the function
call.
Keyword arguments are often used for optional parameters. (similar to default values of a
parameter that you learnt in Java).
You can also pass multiple strings and also, change the default separator using sep keyword argument
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.
If you need to modify a global variable from within a function, use the global statement.
It tells Python, “In this function, eggs refers to the global variable, so don’t create a local variable
with this name.”
Output:
There are four rules to tell whether a variable is in a local scope or global scope:
1. If a variable is being used in the global scope (that is, outside of all functions), then it is always a
global variable.
2. If there is a global statement for that variable in a function, it is a global variable.
3. Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
4. But if the variable is not used in an assignment statement, it is a global variable.
Output:
If you try to use a local variable in a function before you assign a value to it, as in the following
program, Python will give you an error
**Note: But in Python you can use the global variable in a function before it is assigned.
Right now, getting an error, or exception, in your Python program means the entire program will
crash. You don’t want this to happen in real-world programs.
print(spam(0)) throws an error and the program exits before executing print(spam(1))
The code that could potentially have an error is put in a try clause. The program execution moves to
the start of a following except clause if an error happens.
Output:
The reason print(spam(1)) is never executed is because once the execution jumps to the code in the
except clause, it does not return to the try clause.