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

Python 01 CourseIntro

Python_01_CourseIntro

Uploaded by

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

Python 01 CourseIntro

Python_01_CourseIntro

Uploaded by

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

Introduction

Agenda
• Introduction
• Install python
• Execution mode
• First python application
• Python IDEs
• Keywords, comment, syntax
• Common errors

2
Python!
• Created in 1991 by Guido van Rossum (now at Google)
– Named for Monty Python

• Useful as a scripting language


– script: A small program meant for one-time use
– Targeted towards small to medium sized projects

• Used by:
– Google, Yahoo!, Youtube
– Many Linux distributions
– Games and apps (e.g. Eve Online)

3
What is Python use for?
• Data analysis and machine learning
• Web development
• Automation or scripting
• Software testing and prototyping
• Everyday [automation] tasks

4
Why Python is popular?
• It has a simple syntax that mimics natural language, so it’s easier to
read and understand. This makes it quicker to build projects, and faster
to improve on them.
• It’s versatile. Python can be used for many different tasks, from web
development to machine learning.
• It’s beginner friendly, making it popular for entry-level coders.
• It’s open source, which means it’s free to use and distribute, even for
commercial purposes.
• Python’s archive of modules and libraries is vast and growing.
• Python has a large and active community that contributes to
Python’s pool of modules and libraries, and acts as a helpful resource for
other programmers. The vast support community means that if coders
run into a stumbling block, finding a solution is relatively easy;
somebody is bound to have encountered the same problem before.

5
Interpreted Languages
• interpreted
– Not compiled like Java
– Code is written and then directly executed by an
interpreter
– Type commands into interpreter and see immediate
results
Java: Runtime
Code Compiler Computer
Environment

Python: Code Interpreter Computer

6
The Python Interpreter
• Allows you to type commands one-at-a-time and
see results
• A great way to explore Python's syntax
– Repeat previous command: Alt+P

7
Installing Python
Windows: Mac OS X:
• Download Python from • Python is already installed.
http://www.python.org • Open a terminal and run python
• Install Python. or run Idle from Finder.
• Run Idle from the Start Menu.
Linux:
• Chances are you already have
Python installed. To check, run
python from the terminal.
• If not, install from your
distribution's package system.
Note: For step by step installation
instructions, see the course web site.

8
Installing Python (cont)
1. Install Python 3 + IDE Pycharm

2. Install Python 3 using Anaconda (RAM > 4GB)


– Install IDE Pycharm
– Anaconda hỗ trợ IDE Spyder

• Code Python in web browser


– No install
– Need laptop connect internet

9
Installing Python (cont)
Check install python successful:
1. Open Command Promt (cmd)
2. Type : python --version or python

10
Installing Python (cont)
Or using Python Shell
• Open “Start menu” search “ idle” and run file
“idle.exe”

11
Execution mode
• Python have 2 execution mode
– Interactive mode: run commands one by one
– Script mode: write more than one instruction in a
file (file script *.py) and run: python <file>.py

12
Execution mode (cont)
Interactive mode: open cmd -> type python / or open Python
Shell
– The python interpreter will wait for the user to type each command
line
– After typing any command line, python immediately runs that line
– Terminate this mode by typing the command: “quit()” or Ctrl+Z

13
Execution mode (cont)
Script mode
• Using Notepad (or other IDE) to create python file,
eg: test.py
– Open Command Promt (cmd)
– type: python <path_to_test.py>
– Example: python C:\Users\HIENLTH\Desktop\test.py

test.py
# -*- coding: utf-8 -*
print(“Nhat Nghe")
a = 20
b = 21
c = a+b
print("c=%d" %c)

14
Our First Python Program
• Python does not have a main method like Java
– The program's main code is just written directly in the
file
• Python statements do not end with semicolons
hello.py
1 print("Hello, world!")

15
The print Statement
print("text")
print() (a blank line)
– Escape sequences such as \" are the same as in Java
– Strings can also start/end with '

swallows.py
1 print("Hello, world!")
2 print()
3 print("Suppose two swallows \"carry\" it together.")
4 print('African or "European" swallows?')

16
Python IDEs
• Should not using text editors (notepad,
notepad++, wordpad, gedit, vi, ...)
– There is no feature to automatically prompt and correct
syntax errors (missing spaces/tabs, misspelling function
names, wrong library names, etc.)
– No debug feature.
–…
• Using Integrated Development Environment-IDE
– PyCharm
– Spyder
– PyDev
– …
17
Python IDEs: PyCharm
Install Pycharm IDE after installing python
• Step 1: Go to:
https://www.jetbrains.com/pycharm/download/ and click
“Download” button of Community version:

18
Python IDEs: PyCharm (tt)
Install Pycharm IDE after installing python
• Step 2: After download completed, run exe file to
install PyCharm.

19
Python IDEs: PyCharm (tt)
• Goto menu “File” and choose “New”. Next, choose
“Python File”.

20
Python IDEs: PyCharm (tt)
• In popup, input file name.

21
Python IDEs: PyCharm (tt)
• Simple python application print("hello world")
a = 'string1'
b = 'string2'
print(a)
print(b)
print(a+b)

22
Code Python in web browser
• http://pythonfiddle.com/

https://www.online-python.com

23
Code Python in web browser
• https://www.pythonanywhere.com/try-ipython/

24
Python keyword
• Distinguish between upper and lower case letters
• Keywords:
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

https://www.w3schools.com/python/python_ref_keywords.asp

25
Comments
• Syntax:
# comment text (one line)

swallows2.py
1 # Suzy Student, CSE 142, Fall 2097
2 # This program prints important messages.
3 print("Hello, world!")
4 print() # blank line
5 print("Suppose two swallows \"carry\" it together.")
6 print('African or "European" swallows?')

26
Comments (cont.)
• Using ''' or """ to comment a paragraph
'''
This is a comment,
Many comments here.
'''

27
Syntax
• Each line of code within a block must be indented by the
same number of spaces or tabs
• Commands are written on multiple lines using characters \
total = item_one + \
item_two + \
item_three
• Commands surrounded by brackets: [], {}, () do not need
to use the \ character to continue the line
days = ['Monday', 'Tuesday', 'Wednesday’,
'Thursday', 'Friday']
• Using ; to separate multiple commands on the line
import sys; x = 'foo'; sys.stdout.write(x + '\n')

28
Common errors
• Error missing/excess spaces or tabs
# -*- coding: utf-8 -*
print("Hello world")
a = 1 # thua khoang trang
b=2
c = a+b
print("c=%d" %c)

29
Common errors (tt)
• Invalid path to file *.py or file doesn’t exist.

30

You might also like