COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
PRELIMS | W EEK 1 Scalable: provides structure for supporting large
programs
INTRODUCTION TO PYTHON AND Integrated
PROGRAMMING BASICS Expressive language
Python Python Interpreter
Popular programming language Types:
Created by Guido Van Rossum in 1980 and
released in 1991 PyCharm
Uses: Python IDLE
o Web development (server-side) The Python Bundle
o Software Development pyGUI
Sublime Text
o Mathematics
o System Spacing Two Modes to Use
High level language
Open-source scripting language InteractiveWithout passing python script to the
Case sensitive language interpreter
Directly execute code to Python
One of the official languages of google
(command line)
History Script In this mode source code is stored in
a file with the .py extension and use
Developed by Guido Van Rossum in late the interpreter to execute the contents
eighties to early nineties at National Research of the file.
Institute for Mathematics and Computer Science To execute the script by the
in Netherlands interpreter, you have to tell the
Derived for other language (ABC, Modula-3, C+ interpreter the name of the file
+, Algol-68, SmallTalk, and Unix shell, etc.) Python Syntax Vs Other language
Python is copyrighted and available in GNU Python is designed for readability and some
General Public License similarities to English language with influence of
Maintained by a core development team mathematics
Python 1.0 -Nov 1994 Python uses new lines to complete a command,
Python 2.0- 200; Python 2.7.11 – latest edition of as opposed to other programming language
Python 2 which often use semicolons or parentheses
Python 3.0 – 2008; not backward compatible Python relies on indention, using whitespaces, to
with P2; removes duplicate programming define scope; such as the scope of loops,
constructs and module; Python 3.5.1 – latest functions and classes. Other programming
version languages often use curly brackets for this
Advantages purpose
Works on different platforms Python Character Set
Simple syntax like English language It is a set of valid characters that a language
Syntax that allows developer to write programs recognize.
with fewer line than some programming o Letters: A-Z, a-z
languages o Digits : 0-9
Runs on an interpreter system, code is executed
o Special Symbols
as soon as it is written, prototyping is very quick
o Whitespace
Treated in procedural; way, an object-oriented
way or a functional way Tokens
Characteristics Token: Smallest individual unit in a program is
Interpreted – source code is compiled to byte known as token.
code as .pye file- then interpreted There are five types of token in python:
Interactive o 1. Keyword
Object oriented programming language o 2. Identifier
Easy and simple o 3. Literal
Portable o 4. Operators
o 5. Punctuators
1|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
1. Keyword
Reserved words in the library of a language.
There are 33 keywords in python
2. Identifier
The name given by the user to the entities like
variable name, class name, function-name etc
Rules
1. It can be a combination of letters in lowercase (a
to z) or uppercase (A to Z) or digits (0 to 9) or an
underscore.
2. It cannot start with a digit.
3. Keywords cannot be used as an identifier.
4. We cannot use special symbols like !, @, #, $,
%, + etc. in identifier.
5. _ (underscore) can be used in identifier.
6. Commas or blank spaces are not allowed within
an identifier. 4. Operators
3. Literals An operator performs the operation on
Literals are the constant value. operands. Basically there are two types of
Literals can be defined as a data that is given in operators in python according to number of
a variable or constant operands:
Types: Unary Performs the operation on one
operand
Numeric Immutable + unary plus
Ex: 5, 6.7,6+9j - unary minus
String Formed by eclosing a text in quotes ~ bitwise complement
We can use both single as well as Not logical negation
double quoted for a string Binary Performs operation of on two
Ex: “Aman” ‘kapoy’ operands
Boolean 2 values
True or False
Special Only one special literal: none 5. Separator or punctuator
None- specify that field that is not : , ; () {} []
created; used for end of lists in python
Literal Tuples, lists, doctionaries Mantissa and Exponent Form:
A real number in exponent form has two parts:
Mantissa : It must be either an integer or a
proper real constant.
2|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
Exponent : It must be an integer. Represented compare two values
by a letter E or e followed by integer value. Multi-Line Multiline comments can be written in
Comment more than one lines. Triple quoted ‘ ’ ’
or “ ” ”) multi-line comments may be
used in python. It is also known as
docstring
Example:
‘’’ This program will calculate the
average of 10 values.
First find the sum of 10 values
and divide the sum by number of
values
‘’’
Basic Terms in Python Programming
1. Block and Indentation
Python provides no braces to indicate blocks of
code for class and function definition or flow
control.
Maximum line length should be maximum 79
characters.
Blocks of code are denoted by line indentation,
which is rigidly enforced. Multi-Line Statements
The number of spaces in the indentation is
variable, but all statements within the block must Statements in python typically end with a new
be indented the same amount. line
for example – However, python allows the use of the line
continuation character (\) to denote that line
if True: should continue
Statements contained within {},[],() brackets fo
print(“True”)
not need to use the line continuation character
else:
print(“False
2. Statements
a line which has instructions or expressions
3. Expressions
Multiple Statements on a Single Line
a legal combination of symbols and values that
produce a result The semicolon (;) allows multiple statements on
Generally, it produces a value a single line given that no statement starts a new
code block
4. Comments
Not executed
Explain a program and make a program and
make a program understandable and readable
All characters after the # and up to the end of Quotation in Python
the physical line are part of the comment and the Python accept single (‘) , double (“) and triple (‘’’
Python interpreter ignore them or “””) quotes to denote string literals, as long as
Single This type of comments start in a line the same type of quote starts an ends the string
Line and when a line ends, it is The triple quotes are used to span the string
Comment automatically ends. Single line across multiple lines
comment starts with # symbol.
Example: if a>b: # Relational operator
3|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
Multiple Statement Group as Suites
Group of individual statements, which make a
single code block are called suites in Python
Compound or complex statements, such as if,
while, def, and class require a header line and
suite
Header lines begin the statement (with the
keyword) and terminate with a colon (:) and are
followed by one or more line which make up a
suite
Rules for Python Variables
1. Must start with a letter or the underscore
character
2. Cannot start with a number
Variable 3. Can only contain alpha-numeric characters and
underscore
Reserved memory location to store values 4. Case-sensitive (age, Age and AGE)
When you create a variable, you reserve some
space in the memory LValue
Name location that refers to a value and whose
A value of an expression
value can be used and processed during
LHS of the expression
program execution
It is created the moment you first assign a value RValue
to it
Dynamic Typing- does not need to be declared A value of an expression
with any particular type and can even change RHS
type after the have been set
Input
Assigning Values Input()- method used to take input from the user
Returns a value of a string type
Variables don’t need explicit declaration to
reserve memory space.
Declaration happens automatically when you
assign a value to a variable
Allows you to assign a single value to multiple
variables
Assign multiple values to multiple varaibe;s
Type Casting
4|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
Convert one data into another type DATA TYPES AND OPERATORS
Done using a constructor functions
Operators
Constructs which manipulates the value of
operands
4 + 5 = 9 : 4 and 5 are the operands, + is the
operator
Data Types
Primitive
1. Numbers
Store numeric values
3 types: int, float, complex
Output using print()
a. Integer
Object- one or multiple objects separated by a Int and Boolean
comma Int: int or integer is a whole number, positive or
Sep- sep argument specifies the separator negative, without decimals
character or string; it separates the
objects/items; by default, adds space in between
items when printing
b. Float
End- determines the ed character that will be
printed at the end of the print line; newline Float or floating point number
character (\n) Number, positive or negative, containing one or
more decimals
Can be also scientific numbers with an “e” to
indicate the power of 10
PRELIMS | W EEK 2
5|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
c. Complex
Written with a j as the imaginary part
Real and imaginary part of a number can be
accessed through the attributes real and imag
2. Relational – compare values; either returns true or
false bases on the condition
d. Boolean
2 values: true and false
True: has value of 1 3. Logical – assume variable ‘a’ holds True and variable
False: has value of zero ‘b’ holds false then
2. String
Sequence of characters represented in the
quotation marks 4. Bitwise – acts on bits and performs bit by bit
Python allows for either pairs of single or double
operation
quotes
Python does not have a character data type, a
single character is simple a string with a length
of one.
The python string store Unicode characters
Each character in a string has it’s own index
String is immutable data type means it can never
change its value in place
Collection Data Type
Versatile data structures In python that is used to
stroe collections of items
Types:
o List
o Tuple
5. Assignment- used to assign values to the variables
o Set
o Dictionary
Basic Operators in Python
1. Arithmetic – to perform mathematical operations
6|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
Whenever two or more operators have the same
precedence, the associativity defines the order
of operation
PRELIMS | W EEK 2
6. Other special Operators DECISION MAKING AND LOOPING STATEMENTS
Identify Operators – compare the memory Decision Making
locations of two objects Is about deciding the order of execution of
statements based on certain conditions
Decision structures evaluate multiple extensions
which produce TRUE or FALSE as outcome
Membership Operators – test for membership
in a sequence such as strings, lists, or tuples
IF Statement
Similar to other languages
Contains a logical expression using which the
data is compared and a decision is made based
on the results of the comparison
Operator Precedence If the Boolean expression evaluates TRUE, then
the block of statement(s) inside the statement is
Describe the order in which operations are executed
performed when an expression is evaluated In python, statements in a block are uniformly
Operators with higher precedence perform the indented after the “:” symbol.
operation first If Boolean evaluates to FALSE, the first set of
code after the end of the block is executed
Operator Associativity
7|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
ELIF Statement
Allows you to check multiple expressions for
TRUE and execute a block of code as one of the
conditions to evaluate to TRUE
Similar to else, the elif statement is optional
There can be an arbitrary number of elif
statement
IF….ELSE Statements
Else statement can be combined with an if
statement
Else statement contains a block of code that
executes if the conditional expression in the if
statement resolves to ) or FALSE
The else statement is an optional statement and
there could be at the most only one else
statement following if
Nested IF Statements
For situations where you want to check another
condition after a condition resolves to true.
You can have an if…elif…else construct inside
another if…elif…else construct
8|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
Types
1. While Loop
Repeatedly executes a target statement as long
as given condition is true
Here, statements may be a single statement or a
block pf statements with uniform indent
The condition may be any expression, and true
is any non-zero value
The loop iterates while the condition is true;
when false the program control passes to the
line immediately following the loop
All statements indented by the same number of
character spaces after a programming construct
are considered to be a part of a single block of
code
Python uses indention as its method of grouping
statements
Single Statement Suites
If the suite of an if clause consists only of a
single line, it may go on the same line as the
header statement
Loops
Execute a set of statements repeatedly until a
particular condition is satisfied
9|Page
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
Infinite Loop o stop: Generate numbers up to, but not
including last number.
A loop becomes an infinite loop if a condition
o step: Difference between each number
never becomes false
in the sequence.
You must be cautious when using while loops
because of the possibility that this condition
never resolves to a false value
This results in a loop that never ends
Useful in client/server programming where the
server needs to run continuously so that client
Uses of Range Function
programs can communicate with it as when
required 1. Range (Stop)
Single Statement Suites By default, It starts from 0 and increments by 1
and ends up to stop, but not including stop
Similar to if statement syntax, if your while
value.
clause consists only of a single statement, it may
be placed on the same line as the while header
2. for Loop Statements
Has the ability to iterate over the items of any
sequence, such as a list or a string
If a sequence contains an expression list, it is
evaluated first
Then, the first item in the sequence is assigned
to the iterating variable iterating_var 2. range(start,stop)
Next, the statements block is executed
It starts from the start value and up to stop, but
Each item in the list is assigned to iterating_var,
not including stop value.
and the statements block is executed until the
entire sequence is exhausted
Range() Function
3. range(start,stop,step)
it generates a list of numbers, which is generally
used to iterate over with for loop. range( ) Third parameter specifies to increment or
function uses three types of parameters, which decrement the value by adding or subtracting
are: the value.
o start: Starting number of the sequence.
10 | P a g e
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
1. Break Statement
break statement terminates the loop
unconditionally
: If the break statement appears in a nested
loop, then it will terminate the very loop it is in
i.e. if the break statement is inside the inner loop
then it will terminate the inner loop only and the
outer loop will continue as it
Loop else Statement
The else statement of a python loop executes
when the loop terminates normally.
2. continue statement
The else statement of the loop will not execute
when the break statement terminates the loop. With the continue statement we can stop the
current iteration, and continue with the next
The else clause of a loop appears at the same iteration
indentation as that of the loop keyword while or
for
3. Nested Loop
A loop inside another loop
Sample Codes
Jump Statements
11 | P a g e
COMP 133 | Sir JOFER Keziah Arcenas ECE 1C
12 | P a g e