Allunits
Allunits
Agenda
● Features of Python
● Python Installation Procedure
● Python Shell
● IDE
● Variables
● Common Functions with Numbers
● Logical Operators
● Strings and Printing
Introduction
● Python was released under a GPL-compatible license (combining with others).
● Python Software Foundation (PSF) holds copyright of Python. ● Guido
Van Rossum was inventor.
● Python was released at Centrum Wiskunde and Informatica (CWI) in
Netherlands as a successor of ABC Lang.
● Python was named after a popular comedy show called “Monty Python’s
Flying Circus” — not by Python snake…
Features of Python
(Windows):
(Ubuntu):------present by default
To check its default presence:
1. Go to “Terminal”.
2. Type “$which python3”.
Need to get “Path” if successfully installed.
--------------e.g. “/usr/local/bin/python3”
Ubuntu OS
Terminal
To check by default
If Absent:
1. Go to “Terminal”.
2. Type “$sudo apt-get update”.
3. Type “$sudo apt-get install python 3.8”.
To check:
1. Go to “Terminal”.
2. Type “python”.
Need to get “>>>” if successfully installed.
Python Shell
To open (Windows):
1. Go to “Command Prompt” in menu.
2. Enter “python”.
If this appears “>>>” then shell activated.
IDE — Integrated Development Environment
(IDLE, Jupyter Notebook, Atom, Thonny, Pycharm, ….)
● IDE is an editor that allows to write, test, and debug the code in easy way.
● Basic TEXT editor can be used to edit. But, it fails to support automated
service:
○ Code completion
○ Highlighting code error
○ Resource management
○ …….
● IDE supports along with above three:
○ Language translator
○ Creating binary executables
○ Debugging tools
Variables
● One can define variables, assign values to them, and do algebra.
● Python interpreter returns:
Rules of Identifiers:
● The first character has to be a small or a capital letter or ‘_’ (underscore).
● The characters ‘$’ and ‘?’ cannot be used in an Identifier.
● The Identifier should not begin or end with a pair of underscores. In fact
these are reserved for specific use.
● Keywords needed to be avoided as Identifiers.
Note:
Identifiers are case sensitive; a and A are different variables.
The set of keywords in Python:
Note:
Avoiding their use directly or in combinations is a healthy programming
practice.
Operators in Python: Algebraic operators are listed in order of ascending priorities:
Useful role in
interactive sessions
Common Functions with Numbers
In any algebraic chain ‘**’—if present—will be evaluated first; then ‘%’ and so on. ‘+’
operation is the last one to be carried out.
Note:
Since the division always returns a floating point number the final result (algebra involving a mixture of integers and floating
point numbers) yields a floating number.
Logical Operators
● Operates bit-wise on integers.
Strings and Printing
Agenda
● Formatted Output
● Control Structure Statements
● Functions
● User Defined functions
● Errors and Exceptions
Formatted Output
01).
02).
Questions:
● If year is divisible by 4, print “Leap Year!!!...” ; otherwise print “not a Leap Year”. ●
If ticket is equal to lottery, print “You Won!!!...” ; otherwise print “Better Luck next
time…”.
Multi-Way Selection Statement: “if-elif” ladder
To execute multiple block of statement
based on multiple
conditions if-elif statement is used.
● The general format:
If <condition>:
<non-Indented instruction>
e.g.:
Functions
e.g.:
Another example:
Do the coding for a function that returns a 1=p to the desired accuracy. Run it for a = 50
and p = 4. The accuracy in the computed value has to be better than 0.2%.
By referring figure we can conclude:
● Many situations call for the use of single line functions. ● The
keyword ‘lambda’ facilitates this in a compact form. ● It defines
an anonymous function of the specified arguments.
Note:
situations.
e.g.:
e.g.:
Obtain the sum of the cube roots of all the integers from 3
to 8 (inclusive).
Nested Functions:
Nested Scope
● Any object in its environment in Python (like a variable, a function and c.) can
be read for its value using proper references.
● The value can be altered and reassigned in the same environment when
possible.
● Declaring an object as ‘global’ or ‘nonlocal’ makes it possible to change the
scope of access of the object for reading or reassigning in different ways.
Note:
● When ff1 is called Python
searches for a1 within ff1 first; if
not available here the scope of
search is widened to the
immediate outer domain.
● If a1 is not available there either,
the search continues in the next
outer domain and so on.
Note:
● Python provide several built-in functions to perform a specific task. ● All these
built-in functions accept zero or more input arguments and returns a result, it is
easy to call these functions using single line statement by function name and
pass required arguments to function.
● Python allow user to define their own function to accomplish the specific task.
● User-defined function contains a set of instructions grouped together under a
single name to perform a specified task.
● Like built-in functions, user-defined function accept zero or more number of
input arguments from user and return result.
General Format:
Note:
Python does not allow calling a function before it is defined.
Note:
The print statement can print the value or message but it cannot return value.
Example:
Type Error
Another Example:
In Python, Function Definitions are just like other statements.
Comments in Python: (‘#’ Symbol)