College of Engineering and Technology Name: Karabo Davis Bogaisang Student ID: 14001009 LAB EXPERIMENT: LAB 01, Introduction to Python Module: Artificial Intelligence and Neural Networks Code: EEEN 519 Introduction Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985-1990. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages. Objectives • Introducticing basic mathematical operations, variable types and printing options. ◦ Modules ◦ comments ◦ print ◦ arithmetic operators ◦ if ◦ else ◦ elif ◦ loops ◦ for ◦ while ◦ break ◦ continue Background Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them. Python has five standard data types: • • • • • Numbers String List Tuple Dictionary Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type. A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses. 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. Tuples can be thought of as read-only lists Python's dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object. Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]). Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions. Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. Experiment # LAB 01 # 1.0 VARIABLES import random a = 5 b = 13 x = 5.0 y = 13.0 m = "Mary" n = "Nancy" print(a) print(a, x, m, n) print(a + b) c = a + b print(c) c = 20 + a print(c) # 1.2 MANIPULATING NUMBERS print("5 + 2 =", 5+2) print("5 - 2 =", 5-2) print("5 * 2 =", 5*2) print("5 / 2 =", 5/2) print("5 % 2 =", 5%2) print("5 ** 2 =", 5**2) print("5 // 2 =", 5//2) print("1 + 2 - 3 * 2 =", 1 + 2 - 3 * 2) print("(1 + 2 - 3) * 2 =", (1 + 2 - 3) * 2) # 2.0 MAKING COMMENTS # This is my first Python program print("Hello World!") # This is my second Python program print("Hello World!") print(""" Look at the odd formatting of these They will show up as defined! """) quote = "\"Always remember your unique," multi_line_quote = ''' just like everyone else" ''' print(quote + multi_line_quote) # embed string in output print("%s %s %s" % ('I like the quote', quote, multi_line_quote)) # printing new line print("I don't like ", end="") print("newlines") # printing string multiple times print('\n' * 5) # 3.0 LISTS # creating a list grocery_list = ['Juice', 'Tomatoes', 'Potatoes', 'Bananas'] print('The first item is', grocery_list[1]) # change the value in a list grocery_list[0] = "Green Juice" print(grocery_list) # subset a list print(grocery_list[1:3]) # add values grocery_list.append('Onions') print(grocery_list) # insert item grocery_list.insert(1, "Pickle") print(grocery_list) # delete item del grocery_list[4] print(grocery_list) # 4.0 TUPLES pi_tuple = (3, 1, 4, 1, 5, 9) print(pi_tuple) # Convert tuple into a list new_tuple = list(pi_tuple) print(new_tuple) # Convert a list into a tuple new_list = tuple(grocery_list) print(new_list) # 5.0 DICTIONARY OR MAP super_villains = {'Fiddler': 'Isaac Bowin', 'Captain Cold': 'Leonard Snart', 'Weather Wizard': 'Mark Mardon', 'Mirror Master': 'Sam Scudder', 'Pied Piper': 'Thomas Peterson'} print(super_villains['Captain Cold']) # number of items in a map print(len(super_villains)) # value of the passed key print(super_villains.get("Pied Piper")) # list of dictionary keys print(super_villains.keys()) # list of dictionary values print(super_villains.values()) # 6.0 CONDITIONALS age = 30 if age > 16: print('You are if age > 16: print('You are else: print('You are if age >= 21: print('You are elif age >= 16: print('You are else: print('You are # old enough to drive') old enough to drive') not old enough to drive') old enough to drive a tractor trailer') old enough to drive a car') not old enough to drive') # 7.0 LOOPS for x in range(0, 10): print(x , ' ', end="") print('\n') for y in grocery_list: print(y) for x in [2, 4, 6, 8, 10]: print(x) num_list = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]; for x in range(0, 3): for y in range(0, 3): print(num_list[x][y]) # 8.0 WHILE LOOPS random_num = random.randrange(0, 100) while (random_num != 15): print(random_num) random_num = random.randrange(0, 100) i = 0; while (i <= 20): if i%2 == 0: print(i) elif i == 9: # Force the loop to end all together break else: # Shorthand for i = i + 1 i += 1 # Skips to the next iteration of the loop continue i += 1 Results Print outs obtained after running the codes. 1.0 VARIABLES 5 5 5.0 Mary Nancy 18 18 25 1.2 MANIPULATING NUMBERS 5+2=7 5-2=3 5 * 2 = 10 5 / 2 = 2.5 5%2=1 5 ** 2 = 25 5 // 2 = 2 1 + 2 - 3 * 2 = -3 (1 + 2 - 3) * 2 = 0 2.0 MAKING COMMENTS Hello World! Hello World! Look at the odd formatting of these They will show up as defined! "Always remember your unique, just like everyone else" I like the quote "Always remember your unique, just like everyone else" I don't like newlines 3.0 LISTS The first item is Tomatoes ['Green Juice', 'Tomatoes', 'Potatoes', 'Bananas'] ['Tomatoes', 'Potatoes'] ['Green Juice', 'Tomatoes', 'Potatoes', 'Bananas', 'Onions'] ['Green Juice', 'Pickle', 'Tomatoes', 'Potatoes', 'Bananas', 'Onions'] ['Green Juice', 'Pickle', 'Tomatoes', 'Potatoes', 'Onions'] 4.0 TUPLES (3, 1, 4, 1, 5, 9) [3, 1, 4, 1, 5, 9] ('Tomatoes', 'Pickle', 'Onions', 'Green Juice') 5.0 DICTIONARY OR MAP Leonard Snart 5 Thomas Peterson dict_keys(['Fiddler', 'Captain Cold', 'Weather Wizard', 'Mirror Master', 'Pied Piper']) dict_values(['Isaac Bowin', 'Leonard Snart', 'Mark Mardon', 'Sam Scudder', 'Thomas Peterson']) 6.0 CONDITIONALS You are old enough to drive You are old enough to drive You are old enough to drive a tractor trailer 7.0 FOR LOOPS 0 1 2 3 4 5 6 7 8 9 Tomatoes Pickle Onions Green Juice 2 4 6 8 10 1 2 3 10 20 30 100 200 300 8.0 WHILE LOOPS 85 97 62 25 75 24 41 69 42 27 45 46 6 59 73 14 80 85 35 76 81 69 94 96 7 59 22 48 7 1 24 40 9 9 8 92 48 79 46 17 58 35 51 28 36 78 68 37 98 86 96 33 91 88 20 23 59 66 85 23 91 14 11 97 61 89 45 47 59 31 50 84 21 26 55 2 77 23 55 1 29 96 68 24 76 43 12 67 52 0 2 4 6 8 Questions and Solutions Q 3.1: Remove ‘Potatoes’ from the grocery list? del grocery_list[3] print(grocery_list) ['Green Juice', 'Pickle', 'Tomatoes', 'Onions'] Q 3.2:Sort the list? grocery_list.sort() print(grocery_list) ['Green Juice', 'Onions', 'Pickle', 'Tomatoes'] Q 3.3:Reverse sort the list? grocery_list.reverse() print(grocery_list) ['Tomatoes', 'Pickle', 'Onions', 'Green Juice'] Q 4.1: Convert grocery list into a tuple? new_grocery_list = tuple(grocery_list) print(new_grocery_list) ('Tomatoes', 'Pickle', 'Onions', 'Green Juice') Q 5.1: Delete Fiddler entry together with its value? del super_villains["Fiddler"] print(super_villains) {'Captain Cold': 'Leonard Snart', 'Weather Wizard': 'Mark Mardon', 'Mirror Master': 'Sam Scudder', 'Pied Piper': 'Thomas Peterson'} Q 5.2: Replace a value at 'Pied Piper' with 'Hartley Rathaway' super_villains["Pied Piper"] = "Rathaway" print(super_villains) {'Captain Cold': 'Leonard Snart', 'Weather Wizard': 'Mark Mardon', 'Mirror Master': 'Sam Scudder', 'Pied Piper': 'Rathaway'} Q 6.1: Write the following code: if you are 1 or 18 or between the ages of 1 and 18, then you get a birthday party. Else if you are 21 or 65 or greater than 65, then you get a birthday party with your family. Else if you are not 30 years of age, then you don’t get a birthday party. Else you get a birthday party yeah. if age >= 1 and age <= 18: print('You get a birthday party') elif age == 21 or age >= 65: print('You get a birthday with a family') elif age != 30: print('You do not get a birthday party') else: print('You get a birthday party yeah') You get a birthday party yeah Q 7.1: Change the following Python code from using a while loop to for loop: x=1 while x<10: print x, x+=1 for x in range (1, 10): print(x) 1 2 3 4 5 6 7 8 9 Q 7.2: Change the following Python code from a for loop to a while loop: for i in range(1,50): print i, i = 1 while i < 50: print(i) i += 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Conclusion Basic mathematical operations were covered. Operations from variable types, printing options, comments, arithmetic operators, order of operation, lists, tuples, dictionaries, conditional operators, logical operators, if, else, elif, loops, for, while, break and continue. The objectives of the experiment were meet. Reference https://www.tutorialspoint.com/python