 
                        CLASS-XI COMPUTER SCIENCE PYTHON DATA TYPES NOTES Data Types : A data type, in Python or in any programming language, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error. The string and int , for example, are Python data types that are used to classify text and an integer is a data type used to classify whole number A=10+12 A= “ram” A=10.5 Classifications of Data Types in Python (i) Numbers (ii) string (iii) list (iv) tuple (v) dictionary Python data types are divided into two categories, mutable data types and immutable data types. Mutable Data Type: Data types in python where the value assigned to a variable can be changed. Immutable Data Type: Data types in Python where the value assigned to a variable cannot be changed. Mutable and Immutable objects: Data objects of the above types are stored in a computer’s memory for processing. Some of these values can be modified during processing, but the contents of others cannot be altered once they are created in the memory. Number values, string, and tuple are immutable, which means their contents can’t be altered after creation. On the other hand, the collection of items in a list or dictionary object can be modified. It is possible to add, delete, insert, and rearrange items in a list or dictionary. Hence they are mutable objects. Example Data Type x = "Hello World" str x = 20 int x = 20.5 float x = 1j complex x = ["apple", "banana", "cherry"] list x = ("apple", "banana", "cherry") tuple x = {"name" : "John", "age" : 36} dict x = {"apple", "banana", "cherry"} set x = True bool NUMBERS DATA TYPE 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. * Integers Integers (signed) Booleans * Floating Point Numbers * Complex Numbers Integers are whole numbers with no fractional parts. Integers can be positive or negative. Booleans are subtype of plain integers. Long integers have unlimited precision and float represent real numbers and are written with a decimal point dividing the integer and fractional parts. Complex numbers have a real and imaginary part. a+bj. Where a is the real part and b is the imaginary part. It is useful for problem solvers to understand a couple of Python’s core data types in order to write well-constructed code. The Python Programming language provides four numeric data types. They are as follows: * int—All the numbers without fraction part(Example-10). For int there is no upper limit. *float –All the numbers with a fraction part(Example- 10.5) .It’s accurate up to 15 decimal places. *complex- All the numbers with real and imaginary parts.(Example-5+10 j). *bool – Boolean values True and False Integers are whole numbers with no fractional parts. Integers can bepositive or negative. Booleans are subtype of plain integers. Long integers have unlimited precision and float represent real numbers and are written with a decimal point dividing the integer and fractional parts. Complex numbers have a real and imaginary part. a+bj. Where a is the real part and b is the imaginary part. It is useful for problem solvers to understand a couple of Python’s core data types in order to write well-constructed code. Integers Data Types Int – signed integers Long-Ling integers for representing higher values Like in math, integers in computer programming are whole numbers that can be positive, negative, or 0(……-1,0.1……).An integer can also be known as an int. As with other programming languages, you should not use commas in number of four digits or more. So when you write 1.000 in your program write it as 1000. A=1000 We can print out as integer in a simple way like this: print(-25) Output : -25 We can do math with integers in Python, too : int ans= 116-68 print(ans) output 48 Floating – Point Numbers Data Types The float data types are used to represent decimal point values. This value is represented by float class. It is real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation. It can be written as fractional form or exponent form. Floating point number has two advantages over integers: 1. They can represent values between two integers. 2. They can represent a much greater range of values. And the disadvantage is : Its operation are slower than integer operations. Example : >>>d= 6.2 >>>type(d) <class ‘float’> >>>a=-0.03 >>>type(a) <class ‘float’> >>>e=6.02e23 >>>e 6.02e+23 >>>type(e) <class ‘float’> To make sure a variable is a float instead of an integer even if it is a whole number, a trailing decimal point is used. Note the difference when a decimal point comes after a whole number: >>>g=5 # no decimal point >>>type(g) <class ‘int’> >>>g=5.0 #decimal point >>>type(g) <class ‘float’> Complex Number Data Type complex numbers: Python represents complex number as a pair of floating point numbers. >>>c= 0+4.5 j >>>d =1.1 +3.4 j >>>c 4.5 j >>>d (1.1 +3.4 j) A complex number with non-zero real part is displayed with parentheses around it. >>>print (c ) 4.5 j >>>print(d) (1.1 +3.4 j) Complex numbers are specified as <real part> + <imaginary part> j. For example : >>>2 +3j (2+3j) >>>type(2+3j) <class ‘complex’> Boolean Data Types A Boolean is such a data type that almost every programming language has, and so does python. Boolean in Python can have two values-True or False. These values can be used for assigning and comparison. Boolean data types are similar to a coin. At a time either we found head or tail, such that Boolean either returns True or False. There are two constants, true and false, these constants are used to assign the value to a Boolean data type. Example 1 Boolean Data Type : A= True B= False A&B A|B Output : False True Example 2 Boolean Data Type : print(True+false+True) output: 2 Note: Python treats a True value as 1 and a False value as 0.So a the above example returns 2(1+0+1) Example 3 Boolean Data Type: print(bool(True)) ; print(bool(False)); print(bool(0)); print(bool()); print(bool(3)); print(bool(“pankaj”)); print(bool(“”)); print((bool(‘’)) print(bool(True+True)); print(bool(False+False)); print(bool(True+False)) print(bool(None)); Output: True False False False True True False True True False True False STRING DATA TYPE A python string is a sequence of characters and each character can be individually accessed using its index. Strings are immutable so assignment is not allowed. Strings in python are stored by storing each character separately in contiguous locations. The characters of the strings are given two-way indices : . 0,1,2,…. in the forward direction and . -1,-2,-3,…. in the backward direction. Ex : name = ‘hello’ name[0]= ‘j’ will cause an error. 0 1 2 3 4 5 P Y T H O N -6 -5 -4 -3 -2 -1 It’s very easy to create the strings objects in Python, any characters which are enclosed by quotes become string(quotes can single or double). A string in Python is an immutable and ordered sequence of items. They can be defined using single – quotes(‘’) or double quotes(“”). A string spanning multiple lines can be defined using triple single-quotes(‘’’) or triple double-quotes(“””).For example: >>>myvar= ‘This\ is a string’ >>>type(myvar) <class ‘str’> >>>my_string= “””This is …my …first …string””” >>>print(my_string) This is My first string >>>my_string2=’’’ …This …is …my …second …string …’’’ >>>print(my_string2) This Is My Second string Since strings in Python are ordered, we can extract individual characters from a string using their integer indices, starting with 0. The first letter of a string is always at position 0, and the positions numerically increases after that. For example: >>>myvar= ‘This is a string’ >>>myvar[0] ‘T’ >>>myvar[10] ‘s’ Strings in Python also support slicing. Slicing is a technique which is used to extract a part of a variable using the notation[start_position: end_position], where start_position and end_position are integers indicating the length of the slice. If start_positon is omitted, then slicing begins at the beginning of the string, and if end position is omitted, then slicing end at the end of the string. For example: >>>myvar[0:1] >>> ‘T’ >>>myvar[0:] ‘This is a string’ >>>myvar[:len(myvar)] ‘This is a string’ TUPLE DATA TYPE The tuple is similar to list in Python. In Python, The tuple data type is immutable. That means the tuples cannot be modified, unlike lists. In Python, The tuple may contain different data type values. The tuples are enclosed in parentheses. They are represent as group of comma-separated values of any data types within parentheses, e.g. following are some tuples: Tuples are identical to lists in all respects, except for the following properties: Tuples are defined by enclosing the elements in parentheses (()) instead of square brackets ([]). Tuples are immutable. p = (1,2,3,4,5) q= (2,4,6.5,8) f=(2.4,7.6,9,”ram”,’v’) r = (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) h = (7,8,9, ‘A’, ‘B’, ‘C’) Example – Python code to illustrate ‘Tuple’ in Python >>>roll=(1,2,3,4,5) >>>print(roll) (1,2,3,4,5) >>>student=(1,“raman”, “12 class”) >>>print(type(student)) <class ’tuple’> >>>first =(1,2,3) >>>second=(4,5,6) >>>print(len(first)) 3 >>>print(max(first)) 3 >>>print(min(first)) 1 >>>print(first+second) (1,2,3,4,5,6) >>>print(first*3) (1,2,3,1,2,3,1,2,3) >>>print(1 in first) True >>>print(5 not in second) False This example shows several basic operations with tuples.The len( ) function returns the number of elements in the first tuple.
Details
- 
                                File Typepdf
- 
                                Upload Time-
- 
                                Content LanguagesEnglish
- 
                                Upload UserAnonymous/Not logged-in
- 
                                File Pages21 Page
- 
                                File Size-
