 Hello and welcome to our Introduction to Python Workshop from RIT Libraries. My name is Rebecca Walker. I am the Digital Humanities and Social Sciences Librarian here at RIT, which means I work with students, faculty, and staff across campus on digital projects, uses of technology, and implementing digital methods. Jacob Pacheron, an undergraduate Digital Humanities and Social Sciences student, helped design today's workshop and recorded the exercises for today. Today we're going to talk about Python, a powerful, flexible, and readable programming language. All of the materials you'll need for this workshop are available in the resources folder shared via email and available on the RIT Libraries website. First of all, thank you all for being flexible as we change the format of this workshop to be video only. We originally designed the workshop to be interactive with hands-on activities in a small learning environment, so we appreciate your patience as we pivoted to try and make it as interactive as possible in this new format. The workshop still includes activities with files located in the Exercise Files folder. The exercises are also recorded and are included in this same recording after this introduction and background of Python. I am always available for questions or meetings regarding the use of Python, and there are resources available for reference or to help you start learning more on your own. You can access all workshop materials at the address here. This links to a Google Drive folder that contains resources, exercise files, and the slides. This link is persistent and will also be available on the RIT Libraries website. This workshop is intended to be a basic introduction to Python. We don't have time to get into all the capabilities of the program today, but we'll go over some of its functionality and examples of how it can be used. So, what is Python? I can say that Python is an interpreted, high-level general-purpose language, but what does that mean exactly? Interpreted means that it executes directions directly and does not need to be interpreted into machine language instructions in order for it to run. High-level means that it is very readable and is close to human language. And general purpose means that it is designed for a variety of domains and is not specific to any type of output. Python was originally released in 1991. It works with multiple operating systems, Windows, Mac, and Linux. It emphasizes readability and uses white space to build an intuitive structure in the code. It is a procedural, object-oriented, and functional language. And also for fun trivia, it was named after Monty Python's flying circus. I've already mentioned a few times how flexible and useful Python is, but what specifically can it be used for? As a general-purpose language, Python is not constrained to certain activities or outputs. You can use Python to build web applications, perform text analysis, or clean structured or unstructured data. Python is also commonly used in artificial intelligence projects and for natural language processing. Python is also very useful for automating repetitive tasks, such as shortening filenames downloaded from my courses or automatically creating a table in XML. Check out Python resources to learn more about how it can support your specific needs and what libraries might be imported to build out functionality you want. Python is available to download for free. You can download it directly from python.org or it is available through RIT, which means you can install it on work machines that might otherwise require administrative rights to install software. If you are using an RIT computer, I recommend using the RIT version, though both will work the same for this workshop and your personal ongoing use. A universal way to get Python is to download it directly from the website python.org. When I navigate to that website in my browser and click downloads, I see this screen here. Since I am on a Windows machine, the site shows download the latest version for Windows with the yellow download button directly underneath with the latest version, 3.8.2. If you are on a Mac, your screen should indicate the difference. To download from python.org, hit the yellow download button and go through the simple process. This should take a few minutes at most. To download Python onto a machine managed by RIT, you can use RIT Software Center. Follow instructions specific to your computer type, Windows or Mac. On a Windows machine, you can open the Windows menu and start typing Software Center, and it will pop up. Inside the window, search for Python. Choose the version with the highest addition number, in this case 3.8.2, and install to your machine. All Python installs come with idle, a text editor and shell, and a GUI or graphical user interface. You can use idle to write and execute Python files immediately, with no setup or connections required. We'll be using idle for today's exercises. Let's open idle and start exploring. When I open the window, I see my version of Python printed at the top. With some additional commands, you can use to get more information or help. The three arrows and blinking cursor indicate the shell is ready for input. The shell is useful for executing immediate tasks, but to create reusable files and save your work, you can open and save files inside idle. These files will have the file extension .py for Python. We'll go over some of the basics of Python, but before we get too far in, remember to always check the documentation. Python is very well documented and established, and has very good documentation available for any question you might have. One of the fundamentals of any programming language is operators. Operators are symbols that tell the computer to perform a specific action. These actions can be simple like an addition problem or more complex like combining function inputs. We see some common operators on the slide here, and the resources packet has a longer list with examples. Python follows order of operations for all inputs, so remember to follow PEMDAS, parentheses, exponents, multiplication, division, addition, and subtraction. Within idle, we see that Python is already ready to execute mathematical operations. So if I do seven plus eight and enter, I get the output of 15. If I do seven plus eight and supply by two, I get 30. Instead of 23 without the parentheses. So that's just one example of some of the operators. Some of the less known operators are useful for different things, including to the power of greater than less than or are not equal or are equal to. Understanding data types is also integral to many coding and computing languages, and Python is no exception. Individual data types can be used differently and tie into commands we will use in Python. The table here shows some most common data types, and the resources packet contains even more types. Strings are any series of characters bound by quotation marks. They can be single, double, or triple. String values can include spaces, symbols, numbers, and letters. Integers are positive or negative whole numbers, while floating point numbers or floats are integers that contain fractional components. Boolean values come with one of two built-in values of true or false, and Python these values are always capitalized unlike in some other languages. Lists are ordered collection of data items contained in square brackets. Python has a built-in type function that will return information on the type of your input. So to call a function in Python, we type out the name of the function and then an opening and a closing parentheses. So if I do type of the number eight, I get back a class of int or integer. If I do a type of my name in quotation marks, I get back a class of str or string. Please note that you do need to enclose a string in quotations even when you're checking for its type. Variables are another building block of Python and most coding languages. Variables are essentially data containers. They associate a name with a specific value and they store that value for future use. Unlike some other programming languages, you don't need to declare variables before use in Python. You can just define them. We see that here with these examples. The first variable is message with the input hello world. The second example shows two variables being named place one and place two with inputs of Rochester, New York and Rochester, Minnesota. When defining a variable, note you need to follow the rules for data types. So variables that are strings need to be surrounded by quotation marks but integer variables do not. There are some rules for naming variables. Variable names cannot start with a number. They are case sensitive. They cannot contain spaces and they cannot be defined keywords. Keywords are words Python reserves for specific actions like else, true and class. If you use a keyword on accident, you will get an error from Python. Variables store the most recent information so you can update them easily by rewriting the variable name and providing a new value. We see that here with the second example when I renamed dessert from chocolate mousse to beat ice cream and then I printed the same print statement but it updated for me. Like in all coding languages, comments are useful to organize and share information about what you're doing. They're very useful as headings or as a way to communicate with yourself or others working on the same file. You can create a comment in Python by typing the pound sign and then whatever text you like. IDLE automatically turns comments into red text. Comments can be useful to mark out code you're not sure about using or are still working on. This keeps the text there but means the program will not run it when you run the file. We used operators with integer values already but you can also use them with strings. We see three common examples of string operators here. The first is concatenate when you essentially join strings together with the plus sign. The second is to multiply which multiplies copies of a string and the third is to append. You can add material to the end of a string by using a specific character a plus sign with an equal sign after it. Python ignores most white space in your coding which is great for readability. You are encouraged to add white space whenever it will increase the readability of your code. Python does use white space to interpret the structure of a document and intuit which parts of code depend on others. In the example here we see that the three indented print statements are defined by their higher level statements of if, lf, and else. This tells Python to check the highest level first and execute the appropriate print function based on the input value. IDLE automatically indents when a definition statement is required like in the example here. We'll see that when we define functions shortly. Functions are another building block of Python. A function is a block of code given a name and used to carry out a specific task. These allow you to save time and reuse code with new inputs also known as arguments. Python comes with a large set of built-in functions and you can define your own to write tasks specific to your needs. Built-in functions come preset with Python. These can be called upon at any time in your code. Some common functions include print, len, sum, and type. Common to all these functions is syntax. You have the function name and the input in the parentheses. The examples on this slide show three of these functions in action and we'll use several of them in the exercises. You can view all built-in functions and Python's documentation at the link here. Note that when you install or import other libraries in Python, those come with their own built-in functions. Defining your own functions in Python is simple and flexible, similar to how we define variables. Defining functions can help you save time and energy by reusing your code. Define your own function with def, assigning a name, and writing out what happens within the function. In the example here, taken from a programming historian tutorial, we see def to let Python know we're creating a function, greet entity as the name of the function and x within parentheses as a parameter. We'll call the x again in our body. So what is happening in this function? We see the built-in function print being called with a string hello and the x printed again. This indicates that when we call this function, it will print the word hello and whatever argument we put in as our x. On the lines below, we see the function being called by writing out its name and two strings, everybody and programming historian as our arguments or inputs. Here we see the output of our function. We see that the output is two printed statements, hello everybody and hello programming historian. This is a relatively simple example of a function, but hopefully it helps you understand how they can be used. Methods in Python are similar to functions in that both take an input and transform it based on a specific command. Methods are different from functions in that they must be associated with objects and are often limited by type. So some only work on strings, some on lists, etc. Both functions and methods transform their input, but we can see the difference in syntax and potential use through these examples. Here I set a variable named thing1 which contains the string, this is a string with spaces. I ran a length function on the variable which gave me an output of 28. Idle changes the function text to purple and we see the input value inside parentheses after the function's name. In the method example on the right, we see we are also calling our input the variable name, but it's before the specific method we're using. In this case, split. The split method takes a delimiter, in this case a space, and breaks up our input by that delimiter. We see our output is a list of words transformed from our original variable. This is just one example, but hopefully it helps you to see how methods and functions look and operate differently. Methods depend on specific objects and are often set by data type. String methods can be used only on string inputs and are used to manipulate text content. The example on our slide shows a small-scale version of this. We are manipulating our string variable, thing1, using four string methods, upper, which prints the string in all uppercase letters, lower, which does the opposite, printing the string in lowercase letters, split, which splits the string into a list by a set delimiter, in this case a space, and replace, which replaces characters in a string and requires the input of which characters we are replacing first, and after a comma, which characters we are replacing them with. You can view all string methods at the link here. Python also has specific methods to manipulate items in lists. You can order lists, add or remove items, count the number of times that something appears and the like. In the example here, we see how I added an item to my list, then removed a different item, and finally counted the number of times a certain element appears. Note that in the count method, you need to follow data type rules, so if you are searching for instances of a string, the input searching for the string needs to be in quotation marks. You can view more list methods at the link here. Conditional expressions are another central feature of many programming languages, Python included. At their core, conditional expressions, or if else statements, check the validity of an input and run sequentially until they find a match. In practice, this can be as simple as what we see in this graphic. If our two input values, A and B match, it will print yes, but if they don't match, it will print no. So how does this look in statement form? Here we see the graphic from the last slide turned into statements. We defined our two variables, A and B at the top. Then we create statements that specify what happens with the values. The first line in a condition begins with the keyword if and then outlines that condition. In this case, we're saying if values A and B match with the operator of two equal signs, then print the word yes. The second line begins with the keyword else because it outlines what happens when anything other than what is in the first line is true. The image on the right shows how these statements would look in idle. If else statements are very powerful and can check cases much more complicated than the example we just looked at. The example here, which you'll build in the exercises with Jacob in a few minutes, is a slightly more complicated conditional. Here we see three statements, one if, one else, and one elif, contracted from else if. If statements always start off the conditional, else statements always start off else statements conclude the conditional and elif statements fall in the middle. They provide in between cases to check for. This conditional checks someone's age and prints out a statement declaring what movies they can see based on ratings. Here are some resources you may be interested to explore. The resources packet in the Google Drive folder contains all of these links as well and many more. I hope you've enjoyed learning about the basics of Python and will stick around for our exercises. We designed two exercises to let you apply some of the things we've been talking about so far. My collaborator Jacob, an undergraduate digital humanities and social sciences student, recorded exercise one, which we'll start shortly in this same video. Exercise two is designed for you to try on your own. It highlights some specific applications of Python we did not talk about much, but the contents will hopefully feel familiar. In exercise two, you'll import a library to Python and build a random number generator. Exercise files are located in a folder called exercise files inside the workshop's Google Drive folder. You will need to download the files to use them within Idle. Exercise one will be done mostly in the Idle shell, but you may be interested in looking at the file at the same time. Thanks for joining us. Hello, everyone. Rather than just throwing an exercise at everyone first, we're going to go through a few basic Python exercises to make sure everyone understands the basics. Opening up Idle will open up a shell. So let's show on the right that you see right here is the Idle shell. I'm going to close out of this real quick. I'm going to open it up again. I have that just written under Idle. I'm going to open it up. It should already have been downloaded for you. So just type in an Idle, open it up. I'm going to put this over to my window over here. And that's over here. This Idle lets you sort of use like short snippets of code where you can just test basic functionalities that you can use to just test basic functionalities of a few basic Python functions. The first function we're going to go over is the print function used for both display and debugging purposes. The print function displays what it takes in as a parameter. We're going to test printing your name and remember that strings must be encapsulated in codes. I'm just going to print my first name. First and last name. You see the shell right now printed my name. And now that I've printed my name you should try printing your name. Once that is done we're going to start to work with variables. Variables are declared with a name and an equal sign. I'm going to create a variable titled name instead of equal to my first and last name. You should also do the same. Remembering that strings must be contained in quotes. Variables should start lowercase due to programming conventions. Really quickly I'm going to just make this variable. The shell will remember variables for the duration of the instance. So as long as the shell is open it will remember that you made this variable. It doesn't matter if you go to the next line. Great. So now I have this sort of variable. Now what do I do? Well we just showed that we can print our first name. First and last name. Up in the line above. Right over here. Well now I'm going to print that variable. And we're going to see what it gives us. Now notice that I didn't have to put the name variable into quotes because Python understands that name is a variable that I just created. Great. Another example of printing involves concatenating or adding string objects. Take the string instance. My name is. We can add the variable name after the string to complete a sentence using the addition operator. Leave a white space after the word is. As the print function will interpret there being a space there literally. The first part must be in quotes but the name variable must not be as that is a variable that is an object Python understands. Now see see that I left this white space I mentioned earlier. This white space will ensure that is isn't directly connected to name. This addition sign here tells Python to add this string to this string. Or the other way around. We will see that my name will pop right after this. There we go. We'll now move on to if and else statements. The gist of an if and else statement is that it'll run sequentially until it hits a statement that matches validity. If else statement contains at minimum an if statement and can contain both elif else if statements or else statements. Elif statements are between state between the first if and the last else. Take for example an if statement that checks which movies a teenager can view. The first if statement would check if the teenager is older than 17. And print a statement saying the teen can see all rate of movies. You can see this on this left open window that I have. You can see that I have a function which we'll go on with this later. It's called check it and it checks this parameter that is passing to it. We can ignore this function for now as we are more focused on this if and else statement down here. The first part of this if statement will check to see if the age being checked is greater than 17. This age is a variable that is thrown into here. If if the age is greater than greater or equal than to 17 it will print this statement that this person can see all rated can't that can see in our rate. If the age is between 17 and greater than 12 I mean they can see a rated P213 movie. Anything else the last the catch all statement see means that they can only see rated PG movies. Now we're going to move on to lists. Lists are created using square brackets. These don't have to be created empty but we're going to create an empty one just for testing purposes. I'm going to make a variable we're going to call it x equal to an empty list. So square brackets. This just created an empty list in Python but this is just an empty list doesn't have anything. So to add to this list I'm going to use the append function. Let's append a bunch of numbers to this list. The append function is used by calling the list in this case the x variable or the object in this case and using the function on it. So x period append parentheses one this will append the number one to this list. I can now print this not this list yes this list I can now print this list to see what isn't. You can see that this list contains number one. I'm now going to add a next few numbers to it. I will add two I will add three and I will add four. We can now see this list contains four objects. Sorry that was wrong. Can these numbers one, two, three and four? In other coding languages you would have to use a four loop to print this list. In this case Python doesn't automatically for us but a four loop is still very useful function and I will use this list here to show what a four loop is. Four loops are traditionally used when you have a block of code which you want to repeat a fixed number of times. And so you want the entire list to be printed we will iterate through it. So four this is how all four loops start four elements in x this is a honor here and hitting enter will not actually create well actually finish this command. I'll jump to this next line and wait for me to finish it. I am going to print the elements and I go to the next line and it will do it for me. This printed sequentially each number in this list. Now I made a mistake here I called this ID holder element so in this case it should be called element. This can be called whatever you wish I could call it for e in x print all print e and it will still do the same. Yes I accidentally made the mistake of capitalizing the x and python yeah let me for that I did exactly the same. The second part of the four loop the word that comes right after four doesn't matter what you name it but it should be named something that you will remember. Give it a descriptive name because you don't want to remember this. This goes the same way for four loops. Let's jump into a short example that hopefully won't be too large and we'll enforce a few ideas a few of these concepts so you can see how lists and four loops work. The idea for this small exercise is to create two lists compare them for similarities and create a third list with any matches removing any duplicates. So we're going to start by creating those lists. I'm going to make a first list called a and I'm going to populate it with a few numbers. I'm going to copy it with 1, 1, 2, 3, 5 and 8 just to be simple these random numbers. So I'm going to make another list and I'm going to call it and I'm going to just fill it up with the next sequential which is sequential numbers from 5 to say just because I like 10 10 is a random number and I'm going to make a third list. This list is going to be empty it's going to be the list that we're going to put any of these matches in together. Now that python has these three lists we're going to see if anything in A matches in B and we're going to use a four loop to pull matches. I'm going to write it the loop burst and then we're going to go through it step by step. So bear with me just for a little while. So for item in A remember that this doesn't matter whatever this word is it doesn't matter it's for me to remember what it all is. So I'm going to call the numbers and then item for any item in A I'm going to go to the next line we're going to check if if the item is in B because that is where the matches want to happen if the if item not in A in C I mean as we want to remove any duplicates add that item to C. Now if I print C I should see that the numbers contained will be one two three five and eight with no one duplicates that is exactly what we get. This is it might look a little long but I promise it's pretty simple. So start from the beginning we're using a for loop to make sure that we get to every single item in these lists every single number every single object in these lists lists can contain more than just numbers they can contain strings they can contain other objects they can contain really whatever they want. A and B don't have to be the same length they don't have to contain the same numbers they can literally contain whatever they want but we're using numbers in this case just to be simple so for every item in A the first line of the for loop we're going to check if that item is in B because that's what we care about these are the matches that are happening and then further on to that that third line we're going to check if that item is in C but not really in C we're going to check if it's not in C because we don't want any duplicates so we know that list A contains one one so two ones but we don't want two ones shown up in that list so we're going to say if it's going to check if the item is not already in C and if it passes all of these checks we're going to add that item using the append function okay so after that little exercise we're going to jump into functions we saw functions earlier when we went over the if statement right here but we're going to explain that more a function is a block of code which only runs when it is called you can pass data known as parameters into a function we're going to take the previous age movie if health statement and make it a function functions in python are defined using the def keyword let's create a check age function with an age parameter so right up here we're going to recreate this function in python and make sure that it works so i'm going to start by the def keyword now i'm going to want to call this function something i remember that's very important a lot of a lot of beginning programmers have no real regard for what they call functions but you're going to want to remember these functions especially when you create code that has thousands of lines now we have five 10 20 lines of code maybe but when you start working on codes or projects that have thousands of lines you're not you're not going to remember what you call what function x y z 1 2 3 means you want to give them a descriptive name so i'm going to call this check age and i'm going to give it a parameter that i will just call age because that's what you're really passed as we're checking that age is the variable object that's being checked in this case and i'm going to tap it off with a and move on to the next line because that's what opens up the function and tells python that this is a function this is what we're we're going to put into this function now i want this function to check how old this person is and tell me if this person can see this movie or not so the first line is going to be that first if statement that we saw so right here it's going to check if the age is greater than or equal to 17 so if the age is greater than or equal to 17 we're going to tell it to print that this person can see all movies okay that's wrong can see radar or movies because there is actually waiting above that now that if statement is done i'm going to go back a bit and i'm going to make an LF statement remember that the LF statement is the go between say between if and the else if is where it starts else is the last case that it ends but we also we want to check if the person can see PG-13 movies so that's like the middle case so if this person or if the age is less than 17 and because both of these we need both of these conditions to be true greater than the 12 not greater than or equal to 12 because technically 12 year olds can see PG-13 movies we are going to print that this person can see rated PG-13 movies and because we're at the last case we don't even need to use an LF statement we're going to use an else statement it's the catch all it's the last thing that happens if the program goes to this if statement and doesn't find any of these matching cases doesn't need any parameters that go with it so if it doesn't find anything so if the person is below 17 and below 12 it's going to print that this person can only see see rated PG movies now these aren't always the perfect descriptive terms these aren't mutually exclusive if you see an R if you can see a rated R movie you can of course see PG-13 movies but I'm just you know going through this for an exercise sake now that taps off the function nothing happened right I mean we just created the function now but we want to test it of course we want to test it to test functions we're going to call it now we called this check age so we're going to write out check age and we're going to give it a number we're going to first check we're going to start from the top we're going to check any number greater than 17 so we're going to check 21 and see what it gives us see this person can see rated R movies and the next case that we check is going to be 17 that's that's called an edge case it is the point between the first if statement and the elif statement so we're going to check the age of 17 and see what it gives us it should give us that this person can see rated R movies and it does the next case will be 15 between 17 and 12 it should tell us that this person can see rated PG-13 movies and it does the next case that we check will be 12 as that is the edge case between the second or the first elif statement and the elf statement this should tell us that this person can only see PG movies as it is as that person is not technically in the PG-13 range and it does and now we're going to check for anything less than 12 as that is the last possible condition that it can't be so let's take a 10 year old this person can see PG movies now for laughs we're going to see what happens if I check if someone is negative 20 years old it does tell us that this person can see negative can see PG movies but it's ridiculous for someone to be negative 20 years old this can be solved by more checks but we will not go over this here right now thank you for joining me for the exercise