Presentation is loading. Please wait.

Presentation is loading. Please wait.

Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:

Similar presentations


Presentation on theme: "Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:"— Presentation transcript:

1 Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics: –Console I/O –while Loops –Conditionals Winter 2016CISC101 - Prof. McLeod1

2 Today… Aside on Comparing Strings. Punctuation. Python Keywords. Winter 2016CISC101 - Prof. McLeod2

3 Aside - Comparing Strings Binary Boolean operators work with either numbers on both sides or strings on both sides. But, how are strings compared? CISC101 - Prof. McLeod3Winter 2016

4 CISC101 - Prof. McLeod4 Comparing Strings, Cont. Some examples: "abc" == "abC" gives False "abc" < "abcd" gives True "A" < "a" gives True "a" < "b" gives True "aaa" < "aaaa" gives True These comparisons are based on the ASCII code values of the characters compared. “American Standard Code for Information Interchange” Winter 2016

5 CISC101 - Prof. McLeod5 ASCII Table (Lower Half) Winter 2016

6 Aside – Why 128 Characters? This seems like a strange number of characters. Consider 255 – what is this number in hex? FF in base 16. How much memory would it take to store this number? 11111111 in base 2 – so 8 bits or one byte. So one byte is needed to store the ASCII character code. The lower half are the keyboard characters. What about the upper half? Winter 2016CISC101 - Prof. McLeod6

7 ASCII Table – Extended Characters Winter 2016CISC101 - Prof. McLeod7

8 Extended Characters, Cont. These may be different depending on your font and even which OS you are using. These are not on your keyboard! (Type them by holding and entering the four digit base 10 ASCII code for the character. Or use “Insert, Symbol” or the Character Mapping tool.) For example “ 0177” gives: ± hex: B1 See: ascii-table.com Winter 2016CISC101 - Prof. McLeod8

9 Unicode Characters Python and most other modern languages actually allocate 2 bytes of memory for a character. This gives you room for 0xFFFF or 65,535 characters! This set is called the Unicode character set. The first 255 match the ASCII characters, but how about the rest? See Unicode.org Winter 2016CISC101 - Prof. McLeod9

10 Unicode Characters, Cont. See a program that shows these characters in Python: UnicodeTable.py. Uses a while loop and the chr() BIF. chr() accepts a character code number and returns the actual character. Also see UnicodeBoxDemo.py. Winter 2016CISC101 - Prof. McLeod10

11 Comparing Strings, Summary So, you are still actually comparing numeric values, character by character. A shorter string is less than an otherwise identical longer string. Capitals are less than lower case letters. This is important when sorting or analyzing textual information. CISC101 - Prof. McLeod11Winter 2016

12 Punctuation All those “things” that are not operators, keywords, literals or variables. Some you see and some you don’t! Many symbols have a different meaning depending on context. CISC101 - Prof. McLeod12Winter 2016

13 Whitespace Punctuation The ones you don’t see (ie. non-printing): –Spaces –Tabs –Carriage return –Line feed Spaces are used as delimiters in expressions and to improve readability. CR/LF is known as a “Newline”. Used at the end of lines and to put empty lines in a program (for better readability, again). CISC101 - Prof. McLeod13Winter 2016

14 CISC101 - Prof. McLeod14 Tabs and Indentation Press the key to get an indent in your code, if you need one. Use the key to get out of an indentation. Don’t type spaces for indents! Indents are very important in Python, they are not just “whitespace”! IDLE starts indentation automatically, especially after you type a : Winter 2016

15 Indentation, Cont. Indentation indicates “containment” of blocks of code. A block can be one or many lines of code. For example, code could be contained: –In a function definition –In an if statement –In a loop As soon as you “de-dent” a line of code, the subsequent code will no longer be contained in whatever block you were defining. CISC101 - Prof. McLeod15Winter 2016

16 CISC101 - Prof. McLeod16 Punctuation You Can See ' " # \ ' and " for string literals. # used to start a comment. \ is used to continue a long line of code onto the next line. Winter 2016

17 More Punctuation You Can See ( ) [ ] { }, :. ; @ These are all multi-use symbols and their meaning depends on context. Probably better to learn about them in that context! CISC101 - Prof. McLeod17Winter 2016

18 Keywords Without keywords you cannot have a programming language. They are the “glue” that holds a program together. They allow you to give structure to your program so that it does what you want. You cannot use a keyword as a variable name. Python prides itself in having a relatively small set of keywords: CISC101 - Prof. McLeod18Winter 2016

19 Python Keywords False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod19Winter 2016

20 Python Keywords, Cont. We have seen a few of these already: False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod20Winter 2016

21 Python Keywords, Cont. We have used conditional keywords: False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod21Winter 2016

22 Python Keywords, Cont. Next are loops: False class finally is return None continue for lambda try True def from nonlocalwhile and del global not with as elif if or yield assert else import pass break except in raise CISC101 - Prof. McLeod22Winter 2016


Download ppt "Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:"

Similar presentations


Ads by Google