Partial preview of the text
Download class 12 compuyer science full notes and more Study notes Computer science in PDF only on Docsity! DETAILED DEFINITIONS OF DATA TYPES © 1. String (str) Definition Astring is a sequence of characters enclosed in single, double, or triple quotes used to represent text data. Key Points e Writtenin' ‘or e Immutable (cannot be changed) e Supports indexing and slicing «© Example: "Hello" © 2. List © Definition Alistis an ordered collection of elements enclosed in square brackets [] that can store multiple values of different data types. Key Points e Mutable (can be changed) e Allows duplicate values e Supports indexing «© Example: [1, 2, 3] © 3.Tuple Definition A tuple is an ordered collection of elements enclosed in parentheses (), which is immutable in nature. Key Points ¢ Cannot be modified after creation e Allows duplicate values e Faster than list «© Example: (1, 2, 3) © 4. Dictionary Definition A dictionary is a collection of data stored in key-value pairs enclosed in curly braces {}, where each key is unique. Key Points e Mutable e Keys must be unique e Values can be duplicated «© Example: {"name": "Ram", “age": 18} «© Example: True © 9. None Type Definition None is a special data type that represents the absence of a value or null value. «© Example: None © 10. Mutable Data Types Definition Mutable data types are those whose values can be changed after creation. «© Examples: List, Dictionary © 11. Immutable Data Types Definition Immutable data types are those whose values cannot be changed after creation. «© Examples: int, float, string, tupl Choter 10 : STRING MANIPULATION String Manipulation Functions and Methods se eau |. [len(string>) _2.__| . capitalize( ) —_ 3. | . count(sub[, st[, end] ] ) {st is start .find(sub[, st[, end]]) st is start _5.__| . index(sub[, st[, end]]) 6. | .isalnum( ) | 7. | . isalpha( ) | 8. | . isdigit( ) 9. | . islower( ) | 10. | . isspace( ) | 11. | . isupper( ) 12, _| . lower( ) | 13. . upper( ) 14. | . 1strip( ) . rstrip( ) | . strip( ) 15. . startswith( ) . endswith( ) 16. . title( ) 17. | . istitle( ) ) 18. . replace(old, new) | 19. | ann iterable>) | 39, | . split ) | _ - - | 21. string. partition() | sep is seperator — At retums the length of its argument string. It returns a copy of the string with its first | There must be at least one cased character. It returns False other- It returns the number of occurrences of the substring sub in string | the (or string [start:end] if these arguments are given). It returns the Jowest index in the string where the substring sub i is found within the slice range of start and end. Returns ~1 if sub is _ not found. It returns the lowest index where the specified substring is is found. | It returns True if the characters in the string are alphanumeric (al- | phabets or numbers) and there is at least one character, False oth- | erwise. It returns True if all characters in the string are alphabetic and | there is at least one character, False otherwise. _ | It returns True if all the characters in the string are digits. There nwust be at least one character, otherwise it returns False. | It returns True if all cased characters in the string are lowercase. | | wise. | It returns True if there are only whitespace characters in n the | string. There must be at least one character. It returns False other- | wise. It tests whether all cased characters in the string are uppercase and requires that there be at least one cased character. Returns True if so and False otherwise. _ It returns a copy of the String or converted to lowercase. It returns a copy of the string with leading wwhite- spac spaces es temoved: It returns a copy of the string with trailing white- spaces removed. It returns a copy of the string with leading and trailing whitespaces removed. _ | Tt returns True if the string starts with the substring sub, otherwise | returns False It returns True if the string ends with the substring sub, otherwise returns False It returns a title-cased version of the string where all words start with uppercase characters and all remaining letters are lowercase. + It returns True if the string has the title case. It returns a copy of the string with all occurrences of substring old replaced by new string. 7 It joins a string or character (ie. _string iterator Le., a string based s - It splits a string(ie., ) based on the given string or character (ie, ) and returns a list containing, split strings as members. 7 = It splits the string at the fil first occurrence of separator, “and returns a tuple containing three items as string till separation, spe | and the string after separator. ) after each member of the Wentce. PUES Perini S.No. Function / Method __ Description so (| ina Bcc hah hth le eRe es aac ee 1. | len() This function returns length of the tuple, i.e. the count of elements in the tuple. 2. |max() This function returns the element from the tuple havin, maximum value. 8 3. | min() This function returns the element from the tuple having minimum value. 4. | sum () The sum( ) function takes the tuple sequence and returns the sum of the elements of the tuple. 5. | . index (- ) The index( ) method works with tuples in the same Way it works with lists. That is, it returns the index of an existing element of a tuple. 6. |
.count() | The count( ) method returns the count of a member | element/object in a given sequence (list/tuple). _ —_ 1 7. |sorted(, This function takes the name of the tuple as an argument [reverse=False] ) and returns a new sorted list with sorted elements in it. 8. | tuple() This creates tuples from different types of iterable sequences. | ae Summary of Dictionary Manipulation Functions and Methods | 1. od S.No. _ _- — Function / Method [D is Dictionary] len() _ .get( key , [ default ]) | . items() . keys() a . values() : _ dict. fromkeys(, []) dict. setdefault(, ) . update ( ) .copy() _ . pop(key, ) | .popitem() _ a . clear() sorted(, [reverse = False] ) mex ( ) min( ) sum( ) 4 ee — ats Description en Returns the number of items in the dictionary. Returns corresponding value if the key exists, else it r, etums Python's Error if default argument is missing otherwise _returns default argument. e Returns a sequence of (key, value) pairs, the items Returns a list sequence of keys. Returns a list sequence of values. Creates a new dictionary from a sequence containing al] the keys and a common value. Inserts a new key:value pair only if the key doesn’: already exist, else it returns the current value of the key debts Mick —_— ~ Merges key: value pairs from the new dictionary into the original dictionary, adding or replacing as needed. Creates a shallow copy of a dictionary Removes and returns the dictionary element associated to passed key. Removes and returns a (key, value) pair from the dictionary Removes all items from the dictionary. Returns a sorted list of the dictionary keys. Give minimum key, maximum key or sum of keys from the dictionary @ 3. Logical Operators Used with conditions Operator Meaning Example and True ifbothtrue True and False > False or Trueifanytrue True orFalse > True not Reverse result not True > False Fea 4. Assignment Operators Used to assign values Operator Example Meaning = x=5 assign 5 tox SJ 5. Augmented Assignment Operators Shortcut for operations + assignment Operator Example Sameas += xt=3 X=xXt3 -= x-=3 X=x-3 Fa x*=3 X=x*3 = x/=3 x=x/3 //= xH=3 x=x//3 = x%H=3 X=x%3 OK = xX**= 2 X=X**D ID} 6. Identity Operators Check if two variables refer to same object in memory Operator Meaning Example is Same object xisy . Notsame . is not . xis noty object ‘ Example: a = [1,2] b=a ais b # True (same object) @ 7. Membership Operators Check if value exists in a sequence (list, string, tuple) Operator Meaning Example in Present ‘a'in "apple" > True . Not . not in 'z' notin "apple" > True present «& Examples: e math.sqrt() e random.randint() «© Exam point: Use import module_name to access them. Functions that are created by the programmer according to need. i) User Defined Functions «© Exam line: Defined using def keyword. © 3.Creating User Defined Function Definition A user-defined function is created using the def keyword followed by function name and parameters. Syntax def function_name(parameters) : statements | Example def greet(): print ("Hello") © 4. Arguments and Parameters Parameter (Definition) A parameter is a variable in function definition that receives value. Argument (Definition) An argumentis the actual value passed to the function when it is called. \) Example: def add(a, b): # a, b are parameters print(a + b) add(2, 3) # 2, 3 are arguments © 5. Types of Parameters (i) Positional Parameters Values are passed in the same order as parameters. def add(a, b): print(a + b) add(2, 3) «> Order matters. «© Returns multiple values as tuple. © 7. Flow of Execution Definition Flow of execution refers to the order in which statements are executed in a program. Steps 1. Program starts from first statement 2. When function is called > control goes to function 3. After execution > returns back © 8. Scope of Variables Definition Scope defines the area where a variable can be accessed. Types of Scope © (i) Local Scope e Variable declared inside function e Accessible only inside that function def func(): x =5 # local variable © (ii) Global Scope e Variable declared outside function e Accessible throughout program x = 10 # global variable © Difference: Local vs Global Scope Local Global Inside Outside function function Limited Whole program access Temporary Permanent 129 FINAL REVISION (SUPER SHORT) e Function > reusable block e Built-in > already defined e Module imported functions e User-defined > created by user e Parameter > variable e Argument > actual value e Default > predefined value e Scope > variable visibility from math import sqrt print(sqrt(16) ) (A) Difference: import vs from import module from module import Imports full Imports specific module function Use module.name Use directly 3. MATH MODULE Definition The math module provides mathematical functions and constants. Important Functions & Constants e pi> value oft e e>Euler’s number e sqrt(x) > square root e ceil(x) >round up e floor(x) > round down © pow(x, y) >x“y e fabs(x) > absolute value e sin(x), cos(x), tan(x) > trigonometric functions Example Code import math print(math. pi) print(math.sqrt(25)) print(math.ceil(2.3)) print(math.floor(2.9)) print(math.pow(2, 3)) print(math. fabs (-10)) 4. RANDOM MODULE Definition The random module is used to generate random numbers. Important Functions e random() > random float (0 to 1) e randint(a, b) > random integer between a and b e randrange(start, stop) > random number in range Example Code import random print(random. random() ) print(random.randint(1, 10)) print(random.randrange(1, 20))