Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Islamic University Of Gaza Faculty of Engineering Computer Engineering Department Lab 3 Variable and Data Type II Eng. Ibraheem Lubbad October 7, 2016 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: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2] ['apple', 'orange', 'apple', 'orange', 'apple', 'orange'] >>> items[2]=items[2]+50 >>> items ['apple', 'orange', 150, 25.5] >>> items[0:2]=[20,30] # replace some elements >>> items [20, 30, 150, 25.5] >>> items[0:2]=[] # remove some elements >>> items [150, 25.5] >>> items[2:2]=[200,400] # insert some elements >>> items [150, 25.5, 200, 400] >>> del items[0] # delete element at index 0 from list >>> items [25.5, 200, 400] >>> items.append("orange") # List.append(object) -- append object to end >>> items [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) cmp(x, y) max( [ ] ) min( [ ] ) sum ( [ ] ) The absolute value of x. -1 if x < y, 0 if x == y, or 1 if x > y return its largest item return its smallest item 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) import math 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) floor(x) fabs(x) sqrt(x) log(x) log10(x) Trigonometric Functions sin(x), cos(x), tan(x)… Return the ceiling of x as a float, the smallest integer value greater than or equal to x. Return the floor of x as a float, the largest integer value less than or equal to x. Return the absolute value of x as a float. The square root of x for x > 0 The natural logarithm of x, for x> 0 The base-10 logarithm of x for x> 0 Return value of function in radians 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 e The mathematical constant π = 3.141592... 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. 𝐴𝑟𝑒𝑎 = 𝜋𝑟 2 2) Write a Python program to solve quadratic equations of the form 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0 Where the coefficients a, b, and c are real numbers taken from user. The two real number solutions are derived by the formula −𝑏 ± √𝑏 2 − 4𝑎𝑐 2𝑎 For this exercise, you may assume that a ≠ 0 and 𝑏 2 ≥ 4𝑎𝑐 𝑥=