• Home
  • The University of British Columbia
  • Plant Genetics
  • Introduction to Programming in Python

Introduction to Programming in Python

Start up of Programming in Python Part 0 Start up of Programming in Python Part 0. 1 Start up 0-1 Using the Python Console as a calculator 1 Introducing memory 2 1 Experiments with variables 2 Types and operators. 3 More on operators 4 Mathematical functions 6 Start-up 0-2 7 Introducing textual data 7 Special characters in strings 8 The string type (str) 8 Simple input and output 9 Methods of type str 10 Start up 0-1 This is an introduction to the Course programming in Python. All assignments which will be handled during the PC-labs and Class Exercises will be found in files like these. Separate files will be available for the homework. Before we can start making assignments we will first have to prepare PyCharm, please follow follow the instructions in document GettingStartedWithPyCharm.pdf (on Brightspace) before continuing here. Below, we introduce Python and PyCharm further. Using the Python Console as a calculator At the prompt of the Python Console (in PyCharm started by Tools > Python Console ... ), you can type arithmetic expressions, e.g. (-3.0 + 8.25) - (1 + 3) The console shows the answer at the next line, with Out[2], Out[3] etc. in front of it. Try some expressions. Use * for multiplication, ** for exponentiation, and / for division. After completing an expression, use the Enter key to let Python compute its value. [Note: In mathematics, we may write two parts of a formula next to one another to indicate multiplication. In computer programming, multiplication must always be indicated explicitly.] Try some more expressions. Also see what happens if you make an error in an expression. If you really don't know how to return to the Python prompt, you may always break off the formula by means of Ctrl+C. [We use the words "formula" and "expression" as synonyms.] When typing a long formula, it is frustrating to find you made a small error and have to retype the complete formula again. Fortunately, it is not necessary to retype everything. Introduction Programming in Python 1 INF - 2019-08-20 By arrow key Up you can retrieve the last formula. When you press Enter immediately after arrow key Up, Python calculates the formula again. But you can change the formula before pressing Enter: move through the formula by means of arrow keys Left and Right; remove characters by means of the keys Backspace and Delete; and insert characters with the normal character keys - like you would in a word processor. You can press the Enter key at any point to calculate the changed formula. Also try what happens when you use arrow key Up repeatedly, or after typing the first few characters of a previous expression. Introducing memory On a real pocket calculator, you can continue calculating with the outcome of the previous calculation. In interactive Python, you may recall the last interactive answer by means of the special symbol "_" (just one underscore character; the quotes are not part of the symbol). On its own,