Page 1 :
21, , , , Tn We Chapter, , 2.1 Introduction 2.4 Barebones of a Python Program, , 2.2 Python Character Set 2.5 Variables and Assignments, , 2.3 Tokens 2.6 Simple Input and Output, INTRODUCTION, , You must have heard the term IPO — Input, Process, Output. Most (in fact, nearly all) daily life, and computer actions are governed by IPO cycle. That is, there is certain Input, certain kind of, Processing and an Output., , Do you know that programs make IPO cycle happen ?, , Anywhere and everywhere, where you want to transform some kind of input to certain output,, you have some kind of input to certain output, you have to have a program. A Program is a set of, instructions that govern the processing. In other words, a program forms the base for, processing., , In this chapter, we shall be talking about all basic elements that a Python program can contain., You'll be learning about Python’s basics like character set, tokens, expressions, statements, simple, , input and output etc. So, are we all ready to take our first sincere step towards Python, programming ? And, here we go :-).
Page 2 :
ca, , COMPUTER SCIENCE WITH PYTiqey, an, , 2.2 PYTHON CHARACTER SET, , Character set is a set of valid characters that a language can recognize, A character repr..., any letter, digit or any other symbol. Python supports Unicode encoding standard That me,, , Python has the following character set :, , © Letters A-Z, a-z, , © Digits e-9, , © Special symbols space +-9/**V\C) (1 Cb // eleemc no ik, &# <= >=@_(underscore), , © Whitespaces Blank space, tabs (—»), carriage return (.)), newline, forme., , © Other characters Python can process all ASCII and Unicode characters as pary, data or literals., , 2.3. TOKENS, , In a passage of text, individual words and punctuation marks are called tokens or lexical u», lexical elements. The smallest individual unit in a program is known as a Token or a lexical «>, , Python has following tokens :, , Ca,, () Keywords _ (ii) Identifiers (Names) _ (iii) Literals The sanalleet sidhAdusl une, f tors ») Pur program is known as 2 Token, (iv) Opera (v) Punctuators : = aa, Let us talk about these one by one., 2.3.1 Keywords, COC a, , Keywords are the words that convey a special meaning to the, language compiler/interpreter. These are reserved for special, purpose and must not be used as normal identifier names., Python programming language contains the following, keywords :, , False —_ assert del for in or, None break elif from is pass, True class — else. ~—global_—s lambda raise, , continue except — if, , and, as def _— finally import not try, , 2.3.2 Identifiers (Names) :, Identifiers are fundamental building blocks of a program and are used a5 the get?, terminology for the names given to different parts of the program viz. variables. objects. €, , functions, lists, dictionaries etc., Identifier forming rules of Python are being specified below :, , nonlocal return, , A keyword is a word tore, special meanang reverved >, , Ese, , Jasse®, , © An identifier is an arbitrarily long sequence of letters and digits., © The first character must be a letter; the underscore (_ ) counts as a letter
Page 3 :
Chapter 2 : PYTHON FUNDAMENTALS x, , © Upper and lower-case letters are different. All characters are significant., © The digits 0 through 9 can be part of the identifier except for the first character., , © Identifiers are unlimited in length. Case is significant i.e, Python is case sensitive as it, treats upper and lower-case characters differently., , An identifier must not be a keyword of Python., © An identifier cannot contain any special character except for underscore (_)., , The following are some valid identifiers :, , Myfile DATE9_7.77 _2T@Z9 ike a, MYFILE ps —HII3_JK treats upper and lower-case, _CHK FILE13 characters differently., , The following are some invalid identifiers :, , DATA-REC contains special character - (hyphen), (other than A - Z, a- z and _ (underscore) ), 29CLCT Starting with a digit, break reserved keyword, My. file contains special character dot (. ), , 2.3.3. Literals / Values, Literals (often referred to as constant-Values) are data items that have a fixed value. Python, allows several kinds of literals :, (d String literals (ii) Numeric literals (iii) Boolean literals, , (iv) Special Literal None (v) Literal Collections, , 2.3.3A String Literals, , The text enclosed in quotes forms a string literal in Python., , ist For example, ‘a’, ‘abc’, “abc” are all string literals in Python., Ho. 1 : Unlike many other languages, both single character, ~ enclosed in quotes such as “a” or ‘x’ or multiple characters, , 1. What is meant by token ? Name the enclosed in quotes such as “abc” or ‘xyz’ are treated as, , tokens available in Python. String literals., 2. What are pe Can keywords be As you can notice, one can form string literals by enclosing, used as identifiers ? text in both forms of quotes - single quotes or double, , 3, What is an identifier ? What are the, , identifier forming rules-of Python’? quotes. Following are some valid string literals in Python :, , 4. Is Python case sensitive ? What is ‘Astha’ “Rizwan", meant by the term ‘case sensitive’ ? ‘HelloWorld "Amy? s" 5, , 5. Which of the following are valid "129045" In Python, one can form string, identifiers and why/why not : "1ex-O-w-25! literals by enclosing text in both, Data_rec, data, 1 data, data1, my.file, forms of quotes — single quotes, , elif, switch, lambda, break ? 112FBp291" or double quotes.
Page 4 :
COMPUTER SCIENCE WITH PYTHON - x;, , , , . . . Nongraphic characters, Python allows you to have certain nongraphic-characters in ae atte, tabs, carriage, are those characters that cannot be typed directly from keyboard ¢.8.,, return etc. (No character is typed when these keys ar, Pressed, only some action takes place). These ne A string literal is a sequence of, characters can be represented by using escape sequences. by characters surrounded by quotes, escape sequence is represented by a backslash (\) followed by (single or double or triple quotes),, one or more characters.!, , Following table (Table 2.1) gives a listing of escape sequences., , , , , , Table 2.1 Escape Sequences in Python, , , , , , , , | ; What it does, Bnicce ieee a : ‘ene | [Non-graphic character], . BOS | Sadlenanes \r | Carriage Return (CR), \ | Single quote (’) \t | Horizontal = ie ., \" | Double quote (”) \uxxxx | mee uaa it hex value, \a ASCII Bell (BEL) \Uxxxaoxx | pcs Dailey = value, \b ASCII Backspace (BS) \v ASCII Vertical Tab (VT), \f ASCII Formfeed (FF) \ooo Character with octal value 000, \n | New line character \xhh Character with hex value hh, \N{name} Character named name in the |, Unicode! database (Unicode only) |, , , , , , In the above table, you see Sequences representing \,',", , ,". Though these characters can be typed, from the keyboard but when used without escape Sequence, these carry a special meaning and, have a special purpose, however, if these are to be typed as it is, th, , len escape sequences should, be used. (In Python, you can also directly type a double-quote, inside a single-quoted string and vice-versa. eg, "anu's" is a COT aay, valid string in Python) :, , An escape sequence repr e, a single character and, , consumes one byte in ‘ 4, Python allows you to have two string types : pe puesentaten., , (i) Single-line Strings sc0u, (i) Multiline Strings, , , , , String Types in Python, , (i) Single-line Strin, ic stri ‘ _—, a 18S (Basic strings). Th : ingle, quotes (’) or double quotes wry pe strings that you create by enclosing text in sing], , one line. T ; Te normally single-line strings, i.e., they must terminate in, 0 understand this, try typing the following in IDLE window and see yourselves :, Text1= "hello .), , there’, , eicciemenee cece, 4, Unicode and ASCII are two character encoai.o go, , T encodings discussed later in Chapter, , 43° (Data Reprseat at
Page 5 :
Chapter 2: PYTHON FUNDAMENTALS, , Python will show, you ane, Tror the moment you press Enter key after hello (see below), , , , , , , , [Ble Est Sewch Souse fun” Dalug “Cass “poe, Brojects Ht, Oe aeme anes yeas,, “E:Python Work). 8) Hap —, , ME BK FO © >, , , , (were Scarce [Corecle ¥)} object ax, ; =_— : ee, , znwn, 3@author: Sumit Ven’, , , , , , , , | Varable explorer” Fle exior | Help =, , | Python console, Ca ce wt BF ra, In{t}:6*3 a2 a!, Out[1]: 18 Python shows ERROR when, In [2]: Textt = ' hello Sshibe cacti, , , , , , , , , , File "<ipython-input -2-£98177eb9351>", line 1, Text1 = ' hello Zo 5, , SyntaxError: EOL while scanning string literal, , , , EOL means End of Line, , , , , , , , | In [3]:, , , , 29, , The reason for the above error is quite clear — Python by default creates single-line strings with, both single or double quotes. So, if at the end of a line, there is no closing quotation mark for an, , opened quotation mark, Python shows an error., , (ii) Multiline Strings. Sometimes you need to store some text spread across multiple lines as, , one single string. For that Python offers multiline strings., Multiline strings can be created in two ways :, (a) By adding a backslash, , strings, just add a bac, the next line. For instance,, , at the end of normal single-quote / double-quote strings. In normal, slash in the end before pressing Enter to continue typing text on, , all, , Text1 ='hello\ Do not int a el Fa haste ctife rnust baveoreee, , world’ foping in next One gd , on a single line, or continued, , with a backslash (\) as the very, , Following figure shows this : last character of a line if it is to, be closed with a closing quote in, , next line., , , , , , ——— Adding a backslash (\) at the, end of the line allows you to, , continue typing text in next line., , Do not forget to close the, string by having a closing, quotation mark., , , , , , , Even though written in two lines, (separating with a \), the textis, considered continuous. That is , string, , , , , , : Text1 = ‘hello\, world*, , , , , , In [3, , , , , , , , In [4]: Text1, Out[4]: ‘helloworld’, , , , world, , , , , , would be considered as ‘helloworld’, Display the string variable to see it, yourself., , , , , , , , In [5]:, Give commands next to Inf]: prompt, twill give output in Outf}: line, , » | Python conade | History, __End-of-lines: CRLF_ Encoding: UTF-®