0% found this document useful (0 votes)
12 views

3 - Introduction To Python Programming I

This document provides an introduction to Python programming for a course on applying machine learning in mechatronic systems. It discusses why Python is well-suited for this application, how to download and install the Anaconda distribution with the Anaconda Navigator interface, and gives an overview of Python data types, variables, strings, and lists.

Uploaded by

Osama Mahmood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

3 - Introduction To Python Programming I

This document provides an introduction to Python programming for a course on applying machine learning in mechatronic systems. It discusses why Python is well-suited for this application, how to download and install the Anaconda distribution with the Anaconda Navigator interface, and gives an overview of Python data types, variables, strings, and lists.

Uploaded by

Osama Mahmood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

MSE491: Application of Machine Learning in

Mechatronic Systems

Introduction to Python Programming I

Mohammad Narimani, Ph.D., P.Eng.


Lecturer
School of Mechatronic Systems Engineering
Simon Fraser University

MSE491: Application of Machine Learning in Mechatronic Systems


1
Why Python
1. Its flexibility and simplicity which makes it easy to learn.
2. Its use by the Data Science community where it provides a
more standard programming language than some rivals such as R.

4. Its ability to run on (almost) any operating system, but


particularly the big three operating systems Windows, MacOS and
Linux.
5. The availability of a wide range of libraries (modules) that can
be used to extend the basic features of the language.
3. Its suitability as a scripting language where it provides a higher
level of abstraction than alternative languages traditionally used.

6. It is free!

MSE491: Application of Machine Learning in Mechatronic Systems


2
Anaconda Individual Edition
▪ There are different Python IDEs. In this course we use Anaconda

▪ Anaconda is a free and open-source distribution of the Python language for


scientific computing that aims to simplify package management and
deployment

▪ “Anaconda Individual Edition” is the free version we used here. The link for
downloading and installation on your computer is below:
https://www.anaconda.com/products/individual

▪ “Anaconda Individual Edition” includes a GUI, Anaconda Navigator, as a


graphical alternative to the command line interface (CLI).

MSE491: Application of Machine Learning in Mechatronic Systems


3
Anaconda Navigator
▪ Anaconda Navigator allows you to launch applications and easily search for
packages on Anaconda Cloud or in a local Anaconda Repository. It is
available for Windows, macOS, and Linux.

▪ Python in Spyder IDE (integrated development environment)


• For large codes and programs
• Easier to navigate multiple files open in tabs

▪ Python in a Jupyter Notebook


• An interactive environment
• For small project and practice
https://docs.anaconda.com/anaconda/user-guide/getting-started/

▪ Anaconda Navigator Cheat Sheet

MSE491: Application of Machine Learning in Mechatronic Systems


4
Python Language
▪ Online tutorial for Python:
https://pythonprogramming.net/introduction-learn-python-3-tutorials/

MSE491: Application of Machine Learning in Mechatronic Systems


5
Introduction to Data types

▪ Arithmetic operations in Python:

MSE491: Application of Machine Learning in Mechatronic Systems


6
Variable Assignments
▪ Rules for variable names:
▪ Cannot start with number
▪ Cannot be space in the name, use ‘_’ instead
▪ Cannot use :?/’<>\|”@#$%^&*~+-!
▪ Keep variable names lower case
▪ Avoid using words that may have special meaning in Python “str”, “list”

▪ Dynamic Typing:
▪ In Python you may reassign variables to different data types

▪ Determining variable type with type()


You can check what type of object is assigned to a variable using Python’s
built-in type() function.
Common data types:
int (for integer), float, str (for string), list, tuple, dict (for dictionary), set,
bool (for Boolean True/False)

MSE491: Application of Machine Learning in Mechatronic Systems


7
Strings
▪ Strings are sequence of characters and used in Python to record text information, such as
names.

▪ We can use a print statement to print a string.

▪ The usage of single quotes and double quotes

MSE491: Application of Machine Learning in Mechatronic Systems


8
Basics of Strings
▪ Use a function called len() to check the length of a string

▪ Strings are ordered sequence means we can use Indexing and Slicing

MSE491: Application of Machine Learning in Mechatronic Systems


9
Basics of Strings
▪ Indexing

▪ Slicing

MSE491: Application of Machine Learning in Mechatronic Systems


10
String Properties
▪ Strings have “immutability” property. This means that once a string is created, the
elements within it can not be changed or replaced.

TypeError: str object does not support item assignment

▪ we can concatenate strings!

MSE491: Application of Machine Learning in Mechatronic Systems


11
String Properties
▪ The multiplication symbol is used to create repetition of a string:

MSE491: Application of Machine Learning in Mechatronic Systems


12
String Properties
▪ Built-in Functions for Strings:
Strings are a type of objects. Built-in functions for objects are called methods. we will learn
about these in much more depth later.
Methods are in the form:
object.method(parameters)

MSE491: Application of Machine Learning in Mechatronic Systems


13
Formatting with placeholders

▪ Formatting with placeholders


String formatting lets us inject items into a string. There are three ways to inject items inside a
string:

MSE491: Application of Machine Learning in Mechatronic Systems


14
Formatting with placeholders
▪ Padding and Precision of Floating Point Numbers

MSE491: Application of Machine Learning in Mechatronic Systems


15
String Formatting
▪ Formatting with the .format() method

MSE491: Application of Machine Learning in Mechatronic Systems


16
String Formatting
▪ Formatting with f-string:

MSE491: Application of Machine Learning in Mechatronic Systems


17
String Formatting
▪ Padding and Precision of Floating-point Numbers
▪ Floating-point numbers use the format %{width}.{precision}f, where {width} is the minimum number
of characters the string should contain. {precision)} shows how many numbers to show past the
decimal point.

▪ with the .format() method the format is {0:{width}.{precision}f}".format(value))


▪ with f-string method the format is {value:{width}.{total_num_digits}}

MSE491: Application of Machine Learning in Mechatronic Systems


18
Lists in Python
▪ Lists are ordered sequences that can hold variety of objects. They are mutable,
meaning the elements can be changed!

▪ The following sections are introduced here:


▪ Creating lists

▪ Indexing and Slicing Lists

▪ Basic List Methods

▪ Nesting Lists

▪ Introduction to List Comprehensions

MSE491: Application of Machine Learning in Mechatronic Systems


19
Creating lists
▪ Lists are constructed with brackets [] and commas separating elements in the list.

▪ Indexing and Slicing are similar to strings

MSE491: Application of Machine Learning in Mechatronic Systems


20
Creating lists
▪ Concatenate lists, just like we did for strings using ‘+’.

▪ Reassigning an element

MSE491: Application of Machine Learning in Mechatronic Systems


21
Basic List Methods
▪ append method add an item to the end of a list

▪ pop remove an item from the list

MSE491: Application of Machine Learning in Mechatronic Systems


22
Basic List Methods
▪ reverse methods sort the list elements from end to the first element

MSE491: Application of Machine Learning in Mechatronic Systems


23
Nesting Lists
▪ In this data structure we can have a list within another list

MSE491: Application of Machine Learning in Mechatronic Systems


24
Nesting Lists
▪ In this data structure we can have a list within another list

MSE491: Application of Machine Learning in Mechatronic Systems


25
Dictionaries
▪ Lists are “ordered sequences” in Python, while dictionaries are
“mappings”.
▪ mappings are a collection of objects that are stored by a “key”.
{key1:value1, key2:value2, …}
▪ Mappings do not retain orders.

MSE491: Application of Machine Learning in Mechatronic Systems


26
Dictionaries
▪ Changing an assigned value for a key:

▪ Adding a key and value to a dictionary

MSE491: Application of Machine Learning in Mechatronic Systems


27
Dictionaries
▪ Methods:

MSE491: Application of Machine Learning in Mechatronic Systems


28
Tuples
▪ Tuples are similar to lists; however, they are immutable meaning they can
not be changed. Tuples are used when you want to make sure the data is not
changed.

MSE491: Application of Machine Learning in Mechatronic Systems


29
Sets
▪ Sets are an unordered collection of unique elements.
▪ Sets are constructed by the set() function

▪ Sets can be constructed from lists

MSE491: Application of Machine Learning in Mechatronic Systems


30
Booleans
▪ Booleans have True and False values that sometime are represented by
integers 1 and 0.
▪ It also has a placeholder object called None.

MSE491: Application of Machine Learning in Mechatronic Systems


31
Files
▪ To interact with an external file Python firstly convert it to an object.
▪ The file can be any sort of file such as audio file, a text file, emails, Excel
documents, etc.

▪ Note: When a file is read completely the reading “cursor” will be at the end
of the file. So, if we try to read the file again the output will be ‘’. We can
reset the “cursor” using seek(cursor_position) method.

MSE491: Application of Machine Learning in Mechatronic Systems


32
Files
▪ Writing to a File

▪ w: Opens in write-only mode. The pointer is placed at the beginning of the file and this will
overwrite any existing file with the same name.
▪ w+: Opens a file for writing and reading.
▪ Opening a file with 'w' or 'w+’ will delete all the contents of a file

MSE491: Application of Machine Learning in Mechatronic Systems


33
Files
▪ Appending to a File:

▪ a: Opens a file for appending new information to it. The pointer is placed at the end of the file.
▪ a+: Opens a file for both appending and reading.
▪ If the file does not exist, one will be created.
▪ Note: When working with a file is done, make sure you close it using close() method.

MSE491: Application of Machine Learning in Mechatronic Systems


34
Statements in Python
▪ Python uses indentation to indicate a block of code

▪ for Loops: “For loops” are used for sequences or other iterable items such
as lists, strings, tuples, dictionaries….

MSE 110 Mechatronics Design I


35
Statements in Python
▪ More example

MSE 110 Mechatronics Design I


36
Statements in Python
▪ For loop with tuples:

MSE 110 Mechatronics Design I


37
Statements in Python
▪ “for loop” with dictionaries:

MSE 110 Mechatronics Design I


38
Statements in Python
▪ “if, elif, and else” statemts:

MSE 110 Mechatronics Design I


39
Statements in Python
▪ A “while” statement will repeatedly execute a single statement or group of
statements as long as the condition is true.

MSE 110 Mechatronics Design I


40
Statements in Python
▪ break, continue, pass: these statements in “for” and ‘while” loops are used
to add additional functionality:

• continue: Goes to the top of the closest enclosing loop.

MSE 110 Mechatronics Design I


41
Statements in Python
• pass: Does nothing at all.

• break: Breaks out of the current closest enclosing loop.

MSE 110 Mechatronics Design I


42
Comparison Operators

MSE491: Application of Machine Learning in Mechatronic Systems


43
Chained Comparison Operators

MSE491: Application of Machine Learning in Mechatronic Systems


44

You might also like