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

Lecture 01 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 01 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

MCI MAGIC CODE INSTITUTE

PYTHON

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


MCI MAGIC CODE INSTITUTE

PYTHON OVERVIEW CASE STUDY

Preliminaries Algorithm

IPython Basics Real cases

Python Language Basics


HOMEWORK
Built-in Data Structures
Assignment 1
Functions & Files Assignment 2

Assignment 3

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


I
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


1 PRELIMINARIES

Python – fast growing


PYTHON OVERVIEW

Python – easy to learn


Python – large community

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 4


Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 5
Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 6
PYTHON: WHAT & WHY
• Invented in 1990:
• Guido van Rossum • Simple & Fast
• Interpreted • Easy to learn
• High-Level • Clear syntax
• General Purpose • Strict indentation
• Dynamically-Typed • No “{…}” - No “;”
• Procedural • Huge community
• Object-Oriented, and • Lots of libraries
more • Good time to start: NOW
• Stable Release: 3.8.2

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


THE ZEN OF PYTHON

Beautiful is better Complex is better


1 4
than ugly. than complicated.

Explicit is better 2 5 Readability counts.


than implicit.

Simple is better 3
than complex.

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


PYTHON'S IDEOLOGY

Perl:
•There is more than one way to do it
Python:
•There should be one – and preferably
one – obvious way to do it

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 9


WHO ARE USING PYTHON?
Google: Dropbox:
• Python where we can • Recruited Guido van
• C++ where we must Rossum from Google for:
• Perl/Bash -> recode to • Desktop Client
Python • Server-Side
Instagram:
• Python’s Django Web Spotify:
Framework • Back-End Networking
Facebook:
Netflix
Multiple services in • Server-Side Data Analysis
infrastructure management • Choice of software
engineers

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


WHAT PYTHON IS GOOD FOR?
• Data Science
• Prototyping • Machine Learning
• Scripting • Deep Learning
• Numerical computing • Data Exploration
• Command line tools to automate • Data Visualization
workflows • Important libraries:
• Command line application • numpy, scipy, sympy, pandas
• Server-Side application • statmodels, sklearn, tensorflow,
keras, theano, pytorch
• Web development
• seaborn, matplotlib, altair
• plotly, dash, bokeh

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 11


Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 12
Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378
Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378
2 IPYTHON BASICS
• The Python Interpreter
• Running the IPython Shell
PYTHON OVERVIEW

• Running the Jupyter Notebook


• Tab Completion
• Introspection
• The %run command
• Interrupting Running Code
• Terminal Keyboard Shortcuts
• Magic Commands

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 15


2 IPYTHON BASICS
The Python Interpreter
PYTHON OVERVIEW

You can run it by executing the following command (the hello_world.py file must be
in your current working terminal directory):
$ python hello_world.py
Hello world

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 16


2 IPYTHON BASICS
The Python Interpreter
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 17


2 IPYTHON BASICS
Running the IPython Shell
You can launch the IPython shell on the command line just like launching
PYTHON OVERVIEW

the regular Python interpreter except with the ipython command.

Import numpy package


Create a variable named data
Print the value of data in the console

Be aware of current
Keyboard Language mode:
ENG – VIE  change it to
ENG
Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 18
2 IPYTHON BASICS
Running the Jupyter Notebook
One of the major components of the Jupyter project is the notebook, a type of interactive document for
PYTHON OVERVIEW

code, text (with or without markup), data visualizations, and other output. The Jupyter notebook
interacts with kernels, which are implementations of the Jupyter interactive computing protocol in any
number of programming lan guages. Python’s Jupyter kernel uses the IPython system for its
underlying behavior.

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 19


2 IPYTHON BASICS
Running the Jupyter Notebook
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 20


2 IPYTHON BASICS
Tab Completion
On the surface, the IPython shell looks like a cosmetically different version of the standard
PYTHON OVERVIEW

terminal Python interpreter (invoked with python). One of the major improvements over the
standard Python shell is tab completion, found in many IDEs or other interactive computing
analysis environments. While entering expressions in the shell, pressing the Tab key will search
the namespace for any variables (objects, functions, etc.) matching the characters you have typed
so far:

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 21


2 IPYTHON BASICS
Introspection
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 22


2 IPYTHON BASICS
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 23


2 IPYTHON BASICS
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 24


2 IPYTHON BASICS
PYTHON OVERVIEW

Magic
Commands

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 25


3 PYTHON LANGUAGE BASICS
• Language Semantics
• Scalar Types
PYTHON OVERVIEW

• Control Flow

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 26


3 PYTHON LANGUAGE BASICS
Language Semantics
PYTHON OVERVIEW

Indentation, not braces

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 27


3 PYTHON LANGUAGE BASICS
Everything is an object
An important characteristic of the Python language is the consistency of its object
PYTHON OVERVIEW

model. Every number, string, data structure, function, class, module, and so on exists
in the Python interpreter in its own “box,” which is referred to as a Python object.
Each object has an associated type (e.g., string or function) and internal data. In prac‐
tice this makes the language very flexible, as even functions can be treated like any
other object.

Comments
Any text preceded by the hash mark (pound sign) # is
ignored by the Python interpreter. This is often used to
add comments to code. At times you may also want to
exclude certain blocks of code without deleting them. An
easy solution is to comment
out the code

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 28


3 PYTHON LANGUAGE BASICS
Function and object method calls
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 29


3 PYTHON LANGUAGE BASICS
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 30


3 PYTHON LANGUAGE BASICS
Scalar Types
Python support some data types by 7 groups as below:
PYTHON OVERVIEW

Ø Text type: suitable for text format: str

Ø Numeric types: number format: int, float, complex

Ø Sequence types: for data chain: list, tuple, range

Ø Mapping type: for reference (dữ liệu ánh xạ): dict

Ø Set types: dữ liệu tập hợp: set, frozenset

Ø Boolean type: dữ liệu chân lý, đúng sai: bool

Ø Binary types: dữ liệu nhị phân: bytes, bytearray, memoryview

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 31


3 Python Data type examples
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


3 Casting type
• Casting type allows change data type to expected data type
as below
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


3 PYTHON LANGUAGE BASICS
Control Flow
Python has several built-in keywords for
PYTHON OVERVIEW

conditional logic, loops, and other standard


control flow concepts found in other
programming languages.

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 34


3 Condition (if...elif...else)
• Condition programming allows to run next depending on
previous condition
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


3 Loop (for, while)
• Loop allows to run a line of code
multiple times
• 02 keywords frequently used in a loop
Ø continue: go next another
loop time
Ø break: go out of loop

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


4 BUILT-IN DATA STRUCTURES
• Strings
• Lists
PYTHON OVERVIEW

• Tuple
• Dictionary

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 37


4 BUILT-IN DATA STRUCTURES - Strings
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 38


4 BUILT-IN DATA STRUCTURES - Strings
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 39


4 BUILT-IN DATA STRUCTURES - Strings
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 40


4 BUILT-IN DATA STRUCTURES - Strings
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 41


4 BUILT-IN DATA STRUCTURES - Lists
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 42


4 BUILT-IN DATA STRUCTURES - Lists
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 43


4 BUILT-IN DATA STRUCTURES - Lists
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 44


4 BUILT-IN DATA STRUCTURES - Lists
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 45


4 BUILT-IN DATA STRUCTURES - Lists
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 46


4 BUILT-IN DATA STRUCTURES - Tuple
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 47


4 BUILT-IN DATA STRUCTURES - Tuple
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 48


4 BUILT-IN DATA STRUCTURES - Tuple
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 49


4 BUILT-IN DATA STRUCTURES - Dictionary
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 50


4 BUILT-IN DATA STRUCTURES - Dictionary
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 51


4 BUILT-IN DATA STRUCTURES - Dictionary
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 52


4 BUILT-IN DATA STRUCTURES - Dictionary
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 53


4 BUILT-IN DATA STRUCTURES - Dictionary
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 54


5 FUNCTIONS & FILES

• Built-in functions
PYTHON OVERVIEW

• User-defined functions

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 55


5 FUNCTIONS & FILES - Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 56


5 FUNCTIONS & FILES - Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 57


5 FUNCTIONS & FILES - Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 58


5 FUNCTIONS & FILES - Functions
User-Defined Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 59


5 FUNCTIONS & FILES - Functions
User-Defined Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 60


5 FUNCTIONS & FILES - Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 61


5 FUNCTIONS & FILES - Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 62


5 FUNCTIONS & FILES - Functions
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 63


5 FUNCTIONS & FILES - Files
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 64


5 FUNCTIONS & FILES - Files
PYTHON OVERVIEW

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 65


II
CASE STUDY

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


1 CASE STUDY - 01

• Assuming (name = “John Smith”), what does name[1] return?


CASE STUDY

• What about name[-2]?

• What about name[1:-1]?

• How to get the length of name?

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 67


1 CASE STUDY - 02
CASE STUDY

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 68


III
HOMEWORK

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378


1 Assignment 01

• Write a function that returns the maximum of two numbers.


HOMEWORK

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 70


1 Assignment 02

• Write a function called show_stars(rows). If rows is 5, it should print


the following:
HOMEWORK

• *
• **
• ***
• ****
• *****

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 71


1 Assignment 03

• Write a function called fizz_buzz that takes a number.


• If the number is divisible by 3, it should return “Fizz”.
HOMEWORK

• If it is divisible by 5, it should return “Buzz”.


• If it is divisible by both 3 and 5, it should return “FizzBuzz”.
• Otherwise, it should return the same number

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 72


MCI MAGIC CODE INSTITUTE

THANKS FOR LISTENING!!!

Facebook.com/MagicCodeInstitue/ Le Van Tiep & 0982 521 378 73

You might also like