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
Portnov Computer School Test Automation For Web-Based Applications Presenter: Ellie Skobel Day 7 Python Basics 2 Memory locations reserved to store values Variables are dynamically typed Use the equal sign (=) to assign a value to a my_variable = “hello” variable Assign a single value to several variables a = b = c = 22 simultaneously Assign multiple values to multiple variables name, age, height= "Bob", 22, ”5’11" 3 Variable names are case-sensitive. A variable's name can be any legal identifier (an unlimited-length sequence of Unicode letters and digits) Must beginning with a letter or underscore Variables that start with an underscore (_) are treated as local and private, and cannot be imported from outside the module When choosing a name for your variables, use full words instead of cryptic abbreviations. This will make your code easier to read and understand 4 Built-in Functions Definition dict(iterable) Create a new dictionary enumerate(sequence, start=0) Count and iterate through the sequence eval(expression) Compiles and immediately evaluates an exec(expression) This statement supports dynamic execution of Python code. float(arg), int(arg), str(arg) Return a float, integer, or string constructed from the argument hasattr(object, str_arg_name) Returns true/false whether an attribute exists in an object list(iterable) Create a new list expression View all of the Built-in functions here: https://docs.python.org/2/library/functions.html 5 Built-in Functions Definition open(name, mode=r) Open a file print(objects, sep=' ') Print objects to the stream file range(start, stop[, step]) Create lists containing arithmetic progressions reversed(seq) Return a reverse iterator. seq round(number[, ndigits]) Return the floating point value number rounded to ndigits digits sorted(iterable) Return a new sorted list from the items in iterable. zip([iterable, iterable,…]) This function returns a list of tuples (2 or more dimensional list) View all of the Built-in functions here: https://docs.python.org/2/library/functions.html 6 Can be concatenated: ◦ "My name is " + "Jane" ◦ "My name is " + name "h" Can be sliced: ◦ name = "Geraldine" ◦ name[1:4] "era" ◦ ◦ ◦ ◦ Can be indexed: ◦ name = "John” ◦ name[2] Can be measured: ◦ name = "Geraldine" ◦ len(name) 9 Can be formatted: name = "JoHn" name.lower() name.upper() name.title() "john" "JOHN" "John” Can be repeated: ◦ name = "Bob" ◦ name*3 "BobBobBob" Can be reversed: ◦ name = "Geraldine" ◦ name[::-1] "enidlareG 7 Easy to create: ◦ my_list = [1,2,3] ◦ names = ["Bob", "Eva"] Can be combined: ◦ list = [4,2,6,8,3] ◦ list.sort()[2,3,4,6,8] ◦ a, b = [1, 2], [6, 7] ◦ a + b [1, 2, 6, 7] Can be indexed: Can be sliced: ◦ my_list = [1,2,3,4,5] ◦ my_list[1:4:2] [2,4] Can be repeated: ◦ list = [1,2,3] ◦ list*2 [1,2,3,1,2,3] ◦ names = ["Bob", "Eva"] ◦ names[1] "Eva" Can be sorted: Can be measured: ◦ list = ["a","b","c"] ◦ len(list) 3 Can be reversed: ◦ list = [1,2,3,4] ◦ list[::-1] [4,3,2,1] 8 IF ◦ x = 4 ◦ if x > 0: print("yes") IF / ELSE ◦ x = 4 ◦ if x > 0: print("yes") ◦ else: print("no") IF / ELIF / ELSE x = 4 if x > 2: print("yes") elif x > 0: print("still yes") else: print(""no) Conditional assignment ◦ age = 12 ◦ name = "Bob" if age > 18 else "Bobby" 9 Iterate through a list: list_of_items = [1,2,3,4] for item in list_of_items: print('-> ' + item) Iterate x times: x = 4 for j in range(x): print('number:', x) Iterate and enumerate (count): -> -> -> -> 1 2 3 4 number: number: number: number: 0 1 2 3 item 1 is an apple item 2 is an orange items = ["apple", "orange"] for j, item in enumerate(items): print("item {0} in an {1}".format(j+1, item)) 10 break: breaks out of the smallest enclosing loop loop stops after for x in range(2, 6): x=2 if 8 % x == 0: print 8, 'equals', x, '*', 8/x break 8 equals 2 * 4 continue: skips code which follows and continues with the next iteration of the loop: x = 4 for j in range(x): if x % 2 == 0: 3 is an odd number continue print x, 'is an odd number' print statement skipped for even numbers 11 ELSE: a loop’s else clause runs when no break occurs for x in ('a', 'b', 'c', 'd'): pass PASS statement does nothing. else: print('loop finished') loop finished 12