SlideShare a Scribd company logo
Python Basics
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
print("Hello, Class!")
print(1)
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
num = input("Enter number:")
print(num)
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
if 2 > 1:
print("Two is greater than one.")
if 2 > 1:
print("Two is greater than one.")
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
if 2 > 1:
print("Two is greater than one.")
if 2 > 1:
print("Two is greater than one.")
Python Syntax
▪indentation
• the same number of spaces should be used in the
same block of code
▪ Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
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!")
Python Comments
▪start with a #
▪make code readable
▪completely ignored by the interpreter
▪Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
#This is a comment.
print("Hello, Class!")
print("Hello, Class!") #This is a comment.
Python Comments
▪Multiline Comments
▪Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
#Comment1
#Comment2
#Comment3
print("Hello, Class!")
"""Comment1
Comment2
Comment3"""
print("Hello, Class!")
Multiline String can also be used
• since Python will ignore string literals
that are not assigned to a variable
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
x = 1
y = "Hello"
print(x)
print(y)
assignment operator: =
a, b, c = 9, 2.5, 'Hello'
print(a)
print(b)
print(c)
num1 = num2 = 20
print(num1)
print(num2)
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
x = 1 # x is of type int
x = "Hello" # x is now of type str
print(x)
Python Variables
▪Casting
▪ done to specify type of
variable
▪ Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
x = str(1) # x will be '1'
y = int(1) # y will be 1
z = float(1) # z will be 1.0
▪type() function
▪ returns the data type of a
variable
▪ Ex:
x = 1
y = "Hello"
print(type(x))
print(type(y))
Python Variables
output variables
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
x = "A good day"
print(x)
x = "A"
y = "good"
z = "day"
print(x, y, z)
x = "A"
y = "good"
z = "day"
print(x+y+z)
x = 2
y = 9
print(x+y)
x = 2
y = "good"
print(x+y)
x = 2
y = "good"
print(x,y)
Python Variables
Global variables
▪created outside of a function
▪can be used inside and outside of functions
▪Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
x = "hi"
def myfunc():
print("Python is " + x)
myfunc()
x = "hi"
def myfunc():
x = "hello"
print("Python is " + x)
myfunc()
print("Python is " + x)
Python Data Types
Built-in DataTypes
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Type
TextType: str
NumericTypes: int, float, complex
SequenceTypes: list, tuple, range
MappingType: dict
SetTypes: set, frozenset
BooleanType: bool
BinaryTypes: bytes, bytearray, memoryview
NoneType: NoneType
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
x = 1 # int
y1 = 2.9 # float
y2 = 3e4 # float
z = 5j # complex
print(type(x))
print(type(y1))
print(type(y2))
print(type(z))
to verify the data type:
Python Data Types
Python Strings
▪Strings – enclosed by "" or ''
▪ Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
x = "Hello" # string
y = 'Hello' # string
x = """Python is a programming
language that lets you work quickly
and integrate systems more
effectively."""
print(x)
Multiline String
• enclosed with three (3) double or
single quotes
Python Data Types
Python Booleans
▪True or False
▪usually used in evaluating expressions
▪ Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
print(3>2)
print(3<2)
print(3==2)
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:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
bool("a") #true
bool("1") #true
bool('') #false
bool(None) #false
Python Data Types
Lists
▪used to store multiple items in a single variable
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
colorList = ["red", "blue", "yellow", "green"]
print(colorList)
✓Items in a list are ordered, changeable, and
allow duplicate values
✓They are indexed (1st item has index [0], the 2nd
item has index [1], and so on)
enclosed with
brackets
Python Data Types
Lists
▪can be of any data type
▪can contain different data types
▪defined as objects with the data type 'list'
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
list1 = ["red", "blue", "yellow"]
list2 = [1, 3, 5, 7, 9]
list3 = [False, True, False]
list1 = ["Peter", 30, "male", True, "Mary", 29, "female"]
Python Data Types
Lists
▪len() function
▪ determines the number of items in a list
▪ Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
colorList = ["red", "blue", "yellow", "green"]
print(len(colorList))
Python Data Types
Lists
▪list() Constructor
▪ can also be used to create a new list
▪ Ex:
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
colorList = list(("red", "blue", "yellow", "green"))
print(len(colorList))
double
parentheses
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
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
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
Python Comparison Operators
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
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
Python Logical Operators
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Operator Name Example
and returnsTrue if both statements are
true
x < 1 and x < 5
or returnsTrue if one of the statements
is true
x < 4 or x < 8
not reverse the result, returns False if the
result is true
not(x < 3 and x < 6)
Python Identity Operators
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Operator Name Example
is ReturnsTrue if both variables are the
same object
x is y
is not ReturnsTrue if both variables are not
the same object
x is not y
Python Membership Operators
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Operator Name Example
in ReturnsTrue if a value is present in a
sequence
x in y
not in ReturnsTrue if a value is not present
in a sequence
x not in y
Python Bitwise Operators
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
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
and let the leftmost bits fall off
x << 2
>> Signed right shift Shift right by pushing copies of the leftmost
bit in from the left, and let the rightmost bits
fall off
x >> 2
& AND Sets each bit to 1 if both bits are 1 x & y
Operator
Precedence
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Operator Name
() Parentheses
** Exponentiation
+x -x ~x Unary plus, unary minus, and bitwise NOT
* / // % Multiplication, division, floor division, and modulus
+ - Addition and subtraction
<< >> Bitwise left and right shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=,
is, is not, in, not in
Comparisons, identity, and membership operators
not Logical NOT
and AND
or OR
If two operators have
the same precedence,
the expression is
evaluated from left
to right.
References:
W3schools (PythonTutorial)
Programiz
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Ad

More Related Content

What's hot (20)

Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
eShikshak
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
Python recursion
Python recursionPython recursion
Python recursion
Prof. Dr. K. Adisesha
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
VedaGayathri1
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
Emertxe Information Technologies Pvt Ltd
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python quick guide1
Python quick guide1Python quick guide1
Python quick guide1
Kanchilug
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Fundamentals of Python Programming
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
Kamal Acharya
 
Sorting in python
Sorting in python Sorting in python
Sorting in python
Simplilearn
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
Srinivas Narasegouda
 
Python
PythonPython
Python
SHIVAM VERMA
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
narmadhakin
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Andrew Ferlitsch
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
Mohd Arif
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
deepalishinkar1
 
Introduction to Python programming
Introduction to Python programmingIntroduction to Python programming
Introduction to Python programming
Damian T. Gordon
 
Python - An Introduction
Python - An IntroductionPython - An Introduction
Python - An Introduction
Swarit Wadhe
 

Similar to Basics of Python Programming in one PDF File.pdf (20)

Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 
Theperlreview
TheperlreviewTheperlreview
Theperlreview
Casiano Rodriguez-leon
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?
lichtkind
 
UNIT 1 .pptx
UNIT 1                                                .pptxUNIT 1                                                .pptx
UNIT 1 .pptx
Prachi Gawande
 
Sessisgytcfgggggggggggggggggggggggggggggggg
SessisgytcfggggggggggggggggggggggggggggggggSessisgytcfgggggggggggggggggggggggggggggggg
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
samiwaris2
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
Anshul Sharma
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
SnW: Introduction to PYNQ Platform and Python Language
SnW: Introduction to PYNQ Platform and Python LanguageSnW: Introduction to PYNQ Platform and Python Language
SnW: Introduction to PYNQ Platform and Python Language
NECST Lab @ Politecnico di Milano
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
Tanwir Zaman
 
Python Programming
Python ProgrammingPython Programming
Python Programming
Saravanan T.M
 
Introduction to Python Programming .pptx
Introduction to Python Programming .pptxIntroduction to Python Programming .pptx
Introduction to Python Programming .pptx
tilakrajpanchal22600
 
Python
PythonPython
Python
Gagandeep Nanda
 
effective_r27
effective_r27effective_r27
effective_r27
Hiroshi Ono
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
sangeeta borde
 
Python programming language presentation
Python programming language presentationPython programming language presentation
Python programming language presentation
dhanishev1
 
Python Module-1.1.pdf
Python Module-1.1.pdfPython Module-1.1.pdf
Python Module-1.1.pdf
4HG19EC010HARSHITHAH
 
Module 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptxModule 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptx
GaneshRaghu4
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
gabriellekuruvilla
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?
lichtkind
 
Sessisgytcfgggggggggggggggggggggggggggggggg
SessisgytcfggggggggggggggggggggggggggggggggSessisgytcfgggggggggggggggggggggggggggggggg
Sessisgytcfgggggggggggggggggggggggggggggggg
pawankamal3
 
Python (3).pdf
Python (3).pdfPython (3).pdf
Python (3).pdf
samiwaris2
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
Introduction to Python Programming .pptx
Introduction to Python Programming .pptxIntroduction to Python Programming .pptx
Introduction to Python Programming .pptx
tilakrajpanchal22600
 
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
2022-23TYBSC(CS)-Python Prog._Chapter-1.pptx
sangeeta borde
 
Python programming language presentation
Python programming language presentationPython programming language presentation
Python programming language presentation
dhanishev1
 
Module 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptxModule 1 - Programming Fundamentals.pptx
Module 1 - Programming Fundamentals.pptx
GaneshRaghu4
 
Ad

Recently uploaded (20)

Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Gojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service BusinessGojek Clone App for Multi-Service Business
Gojek Clone App for Multi-Service Business
XongoLab Technologies LLP
 
AI in Business Software: Smarter Systems or Hidden Risks?
AI in Business Software: Smarter Systems or Hidden Risks?AI in Business Software: Smarter Systems or Hidden Risks?
AI in Business Software: Smarter Systems or Hidden Risks?
Amara Nielson
 
Exchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv SoftwareExchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv Software
Shoviv Software
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??
Web Designer
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
The Elixir Developer - All Things Open
The Elixir Developer - All Things OpenThe Elixir Developer - All Things Open
The Elixir Developer - All Things Open
Carlo Gilmar Padilla Santana
 
Protect HPE VM Essentials using Veeam Agents-a50012338enw.pdf
Protect HPE VM Essentials using Veeam Agents-a50012338enw.pdfProtect HPE VM Essentials using Veeam Agents-a50012338enw.pdf
Protect HPE VM Essentials using Veeam Agents-a50012338enw.pdf
株式会社クライム
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
AI in Business Software: Smarter Systems or Hidden Risks?
AI in Business Software: Smarter Systems or Hidden Risks?AI in Business Software: Smarter Systems or Hidden Risks?
AI in Business Software: Smarter Systems or Hidden Risks?
Amara Nielson
 
Exchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv SoftwareExchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv Software
Shoviv Software
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??Serato DJ Pro Crack Latest Version 2025??
Serato DJ Pro Crack Latest Version 2025??
Web Designer
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdfTop Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
Top Magento Hyvä Theme Features That Make It Ideal for E-commerce.pdf
evrigsolution
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Protect HPE VM Essentials using Veeam Agents-a50012338enw.pdf
Protect HPE VM Essentials using Veeam Agents-a50012338enw.pdfProtect HPE VM Essentials using Veeam Agents-a50012338enw.pdf
Protect HPE VM Essentials using Veeam Agents-a50012338enw.pdf
株式会社クライム
 
Ad

Basics of Python Programming in one PDF File.pdf

  • 2. 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
  • 3. 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
  • 4. 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
  • 5. Python Input/Output Functions print() ▪used to generate an output at the console ▪Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova print("Hello, Class!") print(1)
  • 6. 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: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova num = input("Enter number:") print(num)
  • 7. 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: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova if 2 > 1: print("Two is greater than one.") if 2 > 1: print("Two is greater than one.")
  • 8. 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: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova if 2 > 1: print("Two is greater than one.") if 2 > 1: print("Two is greater than one.")
  • 9. Python Syntax ▪indentation • the same number of spaces should be used in the same block of code ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova 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!")
  • 10. Python Comments ▪start with a # ▪make code readable ▪completely ignored by the interpreter ▪Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova #This is a comment. print("Hello, Class!") print("Hello, Class!") #This is a comment.
  • 11. Python Comments ▪Multiline Comments ▪Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova #Comment1 #Comment2 #Comment3 print("Hello, Class!") """Comment1 Comment2 Comment3""" print("Hello, Class!") Multiline String can also be used • since Python will ignore string literals that are not assigned to a variable
  • 12. 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: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova x = 1 y = "Hello" print(x) print(y) assignment operator: = a, b, c = 9, 2.5, 'Hello' print(a) print(b) print(c) num1 = num2 = 20 print(num1) print(num2)
  • 13. 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
  • 14. Python Variables ▪do not need to be declared with any particular type, and can even change type after they have been set ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova x = 1 # x is of type int x = "Hello" # x is now of type str print(x)
  • 15. Python Variables ▪Casting ▪ done to specify type of variable ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova x = str(1) # x will be '1' y = int(1) # y will be 1 z = float(1) # z will be 1.0 ▪type() function ▪ returns the data type of a variable ▪ Ex: x = 1 y = "Hello" print(type(x)) print(type(y))
  • 16. Python Variables output variables ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova x = "A good day" print(x) x = "A" y = "good" z = "day" print(x, y, z) x = "A" y = "good" z = "day" print(x+y+z) x = 2 y = 9 print(x+y) x = 2 y = "good" print(x+y) x = 2 y = "good" print(x,y)
  • 17. Python Variables Global variables ▪created outside of a function ▪can be used inside and outside of functions ▪Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova x = "hi" def myfunc(): print("Python is " + x) myfunc() x = "hi" def myfunc(): x = "hello" print("Python is " + x) myfunc() print("Python is " + x)
  • 18. Python Data Types Built-in DataTypes ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova Type TextType: str NumericTypes: int, float, complex SequenceTypes: list, tuple, range MappingType: dict SetTypes: set, frozenset BooleanType: bool BinaryTypes: bytes, bytearray, memoryview NoneType: NoneType
  • 19. 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
  • 20. Python Data Types Python Numbers ▪integer (int), floating point number (float), complex ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova x = 1 # int y1 = 2.9 # float y2 = 3e4 # float z = 5j # complex print(type(x)) print(type(y1)) print(type(y2)) print(type(z)) to verify the data type:
  • 21. Python Data Types Python Strings ▪Strings – enclosed by "" or '' ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova x = "Hello" # string y = 'Hello' # string x = """Python is a programming language that lets you work quickly and integrate systems more effectively.""" print(x) Multiline String • enclosed with three (3) double or single quotes
  • 22. Python Data Types Python Booleans ▪True or False ▪usually used in evaluating expressions ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova print(3>2) print(3<2) print(3==2)
  • 23. 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: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova bool("a") #true bool("1") #true bool('') #false bool(None) #false
  • 24. Python Data Types Lists ▪used to store multiple items in a single variable ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova colorList = ["red", "blue", "yellow", "green"] print(colorList) ✓Items in a list are ordered, changeable, and allow duplicate values ✓They are indexed (1st item has index [0], the 2nd item has index [1], and so on) enclosed with brackets
  • 25. Python Data Types Lists ▪can be of any data type ▪can contain different data types ▪defined as objects with the data type 'list' ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova list1 = ["red", "blue", "yellow"] list2 = [1, 3, 5, 7, 9] list3 = [False, True, False] list1 = ["Peter", 30, "male", True, "Mary", 29, "female"]
  • 26. Python Data Types Lists ▪len() function ▪ determines the number of items in a list ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova colorList = ["red", "blue", "yellow", "green"] print(len(colorList))
  • 27. Python Data Types Lists ▪list() Constructor ▪ can also be used to create a new list ▪ Ex: ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova colorList = list(("red", "blue", "yellow", "green")) print(len(colorList)) double parentheses
  • 28. 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
  • 29. Python Arithmetic Operators ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova 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
  • 30. Python Comparison Operators ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova 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
  • 31. Python Logical Operators ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova Operator Name Example and returnsTrue if both statements are true x < 1 and x < 5 or returnsTrue if one of the statements is true x < 4 or x < 8 not reverse the result, returns False if the result is true not(x < 3 and x < 6)
  • 32. Python Identity Operators ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova Operator Name Example is ReturnsTrue if both variables are the same object x is y is not ReturnsTrue if both variables are not the same object x is not y
  • 33. Python Membership Operators ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova Operator Name Example in ReturnsTrue if a value is present in a sequence x in y not in ReturnsTrue if a value is not present in a sequence x not in y
  • 34. Python Bitwise Operators ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova 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 and let the leftmost bits fall off x << 2 >> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off x >> 2 & AND Sets each bit to 1 if both bits are 1 x & y
  • 35. Operator Precedence ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova Operator Name () Parentheses ** Exponentiation +x -x ~x Unary plus, unary minus, and bitwise NOT * / // % Multiplication, division, floor division, and modulus + - Addition and subtraction << >> Bitwise left and right shifts & Bitwise AND ^ Bitwise XOR | Bitwise OR ==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, identity, and membership operators not Logical NOT and AND or OR If two operators have the same precedence, the expression is evaluated from left to right.
  • 36. References: W3schools (PythonTutorial) Programiz ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
  翻译: