- "text": "Open In Colab\n\n\ntitle: “Lesson 1 – Basic Introduction to Programming in Python: Operators and Data Types” jupyter: python3 format: live-html\n\nLearning objectives\nStudents will gain an introduction to programming in python, working in interactive notebooks, and learning how to leverage outside resources for coding help. Moreoever students will gain a basic understanding of variables, data types, and working with simple expressions for comparison and computation.\n\nBasic math operators: +, -, *, /, **\nUse of the assignment operator (=)\nData types: integer, float, string, boolean\nBasic functions: print(), type(), int(), float(), str(), bool()\nComparison operators: >, <, ==, >=, <=\nIn Class Exercises\n\n\nBasic Introduction to Programming in Python \nPython is a general purpose programming language that is widely used in scientific computing, image analysis, machine learning etc. It allows you to specify a set of instructions, written as a script or a program, to execute some task of interest.\n\nHave any of you used a script before? What did you do to run it? What was the purpose of the script? Did it aid in reproducibility? Was it quicker to run or modify than calculating things individually by hand?\n\nThe purpose of this series of python workshops is not to give you an extensive in depth overview of everything you can do in python. However, we do aim to give you all the skills and terminology necessary to learn how to learn to code in python. Google, stack overflow, and github issues) are invaluable tools you can use to your advantage for (1) gaining coding help and (2) learning how to write code from reading code.\n\n\nIntroduction to Jupyter Notebooks and Google Colab \n“The Jupyter Notebook is an incredibly powerful tool for interactively developing and presenting data science projects. A notebook integrates code and its output into a single document that combines visualizations, narrative text, mathematical equations, and other rich media. The intuitive workflow promotes iterative and rapid development, making notebooks an increasingly popular choice at the heart of contemporary data science, analysis, and increasingly science at large.” - dataquest\nAll of our lessons will be presented in Jupyter notebooks due to their interactive nature (.ipynb file extension). They consist of two main attributes, a kernel and cells. * A kernel interprets and executes the code. Here we are using the kernel for python; however, you can specify a kernel for another language like R. * A cell is a container for either text or code to be executed.\nTo run the python code in a cell, you just hit shift + enter. Try it with the code below.\n\nprint('hello world')\n\n\n\nBasic math operators: +, -, *, /, ** \nThe simplest way we can use python is to use is as a calculator! You can use python to do addition, subtraction, division, multiplication, and exponentiation. Let’s walk through some simple math calculations.\n\\[ 2 + 3 \\]\n\\[ \\frac{6}{2} * 20 - 100 \\]\n\\[ 2^{8} \\]\n\nWas the above result what you expected? If you used ^ then your result was 10; however, if you used ** then your result was 256. This demonstrates why it’s important to check the answers you get from your code to make sure the results seem reasonable. You might not always get an error when your code is incorrect :)\n\n\n\nUse of the assignment operator (=) \nWhat if you want to store some value (like the result of the above calculation) and use it for another task later? Here, you can assign it to a variable with the assignment operator =.\nFor example, let’s assign the value 22 to variable x as,\n\nx = 22\n\nNotice that the code above did not return any result. That’s because it is now stored in the variable called x!\n\nx\n\nTry assigning the values 1, 2, and 3 to the variables x, y, and z respectively.\nIf you’d like to see what the values of the variables were, you can use the print() function. Here, the input of the function (what goes inside the parantheses) is whatever you’d like to display to the terminal.\n\nprint(x)\nprint(y)\nprint(z)\n\nOnce you have variables assigned, you can use them to do basic operations. For example, try calculating the following. Be sure to use the assingment operator when necessary and print your results to check that they make sense.\n\nAssign the variable a as the result of \\(4 + 5*2\\)\nCompute \\(5 + a\\)\nAssign the variable b as the result of \\(\\frac{a}{2.5}\\)\n\nOf note, you can name a variable anything you’d like (although it’s best to be descriptive for you and others who will end up reading your code). However, a variable name must start with a letter and not a number. For fun, try assigning the value 3 as the variable 1variable and see what the result is.\n\n\nData types: integer, float, string, boolean \nProgramming languages often include different data types that can be used for different tasks (e.g. arithmetic, comparison, debugging etc.). Here we’ll discuss four of the most common types: 1. integer: whole number 2. float: decimal number 3. string: text 4. boolean: True or False\nIntegers \nIntegers are positive or negative whole numbers with no decimals. We’ve already been using them it today’s lesson. For example, print the variables x, y, z, and a.\nFloats \nFloats, short for floating point real values, are positive or negative numbers with decimals. As with integers, you can use them to do arithmetic or assign them to variables. Try defining and printing the following expressions.\n\nf1 assigned to value 4.5\nf2 assigned to value -3.0\nf1 + f2\n\nStrings \nStrings refer to text (ie they’re a series of characters) and can be (1) assigned to variables for data manipulation or (2) can be used in print statements for debugging or monitoring tasks. You define them by placing the text within single or double quotation marks as,\n\noligo1 = 'GCGCTCAAT'\noligo2 = 'TACTAGGCA'\nprint(oligo1, oligo2)\nprint('string variable 1 is', oligo1)\n\nBooleans \nBooleans are either True or False. Note the capitilization of the first letter. They are often used for comparisons (e.g. the output of \\(1 > 2\\) would result in False). Additionally, it’s important to note that True behaves as 1 and False behaves as 0 in arithmetic operations.\n\nt = True\nf = False\n\nprint(2*t)\nprint(2*f)\n\n\n\nComparison operators: >, <, ==, >=, <= \nComparison operators (>, <, ==, >=, <=) can be used to compare two or more variables. The output of the comparison is a Boolean (ie True or False). Therefore, we often use comparison operators to set up condition statements (e.g. if this comparison statement is True, then do this task. If it is False, then do something else). You’ll learn more about condition statements and control in Lesson 2.\nLet’s set up a few comparisons and use print statements to see the results.\n\\[ 2 > 1.5 \\] \\[ 4 <= 2 \\] \\[ 5 == 5.0 \\] \\[ 6 >= 6 \\] \\[ 7.4 < 3 \\]\n\n\nBasic functions: type(), int(), float(), str(), bool() \nWe’ve already started using one basic function, print(). Each function has a goal and does a task. How it works is it takes whatever is specified within the parantheses (the arguments), performs the task, and then outputs the results. Functions are extraordinarily useful for reproducible code and you’ll learn more about them in Lesson 3.\nFor now, let’s explore some other basic functions. The function type() returns the data type of the argument you give it. See the example below.\n\ntype(2.1)\n\n\ntype('hello world')\n\n\ntype(1)\n\n\ntype(True)\n\nThere are other useful functions that let you specify the data type or coerce the data type into another. int(), float(), bool(), and str() let you specify data as an integer, float, boolean, or string. Let’s take a look at how this works for the int() and str() functions.\n\nint(2.1)\n\n\nstr(20)\n\n\n\nIn Class Exercises \nThe following exercises will help you understand the concepts taught in this lesson better.\n\nLike in algebra, parentheses can be used to specify the order of operations. What would you expect to be the result of the following expressions? (Try to predict the answer before checking your answers in python.) \\[1 + 3*3\\] \\[(1 + 3)*3\\] \\[(4*4) + 5\\] \\[1 > 3\\] \\[\"1\" > \"3\"\\] \\[14 >= 2*7\\] \\[0 == \\mathrm{False}\\] \\[\\mathrm{True} == 1^{0}\\] \\[\\mathrm{int}(\\mathrm{True}) == \\mathrm{str}(1^{0})\\] \\[\\mathrm{float(\"one\")}\\]\nCreate two new variables, comp_oligo1 and comp_oligo2, that are the complementary DNA sequences of oligo1 and oligo2 (hint: A <-> T and G <-> C)\nCompute the melting temperature of oligo1 and oligo2 using the following equation: \\[\\mathrm{T_{m}} = 2(\\mathrm{A} + \\mathrm{T}) + 4(\\mathrm{G} + \\mathrm{C})\\] where A, T, G, and C refer to the number of A, T, G, C nucleotides in the primer.\n\n\n#question 1\n\n\n#question 2\n\n\n#question 3",
0 commit comments