Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Python CS 4320, SPRING 2015. Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.

Similar presentations


Presentation on theme: "Introducing Python CS 4320, SPRING 2015. Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is."— Presentation transcript:

1 Introducing Python CS 4320, SPRING 2015

2 Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is syntactically important ◦Blocks of code, as inside an ‘if’, are indicated by indenting ◦Code that is sequential must not be indented Statement continuation ◦Statements normally finish at the end of a line ◦A statement will continue to further lines if there are parentheses or braces still not matched ◦A single backslash, \, at the end of a line will also continue the statement ◦Note: no characters, including spaces, are allowed after \ in this case Summary: In Python, white-space is important 1/7/2015CS 4320, SPRING 2015 2

3 Global Level Python allows definitions and code at the global level That is, not everything has to be inside a class definition The famous ‘hello world’ program can be one line: ◦ print(‘hello world!’) This also means that variables can have a global scope: seen by every part of the code. Be careful, global variables can make very difficult code Many important pre-defined functions are free (Built-in Functions Reference)Built-in Functions Reference 1/7/2015CS 4320, SPRING 2015 3

4 First Program Creating a project Creating a file Running from the IDE Running a script from the command line ◦The interpreter definition line #! ◦Change the mode of the file to executable Running the Python interpreter interactively 1/7/2015CS 4320, SPRING 2015 4

5 Printing The print function is used to print data to standard output Arguments must be enclosed in parentheses (a major difference from Python 2) Arguments are separated by a space when printed ◦So, print(“x is”, x) will leave a space between the label and the value The print function inserts a new line after printing the arguments ◦You can change this by using the keyword argument end=… 1/7/2015CS 4320, SPRING 2015 5

6 Basic Data Types Numeric Strings Lists Tuples Dictionaries 1/7/2015CS 4320, SPRING 2015 6

7 Numeric Data Float and Integer are subsumed under a single type Integer arithmetic is arbitrary precision When doing division ◦/ gives a float result ◦// gives an integer result 1/7/2015CS 4320, SPRING 2015 7

8 Built-in Numeric Functions abs divmod max min pow round 1/7/2015CS 4320, SPRING 2015 8

9 Numeric Conversions To numeric from string or other types ◦int ◦float ◦Complex From numeric to string ◦hex ◦bin ◦oct ◦str 1/7/2015CS 4320, SPRING 2015 9

10 Strings Strings are, as in Java, immutable sequences of characters ◦The global len function will return the length of a string String literals can be delimited by single or double quotes, these must be on one line Strings can be delimited by ‘triple quotes’ (either ‘’’ or “””) ◦This form can be multi-line 1/7/2015CS 4320, SPRING 2015 10

11 String format Usage: format-string.format(value, value, …) The format specification language specifies how to create a format-string properly The format-string is passed through unchanged except for replacement fields which are replaced by formatted representations of one of the values passed as a parameter Replacement fields are distinguished by a matched pair of curly braces that being and end a field The characters between the braces specify which value to format and how to format that value 1/7/2015CS 4320, SPRING 2015 11

12 Format: Simple substitution Parameters to format are indexed starting at 0 A pair of braces with a numeric value in between is replaced by the parameter at that index: {2} ◦This does a standard conversion of the value to string ◦If the index is out of bounds, there is an error reported If all the braces appear without values, just as {}, then values are substituted in order from the parameters 1/7/2015CS 4320, SPRING 2015 12

13 Format: Field widths and Alignment The string representation of a value can be padded out to a specific width with spaces This will make neat columns of data when printed The desired number of characters is specified by including a colon in the replacement field, right after the parameter index if it is included: {2:10} or {:10} ◦Following the colon is the number of total characters the value is to take If the value takes more than the specified number of characters when first converted, no padding is done ◦This will cause columns to not align properly, but no data is lost An alignment character can appear between the colon and the width ◦< forces left alignment {2:<10} ◦> forces right alignment {2:>10} ◦^ forces centering {2:^10} 1/7/2015CS 4320, SPRING 2015 13

14 Format: Decimal Places For floating point numbers, the format can specify the exact number of decimal places to display The value displayed is rounded from the internal value To specify decimal places, put a decimal point following the field width, or directly following the colon if there is not field width specified After the decimal point, put the number of decimal places desire Follow this with the letter ‘f’ (must be lower case) Note: the number of decimal places is not added to the field width, it is part of the total field width ◦{:10.3f} will use 10 characters total. Of these, one will be a decimal point and 3 will be for decimal places. That leaves six characters for the integer part and a possible negative sign 1/7/2015CS 4320, SPRING 2015 14

15 Comments About Formatting The Format String Syntax section of the Python library documentation gives details about many other options and possibilitiesFormat String Syntax There is a built-in function format that can be used for single values. This can be helpful with web pages since creating columns is done with tags in HTML rather than spacing Do not attempt to format floating point values by using rounding functions. ◦This will often lose precision in calculations, which is not desirable ◦The rounded value will not always display with the expected number of decimal points because of the way floating point arithmetic is carried out 1/7/2015CS 4320, SPRING 2015 15


Download ppt "Introducing Python CS 4320, SPRING 2015. Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is."

Similar presentations


Ads by Google