0% found this document useful (0 votes)
10 views79 pages

Python Notes

Uploaded by

markajcaspillo13
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
10 views79 pages

Python Notes

Uploaded by

markajcaspillo13
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 79

PYTHON

Computer
Programming II

By: Mark AJ L. Caspillo


Instructor
What is Python?
Python is a popular programming language. It was created by
Guido van Rossum, and released in 1991.

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

• Python was designed for readability, and has some similarities to


the English language with influence from mathematics.

• Python uses new lines to complete a command, as opposed to


other programming languages which often use semicolons or
parentheses.

• Python relies on indentation, using whitespace, to define scope;


such as the scope of loops, functions and classes. Other
programming languages often use curly-brackets for this
purpose.
Python Syntax
Python Syntax
Python syntax can be executed by writing directly.

Example:

Output
Python Syntax
Creating a python file on the server, using the .py file
extension

Example:
Python Indentation
Python Indentation

● Indentation refers to the spaces at the beginning of a code line.


● Where in other programming languages the indentation in code
is for readability only, the indentation in Python is very
important.
● Python uses indentation to indicate a block of code.
Python Comments
Python Comments

● Comments can be used to explain Python code.


● Comments can be used to make the code more readable.
● Comments can be used to prevent execution when testing
code.
Creating a Comment

Comments starts with a #, and Python will ignore them


Python Comments
Python Comments
Multiline Comment
Since Python will ignore string literals that are not assigned to a variable,
you can add a multiline string (triple quotes) in your code, and place your comment
inside it:
Variables
Variables are containers for storing data values.
Variable Names
A variable can have a short name (like x and y) or a
more descriptive name (age, carname, total_volume).
Rules for Python variables:
• 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)
Variable Names
Variable Names
Variable Names
Variables
Variables do not need to be declared with any particular type, and
can even change type after they have been set.
Casting
If you want to specify the data type of a variable, this can
be done with casting.
Get the Type
You can get the data type of a variable with the type() function.
Single or Double Quotes
String variables can be declared either by using single or
double quotes.
Case-Sensitive
Variable names are case-sensitive.
Python Variables - Assign Multiple Values

● Many Values to Multiple Variables


● One Value to Multiple Variables
● Unpack a Collection
Many Values to Multiple Variables
Python allows you to assign values to multiple variables in one line
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you
to extract the values into variables. This is called unpacking.
Output Variables
Output Variables
The Python print() function is often used to output
variables.
Output Variables
In the print() function, you output multiple variables, separated by
a comma:
Output Variables
You can also use the + operator to Notice the space character after "Python " and "is
output multiple variables: ", without them the result would be
"Pythonisawesome".
Output Variables
For numbers, the + character
works as a mathematical
operator:
Output Variables
In the print() function, when you
try to combine a string and a
number with the + operator,
Python will give you an error:
Output Variables
Data Types
Data Types
The data stored in memory can be of many types. For example, a student
roll number is stored as a numeric value and his or her address is stored
as alphanumeric characters. Python has various standard data types that
are used to define the operations possible on them and the storage
method for each of them.
Example of Data Types
• Int
• String
• Float
• Boolean
List
List
• It is a general purpose most widely used in data structures

• List is a collection which is ordered and changeable and allows


duplicate members. (Grow and shrink as needed, sequence type,
sortable).

• To use a list, you must declare it first. Do this using square brackets
and separate values with commas.

• We can construct / create list in many ways.


List
Example

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.

You might also like