# Module 2 ## Datatypes and operatord in Python 1. Comments 2. Docstrings 3. Built-in-data types 4. Sequences 5. Sets 6. Literals 7. User defined data types 1. Basic Operators 2. Membership Operators 3. Operator precedence and associativity 4. Output & Input statements 5. Command line statement 6. Control Statement ## Types of comments 1. Single line comments ``` #Comment ``` 2. Multi line comments ``` #using #multiple #hashtags ``` or ``` ''' or using triple quotes''' ``` ## Datatypes in Python 1. Built-in Datatypes: 1. None Type 2. Numeric Type 1. Int 2. Float 3. Complex 3. Sequence 1. str 2. bytes 3. bytearray 4. list 5. tuple 6. range 4. Sets 5. Mapping ## Built in list methods assuming we have list1 and list2 | Method | Description | | ------------- | ------------- | | cmp(list1, list2) |it compares the elements of the lists| | len(list1) | it would tell you about the length of the list | |max(list1)|it would tell you about the item with largest value or the last item in alphabatical order| |min(list1)|it would tell you about the item with smallest value or the first item in alphabatical order| |list(tupple)|tupple is converted to list| |list.append(item)|item is added to the end of the list| |list.count(item)|counts the number of the item in our list| |list1.extend(list2)|list2 will be added to list1 making a new list and saving it as list1| |list.index(item)|tells you about the position of the item in our list | |list.insert(index,item)|inserts the item on the corresponding index number in our list| |list.pop(index)|item at the following index will be removed | |list.pop()|the last item of the list will be removed| |list.reverse()|the list will be saved in reverse order| |list.remove(item)|the following item will be removed from the list| |list.remove()|would give you error as the item needs to be mentioned| |list.sort()|sorts the list alphabatically or ascending | |x = sum(list)|integers in the list are added and sum is returned if list has strings then "AttributeError"| |list.clear()|deletes all the items in the list| # Datatypes in Python ## 1. Basic operator ### Arithmetic Operators 1. '+' Additition 2. '-' Subtraction 3. '*' Multiplication 4. '/' Division 5. '**' Exponent 6. '//' Floor division ### Assignment Operator 1. '=' Equal to 2. '+=' z+=x -> z = z + x 3. '-=' z-=x -> z = z - x 4. '*=' z*=x -> z = z * x 5. '/=' z/=x -> z = z / x 6. Similarly for ** (Exponential) 7. '//=' z//=x -> z = z // x ### Unary Operator add a '-' negative sign to make a positive number negative and vice versa ### Relational Operator this operator would check the condition and give us True or False 1. '>' a>b = a is greater than b 2. '<' a<b = a is less than b 3. '>=' a>=b = a is greater than or equal to b 4. '<=' a<=b = a is less than or equal to b 5. '==' a == b = a is equal to b 6. '=!' a != b = a is not equal to b ### Bitwise Operator 1. Bitwise Complement Operator (~) : NOT Gate 2. Bitwise AND Operator (&) : AND Gate 3. Bitwise OR Operator (|) : OR Gate 4. Bitwise XOR Operator (^) : XOR Gate 5. Bitwise left shift operator (<<): Shifts binary to the left and fills empty space with 0 6. Bitwise left shift operator (>>): Shifts binary to the right and fills empty space with 0 ### Logical Operators 1. 'and' : x and y = If x is False (0) it returns x otherwise it returns y 2. 'or' : x or y = If x is False (0) it returns y otherwise it returns x 3. 'not : not x = if x is 0 it returns True otherwise False ### Boolean Operators 1. 'and' : And operation of the condition and gives us boolean value 2. 'or' : Or operation of the condition and gives us boolean value 3. 'not : does not operation of the boolean value ### Membership Operators 1. 'in' 2. 'not in' used in loops eg: ``` name = [ yash , sejal , manan] for name in names: print(name) output: yash sejal manan ``` ### Identify Operators 1. 'is' 2. 'not is' similar to == and != operator ## Mathematical Functions ### Functions: 1. ceil(x): raises x to the next integer 2. floor(x): decreases x to the previous integer 3. degrees(x): converts x into degree 4. radians(x): converts x into radian 5. sin(x) 6. cos(x) 7. tan(x) 8. exp(x) 9. fabs(x): mod value of x 10. factorial(x) 11. fmod(x,y): returns x%y for floats 12. fsum(values): sum for floats 13. modf(x): returns integer part and decimal part of x seperately 14. log10(x) 15. log(x,base) 16. sqrt(x) 17. gcd(x,y) 18. trunc(x): round off of towards 0 19. isinf(x): if x is +/- infinity True is returned 20. isnan(x): if x is Not An Number True is returned ### Constants: 1. pi 2. e 3. inf 4. nan