Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Introduction to Python for Science 2013.pdf
Скачиваний:
60
Добавлен:
21.05.2015
Размер:
2.41 Mб
Скачать

Introduction to Python for Science, Release 0.9.23

4.5 Exercises

1.Write a Python program that calculates how much money you can spend each day for lunch for the rest of the month based on today’s date and how much money you currently have in your lunch account. The program should ask you: (1) how much money you have in your account, (2) what today’s date is, and (3) how many days there are in month. The program should return your daily allowance. The results of running your program should look like this:

How much money (in dollars) in your lunch account? 118.39

What day of the month is today? 17

How many days in this month? 30

You can spend $8.46 each day for the rest of the month.

Extra: Create a dictionary (see Dictionaries) that stores the number of days in each month (forget about leap years) and have your program ask what month it is rather than the number of days in the month.

2.From the IPython terminal, create the following three NumPy arrays:

a = array([1, 3, 5, 7]) b = array([8, 7, 5, 4]) c = array([0, 9,-6,-8])

Now use the zip function to create the object d defined as d = zip(a, b, c)

Print d out at the terminal prompt. What kind of object is d? Hint: It is not a NumPy array. Convert d into a NumPy array and call that array e. Type e at the terminal prompt so that e is printed out on the IPython terminal. One of the elements of e is -8. Show how to address and print out just that element of e. Show how to address that same element of d. What has become of the three original arrays a, b, and c, that is, how do they appear in e?

3.Create the following data file and then write a Python script to read it into a three NumPy arrays with the variable names f, a, da for the frequency, amplitude, and amplitude error.

Date: 2013-09-16

Data taken by Liam and Selena

frequency (Hz) amplitude (mm) amp error (mm)

70

Chapter 4. Input and Output

Introduction to Python for Science, Release 0.9.23

0.7500

13.52

0.32

1.7885

12.11

0.92

2.8269

14.27

0.73

3.8654

16.60

2.06

4.9038

22.91

1.75

5.9423

35.28

0.91

6.9808

60.99

0.99

8.0192

33.38

0.36

9.0577

17.78

2.32

10.0962

10.99

0.21

11.1346

7.47

0.48

12.1731

6.72

0.51

13.2115

4.40

0.58

14.2500

4.07

0.63

Show that you have correctly read in the data by having your script print out to your computer screen the three arrays. Format the printing so that it produces output like this:

f

=

 

 

 

 

 

[

0.75

1.7885

2.8269

3.8654

4.9038

5.9423

 

6.9808

8.0192

9.0577

10.0962

11.1346

12.1731

 

13.2115

14.25

]

 

 

 

a =

 

 

 

 

 

 

 

 

 

[

13.52

12.11

14.27

16.6

22.91

35.28

60.99

33.38

 

17.78

10.99

7.47

 

6.72

4.4

4.07]

 

 

da =

 

 

 

 

 

 

 

 

 

[

0.32

0.92

0.73

2.06

1.75

0.91

0.99

0.36

2.32

0.210.48 0.51 0.58 0.63]

Note that the array f is displayed with four digits to the right of the decimal point while the arrays a and da are displayed with only two. The columns of the displayed arrays need not line up as they do above.

4.Write a script to read the data from the previous problem into three NumPy arrays with the variable names f, a, da for the frequency, amplitude, and amplitude error and then, in the same script, write the data out to a data file, including the header, with the data displayed in three columns, just as its displayed in the problem above. It’s ok if the header lines begin with the # comment character. Your data file should have the extension .txt.

5.Write a script to read the data from the previous problem into three NumPy arrays with the variable names f, a, da for the frequency, amplitude, and amplitude error and then, in the same script, write the data out to a csv data file, without the header, to a data file with the data displayed in three columns. Use a single format specifier

4.5. Exercises

71

Introduction to Python for Science, Release 0.9.23

and set it to "%0.16e". If you have access the spreadsheet program (like MS Excel), try opening the file you have created with your Python script and verify that the arrays are displayed in three columns. Note that your csv file should have the extension .csv.

72

Chapter 4. Input and Output

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]