Python Notes
Python Notes
Computer
Programming II
It is used for:
● web development (server-side),
● software development,
● mathematics,
● system scripting
What can Python do?
• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and
modify files.
• Python can be used to handle big data and perform complex
mathematics.
• Python can be used for rapid prototyping, or for production-
ready software development.
Why Python?
• Python works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with
fewer lines than some other programming languages.
• Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping
can be very quick.
• Python can be treated in a procedural way, an object-oriented
way or a functional way.
Python Syntax compared to other
programming languages
Python Syntax compared to other programming languages
Example:
Output
Python Syntax
Creating a python file on the server, using the .py file
extension
Example:
Python Indentation
Python Indentation
• To use a list, you must declare it first. Do this using square brackets
and separate values with commas.
list1=[1,2,3,4,5,'A’,"B",7,8,[9,10]]
print(list1)
Output:
Expression
Expression
An expression is a combination of values, variables, and operators.
An expression is evaluated using assignment operator.
Example:
Y=x + 17
Types of Expression
in Python
Constant Expression
A constant expression in Python that contains only constant values is known
as a constant expression. In a constant expression in Python, the
operator(s) is a constant. A constant is a value that cannot be changed
after its initialization.
Arithmetic Expression
An expression in Python that contains a combination of
operators, operands, and sometimes parenthesis is known as
an arithmetic expression.
Arithmetic Expression
Before getting into the
example of an
arithmetic expression
in Python, let us first
know about the
various operators
used in the arithmetic
expressions.
Arithmetic Expression Example:
Integral Expression
An integral expression in Python is used for computations and
type conversion (integer to float, a string to integer, etc.). An
integral expression always produces an integer value as a
resultant.
Integral Expression
Floating Expression
Floating Expression
# we need to convert the floating-point number into an integer or vice versa
for summation. result = x + int(y)
Relational Expression
A relational expression in Python can be considered as a combination of two or
more arithmetic expressions joined using relational operators. The overall
expression results in either True or False (boolean result). We have four types of
relational operators in Python (i.e.>,<,>=,<=).
A relational operator produces a boolean result so they are also known as Boolean
Expressions.
For example :
10+15>20
Relational Expression
In this example, first, the arithmetic expressions (i.e. 10+1510+15 and 2020) are
evaluated, and then the results are used for further comparison.
The expression
checks if the sum
of (a and b) is the
same as the
difference of (c
and d).
Logical Expression
As the name suggests, a logical expression performs the logical computation, and
the overall expression results in either True or False (boolean result). We have
three types of logical expressions in Python, let us discuss them briefly.
Logical Expression
As the name suggests, a logical expression performs the logical computation, and
the overall expression results in either True or False (boolean result). We have
three types of logical expressions in Python, let us discuss them briefly.
Bitwise Expression
The expression in which the operation or computation is performed at the bit level is
known as a bitwise expression in Python. The bitwise expression contains the
bitwise operators.
Combinational Expression
As the name suggests, a combination expression can contain a single or multiple
expressions which result in an integer or boolean value depending upon the
expressions involved.
Statement
Statement
A statement is an instruction that a Python
Example of Assign statement:
interpreter can execute. So, in simple words, we
can say anything written in Python is a
statement.
Python statement ends with the token
NEWLINE character. It means each line in a
Python script is a statement.
For example, a = 10 is an assignment
statement. where a is a variable name and 10 is
its value. There are other kinds of statements
such
as if statement, for statement, while statement,
etc., we will learn them in the following lessons.
There are mainly four types of statements in
Python, print statements, Assignment
statements, Conditional statements,
Looping statements.
The Difference of
Statement and
Expression
The Difference of Statement and
Expression
Operators
Arithmetic operator
Arithmetic operators are the most commonly used. The
Python programming language provides arithmetic
operators that perform addition, subtraction,
multiplication, and division. It works the same as basic
mathematics.
Arithmetic operator
There are seven arithmetic operators we can use to perform different
mathematical operations, such as:
1.+ (Addition)
2.- (Subtraction)
3.* (Multiplication)
4./ (Division)
5.// Floor division)
6.℅ (Modulus)
7.** (Exponentiation)
Division
Divide the left operand (dividend) by the right one (divisor) and provide the
result (quotient ) in a float value. The division operator is denoted by
a / symbol.
Note:
•The division operator performs floating-point arithmetic. Hence it always
returns a float value.
•Don’t divide any number by zero. You will get a Zero Division Error:
Division by zero
Division
Divide the left operand (dividend) by the right one (divisor) and provide the
result (quotient ) in a float value. The division operator is denoted by
a / symbol.
Note:
•The division operator performs floating-point arithmetic. Hence it always
returns a float value.
•Don’t divide any number by zero. You will get a Zero Division Error:
Division by zero
Floor Division
Floor division returns the quotient (the result of division) in which the digits
after the decimal point are removed. In simple terms, It is used to divide one
value by a second value and gives a quotient as a round figure value to the
next smallest whole value.
It works the same as a division operator, except it returns a possible integer.
The // symbol denotes a floor division operator.
Note:
•Floor division can perform both floating-point and integer arithmetic.
•If both operands are int type, then the result types. If at least one operand
type, then the result is a float type.
Floor Division
Exponent
Using exponent operator left operand raised to the power of right. The
exponentiation operator is denoted by a double asterisk ** symbol. You can
use it as a shortcut to calculate the exponential value.
For example, 2**3 Here 2 is multiplied by itself 3 times, i.e., 2*2*2. Here the
2 is the base, and 3 is an exponent.