EE2108 Week 1 Python Basics
EE2108 Week 1 Python Basics
Algorithms in Python
Week 1: Python Basics
Wang Lipo
[email protected]
EE2108 / 0
Contact Information: Wang Lipo
School of EEE
Office: S1-B1C-98
Office Phone: 6790 6372
Email: [email protected]
EE2108 / 1 1
Consultation
Or make an appointment
Your suggestions/comments
are always welcome!
EE2108 / 2 2
Course Content
Topics
EE2108 / 4
Python Types and Operators
EE2108 / 5
The Python Interpreter
EE2108 / 6
An Example Python Program
EE2108 / 7
Object-Oriented Programming: Class
EE2108 / 8
Classes vs Objects: an Example
• name
Map of Poland
• population
• area
• loan
• compute population
density
• get international loan
EE2108 / 9
Identifiers
❑ For identifying a variable, function, class, etc.
❑ Identifiers in Python are case-sensitive, so temperature
and Temperature are distinct names
❑ Identifiers can be composed of almost any
combination of letters, numerals, and underscore
characters
❑ An identifier cannot begin with a numeral
❑ There are 33 specially reserved words that cannot be
used as identifiers:
EE2108 / 10
Python’s built-in Classes
❑ int: integers
❑ float: floating-point values
❑ bool: Boolean values
❑ list: a sequence of objects
❑ str: character strings
❑ set: an unordered set of distinct objects
❑ dict: dictionary (aka associative mapping)
EE2108 / 11
The int Class
EE2108 / 12
The float Class
EE2108 / 13
The bool Class
EE2108 / 14
The list Class: a sequence of objects
EE2108 / 15
The str Class
EE2108 / 16
The set Class: an unordered collection
EE2108 / 17
The dict Class
EE2108 / 18
Calling Methods
❑ Methods (also known as member functions) are
invoked on a specific instance of a class using
the dot (“.”) operator
❑ For example, Python’s list class has a method
named sort that can be invoked with a syntax
such as data.sort( ), which rearranges the
contents of the list data so that they are sorted
EE2108 / 19
The Assignment Statement
EE2108 / 20
Expressions and Operators
EE2108 / 21
Logical Operators
EE2108 / 22
Equality Operators
EE2108 / 23
Comparison Operators
EE2108 / 24
Arithmetic Operators
EE2108 / 25
Sequence Operators
EE2108 / 26
Sequence Comparisons
EE2108 / 27
Python Functions and Control Flow
EE2108 / 28
Conditionals
EE2108 / 29
While Loops
EE2108 / 30
For Loops
EE2108 / 31
Continue
EE2108 / 32
Break
EE2108 / 33
Functions
EE2108 / 34
Input
❑ Displays a prompt and then waits until the user enters
some sequence of characters followed by the return
key
❑ The return value of the function is the string of
characters that were entered before the return key
EE2108 / 35
Files
EE2108 / 36
Python Installation and Usage
EE2108 / 37
Python Installation on Windows
❑Step 1: Install Anaconda
▪ https://www.anaconda.com/
▪ If you are now learning to code in Python, you can download the
latest version of Python
▪ You would need older versions of Python only if you wish to use some existing
Python programs that require older versions of Python
▪ Download the installation file for your particular computer (e.g., 64-
bit or 32-bit). The installation may take a few minutes.
EE2108 / 38
Use Jupyter Notebook
❑ Step 3: Start Jupyter Notebook in Anaconda
This will open Jupyter Notebook in your default web
browser:
EE2108 / 39
Python Coding with Jupyter Notebook
1. Click the "New" button on the right and select "Python 3" to create a new
Jupyter Notebook.
3. To run the code, press Shift + Enter or click the "Run" button in the toolbar
You should see the following output:
EE2108 / 40
Python Coding with Jupyter Notebook
4. To download the code that you wrote into a file for later use, click File and
select Download, then “as Notebook”. You should see your new Jupyter
Notebook file in “Download” directory:
5. To upload an existing Jupyter Notebook file, click Upload, select your file, and
then click upload. You should be able to find your newly uploaded file in the file
list below. Click the file name to open the file.
Happy Programming!
EE2108 / 41