This document provides an overview of the Python programming language. It discusses why Python is useful, highlighting its built-in data types, clean syntax, and suitability for tasks like web development, natural language processing, and machine learning. It also summarizes Python's ease of use, IDE, data types, syntax features like indentation and comments, and how to install and get started with Python development.
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Â
Introduction to phython programming
1. Why Python?
A programming language with strong similarities to PERL, but with powerful typing and object
oriented features. Commonly used for producing HTML content on websites. Great for text files.
⌠Useful built-in types (lists, dictionaries).
⌠Clean syntax, powerful extensions.
Natural Language Toolkit
Ease of use; interpreter
AI Processing: Symbolic
⌠Pythonâs built-in data types for strings, lists, and more.
⌠Java or C++ require the use of special classes for this.
AI Processing: Statistical
⌠Python has strong numeric processing capabilities: matrix operations, etc.
⌠Suitable for probability and machine learning code.
2. Learning Python
Unfortunately, we wonât have time to cover all of
Python in class; so, weâre just going to go over ome
highlights.
âŚYouâll need to learn more on your own.
âŚHomework 0 asks you to install Python or get comfortable
using it here in the lab. It also asks you to read some online
Python tutorials.
âŚLater homeworks will include Python programming
exercises to help you practice.
âŚWe will fairly rapidly move to using the Natural Language
ToolKit.
3. Installing Python
Python is installed on the PCs in 156.
Python for Win/Mac/Unix/Linux is available from www.python.org.
⌠Generally an easy install.
⌠On macs, already part of OS X.
⌠For NLTK you need Python 2.3 or higher.
GUI development environment: IDLE.
4. IDLE Development
Environment
Shell for interactive evaluation.
Text editor with color-coding and smart indenting for creating python
files.
Menu commands for changing system settings and running files.
We will use IDLE in class.
5. Look at a sample of
codeâŚ
x = 34 - 23 # A comment.
y = âHelloâ # Another one.
z = 3.45
if z == 3.45 or y == âHelloâ:
x = x + 1
y = y + â Worldâ # String concat.
print x
print y
6. Basic Data types
Integers (default for numbers)
z = 5 / 2 # Answer is 2, integer division.
Floats
x = 3.456
Strings
Can use ââ or ââ to specify. âabcâ âabcâ (Same thing.)
Unmatched ones can occur within the string. âmattâsâ
Use triple double-quotes for multi-line strings or strings than
contain both â and â inside of them: âââaâbâcâââ
7. Whitespace
Whitespace is meaningful in Python: especially
indentation and placement of newlines.
âŚUse a newline to end a line of code.
(Not a semicolon like in C++ or Java.)
(Use when must go to next line prematurely.)
âŚNo braces { } to mark blocks of code in PythonâŚ
Use consistent indentation instead. The first line with a
new indentation is considered outside of the block.
âŚOften a colon appears at the start of a new block. (Weâll
see this later for function and class definitions.)
8. Comments
Start comments with # â the rest of line is ignored.
Can include a âdocumentation stringâ as the first line
of any new function or class that you define.
The development environment, debugger, and other
tools use it: itâs good style to include one.
def my_function(x, y):
âââThis is the docstring. This
function does blah blah blah.âââ
# The code would go here...
9. Python and Types
Python determines the data types
in a program automatically. âDynamic Typingâ
But Pythonâs not casual about types, it
enforces them after it figures them out. âStrong Typingâ
So, for example, you canât just append an integer to a string. You
must first convert the integer to a string itself.
x = âthe answer is â # Decides x is string.
y = 23 # Decides y is integer.
print x + y # Python will complain about this.
10. Naming Rules
Names are case sensitive and cannot start with a
number. They can contain letters, numbers, and
underscores.
bob Bob _bob _2_bob_ bob_2 BoB
There are some reserved words:
and, assert, break, class, continue,
def, del, elif, else, except, exec,
finally, for, from, global, if, import,
in, is, lambda, not, or, pass, print,
raise, return, try, while
11. String Operations
We can use some methods built-in to the string data
type to perform some formatting operations on
strings:
>>> âhelloâ.upper()
âHELLOâ
There are many other handy string operations
available. Check the Python documentation for more.