GIS4102 Homework for Python Basic

23 downloads 1684 Views 281KB Size Report
Click the link to download Python Basics Lecture. Read AND practice slide by slide. Start with slide 8. Type 1+4*3 on the IDLE (Start | All programs | ArcGIS | Python 2.7 | IDLE (Python. GUI). Note: To practice ... Lecture PPT slide #14 Exercise.
GIS4102 Homework for Python Basic Click the link to download Python Basics Lecture. Read AND practice slide by slide. Start with slide 8. Type 1+4*3 on the IDLE (Start | All programs | ArcGIS | Python 2.7 | IDLE (Python GUI). Note: To practice some Pythons commands/statements (e.g., for loop, while loop, if..elif,..), use the PythonWin or the Python Editor from IDLE (Python GUI), File | New Window. Those Python basics in the lecture are sufficient for you to learn Python for ArcGIS in the textbook. In fact you have already learned those in Chapter 4. I give one useful tip: In IDLE, you can repeat previous/next commands by typing Alt+P (or Alt + N) simultaneously so that you do not need retype a same commands. Please complete the following assignments and submit screen shots of the code and running result to the dropboxes. Due dates: Assignments 1-3: 4:00pm, 10/2, dropboxes: Python1, Python2, Python; Assignments 3-6: 4:00pm, 10/9, dropboxes: Python4, Pyhton5, Python6. Assignment 1 (5 points) Lecture PPT slide #14 Exercise. Your Python shell output should look like the following, but your numbers can be different (price, amount of money you have) – that is that you need to use variables to hold values . You may need to use print, %, /, int(), and input. >>> What is the price of the wii? 3.5 how much money do you have?85.4 I can afford 25 wiis. Money left after purchasing 7 wiis: 1.4 I need 2.1 dollars to buy an additional wii. >>>

Hint: 85.4/3.5 , you get 24.4 , then you use int(24.4) to get an integer 24, the number of wiis you can buy. You use 85.4%3.5 to get the remainder (money left) 1.4, then 3.5-1.4 (=2.1) is the additional money you need to buy another wii.

Assignment 2 (5 points) Lecture PPT slide #17 Exercise . Hint: You need to use print, range, and for loop. Pay attention to the last two paragraphs of the lyrics which do not follow the pattern of the repetition (therefore they should not be inside the for loop). Assignment 3 (5 points) Use for loop to calculate the sum of 1, 2, 3, …, 100. Refer to slide #18. You need to accumulate the sum. The output should be like this: >>> Sum of 1, 2, 3, ...,100 is 5050 >>> Assignment 4 (5 points) Slide 18 Exercise. Calculate the factorial of a positive integer number n! = 1*2*3*…*(n-2)*(n-1)*n. Your input number should be flexible to be any positive integer number. You need to define an integer variable and use input to accept user’s input. Do not use existing math function of factorial. Use loops! Your output should be like this (Note: What is shown is for calculating the factorial of 7. Your input number should be flexible to be any positive integer number. You need to define an integer variable and use input to accept user’s input.) >>> Enter the integer number and calcuate the factorial of the number: 7 The factorial of 7 is 5040 >>> Assignment 5 (5 points) String Read and practice the lecture slides 24-26. You can find the solution from there. Assign a string “GIS Programming” to a string variable. Print all the letters (in uppercase) line by line (including the space between “GIS” and “Programming”). You are REQUIRED to use “for” loop, use string index (PPTslide #25) for the loop counter , function “len”, and string method “str.upper()”, (see slide 26). Do NOT use “for c in string”. Your output should look like this: G I S P R O

G R A M M I N G Assignment 6 (5 points): Rotation cipher Slide 29 Exercise: Write a program that performs a rotation cypher. Objective is to covert “attack” to a ciphered string by rotating 1 . First you need to understand the ASCII table ((http://www.asciitable.com , I taught this in Intro GIS class). Basically, each character on a computer keyboard has an ASCII code represented in different numbering system (octal, decimal, hexadecimal). Those representations are finally translated to a binary system (1s and 0s). E.g., decimal number for letter ‘a’ is 97. If you rotate ‘a’ by 1, you change the number from 97 to 98. Then you convert 98 to a letter(“b”). You need to access each character in “attack” using for loop as slide 28. You need to use ord() and chr() functions and print. Your output is: >>> buubdl >>>