0% found this document useful (0 votes)
3 views

lec1_intro_to_computers_and_programing

The document outlines the course GNBF5010, focusing on programming concepts, particularly in Python, for bioinformatics. It includes course logistics, evaluation criteria, recommended textbooks, and resources for learning Python. Additionally, it covers the basics of computers, programming languages, and provides guidance on how to succeed in the course.

Uploaded by

Michael Chu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lec1_intro_to_computers_and_programing

The document outlines the course GNBF5010, focusing on programming concepts, particularly in Python, for bioinformatics. It includes course logistics, evaluation criteria, recommended textbooks, and resources for learning Python. Additionally, it covers the basics of computers, programming languages, and provides guidance on how to succeed in the course.

Uploaded by

Michael Chu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Lecture 1

Introductions to Computers
and Programs
GNBF5010
Instructor: Jessie Y. Huang

1
GNBF5010 Learning Outcomes
Upon completion of this course, students should be able to:

• Master key programming concepts such data-types,


variables, conditions, loops and functions

• Understand fundamental concepts in object-oriented


programing
• Solve small to medium size programming tasks that arise in
bioinformatics
• Write well-structured and maintainable programs in
Python

2
Course Logistics
• Course Evaluation
• Assignments 20%
• Project 15%
• Midterm exam 25%
• Final exam 40%

3
Textbooks and Resources

4
Textbooks
A Primer for Computational Biology, by
Shawn T. O’Neil.

The practice of Computing Using Python,


by Punch and Enbody (Ebook available via
the CUHK library)

5
Textbooks

Python for Everyone, by Charles R.


Severance. (website)

Starting Out with Python, by Tony


Gaddis. 2021. Pearson. (Ebook available
via CUHK library)

6
Recommended books
Python for Biologists: A complete
programming course for beginners. By
Martin Jones. 2013. (Available online)

Python for Bioinformatics, 2nd edition,


by Sebastian Bassi. 2017.
• Covers Biopython
• Older version available in the CUHK
library

7
Online tutorials
• introtopython.org: A complete intro class written in
Python notebooks. Includes exercises.
• Google's Python Class: Online materials, videos, and
exercises from their two-day intro course.
• Guru99 Python tutorial
• Python for Biologists: introduction to Python focused
on tasks (and using examples) that are relevant to many
biologists
• Python Tutorial: the official Python tutorial. Think of it
more as reference material with lots of examples. It
teaches Python, not programming.

8
Resources for reference
• Python Language Reference: documentation of core Python
syntax.
• Python Standard Library Documentation: what all of the
built-in libraries do and how to use them.
• Python Quick Reference: useful set of scripts and notebooks
by topic to remember (or learn) how to do specific tasks in
Python.
• Python 101: notebook with basic examples. End of the
notebook gets into more advanced topics/examples, but a
useful collection of snippets for quick reminders on how to
do things.
• Python Basics Cheat Sheet: one page pdf with reminders of
basic string, list, and numpy array operations.

9
How to succeed in this course

10
How to succeed in this course
1. Practice things by yourself

Programming is similar to getting fit.

You cannot just watch. You have to do it yourself.

11
How to succeed in this course
2. Practice lots of programs

Why it is doing that when I want it to do not that?


(image credit: Pixabay)

12
How to succeed in this course
3. Trace lots of code (computer programs)

4. If you are unclear about some concepts, research


on your own first, then ask.

13
Today …
• Major components of a computer and how it works
• What is a programming language
• How to use the python interpreter
• Python basics

14
What is a computer and how it works

15
Computers

Blade Server

16
What is a computer?
An electronics device that can perform computations
and make logical decisions.
It includes:

• Hardware: physical electronic devices of which a


computer is made
• Software: programs that run on a computer

17
Hardware
• Input devices: keyboard, mouse, etc.

• CPU - Central Processing Unit

• RAM - Random Access Memory (Main Memory)

• Secondary storage devices

• Output devices: monitor, speaker, printer, etc.

18
Software = the computer programs
A computer program is a set of instructions that a
computer follows to perform a task.
Two categories of computer programs:
• System software: programs that control basic
operations of a computer, e.g., OS, compilers
• Application software: programs that make a computer
useful for everyday tasks; e.g. web browsers, MS Word,
etc.

19
How a computer program works

2. CPU sends request to


1. Instruction sent hard drive for program to
to the CPU. be loaded onto the RAM.
3. Hard drive loads program onto the RAM.

RAM

4. CPU now ready to


work with the program.

CPU

www.computerscienceuk.com 20
How computers store a number:
Binary number system
A number (0~255) is encoded by a binary number of 8 bits , or 1 byte.
Example: how computers store the number of 109.
Consider numerous
physical switches in
the RAM

Binary number: 0 1 1 0 1 1 0 1

Decimal Sum of 0x27 1x26 1x25 0x24 1x23 1x22 0x21 1x20
number: = 64 + 32 + 8 + 4 + 1
= 109
Binary number of 01101101 = Decimal number 109
21
How computers store a character
• A character is first converted to a numeric code, called ASCII
code.
• The numeric code is then stored as a binary number.

22
The ASCII Table
Example: The ASCII code for uppercase B is 66, for uppercase C is 67, and so forth.

23
Example
All information is digitized - broken down into pieces and
represented as numbers via ASCII table.

72 105 44 32 72 101 97 116 104 101 114 46

24
What is a computer program language
(or programing language)?

25
What is a programming language
A vocabulary and set of grammatical rules for
instructing a computer or computing device to
perform specific tasks.

• The term programming language usually refers to


high-level languages, such as BASIC, C, C++, Java,
FORTRAN, Python and Perl.

Source: webopedia.com
26
The Top Programming Languages

27
https://spectrum.ieee.org
Aspects of a programming language
• Syntax: a set of rules that must be strictly followed
when writing a program.
• e.g. print(“Hello”) (correct)
Print[“Hello”] (syntax error)
• Statements: individual instructions used to write a
program.
• e.g. print(“Hello world!”)
• Source code: The statements that a programmer
written in a high-level language
• Usually saved in plain text files.

28
Aspects of a programming language
• Keywords

• Operators
• e.g. +, -, *, /, etc.

29
Exercise
1. What is a program?
2. What part of the computer actually runs programs?
3. What part of the computer serves as a work area to store a
program and its data while the program is running?
4. What part of the computer holds data for long periods of time,
even when there is no power to the computer?

5. Each language has a set of rules that must be strictly followed


when writing a program. What is this set of rules called?
6. What type of mistake is usually caused by a misspelled key
word, a missing punctuation character, or the incorrect use of
an operator?

30
Using Python

31
Install Python
• Download and install the latest version for
Windows or MacOS
• https://www.python.org/downloads/

32
Using Python
• The Python interpreter
• can run Python programs that are saved in files, or
• interactively execute Python statements that are typed
at the keyboard.

• IDLE (Integrated DeveLopment Environment)


• IDLE comes with the Python installation pacakge
• IDLE simplifies the process of writing, executing, and
testing programs.
• Alternatives to IDLE: Atom, PyCharm, Vim, Spyder, etc.

33
The Python interpreter – Interactive mode
When the Python interpreter is running in interactive mode, it is
commonly called the Python shell.
When the Python interpreter starts in interactive
mode, you will see this in a console window:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47)
[MSC v.1914 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>

Let’s try:

34
The Python interpreter – Script mode
If you want to save your python statements as a program, you
save those statements in a file and run it with the interpreter.
Example:
Save the following statements in a plain text file, called
“test.py”.
print('Nudge nudge')
print('Wink wink')
print('Know what I mean?')

To run the program, go to the directory where the file was


saved and type the following command in command line:
python test.py

We also use script mode in Linux for GNBF5030.

35
IDLE Programming Environment
The IDLE simplifies the process of writing, executing, and testing
programs.

We will use IDLE in Windows/MacOS for purpose of learning python programming.

36
Python Basics

37
Python programs

38
Data objects in python

39
Types of scalable objects

40
Type conversions

41
Expressions

42
Operators on int and floats

43
Binding variables and values

44
Variable naming rules
▪ cannot be a Python keyword
▪ cannot contain spaces
▪ first character must be a letter or an underscore (_)
▪ after first character, may use letters, digits, or
underscores
▪ case sensitive

▪ Examples: x1, X1, pre_val, a2b

45
Abstracting expressions

46
Programming vs math

47
Changing bindings

48
String Manipulation

49
STRINGS
▪ may include letters, special characters, spaces, digits
▪ must be enclosed in quotation marks
▪ can be compared with ==, >, <, etc.
▪ can be concatenated: 'ab'+'cd’ or 'abc'*3
▪len() is a function used to retrieve the length of a
string:
s = "abc"
len(s) # evaluates to 3

50
OUTPUT STRINGS: print()

x = 1
print(x)
x_str = str(x)
print("my fav num is", x, ".", "x =", x)
print("my fav num is " + x_str + ". " + "x = " + x_str)
print(f"my fav num is {x}. x={x}") # f-string
52
53
54
STRING INDEXING
▪ use square brackets to perform indexing into a string
to get the value at a certain index/position
s = "abc"
0 1 2  indexing always starts at 0 and ends at (len-1)
-3 -2 -1  or use negative index: the last element is at index (-1)

s[0] → evaluates to "a"


s[1] → evaluates to "b"
s[2] → evaluates to "c"
s[3] → trying to index out of bounds, error
s[-1] → evaluates to "c"
s[-2] → evaluates to "b"
s[-3] → evaluates to "a"
46
STRING SLICING
▪ strings can be sliced using [start:stop:step]
▪ if two numbers given, [start:stop], step=1 by default
▪ the numbers can be omitted and leave just colons
s = "abcdefgh"
s[3:6] → evaluates to "def", same as s[3:6:1]
s[3:6:2] → evaluates to "df"

s[::] → evaluates to "abcdefgh", same as s[0:len(s):1]


s[::-1] → evaluates to "hgfedbca", same as s[-1:-(len(s)+1):-1]

56
IMMUTABILITY
▪ strings are “immutable” – cannot be modified
s = "hello"
s[0] = 'y' → gives an error
s = 'y'+s[1:len(s)] → is allowed,
s bound to new object

"hello"

"yello"

57
Exercise
1. Python vs. Math
Which of the following statements is allowed in
Python?
A. x + y = 2
B. x * x = 2
C. 2 = x
D. xy = 2
E. None of the above

58
2. Bindings
Trace the code and predict what will be printed.
usa_gold = 46
uk_gold = 27
romania_gold = 1

total_gold = usa_gold + uk_gold + romania_gold


print(total_gold)

romania_gold += 1
print(total_gold)

A. 74 then 74
B. 74 then 75
C. 74
D. 75
59
3. input function

Suppose you need the user of a program to enter a


customer’s last name. Write a statement that
prompts the user to enter this data and assigns the
input to a variable.

60
READING
Chapter 1, Starting Out with Python, by Tony Gaddis.

You might also like