Python IDP
Python IDP
Tuples
Sets
Dictionary
Relations
Logical Operators
Shorthand Operator
Expressions
Type Conversion
Integer arithmetic
Truth Value
if,
if-else
Nested conditions
While loop For loop
Functions
Defining a Function
Local and global variables
Default argument
Keyword arguments
Closures
Higher order functions
map functions
Modules
Introduction to Module
Importing Modules
Submodules
Strings Methods of string objects
String module
String formatting
The pprint module the REPR module unicode
File Handling Open a file in Python.
Python file encoding.
Close a file in Python.
Python Write operation.
Python Read operation.
File read positions in Python.
Renaming and deleting files in Python.
Python file object methods.
Pickling the data
Reading file from URL
Exceptions
Try statement
NameError
TypeError
Handing Exceptions
Raising Exceptions
Finally
Custom Exception Objects
List Data Type
1. A list is a collection which is ordered and changeable.
2. Allows duplicate members
3. In Python lists are written with square brackets.
4. Heterogeneous objects are allowed
Dict Types
Result
True if x is exactly equal to y.
True if x is exactly not equal to y.
True if x (left-hand argument) is greater than y (right-hand argument).
True if x (left-hand argument) is less than y (right-hand argument).
True if x (left-hand argument) is greater than or equal to y (left-hand argument).
True if x (left-hand argument) is less than or equal to y (right-hand argument).
d y are true.
Description
Adds 2 numbers and assigns the result to left operand.
Subtracts 2 numbers and assigns the result to left operand.
Multiplies 2 numbers and assigns the result to left operand.
Divides 2 numbers and assigns the result to left operand.
Computes the modulus of 2 numbers and assigns the result to left operand.
Performs exponential (power) calculation on operators and assign value to the equivalent to left operand.
Performs floor division on operators and assign value to the left operand.
Description
Bits that are set in both x and y are set.
Bits that are set in either x or y are set.
Bits that are set in x or y but not both are set.
Bits that are set in x are not set, and vice versa.
Shift the bits of x, y steps to the left
Shift the bits of x, y steps to the right.
a sequence is presented in an object:
Example
x in y
x not in y
1 Type Conversion
Casting in python is therefore done using constructor functions:
1. int() - constructs an integer number from an integer literal, a float literal (by rounding down to the
2. float() - constructs a float number from an integer literal, a float literal or a string literal (providing
3. str() - constructs a string from a wide variety of data types, including strings, integer literals and fl
2 Truth Values
We can use any object to test the truth value. By providing the condition in the if or while statement, the checking
al (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)
or a string literal (providing the string represents a float or an integer)
trings, integer literals and float literals.
If Statement
if condition :
statement-1
statement-2
statement-3
If condition is true then statements will be executed.
if-elif Statement
if condition :
Action-1
else :
Action-2
if condition is true then Action-1 will be executed otherwise Action-2 will be executed.
if-elif-else Statement
if condition1:
Action-1
elif condition2:
Action-2
elif condition3:
Action-3
elif condition4:
Action-4
...
else:
Default Action
Based condition the corresponding action will be executed.
Transfer Statements
7.1 Break
break statement we can stop the loop before it has looped through all the items:
We can use break statement inside loops to break loop execution based on some
condition.
Continue
With the continue statement we can stop the current iteration of the loop, and continue with the next:
pass
for loops cannot be empty, but if you for some reason have a for loop with no content, put in the pass statement to
Iterative Statements
For
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
9.2 While
10 Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for
• • A variable name must start with a letter or the underscore character
• • A variable name cannot start with a number
• • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
• • Variable names are case-sensitive (age, Age and AGE are three different variables)
with the next:
a set, or a string).
me, total_volume). Rules for Python variables:
1.3 Arguments
1. Information can be passed into functions as arguments.
2. Arguments are specified after the function name, inside the parentheses.
3. You can add as many arguments as you want, just separate them with a comma.
4. The following example has a function with one argument (fname).
5. When the function is called, we pass along a first name, which is used inside the fun
g, number, list, dictionary etc.), and it will be treated as the same data type inside the function. E.g. if you send a List as an a
ur function, add two asterix: ** before the parameter name in the function definition.
ccordingly:
data hiding.
nted in a class, closures can provide an alternate and more elegant solutions.
g. if you send a List as an argument, it will still be a List when it reaches the function: