Differences between revisions 1 and 17 (spanning 16 versions)
Revision 1 as of 2008-10-30 09:31:30
Size: 1935
Editor: anonymous
Comment:
Revision 17 as of 2008-11-17 10:04:40
Size: 2430
Editor: AnjaRoesel
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
= For loops =
== a) ==
{{{
#!python
Line 6: Line 10:
for i in range(0,4,1):
     print i
Line 7: Line 13:
= For loops =

== a) ==

{{{#!python
for ...:
     print i
  
Line 16: Line 14:
1  1
Line 20: Line 18:
Line 23: Line 20:
[[/Solution1A|Solution]] Solution:
Line 26: Line 23:

{{{#!python
for ... in zip(range(0,4,1),range(4,0,-1)):
     print i,j
  
0 4
1 3
2 2
3 1
}}}

 * Insert an expression in {{{...}}} to get the results.

[[/Solution1B|Solution]]


== c) ==
Line 61: Line 41:
Line 64: Line 43:
[[/Solution1C|Solution]] Solution:

In [44]: a=1,2,3,4
In [45]: a*4
Out[45]: (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
Line 67: Line 50:
= Data types =
{{{
A=dict([('a',1),('b',2)])
}}}
Please specify the data types:

 * A is a ...
 * 'a' is a ...
 * 1 is a ...
 * ('a',1) is a ...
 * [('a',1),('b',2)] is a ...

Provide an alternative way to assign A
Line 68: Line 65:
Line 71: Line 67:
One byte consists of eight bit and covers the value range of 0..255 ({{{uint8}}}, unsigned integer)
or -128..127.
One byte consists of eight bit and covers the value range of 0..255 ({{{uint8}}}, unsigned integer)  or -128..127.
Line 74: Line 69:
{{{#!python {{{
#!python
Line 81: Line 78:
Line 83: Line 79:
Line 92: Line 87:
[[/Solution2A|Solution]]

Solution:
Line 97: Line 90:
Line 103: Line 95:
[[/Solution2B|Solution]] Solution:
Line 107: Line 99:
[[/Solution3A|Solution]] {{{
#!python
Line 109: Line 102:
== b) ==
{{{#!python
Line 113: Line 104:
Line 116: Line 106:
 * Display all values of {{{D}}} using the method {{{.values()}}}
Line 117: Line 108:
 * Display the elements {{{3:6}}} of arrays {{{B}}}   * Display the elements {{{3:6}}} of arrays {{{B}}}
Line 119: Line 110:
 * Insert a new key-values pair {{{C:array(range(0,10,-1))}}} into {{{D}}}   * Display all values of {{{D}}} using the method {{{.values()}}}  * Insert a new key-values pair {{{C:array(range(0,10,-1))}}} into {{{D}}}
Line 122: Line 112:
[[/Solution3B|Solution]] Solution:

= Reading and practicing =
Install python and ipython on your computer and read through the tutorial which comes with the documentation. There are translations in other languages:

 * [[http://wiki.python.org/moin/Languages/German?highlight=(CategoryLanguage)|German Language]]
 * [[http://starship.python.net/crew/gherman/publications/tut-de/online/tut/|Das Python-Tutorium (Version 1.5.2)]]
 * [[http://www.heise.de/ix/artikel/1999/11/184/|Beschränkung aufs Wesentliche (iX 11/1999)]]

For loops

a)

   1 for i in range(0,4,1):
   2      print i
   3 
   4 0
   5 1
   6 2
   7 3
  • Insert an expression in ... to get the results.

Solution:

b)

0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3
3 0
3 1
3 2
3 3
  • What are the corresponding loops?

Solution:

In [44]: a=1,2,3,4 In [45]: a*4 Out[45]: (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

Data types

A=dict([('a',1),('b',2)])

Please specify the data types:

  • A is a ...
  • 'a' is a ...
  • 1 is a ...
  • ('a',1) is a ...
  • [('a',1),('b',2)] is a ...

Provide an alternative way to assign A

Scipy numeric types

from scipy import *

One byte consists of eight bit and covers the value range of 0..255 (uint8, unsigned integer) or -128..127.

   1 b=int8(128)
   2 -128 # Invalid range
   3 
   4 b.nbytes
   5 1

a)

What is the value range of the following types and how many bytes are allocated in the memory?

  • bool8
  • uint8, int8
  • uint16, int16
  • uint32, int32
  • uint64, uint64

Solution:

b)

How many bytes are allocated in the memory by these floating point types?

  • float32, float64, float96
  • complex64, complex128, complex192

Solution:

Data structures

a)

   1 D={'A':array(range(10)),'B':array(range(10))+1}
  • Which keys contains the dictionary D

  • Get the keys with the method D.keys()

  • Display all values of D using the method .values()

  • Display array A

  • Display the elements 3:6 of arrays B

  • Set this elements to 0
  • Insert a new key-values pair C:array(range(0,10,-1)) into D

Solution:

Reading and practicing

Install python and ipython on your computer and read through the tutorial which comes with the documentation. There are translations in other languages:

LehreWiki: Python/Exercise2 (last edited 2008-11-17 14:23:55 by KorFreier)