Part 1. Introduction to Python

[Pages:11]Part 1. Introduction to Python

In this exercise, we will cover some basic concepts in Python while introducing Jupyter Notebook, the Python interpreter, text editors, and Spyder IDE.

Jupyter Notebook

We will use Jupyter Notebook to demonstrate some of the basics of Python programming. Jupyter Notebook provides an interactive user friendly way of writing, testing, and documenting code.

The print() function

For our first code, we will have Python output a message using the print() function (to execute code in the code boxes, type shift + return):

In [1]: print("Hello, world!") Hello, world!

Python is very particular about syntax. Try removing the parentheses:

In [3]: print Hello, world! File "", line 1 print Hello, world! ^

SyntaxError: Missing parentheses in call to 'print'. Did you mean prin t(Hello, world!)?

Try removing the quotes:

In [4]: print(Hello, world!) File "", line 1 print(Hello, world!) ^

SyntaxError: invalid syntax

Try changing the double quotes to single quotes:

In [5]: print('Hello, world!') Hello, world!

If we want the message to span multiple lines, we can use triple quotes:

In [8]: print("""Hello, world!""")

Hello, world!

The Python interpreter

Open a terminal and type python --version at the prompt ( $ ). If your default version of python is 2.x, and you've installed python version 3.x, you can invoke it by typing python3 .

At the prompt ( >>> ), use the print function to print the phrase "I love Python!".

Python scripts

The Jupyter notebook and the Python intepretor are great for writing and testing snippets of code, but when we want to actually create a Python program we write the instructions in a file, often called a script, using text editing software (gedit, TextWrangler, Notepad++, emacs, etc) or an IDE (Spyder, PyCharm).

Building blocks of a script

Examine the building_blocks.py script available in the scripts link on the course website. Note some of the common components of a Python script.

Exercise 1a

Open a text editor and create a new document. Save the document as ex1a.py in a new folder called Scripts or some other descriptive name. The .py extension is the conventional way of designating a file as a python script but it is not necessary to execute the script for Unix-based operating systems. Everything in the file will be intepreted as code, unless it's commented out with a # . Write a Python script that prints the message "My first Python script!". The text editing software you use should color the code to make it easier to read. Adding the .py extension should be sufficient to trigger syntax coloring within the file. To execute the script from the command line, type python (or python3 , if Python v2 is the default) followed by the name of the script (e.g. python3 ex1a.py ).

Math

Python has 7 arithmetic operators: + , - , \* , / , ** (to the power of), % (modulus), // (floor division). Python does math in a fairly intuitive way. For example, what is 7 minus 3?

In [9]: 7-3 Out[9]: 4

What is 1 divided by 2?

In [10]: 1/2 Out[10]: 0.5

What is 2 times 3?

In [12]: 2*3 Out[12]: 6

What is 2 cubed ('to the power of' uses the syntax ** )?

In [13]: 2**3 Out[13]: 8

Try some more complex math. Does Python follow conventional rules for presedence of mathamatical operations?

In [14]: 9-3*3 Out[14]: 0

In math, we often work with variables, such as x and y. Try assigning a value to a variable x , just like you would if you were doing an algebra problem.:

In [15]: x = 5

Now use the variable in an equation:

In [16]: x + 10 Out[16]: 15

Try returning the value of the variable using the print() function:

In [17]: print("x") x

If you followed the syntax used for the the print() function in the earlier example, you probably got as output exactly what you entered. What happens if you remove the quotes?

In [18]: print(x) 5

Try returning the value of a math operation using the print() function. Test what happens when you include or exclude quotes:

In [19]: print("5+10") 5+10

In [20]: print(5+10) 15

Exercise 1b

Write a script in Spyder, which can be accessed via Anaconda-Navigator, called ex1b.py that does the following:

1. Stores a number to a variable. 2. Calculates the square root of the variable and stores it as a new variable. 3. Prints the result to the terminal.

The script above should have three lines of code. How can you condense it down to two lines of code while obtaining the same output? How can you condense it down to one line of code while obtaining the same output? Which is best?

In [ ]:

Part 2. Values, variables, expressions, and statements

Values

We have now seen examples of two types of values: integers and strings. As demonstrated using the print() function, these two types of values are interpreted differently. Strings, which are just sequences of characters, such as Hello, world! , have the designation str . Integers, which are of course whole numbers, are one type of numerical value of type int . Floating-point numbers (numbers containing a decimal point) belong to a second type called float . Numbers, both int and float type, can be treated as strings but strings cannot be treated as numbers, as we demonstrated using the print() function.

Variables

Variables can be assigned numbers (either int or float ) or strings ( str ). For example, we can assign a sequence of As, Cs, Ts, and Gs to a variable seq as follows (recall that variables are assigned with the syntax variable_name = value ):

In [ ]: seq = 'ATGAGCC'

Because we are assigning a string to the variable dna , as with the print() function, the value has to be in quotes. What if we were assigning a number to the variable seq ?

In [22]: seq = '5' seq + 5

----------------------------------------------------------------------

-----

TypeError

Traceback (most recent call

last)

in ()

1 seq = '5'

----> 2 seq + 5

TypeError: must be str, not int

Things start to get a little bit tricky. If we include quotes around a number, than it becomes a string and a string no longer has a numerical value. So even though the variable seq may appear to be a number, it will depend on if it was assigned with or without quotes.

When assinging a number without quotes to a variable, if we include a decimal point, Python will assign float as the type by default, but if we exlude a decimal point, by deault it will be an int .

In [23]: n1 = 5.0 n2 = 5

The type() function allows us to identify the type of an object, such as a variable:

In [26]: type(n1) Out[26]: float

Assign a number to a variable without quotes:

In [28]: n = 5

Use the variable in a math operation:

In [29]: n + 5 Out[29]: 10

Now try assigning a number to a variable with quotes:

In [30]: n = "5"

Try multiplying the variable by 5 (the result may surprise you):

In [32]: n * 5 Out[32]: '55555'

Commas are not permissible in numbers. For example, 1,000 will not mean what you expect it to:

In [33]: n = 1,000 + 5 print(n) (1, 5)

This is an example of a semantic error - valid code that doesn't do what you intended it to do. In contrast, as we've already seen many times, sytax errors are generated by invalid code and produce an error message.

Variable naming conventions

Variable names can be just about anything in Python but they cannot start with a number or have spaces or special characters (underscores are ok). It is best to give variables descriptive names and use lowercase letters, in particular the first letter should be lowercase (also note that variable names are case sensitive so rrna is not the same as rRNA). And although it is permissible, it is not wise to give a variable the same name as a function (such as print ) and Python has ~30 special kewords that are off limits:

False None True and as assert except

class continue def del elif else in

finally for from global if import raise

is lambda nonlocal not or pass

return try while with yield break

It is best to use variable names that are descriptive to make the code more interpretable (but be careful to avoid keywords and function names):

In [ ]: x = 'ATGTGTCA' # not very informative seq = 'ATGTGTCA' # informative at least to a biologist seq1 = 'ATGTGTCA' # may be useful in distinguising multiple seqeuence-based v 1seq = 'ATGTGTCA' # not permissable because a variable name can't start with SEQ = 'ATGTGTCA' # permissable but not advisable becasue at least the first l seq new = 'ATGTGTCA' # not permissable because a variable names can't contain seq-new = 'ATGTGTCA' # not permissable because special characters are not all seq_new = 'ATGTGTCA' # use underscores in place of spaces class = 'DNA' # not allowable as class is a keyword max = 30 # permissable but not advisable as max is the name of a function whi min = 10 # permissable but not advisable as min is also the name of a functio

Statements

In Python, a statement is any code that can be executed. 1+1 is a statement. dna = 'ATGCC' is also a statement. Statements can be thought of as any action or command:

In [34]: print("zizzer zazzer zuz")

zizzer zazzer zuz

Expressions

An expression is something that represents a value or is a value. It can be a single value, a combination of values, variable, and operators, and calls to functions as long as it boils down to a single value. Expressions are things that need to be evaluated. As we saw on Tuesday, in interactive mode, the value of expressions are output upon hitting enter but in a script, they are not. Any section of code that evaluates to a value within a statment is an expression. Python programs are series of statements in a sequential order:

In [ ]: num1 = 5 # Assignment statement num2 = 7 # Assignment statement num3 = num1 + num2 # Assignment with expression print(num3) # Print statement

Arithmetic Operators

On Tuesday, we introduced the 7 arithmetic operators: + , - , * , / , ** (to the power of), % (modulus), // (floor division).

Recall that the modulus operator yields the remainder of a division. What is the remainder of 11/3?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download