Page 1 :
at, , » IMPORTANT : In this chapter an, _ Spyder IDE (IPython shell) which uses, "have given commands with >>> prom!, "given on a Python shell prompt., , though IPython shell is an enhanced shell, used |, , All Python shells exe, ' in many universities (e-9-», , Data, Handling, , «jie Chartet, , \n, 3.4 Operators, 3.5 Expressions, , 3.1. Introduction, , 3.2 Data Types, 3.3 Mutable and Immutable Types, , INTRODUCTION, Is you need to know before you can write even the, , there are some fundamental, fundamentals : data types,, , most elementary programs. This chapter introduces some such, variables, operators and expressions in Python., , Python provides a predefined set of data types, , in any of these data types- This chapter is going to, store in Python. Of course, a program also needs a means to identify stored data., , So, this chapter shall also talk about mutable and immutable variables in Python., , In any language,, , for handling the data it uses. Data can be stored, discuss various types of data that you can, , ve shown screenshots of Python shell used in |, pt to take commands. But in text section, we i, that this command is |, , d onwards, we ha’, In[ ] : prom, , pt, which is a standard way of telling, , cute the code in the same way,, ). You are free to use any Python IDE/shell of your, , nda distribution which installs maximum, ) along with Python. As Spyder IDE and, ution and is very easy t0 use, we hav, , Toronto University,, choice, but we have installed Python using Anaco!, libraries (such as NumPy, SciPy, Panda and many more, IPython shell is available as Patt of Anaconda Python distrib, given screenshots of SP. der IDE and IPython shell in this ‘book at times.
Page 2 :
3.2 DATA TYPES, , Data 2, hr an be of many types ¢.g., character, integer, real, otring eto. Anything, enoved in quot, epresents stri she, ae me string data in Python. Numbers without fractions represent integer data Sumber, i P >prese ;, ractions represent real data and True and Fale represent Boolean data. Since the data to be, , dealt with are of many types, a programming, language, must provide ways and facilities to handle all types of data Built-in Core Data Types, Before you learn how you can process different types of Num biesi(al Jaaltenples, data in Python, let us discuss various data-types supported @ String, in Python. In this discussion of data types, you'll be able @ List, to know Python’s capabilities to handle a specific type of % Tuple, , % Dictionary, , data, such as the memory space it allocates to hold a, certain type of data and the range of values supported for, , a data type etc., Python offers following, built-in core data types : (i) Numbers (11) String (ut) List (1v) Tuple, (v) Dictionary. ‘, , 3.2.1 Numbers, , As it is clear by the name the Number data types are used to store numeric values in Python, The Numbers in Python have following core data types : :, , (i) Integers, © Integers (signed), © Booleans, (ii) Floating-Point Numbers, (iii) Complex Numbers, , 3.2.1A_ Integers, Integers are whole numbe!, They have no fractional parts., in Python by numeric values, Integers can be positive or negative,, (missing + OF ~ symbol means it is p, , There are two types of integers in Python :, , (i) Integers (signed). It is the normal integer’ representation of whole numbers. Integers i, , Python 3.x can be of any length, it is only limited by the memory ay ailable. Unhke other, , languages, Python 3.x provides single data type (int) to store any integer, whether big or small, , It is signed representation, 1, the integers can be positive as w ell as neg, , (ii) Booleans. These represent the truth values False and True. The Boolean type is a subtype of, Jain integers, and Boolean values False and True behave like the values 0 and 1, respectively, , Gn get the Boolean equivalent of 0 or 1, you can type bool(0) or bool (1), Python will return False, , or True respectively., , rs such as 5, 39, 1917, 0 etc., , Integers are represented RESIS MITE sers In-Python, with no decimal point., e.g., +12, - 15, 3000, sitive number)., , % Integers (signed), , % Booleans, , ative, , exception OverflowError is raised instead if the given number cannot be represented throug, 1 cases, the P', in some, , number of bytes.
Page 4 :
*, , 70 COMPUTER SCIENCE WITH Pyro,, , 3.2.1C Complex Numbers, , Python is a versatile language that offers you a numeric type to represent Complex Numbers are, Complex Numbers ? Hey, don’t you know about Complex numbers ? Uhh, Isee. You are Boing », study about Complex numbers in class XI Mathematics book. Well, if you don’t know any thi, about complex numbers, then for you to get started, I am giving below brief introduction :, Complex numbers and then we shall talk about Python’s representation of Complex numbers,, , Mathematically, a complex number is a number of the form A + Bi where i is the imaginary number,, , equal to the square root of —1i.e., y-1. ORR, , A complex number is made up of both real and imaginary, components. In complex number A+Bi, A and B are real, numbers and i is imaginary. If we have a complex number z, where number, equal to the square, z=a+bi then a would be the real component and b would root of —1 ie, \/-1, that is, represent the imaginary component of z, e.g., real component of fant, , z=4-+ 3iis 4 and the imaginary component would be 3., , A complex number is in the form, A+ Bi where i is the imaginary, , Complex Numbers in Python, , Python represents complex numbers in the form A+B j. That is, to represent imaginary, number, Python uses j (or J) in place of traditional i. So in Python j=y-l. Consider the, following examples where a and b are storing two complex numbers in Python :, , a=0+3.1j, , b=1.54+2j |, The above complex number a has real component as 0 and Ce |, imaginary component as 3.1 ; in complex number b, the real, part is 1.5 and imaginary part is 2. When you display complex Python represents complex |, numbers, Python displays complex numbers in parentheses numbers as a pair of floating, when they have a nonzero real part as shown in following Point numbers,, examples., , >>> C=04+4.5j5, , seis dient ia) Cree”, >>>C€ if, , 4.55 Complex numbers are quite, >>>d commonly used in ela, (1.1+3.4j) Engineering, In the ais |, —_ See, a complex number with electricity, however 7, >>> print (c) non-zero real part is displayed with the symbol jis used to represent, 4.53 parentheses around it. current, they use the symbol J, But no parentheses around complex for the square root of -1 pytho!, , >>> print (d) mumber with real part as zero(0) adheres to this convention, (1.14+3.43) }, , ike Python’s other numeric types, complex numbers are a composite quantity made of ty?, mre te real part and the imaginary part, both of which are represented internally as fla!, , are point numbers)., , values (floating,
Page 5 :
opte, , DATA HANDLING, , You can retrieve the tw:, , © z.real gives the real part., , © z.imag gives the imaginary part as, , For example,, , >>> Z= (1+2.56j) + (4-3. 564), , /\, , oO coOMmpone, Ponents using attribute reterences, Fora complex number 2, , 4 float, not as a comples value, , TTP ema, , >>> a, , (3 -1j) The real and imaginary parts of @, _ It will display real part of comple complex number # can be, , >>> z.real « number 2 ‘ um retrieved through the read-only, , 3.0 a attributes zreal and z.Jmag., , >>>z.imag + Shaetlsptsp imaginary part of comples, , 1.0, , 1 ze of numbers represente ', The range of numbers represented through Python's numeric data types is given below., , WS, , , , . What are floating point numbers ?, , When are they preferred over integers ?, . What are complex numbers ? How, would Python represent a complex, number with real part as 3 and, imaginary part as — 2.5 ?, . What will be the output of following, code ?, p=3j, q=p+(1+1.55), print (p), print (q), 4. What will be the output of following, code ?, r=2.543.95, print (r.real), print (r.imag), , Why does Python uses symbol j to, represent imaginary part of a complex, Number instead of the conventional i?, , Hint. Refer note above., , 2,, Python has no separate character datatype, which most other, , ‘Table 3.1 The Range of Python Numbers, , Range |, |, |, , an unlimited range, subject to available, (virtual) memory only, , , , Data type, Integers, , Booleans two values True (1), False (0), , , , an unlimited range, subject to available, (virtual) memory on underlying machine, architecture., , Floating point, numbers, , Complex Same as floating, point numbers because the real, numbers _{| and imaginary parts are represented as floats., 3.2.2 Strings, , You already know about strings (as data) in Python. In this, section, we shall be talking about Python’s data type string., A string data type lets you hold string data, ie., any number, alid characters into a set of quotation marks., , ach character stored ina string? is a Unicode, character. Or in other words, all strings in Python 3.x are, sequences of pure Unicode characters. Unicode is a system, designed to represent every character from every language., , A string can hold any type, , of known characters ie,, r, All Python (3.x) strings store, , letters, numbers, and special, of any known Unicode characters., , of vi, In Python 3.x, e, , characters,, scripted language., , Following are all, strings in Python :, “abed” , "1234", ‘$%*&', 77, “SABE A , 2272?, 2922, (2227, "22", , ‘programming languages have, , legal, , — that can hold a single, , Character. In Python, a character is a string type only, with single character.