Python Programming Lecture 1
Python Programming Lecture 1
LECTURE 1
Python
▪a programming language that lets you work
quickly and integrate systems more effectively
(Python Software Foundation)
▪created by Guido van Rossum (1990)
▪named after the popular British comedy troupe
Monty Python’s Flying Circus
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Why Python?
▪works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc.)
▪has a simple syntax
▪runs on an interpreter system
▪can be treated in a procedural way, an object-
oriented way or a functional way
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Why Python?
▪has extremely rich libraries
▪has extensive online documentation
▪has multiple programming communities
▪has diverse applications:
✓ web development (server-side)
✓ software development
✓ mathematics
✓ system scripting
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Input/Output Functions
print()
▪used to generate an output at the console
▪Ex:
print("Hello, Class!")
print(1)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Input/Output Functions
input()
▪used to read a line of input entered by the user at
the console and returns it as a string
▪Ex:
num = input("Enter number:")
print(num)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Syntax
▪indentation
• refers to the spaces at the beginning of a code line
• very important in Python since it indicates a block
of code
▪ Ex:
if 2 > 1:
print("Two is greater than one.")
if 2 > 1:
print("Two is greater than one.")
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Syntax
▪indentation
• number of spaces is up to you as a programmer
• the most common use is four, but it has to be at
least one
▪ Ex:
if 2 > 1:
print("Two is greater than one.")
if 2 > 1:
print("Two is greater than one.")
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Syntax
▪indentation
• the same number of spaces should be used in the
same block of code
▪ Ex: if 2 > 1:
print("Two is greater than one!")
print("Two is greater than one!")
if 2 > 1:
print("Two is greater than one!")
print("Two is greater than one!")
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Comments
▪start with a #
▪make code readable
▪completely ignored by the interpreter
▪Ex:
#This is a comment.
print("Hello, Class!")
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Comments
▪Multiline Comments
▪Ex:
#Comment1
#Comment2
#Comment3
print("Hello, Class!")
"""Comment1
Multiline String can also be used Comment2
• since Python will ignore string literals Comment3"""
that are not assigned to a variable print("Hello, Class!")
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Variables
▪containers for storing data values
▪Python has no command for declaring a variable
▪a variable is created the moment you first assign a value to it
▪ Ex:
assignment operator: =
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Variables
Rules for naming variables
▪must start with a letter or the underscore character
▪cannot start with a number
▪can only contain alpha-numeric characters and underscores (A-
z, 0-9, and _ )
▪are case-sensitive (age, Age and AGE are three different
variables)
▪cannot be any of the Python keywords
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Variables
▪do not need to be declared with any particular type,
and can even change type after they have been set
▪ Ex:
x = 1 # x is of type int
x = "Hello" # x is now of type str
print(x)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Variables
▪Casting ▪type() function
▪ done to specify type of ▪ returns the data type of a
variable variable
▪ Ex: ▪ Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Variables
output variables
x = "A good day" x = 2
print(x) y = 9
print(x+y)
x = "A"
y = "good" x = 2
z = "day" y = "good"
print(x, y, z) print(x+y)
x = "A" x = 2
y = "good" y = "good"
z = "day" print(x,y)
print(x+y+z)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Variables
Global variables
▪created outside of a function
▪can be used inside and outside of functions
▪Ex: x = "hi"
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Built-in Data Types
Type
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Python Numbers
▪integer (int), floating point number (float), complex
✓ int – whole number (positive/negative)
✓ float – contains decimal (positive/negative); can also be scientific
numbers with an “e” to indicate power of 10
✓ complex – written with a “j” as the imaginary part
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Python Numbers
▪integer (int), floating point number (float), complex
▪ Ex:
x = 1 # int
y1 = 2.9 # float
y2 = 3e4 # float
z = 5j # complex
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Python Strings
▪Strings – enclosed by "" or ''
▪ Ex:
x = "Hello" # string
y = 'Hello' # string
x = """Python is a programming
Multiline String language that lets you work quickly
• enclosed with three (3) double or and integrate systems more
single quotes effectively."""
print(x)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Python Booleans
▪True or False
▪usually used in evaluating expressions
▪ Ex:
print(3>2)
print(3<2)
print(3==2)
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Python Booleans
▪bool() function
▪ evaluates any value to true or false
▪if a value has content, it is evaluated to true (i.e. any string is true except empty string,
any number is true except 0, etc.)
▪empty values, such as (),[],{},"",0,and None, evaluate to false
▪ Ex:
bool("a") #true
bool("1") #true
bool('') #false
bool(None) #false
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Lists
▪used to store multiple items in a single variable
colorList = ["red", "blue", "yellow", "green"] enclosed with
print(colorList) brackets
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Lists
▪can be of any data type
list1 = ["red", "blue", "yellow"]
list2 = [1, 3, 5, 7, 9]
list3 = [False, True, False]
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Lists
▪list() Constructor
▪ can also be used to create a new list
▪ Ex:
colorList = list(("red", "blue", "yellow", "green"))
print(len(colorList))
double
parentheses
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Data Types
Python Collections (Arrays)
▪ 4collection data types:
1. List
▪ a collection which is ordered, changeable, and allows duplicate members
2. Tuple
▪ a collection which is ordered, unchangeable, and allows duplicate members.
3. Set
▪ a collection which is unordered, unchangeable (but you can add/remove items), and unindexed.
No duplicate members.
4. Dictionary
▪ a collection which is ordered** and changeable. No duplicate members.
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Arithmetic Operators
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x**y
// Floor division x//y
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Comparison Operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Logical Operators
Operator Name Example
and returns True if both statements are x < 1 and x < 5
true
or returns True if one of the statements x < 4 or x < 8
is true
not reverse the result, returns False if the not(x < 3 and x < 6)
result is true
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Identity Operators
Operator Name Example
is Returns True if both variables are the x is y
same object
is not Returns True if both variables are not x is not y
the same object
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Membership Operators
Operator Name Example
in Returns True if a value is present in a x in y
sequence
not in Returns True if a value is not present x not in y
in a sequence
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Bitwise Operators
Operator Name Description Example
& AND Sets each bit to 1 if both bits are 1 x&y
| OR Sets each bit to 1 if one of two bits is 1 x|y
^ XOR Sets each bit to 1 if only one of two bits is 1 x^y
~ NOT Inverts all the bits ~x
<< Zero fill left shift Shift left by pushing zeros in from the right x << 2
and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost x >> 2
bit in from the left, and let the rightmost bits
fall off
& AND Sets each bit to 1 if both bits are 1 x&y
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Operator
Operator Name
() Parentheses
Precedence **
+x -x ~x
Exponentiation
and AND
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova