lec1_intro_to_computers_and_programing
lec1_intro_to_computers_and_programing
Introductions to Computers
and Programs
GNBF5010
Instructor: Jessie Y. Huang
1
GNBF5010 Learning Outcomes
Upon completion of this course, students should be able to:
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.
5
Textbooks
6
Recommended books
Python for Biologists: A complete
programming course for beginners. By
Martin Jones. 2013. (Available online)
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
11
How to succeed in this course
2. Practice lots of programs
12
How to succeed in this course
3. Trace lots of code (computer programs)
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:
17
Hardware
• Input devices: keyboard, mouse, 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
RAM
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.
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.
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?
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.
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?')
35
IDLE Programming Environment
The IDLE simplifies the process of writing, executing, and testing
programs.
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
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)
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
romania_gold += 1
print(total_gold)
A. 74 then 74
B. 74 then 75
C. 74
D. 75
59
3. input function
60
READING
Chapter 1, Starting Out with Python, by Tony Gaddis.