\n",
"\n",
"C++:\n",
"int add_one(int num){\n",
" return num + 1;\n",
"}\n",
"Assembly x64, Intel syntax:\n",
"add_one(int):\n",
" push rbp\n",
" mov rbp, rsp\n",
" mov DWORD PTR [rbp-4], edi\n",
" mov eax, DWORD PTR [rbp-4]\n",
" add eax, 1\n",
" pop rbp\n",
" ret\n",
"HEX: 554889E5897DFC8B45FC83C0015DC3\n",
"BIN: 10101010100100010001001111001011000100101111101111111000000000000000000000000000000000000000000000000000000000000000000\n",
"\n",
"C++:\n",
"int add_1(const int& num){\n",
" return num + 1;\n",
"}\n",
"add_1(int const&):\n",
" push rbp\n",
" mov rbp, rsp\n",
" mov QWORD PTR [rbp-8], rdi\n",
" mov rax, QWORD PTR [rbp-8]\n",
" mov eax, DWORD PTR [rax]\n",
" add eax, 1\n",
" pop rbp\n",
" ret\n",
"HEX: 554889E548897DF8488B45F88B0083C0015DC3\n",
"BIN: 1010101010010001000100111100101010010001000100101111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n",
"\n",
"C++:\n",
"void fun(){\n",
" int x = 111;\n",
" x += 222;\n",
"}\n",
"fun():\n",
" push rbp\n",
" mov rbp, rsp\n",
" mov DWORD PTR [rbp-4], 111\n",
" add DWORD PTR [rbp-4], 222\n",
" nop\n",
" pop rbp\n",
" ret\n",
"HEX: 554889E5C745FC6F0000008145FCDE000000905DC3 \n",
"BIN: 10101010100100010001001111001011100011101000101111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Your first line of code \n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# type in your first Python code below\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables, data types \n",
"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. \n",
"\n",
"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>\".\n",
"\n",
"Here is how to do it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"name = input(\"Please enter your name: \")\n",
"print(\"Hello \"+ name)\n",
"print(\"Hello\", name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Variables can be used to store numeric values (integers, floating-point numbers, complex numbers) as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 12\n",
"y = 89\n",
"\n",
"z = x*y\n",
"print(z)\n",
"\n",
"z = z/(x+y)\n",
"print(z)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, we can print a text, we can print a number; how about printing texts and numbers together?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number = 21\n",
"text = \"is twenty-one.\"\n",
"print(number, text) \n",
"print(number + text) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number = 21\n",
"text = \"Twenty-one is\"\n",
"print(text, number) \n",
"print(text + number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"number = 21\n",
"text = \"Twenty-one is\"\n",
"print(text, number) \n",
"print(text + str(number))\n",
"print(text + \" \" + str(number))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Can we also convert a string into a number? Only when the string represents a valid number! Try the code below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"age = input(\"Please enter your age: \")\n",
"name = input(\"Please enter your name: \")\n",
"x = int(age)\n",
"y = float(name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Practice**\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# write your own code here\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conditionals "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lists, loops "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lab: DNA, RNA, proteins "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}