Python Programming - II. The Basics

2,269 views
2,134 views

Published on

Feel free to download the material for offline viewing later, better images' resolutions, and crispier fonts.

Published in: Technology, Education
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,269
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
291
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Python Programming - II. The Basics

  1. 1. Engr. Ranel O. Padon PYTHON PROGRAMMING II. The Basics
  2. 2. 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
  3. 3. RUNNING A COMPUTER PROGRAM Source Translator Target Low-Level Language Assembler Machine Code High-Level Language Compiler Interpreter
  4. 4. 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. Atom/Notepad++/Sublime Text and command prompt 2. Atom/Notepad++/Sublime Text and IDLE 3. IDLE
 4. PyCharm, PyScripter, Aptana Studio, NINJA IDE…
  5. 5. 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”
  6. 6. 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
 
 
 
 
 

  7. 7. 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.
 
 
 
 
 
 

  8. 8. FORCING NEW LINES 
 
 
 
 
 
 print “Hello nWorld nPo!” n = new line
 
 
 
 
 

  9. 9. FORCING NEW LINES & TABS 
 
 
 
 
 
 print “Hello ntWorld ntPo!” t = tab
 
 
 
 
 

  10. 10. ESCAPE SEQUENCES 
 
 
 
 
 
 print “Hello ntWorld ntPo!” t = tab
 
 
 
 
 

  11. 11. 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.
 
 
 
 

  12. 12. 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 comment print ‘Magandang Araw Po!’ #this function computes the sum of two integers def sum(x, y): ...
  13. 13. 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
 
 
 
 

  14. 14. 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
 
 
 
 

  15. 15. 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.
 
 
 
 

  16. 16. 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
 
 
 
 

  17. 17. MEMORY CONCEPTS 
 
 
 
 
 
 x = raw_input(“Unang Numero:”) x = int(x) print x
  18. 18. ARITHMETIC OPERATIONS 
 
 
 
 
 

  19. 19. TRUE DIVISION RESULT 
 
 
 
 
 

  20. 20. TRUE DIVISION RESULT 
 
 
 
 
 

  21. 21. THE PEMMDAS PRECEDENCE 
 
 
 
 
 

  22. 22. THE PEMMDAS PRECEDENCE 
 
 
 
 
 

  23. 23. THE PEMMDAS PRECEDENCE 
 
 
 
 
 

  24. 24. ASSOCIATIVITY 
 
 
 
 
 
 import arcpy input_file = "D:/Project/PRTSAS/Geodatabases/ Market_Values/Pasong Tamo Creek/ GIS_Computed_Market_Values.shp" rows = arcpy.SearchCursor(input_file)
  25. 25. STRING FORMATTING 
 
 
 
 
 

  26. 26. 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
  27. 27. STRING FORMATTING
  28. 28. STRING FORMATTING
  29. 29. STRING FORMATTING
  30. 30. STRING FORMATTING
  31. 31. STRING CONVERSION SPECIFIER
  32. 32. STRING CONVERSION SPECIFIER
  33. 33. RELATIONAL OPERATOR
  34. 34. COMMON ERROR Error: inserting spaces between the pair of symbols:
 
 ==, !=, >= and <=
  35. 35. COMMON ERROR reversing the order of the pair of operators in any of the operators: ! =, <>, >= and <= Error: writing them as =!, ><, => and =<
  36. 36. 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.
  37. 37. KEYWORDS/RESERVED WORDS They cannot be used as identifiers.

  38. 38. RELATIONAL OPERATOR
  39. 39. RELATIONAL OPERATOR
  40. 40. RELATIONAL OPERATOR
  41. 41. PROPER INDENTATION
  42. 42. COMMON ERROR Failure to insert a colon (:) in an if structure.
 
 Failure to indent the body of an if structure.
  43. 43. 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.
  44. 44. 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)
  45. 45. ZEN OF PYTHON (import this) 
 
 
 
 
 
 q  typing import this will yield an awesome narrative
  46. 46. PRACTICE EXERCISE 1
  47. 47. 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.
  48. 48. 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.
  49. 49. PRACTICE EXERCISE 4 Write a program that reads in two integers and determines and prints whether the first is a multiple of the second.
  50. 50. 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
  51. 51. PRACTICE EXERCISE 5 | SOLUTION a = 14 print "Tens:", a/10 print "Ones:", a%10
  52. 52. PRACTICE EXERCISE 5 | SOLUTION a = 214 print "Hundreds:", a/100 hundreds_remainder = a%100 print "Tens:", hundreds_remainder/10 tens_remainder = hundreds_remainder%10 print "Ones:", tens_remainder
  53. 53. PRACTICE EXERCISE 5 | DEBUGGING a = 214 print "Hundreds:", a/100 remainder_100 = a%100 print remainder_100 print "Tens:", remainder_100/10 remainder_10 = remainder_100%10 print "Ones:", remainder_10
  54. 54. PRACTICE EXERCISE 5 | DEBUGGING a = 214 print "Hundreds:", a/100 remainder_100 = a%100 print "Tens:", remainder_100/10 remainder_10 = remainder_100%10 print "Ones:", remainder_10
  55. 55. PRACTICE EXERCISE 5 | SOLUTION a = input(“Magbigay ng 3-digit na numero:”) print "Hundreds:", a/100 remainder_100 = a%100 print "Tens:", remainder_100/10 remainder_10 = remainder_100%10 print "Ones:", remainder_10
  56. 56. PRACTICE EXERCISE 6 Write a program that reads in 3 integers and determines and prints the largest number.
  57. 57. PRACTICE EXERCISE 6 | SOLUTION a = 2 b = 1 c = 4 max = a if b > max: max = b if c > max: max = c print max
  58. 58. PRACTICE EXERCISE 6 | SOLUTION a = input("Unang Numero Po:") b = input("Pangalawang Numero Po:") c = input("Pangatlong Numero Po:") max = a if b > max: max = b if c > max: max = c print max
  59. 59. PRACTICE EXERCISE 7 Write a program that reads in 3 integers and determines and prints the smallest & largest numbers.
  60. 60. PRACTICE EXERCISE 7 | SOLUTION a = input("Unang Numero Po:") b = input("Pangalawang Numero Po:") c = input("Pangatlong Numero Po:") max = a min = a if b > max: max = b if c > max: max = c print max if b < min: min = b if c < min: min = c print min
  61. 61. The Amusing Evolution of a Programmer
 
 http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html
  62. 62. REFERENCES q  Deitel, Deitel, Liperi, & Wiedermann - Python: How to Program (2001). q  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. J

×