Py in
Py in
Python is used in various software domains some application areas are given below.
Python provides various web frameworks to develop web applications. The popular python web
frameworks are Django, Pyramid, Flask.
It is Extensible
Extensible: It is very flexible and extensible with any module.
Object-oriented
Object-oriented: Python allows to implement the Object-Oriented concepts to build
application solution.
PEP 8 stands for Python Enhancement Proposal, it can be defined as a document that helps us
to provide the guidelines on how to write the Python code. It is basically a set of rules
that specify how to format Python code for maximum readability.
Variable is a name that is used to refer to memory location. Python variable is also known
as an identifier and used to hold value.
There are two types of variables in Python - Local variable and Global variable.
Local variables are the variables that declared inside the function and have scope within
the function
Global variables can be used throughout the program, and its scope is in the entire program.
We can use global variables inside or outside the function.
A variable declared outside the function is the global variable by default.
Python breaks each statement into a sequence of lexical components known as tokens
Literals are the fixed values used in a source code.Literals can be defined as a data which
is given in a variable or constant.
Numeric Literals: These are the literals written in form of numbers. Python supports the
following numerical literals:
Integer Literal: It includes both positive and negative numbers along with 0. It doesn’t
include fractional parts. It can also include binary, decimal, octal, hexadecimal literal.
Float Literal: It includes both positive and negative real numbers. It also includes
fractional parts.
Complex Literal: It includes a+bi numeral, here a represents the real part and b represents
the complex part.
Boolean literals have only two values in Python. These are True and False.
Special Literals: Python has a special literal ‘None’. It is used to denote nothing, no
values, or the absence of value.
Character Literals: Character literal is also a string literal type in which the character
is enclosed in single or double-quotes.
Literals Collections: Literals collections in python includes list, tuple, dictionary, and
sets.
List:
List contains items of different data types. Lists are mutable i.e., modifiable.
The values stored in List are separated by comma(,) and enclosed within square brackets([]).
We can store different types of data in a List.
Dictionary:
Tuple:
Set:
Python Operators
The operator can be defined as a symbol which is responsible for a particular operation
between two operands.
Arithmetic operators
Comparison operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic operators are used to perform arithmetic operations between two operands. It
includes + (addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor
division), and exponent (**) operators.
Comparison operators are used to comparing the value of the two operands and returns Boolean
true or false accordingly.
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition
becomes true.
>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.
> If the first operand is greater than the second operand, then the condition becomes
true.
< If the first operand is less than the second operand, then the condition becomes
true.
The assignment operators are used to assign the value of the right expression to the left
operand. The assignment operators are described in the following table.
Operator Description
= It assigns the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 10, b = 20 => a+ = b
will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 20, b = 10 => a- = b
will be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and
assigns the modified value back to then the left operand. For example, if a = 10, b = 20 =>
a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and
assigns the reminder back to the left operand. For example, if a = 20, b = 10 => a % = b
will be equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 =
16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign
4//3 = 1 to a.
The bitwise operators perform bit by bit operation on the values of the two operands
Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied
to the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the
resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the
resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is
0, the resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the
right operand.
>> (right shift) The left operand is moved right by the number of bits present in the
right operand.
The logical operators are used primarily in the expression evaluation to make a decision.
Operator Description
and If both the expression are true, then the condition will be true. If a and b are the
two expressions, a → true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a and b are
the two expressions, a → true, b → false => a or b → true.
not If an expression a is true, then not (a) will be false and vice versa.
Python membership operators are used to check the membership of value inside a Python data
structure. If the value is present in the data structure, then the resulting value is true
otherwise it returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the second operand
(list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand
(list, tuple, or dictionary).
The identity operators are used to decide whether an element certain class or type.
Operator Description
is It is evaluated to be true if the reference present at both sides point to the same
object.
is not It is evaluated to be true if the reference present at both sides do not point to
the same object.
Keywords are reserved words with fi xed meanings assigned to them. Keywords cannot be used
as identifiers or variables.
Python contains thirty-five keywords in the most recent version, i.e., Python 3.8. Here we
have shown a complete list of Python keywords for the reader's reference.
FUNCTIONS
A function is a self-contained block of one or more statements that perform a special task
when called.
A function’s defi nition in Python begins with the def keyword followed by the function’s
name,
parameter and body.
The function header may contain zero or more number of parameters.
Parameters are the names that appear in a function’s defi nition.
Arguments are the values actually passed to a function while calling a function.
Arguments to a function can be passed as positional or keyword arguments.
The arguments must match the parameters in order, number and type as defi ned in the
function.
A variable must be created before it is used.
Variables defi ned within the scope of a function are said to be local variables.
Variables that are assigned outside of functions are said to be global variables.
The return statement is used to return a value from a function.
Functions in Python can return multiple values.
Python also supports a recursive feature, i.e. a function can be called repetitively by
itself
Built-In Functions: copy(), len(), count() are the some built-in functions.
User-defined Functions: Functions which are defined by a user known as user-defined
functions.
Anonymous functions: These functions are also known as lambda functions because they are not
declared with the standard def keyword.
Pass by references
Pass by value
By default, all the parameters (arguments) are passed "by reference" to the functions. Thus,
if you change the value of the parameter within a function, the change is reflected in the
calling function as well.
The pass by value is that whenever we pass the arguments to the function only values pass to
the function, no reference passes to the function. It makes it immutable that means not
changeable
Read-only mode (r): Open a file for reading. It is the default mode.
Write-only mode (w): Open a file for writing. If the file contains data, data would be lost.
Other a new file is created.
Read-Write mode (rw): Open a file for reading, write mode. It means updating mode.
Append mode (a): Open for writing, append to the end of the file, if the file exists.
An operator is a particular symbol which is used on some values and produces an output as a
result. An operator works on operands. Operands are numeric literals or variables which hold
some values. Operators can be unary, binary or ternary. An operator which requires a single
operand known as a unary operator, which require two operands known as a binary operator and
which require three operands is called ternary operator.
iterators are used to iterate a group of elements, containers like a list. Iterators are
the collection of items, and it can be a list, tuple, or a dictionary. Iterators are
objects which can be traversed though or iterated upon
Slicing is a mechanism used to select a range of items from sequence type like list, tuple,
and string. It is beneficial and easy to get elements from a range by using slice way. It
requires a : (colon) which separates the start and end index of the field.
The Python index() method helps you find the index position of an element or an item in a
string of characters or a list of items. The index() method returns the index of the
specified element in the list.
An array is defined as a collection of items that are stored at contiguous memory locations.
It is a container which can hold a fixed number of items, and these items should be of the
same type
A combination of arrays saves a lot of time. The array can reduce the overall size of the
code.
print( ) function-
type( ) function -The type() function returns the type of the specified object.
input( ) function -The input() function allows taking the input from the user.
abs( ) function - The abs() function returns the absolute value of the specified number.
pow( ) function - The pow() function returns the calculated value of x to the power of y
dir( ) function-The dir() function returns all the properties and methods of the specified
object, without the values.
sorted( ) function-The sorted() function returns a sorted list of the specified iterable
object.
max( ) function-The max() function returns the item with the maximum value or the item with
the maximum value in an iterable.
round( ) function-The round() function returns a floating-point number that is a rounded
version of the specified number, with the specified number of decimals
len( ) function=The len() function returns the count of items present in a specified object.
sum( ) function-The sum() function returns a number, the sum of all items in an iterable.
help( ) function-The help() function is used to display the documentation of modules,
functions, classes, keywords, etc.