python programming - ii. the basics
DESCRIPTION
Feel free to download the material for offline viewing later, better images' resolutions, and crispier fonts.TRANSCRIPT
![Page 1: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/1.jpg)
Engr. Ranel O. Padon
PYTHON PROGRAMMINGII. The Basics
![Page 2: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/2.jpg)
PYTHON PROGRAMMING TOPICS
I • Introduction to Python Programming
II • Python Basics
III • Controlling the Program Flow
IV • Program Components: Functions, Classes, Modules, and Packages
V • Sequences (List and Tuples), and Dictionaries
VI • Object-Based Programming: Classes and Objects
VII • Customizing Classes and Operator Overloading
VIII • Object-Oriented Programming: Inheritance and Polymorphism
IX • Randomization Algorithms
X • Exception Handling and Assertions
XI • String Manipulation and Regular Expressions
XII • File Handling and Processing
XIII • GUI Programming Using Tkinter
![Page 3: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/3.jpg)
![Page 4: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/4.jpg)
RUNNING A COMPUTER PROGRAM
Source Translator Target
Low-Level Language Assembler
Machine CodeHigh-Level Language
Compiler
Interpreter
![Page 5: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/5.jpg)
RUNNING A PYTHON PROGRAM
to write & run a Python program you need a Text Editor
and a Python Interpreter (preferably Version 2.x
since version 3.x is not yet mainstream)
Possible Ways (Common Ones)
1. Notepad++/Sublime Text and command prompt
2. Notepad++/Sublime Text and IDLE
3. IDLE
4. PyScripter, PyCharm, Aptana Studio, NINJA IDE…
![Page 6: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/6.jpg)
HELLO WORLD
as a salute to all programmers in the world, beginning
programmers usually print in the console the infamous Hello
World mystical line
print “Hello World”
![Page 7: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/7.jpg)
HELLO WORLD IN 2 LINES
print “Hello”print “World”
in Python, the print statement will always
move the cursor to the next line after printing
![Page 8: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/8.jpg)
HELLO WORLD, SUPRESSING NEWLINES
print “Hello”,print “World”
A comma (,) will suppress the auto newline insertion
in print statements; it will add a space instead.
![Page 9: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/9.jpg)
FORCING NEW LINES
print “Hello \nWorld \nPo!”
\n = new line
![Page 10: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/10.jpg)
FORCING NEW LINES & TABS
print “Hello \n\tWorld \n\tPo!”
\t = tab
![Page 11: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/11.jpg)
ESCAPE SEQUENCES
print “Hello \n\tWorld \n\tPo!”
\t = tab
![Page 12: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/12.jpg)
EXERCISE
Using Escape Sequences, print the following, including the
punctuations:
“Mahal ko po si Migueeeel!”, sabi ni Amanda.
“Hindi maari yan, lalo na’t si Miguel ay isang Halimaw!”, ang
tugon ng kanyang ina.
![Page 13: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/13.jpg)
COMMENTS
comments don’t do anything except to serve as a
documentation part of a program
use comments as often as you can because programmers
tend to forget what they’ve programmed after a month or so
#this is a commentprint ‘Magandang Araw Po!’
#this function computes the sum of two integersdef sum(x, y):
...
![Page 14: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/14.jpg)
RAW INPUT, TEXT
x = raw_input(“Unang Salita:”)y = raw_input(“Pangalawang Salita:”)
print x + y
raw_input is a built-in function; it’s included in the standard
library & returns a string data type
![Page 15: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/15.jpg)
RAW INPUT, TEXT TO INTEGER
x = int(raw_input(“Unang Numero:”))y = int(raw_input(“Pangalawang Numero:”))
print x + y
int is a built-in function; it’s included in the standard library &
converts a string to integer
![Page 16: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/16.jpg)
INPUT, AUTO TEXT TO INTEGER
x = input(“Unang Numero:”)y = input(“Pangalawang Numero:”)
print x + y
input is a built-in function; it’s included in the standard library
& converts a string number to integer.
![Page 17: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/17.jpg)
VARIABLE ASSIGNMENT
x = input(“Unang Numero:”)y = input(“Pangalawang Numero:”)
sum = x + y
print “Ang sum po ay”, sum
results could be printed directly or assigned to a variable
before printing
![Page 18: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/18.jpg)
MEMORY CONCEPTS
x = raw_input(“Unang Numero:”)x = int(x)
print x
![Page 19: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/19.jpg)
ARITHMETIC OPERATIONS
![Page 20: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/20.jpg)
TRUE DIVISION RESULT
![Page 21: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/21.jpg)
TRUE DIVISION RESULT
![Page 22: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/22.jpg)
THE PEMMDAS PRECEDENCE
![Page 23: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/23.jpg)
THE PEMMDAS PRECEDENCE
![Page 24: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/24.jpg)
THE PEMMDAS PRECEDENCE
![Page 25: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/25.jpg)
ASSOCIATIVITY
import arcpy
input_file = "D:/Project/PRTSAS/Geodatabases/ \Market_Values/Pasong Tamo Creek/ \GIS_Computed_Market_Values.shp"
rows = arcpy.SearchCursor(input_file)
![Page 26: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/26.jpg)
STRING FORMATTING
![Page 27: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/27.jpg)
STRING FORMATTING
1. Rounding-off floating-point values
2. Representing numbers in exponential notation
3. Aligning a column of numbers
4. Right-justifying and left-justifying outputs
5. Inserting characters or strings at precise locations
6. Displaying with fixed-size field widths and precision
![Page 28: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/28.jpg)
STRING FORMATTING
![Page 29: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/29.jpg)
STRING FORMATTING
![Page 30: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/30.jpg)
STRING FORMATTING
![Page 31: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/31.jpg)
STRING FORMATTING
![Page 32: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/32.jpg)
STRING CONVERSION SPECIFIER
![Page 33: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/33.jpg)
STRING CONVERSION SPECIFIER
![Page 34: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/34.jpg)
RELATIONAL OPERATOR
![Page 35: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/35.jpg)
COMMON ERROR
Error: inserting spaces between the pair of symbols:
==, !=, >= and <=
![Page 36: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/36.jpg)
COMMON ERROR
reversing the order of the pair of operators in any of the operators:
!=, <>, >= and <=
Error: writing them as =!, ><, => and =<
![Page 37: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/37.jpg)
COMMON ERROR
Confusing the equality operator == with the assignment symbol =
is an error.
The equality operator should be read “is equal to” and the
assignment symbol should be read “gets,” “gets the value of” or
“is assigned the value of.”
In Python, the assignment symbol causes a syntax error when
used in a conditional statement.
![Page 38: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/38.jpg)
KEYWORDS/RESERVED WORDS
They cannot be used as identifiers.
![Page 39: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/39.jpg)
RELATIONAL OPERATOR
![Page 40: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/40.jpg)
RELATIONAL OPERATOR
![Page 41: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/41.jpg)
RELATIONAL OPERATOR
![Page 42: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/42.jpg)
PROPER INDENTATION
![Page 43: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/43.jpg)
COMMON ERROR
Failure to insert a colon (:) in an if structure.
Failure to indent the body of an if structure.
![Page 44: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/44.jpg)
LINE CONTINUATION
A lengthy statement may be spread over several lines with the
backslash (\) line continuation character.
If a single statement must be split across lines, choose breaking
points that make sense, such as after a comma in a print
statement or after an operator in a lengthy expression.
![Page 45: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/45.jpg)
LINE CONTINUATION
import arcpy
input_file = "D:/Project/PRTSAS/Geodatabases/ \Market_Values/Pasong Tamo Creek/ \GIS_Computed_Market_Values.shp"
rows = arcpy.SearchCursor(input_file)
![Page 46: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/46.jpg)
PRACTICE EXERCISE 1
![Page 47: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/47.jpg)
PRACTICE EXERCISE 2
Write a program that requests the user to enter two numbers and
prints the sum, product, difference and quotient of the two numbers.
![Page 48: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/48.jpg)
PRACTICE EXERCISE 3
Write a program that reads in the radius of a circle and prints the
circle’s diameter, circumference and area. Use the constant value
3.14159 for π. Do these calculations in output statements.
![Page 49: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/49.jpg)
PRACTICE EXERCISE 4
Write a program that reads in two integers and determines and
prints whether the first is a multiple of the second.
![Page 50: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/50.jpg)
PRACTICE EXERCISE 5
Write a program that reads in a 3-digit number, dissects the number
and prints the 3 individual numbers.
For example, 214 will be printed as:
Hundreds: 2
Tens: 1
Ones: 4
![Page 51: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/51.jpg)
PRACTICE EXERCISE 6
Write a program that reads in 3 integers and determines and prints
the largest number.
![Page 52: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/52.jpg)
PRACTICE EXERCISE 7
Write a program that reads in 3 integers and determines and prints
the smallest & largest numbers.
![Page 53: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/53.jpg)
![Page 54: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/54.jpg)
The Amusing Evolution of a Programmer
http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html
![Page 55: Python Programming - II. The Basics](https://reader031.vdocuments.site/reader031/viewer/2022020115/554a4609b4c905863d8b53ce/html5/thumbnails/55.jpg)
REFERENCES
Deitel, Deitel, Liperi, & Wiedermann - Python: How to Program (2001).
Disclaimer: Most of the images/information used here have no proper source
citation, and I do not claim ownership of these either. I don’t want to reinvent the
wheel, and I just want to reuse and reintegrate materials that I think are useful or
cool, then present them in another light, form, or perspective. Moreover, the
images/information here are mainly used for illustration/educational purposes
only, in the spirit of openness of data, spreading light, and empowering people
with knowledge.