Python Lists: Lists are Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). All the items belonging to a list can be of different data type. The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1. Example 1: =["apple", "orange",100,25.5] [0] 'apple' >>> 3*items[:2] ['apple', 'orange', 'apple', 'orange', 'apple', 'orange'] [2]=items[2]+50 ['apple', 'orange', 150, 25.5] [0:2]=[20,30] # replace some elements [20, 30, 150, 25.5] [0:2]=[] # remove some elements [150, 25.5] [2:2]=[200,400] # insert some elements [150, 25.5, 200, 400] >>> del items[0] # delete element at index 0 from list [25.5, 200, 400].append("orange") # List.append(object) -- append object to end [25.5, 200, 400, 'orange'] >>> 200 in items # check existing item in list True >>> 200 not in items # check not existing item in list False
Python Tuples: A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas, tuples are enclosed within parentheses (). Tuples its read only, that s means are immutable. The main differences between lists and tuples are: Lists are enclosed in brackets ([ ]) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. For example: Example 2: >>> t=("orange",55,3.5) >>> t[0] 'orange' >>> t[1:3] (55, 3.5) >>> t[-1] 3.5 >>> len(t) 3 >>> # Tuples may be nested >>> u=(t,(1,2,3)) >>> u (('orange', 55, 3.5), (1, 2, 3)) >>> len(u) 2 >>> # Tuples are immutable: >>> t[0]=100 # not allow to update tuple Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
Python Dictionary: Python's dictionaries are kind of hash table type consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. A dictionary Values can be any arbitrary Python object. Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). Dictionaries have the elements are "out of order", they are simply unordered. For example: Example 3: >>> dict={'name':'ali', 'code':12016000,'dept':"engineering"} >>> dict['name'] 'ali' >>> dict.keys() ['dept', 'code', 'name'] >>> dict.values() ['Engineering', 12016000, 'ali'] >>> dict2={ } >>> dict2[ "one" ]=" this is one" #add a new element to dictionary >>> dict2 [2]= "this is two" >>> dict {2: 'this is two', 'one': ' this is one'} >>> del dict["one"] # delete element has a key one from dictionary >>> dict {2: 'this is two'} >>> dict.clear() # delete all elements >>> dict {}
Mathematical functions: 1. Build-in Functions The Python interpreter has a number of functions built into it that are always available: abs(x) The absolute value of x. cmp(x, y) -1 if x < y, 0 if x == y, or 1 if x > y max( [ ] ) return its largest item min( [ ] ) return its smallest item sum ( [ ] ) return the sum of a sequence of numbers Example 4: print "abs(-5) = ",abs(-5) print "list=[88,5,9], largest number in list is = ",max([88,5,9]) print "list=[88,5,9], smallest number in list is = ",min([88,5,9]) print "sum of a sequence of numbers [8,6,2,5] = ",sum([8,6,2,5]) print "compare two number :",cmp(8,5) Output: 2. Math module functions : The following functions are provided by this module, before use it we must invoke module using import keyword: ceil(x) Return the ceiling of x as a float, the smallest integer value greater than or equal to x. floor(x) Return the floor of x as a float, the largest integer value less than or equal to x. fabs(x) Return the absolute value of x as a float. sqrt(x) The square root of x for x > 0 log(x) The natural logarithm of x, for x> 0 log10(x) The base-10 logarithm of x for x> 0 Trigonometric Functions Return value of function in radians sin(x), cos(x), tan(x)
radians(x) degrees(x) exp(x) Converts angle x from degrees to radians. Converts angle x from radians to degrees. The exponential of x: ex Constants: pi The mathematical constant π = 3.141592... e The mathematical constant e = 2.718281... How to use it: Example 5: import math # firstly we must import math module print "sin(90) = ",math.sin(math.pi/2) print "cos(180) = ",math.cos(math.pi) print "fabs(-5)=", math.fabs(-5) print "floor(5.9)=",math.floor(5.9) print "ceil(5.9)=",math.ceil(5.9) print "sqrt(49) =",math.sqrt(49) print "log10(1000) =",math.log10(1000) print "pi = ", math.pi print "degrees(math.pi/2) =",math.degrees(math.pi/2) print "e^2 = ",math.exp(2) print "log e ^2 = ",math.log(math.exp(2)) Output: End
Exercises: 1) Write a Python program that compute the area of a circle given the radius entered by the user. Area = πr 2 2) Write a Python program to solve quadratic equations of the form ax 2 + bx + c = 0 Where the coefficients a, b, and c are real numbers taken from user. The two real number solutions are derived by the formula x = b ± b2 4ac 2a For this exercise, you may assume that a 0 and b 2 4ac