Untitled document.pdf
Document Details
Uploaded by AmbitiousSaxhorn5504
Full Transcript
University of New Haven TCOE: ECECS 6602: Python Programming for Data Science Fall 2024 keyboard_arrow_down Class 1 : Python: Hello World 1.1 Why Programming is essential...
University of New Haven TCOE: ECECS 6602: Python Programming for Data Science Fall 2024 keyboard_arrow_down Class 1 : Python: Hello World 1.1 Why Programming is essential for Data Science? 1. Data Engineering: (Data Revtrieval, Manipulation) 2. Pre-built analytics libraries accessible through code only; 3. Customization of solutions 4. Scalability 5. Automation (Repeatability) 6. Integration with other systems. https://www.scaler.com/blog/programming-languages-for-data-science/ 1.1.2 Why Python ? Cross platform Application Areas: Data Science, AI, IoT, Web, Scripting Mode: REPL/Shell, Program, Interactive Flexibility of paradigm: Structural, Functional and Object Oriented Ecosystem: Large sets of libraries to accoplish many tasks 1.2 Programming Languages 1.2.1 Genesis of Programming Languages Binary, Assembly, 3/4th Gen languages purpose driven vs general purpose (Java, JavaScript) 1.2.2 Compiled vs Interpreted Languages Interpreted Compiled into binary for a specific operting system Executed within sandboxes (including Browser or VMs) Hybrid 1.2.3 Based on Programming Paradigms Structural or procedural (Pascal, Fortran) Object Oriented Functional 1.3 Installing Python 1.3.1 Setup Python on your computer: Download Python | Python.org https://www.python.org/downloads/ 🤔 During the installation do not forget to select the option to "add python into system path". 1.3.2. Setup VS Code as Editor on your computer: Download Visual Studio Code - Mac, Linux, Windows https://code.visualstudio.com/download 1.3.3. Test Python: open terminal or command line tool. (term on linux or mac, CMD or Command on Windows) type python --version If you get a python version info, you are good!! 1.3.4. Test VS Code: open terminal or command line tool. (term on linux or mac, CMD or Command on Windows) type "code." if VS Code opens, you are good!! 1.4 Running Python Three ways to run python: 1.4.1 Run interactively as SHELL or REPL Interpreter One instruction at a time Interactive Interpreted (kind of) Terminal or Shell mode 1.4.2 Create a file with python code and run it Create a fileanme.py file in VS Code or your favorite editor (IDE) Run the file from terminal or shell as python filename.py 1.4.3 Run as a Jupyter Notebook Think of it as a mix of both interactive and program file mode "Notebook" approach means it will have cells of code(for python to execute) and markdown (for users to read and understand) Create a file with : fileanme.ipynb ( Interactive PYthon NoteBook ) keyboard_arrow_down How to write in Python Python is space sensitive one line of code Top Block of code line 1 Top Block of code line 2 First Block of code line 1 First Block of code line 2 First Block of code line 3 Second Block of code line 1 Second Block of code line 2 Second Block of code line 3 Python is case sensitive if a > b: # correct case c=45 IF a > b: # Incorrect c=45 MyName = "Joe Smith" myName = "Jane Doe" myname = "Jess Jones" # All the variables are DIFFERENT Python is scope sensitive def func_one(): var_1=value var_2 = var_1 + 5 # Error as var_1 scoped out VARIABLE_A = Value VARIABLE_B = Value VARIABLE_C = VARIABLE_A + VARIABLE_B # ERROR VARIABLE_A = Value VARIABLE_B = Value VARIABLE_C = VARIABLE_A + VARIABLE_B # ERROR def Some_Function (): VAR_SCOPED="Earth Math failed on this planet;" print(VAR_SCOPED) # Causes Error as VAR_ONE is limited in scope within function defintion --------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In, line 5 1 def Some_Function (): 2 VAR_SCOPED="Earth Math failed on this planet;" ----> 5 print(VAR_SCOPED) 6 # Causes Error as VAR_ONE is limited in scope within function defintion NameError: name 'VAR_SCOPED' is not defined Comment in Python # acts as single line comment There's no multiline comment in python keyboard_arrow_down Let's write "Hello World!" program When this is exeucted, it will output a simple greeting to user. print ("message") is the function to use for outputting a message to user. print ( "Hello World!") # This is great beginning Hello World! keyboard_arrow_down 1.5 Variables Variables are boxes or labels that carry values! Think of variable as an empty "labeled" box 📦 that can carry a specific type of content. (Shoe box) Variable = Box (Label) Data Type of variable = What type of content can this variable hold (what type of content is in the box.) Value = Content in the box 1.5.1 Data Types Type Data Type Numeric int , float Aplhanumeric string Null NoneType Type Data Type Boolan bool valid values are True or False Complex Complex Numbers real + j (imaginary) # int i = 45 # i is variable, 45 is value, type of i is "int" type(i) # type is a global function like print() int # float f = 42.34 type(f) # type is a global function like print() float k=45. f= "Now is a string" # Don't do this keyboard_arrow_down 1.5.2 Data Types Determination are determined without EXPLICIT declaration i=42 print( "42 tranlsates into") print ( type(i) ) f=43.34 print( "43.34 tranlsates into ") print ( type(f) ) s="This is awesome" print( "\"This is awesome\" tranlsates into ") print ( type(s) ) b=True print( "True tranlsates into ") print( type(b) ) 42 tranlsates into 43.34 tranlsates into "This is awesome" tranlsates into True tranlsates into keyboard_arrow_down 1.5.3 Variable naming A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables: A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) A variable name cannot be any of the Python keywords. # Legal variable names myvar = "John" my_var = "John" _my_var = "John" # Underscore 💪🏼 has a special place in Python myVar = "John" MYVAR = "John" myvar2 = "John" # Illegal variable names # that cuase errors 2myvar = "John" my-var = "John" my var = "John" keyboard_arrow_down 1.5.4 Multiple Assignment x = y = z = 42.54 print(y) 42.54 keyboard_arrow_down 1.6 Variable Operators 1.6.1 Operators for numeric data Operator Operation + Addition - Subtraction / Divsion * Multiplication // Floor Division % Modulus ** Exponent keyboard_arrow_down 1.6.2 Operators for alpha-numeric or String data Operator Operation + Concatenation * Repeat first_name="John" last_name="Smith" full_name = first_name + last_name print(full_name) banner_my_name=first_name * 5 print(banner_my_name) print(45*"-") JohnSmith JohnJohnJohnJohnJohn --------------------------------------------- str1="Howdy" str2=str1 - 42 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 3 1 str1="Howdy" ----> 3 str2=str1 - 42 TypeError: unsupported operand type(s) for -: 'str' and 'int' Bitwise Operators Operator Operation Exam & AND : Sets each bit to 1 if both bits are 1 x&y | OR : Sets each bit to 1 if one of two bits is 1 x|y ^ XOR : Sets each bit to 1 if only one of two bits is 1 x^y ~ NOT : Inverts all the bits ~x Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off x >> 2 keyboard_arrow_down 1.7 Basic Input and Output 1.7.1 Output to display (StdOut) print() print("Hello World") Print Variables Strings name="Test" print(name) Non-Strings cost=42.35 print("Product Costs "+str(cost)) name="Test" print(name) cost=42.35 print(str(cost)) # Not changing the type may cause error print("Product Costs "+str(cost)) print("Product Costs "+cost) #Won't work Test 42.35 Product Costs 42.35 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 7 5 print(str(cost)) # Not changing the type may cause error 6 print("Product Costs "+str(cost)) ----> 7 print("Product Costs "+cost) TypeError: can only concatenate str (not "float") to str keyboard_arrow_down 1.7.1 Input from Keyboard (StdIn) variable = input ("Prompt") name = input("What is your name?") print(name) bill age= input ("What's your age?") myAgeinTwoYears=age+2 print(myAgeinTwoYears) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 3 1 age= input ("What's your age?") ----> 3 myAgeinTwoYears=age+2 5 print(myAgeinTwoYears) TypeError: can only concatenate str (not "int") to str keyboard_arrow_down End of session 1 University of New Haven TCOE: ECECS 6602: Python Programming for Data Science Fall 2024 keyboard_arrow_down Class 2 : Control Flow keyboard_arrow_down 2.1 Conditional Processing 2.1.1 Binary Conditions : True and False Processing IF **CONDITION**: WHEN TRUE DO THIS... ELSE: WHEN FALSE DO THIS... For Example: If passing score for course is 40 points, student with x score is pass or failed the class. # Passing score is 40 score = 45 if score >= 40: print("Student Passed the course.") print("This is True") type(score) else: print("Student failed the course.") print("This is False") print("When will this be executed") Student Passed the course. keyboard_arrow_down 2.1.2 Multiple conditions Processing IF **CONDITION**: WHEN TRUE DO THIS... ELIF **Another Condition**: WHEN TRUE DO THIS... ELIF **Another Condition**: WHEN TRUE DO THIS... ELSE: WHEN ALL CONDITIONS WERE FALSE... Use Case: Let's assign letter grades to student Letter Grade Score Range A 90 to 100 B 80 to 89 C 70 to 79 D 50 to 69 if score >= 90: E 40 to 49 print("Student Passed the course with an A.") elif score >= 80: print("Student Passed the course with a B.") elif score >= 70: print("Student Passed the course with a C.") elif score >= 50: print("Student Passed the course with a D.") elif score >= 40: print("Student Passed the course with a E.") else: print("Student failed the course.") Student Passed the course with a E. keyboard_arrow_down 2.2 Repeatable Code: Methods and ways to execute a particular step or operation multiple times without hacing to write it multiple times. For example, if we need to track 5 students and their scores. score_student_1 = 89 score_student_2 = 65 score_student_3 = 76 score_student_4 = 94 score_student_5 = 81 print(score_student_1) print(score_student_2) print(score_student_3) print(score_student_4) print(score_student_5) 89 65 76 94 81 keyboard_arrow_down Define LIST for a set of values Python allows us to create an array or a list of all students in a single variable of type list scores = [ 89,65,76,94,81 ] # Holds scores of all students type(scores) # What's the data type of this variable list len(scores) 5 List have values within SQAURE Brackets [ and ] and values are sperated by Comma , scores = [ 89, 65, 76, 94, 81 ] keyboard_arrow_down 2.2.1 Repeatable code through Loops Loops instruct python to execute a command or set of commands multiple times. There are three types of loops: 1. Loops to execute commands for EACH memeber of a group 2. Loop to execute commands certain number of times 3. Loop to execute commands as long as a certian condition is met. 2.2.1.1 for each member of group, do something; for each in list loop Instead of printing one score at a time you can create a for loop for loop syntax FOR x in SET: DO THIS FOR EACH VALUE IN SET for score in scores: print (score) 89 65 76 94 81 keyboard_arrow_down 2.2.1.2 for certain number of times, do something; for loop with range(count) loop For exmaple printing numbers one through 10. for loop iterations syntax FOR x in range(count): DO THIS FOR EACH ITERATION x = x + 1 # Default for i in range(10): # from Zero to Number - 1 print(i) 0 1 2 3 4 5 6 7 8 9 for i in range(5,10): #from Start to End - 1 print(i) 5 6 7 8 9 for i in range(50,5, -7): # from Start to End - 1 with Step increment print(i) 50 43 36 29 22 15 8 keyboard_arrow_down Use Case 1: Calculate Student's letter grades Let's combine conditional statements and for loop to calculate the letter grades for students. # Let's create a new list without any values in it grades= [] for score in scores: grades.append(score) print(grades) [89, 65, 76, 94, 81] # Let's create a new list without any values in it grades= [] for score in scores: if score >= 90: grade='A' elif score >= 80: grade='B' elif score >= 70: grade='C' else: grade='F' grades.append(grade) print(scores) print(grades) [89, 65, 76, 94, 81] ['B', 'F', 'C', 'A', 'B'] grades.append(45.32) grades ['B', 'F', 'C', 'A', 'B', 45.32] for v in grades: print(type(v)) keyboard_arrow_down 2.2.1.3 while certain condition is true, do something; while loop with condition loop For exmaple running something until certain time is reached or a particular criteria is achieved. while loop syntax WHILE **CONDITION**: DO SOMETHING... DON't FORGET TO ALTER VARIABLES IN CONDITION flag=True while flag: print("test") flag=False test keyboard_arrow_down Use Case 2: Guess a Number Let's play a game! Decide upon a number and ask user to guess that number. # This is the number I have decided number = 78 guess = 0 attempts = 0 # Equals == # Not Equals != while number != guess: print ('Guess the number (between 1 and 100') guess = int(input()) if guess < number: print('try higher') elif guess > number: print ('try lower') attempts += 1 print ('well done. Attempts:',attempts) Guess the number (between 1 and 100 try higher Guess the number (between 1 and 100 try higher Guess the number (between 1 and 100 try lower Guess the number (between 1 and 100 well done. Attempts: 4 keyboard_arrow_down End of session 2 %%html body{font-family: Fira ;} University of New Haven TCOE: ECECS 6602: Python Programming for Data Science Fall 2024 Class 3 : Functions keyboard_arrow_down Functions Repeatable code with passable parameters and return values; def function_name (): code to repeat # function "defintion" : write once part def intro () : print("Hi, My name is Joseph Smith and I am from New Haven, Connecticut!!") print("-"*30) # function "call" : Use mutliple times part intro() intro() intro() intro() intro() intro() Hi, My name is Joseph Smith and I am from New Haven, Connecticut!! ------------------------------ Hi, My name is Joseph Smith and I am from New Haven, Connecticut!! ------------------------------ Hi, My name is Joseph Smith and I am from New Haven, Connecticut!! ------------------------------ Hi, My name is Joseph Smith and I am from New Haven, Connecticut!! ------------------------------ Hi, My name is Joseph Smith and I am from New Haven, Connecticut!! ------------------------------ Hi, My name is Joseph Smith and I am from New Haven, Connecticut!! ------------------------------ keyboard_arrow_down Functions with Parameters def intro_to_person (person): print("Hi "+person+", My name is Joseph Smith and I am from Connecticut!!") # Call intro_to_person('Bob') intro_to_person('Tom') #Try this without passing a value intro_to_person () Hi Bob, My name is Joseph Smith and I am from Connecticut!! Hi Tom, My name is Joseph Smith and I am from Connecticut!! --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 9 6 intro_to_person('Tom') 8 #Try this without passing a value ----> 9 intro_to_person () TypeError: intro_to_person() missing 1 required positional argument: 'person' keyboard_arrow_down Functions with Parameters and Defaults If no name provided, use "Friend" def intro_to_person_with_default (person='Friend'): print("Hi "+person+", My name is Joseph Smith and I am from Connecticut!!") intro_to_person_with_default("Roy") intro_to_person_with_default() Hi Roy, My name is Joseph Smith and I am from Connecticut!! Hi Friend, My name is Joseph Smith and I am from Connecticut!! # C = 5/9 * ( F - 32) # Bill_amt def calculate_total ( bill_amt ): total_amt = bill_amt + 0.16 * bill_amt return total_amt Amt_to_CC= calculate_total(78.0) print(calculate_total(78.0)) print(calculate_total(128.0)) 92.03999999999999 151.04 print("How much was the bill?") amt = float(input()) #assumed to be str # have to convert to float total = calculate_total( amt) print("Total amount", total) How much was the bill? Total amount 52.568999999999996 # Bill_amt def calculate_total_service ( bill_amt, service='okay'): if service == 'good': tip = 0.2 elif service == 'okay': tip = 0.15 else: tip = 0.10 total_amt = bill_amt + tip * bill_amt return total_amt print("How much was the bill?") amt = float(input()) #assumed to be str # have to convert to float print("How was the service ? ('good', 'okay', 'bad'): ") service = input() #assumed to be str # have to convert to float total = calculate_total_service( amt, service ) print("Total amount", total) How much was the bill? How was the service ? ('good', 'okay', 'bad'): Total amount 52.8 keyboard_arrow_down Positional vs named arguments print("How was the service ? ('good', 'okay', 'bad'): ") serv = input() #assumed to be str # have to convert to float print("How much was the bill?") amt = float(input()) #assumed to be str # have to convert to float total = calculate_total_service( service=serv, bill_amt=amt ) print("Total amount", total) How was the service ? ('good', 'okay', 'bad'): How much was the bill? Total amount 28.14 # Not a good idea :( total = calculate_total_service( serv, amt ) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 2 1 # Not a good idea :( ----> 2 total = calculate_total_service( serv, amt ) Cell In, line 9, in calculate_total_service(bill_amt, service) 7 else: 8 tip = 0.10 ----> 9 total_amt = bill_amt + tip * bill_amt 10 return total_amt TypeError: can't multiply sequence by non-int of type 'float' print( calculate_total_service( bill_amt=amt ) ) 26.967499999999998 print( calculate_total_service( bill_amt=123 ) ) print( calculate_total_service( bill_amt=45 ) ) 141.45 51.75 amts = [123,45,67,87, 45,67,85,128, 289,677] for amt in amts: print( calculate_total_service( bill_amt=amt ) ) 141.45 51.75 77.05 100.05 51.75 77.05 97.75 147.2 332.35 778.55 # Write functions : - Converting temperature from F to C and C to F - one call to demo each function keyboard_arrow_down End of session 3 %%html body{font-family: Fira ;} University of New Haven TCOE: ECECS 6602: Python Programming for Data Science Fall 2024 keyboard_arrow_down Class 4 : Strings and Text processing keyboard_arrow_down String types a = 'This is single quoted string' b = "This is double quoted string" print (a) print (b) This is single quoted string This is double quoted string keyboard_arrow_down Quotes don't matter as long as they are same c = "This is mixed quoted string' print(c) Cell In, line 1 b = "This is double quoted string' ^ SyntaxError: EOL while scanning string literal keyboard_arrow_down Formatted String Use 3 double quotes to keep formatting para_if = """ If you can dream—and not make dreams your master; If you can think—and not make thoughts your aim; If you can meet with Triumph and Disaster And treat those two impostors just the same; If you can bear to hear the truth you’ve spoken Twisted by knaves to make a trap for fools, Or watch the things you gave your life to, broken, And stoop and build ’em up with worn-out tools: """ print(para_if) If you can dream—and not make dreams your master; If you can think—and not make thoughts your aim; If you can meet with Triumph and Disaster And treat those two impostors just the same; If you can bear to hear the truth you’ve spoken Twisted by knaves to make a trap for fools, Or watch the things you gave your life to, broken, And stoop and build ’em up with worn-out tools: para_a = """ x = 45 y = 23 print(x//y) """ print(para_a) x = 45 y = 23 print(x//y) Often misused to comment code for multiple commenting keyboard_arrow_down String Operations a + b Concatenation (Join two or more strings) a * b repeats string a b number of times if a in b: If a included in b for a in b: for each character in string b, repeat processing. print( type("Triumph" in para_if)) if "Triumph" in para_if: print("found it") else: print("Not there") found it for letter in para_if: print(letter) I f y o u c a n d r e a m — a n d n o t m a k e d r e a m s y o u r m a s t e r ; keyboard_arrow_down String SLICING 🍕 Slicing is used to find parts of string (sub-string) SLICE of strings Slicing syntax Square brackets and comma [ : ] Starting position : ending position (not included) a = "My phone number is 555-333-4444" a[9:15] 'number' a = "My phone number is 555-333-4444" a[19:31] '555-333-4444' keyboard_arrow_down If positions not specified, start = zero and end = entire length a = "My phone number is 555-333-4444" print ( "Without Start ") print ( a[:10] ) print ( "Without End ") print ( a[3:] ) Without Start My phone n Without End phone number is 555-333-4444 keyboard_arrow_down Negative Indexing Start with end of the string and go backwards # Pick up the last 12 characters print ( "With -12 for start: ") print ( a[-12:] ) # Pick entire number print ( a[-12:-9] ) With -12 for start: 555-333-4444 555 keyboard_arrow_down Slice with step [ start : end : step] # Pick up the last 12 characters print ( "With negative step 15 to 5 : ") print ( a[5:22:2] ) With negative step 15 to 5 : oenme s55 # Pick up the last 12 characters print ( "With negative step 15 to 5 : ") print ( a[20:2:-1] ) With negative step 15 to 5 : 55 si rebmun enohp print ( a[::-1] ) 4444-333-555 si rebmun enohp yM keyboard_arrow_down String Modifications upper() lower () strip() replace() split() : Tokenizes String by character/string print(address) 100 main street, west haven, CT address.upper() ' 100 MAIN STREET, WEST HAVEN, CT ' address.lower() ' 100 main street, west haven, ct ' address = " 100 main street, west haven, CT " address.strip() '100 main street, west haven, CT' address = " 100 main street, west haven, CT " print( address.replace ("main", "west main")) address=address.replace ("main", "west main") print(address) 100 west main street, west haven, CT 100 main street, west haven, CT address.split(",") [' 100 west main street', ' west haven', ' CT '] keyboard_arrow_down Split generates a list type(address.split(",")) list len (address.split(",")) 3 len (para_if.split(" ")) 126 for word in address.split(","): print (word[::-1]) teerts niam 001 nevah tsew TC keyboard_arrow_down Formatting Strings name = "Joseph Smith" score = 45 report = "Hi " + name + ", This is your score : " print(report) Hi Joseph Smith, This is your score : name = "Joseph Smith" score = 45 report = "Hi " + name + ", This is your score : "+score # CAUSES ERROR print(report) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 3 1 name = "Joseph Smith" 2 score = 45 ----> 3 report = "Hi " + name + ", This is your score : "+score # CAUSES ERROR 4 print(report) TypeError: can only concatenate str (not "int") to str # Option 1 : Convert to String name = "Joseph Smith" score = 45 report = "Hi " + name + ", This is your score : "+str(score) # Works print(report) Hi Joseph Smith, This is your score : 45 # Option 2 : Use format () function with string placeholders name = "Joseph Smith" score = 45 report = "Hi {}, This is your score : {}" # FORMATTED STRING print(report.format(name,score)) print(report) Hi Joseph Smith, This is your score : 45 Hi {}, This is your score : {} # Option 3 : Use f prefix to replace variables within string name = "Joseph Smith" score = 45 report = f"Hi {name}, This is your score : {score}" # FORMATTED STRING print(report) Hi Joseph Smith, This is your score : 45 keyboard_arrow_down Format function `string.format(var=value, var=value) txt = "For only {price:.4f} dollars!" print(txt.format(price = 49.78877)) For only 49.7888 dollars! Placeholders and positional replacements txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36) print(txt1) txt2 = "My name is ({2} {1}, I'm {0}".format("John",36,45) print(txt2) txt3 = "My name is {}, I'm {}".format("John",36) print(txt3) My name is John, I'm 36 My name is John, I'm 36 My name is John, I'm 36 keyboard_arrow_down Formatting Types :b :e :o :x :% txt = "For only {price:b} dollars in Binary!" # Binary print(txt.format(price = 49)) txt = "For only {price:o} dollars in Octal!" # Binary print(txt.format(price = 49)) txt = "For only {price:x} dollars in Hex!" # Binary print(txt.format(price = 49)) txt = "For only {price:.2%} dollars in percentage!" # Binary print(txt.format(price = 0.23)) For only 110001 dollars in Binary! For only 61 dollars in Octal! For only 31 dollars in Hex! For only 23.00% dollars in percentage! keyboard_arrow_down Escape Characters in Strings \' Single Quote \ Backslash \n New Line \r Carriage Return \t Tab \b Backspace \f Form Feed \ooo Octal value \xhh Hex value speech = "Joseph O'Connor, my quote is as follows "Be Good. Do Good"" speech = 'Joseph O'Connor, my quote is as follows "Be Good. Do Good"' print(speech) Cell In, line 1 speech = "Joseph O'Connor, my quote is as follows "Be Good. Do Good"" ^ SyntaxError: invalid syntax speech = "Joseph O'Connor, my quote is as follows \"Be Good. Do Good\"" print(speech) speech = 'Joseph O\'Connor, my quote is as follows "Be Good. Do Good"' print(speech) Joseph O'Connor, my quote is as follows "Be Good. Do Good" Joseph O'Connor, my quote is as follows "Be Good. Do Good" keyboard_arrow_down Other String Methods capitalize() Converts the first character to upper case casefold() Converts string into lower case center() Returns a centered string count() Returns the number of times a specified value occurs in a string encode() Returns an encoded version of the string endswith() Returns true if the string ends with the specified value expandtabs() Sets the tab size of the string find() Searches the string for a specified value and returns the position of where it was found format() Formats specified values in a string format_map() Formats specified values in a string index() Searches the string for a specified value and returns the position of where it was found isalnum() Returns True if all characters in the string are alphanumeric isalpha() Returns True if all characters in the string are in the alphabet isascii() Returns True if all characters in the string are ascii characters isdecimal() Returns True if all characters in the string are decimals isdigit() Returns True if all characters in the string are digits isidentifier() Returns True if the string is an identifier islower() Returns True if all characters in the string are lower case isnumeric() Returns True if all characters in the string are numeric isprintable() Returns True if all characters in the string are printable isspace() Returns True if all characters in the string are whitespaces istitle() Returns True if the string follows the rules of a title isupper() Returns True if all characters in the string are upper case join() Joins the elements of an iterable to the end of the string ljust() Returns a left justified version of the string lower() Converts a string into lower case lstrip() Returns a left trim version of the string maketrans() Returns a translation table to be used in translations partition() Returns a tuple where the string is parted into three parts replace() Returns a string where a specified value is replaced with a specified value rfind() Searches the string for a specified value and returns the last position of where it was found rindex() Searches the string for a specified value and returns the last position of where it was found rjust() Returns a right justified version of the string rpartition() Returns a tuple where the string is parted into three parts rsplit() Splits the string at the specified separator, and returns a list rstrip() Returns a right trim version of the string split() Splits the string at the specified separator, and returns a list splitlines() Splits the string at line breaks and returns a list startswith() Returns true if the string starts with the specified value strip() Returns a trimmed version of the string swapcase() Swaps cases, lower case becomes upper case and vice versa title() Converts the first character of each word to upper case translate() Returns a translated string upper() Converts a string into upper case zfill() Fills the string with a specified number of 0 values at the beginning para_if.count('If') 4 import requests r = requests.get('https://peps.python.org/pep-0020/#the-zen-of-python') print(r.text) PEP 20 – The Zen of Python | peps.python.org 13 location=0 TypeError: 'tuple' object does not support item assignment keyboard_arrow_down Set # Define a Set products = { 'laptops', 'accessories', 'servers', 'peripherals', 'services' } #Type print(type(products)) # Can you index it? print(products) {'peripherals', 'laptops', 'services', 'accessories', 'servers'} # Can you index it? #print(products) # This throws error "TypeError: 'set' object is not subscriptable" new_products = { 'laptop', 'accessories', 'server', 'peripherals', 'service','laptop', 'serv print(new_products) print(len(new_products)) {'server', 'service', 'peripherals', 'laptop', 'accessories'} 5 keyboard_arrow_down Dictionary # Define a Dictionary student = { "name": "Joe Smith", # single key : value pair "student_number": 123456, "gpa": 3.78, "address": "100 main st, West Haven, CT 06666" } #Type print(type(student)) # Can you index it? #print(student) # Can not access by index Error: keyerror: 1 print(student["address"]) # Can only access by "key" # Can you slice it? #print(student [2:4]) # No can't do : Erorr "TypeError: unhashable type: 'slice'" 100 main st, West Haven, CT 06666 #Can you add duplicate clever_student = { "name": "Joe Smith", "student_number": 123456, "gpa": 3.78, "address": "100 main st, West Haven, CT 06666", "gpa" : 3.99 } print(clever_student) {'name': 'Joe Smith', 'student_number': 123456, 'gpa': 3.99, 'address': '100 main st, We clever_student["test"]='blah' clever_student["gpa"]=4.22 print(clever_student) {'name': 'Joe Smith', 'student_number': 123456, 'gpa': 4.22, 'address': '100 main st, We keyboard_arrow_down List: More about List Characteristics: Indexed: Every member of List has a place or index starting with 0. Ordered: members of the List can be accessed in sequence of Index. Mutable: You can change or update values in-place. Can hold Heterogenous Data : Not all members have to be of same data type NOT Exactly like arrays but can be used like one : keyboard_arrow_down Add or update data in list #Create a new list with brackets students= [] # Add Data into List students.append('Cool Cat') print(students) students.append('Bob Cat') print(students) students.append('Tom Cat') print(students) students.append('Ray Cat') print(students) students.append('Smart Cat') print(students) ['Cool Cat'] ['Cool Cat', 'Bob Cat'] ['Cool Cat', 'Bob Cat', 'Tom Cat'] ['Cool Cat', 'Bob Cat', 'Tom Cat', 'Ray Cat'] ['Cool Cat', 'Bob Cat', 'Tom Cat', 'Ray Cat', 'Smart Cat'] keyboard_arrow_down Length of the list # Length of List print( " Length ") print(30 * "-") print(len(students)) Length ------------------------------ 5 keyboard_arrow_down Accessing all the items in list # Loop through print( " Loop through ") print(30 * "-") for i in students: print(i) Loop through ------------------------------ Cool Cat Bob Cat Tom Cat Ray Cat Smart Cat keyboard_arrow_down Accessing individual or selected items from a list # Access by index print( " Access by index : Find at index 3 ") print(30 * "-") print(students) Access by index : Find at index 3 ------------------------------ Ray Cat keyboard_arrow_down SLICING 🍕 works with LIST too Slicing is used to find parts of list (sub-list) Slicing syntax Square brackets and comma [ : ] Starting position : ending position (not included) Starting index : ending index (not included) few_students = students [2:4] # 0 1 2 3 4 5 print(few_students) ['Tom Cat', 'Ray Cat'] print (students[-1:]) print(students[4:]) print(students[:2]) # [ start : end : step ] ['Smart Cat'] ['Smart Cat'] ['Cool Cat', 'Bob Cat'] keyboard_arrow_down List are Mutable (Fancy way of saying they can change ) grades = [87,67,77,98] print(grades) [87, 67, 77, 98] print(grades) # Add two points to grades grades += 2 print(grades) print(students) students= "Super Cat" print(students) [87, 67, 77, 98] [87, 69, 77, 98] ['Cool Cat', 'Bob Cat', 'Tom Cat', 'Ray Cat', 'Smart Cat'] ['Cool Cat', 'Super Cat', 'Tom Cat', 'Ray Cat', 'Smart Cat'] keyboard_arrow_down Operations with List + Joins two lists * Adds same elements n number of times new_students = ["Leo Lion", "Peter Panther", "Chuck Cheetah"] print (students + new_students) print (students ) students += new_students # students = students + new_students print(students) ['Cool Cat', 'Super Cat', 'Tom Cat', 'Ray Cat', 'Smart Cat', 'Leo Lion', 'Peter Panther' ['Cool Cat', 'Super Cat', 'Tom Cat', 'Ray Cat', 'Smart Cat'] ['Cool Cat', 'Super Cat', 'Tom Cat', 'Ray Cat', 'Smart Cat', 'Leo Lion', 'Peter Panther' print("-"*30) print(grades) print(grades*2) print(grades*3) ------------------------------ [87, 69, 77, 98] [87, 69, 77, 98, 87, 69, 77, 98] [87, 69, 77, 98, 87, 69, 77, 98, 87, 69, 77, 98] keyboard_arrow_down Methods availbale with List -.sort () # sorts the list in alphabetical or numeric order -.reverse() # reverse sort order of items -.join(), extend() # joins two or more lists print(students) Bob Cat print(students) # Alpha-numeric sort students.sort() # In-place operation print(students) # Numeric sort grades.sort() print(grades) print(students) ['Cool Cat', 'Super Cat', 'Tom Cat', 'Ray Cat', 'Smart Cat', 'Leo Lion', 'Peter Panther' ['Chuck Cheetah', 'Cool Cat', 'Leo Lion', 'Peter Panther', 'Ray Cat', 'Smart Cat', 'Supe [69, 77, 87, 98] Cool Cat students.sort(reverse=True) # In-place operation print(students) print(students) ['Tom Cat', 'Super Cat', 'Smart Cat', 'Ray Cat', 'Peter Panther', 'Leo Lion', 'Cool Cat' Super Cat students.append('Chuck Cheetah') students.append('Chuck Cheetah') students.append('Chuck Cheetah') print (students.index('Chuck Cheetah')) 7 print (students.count('Chuck Cheetah')) students.append('Chuck Cheetah') students.append('Chuck Cheetah') print (students.count('Chuck Cheetah')) students.remove("Chuck Cheetah") print (students.count('Chuck Cheetah')) 4 6 5 students print (students) students="I am changing this" # replaces the value print (students) students.insert (3, "New cat") # Pushes all other elemnts and makes room for new one print (students) ['Tom Cat', 'Smart Cat', 'Ray Cat', 'I am changing this', 'Leo Lion', 'Cool Cat', 'Chuck ['Tom Cat', 'Smart Cat', 'Ray Cat', 'I am changing this', 'Leo Lion', 'Cool Cat', 'Chuck ['Tom Cat', 'Smart Cat', 'Ray Cat', 'New cat', 'I am changing this', 'Leo Lion', 'Cool C keyboard_arrow_down List Methods append() Adds an element at the end of the list clear() Removes all the elements from the list copy() Returns a copy of the list count() Returns the number of elements with the specified value extend() Add the elements of a list (or any iterable), to the end of the current list index() Returns the index of the first element with the specified value insert() Adds an element at the specified position pop() Removes the element at the specified position remove() Removes the item with the specified value reverse() Reverses the order of the list sort() Sorts the list keyboard_arrow_down Representation of complex data structures students = [] student1 = { "name":"joe", "gpa":3.4, "address":"100 main...", "classes": [ 'Coding 101', "Data Sci 101"] } student2 = { "name":"joe", "gpa":3.4, "address":"100 main...", "classes": [ 'Coding 101', "Data Sci 101", "Poli Sci 101"] } student3 = { "name":"joe", "gpa":3.4, "address":"100 main...", "classes": [ 'Coding 101', "Data Sci 101", "Poli Sci 101", "test"] } students.append(student1) students.append(student2) students.append(student3) print(students) print(students['classes']) print(students["classes"][-1:] ) #students["classes"][-1:]="42" print(students) [{'name': 'joe', 'gpa': 3.4, 'address': '100 main...', 'classes': ['Coding 101', 'Data S Coding 101 --------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In, line 28 24 print(students['classes']) 26 #print(students["classes"][-1:] ) ---> 28 students["classes"][-1:]="42" 29 print(students) KeyError: 'classes' keyboard_arrow_down Nested Lists List of lists semester_1=['6601', '6602', '6604'] semester_2=['6608', '6612', '6614'] semester_3=['6621', '6622', '6608'] semester_4=['6641', '6645', '6698'] students_courses= [semester_1,semester_2, semester_3,semester_4] # Defining it as single variable Plan=[ ['6601', '6602', '6604'], ['6608', '6612', '6614'], ['6621', '6622', '6608'], ['6641', '6645', '6698']] t1=(2,3,4) t2=(4,5,6) l1=[t1,t2] #Tuple of lists l1=(7,8,9) # Can do this #l1=7 #can't do this t1=([1,2,3],[4,5,6]) # List of Tuple t1=9 print(t1) #t1=[7,8,9] # Can't do this ( ) ([1, 2, 3], [4, 9, 6]) l1=[1,2,3] l2=[4,5,6] t2=(5,5,5) s2={4,44,444} l3=[7,8,9] l1.extend(t2) # Allows appending ANY tpye of iterable; i.e. List, Tuple, Set etc print(l1) l1.extend(s2) # Allows appending ANY tpye of iterable; i.e. List, Tuple, Set etc print(l1) # l1=l1+s2 # Can't use + to add Non-list into list, you have to use extend print(l1) [1, 2, 3, 5, 5, 5] [1, 2, 3, 5, 5, 5, 444, 4, 44] [1, 2, 3, 5, 5, 5, 444, 4, 44] End of session 5 University of New Haven TCOE: ECECS 6602: Python Programming for Data Science Fall 2024 keyboard_arrow_down Class 7 : Tuples, Sets and Dictionaries List is a collection which is ordered and changeable. Allows duplicate members. Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members. Dictionary is a collection which is ordered** and changeable. No duplicate members. keyboard_arrow_down List [ , , ] Ordered Indexed Changeable Mutable Allow Duplicates Tuple ( , , ) Ordered Indexed Unchangeable Immutable Allow Duplicates Set { , , } Unordered NOT Indexed Changeable Mutable DO NOT ALLOW Duplicates Dictionary { key : value } { 'key':'value', 'key':'value', } Ordered Can be Indexed Changeable Mutable DO NOT ALLOW Duplicates Keys Data type validations of input in future assignments. Please use markdown to describe code or comments. Clean messages. Non valid values : don’t exit the code Simplicity of solution : (Don't use all features you know;) # Create an empty list students= [ ] # Print the type of the students print(type(students)) keyboard_arrow_down Tuple location2 = ( ) print(type(location2)) # Defined as tuple location = (40.7648, -73.9808) # Latitude, longitude point_1 = (1,3,5) # x,y,z axis intercepts #Type print(type(location)) #Indexed Access print(location) # print longitude location = (40.7648, -73.9808) # Latitude, longitude # Change longitude location=0 # Error : 'tuple' object does not support item assignment -73.9808 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 18 15 location = (40.7648, -73.9808) # Latitude, longitude 17 # Change longitude ---> 18 location=0 # Error : 'tuple' object does not support item assignment TypeError: 'tuple' object does not support item assignment t1=(1,2,3) t2=1,2,3 t3=1, print(type(t2)) print(t1) a,b,c = t1 print(a) print(b) (1, 2, 3) 1 2 lat, long= location print(lat) print(long) 40.7648 -73.9808 keyboard_arrow_down Tuples Packing and unpacking # Packing variables into a tuple x=45 y=22 t1=(x,y) # Packing it into a tuple print(t1) x=47 print(t1) (45, 22) (45, 22) # Unpacking a tuple : Extracting values into new variables (a, b) = t1 a,b = t1 #a=t1 #b=t1 print(a) print(b) t2=3,4,5 a,b = t2 print(b) 45 22 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In, line 10 6 print(b) 9 t2=3,4,5 ---> 10 a,b = t2 11 print(b) ValueError: too many values to unpack (expected 2) keyboard_arrow_down Unpacking with * Operator #a,b = (1,2,3,4,5) # This throws error "ValueError: too many values to unpack (expected 2)" #unpacking with * operator # *var at the end creates a new list (a,b,*c) = (1,2,3,4,5,6,7,8,9) print(a) print(b) print(c) print(type(c)) 1 2 [3, 4, 5, 6, 7, 8, 9] #unpacking with * operator # *var in the middle creates a new list for all middle values (a,*b,c,d) = (1,2,3,4,5,6,7,8,9) print(a) print(b) print(c) print(d) print(type(b)) Cell In, line 3 (a,*b,*c,d) = (1,2,3,4,5,6,7,8,9) ^ SyntaxError: multiple starred expressions in assignment keyboard_arrow_down Tuple Operators Adds two tuples Repeats values t1 = (1,2,3) t2 = (4,5,6) t3 = t1 + t2 print(t3) t4 = t1 * 3 print(t4) (1, 2, 3, 4, 5, 6) (1, 2, 3, 1, 2, 3, 1, 2, 3) keyboard_arrow_down Built-in functions len(tuple) gives the total length of the tuple. max(tuple) returns item from the tuple with max value. min(tuple) returns item from the tuple with min value. tuple(list) converts a list into tuple. t1 = (1,2,3,4,5) print(len(t1)) print(max(t1)) print(min(t1)) l1 = [1,2,3,4,5] t2 = tuple(l1) print(t2) print(type(t2)) 5 5 1 (1, 2, 3, 4, 5) keyboard_arrow_down Tuple Methods count() Returns the number of times a specified value occurs in a tuple index() Searches the tuple for a specified value and returns the position of where it was found t1 = (1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9) print(t1.count(1)) print(t1.index(9)) #Iterating through a tuple t1 = (1,2,3,4,5,6,7,8,9) for i in t1: print(i) 2 8 1 2 3 4 5 6 7 8 9 keyboard_arrow_down Tuple Slicing t1 = (1,2,3,4,5,6,7,8,9) print(t1[1:5]) print(t1[1:5:2]) print(t1[1:5:3]) print(t1[::2]) print(t1[::-1]) keyboard_arrow_down Conditions with Tuples #Membership test in tuple t1 = (1,2,3,4,5,6,7,8,9) print(1 in t1) print(10 in t1) print(1 not in t1) print(10 not in t1) keyboard_arrow_down Looping through Tuple t1 = (1,2,3,4,5,6,7,8,9) for i in t1: print(i) 1 2 3 4 5 6 7 8 9 keyboard_arrow_down Comparing Tuples (or other indexed structures) The comparison operators can be used with tuples and other sequences. Python starts by comparing the first element from each sequence. If they are equal, it compares the next element of each, and so on, until it finds elements that differ. #Comparing tuples t1 = (1,2,3,5) t2 = (1,2,3,5) t3 = (1,2,4,2) print(t1 == t2) print(t1 == t3) print(t1 > t2) print(t1 < t3) True False False True #Comparing tuples t1 = (1,2,3,5) t2 = (1,2,5) t3 = (1,2,4,2, 7) print(t1 == t2) print(t1 == t3) print(t1 > t2) print(t1 < t3) False False False True keyboard_arrow_down Set # Define a Set products = { 'laptops', 'accessories', 'servers', 'peripherals', 'services' } #Type print(type(products)) # Can you index it? print(products) # Can you index it? #print(products) # This throws error "TypeError: 'set' object is not subscriptable" new_products = { 'laptop', 'accessories', 'server', 'peripherals', 'service','laptop', 'server' } print(new_products) print(len(new_products)) {'laptops', 'accessories', 'peripherals', 'services', 'servers'} {'laptop', 'accessories', 'peripherals', 'service', 'server'} 5 Basics of set : Defining set set1 = {1,2,3,4,5,6,7,8,9} print(set1) print(type(set1)) keyboard_arrow_down Creating a set set1 = {1,2,3,4,5,6,7,8,9} print(set1) print(type(set1)) Creating a set using set() constructor set1 = set((1,2,3,4,5,6,7,8,9)) print(set1) print(type(set1)) Creating an empty set set1 = {} print(set1) print(type(set1)) set1 = set() print(set1) print(type(set1)) keyboard_arrow_down Adding elements into set Add() : Add one element Update () : One or more elements #Adding elements to a set set1 = {1,2,3,4,5,6,7,8,9} set1.add(10) print(set1) #Adding multiple elements to a set set1 = {1,2,3,4,5,6,7,8,9} set1.update([9,11,12,12]) print(set1) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12} keyboard_arrow_down Removing elements from set remove or disard a specific element pop : remove first element clear : delete all elements #Removing elements from a set set1 = {1,2,3,4,5,6,7,8,9} set1.remove(9) print(set1) set1 = {1,2,3,4,5,6,7,8,9} set1.discard(9) print(set1) set1 = {1,2,3,4,5,6,7,8,9} set1.pop() print(set1) set1 = {1,2,3,4,5,6,7,8,9} set1.clear() print(set1) {1, 2, 3, 4, 5, 6, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8} {2, 3, 4, 5, 6, 7, 8, 9} set() keyboard_arrow_down Set operations Mathematical operations of SET theory Union | Intesection & Difference - Symmetric Difference ^ set1 = {1,2,3,4,5,6,7,8,9} set2 = {5,6,7,8,9,10,11,12} # Union | print(set1.union(set2)) # Function print(set1 | set2 ) # Operator # intersection & print(set1.intersection(set2)) print(set1 & set2) # difference - print(set1.difference(set2)) print(set1 - set2) # symmetric_difference ^ print(set1.symmetric_difference(set2)) print(set1 ^ set2) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} {5, 6, 7, 8, 9} {5, 6, 7, 8, 9} {1, 2, 3, 4} {1, 2, 3, 4} {1, 2, 3, 4, 10, 11, 12} {1, 2, 3, 4, 10, 11, 12} keyboard_arrow_down Other set things len max min Remember + and * are not supported in set; set1 = {1,2,3,4,5,6,7,8,9} set2 = {5,6,7,8,9,10,11,12} # Len print(len (set1)) # len # min print(min (set1)) # max print(max (set1)) 9 1 9 s1={"test", 4.5, 4 , [1,2,3]} print(s1) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In, line 1 ----> 1 s1={"test", 4.5, 4 , [1,2,3]} 3 print(s1) TypeError: unhashable type: 'list' keyboard_arrow_down Dictionary d1= { "name":"Joe Smith", "age":22, "gpa":4.1, "address":{ "city":"west haven", "zip": 66616 }, "courses":[ 'a','b']} print(d1) print(type(d1)) d1 ["address"]["city"]="New Haven" d1["address"]["street"]="100 Main st" print(d1) {'name': 'Joe Smith', 'age': 22, 'gpa': 4.1, 'address': {'city': 'west haven', 'zip': 66616}, 'courses': ['a', 'b']} {'name': 'Joe Smith', 'age': 22, 'gpa': 4.1, 'address': {'city': 'New Haven', 'zip': 66616, 'street': '100 Main st'}, 'courses': ['a', ' print(d1.keys()) print(type(d1.keys())) print(d1.values()) print(type(d1.values())) ## Find Keys for k in d1.keys(): print (k) dict_keys(['name', 'age', 'gpa', 'address', 'courses']) dict_values(['Joe Smith', 22, 4.1, {'city': 'New Haven', 'zip': 66616, 'street': '100 Main st'}, ['a', 'b']]) name age gpa address courses ## Find values for v in d1.values(): print (v) Joe Smith 22 4.1 {'city': 'New Haven', 'zip': 66616, 'street': '100 Main st'} ['a', 'b'] print(d1.items()) print(type(d1.items())) '@print' is not an allowed annotation – allowed values include [@param, @title, @markdown]. edit ## Find keys and values #for t in d1.items(): # print(t) #@print (f"key {k} : = value {v}" ) ## Find keys and values for k, v in d1.items(): print (f"key {k} : = value {v}" ) d1.items() dict_items([('name', 'Joe Smith'), ('age', 22), ('gpa', 4.1), ('address', {'city': 'New Haven', 'zip': 66616, 'street': '100 Main st'}), key name : = value Joe Smith key age : = value 22 key gpa : = value 4.1 key address : = value {'city': 'New Haven', 'zip': 66616, 'street': '100 Main st'} key courses : = value ['a', 'b'] dict_items([('name', 'Joe Smith'), ('age', 22), ('gpa', 4.1), ('address', {'city': 'New Haven', 'zip': 66616, 'street': '100 Main st'}), ('courses', ['a', 'b'])]) Start coding or generate with AI. d1= { "name":"Joe Smith", "age":22, "gpa":4.1} d1["test_score"]=98 print(d1) d1["test_score"]=99 print(d1) {'name': 'Joe Smith', 'age': 22, 'gpa': 4.1, 'test_score': 98} {'name': 'Joe Smith', 'age': 22, 'gpa': 4.1, 'test_score': 99} keyboard_arrow_down End of session 7 %%HTML body {--vscode-font-family: "Linux Liberation"}