Page 20<strong>Analysis</strong> <strong>of</strong> <strong>Structures</strong> - WS10/11• formating a stringA string is formated by the escapes %sA leading number n within a format %nT, with T the type <strong>of</strong> the format, sets up the width <strong>of</strong> the output.The following example shows the formating <strong>of</strong> an integer in decimal and hexadecimal mode. At thehexadecimal format a lower x sets lower digit letter, the capital X sets capital digit letters.>>> "%d,%3d,%6d" % (2,2,2)’2, 2, 2’>>> "%x,%3X,%6X" % (31,31,31)’1f, 1F, 1F’Formating floats there are two different formats available, the fixed format and the exponential format,which is also called scientific format. The f format sets a non exponential representation. The e or Eformat sets a exponential format. e uses a small e letter, and E uses a capital E letter. The g or G formatsets an optimized representation, i.e. a fixed or an exponential format, depending on the outputs length.The number after the dot sets the number <strong>of</strong> digits after the comma for f and e format, it sets the number<strong>of</strong> significant digits for the g format.>>> "%f,%e,%g" % (12.34,12.34,12.34)’12.340000,1.234000e+01,12.34’>>> "%.2f,%.2e,%.2g" % (1234567.89,1234567.89,1234567.89)’1234567.89,1.23e+06,1.2e+06’2.5 Basic Data TypesRecording to the available data types, Python is very different comparing it with common languages likeC, Fortran and Basic. Most <strong>of</strong> the languages <strong>of</strong>fers the programmer data types, which are one by onerelated to the underlaying hardware.So for example Fortran and C <strong>of</strong>fer 2 and 4 byte integers on 32 bit operating systems by default 8 On a64 bit operating platform a long integer <strong>of</strong> 8 bytes will be available. On the other hand there are 4 and 8byte floats available.Python however <strong>of</strong>fers on 32 bit platforms a normal integer <strong>of</strong> 4 bytes, which is directly related to thehardware, for example 11234, and furthermore a so called long integer, for example 1234L, which ishandled by the Python s<strong>of</strong>tware. The long integer, which is marked by a succeeding L, is only restrictedby the computers memory, that means that a really incredible number <strong>of</strong> digits can be considered. Laterwe will calculate the factorial <strong>of</strong> a incredible high number.Furthermore Python as already mentioned <strong>of</strong>fers only one float data type with 8 bytes. The standardized4 byte float is not supported, for example 1.23 or 1.23e+2.Python also supports a complex arithmetic with an complex data type, consisting <strong>of</strong> two floats for realand imaginary part <strong>of</strong> the complex number. The complex unit in Python is called j. Therefor the complexnumber 1 + 4i will be represented in Python with 1 + 4j .8 That means without applying provider depended tricks.E. Baeck
2.6. CODE BLOCKS Page 21The last data type used in Python is a string consisting <strong>of</strong> one or more characters.The data type <strong>of</strong> a variable can be determined using the build in function type, as shown in the followingexample. Within a first step different variables were created by a simple assignment. The content <strong>of</strong> thevariable determines the type <strong>of</strong> the variable, no explicit declaration is needed or available, like in C. Afterhaving created the variables the type <strong>of</strong> the variables will be determined by subsequent type calls.To check the data type within a program the following tests can be made.1. if type(d).__name__ == ’int’You can check the type with the types __name__ member.2. if type(d) == int... or you can check the type with the type class name (discussed later).>>> a = 2>>> b = 3L>>> c = 4.5>>> d = 6 + 7j>>> e = "Hello World">>> type (a)>>> type (b)>>> type (c)>>> type(d)>>> type(e)You see, ’int’ is integer, ’long’ is long integer, ’float’ is float, ’complex’ is complex and’str’ is string data type.Furthermore there are some sequences in Python available, which combines the mentioned data types ina more or less sophisticated mode. More about that later.2.6 Code BlocksOne very imported feature <strong>of</strong> Python is, that code blocks are bracketed by an unique indent. The mostprogramming languages uses there specific code parenthesis. There is one opening parenthesis whichstarts the code block and there is one closing parenthesis, which closes the code block.The following example shows a code block in C.if (a > b){c = a + bd = a - bFurther Lines <strong>of</strong> C-Code}2.12.2011