Contents
Introduction
Your first line of code
Variables, data types
Conditionals
Lists, loops
Lab: DNA, RNA, proteins
Laptops and smartphones are essentially just pieces of circuits which communicate with each other by sending and receiving electronic signals. The only language they understand is binary which is composed of only two symbols, $1$ for "on" (usually one voltage) and $0$ for "off" (usually near ground potential zero voltage).
For the computers to be useful, we need to give them instructions on what operations they must do and when. Technically, these instructions need to be streams of raw binary code, the only language that the hardware can understand. Fortunately, there have been a large number of amazing people who came up with designs for compilers which help us translate human-readable instructions to machine code.
Examples of human-readable instructions (in high-level programming languages) to machine code (more details in c
folder):
C++: int add_one(int num){ return num + 1; } Assembly x64, Intel syntax: add_one(int): push rbp mov rbp, rsp mov DWORD PTR [rbp-4], edi mov eax, DWORD PTR [rbp-4] add eax, 1 pop rbp ret HEX: 554889E5897DFC8B45FC83C0015DC3 BIN: 10101010100100010001001111001011000100101111101111111000000000000000000000000000000000000000000000000000000000000000000 C++: int add_1(const int& num){ return num + 1; } add_1(int const&): push rbp mov rbp, rsp mov QWORD PTR [rbp-8], rdi mov rax, QWORD PTR [rbp-8] mov eax, DWORD PTR [rax] add eax, 1 pop rbp ret HEX: 554889E548897DF8488B45F88B0083C0015DC3 BIN: 1010101010010001000100111100101010010001000100101111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 C++: void fun(){ int x = 111; x += 222; } fun(): push rbp mov rbp, rsp mov DWORD PTR [rbp-4], 111 add DWORD PTR [rbp-4], 222 nop pop rbp ret HEX: 554889E5C745FC6F0000008145FCDE000000905DC3 BIN: 10101010100100010001001111001011100011101000101111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Today, we are going to learn how to write some high-level instructions in [Python](https://en.wikipedia.org/wiki/Python_%28programming_language%29) to tell the computer to perform some simple tasks for us.
One of the simplest tasks we want to tell the computer to do is to print (display) some text to the console. Go ahead and type print("Hello world!")
in the cell below, then click the "Run" button (at the top of this window) or press the key combination Shift+Enter
to see the result.
# type in your first Python code below
In Python or any other programming languagues, we can declare variables, store values in them so that we can retrieve and modify the values later in the program. This is convenient since we can refer to those values via the predefined variables without worrying too much about what the real values are.
Let's consider your first line of code above. What if we give the computer a name and ask it to customize the greeting with that specific name instead of just "Hello world!"? There are gazillions of names to choose from so it is not practical to write print("Hello Anna")
, print("Hello Bob")
, print("Hello Charlie")
,... What we should do is to ask the user for the name, save that value into a variable called name
, and ask the computer to print "Hello <name>".
Here is how to do it:
name = input("Please enter your name: ")
print("Hello "+ name)
print("Hello", name)
Variables can be used to store numeric values (integers, floating-point numbers, complex numbers) as well.
x = 12
y = 89
z = x*y
print(z)
z = z/(x+y)
print(z)
As you can see, we can print a text, we can print a number; how about printing texts and numbers together?
number = 21
text = "is twenty-one."
print(number, text)
print(number + text)
The error said "unsupported operand type(s) for +: 'int' and 'str'". str
means string, a list of character, or basically a text. int
means integer. Why did we get such error? Does it make sense to calculate the sum of a text and a number? What if we write print(text + number)
, perhaps Python is smart enough to figure out we want to append the number to the text?
number = 21
text = "Twenty-one is"
print(text, number)
print(text + number)
The error is now "must be str, not int". Here, we've got a clue; Python wanted a string, not an integer. That is reasonable since text
is a string and it can be combined only with another string. So how can we convert our number into a string? Here is how:
number = 21
text = "Twenty-one is"
print(text, number)
print(text + str(number))
print(text + " " + str(number))
Can we also convert a string into a number? Only when the string represents a valid number! Try the code below:
age = input("Please enter your age: ")
name = input("Please enter your name: ")
x = int(age)
y = float(name)
Practice
Now, let's write some code to calculate the birth year of the user. The code will first ask them for their age, then subtract their age from this year (2020) to find their birth year, and print out the result.
# write your own code here